rmath3d 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,92 @@
1
+ /* -*- C -*- */
2
+ #ifndef RMATHQUAT_H_INCLUDED
3
+ #define RMATHQUAT_H_INCLUDED
4
+
5
+ #include "RType.h"
6
+
7
+ struct RVec3;
8
+ struct RMtx4;
9
+
10
+ typedef struct RQuat
11
+ {
12
+ union
13
+ {
14
+ struct
15
+ {
16
+ rmReal x, y, z, w;
17
+ };
18
+ rmReal e[4];
19
+ };
20
+ } RQuat;
21
+
22
+ #ifdef __cplusplus
23
+ extern "C" {
24
+ #endif
25
+
26
+ void RQuatSetElements( RQuat* out, rmReal x, rmReal y, rmReal z, rmReal w );
27
+ void RQuatSetElement( RQuat* out, int at, rmReal f );
28
+ void RQuatSetX( RQuat* out, rmReal x );
29
+ void RQuatSetY( RQuat* out, rmReal y );
30
+ void RQuatSetZ( RQuat* out, rmReal z );
31
+ void RQuatSetW( RQuat* out, rmReal w );
32
+ void RQuatSetXYZ( RQuat* out, const struct RVec3* v );
33
+
34
+ rmReal RQuatGetElement( const RQuat* in, int at );
35
+ rmReal RQuatGetX( const RQuat* in );
36
+ rmReal RQuatGetY( const RQuat* in );
37
+ rmReal RQuatGetZ( const RQuat* in );
38
+ rmReal RQuatGetW( const RQuat* in );
39
+ void RQuatGetXYZ( struct RVec3* out, const RQuat* in );
40
+
41
+ int RQuatEqual( const RQuat* q1, const RQuat* q2 );
42
+ rmReal RQuatDot( const RQuat* q1, const RQuat* q2 );
43
+
44
+ void RQuatIdentity( RQuat* out );
45
+ void RQuatCopy( RQuat* out, const RQuat* in );
46
+ void RQuatNormalize( RQuat* out, const RQuat* in );
47
+ void RQuatConjugate( RQuat* out, const RQuat* in );
48
+ void RQuatInverse( RQuat* out, const RQuat* in );
49
+
50
+ void RQuatAdd( RQuat* out, const RQuat* q1, const RQuat* q2 );
51
+ void RQuatSub( RQuat* out, const RQuat* q1, const RQuat* q2 );
52
+ void RQuatMul( RQuat* out, const RQuat* q1, const RQuat* q2 );
53
+ void RQuatScale( RQuat* out, const RQuat* in, rmReal f );
54
+
55
+ rmReal RQuatLength( const RQuat* in );
56
+ rmReal RQuatLengthSq( const RQuat* in );
57
+
58
+ void RQuatSlerp( RQuat* out, const RQuat* q1, const RQuat* q2, rmReal t );
59
+
60
+ void RQuatRotationMatrix( RQuat* out, const struct RMtx4* mtx );
61
+ void RQuatRotationAxis( RQuat* out, const struct RVec3* axis, rmReal radian );
62
+ void RQuatToAxisAngle( const RQuat* in, struct RVec3* axis, rmReal* radian );
63
+
64
+ #ifdef __cplusplus
65
+ }
66
+ #endif
67
+
68
+ #endif
69
+
70
+ /*
71
+ RMath : Ruby math module for 3D Applications
72
+ Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
73
+
74
+ This software is provided 'as-is', without any express or implied
75
+ warranty. In no event will the authors be held liable for any damages
76
+ arising from the use of this software.
77
+
78
+ Permission is granted to anyone to use this software for any purpose,
79
+ including commercial applications, and to alter it and redistribute it
80
+ freely, subject to the following restrictions:
81
+
82
+ 1. The origin of this software must not be misrepresented; you must not
83
+ claim that you wrote the original software. If you use this software
84
+ in a product, an acknowledgment in the product documentation would be
85
+ appreciated but is not required.
86
+
87
+ 2. Altered source versions must be plainly marked as such, and must not be
88
+ misrepresented as being the original software.
89
+
90
+ 3. This notice may not be removed or altered from any source
91
+ distribution.
92
+ */
@@ -0,0 +1,76 @@
1
+ #ifndef RTYPE_H_INCLUDED
2
+ #define RTYPE_H_INCLUDED
3
+
4
+ /*
5
+ * Build Configuration
6
+ *
7
+ * * RMATH_SINGLE_PRECISION : float (32-bit single scalar)
8
+ */
9
+ /*
10
+ #define RMATH_SINGLE_PRECISION
11
+ */
12
+
13
+
14
+ #if defined(RMATH_SINGLE_PRECISION)
15
+ typedef float rmReal;
16
+ #else
17
+ typedef double rmReal;
18
+ #endif
19
+
20
+ #if defined(RMATH_SINGLE_PRECISION)
21
+ # define RMATH_TOLERANCE (1e-6)
22
+ #else
23
+ # define RMATH_TOLERANCE (1e-15)
24
+ #endif
25
+
26
+ #if defined(RMATH_SINGLE_PRECISION)
27
+
28
+ #define rmAcos acosf
29
+ #define rmAsin asinf
30
+ #define rmAtan atanf
31
+ #define rmAtan2 atan2f
32
+ #define rmCos cosf
33
+ #define rmSin sinf
34
+ #define rmTan tanf
35
+ #define rmSqrt sqrtf
36
+ #define rmFabs fabsf
37
+
38
+ #else
39
+
40
+ #define rmAcos acos
41
+ #define rmAsin asin
42
+ #define rmAtan atan
43
+ #define rmAtan2 atan2
44
+ #define rmCos cos
45
+ #define rmSin sin
46
+ #define rmTan tan
47
+ #define rmSqrt sqrt
48
+ #define rmFabs fabs
49
+
50
+ #endif
51
+
52
+ #endif
53
+
54
+ /*
55
+ RMath : Ruby math module for 3D Applications
56
+ Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
57
+
58
+ This software is provided 'as-is', without any express or implied
59
+ warranty. In no event will the authors be held liable for any damages
60
+ arising from the use of this software.
61
+
62
+ Permission is granted to anyone to use this software for any purpose,
63
+ including commercial applications, and to alter it and redistribute it
64
+ freely, subject to the following restrictions:
65
+
66
+ 1. The origin of this software must not be misrepresented; you must not
67
+ claim that you wrote the original software. If you use this software
68
+ in a product, an acknowledgment in the product documentation would be
69
+ appreciated but is not required.
70
+
71
+ 2. Altered source versions must be plainly marked as such, and must not be
72
+ misrepresented as being the original software.
73
+
74
+ 3. This notice may not be removed or altered from any source
75
+ distribution.
76
+ */
@@ -0,0 +1,285 @@
1
+ #include <math.h>
2
+ #include <string.h>
3
+
4
+ #include "RVec3.h"
5
+ #include "RVec4.h"
6
+ #include "RQuat.h"
7
+ #include "RMtx3.h"
8
+ #include "RMtx4.h"
9
+
10
+ #define SET_ELEMENT( out, at, f )
11
+
12
+ void
13
+ RVec3SetElement( RVec3* out, int at, rmReal f )
14
+ {
15
+ out->e[at] = f;
16
+ }
17
+
18
+ void
19
+ RVec3SetX( RVec3* out, rmReal x )
20
+ {
21
+ out->x = x;
22
+ }
23
+
24
+ void
25
+ RVec3SetY( RVec3* out, rmReal y )
26
+ {
27
+ out->y = y;
28
+ }
29
+
30
+ void
31
+ RVec3SetZ( RVec3* out, rmReal z )
32
+ {
33
+ out->z = z;
34
+ }
35
+
36
+ void
37
+ RVec3SetElements( RVec3* out, rmReal x, rmReal y, rmReal z )
38
+ {
39
+ RVec3SetX( out, x );
40
+ RVec3SetY( out, y );
41
+ RVec3SetZ( out, z );
42
+ }
43
+
44
+ rmReal
45
+ RVec3GetElement( const RVec3* in, int at )
46
+ {
47
+ return in->e[at];
48
+ }
49
+
50
+ rmReal
51
+ RVec3GetX( const RVec3* in )
52
+ {
53
+ return in->x;
54
+ }
55
+
56
+ rmReal
57
+ RVec3GetY( const RVec3* in )
58
+ {
59
+ return in->y;
60
+ }
61
+
62
+ rmReal
63
+ RVec3GetZ( const RVec3* in )
64
+ {
65
+ return in->z;
66
+ }
67
+
68
+ int
69
+ RVec3Equal( const RVec3* v1, const RVec3* v2 )
70
+ {
71
+ if ( 0 == memcmp( v1, v2, sizeof(RVec3) ) )
72
+ return !0;
73
+ else
74
+ return 0;
75
+ }
76
+
77
+ void
78
+ RVec3Add( RVec3* out, const RVec3* v1, const RVec3* v2 )
79
+ {
80
+ out->x = v1->x + v2->x;
81
+ out->y = v1->y + v2->y;
82
+ out->z = v1->z + v2->z;
83
+ }
84
+
85
+ void
86
+ RVec3Sub( RVec3* out, const RVec3* v1, const RVec3* v2 )
87
+ {
88
+ out->x = v1->x - v2->x;
89
+ out->y = v1->y - v2->y;
90
+ out->z = v1->z - v2->z;
91
+ }
92
+
93
+ void
94
+ RVec3Scale( RVec3* out, const RVec3* in, rmReal f )
95
+ {
96
+ out->x = in->x * f;
97
+ out->y = in->y * f;
98
+ out->z = in->z * f;
99
+ }
100
+
101
+ void
102
+ RVec3Cross( RVec3* out, const RVec3* v1, const RVec3* v2 )
103
+ {
104
+ RVec3 tmp;
105
+ rmReal v1x, v1y, v1z, v2x, v2y, v2z;
106
+
107
+ v1x = v1->x;
108
+ v1y = v1->y;
109
+ v1z = v1->z;
110
+ v2x = v2->x;
111
+ v2y = v2->y;
112
+ v2z = v2->z;
113
+
114
+ tmp.x = v1y*v2z - v1z*v2y;
115
+ tmp.y = v1z*v2x - v1x*v2z;
116
+ tmp.z = v1x*v2y - v1y*v2x;
117
+
118
+ RVec3Copy( out, &tmp );
119
+ }
120
+
121
+ rmReal
122
+ RVec3LengthSq( const RVec3* in )
123
+ {
124
+ return in->x*in->x + in->y*in->y + in->z*in->z;
125
+ }
126
+
127
+ rmReal
128
+ RVec3Length( const RVec3* in )
129
+ {
130
+ return rmSqrt( in->x*in->x + in->y*in->y + in->z*in->z );
131
+ }
132
+
133
+ rmReal
134
+ RVec3Dot( const RVec3* v1, const RVec3* v2 )
135
+ {
136
+ return v1->x*v2->x + v1->y*v2->y + v1->z*v2->z;
137
+ }
138
+
139
+ void
140
+ RVec3Copy( RVec3* out, const RVec3* in )
141
+ {
142
+ out->x = in->x;
143
+ out->y = in->y;
144
+ out->z = in->z;
145
+ }
146
+
147
+ void
148
+ RVec3Normalize( RVec3* out, const RVec3* in )
149
+ {
150
+ rmReal length = RVec3Length( in );
151
+ RVec3Scale( out, in, 1.0f/length );
152
+ }
153
+
154
+ void
155
+ RVec3Transform( RVec4* out, const RMtx4* m, const RVec3* in )
156
+ {
157
+ RVec4 result, in_vec4;
158
+ int row;
159
+
160
+ RVec4SetXYZ( &in_vec4, in );
161
+ RVec4SetW( &in_vec4, 1.0f );
162
+ for ( row = 0; row < 4; ++row )
163
+ {
164
+ RVec4 row_vector;
165
+ RMtx4GetRow( &row_vector, m, row );
166
+ RVec4SetElement( &result, row, RVec4Dot( &row_vector, &in_vec4 ) );
167
+ }
168
+ RVec4Copy( out, &result );
169
+ }
170
+
171
+ void
172
+ RVec3TransformCoord( RVec3* out, const RMtx4* m, const RVec3* in )
173
+ {
174
+ RVec4 tmp;
175
+ rmReal w;
176
+
177
+ RVec3Transform( &tmp, m, in );
178
+ w = RVec4GetW( &tmp );
179
+ w = 1.0f / w;
180
+
181
+ out->x = w * RVec4GetX( &tmp );
182
+ out->y = w * RVec4GetY( &tmp );
183
+ out->z = w * RVec4GetZ( &tmp );
184
+ }
185
+
186
+ void
187
+ RVec3TransformNormal( RVec3* out, const RMtx4* m, const RVec3* in )
188
+ {
189
+ RVec4 result, in_vec4;
190
+ int row;
191
+
192
+ RVec4SetXYZ( &in_vec4, in );
193
+ RVec4SetW( &in_vec4, 0.0f );
194
+ for ( row = 0; row < 4; ++row )
195
+ {
196
+ RVec4 row_vector;
197
+ RMtx4GetRow( &row_vector, m, row );
198
+ RVec4SetElement( &result, row, RVec4Dot( &row_vector, &in_vec4 ) );
199
+ }
200
+
201
+ out->x = RVec4GetX( &result );
202
+ out->y = RVec4GetY( &result );
203
+ out->z = RVec4GetZ( &result );
204
+ }
205
+
206
+ void
207
+ RVec3TransformRS( RVec3* out, const RMtx3* m, const RVec3* in )
208
+ {
209
+ RVec3 result;
210
+ int row;
211
+
212
+ for ( row = 0; row < 3; ++row )
213
+ {
214
+ RVec3 row_vector;
215
+ RMtx3GetRow( &row_vector, m, row );
216
+ RVec3SetElement( &result, row, RVec3Dot( &row_vector, in ) );
217
+ }
218
+
219
+ out->x = RVec3GetX( &result );
220
+ out->y = RVec3GetY( &result );
221
+ out->z = RVec3GetZ( &result );
222
+ }
223
+
224
+ void
225
+ RVec3TransformRSTransposed( RVec3* out, const struct RMtx3* m, const RVec3* in )
226
+ {
227
+ RVec3 result;
228
+ int column;
229
+
230
+ for ( column = 0; column < 3; ++column )
231
+ {
232
+ RVec3 column_vector;
233
+ RMtx3GetColumn( &column_vector, m, column );
234
+ RVec3SetElement( &result, column, RVec3Dot( &column_vector, in ) );
235
+ }
236
+
237
+ out->x = RVec3GetX( &result );
238
+ out->y = RVec3GetY( &result );
239
+ out->z = RVec3GetZ( &result );
240
+ }
241
+
242
+ void
243
+ RVec3TransformByQuaternion( RVec3* out, const struct RQuat* q, const RVec3* in )
244
+ {
245
+ rmReal qcx = -q->x;
246
+ rmReal qcy = -q->y;
247
+ rmReal qcz = -q->z;
248
+ rmReal qcw = q->w;
249
+ rmReal v_x = in->x;
250
+ rmReal v_y = in->y;
251
+ rmReal v_z = in->z;
252
+
253
+ rmReal t_x = q->w*v_x + q->y*v_z - q->z*v_y;
254
+ rmReal t_y = q->w*v_y - q->x*v_z + q->z*v_x;
255
+ rmReal t_z = q->w*v_z + q->x*v_y - q->y*v_x ;
256
+ rmReal t_w = - q->x*v_x - q->y*v_y - q->z*v_z;
257
+
258
+ out->x = t_w*qcx + t_x*qcw + t_y*qcz - t_z*qcy;
259
+ out->y = t_w*qcy - t_x*qcz + t_y*qcw + t_z*qcx;
260
+ out->z = t_w*qcz + t_x*qcy - t_y*qcx + t_z*qcw;
261
+ }
262
+
263
+ /*
264
+ RMath : Ruby math module for 3D Applications
265
+ Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>.
266
+
267
+ This software is provided 'as-is', without any express or implied
268
+ warranty. In no event will the authors be held liable for any damages
269
+ arising from the use of this software.
270
+
271
+ Permission is granted to anyone to use this software for any purpose,
272
+ including commercial applications, and to alter it and redistribute it
273
+ freely, subject to the following restrictions:
274
+
275
+ 1. The origin of this software must not be misrepresented; you must not
276
+ claim that you wrote the original software. If you use this software
277
+ in a product, an acknowledgment in the product documentation would be
278
+ appreciated but is not required.
279
+
280
+ 2. Altered source versions must be plainly marked as such, and must not be
281
+ misrepresented as being the original software.
282
+
283
+ 3. This notice may not be removed or altered from any source
284
+ distribution.
285
+ */