rmath3d 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fa4b132536ee9bcbe0c7da9e51fe53bd566010e
4
- data.tar.gz: 17eee04a0ac050c85fbc1953d7302ff7f41e1d06
3
+ metadata.gz: cb1e762ea46c80e060d6efdcaf6db861e6ad2ffc
4
+ data.tar.gz: c4cbac65664fd3c54d08ca66dd9e207a9f3384b0
5
5
  SHA512:
6
- metadata.gz: 9bfefb8f3a0a41f88c8e25fa3074d1b45a05a2be87085c6759029b7566362188c65d41261254b7afacaab22c1fdc221b2e50bccac6097ca36e2e42089cc167d6
7
- data.tar.gz: ac604ce99ae372569cdf291ae09166dc4c67f8d9e86f970bfe6da7b2967ebe6e57ac652dcd0f1a07f222d9801a348df8d5372c6dc5d6d395ea291dfc9d08ec5c
6
+ metadata.gz: bf14364015645a9631a9e57c6c54279a93090f2bb8ed7470bfd75795e04cfdb2ca519aceaaeaf9f10d19caf5909b1e7a71645cd4237bd563236dc788c93b8081
7
+ data.tar.gz: 158e3a7d837715897afadf073c644aa10fa7dbd3b9fc735bdf6c1d5bbd029f6637fb82e0731d9806109bc9c7122f39d7ac8113fe84a01a681956e5854f52f8bd
data/ChangeLog CHANGED
@@ -1,3 +1,7 @@
1
+ 2015-04-12 vaiorabbit <http://twitter.com/vaiorabbit>
2
+
3
+ * RVec2.c|h, RMtx2.c|h: Added.
4
+
1
5
  2014-05-17 vaiorabbit <http://twitter.com/vaiorabbit>
2
6
 
3
7
  * ext/rmath3d/rmath3d.c: Fixed memory management
@@ -1,5 +1,5 @@
1
1
  rmath3d : Ruby math module for 3D Applications
2
- Copyright (c) 2008-2014 vaiorabbit <http://twitter.com/vaiorabbit>
2
+ Copyright (c) 2008-2015 vaiorabbit <http://twitter.com/vaiorabbit>
3
3
 
4
4
  This software is provided 'as-is', without any express or implied
5
5
  warranty. In no event will the authors be held liable for any damages
data/README.md CHANGED
@@ -4,18 +4,20 @@
4
4
 
5
5
  rmath3d is a math module for 3D game programming and computer graphics.
6
6
 
7
- * Last Update: May 17, 2014
7
+ * Last Update: Apr 12, 2015
8
8
  * Since: Jul 20, 2008
9
9
 
10
10
  ## Features ##
11
11
 
12
12
  ### Supports frequently-used vector and matrix classes ###
13
13
 
14
+ * RMtx2 (2x2 matrix)
14
15
  * RMtx3 (3x3 matrix)
15
16
  * RMtx4 (4x4 matrix)
16
17
  * RQuat (Quaternion)
17
- * RVec3 (3 element vector)
18
- * RVec4 (4 element vector)
18
+ * RVec2 (2-element vector)
19
+ * RVec3 (3-element vector)
20
+ * RVec4 (4-element vector)
19
21
 
20
22
  ### Two implementations that are interchangeable with each other ###
21
23
 
@@ -2,9 +2,11 @@
2
2
  #define RMATH_H_INCLUDED
3
3
 
4
4
  #include "RType.h"
5
+ #include "RVec2.h"
5
6
  #include "RVec3.h"
6
7
  #include "RVec4.h"
7
8
  #include "RQuat.h"
9
+ #include "RMtx2.h"
8
10
  #include "RMtx3.h"
9
11
  #include "RMtx4.h"
10
12
 
@@ -0,0 +1,255 @@
1
+ #include <math.h>
2
+ #include <string.h>
3
+
4
+ #include "RVec2.h"
5
+ #include "RMtx2.h"
6
+
7
+ /* NOTE : column-major */
8
+ #define SET_ELEMENT(out, row, col, val) (out)->e[(col)*2+(row)] = (val)
9
+ #define GET_ELEMENT(in, row, col) ((in)->e[(col)*2+(row)])
10
+
11
+ void
12
+ RMtx2SetElement( RMtx2* out, int row, int col, rmReal e )
13
+ {
14
+ SET_ELEMENT( out, row, col, e );
15
+ }
16
+
17
+ rmReal
18
+ RMtx2GetElement( const RMtx2* in, int row, int col )
19
+ {
20
+ return GET_ELEMENT( in, row, col );
21
+ }
22
+
23
+ void
24
+ RMtx2GetRow( RVec2* out, const RMtx2* in, int row )
25
+ {
26
+ RVec2SetX( out, GET_ELEMENT( in, row, 0 ) );
27
+ RVec2SetY( out, GET_ELEMENT( in, row, 1 ) );
28
+ }
29
+
30
+ void
31
+ RMtx2GetColumn( RVec2* out, const RMtx2* in, int col )
32
+ {
33
+ RVec2SetX( out, GET_ELEMENT( in, 0, col ) );
34
+ RVec2SetY( out, GET_ELEMENT( in, 1, col ) );
35
+ }
36
+
37
+ void
38
+ RMtx2SetRow( RMtx2* out, const RVec2* in, int row )
39
+ {
40
+ SET_ELEMENT( out, row, 0, RVec2GetElement( in, 0 ) );
41
+ SET_ELEMENT( out, row, 1, RVec2GetElement( in, 1 ) );
42
+ }
43
+
44
+ void
45
+ RMtx2SetColumn( RMtx2* out, const RVec2* in, int col )
46
+ {
47
+ SET_ELEMENT( out, 0, col, RVec2GetElement( in, 0 ) );
48
+ SET_ELEMENT( out, 1, col, RVec2GetElement( in, 1 ) );
49
+ }
50
+
51
+ void
52
+ RMtx2Copy( RMtx2* out, const RMtx2* in )
53
+ {
54
+ memmove( out, in, sizeof(RMtx2) );
55
+ }
56
+
57
+ void
58
+ RMtx2SetElements( RMtx2* out,
59
+ rmReal e00, rmReal e01,
60
+ rmReal e10, rmReal e11
61
+ )
62
+ {
63
+ SET_ELEMENT( out, 0, 0, e00 );
64
+ SET_ELEMENT( out, 0, 1, e01 );
65
+
66
+ SET_ELEMENT( out, 1, 0, e10 );
67
+ SET_ELEMENT( out, 1, 1, e11 );
68
+ }
69
+
70
+ void
71
+ RMtx2Zero( RMtx2* out )
72
+ {
73
+ int row, col;
74
+ for ( row = 0; row < 2; ++row )
75
+ for ( col = 0; col < 2; ++col )
76
+ SET_ELEMENT( out, row, col, 0.0f );
77
+ }
78
+
79
+ void
80
+ RMtx2Identity( RMtx2* out )
81
+ {
82
+ int row, col;
83
+ for ( row = 0; row < 2; ++row )
84
+ for ( col = 0; col < 2; ++col )
85
+ SET_ELEMENT( out, row, col, (row==col) ? 1.0f : 0.0f );
86
+ }
87
+
88
+ rmReal
89
+ RMtx2Determinant( const RMtx2* in )
90
+ {
91
+ #define I( r, c ) GET_ELEMENT( in, (r), (c) )
92
+ #define D( e00, e01, e10, e11 ) ((e00)*(e11)-(e01)*(e10))
93
+
94
+ return D( I(0,0), I(0,1), I(1,0), I(1,1) );
95
+
96
+ #undef I
97
+ #undef D
98
+ }
99
+
100
+ void
101
+ RMtx2Transpose( RMtx2* out, const RMtx2* in )
102
+ {
103
+ int row, col;
104
+ RMtx2 tmp;
105
+ for ( row = 0; row < 2; ++row )
106
+ for ( col = 0; col < 2; ++col )
107
+ SET_ELEMENT( &tmp, row, col, GET_ELEMENT( in, col, row ) );
108
+
109
+ RMtx2Copy( out, &tmp );
110
+ }
111
+
112
+ rmReal
113
+ RMtx2Inverse( RMtx2* out, const RMtx2* in )
114
+ {
115
+ #define I( r, c ) GET_ELEMENT( in, (r), (c) )
116
+ #define R( r, c ) GET_ELEMENT( &result, (r), (c) )
117
+ #define D( e00, e01, e10, e11 ) ((e00)*(e11)-(e01)*(e10))
118
+
119
+ RMtx2 result;
120
+ rmReal det;
121
+
122
+ det = D( I(0,0), I(0,1), I(1,0), I(1,1) );
123
+
124
+ if ( rmFabs(det) < RMATH_TOLERANCE )
125
+ return det;
126
+
127
+ SET_ELEMENT( &result, 0, 0, I(1,1) );
128
+ SET_ELEMENT( &result, 0, 1, -I(0,1) );
129
+
130
+ SET_ELEMENT( &result, 1, 0, -I(1,0) );
131
+ SET_ELEMENT( &result, 1, 1, I(0,0) );
132
+
133
+ RMtx2Scale( out, &result, 1.0f / det );
134
+
135
+ return det;
136
+
137
+ #undef I
138
+ #undef R
139
+ #undef D
140
+ }
141
+
142
+
143
+ /* http://en.wikipedia.org/wiki/Rotation_representation_%28mathematics%29
144
+ http://en.wikipedia.org/wiki/Rotation_matrix
145
+ */
146
+ void
147
+ RMtx2Rotation( RMtx2* out, rmReal radian )
148
+ {
149
+ rmReal s = rmSin( radian );
150
+ rmReal c = rmCos( radian );
151
+
152
+ RMtx2Identity( out );
153
+ SET_ELEMENT( out, 0, 0, c );
154
+ SET_ELEMENT( out, 0, 1, -s );
155
+ SET_ELEMENT( out, 1, 0, s );
156
+ SET_ELEMENT( out, 1, 1, c );
157
+ }
158
+
159
+ void
160
+ RMtx2Scaling( RMtx2* out, rmReal sx, rmReal sy )
161
+ {
162
+ RMtx2Identity( out );
163
+ SET_ELEMENT( out, 0, 0, sx );
164
+ SET_ELEMENT( out, 1, 1, sy );
165
+ }
166
+
167
+
168
+ int
169
+ RMtx2Equal( const RMtx2* m1, const RMtx2* m2 )
170
+ {
171
+ if ( 0 == memcmp( m1, m2, sizeof(RMtx2) ) )
172
+ return !0;
173
+ else
174
+ return 0;
175
+ }
176
+
177
+
178
+ void
179
+ RMtx2Add( RMtx2* out, const RMtx2* m1, const RMtx2* m2 )
180
+ {
181
+ int row, col;
182
+ RMtx2 tmp;
183
+ for ( row = 0; row < 2; ++row )
184
+ for ( col = 0; col < 2; ++col )
185
+ SET_ELEMENT( &tmp, row, col,
186
+ GET_ELEMENT( m1, row, col ) + GET_ELEMENT( m2, row, col ) );
187
+
188
+ RMtx2Copy( out, &tmp );
189
+ }
190
+
191
+ void
192
+ RMtx2Sub( RMtx2* out, const RMtx2* m1, const RMtx2* m2 )
193
+ {
194
+ int row, col;
195
+ for ( row = 0; row < 2; ++row )
196
+ for ( col = 0; col < 2; ++col )
197
+ SET_ELEMENT( out, row, col,
198
+ GET_ELEMENT( m1, row, col ) - GET_ELEMENT( m2, row, col ) );
199
+ }
200
+
201
+ void
202
+ RMtx2Mul( RMtx2* out, const RMtx2* m1, const RMtx2* m2 )
203
+ {
204
+ int row, col;
205
+ RMtx2 tmp;
206
+ for ( row = 0; row < 2; ++row )
207
+ {
208
+ for ( col = 0; col < 2; ++col )
209
+ {
210
+ int i;
211
+ rmReal sum = 0.0f;
212
+ for ( i = 0; i < 2; ++i )
213
+ {
214
+ sum += GET_ELEMENT( m1, row, i ) * GET_ELEMENT( m2, i, col );
215
+ }
216
+ SET_ELEMENT( &tmp, row, col, sum );
217
+ }
218
+ }
219
+
220
+ RMtx2Copy( out, &tmp );
221
+ }
222
+
223
+ void
224
+ RMtx2Scale( RMtx2* out, const RMtx2* m, rmReal f )
225
+ {
226
+ int row, col;
227
+ for ( row = 0; row < 2; ++row )
228
+ for ( col = 0; col < 2; ++col )
229
+ SET_ELEMENT( out, row, col,
230
+ GET_ELEMENT( m, row, col ) * f );
231
+ }
232
+
233
+ /*
234
+ RMath : Ruby math module for 3D Applications
235
+ Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
236
+
237
+ This software is provided 'as-is', without any express or implied
238
+ warranty. In no event will the authors be held liable for any damages
239
+ arising from the use of this software.
240
+
241
+ Permission is granted to anyone to use this software for any purpose,
242
+ including commercial applications, and to alter it and redistribute it
243
+ freely, subject to the following restrictions:
244
+
245
+ 1. The origin of this software must not be misrepresented; you must not
246
+ claim that you wrote the original software. If you use this software
247
+ in a product, an acknowledgment in the product documentation would be
248
+ appreciated but is not required.
249
+
250
+ 2. Altered source versions must be plainly marked as such, and must not be
251
+ misrepresented as being the original software.
252
+
253
+ 3. This notice may not be removed or altered from any source
254
+ distribution.
255
+ */
@@ -0,0 +1,88 @@
1
+ /* -*- C -*- */
2
+ #ifndef RMATHMTX2_H_INCLUDED
3
+ #define RMATHMTX2_H_INCLUDED
4
+
5
+ #include "RType.h"
6
+
7
+ struct RVec2;
8
+
9
+ typedef struct RMtx2
10
+ {
11
+ union
12
+ {
13
+ /* NOTE : column-major */
14
+ struct
15
+ {
16
+ rmReal e00, e10;
17
+ rmReal e01, e11;
18
+ };
19
+ rmReal e[4];
20
+ };
21
+ } RMtx2;
22
+
23
+ #ifdef __cplusplus
24
+ extern "C" {
25
+ #endif
26
+
27
+ void RMtx2SetElements( RMtx2* out,
28
+ rmReal e00, rmReal e01,
29
+ rmReal e10, rmReal e11
30
+ );
31
+
32
+ void RMtx2SetElement( RMtx2* out, int row, int col, rmReal e );
33
+ rmReal RMtx2GetElement( const RMtx2* in, int row, int col );
34
+
35
+ void RMtx2GetRow( struct RVec2* out, const RMtx2* in, int row );
36
+ void RMtx2GetColumn( struct RVec2* out, const RMtx2* in, int col );
37
+
38
+ void RMtx2SetRow( struct RMtx2* out, const RVec2* in, int row );
39
+ void RMtx2SetColumn( struct RMtx2* out, const RVec2* in, int col );
40
+
41
+ void RMtx2Copy( RMtx2* out, const RMtx2* in );
42
+
43
+ void RMtx2Zero( RMtx2* out );
44
+ void RMtx2Identity( RMtx2* out );
45
+ rmReal RMtx2Determinant( const RMtx2* in );
46
+ void RMtx2Transpose( RMtx2* out, const RMtx2* in );
47
+ rmReal RMtx2Inverse( RMtx2* out, const RMtx2* in );
48
+
49
+ void RMtx2Rotation( RMtx2* out, rmReal radian );
50
+ void RMtx2Scaling( RMtx2* out, rmReal sx, rmReal sy );
51
+
52
+ int RMtx2Equal( const RMtx2* m1, const RMtx2* m2 );
53
+
54
+ void RMtx2Add( RMtx2* out, const RMtx2* m1, const RMtx2* m2 );
55
+ void RMtx2Sub( RMtx2* out, const RMtx2* m1, const RMtx2* m2 );
56
+ void RMtx2Mul( RMtx2* out, const RMtx2* m1, const RMtx2* m2 );
57
+ void RMtx2Scale( RMtx2* out, const RMtx2* m, rmReal f );
58
+
59
+ #ifdef __cplusplus
60
+ }
61
+ #endif
62
+
63
+
64
+ #endif
65
+
66
+ /*
67
+ RMath : Ruby math module for 3D Applications
68
+ Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
69
+
70
+ This software is provided 'as-is', without any express or implied
71
+ warranty. In no event will the authors be held liable for any damages
72
+ arising from the use of this software.
73
+
74
+ Permission is granted to anyone to use this software for any purpose,
75
+ including commercial applications, and to alter it and redistribute it
76
+ freely, subject to the following restrictions:
77
+
78
+ 1. The origin of this software must not be misrepresented; you must not
79
+ claim that you wrote the original software. If you use this software
80
+ in a product, an acknowledgment in the product documentation would be
81
+ appreciated but is not required.
82
+
83
+ 2. Altered source versions must be plainly marked as such, and must not be
84
+ misrepresented as being the original software.
85
+
86
+ 3. This notice may not be removed or altered from any source
87
+ distribution.
88
+ */
@@ -0,0 +1,167 @@
1
+ #include <math.h>
2
+ #include <string.h>
3
+
4
+ #include "RVec2.h"
5
+ #include "RMtx2.h"
6
+
7
+ #define SET_ELEMENT( out, at, f )
8
+
9
+ void
10
+ RVec2SetElement( RVec2* out, int at, rmReal f )
11
+ {
12
+ out->e[at] = f;
13
+ }
14
+
15
+ void
16
+ RVec2SetX( RVec2* out, rmReal x )
17
+ {
18
+ out->x = x;
19
+ }
20
+
21
+ void
22
+ RVec2SetY( RVec2* out, rmReal y )
23
+ {
24
+ out->y = y;
25
+ }
26
+
27
+ void
28
+ RVec2SetElements( RVec2* out, rmReal x, rmReal y )
29
+ {
30
+ RVec2SetX( out, x );
31
+ RVec2SetY( out, y );
32
+ }
33
+
34
+ rmReal
35
+ RVec2GetElement( const RVec2* in, int at )
36
+ {
37
+ return in->e[at];
38
+ }
39
+
40
+ rmReal
41
+ RVec2GetX( const RVec2* in )
42
+ {
43
+ return in->x;
44
+ }
45
+
46
+ rmReal
47
+ RVec2GetY( const RVec2* in )
48
+ {
49
+ return in->y;
50
+ }
51
+
52
+ int
53
+ RVec2Equal( const RVec2* v1, const RVec2* v2 )
54
+ {
55
+ if ( 0 == memcmp( v1, v2, sizeof(RVec2) ) )
56
+ return !0;
57
+ else
58
+ return 0;
59
+ }
60
+
61
+ void
62
+ RVec2Add( RVec2* out, const RVec2* v1, const RVec2* v2 )
63
+ {
64
+ out->x = v1->x + v2->x;
65
+ out->y = v1->y + v2->y;
66
+ }
67
+
68
+ void
69
+ RVec2Sub( RVec2* out, const RVec2* v1, const RVec2* v2 )
70
+ {
71
+ out->x = v1->x - v2->x;
72
+ out->y = v1->y - v2->y;
73
+ }
74
+
75
+ void
76
+ RVec2Scale( RVec2* out, const RVec2* in, rmReal f )
77
+ {
78
+ out->x = in->x * f;
79
+ out->y = in->y * f;
80
+ }
81
+
82
+ rmReal
83
+ RVec2Cross( const RVec2* v1, const RVec2* v2 )
84
+ {
85
+ rmReal v1x, v1y, v2x, v2y;
86
+
87
+ v1x = v1->x;
88
+ v1y = v1->y;
89
+ v2x = v2->x;
90
+ v2y = v2->y;
91
+
92
+ return v1x*v2y - v1y*v2x;
93
+ }
94
+
95
+ rmReal
96
+ RVec2LengthSq( const RVec2* in )
97
+ {
98
+ return in->x*in->x + in->y*in->y;
99
+ }
100
+
101
+ rmReal
102
+ RVec2Length( const RVec2* in )
103
+ {
104
+ return rmSqrt( in->x*in->x + in->y*in->y );
105
+ }
106
+
107
+ rmReal
108
+ RVec2Dot( const RVec2* v1, const RVec2* v2 )
109
+ {
110
+ return v1->x*v2->x + v1->y*v2->y;
111
+ }
112
+
113
+ void
114
+ RVec2Copy( RVec2* out, const RVec2* in )
115
+ {
116
+ out->x = in->x;
117
+ out->y = in->y;
118
+ }
119
+
120
+ void
121
+ RVec2Normalize( RVec2* out, const RVec2* in )
122
+ {
123
+ rmReal length = RVec2Length( in );
124
+ RVec2Scale( out, in, 1.0f/length );
125
+ }
126
+
127
+ void
128
+ RVec2Transform( RVec2* out, const RMtx2* m, const RVec2* in )
129
+ {
130
+ RVec2 result, in_vec2;
131
+ int row;
132
+
133
+ RVec2Copy( &in_vec2, in );
134
+ for ( row = 0; row < 2; ++row )
135
+ {
136
+ RVec2 row_vector;
137
+ RMtx2GetRow( &row_vector, m, row );
138
+ RVec2SetElement( &result, row, RVec2Dot( &row_vector, &in_vec2 ) );
139
+ }
140
+
141
+ out->x = RVec2GetX( &result );
142
+ out->y = RVec2GetY( &result );
143
+ }
144
+
145
+ /*
146
+ RMath : Ruby math module for 3D Applications
147
+ Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>.
148
+
149
+ This software is provided 'as-is', without any express or implied
150
+ warranty. In no event will the authors be held liable for any damages
151
+ arising from the use of this software.
152
+
153
+ Permission is granted to anyone to use this software for any purpose,
154
+ including commercial applications, and to alter it and redistribute it
155
+ freely, subject to the following restrictions:
156
+
157
+ 1. The origin of this software must not be misrepresented; you must not
158
+ claim that you wrote the original software. If you use this software
159
+ in a product, an acknowledgment in the product documentation would be
160
+ appreciated but is not required.
161
+
162
+ 2. Altered source versions must be plainly marked as such, and must not be
163
+ misrepresented as being the original software.
164
+
165
+ 3. This notice may not be removed or altered from any source
166
+ distribution.
167
+ */