summaryrefslogtreecommitdiff
path: root/src/lib/ephysics/ephysics_quaternion.cpp
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@gmail.com>2013-01-03 22:10:40 +0000
committerGustavo Sverzut Barbieri <barbieri@gmail.com>2013-01-03 22:10:40 +0000
commit9e0788cc2e261ec66cf740e35a3e71f4485df763 (patch)
tree8c1230e446edd6966d39c1b8959c7f97f5c0f731 /src/lib/ephysics/ephysics_quaternion.cpp
parent9edec477ebb83e64f3e464d82665e2b9f01f9bb0 (diff)
efl: merge ephysics
changes: * __UNUSED__ -> EINA_UNUSED * Fixed doc hierarchy SVN revision: 82126
Diffstat (limited to 'src/lib/ephysics/ephysics_quaternion.cpp')
-rw-r--r--src/lib/ephysics/ephysics_quaternion.cpp332
1 files changed, 332 insertions, 0 deletions
diff --git a/src/lib/ephysics/ephysics_quaternion.cpp b/src/lib/ephysics/ephysics_quaternion.cpp
new file mode 100644
index 0000000000..899204d563
--- /dev/null
+++ b/src/lib/ephysics/ephysics_quaternion.cpp
@@ -0,0 +1,332 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_private.h"
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11static void
12_ephysics_quaternion_update(EPhysics_Quaternion *quat, btQuaternion *bt_quat)
13{
14 quat->x = bt_quat->x();
15 quat->y = bt_quat->y();
16 quat->z = bt_quat->z();
17 quat->w = bt_quat->getW();
18}
19
20static EPhysics_Quaternion *
21_ephysics_quaternion_params_check(const EPhysics_Quaternion *quat1, const EPhysics_Quaternion *quat2, EPhysics_Quaternion *result)
22{
23 if ((!quat1) || (!quat2))
24 {
25 ERR("Can't operate over null quaternions.");
26 return NULL;
27 }
28
29 if (result)
30 return result;
31
32 return ephysics_quaternion_new();
33}
34
35EAPI EPhysics_Quaternion *
36ephysics_quaternion_new(void)
37{
38 EPhysics_Quaternion *quat;
39
40 quat = (EPhysics_Quaternion *)calloc(1, sizeof(EPhysics_Quaternion));
41
42 if (!quat)
43 {
44 ERR("Could not allocate ephysics quaternion.");
45 return NULL;
46 }
47
48 quat->w = 1;
49 return quat;
50}
51
52EAPI void
53ephysics_quaternion_get(const EPhysics_Quaternion *quat, double *x, double *y, double *z, double *w)
54{
55 if (!quat)
56 {
57 ERR("Can't get quaternion's values, it is null.");
58 return;
59 }
60
61 if (x) *x = quat->x;
62 if (y) *y = quat->y;
63 if (z) *z = quat->z;
64 if (w) *w = quat->w;
65}
66
67EAPI void
68ephysics_quaternion_axis_angle_get(const EPhysics_Quaternion *quat, double *nx, double *ny, double *nz, double *a)
69{
70 btQuaternion bt_quat;
71
72 if (!quat)
73 {
74 ERR("Can't get quaternion's values, it is null.");
75 return;
76 }
77
78 bt_quat = btQuaternion(quat->x, quat->y, quat->z, quat->w);
79
80 if (nx) *nx = bt_quat.getAxis().getX();
81 if (ny) *ny = bt_quat.getAxis().getY();
82 if (nz) *nz = bt_quat.getAxis().getZ();
83 if (a) *a = bt_quat.getAngle() * RAD_TO_DEG;
84}
85
86EAPI void
87ephysics_quaternion_set(EPhysics_Quaternion *quat, double x, double y, double z, double w)
88{
89 if (!quat)
90 {
91 ERR("Could not set a null quaternion.");
92 return;
93 }
94
95 quat->x = x;
96 quat->y = y;
97 quat->z = z;
98 quat->w = w;
99}
100
101EAPI void
102ephysics_quaternion_axis_angle_set(EPhysics_Quaternion *quat, double nx, double ny, double nz, double a)
103{
104 btQuaternion bt_quat;
105 btVector3 axis;
106
107 if (!quat)
108 {
109 ERR("Could not set a null quaternion.");
110 return;
111 }
112
113 axis = btVector3(nx, ny, nz);
114 bt_quat = btQuaternion(axis, a / RAD_TO_DEG);
115 _ephysics_quaternion_update(quat, &bt_quat);
116}
117
118EAPI void
119ephysics_quaternion_euler_set(EPhysics_Quaternion *quat, double yaw, double pitch, double roll)
120{
121 btQuaternion bt_quat;
122
123 if (!quat)
124 {
125 ERR("Could not set a null quaternion.");
126 return;
127 }
128
129 bt_quat = btQuaternion();
130 bt_quat.setEuler(yaw / RAD_TO_DEG, pitch / RAD_TO_DEG, roll / RAD_TO_DEG);
131 _ephysics_quaternion_update(quat, &bt_quat);
132}
133
134EAPI void
135ephysics_quaternion_normalize(EPhysics_Quaternion *quat)
136{
137 btQuaternion bt_quat;
138
139 if (!quat)
140 {
141 ERR("Can't normalize a null quaternion.");
142 return;
143 }
144
145 bt_quat = btQuaternion(quat->x, quat->y, quat->z, quat->w);
146 bt_quat.normalize();
147 _ephysics_quaternion_update(quat, &bt_quat);
148}
149
150EAPI void
151ephysics_quaternion_invert(EPhysics_Quaternion *quat)
152{
153 btQuaternion bt_quat;
154
155 if (!quat)
156 {
157 ERR("Can't normalize a null quaternion.");
158 return;
159 }
160
161 bt_quat = btQuaternion(quat->x, quat->y, quat->z, quat->w);
162 bt_quat.inverse();
163 _ephysics_quaternion_update(quat, &bt_quat);
164}
165
166EAPI void
167ephysics_quaternion_scale(EPhysics_Quaternion *quat, double scale)
168{
169 btQuaternion bt_quat;
170
171 if (!quat)
172 {
173 ERR("Can't operate over a null quaternion.");
174 return;
175 }
176
177 bt_quat = btQuaternion(quat->x, quat->y, quat->z, quat->w);
178 bt_quat *= scale;
179 _ephysics_quaternion_update(quat, &bt_quat);
180}
181
182EAPI void
183ephysics_quaternion_inverse_scale(EPhysics_Quaternion *quat, double scale)
184{
185 btQuaternion bt_quat;
186
187 if (!quat)
188 {
189 ERR("Can't operate over a null quaternion.");
190 return;
191 }
192
193 bt_quat = btQuaternion(quat->x, quat->y, quat->z, quat->w);
194 bt_quat /= scale;
195 _ephysics_quaternion_update(quat, &bt_quat);
196}
197
198EAPI EPhysics_Quaternion *
199ephysics_quaternion_sum(const EPhysics_Quaternion *quat1, const EPhysics_Quaternion *quat2, EPhysics_Quaternion *result)
200{
201 btQuaternion bt_quat1, bt_quat2, bt_quat;
202 EPhysics_Quaternion *quat;
203
204 quat = _ephysics_quaternion_params_check(quat1, quat2, result);
205 if (!quat) return NULL;
206
207 bt_quat1 = btQuaternion(quat1->x, quat1->y, quat1->z, quat1->w);
208 bt_quat2 = btQuaternion(quat2->x, quat2->y, quat2->z, quat2->w);
209 bt_quat = bt_quat1 + bt_quat2;
210
211 _ephysics_quaternion_update(quat, &bt_quat);
212 return quat;
213}
214
215EAPI EPhysics_Quaternion *
216ephysics_quaternion_diff(const EPhysics_Quaternion *quat1, const EPhysics_Quaternion *quat2, EPhysics_Quaternion *result)
217{
218 btQuaternion bt_quat1, bt_quat2, bt_quat;
219 EPhysics_Quaternion *quat;
220
221 quat = _ephysics_quaternion_params_check(quat1, quat2, result);
222 if (!quat) return NULL;
223
224 bt_quat1 = btQuaternion(quat1->x, quat1->y, quat1->z, quat1->w);
225 bt_quat2 = btQuaternion(quat2->x, quat2->y, quat2->z, quat2->w);
226 bt_quat = bt_quat1 - bt_quat2;
227
228 _ephysics_quaternion_update(quat, &bt_quat);
229 return quat;
230}
231
232EAPI EPhysics_Quaternion *
233ephysics_quaternion_multiply(const EPhysics_Quaternion *quat1, const EPhysics_Quaternion *quat2, EPhysics_Quaternion *result)
234{
235 btQuaternion bt_quat1, bt_quat2, bt_quat;
236 EPhysics_Quaternion *quat;
237
238 quat = _ephysics_quaternion_params_check(quat1, quat2, result);
239 if (!quat) return NULL;
240
241 bt_quat1 = btQuaternion(quat1->x, quat1->y, quat1->z, quat1->w);
242 bt_quat2 = btQuaternion(quat2->x, quat2->y, quat2->z, quat2->w);
243 bt_quat = bt_quat1 * bt_quat2;
244
245 _ephysics_quaternion_update(quat, &bt_quat);
246 return quat;
247}
248
249EAPI EPhysics_Quaternion *
250ephysics_quaternion_slerp(const EPhysics_Quaternion *quat1, const EPhysics_Quaternion *quat2, double ratio, EPhysics_Quaternion *result)
251{
252 btQuaternion bt_quat1, bt_quat2;
253 EPhysics_Quaternion *quat;
254
255 quat = _ephysics_quaternion_params_check(quat1, quat2, result);
256 if (!quat) return NULL;
257
258 bt_quat1 = btQuaternion(quat1->x, quat1->y, quat1->z, quat1->w);
259 bt_quat2 = btQuaternion(quat2->x, quat2->y, quat2->z, quat2->w);
260 bt_quat1.slerp(bt_quat2, ratio);
261
262 _ephysics_quaternion_update(quat, &bt_quat1);
263 return quat;
264}
265
266EAPI double
267ephysics_quaternion_dot(const EPhysics_Quaternion *quat1, const EPhysics_Quaternion *quat2)
268{
269 btQuaternion bt_quat1, bt_quat2;
270
271 if ((!quat1) || (!quat2))
272 {
273 ERR("Can't operate over null quaternions.");
274 return 0;
275 }
276
277 bt_quat1 = btQuaternion(quat1->x, quat1->y, quat1->z, quat1->w);
278 bt_quat2 = btQuaternion(quat2->x, quat2->y, quat2->z, quat2->w);
279
280 return bt_quat1.dot(bt_quat2);
281}
282
283EAPI double
284ephysics_quaternion_angle_get(const EPhysics_Quaternion *quat1, const EPhysics_Quaternion *quat2)
285{
286 btQuaternion bt_quat1, bt_quat2;
287
288 if ((!quat1) || (!quat2))
289 {
290 ERR("Can't operate over null quaternions.");
291 return 0;
292 }
293
294 bt_quat1 = btQuaternion(quat1->x, quat1->y, quat1->z, quat1->w);
295 bt_quat2 = btQuaternion(quat2->x, quat2->y, quat2->z, quat2->w);
296
297 return bt_quat1.angle(bt_quat2) * RAD_TO_DEG;
298}
299
300EAPI double
301ephysics_quaternion_length_get(const EPhysics_Quaternion *quat)
302{
303 btQuaternion bt_quat;
304
305 if (!quat)
306 {
307 ERR("Can't operate over a null quaternion.");
308 return 0;
309 }
310
311 bt_quat = btQuaternion(quat->x, quat->y, quat->z, quat->w);
312 return bt_quat.length();
313}
314
315EAPI double
316ephysics_quaternion_length2_get(const EPhysics_Quaternion *quat)
317{
318 btQuaternion bt_quat;
319
320 if (!quat)
321 {
322 ERR("Can't operate over a null quaternion.");
323 return 0;
324 }
325
326 bt_quat = btQuaternion(quat->x, quat->y, quat->z, quat->w);
327 return bt_quat.length2();
328}
329
330#ifdef __cplusplus
331}
332#endif