Project

General

Profile

SO3Engine
SCOLMaths.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25
34#include "SCOLPack/SO3SCOL.h"
35
36// Scene Graph includes
38
39#include "SO3Utils/SO3Euler.h"
40
41
51{
52#ifdef SO3_DEBUG
53 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerXYZ\n");
54#endif
55
56 int q = MTOP(MMget(m, 0));
57 if (q == NIL)
58 {
59 MMset(m, 0, NIL);
60 return 0;
61 }
62
63 int x = MMfetch(m, q, 0);
64 int y = MMfetch(m, q, 1);
65 int z = MMfetch(m, q, 2);
66 int w = MMfetch(m, q, 3);
67
68 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
69 {
70 MMset(m, 0, NIL);
71 return 0;
72 }
73
74 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
75 quat.normalise();
76
77 Ogre::Matrix3 mat;
78 Ogre::Radian xrad;
79 Ogre::Radian yrad;
80 Ogre::Radian zrad;
81
82 quat.ToRotationMatrix(mat);
83
84 mat.ToEulerAnglesXYZ(yrad, xrad, zrad);
85
86 int tuple = MMmalloc(m, 3, TYPETAB);
87 if (tuple == NIL)
88 {
89 MMset(m, 0, NIL);
90 return MERRMEM;
91 }
92 MMstore(m, tuple, 0, FTOM(xrad.valueRadians()));
93 MMstore(m, tuple, 1, FTOM(yrad.valueRadians()));
94 MMstore(m, tuple, 2, FTOM(zrad.valueRadians()));
95 MMset(m, 0, PTOM(tuple));
96
97 return 0;
98}
99
100
110{
111#ifdef SO3_DEBUG
112 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerXZY\n");
113#endif
114
115 int q = MTOP(MMget(m, 0));
116 if (q == NIL)
117 {
118 MMset(m, 0, NIL);
119 return 0;
120 }
121
122 int x = MMfetch(m, q, 0);
123 int y = MMfetch(m, q, 1);
124 int z = MMfetch(m, q, 2);
125 int w = MMfetch(m, q, 3);
126
127 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
128 {
129 MMset(m, 0, NIL);
130 return 0;
131 }
132
133 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
134 quat.normalise();
135
136 Ogre::Matrix3 mat;
137 Ogre::Radian xrad;
138 Ogre::Radian yrad;
139 Ogre::Radian zrad;
140
141 quat.ToRotationMatrix(mat);
142
143 mat.ToEulerAnglesXZY(yrad, xrad, zrad);
144
145 int tuple = MMmalloc(m, 3, TYPETAB);
146 if (tuple == NIL)
147 {
148 MMset(m, 0, NIL);
149 return MERRMEM;
150 }
151 MMstore(m, tuple, 0, FTOM(xrad.valueRadians()));
152 MMstore(m, tuple, 1, FTOM(yrad.valueRadians()));
153 MMstore(m, tuple, 2, FTOM(zrad.valueRadians()));
154 MMset(m, 0, PTOM(tuple));
155
156 return 0;
157}
158
159
169{
170#ifdef SO3_DEBUG
171 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerYXZ\n");
172#endif
173
174 int q = MTOP(MMget(m, 0));
175 if (q == NIL)
176 {
177 MMset(m, 0, NIL);
178 return 0;
179 }
180
181 int x = MMfetch(m, q, 0);
182 int y = MMfetch(m, q, 1);
183 int z = MMfetch(m, q, 2);
184 int w = MMfetch(m, q, 3);
185
186 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
187 {
188 MMset(m, 0, NIL);
189 return 0;
190 }
191
192 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
193 quat.normalise();
194
195 Ogre::Matrix3 mat;
196 Ogre::Radian xrad;
197 Ogre::Radian yrad;
198 Ogre::Radian zrad;
199
200 quat.ToRotationMatrix(mat);
201
202 mat.ToEulerAnglesYXZ(yrad, xrad, zrad);
203
204 int tuple = MMmalloc(m, 3, TYPETAB);
205 if (tuple == NIL)
206 {
207 MMset(m, 0, NIL);
208 return MERRMEM;
209 }
210 MMstore(m, tuple, 0, FTOM(xrad.valueRadians()));
211 MMstore(m, tuple, 1, FTOM(yrad.valueRadians()));
212 MMstore(m, tuple, 2, FTOM(zrad.valueRadians()));
213 MMset(m, 0, PTOM(tuple));
214
215 return 0;
216}
217
218
228{
229#ifdef SO3_DEBUG
230 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerYZX\n");
231#endif
232
233 int q = MTOP(MMget(m, 0));
234 if (q == NIL)
235 {
236 MMset(m, 0, NIL);
237 return 0;
238 }
239
240 int x = MMfetch(m, q, 0);
241 int y = MMfetch(m, q, 1);
242 int z = MMfetch(m, q, 2);
243 int w = MMfetch(m, q, 3);
244
245 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
246 {
247 MMset(m, 0, NIL);
248 return 0;
249 }
250
251 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
252 quat.normalise();
253
254 Ogre::Matrix3 mat;
255 Ogre::Radian xrad;
256 Ogre::Radian yrad;
257 Ogre::Radian zrad;
258
259 quat.ToRotationMatrix(mat);
260 mat.ToEulerAnglesYZX(yrad, xrad, zrad);
261
262 int tuple = MMmalloc(m, 3, TYPETAB);
263 if (tuple == NIL)
264 {
265 MMset(m, 0, NIL);
266 return MERRMEM;
267 }
268 MMstore(m, tuple, 0, FTOM(xrad.valueRadians()));
269 MMstore(m, tuple, 1, FTOM(yrad.valueRadians()));
270 MMstore(m, tuple, 2, FTOM(zrad.valueRadians()));
271 MMset(m, 0, PTOM(tuple));
272 return 0;
273}
274
275
285{
286#ifdef SO3_DEBUG
287 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerZXY\n");
288#endif
289
290 int q = MTOP(MMget(m, 0));
291 if (q == NIL)
292 {
293 MMset(m, 0, NIL);
294 return 0;
295 }
296
297 int x = MMfetch(m, q, 0);
298 int y = MMfetch(m, q, 1);
299 int z = MMfetch(m, q, 2);
300 int w = MMfetch(m, q, 3);
301
302 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
303 {
304 MMset(m, 0, NIL);
305 return 0;
306 }
307
308 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
309 quat.normalise();
310
311 Ogre::Matrix3 mat;
312 Ogre::Radian xrad;
313 Ogre::Radian yrad;
314 Ogre::Radian zrad;
315
316 quat.ToRotationMatrix(mat);
317
318 mat.ToEulerAnglesZXY(yrad, xrad, zrad);
319
320 int tuple = MMmalloc(m, 3, TYPETAB);
321 if (tuple == NIL)
322 {
323 MMset(m, 0, NIL);
324 return MERRMEM;
325 }
326 MMstore(m, tuple, 0, FTOM(xrad.valueRadians()));
327 MMstore(m, tuple, 1, FTOM(yrad.valueRadians()));
328 MMstore(m, tuple, 2, FTOM(zrad.valueRadians()));
329 MMset(m, 0, PTOM(tuple));
330
331 return 0;
332}
333
334
344{
345#ifdef SO3_DEBUG
346 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerZYX\n");
347#endif
348
349 int q = MTOP(MMget(m, 0));
350 if (q == NIL)
351 {
352 MMset(m, 0, NIL);
353 return 0;
354 }
355
356 int x = MMfetch(m, q, 0);
357 int y = MMfetch(m, q, 1);
358 int z = MMfetch(m, q, 2);
359 int w = MMfetch(m, q, 3);
360
361 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
362 {
363 MMset(m, 0, NIL);
364 return 0;
365 }
366
367 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
368
369 quat.normalise();
370 Ogre::Matrix3 mat;
371 Ogre::Radian xrad;
372 Ogre::Radian yrad;
373 Ogre::Radian zrad;
374
375 quat.ToRotationMatrix(mat);
376
377 mat.ToEulerAnglesZYX(yrad, xrad, zrad);
378
379 int tuple = MMmalloc(m, 3, TYPETAB);
380 if (tuple == NIL)
381 {
382 MMset(m, 0, NIL);
383 return MERRMEM;
384 }
385 MMstore(m, tuple, 0, FTOM(xrad.valueRadians()));
386 MMstore(m, tuple, 1, FTOM(yrad.valueRadians()));
387 MMstore(m, tuple, 2, FTOM(zrad.valueRadians()));
388 MMset(m, 0, PTOM(tuple));
389
390 return 0;
391}
392
393
403{
404#ifdef SO3_DEBUG
405 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerDegreeXYZ\n");
406#endif
407
408 int q = MTOP(MMget(m, 0));
409 if (q == NIL)
410 {
411 MMset(m, 0, NIL);
412 return 0;
413 }
414
415 int x = MMfetch(m, q, 0);
416 int y = MMfetch(m, q, 1);
417 int z = MMfetch(m, q, 2);
418 int w = MMfetch(m, q, 3);
419
420 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
421 {
422 MMset(m, 0, NIL);
423 return 0;
424 }
425
426 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
427 quat.normalise();
428
429 Ogre::Matrix3 mat;
430 Ogre::Radian xrad;
431 Ogre::Radian yrad;
432 Ogre::Radian zrad;
433
434 quat.ToRotationMatrix(mat);
435
436 mat.ToEulerAnglesXYZ(yrad, xrad, zrad);
437 int tuple = MMmalloc(m, 3, TYPETAB);
438 if (tuple == NIL)
439 {
440 MMset(m, 0, NIL);
441 return MERRMEM;
442 }
443 MMstore(m, tuple, 0, FTOM(xrad.valueDegrees()));
444 MMstore(m, tuple, 1, FTOM(yrad.valueDegrees()));
445 MMstore(m, tuple, 2, FTOM(zrad.valueDegrees()));
446 MMset(m, 0, PTOM(tuple));
447
448 return 0;
449}
450
451
461{
462#ifdef SO3_DEBUG
463 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerDegreeXZY\n");
464#endif
465
466 int q = MTOP(MMget(m, 0));
467 if (q == NIL)
468 {
469 MMset(m, 0, NIL);
470 return 0;
471 }
472
473 int x = MMfetch(m, q, 0);
474 int y = MMfetch(m, q, 1);
475 int z = MMfetch(m, q, 2);
476 int w = MMfetch(m, q, 3);
477
478 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
479 {
480 MMset(m, 0, NIL);
481 return 0;
482 }
483
484 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
485 quat.normalise();
486
487 Ogre::Matrix3 mat;
488 Ogre::Radian xrad;
489 Ogre::Radian yrad;
490 Ogre::Radian zrad;
491 quat.ToRotationMatrix(mat);
492
493 mat.ToEulerAnglesXZY(yrad, xrad, zrad);
494 int tuple = MMmalloc(m, 3, TYPETAB);
495 if (tuple == NIL)
496 {
497 MMset(m, 0, NIL);
498 return MERRMEM;
499 }
500 MMstore(m, tuple, 0, FTOM(xrad.valueDegrees()));
501 MMstore(m, tuple, 1, FTOM(yrad.valueDegrees()));
502 MMstore(m, tuple, 2, FTOM(zrad.valueDegrees()));
503 MMset(m, 0, PTOM(tuple));
504
505 return 0;
506}
507
508
518{
519#ifdef SO3_DEBUG
520 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerDegreeYXZ\n");
521#endif
522
523 int q = MTOP(MMget(m, 0));
524 if (q == NIL)
525 {
526 MMset(m, 0, NIL);
527 return 0;
528 }
529
530 int x = MMfetch(m, q, 0);
531 int y = MMfetch(m, q, 1);
532 int z = MMfetch(m, q, 2);
533 int w = MMfetch(m, q, 3);
534
535 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
536 {
537 MMset(m, 0, NIL);
538 return 0;
539 }
540
541 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
542 quat.normalise();
543
544 Ogre::Matrix3 mat;
545 Ogre::Radian xrad;
546 Ogre::Radian yrad;
547 Ogre::Radian zrad;
548
549 quat.ToRotationMatrix(mat);
550
551 mat.ToEulerAnglesYXZ(yrad, xrad, zrad);
552
553 int tuple = MMmalloc(m, 3, TYPETAB);
554 if (tuple == NIL)
555 {
556 MMset(m, 0, NIL);
557 return MERRMEM;
558 }
559 MMstore(m, tuple, 0, FTOM(xrad.valueDegrees()));
560 MMstore(m, tuple, 1, FTOM(yrad.valueDegrees()));
561 MMstore(m, tuple, 2, FTOM(zrad.valueDegrees()));
562 MMset(m, 0, PTOM(tuple));
563
564 return 0;
565}
566
567
577{
578#ifdef SO3_DEBUG
579 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerDegreeYZX\n");
580#endif
581
582 int q = MTOP(MMget(m, 0));
583 if (q == NIL)
584 {
585 MMset(m, 0, NIL);
586 return 0;
587 }
588
589 int x = MMfetch(m, q, 0);
590 int y = MMfetch(m, q, 1);
591 int z = MMfetch(m, q, 2);
592 int w = MMfetch(m, q, 3);
593
594 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
595 {
596 MMset(m, 0, NIL);
597 return 0;
598 }
599
600 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
601 quat.normalise();
602
603 Ogre::Matrix3 mat;
604 Ogre::Radian xrad;
605 Ogre::Radian yrad;
606 Ogre::Radian zrad;
607
608 quat.ToRotationMatrix(mat);
609
610 mat.ToEulerAnglesYZX(yrad, xrad, zrad);
611
612 int tuple = MMmalloc(m, 3, TYPETAB);
613 if (tuple == NIL)
614 {
615 MMset(m, 0, NIL);
616 return MERRMEM;
617 }
618 MMstore(m, tuple, 0, FTOM(xrad.valueDegrees()));
619 MMstore(m, tuple, 1, FTOM(yrad.valueDegrees()));
620 MMstore(m, tuple, 2, FTOM(zrad.valueDegrees()));
621 MMset(m, 0, PTOM(tuple));
622
623 return 0;
624}
625
626
636{
637#ifdef SO3_DEBUG
638 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerDegreeZXY\n");
639#endif
640
641 int q = MTOP(MMget(m, 0));
642 if (q == NIL)
643 {
644 MMset(m, 0, NIL);
645 return 0;
646 }
647
648 int x = MMfetch(m, q, 0);
649 int y = MMfetch(m, q, 1);
650 int z = MMfetch(m, q, 2);
651 int w = MMfetch(m, q, 3);
652
653 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
654 {
655 MMset(m, 0, NIL);
656 return 0;
657 }
658
659 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
660 quat.normalise();
661
662 Ogre::Matrix3 mat;
663 Ogre::Radian xrad;
664 Ogre::Radian yrad;
665 Ogre::Radian zrad;
666
667 quat.ToRotationMatrix(mat);
668
669 mat.ToEulerAnglesZXY(yrad, xrad, zrad);
670
671 int tuple = MMmalloc(m, 3, TYPETAB);
672 if (tuple == NIL)
673 {
674 MMset(m, 0, NIL);
675 return MERRMEM;
676 }
677 MMstore(m, tuple, 0, FTOM(xrad.valueDegrees()));
678 MMstore(m, tuple, 1, FTOM(yrad.valueDegrees()));
679 MMstore(m, tuple, 2, FTOM(zrad.valueDegrees()));
680 MMset(m, 0, PTOM(tuple));
681
682 return 0;
683}
684
685
695{
696#ifdef SO3_DEBUG
697 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerDegreeZYX\n");
698#endif
699
700 int q = MTOP(MMget(m, 0));
701 if (q == NIL)
702 {
703 MMset(m, 0, NIL);
704 return 0;
705 }
706
707 int x = MMfetch(m, q, 0);
708 int y = MMfetch(m, q, 1);
709 int z = MMfetch(m, q, 2);
710 int w = MMfetch(m, q, 3);
711
712 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
713 {
714 MMset(m, 0, NIL);
715 return 0;
716 }
717
718 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
719 quat.normalise();
720
721 Ogre::Matrix3 mat;
722 Ogre::Radian xrad;
723 Ogre::Radian yrad;
724 Ogre::Radian zrad;
725
726 quat.ToRotationMatrix(mat);
727
728 mat.ToEulerAnglesZYX(yrad, xrad, zrad);
729
730 int tuple = MMmalloc(m, 3, TYPETAB);
731 if (tuple == NIL)
732 {
733 MMset(m, 0, NIL);
734 return MERRMEM;
735 }
736 MMstore(m, tuple, 0, FTOM(xrad.valueDegrees()));
737 MMstore(m, tuple, 1, FTOM(yrad.valueDegrees()));
738 MMstore(m, tuple, 2, FTOM(zrad.valueDegrees()));
739 MMset(m, 0, PTOM(tuple));
740
741 return 0;
742}
743
744
754{
755#ifdef SO3_DEBUG
756 MMechostr(MSKDEBUG, "SO3MathsEulerXYZToQuat\n");
757#endif
758
759 int vec = MTOP(MMget(m, 0));
760 if (vec == NIL)
761 {
762 MMset(m, 0, NIL);
763 return 0;
764 }
765
766 int x = MMfetch(m, vec, 0);
767 int y = MMfetch(m, vec, 1);
768 int z = MMfetch(m, vec, 2);
769
770 if ((x == NIL) || (y == NIL) || (z == NIL))
771 {
772 MMset(m, 0, NIL);
773 return 0;
774 }
775
776 Ogre::Radian xrad = Ogre::Radian(MTOF(x));
777 Ogre::Radian yrad = Ogre::Radian(MTOF(y));
778 Ogre::Radian zrad = Ogre::Radian(MTOF(z));
779
780 Ogre::Matrix3 mat;
781
782 mat.FromEulerAnglesXYZ(yrad, xrad, zrad);
783
784 Ogre::Quaternion quat;
785 quat.FromRotationMatrix(mat);
786 quat.normalise();
787
788 int tuple = MMmalloc(m, 4, TYPETAB);
789 if (tuple == NIL)
790 {
791 MMset(m, 0, NIL);
792 return MERRMEM;
793 }
794 MMstore(m, tuple, 0, FTOM(quat.x));
795 MMstore(m, tuple, 1, FTOM(quat.y));
796 MMstore(m, tuple, 2, FTOM(quat.z));
797 MMstore(m, tuple, 3, FTOM(quat.w));
798 MMset(m, 0, PTOM(tuple));
799
800 return 0;
801}
802
803
813{
814#ifdef SO3_DEBUG
815 MMechostr(MSKDEBUG, "SO3MathsEulerXZYToQuat\n");
816#endif
817
818 int vec = MTOP(MMget(m, 0));
819 if (vec == NIL)
820 {
821 MMset(m, 0, NIL);
822 return 0;
823 }
824
825 int x = MMfetch(m, vec, 0);
826 int y = MMfetch(m, vec, 1);
827 int z = MMfetch(m, vec, 2);
828
829 if ((x == NIL) || (y == NIL) || (z == NIL))
830 {
831 MMset(m, 0, NIL);
832 return 0;
833 }
834
835 Ogre::Radian xrad = Ogre::Radian(MTOF(x));
836 Ogre::Radian yrad = Ogre::Radian(MTOF(y));
837 Ogre::Radian zrad = Ogre::Radian(MTOF(z));
838
839 Ogre::Matrix3 mat;
840
841 mat.FromEulerAnglesXZY(yrad, xrad, zrad);
842
843 Ogre::Quaternion quat;
844 quat.FromRotationMatrix(mat);
845 quat.normalise();
846
847 int tuple = MMmalloc(m, 4, TYPETAB);
848 if (tuple == NIL)
849 {
850 MMset(m, 0, NIL);
851 return MERRMEM;
852 }
853 MMstore(m, tuple, 0, FTOM(quat.x));
854 MMstore(m, tuple, 1, FTOM(quat.y));
855 MMstore(m, tuple, 2, FTOM(quat.z));
856 MMstore(m, tuple, 3, FTOM(quat.w));
857 MMset(m, 0, PTOM(tuple));
858
859 return 0;
860}
861
862
872{
873#ifdef SO3_DEBUG
874 MMechostr(MSKDEBUG, "SO3MathsEulerYXZToQuat\n");
875#endif
876
877 int vec = MTOP(MMget(m, 0));
878 if (vec == NIL)
879 {
880 MMset(m, 0, NIL);
881 return 0;
882 }
883
884 int x = MMfetch(m, vec, 0);
885 int y = MMfetch(m, vec, 1);
886 int z = MMfetch(m, vec, 2);
887
888 if ((x == NIL) || (y == NIL) || (z == NIL))
889 {
890 MMset(m, 0, NIL);
891 return 0;
892 }
893
894 Ogre::Radian xrad = Ogre::Radian(MTOF(x));
895 Ogre::Radian yrad = Ogre::Radian(MTOF(y));
896 Ogre::Radian zrad = Ogre::Radian(MTOF(z));
897
898 Ogre::Matrix3 mat;
899
900 mat.FromEulerAnglesYXZ(yrad, xrad, zrad);
901
902 Ogre::Quaternion quat;
903 quat.FromRotationMatrix(mat);
904 quat.normalise();
905
906 int tuple = MMmalloc(m, 4, TYPETAB);
907 if (tuple == NIL)
908 {
909 MMset(m, 0, NIL);
910 return MERRMEM;
911 }
912 MMstore(m, tuple, 0, FTOM(quat.x));
913 MMstore(m, tuple, 1, FTOM(quat.y));
914 MMstore(m, tuple, 2, FTOM(quat.z));
915 MMstore(m, tuple, 3, FTOM(quat.w));
916 MMset(m, 0, PTOM(tuple));
917
918 return 0;
919}
920
921
931{
932#ifdef SO3_DEBUG
933 MMechostr(MSKDEBUG, "SO3MathsEulerYZXToQuat\n");
934#endif
935
936 int vec = MTOP(MMget(m, 0));
937 if (vec == NIL)
938 {
939 MMset(m, 0, NIL);
940 return 0;
941 }
942
943 int x = MMfetch(m, vec, 0);
944 int y = MMfetch(m, vec, 1);
945 int z = MMfetch(m, vec, 2);
946
947 if ((x == NIL) || (y == NIL) || (z == NIL))
948 {
949 MMset(m, 0, NIL);
950 return 0;
951 }
952
953 Ogre::Radian xrad = Ogre::Radian(MTOF(x));
954 Ogre::Radian yrad = Ogre::Radian(MTOF(y));
955 Ogre::Radian zrad = Ogre::Radian(MTOF(z));
956
957 Ogre::Matrix3 mat;
958
959 mat.FromEulerAnglesYZX(yrad, xrad, zrad);
960
961 Ogre::Quaternion quat;
962 quat.FromRotationMatrix(mat);
963 quat.normalise();
964
965 int tuple = MMmalloc(m, 4, TYPETAB);
966 if (tuple == NIL)
967 {
968 MMset(m, 0, NIL);
969 return MERRMEM;
970 }
971 MMstore(m, tuple, 0, FTOM(quat.x));
972 MMstore(m, tuple, 1, FTOM(quat.y));
973 MMstore(m, tuple, 2, FTOM(quat.z));
974 MMstore(m, tuple, 3, FTOM(quat.w));
975 MMset(m, 0, PTOM(tuple));
976
977 return 0;
978}
979
980
990{
991#ifdef SO3_DEBUG
992 MMechostr(MSKDEBUG, "SO3MathsEulerZYXToQuat\n");
993#endif
994
995 int vec = MTOP(MMget(m, 0));
996 if (vec == NIL)
997 {
998 MMset(m, 0, NIL);
999 return 0;
1000 }
1001
1002 int x = MMfetch(m, vec, 0);
1003 int y = MMfetch(m, vec, 1);
1004 int z = MMfetch(m, vec, 2);
1005
1006 if ((x == NIL) || (y == NIL) || (z == NIL))
1007 {
1008 MMset(m, 0, NIL);
1009 return 0;
1010 }
1011
1012 Ogre::Radian xrad = Ogre::Radian(MTOF(x));
1013 Ogre::Radian yrad = Ogre::Radian(MTOF(y));
1014 Ogre::Radian zrad = Ogre::Radian(MTOF(z));
1015
1016 Ogre::Matrix3 mat;
1017
1018 mat.FromEulerAnglesZYX(yrad, xrad, zrad);
1019
1020 Ogre::Quaternion quat;
1021 quat.FromRotationMatrix(mat);
1022 quat.normalise();
1023
1024 int tuple = MMmalloc(m, 4, TYPETAB);
1025 if (tuple == NIL)
1026 {
1027 MMset(m, 0, NIL);
1028 return MERRMEM;
1029 }
1030 MMstore(m, tuple, 0, FTOM(quat.x));
1031 MMstore(m, tuple, 1, FTOM(quat.y));
1032 MMstore(m, tuple, 2, FTOM(quat.z));
1033 MMstore(m, tuple, 3, FTOM(quat.w));
1034 MMset(m, 0, PTOM(tuple));
1035
1036 return 0;
1037}
1038
1039
1049{
1050#ifdef SO3_DEBUG
1051 MMechostr(MSKDEBUG, "SO3MathsEulerZXYToQuat\n");
1052#endif
1053
1054 int vec = MTOP(MMget(m, 0));
1055 if (vec == NIL)
1056 {
1057 MMset(m, 0, NIL);
1058 return 0;
1059 }
1060
1061 int x = MMfetch(m, vec, 0);
1062 int y = MMfetch(m, vec, 1);
1063 int z = MMfetch(m, vec, 2);
1064
1065 if ((x == NIL) || (y == NIL) || (z == NIL))
1066 {
1067 MMset(m, 0, NIL);
1068 return 0;
1069 }
1070
1071 Ogre::Radian xrad = Ogre::Radian(MTOF(x));
1072 Ogre::Radian yrad = Ogre::Radian(MTOF(y));
1073 Ogre::Radian zrad = Ogre::Radian(MTOF(z));
1074
1075 Ogre::Matrix3 mat;
1076
1077 mat.FromEulerAnglesZXY(yrad, xrad, zrad);
1078
1079 Ogre::Quaternion quat;
1080 quat.FromRotationMatrix(mat);
1081 quat.normalise();
1082
1083 int tuple = MMmalloc(m, 4, TYPETAB);
1084 if (tuple == NIL)
1085 {
1086 MMset(m, 0, NIL);
1087 return MERRMEM;
1088 }
1089 MMstore(m, tuple, 0, FTOM(quat.x));
1090 MMstore(m, tuple, 1, FTOM(quat.y));
1091 MMstore(m, tuple, 2, FTOM(quat.z));
1092 MMstore(m, tuple, 3, FTOM(quat.w));
1093 MMset(m, 0, PTOM(tuple));
1094
1095 return 0;
1096}
1097
1098
1108int SO3MathsQuatIsEqual(mmachine m)
1109{
1110#ifdef SO3_DEBUG
1111 MMechostr(MSKDEBUG, "SO3MathsQuatIsEqual\n");
1112#endif
1113
1114 int q2 = MTOP(MMpull(m));
1115 int q1 = MTOP(MMget(m, 0));
1116
1117 if (q1 == NIL || q2 == NIL)
1118 {
1119 MMset(m, 0, NIL);
1120 return 0;
1121 }
1122
1123 int x1 = MMfetch(m, q1, 0);
1124 int y1 = MMfetch(m, q1, 1);
1125 int z1 = MMfetch(m, q1, 2);
1126 int w1 = MMfetch(m, q1, 3);
1127
1128 int x2 = MMfetch(m, q2, 0);
1129 int y2 = MMfetch(m, q2, 1);
1130 int z2 = MMfetch(m, q2, 2);
1131 int w2 = MMfetch(m, q2, 3);
1132
1133 if ((x1 == NIL) || (y1 == NIL) || (z1 == NIL) || (w1 == NIL) || (x2 == NIL) || (y2 == NIL) || (z2 == NIL) || (w2 == NIL))
1134 {
1135 MMset(m, 0, NIL);
1136 return 0;
1137 }
1138
1139 Ogre::Quaternion quat1 = Ogre::Quaternion(MTOF(w1), MTOF(x1), MTOF(y1), MTOF(z1));
1140 Ogre::Quaternion quat2 = Ogre::Quaternion(MTOF(w2), MTOF(x2), MTOF(y2), MTOF(z2));
1141
1142 MMset(m, 0, ITOM((quat1 == quat2 ? 1 : 0)));
1143
1144 return 0;
1145}
1146
1156int SO3MathsQuatDiff(mmachine m)
1157{
1158#ifdef SO3_DEBUG
1159 MMechostr(MSKDEBUG, "SO3MathsQuatDiff\n");
1160#endif
1161
1162 int q2 = MTOP(MMpull(m));
1163 int q1 = MTOP(MMget(m, 0));
1164
1165 if (q1 == NIL || q2 == NIL)
1166 {
1167 MMset(m, 0, NIL);
1168 return 0;
1169 }
1170
1171 int x1 = MMfetch(m, q1, 0);
1172 int y1 = MMfetch(m, q1, 1);
1173 int z1 = MMfetch(m, q1, 2);
1174 int w1 = MMfetch(m, q1, 3);
1175
1176 int x2 = MMfetch(m, q2, 0);
1177 int y2 = MMfetch(m, q2, 1);
1178 int z2 = MMfetch(m, q2, 2);
1179 int w2 = MMfetch(m, q2, 3);
1180
1181 if ((x1 == NIL) || (y1 == NIL) || (z1 == NIL) || (w1 == NIL) || (x2 == NIL) || (y2 == NIL) || (z2 == NIL) || (w2 == NIL))
1182 {
1183 MMset(m, 0, NIL);
1184 return 0;
1185 }
1186
1187 Ogre::Quaternion quat1 = Ogre::Quaternion(MTOF(w1), MTOF(x1), MTOF(y1), MTOF(z1));
1188 Ogre::Quaternion quat2 = Ogre::Quaternion(MTOF(w2), MTOF(x2), MTOF(y2), MTOF(z2));
1189
1190 Ogre::Quaternion quatResult = quat1 * quat2.Inverse();
1191 quatResult.normalise();
1192
1193 int tuple = MMmalloc(m, 4, TYPETAB);
1194 if (tuple == NIL)
1195 {
1196 MMset(m, 0, NIL);
1197 return MERRMEM;
1198 }
1199 MMstore(m, tuple, 0, FTOM(quatResult.x));
1200 MMstore(m, tuple, 1, FTOM(quatResult.y));
1201 MMstore(m, tuple, 2, FTOM(quatResult.z));
1202 MMstore(m, tuple, 3, FTOM(quatResult.w));
1203 MMset(m, 0, PTOM(tuple));
1204
1205 return 0;
1206}
1207
1208
1219{
1220#ifdef SO3_DEBUG
1221 MMechostr(MSKDEBUG, "SO3MathsQuatSubstract\n");
1222#endif
1223
1224 int q2 = MTOP(MMpull(m));
1225 int q1 = MTOP(MMget(m, 0));
1226
1227 if (q1 == NIL || q2 == NIL)
1228 {
1229 MMset(m, 0, NIL);
1230 return 0;
1231 }
1232
1233 int x1 = MMfetch(m, q1, 0);
1234 int y1 = MMfetch(m, q1, 1);
1235 int z1 = MMfetch(m, q1, 2);
1236 int w1 = MMfetch(m, q1, 3);
1237
1238 int x2 = MMfetch(m, q2, 0);
1239 int y2 = MMfetch(m, q2, 1);
1240 int z2 = MMfetch(m, q2, 2);
1241 int w2 = MMfetch(m, q2, 3);
1242
1243 if ((x1 == NIL) || (y1 == NIL) || (z1 == NIL) || (w1 == NIL) || (x2 == NIL) || (y2 == NIL) || (z2 == NIL) || (w2 == NIL))
1244 {
1245 MMset(m, 0, NIL);
1246 return 0;
1247 }
1248
1249 Ogre::Quaternion quat1 = Ogre::Quaternion(MTOF(w1), MTOF(x1), MTOF(y1), MTOF(z1));
1250
1251 Ogre::Quaternion quat2 = Ogre::Quaternion(MTOF(w2), MTOF(x2), MTOF(y2), MTOF(z2));
1252
1253 Ogre::Quaternion quatResult = quat1.Inverse() * quat2;
1254 quatResult.normalise();
1255
1256 int tuple = MMmalloc(m, 4, TYPETAB);
1257 if (tuple == NIL)
1258 {
1259 MMset(m, 0, NIL);
1260 return MERRMEM;
1261 }
1262 MMstore(m, tuple, 0, FTOM(quatResult.x));
1263 MMstore(m, tuple, 1, FTOM(quatResult.y));
1264 MMstore(m, tuple, 2, FTOM(quatResult.z));
1265 MMstore(m, tuple, 3, FTOM(quatResult.w));
1266 MMset(m, 0, PTOM(tuple));
1267
1268 return 0;
1269}
1270
1271
1281int SO3MathsQuatAdd(mmachine m)
1282{
1283#ifdef SO3_DEBUG
1284 MMechostr(MSKDEBUG, "SO3MathsQuatAdd\n");
1285#endif
1286
1287 int q2 = MTOP(MMpull(m));
1288 int q1 = MTOP(MMget(m, 0));
1289
1290 if (q1 == NIL || q2 == NIL)
1291 {
1292 MMset(m, 0, NIL);
1293 return 0;
1294 }
1295
1296 int x1 = MMfetch(m, q1, 0);
1297 int y1 = MMfetch(m, q1, 1);
1298 int z1 = MMfetch(m, q1, 2);
1299 int w1 = MMfetch(m, q1, 3);
1300
1301 int x2 = MMfetch(m, q2, 0);
1302 int y2 = MMfetch(m, q2, 1);
1303 int z2 = MMfetch(m, q2, 2);
1304 int w2 = MMfetch(m, q2, 3);
1305
1306 if ((x1 == NIL) || (y1 == NIL) || (z1 == NIL) || (w1 == NIL) || (x2 == NIL) || (y2 == NIL) || (z2 == NIL) || (w2 == NIL))
1307 {
1308 MMset(m, 0, NIL);
1309 return 0;
1310 }
1311
1312 Ogre::Quaternion quat1 = Ogre::Quaternion(MTOF(w1), MTOF(x1), MTOF(y1), MTOF(z1));
1313 quat1.normalise();
1314 Ogre::Quaternion quat2 = Ogre::Quaternion(MTOF(w2), MTOF(x2), MTOF(y2), MTOF(z2));
1315 quat2.normalise();
1316
1317 Ogre::Quaternion quatResult = quat1 * quat2;
1318 quatResult.normalise();
1319
1320 int tuple = MMmalloc(m, 4, TYPETAB);
1321 if (tuple == NIL)
1322 {
1323 MMset(m, 0, NIL);
1324 return MERRMEM;
1325 }
1326
1327 MMstore(m, tuple, 0, FTOM(quatResult.x));
1328 MMstore(m, tuple, 1, FTOM(quatResult.y));
1329 MMstore(m, tuple, 2, FTOM(quatResult.z));
1330 MMstore(m, tuple, 3, FTOM(quatResult.w));
1331 MMset(m, 0, PTOM(tuple));
1332
1333 return 0;
1334}
1335
1336
1349{
1350#ifdef SO3_DEBUG
1351 MMechostr(MSKDEBUG, "SO3MathsQuatInterpolate\n");
1352#endif
1353
1354 int ishort = MTOI(MMpull(m));
1355 int coef = MMpull(m);
1356 int q2 = MTOP(MMpull(m));
1357 int q1 = MTOP(MMget(m, 0));
1358
1359 if ((q1 == NIL) || (q2 == NIL) || (coef == NIL))
1360 {
1361 MMset(m, 0, NIL);
1362 return 0;
1363 }
1364
1365 bool bshort = false;
1366 if (ishort == 1)
1367 bshort = true;
1368
1369 int x1 = MMfetch(m, q1, 0);
1370 int y1 = MMfetch(m, q1, 1);
1371 int z1 = MMfetch(m, q1, 2);
1372 int w1 = MMfetch(m, q1, 3);
1373
1374 int x2 = MMfetch(m, q2, 0);
1375 int y2 = MMfetch(m, q2, 1);
1376 int z2 = MMfetch(m, q2, 2);
1377 int w2 = MMfetch(m, q2, 3);
1378
1379 if ((x1 == NIL) || (y1 == NIL) || (z1 == NIL) || (w1 == NIL) || (x2 == NIL) || (y2 == NIL) || (z2 == NIL) || (w2 == NIL))
1380 {
1381 MMset(m, 0, NIL);
1382 return 0;
1383 }
1384
1385 Ogre::Quaternion quat1 = Ogre::Quaternion(MTOF(w1), MTOF(x1), MTOF(y1), MTOF(z1));
1386 quat1.normalise();
1387 Ogre::Quaternion quat2 = Ogre::Quaternion(MTOF(w2), MTOF(x2), MTOF(y2), MTOF(z2));
1388 quat2.normalise();
1389
1390 Ogre::Quaternion quatResult = Ogre::Quaternion::Slerp((Ogre::Real)MTOF(coef), quat1, quat2, bshort);
1391 quatResult.normalise();
1392
1393 int tuple = MMmalloc(m, 4, TYPETAB);
1394 if (tuple == NIL)
1395 {
1396 MMset(m, 0, NIL);
1397 return MERRMEM;
1398 }
1399
1400 MMstore(m, tuple, 0, FTOM(quatResult.x));
1401 MMstore(m, tuple, 1, FTOM(quatResult.y));
1402 MMstore(m, tuple, 2, FTOM(quatResult.z));
1403 MMstore(m, tuple, 3, FTOM(quatResult.w));
1404 MMset(m, 0, PTOM(tuple));
1405
1406 return 0;
1407}
1408
1409
1419{
1420#ifdef SO3_DEBUG
1421 MMechostr(MSKDEBUG, "SO3MathsDegreeToRadian\n");
1422#endif
1423
1424 int val = MMget(m, 0);
1425 if (val == NIL)
1426 {
1427 MMset(m, 0, NIL);
1428 return 0;
1429 }
1430
1431 Ogre::Degree deg = Ogre::Degree(MTOF(val));
1432 float rad = deg.valueRadians();
1433
1434 MMset(m, 0, FTOM(rad));
1435
1436 return 0;
1437}
1438
1439
1449{
1450#ifdef SO3_DEBUG
1451 MMechostr(MSKDEBUG, "SO3MathsRadianToDegree\n");
1452#endif
1453
1454 int val = MMget(m, 0);
1455 if (val == NIL)
1456 {
1457 MMset(m, 0, NIL);
1458 return 0;
1459 }
1460
1461 Ogre::Radian rad = Ogre::Radian(MTOF(val));
1462 float deg = rad.valueDegrees();
1463
1464 MMset(m, 0, FTOM(deg));
1465
1466 return 0;
1467}
1468
1469
1479int SO3MathsQuatGetRoll(mmachine m)
1480{
1481#ifdef SO3_DEBUG
1482 MMechostr(MSKDEBUG, "SO3MathsQuatGetRoll\n");
1483#endif
1484
1485 int booleen = MTOI(MMpull(m));
1486 int q = MMget(m, 0);
1487 if (q == NIL)
1488 {
1489 MMset(m, 0, NIL);
1490 return 0;
1491 }
1492
1493 int x = MMfetch(m, MTOP(q), 0);
1494 int y = MMfetch(m, MTOP(q), 1);
1495 int z = MMfetch(m, MTOP(q), 2);
1496 int w = MMfetch(m, MTOP(q), 3);
1497
1498 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
1499 {
1500 MMset(m, 0, NIL);
1501 return 0;
1502 }
1503
1504 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
1505 quat.normalise();
1506
1507 Ogre::Radian roll = quat.getRoll(booleen == 1 ? true : false);
1508
1509 MMset(m, 0, FTOM(roll.valueRadians()));
1510 return 0;
1511}
1512
1513
1523int SO3MathsQuatGetYaw(mmachine m)
1524{
1525#ifdef SO3_DEBUG
1526 MMechostr(MSKDEBUG, "SO3MathsQuatGetYaw\n");
1527#endif
1528
1529 int booleen = MTOI(MMpull(m));
1530 int q = MMget(m, 0);
1531 if (q == NIL)
1532 {
1533 MMset(m, 0, NIL);
1534 return 0;
1535 }
1536
1537 int x = MMfetch(m, MTOP(q), 0);
1538 int y = MMfetch(m, MTOP(q), 1);
1539 int z = MMfetch(m, MTOP(q), 2);
1540 int w = MMfetch(m, MTOP(q), 3);
1541
1542 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
1543 {
1544 MMset(m, 0, NIL);
1545 return 0;
1546 }
1547
1548 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
1549 quat.normalise();
1550
1551 Ogre::Radian yaw = quat.getYaw(booleen == 1 ? true : false);
1552
1553 MMset(m, 0, FTOM(yaw.valueRadians()));
1554 return 0;
1555}
1556
1557
1568{
1569#ifdef SO3_DEBUG
1570 MMechostr(MSKDEBUG, "SO3MathsQuatGetPitch\n");
1571#endif
1572
1573 int booleen = MTOI(MMpull(m));
1574 int q = MMget(m, 0);
1575 if (q == NIL)
1576 {
1577 MMset(m, 0, NIL);
1578 return 0;
1579 }
1580
1581 int x = MMfetch(m, MTOP(q), 0);
1582 int y = MMfetch(m, MTOP(q), 1);
1583 int z = MMfetch(m, MTOP(q), 2);
1584 int w = MMfetch(m, MTOP(q), 3);
1585
1586 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
1587 {
1588 MMset(m, 0, NIL);
1589 return 0;
1590 }
1591
1592 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
1593 quat.normalise();
1594
1595 Ogre::Radian pitch = quat.getPitch(booleen == 1 ? true : false);
1596
1597 MMset(m, 0, FTOM(pitch.valueRadians()));
1598 return 0;
1599}
1600
1601
1612{
1613#ifdef SO3_DEBUG
1614 MMechostr(MSKDEBUG, "SO3MathsQuatGetDegreeRoll\n");
1615#endif
1616
1617 int booleen = MTOI(MMpull(m));
1618 int q = MMget(m, 0);
1619 if (q == NIL)
1620 {
1621 MMset(m, 0, NIL);
1622 return 0;
1623 }
1624
1625 int x = MMfetch(m, MTOP(q), 0);
1626 int y = MMfetch(m, MTOP(q), 1);
1627 int z = MMfetch(m, MTOP(q), 2);
1628 int w = MMfetch(m, MTOP(q), 3);
1629
1630 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
1631 {
1632 MMset(m, 0, NIL);
1633 return 0;
1634 }
1635
1636 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
1637 quat.normalise();
1638
1639 Ogre::Radian roll = quat.getRoll(booleen == 1 ? true : false);
1640
1641 MMset(m, 0, FTOM(roll.valueDegrees()));
1642 return 0;
1643}
1644
1645
1656{
1657#ifdef SO3_DEBUG
1658 MMechostr(MSKDEBUG, "SO3MathsQuatGetDegreeYaw\n");
1659#endif
1660
1661 int booleen = MTOI(MMpull(m));
1662 int q = MMget(m, 0);
1663 if (q == NIL)
1664 {
1665 MMset(m, 0, NIL);
1666 return 0;
1667 }
1668
1669 int x = MMfetch(m, MTOP(q), 0);
1670 int y = MMfetch(m, MTOP(q), 1);
1671 int z = MMfetch(m, MTOP(q), 2);
1672 int w = MMfetch(m, MTOP(q), 3);
1673
1674 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
1675 {
1676 MMset(m, 0, NIL);
1677 return 0;
1678 }
1679
1680 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
1681 quat.normalise();
1682
1683 Ogre::Radian yaw = quat.getYaw(booleen == 1 ? true : false);
1684
1685 MMset(m, 0, FTOM(yaw.valueDegrees()));
1686 return 0;
1687}
1688
1689
1700{
1701#ifdef SO3_DEBUG
1702 MMechostr(MSKDEBUG, "SO3MathsQuatGetDegreePitch\n");
1703#endif
1704
1705 int booleen = MTOI(MMpull(m));
1706 int q = MMget(m, 0);
1707 if (q == NIL)
1708 {
1709 MMset(m, 0, NIL);
1710 return 0;
1711 }
1712
1713 int x = MMfetch(m, MTOP(q), 0);
1714 int y = MMfetch(m, MTOP(q), 1);
1715 int z = MMfetch(m, MTOP(q), 2);
1716 int w = MMfetch(m, MTOP(q), 3);
1717
1718 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
1719 {
1720 MMset(m, 0, NIL);
1721 return 0;
1722 }
1723
1724 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
1725 quat.normalise();
1726
1727 Ogre::Radian pitch = quat.getPitch(booleen == 1 ? true : false);
1728
1729 MMset(m, 0, FTOM(pitch.valueDegrees()));
1730 return 0;
1731}
1732
1733
1742int SO3MathsQuatToAxes(mmachine m)
1743{
1744#ifdef SO3_DEBUG
1745 MMechostr(MSKDEBUG, "SO3MathsQuatToAxes\n");
1746#endif
1747
1748 int q = MMget(m, 0);
1749 if (q == NIL)
1750 {
1751 MMset(m, 0, NIL);
1752 return 0;
1753 }
1754
1755 int x = MMfetch(m, MTOP(q), 0);
1756 int y = MMfetch(m, MTOP(q), 1);
1757 int z = MMfetch(m, MTOP(q), 2);
1758 int w = MMfetch(m, MTOP(q), 3);
1759
1760 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
1761 {
1762 MMset(m, 0, NIL);
1763 return 0;
1764 }
1765
1766 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
1767 quat.normalise();
1768
1769 Ogre::Vector3 xAxis;
1770 Ogre::Vector3 yAxis;
1771 Ogre::Vector3 zAxis;
1772
1773 quat.ToAxes(xAxis, yAxis, zAxis);
1774
1775 // x
1776 int xtuple = MMmalloc(m, 3, TYPETAB);
1777 if (xtuple == NIL)
1778 {
1779 MMset(m, 0, NIL);
1780 return MERRMEM;
1781 }
1782 MMstore(m, xtuple, 0, FTOM(xAxis.x));
1783 MMstore(m, xtuple, 1, FTOM(xAxis.y));
1784 MMstore(m, xtuple, 2, FTOM(xAxis.z));
1785 MMpush(m, PTOM(xtuple));
1786
1787 // y
1788 int ytuple = MMmalloc(m, 3, TYPETAB);
1789 if (ytuple == NIL)
1790 {
1791 MMset(m, 0, NIL);
1792 return MERRMEM;
1793 }
1794 MMstore(m, ytuple, 0, FTOM(yAxis.x));
1795 MMstore(m, ytuple, 1, FTOM(yAxis.y));
1796 MMstore(m, ytuple, 2, FTOM(yAxis.z));
1797 MMpush(m, PTOM(ytuple));
1798
1799 // z
1800 int ztuple = MMmalloc(m, 3, TYPETAB);
1801 if (ztuple == NIL)
1802 {
1803 MMset(m, 0, NIL);
1804 return MERRMEM;
1805 }
1806
1807 MMstore(m, ztuple, 0, FTOM(zAxis.x));
1808 MMstore(m, ztuple, 1, FTOM(zAxis.y));
1809 MMstore(m, ztuple, 2, FTOM(zAxis.z));
1810 MMpush(m, PTOM(ztuple));
1811
1812 // FINAL TUPPLE
1813 int result = MMmalloc(m, 3, TYPETAB);
1814 if (result == NIL)
1815 {
1816 MMset(m, 0, NIL);
1817 return MERRMEM;
1818 }
1819 MMstore(m, result, 2, MMpull(m));
1820 MMstore(m, result, 1, MMpull(m));
1821 MMstore(m, result, 0, MMpull(m));
1822 MMset(m, 0, PTOM(result));
1823
1824 return 0;
1825}
1826
1836{
1837#ifdef SO3_DEBUG
1838 MMechostr(MSKDEBUG, "SO3MathsQuatFromAngle\n");
1839#endif
1840
1841 int vec = MMget(m, 0);
1842 if (vec == NIL)
1843 {
1844 MMset(m, 0, NIL);
1845 return 0;
1846 }
1847
1848 int x = MMfetch(m, MTOP(vec), 0);
1849 int y = MMfetch(m, MTOP(vec), 1);
1850 int z = MMfetch(m, MTOP(vec), 2);
1851
1852 if ((x == NIL) || (y == NIL) || (z == NIL))
1853 {
1854 MMset(m, 0, NIL);
1855 return 0;
1856 }
1857
1858 Ogre::Quaternion quatX;
1859 Ogre::Quaternion quatY;
1860 Ogre::Quaternion quatZ;
1861
1862 quatX.FromAngleAxis(Ogre::Radian(MTOF(x)), Ogre::Vector3(1, 0, 0));
1863 quatY.FromAngleAxis(Ogre::Radian(MTOF(y)), Ogre::Vector3(0, 1, 0));
1864 quatZ.FromAngleAxis(Ogre::Radian(MTOF(z)), Ogre::Vector3(0, 0, 1));
1865
1866 Ogre::Quaternion quatResult;
1867 quatResult = quatX * quatY * quatZ;
1868 quatResult.normalise();
1869
1870 int tuple = MMmalloc(m, 4, TYPETAB);
1871 if (tuple == NIL)
1872 {
1873 MMset(m, 0, NIL);
1874 return MERRMEM;
1875 }
1876 MMstore(m, tuple, 0, FTOM((quatResult.x)));
1877 MMstore(m, tuple, 1, FTOM((quatResult.y)));
1878 MMstore(m, tuple, 2, FTOM((quatResult.z)));
1879 MMstore(m, tuple, 3, FTOM((quatResult.w)));
1880 MMset(m, 0, PTOM(tuple));
1881
1882 return 0;
1883}
1884
1894{
1895#ifdef SO3_DEBUG
1896 MMechostr(MSKDEBUG, "SO3MathsQuatFromDegreeAngle\n");
1897#endif
1898
1899 int vec = MMget(m, 0);
1900 if (vec == NIL)
1901 {
1902 MMset(m, 0, NIL);
1903 return 0;
1904 }
1905
1906 int x = MMfetch(m, MTOP(vec), 0);
1907 int y = MMfetch(m, MTOP(vec), 1);
1908 int z = MMfetch(m, MTOP(vec), 2);
1909
1910 if ((x == NIL) || (y == NIL) || (z == NIL))
1911 {
1912 MMset(m, 0, NIL);
1913 return 0;
1914 }
1915
1916 Ogre::Quaternion quatX;
1917 Ogre::Quaternion quatY;
1918 Ogre::Quaternion quatZ;
1919
1920 Ogre::Degree dx = (Ogre::Degree)MTOF(x);
1921 Ogre::Degree dy = (Ogre::Degree)MTOF(y);
1922 Ogre::Degree dz = (Ogre::Degree)MTOF(z);
1923 quatX.FromAngleAxis((Ogre::Radian)dx.valueRadians(), Ogre::Vector3(1, 0, 0));
1924 quatY.FromAngleAxis((Ogre::Radian)dy.valueRadians(), Ogre::Vector3(0, 1, 0));
1925 quatZ.FromAngleAxis((Ogre::Radian)dz.valueRadians(), Ogre::Vector3(0, 0, 1));
1926
1927 Ogre::Quaternion quatResult;
1928 quatResult = quatX * quatY * quatZ;
1929 quatResult.normalise();
1930
1931 int tuple = MMmalloc(m, 4, TYPETAB);
1932 if (tuple == NIL)
1933 {
1934 MMset(m, 0, NIL);
1935 return MERRMEM;
1936 }
1937 MMstore(m, tuple, 0, FTOM((quatResult.x)));
1938 MMstore(m, tuple, 1, FTOM((quatResult.y)));
1939 MMstore(m, tuple, 2, FTOM((quatResult.z)));
1940 MMstore(m, tuple, 3, FTOM((quatResult.w)));
1941 MMset(m, 0, PTOM(tuple));
1942
1943 return 0;
1944}
1945
1946
1957{
1958#ifdef SO3_DEBUG
1959 MMechostr(MSKDEBUG, "SO3MathsQuatFromAngleAxis\n");
1960#endif
1961
1962 int vec = MMpull(m);
1963 int rad = MMget(m, 0);
1964 if ((vec == NIL) || (rad == NIL))
1965 {
1966 MMset(m, 0, NIL);
1967 return 0;
1968 }
1969
1970 int x = MMfetch(m, MTOP(vec), 0);
1971 int y = MMfetch(m, MTOP(vec), 1);
1972 int z = MMfetch(m, MTOP(vec), 2);
1973
1974 if ((x == NIL) || (y == NIL) || (z == NIL))
1975 {
1976 MMset(m, 0, NIL);
1977 return 0;
1978 }
1979
1980 Ogre::Quaternion quatResult;
1981 Ogre::Vector3 axis(MTOF(x), MTOF(y), MTOF(z));
1982 quatResult.FromAngleAxis(Ogre::Radian(MTOF(rad)), axis);
1983 quatResult.normalise();
1984
1985 int tuple = MMmalloc(m, 4, TYPETAB);
1986 if (tuple == NIL)
1987 {
1988 MMset(m, 0, NIL);
1989 return MERRMEM;
1990 }
1991 MMstore(m, tuple, 0, FTOM((quatResult.x)));
1992 MMstore(m, tuple, 1, FTOM((quatResult.y)));
1993 MMstore(m, tuple, 2, FTOM((quatResult.z)));
1994 MMstore(m, tuple, 3, FTOM((quatResult.w)));
1995 MMset(m, 0, PTOM(tuple));
1996
1997 return 0;
1998}
1999
2009{
2010#ifdef SO3_DEBUG
2011 MMechostr(MSKDEBUG, "SO3MathsQuatFromAxes\n");
2012#endif
2013
2014 int z = MTOP(MMpull(m));
2015 int y = MTOP(MMpull(m));
2016 int x = MTOP(MMget(m, 0));
2017 if (x == NIL || y == NIL || z == NIL)
2018 {
2019 MMset(m, 0, NIL);
2020 return 0;
2021 }
2022
2023 Ogre::Vector3 xAxis;
2024 xAxis.x = MTOF(MMfetch(m, x, 0));
2025 xAxis.y = MTOF(MMfetch(m, x, 1));
2026 xAxis.z = MTOF(MMfetch(m, x, 2));
2027
2028 Ogre::Vector3 yAxis;
2029 yAxis.x = MTOF(MMfetch(m, y, 0));
2030 yAxis.y = MTOF(MMfetch(m, y, 1));
2031 yAxis.z = MTOF(MMfetch(m, y, 2));
2032
2033 Ogre::Vector3 zAxis;
2034 zAxis.x = MTOF(MMfetch(m, z, 0));
2035 zAxis.y = MTOF(MMfetch(m, z, 1));
2036 zAxis.z = MTOF(MMfetch(m, z, 2));
2037
2038
2039 Ogre::Quaternion quat;
2040
2041 quat.FromAxes(xAxis, yAxis, zAxis);
2042 quat.normalise();
2043
2044 int tuple = MMmalloc(m, 4, TYPETAB);
2045 if (tuple == NIL)
2046 {
2047 MMset(m, 0, NIL);
2048 return MERRMEM;
2049 }
2050 MMstore(m, tuple, 0, FTOM((quat.x)));
2051 MMstore(m, tuple, 1, FTOM((quat.y)));
2052 MMstore(m, tuple, 2, FTOM((quat.z)));
2053 MMstore(m, tuple, 3, FTOM((quat.w)));
2054 MMset(m, 0, PTOM(tuple));
2055
2056 return 0;
2057}
2058
2059
2069{
2070#ifdef SO3_DEBUG
2071 MMechostr(MSKDEBUG, "SO3MathsQuatGetXaxis\n");
2072#endif
2073
2074 int q = MMget(m, 0);
2075 if (q == NIL)
2076 {
2077 MMset(m, 0, NIL);
2078 return 0;
2079 }
2080
2081 int x = MMfetch(m, MTOP(q), 0);
2082 int y = MMfetch(m, MTOP(q), 1);
2083 int z = MMfetch(m, MTOP(q), 2);
2084 int w = MMfetch(m, MTOP(q), 3);
2085
2086 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
2087 {
2088 MMset(m, 0, NIL);
2089 return 0;
2090 }
2091
2092 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
2093 quat.normalise();
2094
2095 Ogre::Vector3 vec = quat.xAxis();
2096
2097 int tuple = MMmalloc(m, 3, TYPETAB);
2098 if (tuple == NIL)
2099 {
2100 MMset(m, 0, NIL);
2101 return MERRMEM;
2102 }
2103 MMstore(m, tuple, 0, FTOM((vec.x)));
2104 MMstore(m, tuple, 1, FTOM((vec.y)));
2105 MMstore(m, tuple, 2, FTOM((vec.z)));
2106 MMset(m, 0, PTOM(tuple));
2107
2108 return 0;
2109}
2110
2111
2121{
2122#ifdef SO3_DEBUG
2123 MMechostr(MSKDEBUG, "SO3MathsQuatGetYaxis\n");
2124#endif
2125
2126 int q = MTOP(MMget(m, 0));
2127 if (q == NIL)
2128 {
2129 MMset(m, 0, NIL);
2130 return 0;
2131 }
2132
2133 int x = MMfetch(m, q, 0);
2134 int y = MMfetch(m, q, 1);
2135 int z = MMfetch(m, q, 2);
2136 int w = MMfetch(m, q, 3);
2137
2138 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
2139 {
2140 MMset(m, 0, NIL);
2141 return 0;
2142 }
2143
2144 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
2145 quat.normalise();
2146
2147 Ogre::Vector3 vec = quat.yAxis();
2148
2149 int tuple = MMmalloc(m, 3, TYPETAB);
2150 if (tuple == NIL)
2151 {
2152 MMset(m, 0, NIL);
2153 return MERRMEM;
2154 }
2155 MMstore(m, tuple, 0, FTOM((vec.x)));
2156 MMstore(m, tuple, 1, FTOM((vec.y)));
2157 MMstore(m, tuple, 2, FTOM((vec.z)));
2158 MMset(m, 0, PTOM(tuple));
2159
2160 return 0;
2161}
2162
2163
2173{
2174#ifdef SO3_DEBUG
2175 MMechostr(MSKDEBUG, "SO3MathsQuatGetZaxis\n");
2176#endif
2177
2178 int q = MTOP(MMget(m, 0));
2179 if (q == NIL)
2180 {
2181 MMset(m, 0, NIL);
2182 return 0;
2183 }
2184
2185 int x = MMfetch(m, q, 0);
2186 int y = MMfetch(m, q, 1);
2187 int z = MMfetch(m, q, 2);
2188 int w = MMfetch(m, q, 3);
2189
2190 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
2191 {
2192 MMset(m, 0, NIL);
2193 return 0;
2194 }
2195
2196 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
2197 quat.normalise();
2198
2199 Ogre::Vector3 vec = quat.zAxis();
2200
2201 int tuple = MMmalloc(m, 3, TYPETAB);
2202 if (tuple == NIL)
2203 {
2204 MMset(m, 0, NIL);
2205 return MERRMEM;
2206 }
2207 MMstore(m, tuple, 0, FTOM((vec.x)));
2208 MMstore(m, tuple, 1, FTOM((vec.y)));
2209 MMstore(m, tuple, 2, FTOM((vec.z)));
2210 MMset(m, 0, PTOM(tuple));
2211
2212 return 0;
2213}
2214
2215
2226{
2227#ifdef SO3_DEBUG
2228 MMechostr(MSKDEBUG, "SO3MathsQuatGetDirection\n");
2229#endif
2230
2231 int v = MTOP(MMpull(m));
2232 int q = MTOP(MMget(m, 0));
2233
2234 if (v == NIL || q == NIL)
2235 {
2236 MMset(m, 0, NIL);
2237 return 0;
2238 }
2239
2240 int vx = MMfetch(m, v, 0);
2241 int vy = MMfetch(m, v, 1);
2242 int vz = MMfetch(m, v, 2);
2243
2244 if ((vx == NIL) || (vy == NIL) || (vz == NIL))
2245 {
2246 MMset(m, 0, NIL);
2247 return 0;
2248 }
2249
2250 Ogre::Vector3 vec = Ogre::Vector3(MTOF(vx), MTOF(vy), MTOF(vz));
2251
2252 int qx = MMfetch(m, q, 0);
2253 int qy = MMfetch(m, q, 1);
2254 int qz = MMfetch(m, q, 2);
2255 int qw = MMfetch(m, q, 3);
2256
2257 if ((qx == NIL) || (qy == NIL) || (qz == NIL) || (qw == NIL))
2258 {
2259 MMset(m, 0, NIL);
2260 return 0;
2261 }
2262
2263 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(qw), MTOF(qx), MTOF(qy), MTOF(qz));
2264 quat.normalise();
2265
2266 Ogre::Vector3 rvec = quat * vec;
2267
2268 int tuple = MMmalloc(m, 3, TYPETAB);
2269 if (tuple == NIL)
2270 {
2271 MMset(m, 0, NIL);
2272 return MERRMEM;
2273 }
2274 MMstore(m, tuple, 0, FTOM((rvec.x)));
2275 MMstore(m, tuple, 1, FTOM((rvec.y)));
2276 MMstore(m, tuple, 2, FTOM((rvec.z)));
2277 MMset(m, 0, PTOM(tuple));
2278
2279 return 0;
2280}
2281
2282
2291int SO3MathsClampValue(mmachine m)
2292{
2293#ifdef SO3_DEBUG
2294 MMechostr(MSKDEBUG, "SO3MathsClampValue\n");
2295#endif
2296
2297 int valMax = MMpull(m);
2298 int valMin = MMpull(m);
2299 int val = MMget(m, 0);
2300
2301 if ((val == NIL) || (valMin == NIL) || (valMax == NIL))
2302 {
2303 MMset(m, 0, NIL);
2304 return 0;
2305 }
2306
2307 float result = Ogre::Math::Clamp(MTOF(val), MTOF(valMin), MTOF(valMax));
2308
2309 MMset(m, 0, FTOM(result));
2310
2311 return 0;
2312}
2313
2314
2325{
2326#ifdef SO3_DEBUG
2327 MMechostr(MSKDEBUG, "SO3MathsGetRotationTo\n");
2328#endif
2329
2330 int scolVector2 = MTOP(MMpull(m));
2331 int scolVector1 = MTOP(MMget(m, 0));
2332 if ((scolVector1 == NIL) || (scolVector2 == NIL))
2333 {
2334 MMset(m, 0, NIL);
2335 return 0;
2336 }
2337
2338 // Get the first vector from Scol VM.
2339 int xTemp = MMfetch(m, scolVector1, 0);
2340 int yTemp = MMfetch(m, scolVector1, 1);
2341 int zTemp = MMfetch(m, scolVector1, 2);
2342 if ((xTemp == NIL)
2343 || (yTemp == NIL)
2344 || (zTemp == NIL))
2345 {
2346 MMset(m, 0, NIL);
2347 return 0;
2348 }
2349 Ogre::Vector3 sourceVector(MTOF(xTemp), MTOF(yTemp), MTOF(zTemp));
2350
2351 // Get the second vector from Scol VM.
2352 xTemp = MMfetch(m, scolVector2, 0);
2353 yTemp = MMfetch(m, scolVector2, 1);
2354 zTemp = MMfetch(m, scolVector2, 2);
2355 if ((xTemp == NIL)
2356 || (yTemp == NIL)
2357 || (zTemp == NIL))
2358 {
2359 MMset(m, 0, NIL);
2360 return 0;
2361 }
2362 Ogre::Vector3 destinationVector(MTOF(xTemp), MTOF(yTemp), MTOF(zTemp));
2363
2364 // Compute quaternion to apply to go from source axes to destination axes.
2365 Ogre::Quaternion quatResult = sourceVector.getRotationTo(destinationVector);
2366 quatResult.normalise();
2367
2368 // Send result to Scol VM.
2369 int tuple = MMmalloc(m, 4, TYPETAB);
2370 if (tuple == NIL)
2371 {
2372 MMset(m, 0, NIL);
2373 return MERRMEM;
2374 }
2375 MMstore(m, tuple, 0, FTOM((quatResult.x)));
2376 MMstore(m, tuple, 1, FTOM((quatResult.y)));
2377 MMstore(m, tuple, 2, FTOM((quatResult.z)));
2378 MMstore(m, tuple, 3, FTOM((quatResult.w)));
2379 MMset(m, 0, PTOM(tuple));
2380 return 0;
2381}
2382
2383
2394{
2395#ifdef SO3_DEBUG
2396 MMechostr(MSKDEBUG, "SO3MathsGetRotationToAxis\n");
2397#endif
2398
2399 int scolVector2 = MTOP(MMpull(m));
2400 int scolVector1 = MTOP(MMget(m, 0));
2401 if ((scolVector1 == NIL) || (scolVector2 == NIL))
2402 {
2403 MMset(m, 0, NIL);
2404 return 0;
2405 }
2406
2407 // Get the first vector from Scol VM.
2408 int xTemp = MMfetch(m, scolVector1, 0);
2409 int yTemp = MMfetch(m, scolVector1, 1);
2410 int zTemp = MMfetch(m, scolVector1, 2);
2411 if ((xTemp == NIL)
2412 || (yTemp == NIL)
2413 || (zTemp == NIL))
2414 {
2415 MMset(m, 0, NIL);
2416 return 0;
2417 }
2418 Ogre::Vector3 src(MTOF(xTemp), MTOF(yTemp), MTOF(zTemp));
2419
2420 // Get the second vector from Scol VM.
2421 xTemp = MMfetch(m, scolVector2, 0);
2422 yTemp = MMfetch(m, scolVector2, 1);
2423 zTemp = MMfetch(m, scolVector2, 2);
2424 if ((xTemp == NIL)
2425 || (yTemp == NIL)
2426 || (zTemp == NIL))
2427 {
2428 MMset(m, 0, NIL);
2429 return 0;
2430 }
2431 Ogre::Vector3 axis(MTOF(xTemp), MTOF(yTemp), MTOF(zTemp));
2432
2433 //OrthoNormalize
2434 // N' = |N|
2435 // T' = |T - (N' dot T) N'|
2436 // B' = |B - (N' dot B) N' - (T' dot B) T'|
2437
2438 Ogre::Vector3 forward = src;
2439 forward.normalise();
2440
2441 Ogre::Vector3 up = axis;
2442 up -= forward * forward.dotProduct(up);
2443 up.normalise();
2444
2445 Ogre::Vector3 right = up.crossProduct(forward);
2446 Ogre::Quaternion quatResult;
2447 quatResult.w = sqrtf(1.0f + right.x + up.y + forward.z) * 0.5f;
2448 float w4_recip = 1.0f / (4.0f * quatResult.w);
2449 quatResult.x = (up.z - forward.y) * w4_recip;
2450 quatResult.y = (forward.x - right.z) * w4_recip;
2451 quatResult.z = (right.y - up.x) * w4_recip;
2452
2453 // Send result to Scol VM.
2454 int tuple = MMmalloc(m, 4, TYPETAB);
2455 if (tuple == NIL)
2456 {
2457 MMset(m, 0, NIL);
2458 return MERRMEM;
2459 }
2460 MMstore(m, tuple, 0, FTOM((quatResult.x)));
2461 MMstore(m, tuple, 1, FTOM((quatResult.y)));
2462 MMstore(m, tuple, 2, FTOM((quatResult.z)));
2463 MMstore(m, tuple, 3, FTOM((quatResult.w)));
2464 MMset(m, 0, PTOM(tuple));
2465 return 0;
2466}
2467
2468
2483{
2484#ifdef SO3_DEBUG
2485 MMechostr(MSKDEBUG, "SO3MathsVectorDotProduct\n");
2486#endif
2487
2488 int scolVector2 = MTOP(MMpull(m));
2489 int scolVector1 = MTOP(MMget(m, 0));
2490 if ((scolVector1 == NIL) || (scolVector2 == NIL))
2491 {
2492 MMset(m, 0, NIL);
2493 return 0;
2494 }
2495
2496 // Get the first vector from Scol VM.
2497 int xTemp = MMfetch(m, scolVector1, 0);
2498 int yTemp = MMfetch(m, scolVector1, 1);
2499 int zTemp = MMfetch(m, scolVector1, 2);
2500 if ((xTemp == NIL)
2501 || (yTemp == NIL)
2502 || (zTemp == NIL))
2503 {
2504 MMset(m, 0, NIL);
2505 return 0;
2506 }
2507 Ogre::Vector3 referenceVector(MTOF(xTemp), MTOF(yTemp), MTOF(zTemp));
2508
2509 // Get the second vector from Scol VM.
2510 xTemp = MMfetch(m, scolVector2, 0);
2511 yTemp = MMfetch(m, scolVector2, 1);
2512 zTemp = MMfetch(m, scolVector2, 2);
2513 if ((xTemp == NIL)
2514 || (yTemp == NIL)
2515 || (zTemp == NIL))
2516 {
2517 MMset(m, 0, NIL);
2518 return 0;
2519 }
2520 Ogre::Vector3 destinationVector(MTOF(xTemp), MTOF(yTemp), MTOF(zTemp));
2521
2522 // Compute dot product.
2523 Ogre::Real scalarProduct = referenceVector.dotProduct(destinationVector);
2524
2525 // Send result to Scol VM.
2526 MMset(m, 0, FTOM(scalarProduct));
2527 return 0;
2528}
2529
2530
2540int SO3MathsQuatRotate(mmachine m)
2541{
2542#ifdef SO3_DEBUG
2543 MMechostr(MSKDEBUG, "SO3MathsQuatRotate\n");
2544#endif
2545
2546 int v = MTOP(MMpull(m));
2547 int q = MTOP(MMget(m, 0));
2548
2549 if (v == NIL || q == NIL)
2550 {
2551 MMset(m, 0, NIL);
2552 return 0;
2553 }
2554
2555 int vyaw = MMfetch(m, v, 0);
2556 int vpitch = MMfetch(m, v, 1);
2557 int vroll = MMfetch(m, v, 2);
2558
2559 if ((vyaw == NIL) || (vpitch == NIL) || (vroll == NIL))
2560 {
2561 MMset(m, 0, NIL);
2562 return 0;
2563 }
2564
2565 int qx = MMfetch(m, q, 0);
2566 int qy = MMfetch(m, q, 1);
2567 int qz = MMfetch(m, q, 2);
2568 int qw = MMfetch(m, q, 3);
2569
2570 if ((qx == NIL) || (qy == NIL) || (qz == NIL) || (qw == NIL))
2571 {
2572 MMset(m, 0, NIL);
2573 return 0;
2574 }
2575
2576 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(qw), MTOF(qx), MTOF(qy), MTOF(qz));
2577 Ogre::Euler euler(quat);
2578
2579 euler.rotate(Ogre::Radian(MTOF(vyaw)), Ogre::Radian(MTOF(vpitch)), Ogre::Radian(MTOF(vroll)));
2580 Ogre::Quaternion quatResult = euler.toQuaternion();
2581
2582 // Send result to Scol VM.
2583 int tuple = MMmalloc(m, 4, TYPETAB);
2584 if (tuple == NIL)
2585 {
2586 MMset(m, 0, NIL);
2587 return MERRMEM;
2588 }
2589 MMstore(m, tuple, 0, FTOM((quatResult.x)));
2590 MMstore(m, tuple, 1, FTOM((quatResult.y)));
2591 MMstore(m, tuple, 2, FTOM((quatResult.z)));
2592 MMstore(m, tuple, 3, FTOM((quatResult.w)));
2593 MMset(m, 0, PTOM(tuple));
2594 return 0;
2595}
2596
2606{
2607#ifdef SO3_DEBUG
2608 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerDegreePYR\n");
2609#endif
2610
2611 int q = MTOP(MMget(m, 0));
2612 if (q == NIL)
2613 {
2614 MMset(m, 0, NIL);
2615 return 0;
2616 }
2617
2618 int x = MMfetch(m, q, 0);
2619 int y = MMfetch(m, q, 1);
2620 int z = MMfetch(m, q, 2);
2621 int w = MMfetch(m, q, 3);
2622
2623 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
2624 {
2625 MMset(m, 0, NIL);
2626 return 0;
2627 }
2628
2629 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
2630 Ogre::Euler euler(quat);
2631
2632 Ogre::Radian xrad = euler.getPitch();
2633 Ogre::Radian yrad = euler.getYaw();
2634 Ogre::Radian zrad = euler.getRoll();
2635
2636 int tuple = MMmalloc(m, 3, TYPETAB);
2637 if (tuple == NIL)
2638 {
2639 MMset(m, 0, NIL);
2640 return MERRMEM;
2641 }
2642 MMstore(m, tuple, 0, FTOM(xrad.valueDegrees()));
2643 MMstore(m, tuple, 1, FTOM(yrad.valueDegrees()));
2644 MMstore(m, tuple, 2, FTOM(zrad.valueDegrees()));
2645 MMset(m, 0, PTOM(tuple));
2646
2647 return 0;
2648}
2649
2659{
2660#ifdef SO3_DEBUG
2661 MMechostr(MSKDEBUG, "SO3MathsQuatToEulerPYR\n");
2662#endif
2663
2664 int q = MTOP(MMget(m, 0));
2665 if (q == NIL)
2666 {
2667 MMset(m, 0, NIL);
2668 return 0;
2669 }
2670
2671 int x = MMfetch(m, q, 0);
2672 int y = MMfetch(m, q, 1);
2673 int z = MMfetch(m, q, 2);
2674 int w = MMfetch(m, q, 3);
2675
2676 if ((x == NIL) || (y == NIL) || (z == NIL) || (w == NIL))
2677 {
2678 MMset(m, 0, NIL);
2679 return 0;
2680 }
2681
2682 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
2683 Ogre::Euler euler(quat);
2684
2685 Ogre::Radian xrad = euler.getPitch();
2686 Ogre::Radian yrad = euler.getYaw();
2687 Ogre::Radian zrad = euler.getRoll();
2688
2689 int tuple = MMmalloc(m, 3, TYPETAB);
2690 if (tuple == NIL)
2691 {
2692 MMset(m, 0, NIL);
2693 return MERRMEM;
2694 }
2695 MMstore(m, tuple, 0, FTOM(xrad.valueRadians()));
2696 MMstore(m, tuple, 1, FTOM(yrad.valueRadians()));
2697 MMstore(m, tuple, 2, FTOM(zrad.valueRadians()));
2698 MMset(m, 0, PTOM(tuple));
2699
2700 return 0;
2701}
2702
2712{
2713#ifdef SO3_DEBUG
2714 MMechostr(MSKDEBUG, "SO3MathsEulerPYRToQuat\n");
2715#endif
2716
2717 int q = MTOP(MMget(m, 0));
2718 if (q == NIL)
2719 {
2720 MMset(m, 0, NIL);
2721 return 0;
2722 }
2723
2724 int p = MMfetch(m, q, 0);
2725 int y = MMfetch(m, q, 1);
2726 int r = MMfetch(m, q, 2);
2727
2728 if ((p == NIL) || (y == NIL) || (r == NIL))
2729 {
2730 MMset(m, 0, NIL);
2731 return 0;
2732 }
2733
2734 Ogre::Euler euler(MTOF(y), MTOF(p), MTOF(r));
2735 Ogre::Quaternion quat = euler.toQuaternion();
2736
2737 int tuple = MMmalloc(m, 4, TYPETAB);
2738 if (tuple == NIL)
2739 {
2740 MMset(m, 0, NIL);
2741 return MERRMEM;
2742 }
2743 MMstore(m, tuple, 0, FTOM(quat.x));
2744 MMstore(m, tuple, 1, FTOM(quat.y));
2745 MMstore(m, tuple, 2, FTOM(quat.z));
2746 MMstore(m, tuple, 3, FTOM(quat.w));
2747 MMset(m, 0, PTOM(tuple));
2748
2749 return 0;
2750}
2751
2752
2753NativeDefinition natSO3Maths[] = {
2754 { "SO3MathsQuatToEulerXYZ", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerXYZ },
2755 { "SO3MathsQuatToEulerXZY", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerXZY },
2756 { "SO3MathsQuatToEulerYXZ", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerYXZ },
2757 { "SO3MathsQuatToEulerYZX", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerYZX },
2758 { "SO3MathsQuatToEulerZXY", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerZXY },
2759 { "SO3MathsQuatToEulerZYX", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerZYX },
2760 { "SO3MathsQuatToEulerDegreeXYZ", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerDegreeXYZ },
2761 { "SO3MathsQuatToEulerDegreeXZY", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerDegreeXZY },
2762 { "SO3MathsQuatToEulerDegreeYXZ", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerDegreeYXZ },
2763 { "SO3MathsQuatToEulerDegreeYZX", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerDegreeYZX },
2764 { "SO3MathsQuatToEulerDegreeZXY", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerDegreeZXY },
2765 { "SO3MathsQuatToEulerDegreeZYX", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerDegreeZYX },
2766 { "SO3MathsEulerXYZToQuat", 1, "fun [[F F F]] [F F F F]", SO3MathsEulerXYZToQuat },
2767 { "SO3MathsEulerXZYToQuat", 1, "fun [[F F F]] [F F F F]", SO3MathsEulerXZYToQuat },
2768 { "SO3MathsEulerYXZToQuat", 1, "fun [[F F F]] [F F F F]", SO3MathsEulerYXZToQuat },
2769 { "SO3MathsEulerYZXToQuat", 1, "fun [[F F F]] [F F F F]", SO3MathsEulerYZXToQuat },
2770 { "SO3MathsEulerZYXToQuat", 1, "fun [[F F F]] [F F F F]", SO3MathsEulerZYXToQuat },
2771 { "SO3MathsEulerZXYToQuat", 1, "fun [[F F F]] [F F F F]", SO3MathsEulerZXYToQuat },
2772 { "SO3MathsQuatDiff", 2, "fun [[F F F F] [F F F F]] [F F F F]", SO3MathsQuatDiff },
2773 { "SO3MathsQuatSubstract", 2, "fun [[F F F F] [F F F F]] [F F F F]", SO3MathsQuatSubstract },
2774 { "SO3MathsQuatAdd", 2, "fun [[F F F F] [F F F F]] [F F F F]", SO3MathsQuatAdd },
2775 { "SO3MathsDegreeToRadian", 1, "fun [F] F", SO3MathsDegreeToRadian },
2776 { "SO3MathsRadianToDegree", 1, "fun [F] F", SO3MathsRadianToDegree },
2777 { "SO3MathsQuatGetRoll", 2, "fun [[F F F F] I] F", SO3MathsQuatGetRoll },
2778 { "SO3MathsQuatGetYaw", 2, "fun [[F F F F] I] F", SO3MathsQuatGetYaw },
2779 { "SO3MathsQuatGetPitch", 2, "fun [[F F F F] I] F", SO3MathsQuatGetPitch },
2780 { "SO3MathsQuatGetDegreeRoll", 2, "fun [[F F F F] I] F", SO3MathsQuatGetDegreeRoll },
2781 { "SO3MathsQuatGetDegreeYaw", 2, "fun [[F F F F] I] F", SO3MathsQuatGetDegreeYaw },
2782 { "SO3MathsQuatGetDegreePitch", 2, "fun [[F F F F] I] F", SO3MathsQuatGetDegreePitch },
2783 { "SO3MathsQuatToAxes", 1, "fun [[F F F F]] [[F F F] [F F F] [F F F]]", SO3MathsQuatToAxes },
2784 { "SO3MathsQuatFromAxes", 3, "fun [[F F F] [F F F] [F F F]] [F F F F]", SO3MathsQuatFromAxes },
2785 { "SO3MathsQuatGetXaxis", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatGetXaxis },
2786 { "SO3MathsQuatGetYaxis", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatGetYaxis },
2787 { "SO3MathsQuatGetZaxis", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatGetZaxis },
2788 { "SO3MathsQuatInterpolate", 4, "fun [[F F F F] [F F F F] F I] [F F F F]", SO3MathsQuatInterpolate },
2789 { "SO3MathsQuatGetDirection", 2, "fun [[F F F F] [F F F]] [F F F]", SO3MathsQuatGetDirection },
2790 { "SO3MathsQuatFromAngle", 1, "fun [[F F F]] [F F F F]", SO3MathsQuatFromAngle },
2791 { "SO3MathsQuatFromDegreeAngle", 1, "fun [[F F F]] [F F F F]", SO3MathsQuatFromDegreeAngle },
2792 { "SO3MathsQuatFromAngleAxis", 2, "fun [F [F F F]] [F F F F]", SO3MathsQuatFromAngleAxis },
2793 { "SO3MathsGetRotationTo", 2, "fun [[F F F] [F F F]] [F F F F]", SO3MathsGetRotationTo },
2794 { "SO3MathsGetRotationToAxis", 2, "fun [[F F F] [F F F]] [F F F F]", SO3MathsGetRotationToAxis },
2795 { "SO3MathsClampValue", 3, "fun [F F F] F", SO3MathsClampValue },
2796 { "SO3MathsVectorDotProduct", 2, "fun [[F F F] [F F F]] F", SO3MathsVectorDotProduct },
2797 { "SO3MathsQuatRotate", 2, "fun [[F F F F] [F F F]] [F F F F]", SO3MathsQuatRotate },
2798 { "SO3MathsQuatToEulerDegreePYR", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerDegreePYR },
2799 { "SO3MathsQuatToEulerPYR", 1, "fun [[F F F F]] [F F F]", SO3MathsQuatToEulerPYR },
2800 { "SO3MathsEulerPYRToQuat", 1, "fun [[F F F]] [F F F F]", SO3MathsEulerPYRToQuat },
2801 { "SO3MathsQuatIsEqual", 2, "fun [[F F F F] [F F F F]] I", SO3MathsQuatIsEqual }
2802};
2803
2804
2810int SCOLloadMaths(mmachine m, cbmachine w)
2811{
2812 return PKhardpak2(m, "SO3Maths.pkg", sizeof(natSO3Maths) / sizeof(natSO3Maths[0]), natSO3Maths);
2813}
2814
2815
2821{
2822 return 0;
2823}
int SCOLloadMaths(mmachine m, cbmachine w)
Load the SO3Engine Mathematical functions.
NativeDefinition natSO3Maths[]
int SCOLfreeMaths()
free the SO3Engine Lights function
MMechostr(MSKDEBUG, " > Start loading Plugin SO3Engine dll\n")
SCOL_EXPORT int cbmachine w
Definition SO3SCOL.cpp:5150
Class for Euler rotations.
Definition SO3Euler.h:25
Euler & rotate(const Ogre::Radian &y, const Ogre::Radian &p, const Ogre::Radian &r)
Apply all relative rotations at once.
Definition SO3Euler.h:165
Ogre::Radian getYaw() const
Get the Yaw angle.
Definition SO3Euler.h:70
Ogre::Quaternion toQuaternion()
Calculate the quaternion of the euler object.
Definition SO3Euler.h:187
Ogre::Radian getRoll() const
Get the Roll angle.
Definition SO3Euler.h:76
Ogre::Radian getPitch() const
Get the Pitch angle.
Definition SO3Euler.h:73
int SO3MathsQuatFromAngle(mmachine m)
SO3MathsQuatFromAngle : function to get Quaternion from X Y Z angle.
int SO3MathsEulerXZYToQuat(mmachine m)
SO3MathsEulerXZYToQuat : Scol function to transform euler angles (XZY) (radians) in a quaternion !...
int SO3MathsQuatToEulerYZX(mmachine m)
SO3MathsQuatToEulerYZX : Scol function to transform quaternion vector in euler angles (YZX) !...
int SO3MathsQuatIsEqual(mmachine m)
SO3MathsQuatIsEqual : Scol function test two quaternion equality.
int SO3MathsQuatToEulerDegreeXYZ(mmachine m)
SO3MathsQuatToEulerDegreeXYZ : Scol function to transform quaternion vector in euler angles (degree) ...
int SO3MathsQuatGetPitch(mmachine m)
SO3MathsQuatGetPitch : function to get the pitch value from quaternion.
int SO3MathsQuatToEulerYXZ(mmachine m)
SO3MathsQuatToEulerYXZ : Scol function to transform quaternion vector in euler angles (YXZ) !...
int SO3MathsQuatRotate(mmachine m)
SO3MathsQuatRotate : Scol function to rotate a quaternion.
int SO3MathsQuatToEulerDegreeZXY(mmachine m)
SO3MathsQuatToEulerDegreeZXY : Scol function to transform quaternion vector in euler angles (degree) ...
int SO3MathsQuatFromAxes(mmachine m)
SO3MathsQuatFromAxes : function to get Quaternion from axis.
int SO3MathsQuatDiff(mmachine m)
SO3MathsQuatDiff : Scol function to make the mathematical difference between two quaternions.
int SO3MathsRadianToDegree(mmachine m)
SO3MathsRadianToDegree : function to convert a value from radians to degrees.
int SO3MathsQuatGetDegreePitch(mmachine m)
SO3MathsQuatGetDegreePitch : function to get the pitch value in degree from quaternion.
int SO3MathsClampValue(mmachine m)
SO3MathsClampValue : function to clamp a value.
int SO3MathsQuatGetDegreeRoll(mmachine m)
SO3MathsQuatGetDegreeRoll : function to get the roll value in degree from quaternion.
int SO3MathsQuatGetDegreeYaw(mmachine m)
SO3MathsQuatGetDegreeYaw : function to get the yaw value in degree from quaternion.
int SO3MathsQuatToEulerZYX(mmachine m)
SO3MathsQuatToEulerZYX : Scol function to transform quaternion vector in euler angles (ZYX) !...
int SO3MathsQuatToEulerDegreeYXZ(mmachine m)
SO3MathsQuatToEulerDegreeYXZ : Scol function to transform quaternion vector in euler angles (degree) ...
int SO3MathsQuatSubstract(mmachine m)
SO3MathsQuatSubstract : Scol function to substract two quaternions.
int SO3MathsDegreeToRadian(mmachine m)
SO3MathsDegreeToRadian : function to convert a value from degrees to radians.
int SO3MathsQuatInterpolate(mmachine m)
SO3MathsQuatInterpolate : function to interpolate two quaternions.
int SO3MathsQuatGetZaxis(mmachine m)
SO3MathsQuatGetZaxis : function to get Z-axis from quaternion.
int SO3MathsQuatFromAngleAxis(mmachine m)
SO3MathsQuatFromAngleAxis : function to get Quaternion from angle axis.
int SO3MathsEulerXYZToQuat(mmachine m)
SO3MathsEulerXYZToQuat : Scol function to transform euler angles (XYZ) (radians) in a quaternion !...
int SO3MathsEulerZXYToQuat(mmachine m)
SO3MathsEulerZXYToQuat : Scol function to transform euler angles (ZXY) (radians) in a quaternion !...
int SO3MathsEulerPYRToQuat(mmachine m)
SO3MathsEulerPYRToQuat : Scol function to transform quaternion vector in euler angles (degree) (YZX)
int SO3MathsQuatToEulerXYZ(mmachine m)
Main include.
Definition SCOLMaths.cpp:50
int SO3MathsQuatGetRoll(mmachine m)
SO3MathsQuatGetRoll : function to get the roll value from quaternion.
int SO3MathsQuatToEulerPYR(mmachine m)
SO3MathsQuatToEulerPYR : Scol function to transform quaternion vector in euler angles (Pitch Yaw Roll...
int SO3MathsQuatGetYaxis(mmachine m)
SO3MathsQuatGetYaxis : function to get Y-axis from quaternion.
int SO3MathsQuatToEulerDegreeYZX(mmachine m)
SO3MathsQuatToEulerDegreeYZX : Scol function to transform quaternion vector in euler angles (degree) ...
int SO3MathsEulerYZXToQuat(mmachine m)
SO3MathsEulerYZXToQuat : Scol function to transform euler angles (YZX) (radians) in a quaternion !...
int SO3MathsGetRotationToAxis(mmachine m)
SO3MathsGetRotationToAxis : Scol function to get the shortest arc quaternion to rotate the source vec...
int SO3MathsQuatToEulerDegreeXZY(mmachine m)
SO3MathsQuatToEulerDegreeXZY : Scol function to transform quaternion vector in euler angles (degree) ...
int SO3MathsQuatFromDegreeAngle(mmachine m)
SO3MathsQuatFromDegreeAngle : function to get Quaternion from X Y Z angle in degree.
int SO3MathsQuatToEulerDegreePYR(mmachine m)
SO3MathsQuatToEulerDegreePYR : Scol function to transform quaternion vector in euler angles (degree) ...
int SO3MathsQuatGetXaxis(mmachine m)
SO3MathsQuatGetXaxis : function to get X-axis from quaternion.
int SO3MathsQuatGetYaw(mmachine m)
SO3MathsQuatGetYaw : function to get the yaw value from quaternion.
int SO3MathsEulerYXZToQuat(mmachine m)
SO3MathsEulerYXZToQuat : Scol function to transform euler angles (YXZ) (radians) in a quaternion !...
int SO3MathsQuatGetDirection(mmachine m)
SO3MathsQuatGetDirection : function to get the direction from quaternion.
int SO3MathsQuatToEulerDegreeZYX(mmachine m)
SO3MathsQuatToEulerDegreeZYX : Scol function to transform quaternion vector in euler angles (degree) ...
int SO3MathsQuatToEulerXZY(mmachine m)
SO3MathsQuatToEulerXZY : Scol function to transform quaternion vector in euler angles (XZY) !...
int SO3MathsGetRotationTo(mmachine m)
SO3MathsGetRotationTo : Scol function to get the shortest arc quaternion to rotate the source vector ...
int SO3MathsQuatAdd(mmachine m)
SO3MathsQuatAdd : Scol function to add two quaternions.
int SO3MathsVectorDotProduct(mmachine m)
SO3MathsVectorDotProduct : Calculates the dot (scalar) product of this vector with another.
int SO3MathsQuatToEulerZXY(mmachine m)
SO3MathsQuatToEulerZXY : Scol function to transform quaternion vector in euler angles (ZXY) !...
int SO3MathsQuatToAxes(mmachine m)
SO3MathsQuatToAxes : function to project quaternion to axis values.
int SO3MathsEulerZYXToQuat(mmachine m)
SO3MathsEulerZYXToQuat : Scol function to transform euler angles (ZYX) (radians) in a quaternion !...