MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlQuaternion.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_QUATERNION_H
14#define ML_QUATERNION_H
15
21
22// Resolves system dependencies and load project settings.
24
25// 3D double vector.
26#include "mlVector3.h"
27#include <mlErrorMacros.h>
28#include <mlErrorOutput.h>
30
32
33//--------------------------------------------------------
37//--------------------------------------------------------
38template <typename DT> class TQuaternion
39{
40
41public:
43 typedef DT value_type;
44
45 //------------------------------------------------------
48 //------------------------------------------------------
49 union
50 {
51 // We use a union to make the members accessible
52 // either
53 // - as named members qx, qy, qz, and qw,
54 // - as array[0..3] and
55 // - vector (qv, qw) via an access method qv().
56 // We also provide indexing operators [] on the array.
57 // Note:
58 // The real part qw is stored in the last component.
59
60 //------------------------------------------------------
63 //------------------------------------------------------
64 struct
65 {
66 DT qx;
67 DT qy;
68 DT qz;
69 DT qw;
70 };
71
72 //------------------------------------------------------
79 //------------------------------------------------------
80 DT array[4];
81 };
83
84
85 //------------------------------------------------------
88 //------------------------------------------------------
90 inline TQuaternion()
91 : qx(0)
92 , qy(0)
93 , qz(0)
94 , qw(0)
95 {}
96
99 inline explicit TQuaternion(DT d)
100 : qx(0)
101 , qy(0)
102 , qz(0)
103 , qw(d)
104 {}
105
110 template <typename DT2> inline explicit TQuaternion(const TQuaternion<DT2>& q2)
111 {
112 qx = static_cast<DT>(q2.qx);
113 qy = static_cast<DT>(q2.qy);
114 qz = static_cast<DT>(q2.qz);
115 qw = static_cast<DT>(q2.qw);
116 }
117
118 TQuaternion(const TQuaternion& q2) = default;
119
127 inline explicit TQuaternion(DT x, DT y, DT z, DT w)
128 {
129 qx = x;
130 qy = y;
131 qz = z;
132 qw = w;
133 }
134
143 inline explicit TQuaternion(const FloatingPointVector<DT, 4>& v)
144 {
145 qx = v[1];
146 qy = v[2];
147 qz = v[3];
148 qw = v[0];
149 }
150
155 inline explicit TQuaternion(const FloatingPointVector<DT, 3, Vector3DataContainer<DT>>& v, DT w)
156 : qx(v[0])
157 , qy(v[1])
158 , qz(v[2])
159 , qw(w)
160 {}
161
163
164
165 //-------------------------------------------------------------------------
168 //-------------------------------------------------------------------------
169
173 {
174 return qv();
175 }
176
178 inline DT getRealPart() const
179 {
180 return qw;
181 }
182
191 {
192 return reinterpret_cast<const FloatingPointVector<DT, 3, Vector3DataContainer<DT>>&>(qx);
193 }
194
203
207 inline void set(const FloatingPointVector<DT, 3, Vector3DataContainer<DT>>& v, const DT w)
208 {
209 qx = v[0];
210 qy = v[1];
211 qz = v[2];
212 qw = w;
213 }
214
217 inline void set(const DT x, const DT y, const DT z, const DT w)
218 {
219 qx = x;
220 qy = y;
221 qz = z;
222 qw = w;
223 }
224
230 inline Tmat4<DT> getAsMat4(bool* isConvertible = nullptr) const
231 {
232 // Create return value.
233 Tmat4<DT> m;
234 // Check whether conversion is possible.
235 const DT n = norm();
236 if (0 == n) {
237 if (!isConvertible) {
238 printTemplateError("TQuaternion::getAsMat4() const, quaternion is not convertible to 4x4 matrix",
239 ML_BAD_PARAMETER, "Returning Quaterion");
240 }
241 else {
242 *isConvertible = false;
243 }
245 }
246 else {
247 // Conversion is possible.
248 const DT s = 2. / n;
249 // s is a compressed factor
250 // it originates from the constant factor 2 and the normalization
251 // factor for the vector "1/sqrt(norm(vector))"
252 // To normalize the vector each component is divided by "1/sqrt(norm(vector))"
253 // So each vector component qx,qy,qz has to get its own factor to normalize
254 // the vector while calculating the matrix. However, as s is multiplied
255 // always to a pair of 2 multiplied vector components, the factor can be
256 // pulled in front of the addition (c.f. m11). As pairs of vector components
257 // are added, the sqrt vanishes and what is left is the factor 2./norm(vector)
258 // where norm is what is defined as "norm()" inside the mlQuaternion.h.
259
260 // q0 = qw, first column.
261 const DT qxqx = qx * qx;
262 const DT qyqy = qy * qy;
263 const DT qzqz = qz * qz;
264
265 const DT qxqy = qx * qy;
266 const DT qxqz = qx * qz;
267 const DT qxqw = qx * qw;
268
269 const DT qyqz = qy * qz;
270 const DT qyqw = qy * qw;
271
272 const DT qzqw = qz * qw;
273
274 const DT m11 = 1 - s * (qyqy + qzqz);
275 const DT m12 = s * (qxqy - qzqw);
276 const DT m13 = s * (qxqz + qyqw);
277
278 const DT m21 = s * (qxqy + qzqw);
279 const DT m22 = 1 - s * (qxqx + qzqz);
280 const DT m23 = s * (qyqz - qxqw);
281
282 const DT m31 = s * (qxqz - qyqw);
283 const DT m32 = s * (qyqz + qxqw);
284 const DT m33 = 1 - s * (qxqx + qyqy);
285
286 // Construct matrix from precomputed values
287 m.set(0);
288 m[0][0] = m11;
289 m[0][1] = m12;
290 m[0][2] = m13;
291 m[0][3] = 0.0;
292
293 m[1][0] = m21;
294 m[1][1] = m22;
295 m[1][2] = m23;
296 m[1][3] = 0.0;
297
298 m[2][0] = m31;
299 m[2][1] = m32;
300 m[2][2] = m33;
301 m[2][3] = 0.0;
302
303 m[3][0] = 0.0;
304 m[3][1] = 0.0;
305 m[3][2] = 0.0;
306 m[3][3] = 1.0;
307
308 if (isConvertible) {
309 *isConvertible = true;
310 }
311
312 } // else
313 return m;
314 }
315
317
318
319 //-------------------------------------------------------------------------
322 //-------------------------------------------------------------------------
323
327 inline DT operator[](const size_t i) const
328 {
329 return array[i];
330 }
331
335 inline DT& operator[](const size_t i)
336 {
337 return array[i];
338 }
339
342 {
343 if (this != &q) {
344 qx = q.qx;
345 qy = q.qy;
346 qz = q.qz;
347 qw = q.qw;
348 }
349 return *this;
350 }
352
353
354 //------------------------------------------------------
357 //------------------------------------------------------
358
361 {
362 return TQuaternion<DT>(qx + b.qx, qy + b.qy, qz + b.qz, qw + b.qw);
363 }
366 {
367 qx += b.qx;
368 qy += b.qy;
369 qz += b.qz;
370 qw += b.qw;
371 return *this;
372 }
373
376 {
377 return TQuaternion<DT>(qx - b.qx, qy - b.qy, qz - b.qz, qw - b.qw);
378 }
381 {
382 qx -= b.qx;
383 qy -= b.qy;
384 qz -= b.qz;
385 qw -= b.qw;
386 return *this;
387 }
388
391 {
392 return mult(b);
393 }
396 {
397 *this = mult(b);
398 return *this;
399 }
400
402 inline TQuaternion<DT> operator/(DT d) const
403 {
404 TQuaternion<DT> retVal(*this);
406 retVal /= d;
407 return retVal;
408 }
409
412 {
414 qx /= d;
415 qy /= d;
416 qz /= d;
417 qw /= d;
418 return *this;
419 }
420
421
424 {
425 return TQuaternion<DT>(div(q));
426 }
427
432 inline TQuaternion<DT> compDiv(const TQuaternion<DT>& b, bool* isError = nullptr) const
433 {
434 // Initialize return value with default quaternion.
435 TQuaternion<DT> retVal(0, 0, 0, 1);
436 if (!isError) {
437 // No error flag passed. Handle error if necessary.
442 retVal.set(qx / b.qx, qy / b.qy, qz / b.qz, qw / b.qw);
443 }
444 else {
445 // Error flag is available. Return *isError as true in case of error
446 *isError = (b.qx == 0) || (b.qy == 0) || (b.qz == 0) || (b.qw == 0);
447
448 retVal = *isError ? TQuaternion<DT>(b.qx != 0 ? qx / b.qx : qx, b.qy != 0 ? qy / b.qy : qy,
449 b.qz != 0 ? qz / b.qz : qz, b.qw != 0 ? qw / b.qw : qw)
450 : TQuaternion<DT>(qx / b.qx, qy / b.qy, qz / b.qz, qw / b.qw);
451 }
452 return retVal;
453 }
454
457 inline bool operator==(const TQuaternion<DT>& b) const
458 {
459 return MLValuesAreEqualWOM(qx, b.qx) && MLValuesAreEqualWOM(qy, b.qy) && MLValuesAreEqualWOM(qz, b.qz) &&
460 MLValuesAreEqualWOM(qw, b.qw);
461 }
464 inline bool operator!=(const TQuaternion<DT>& b) const
465 {
466 return !operator==(b);
467 }
469
470
472 inline DT dot(const TQuaternion<DT>& p) const
473 {
474 return qx * p.qx + qy * p.qy + qz * p.qz + qw * p.qw;
475 }
476
479 inline TQuaternion<DT> odd(const TQuaternion<DT>& p) const
480 {
481 return TQuaternion<DT>(qy * p.qz - qz * p.qy, qz * p.qx - qx * p.qz, qx * p.qy - qy * p.qx, 0);
482 }
483
486 inline TQuaternion<DT> even(const TQuaternion<DT>& p) const
487 {
488 return TQuaternion<DT>(qw * p.qx + qx * p.qw, qw * p.qy + qy * p.qw, qw * p.qz + qz * p.qw,
489 qw * p.qw - qx * p.qx - qy * p.qy - qz * p.qz);
490 }
491
494 {
495 Tvec3<DT> img(qv());
496 Tvec3<DT> qimg(q.qv());
497 return TQuaternion<DT>(img.cross(qimg) + img * q.qw + qw * qimg, qw * q.qw - img.dot(qimg));
498 }
499
502 {
503 return conjugate().mult(q);
504 }
505
508 {
509 return (euclideanMult(q) - q.euclideanMult(*this)) / 2;
510 }
511
512
521 inline TQuaternion<DT> div(const TQuaternion<DT>& d, bool* isError = nullptr) const
522 {
523 // Initialize return value with default quaternion.
524 TQuaternion<DT> retVal(0, 0, 0, 1);
525 if (d == TQuaternion<DT>(0, 0, 0, 0)) {
526 if (isError == nullptr) {
527 // No error flag passed. Handle error if necessary.
528 printTemplateError("TQuaternion::div() const, quaternion is not divisible", ML_BAD_PARAMETER,
529 "Returning unchanged quaternion");
530 retVal.set(qx, qy, qz, qw);
531 }
532 else {
533 // Error flag pointer is available. Return *isError as true in case of error
534 *isError = true;
535 retVal.set(qx, qy, qz, qw);
536 }
537 }
538 else {
539 if (isError != nullptr) {
540 *isError = false;
541 }
542 retVal.set(
543 (d.qw * qx - d.qx * qw - d.qy * qz + d.qz * qy) / (d.qw * d.qw + d.qx * d.qx + d.qy * d.qy + d.qz * d.qz),
544 (d.qw * qy + d.qx * qz - d.qy * qw - d.qz * qx) / (d.qw * d.qw + d.qx * d.qx + d.qy * d.qy + d.qz * d.qz),
545 (d.qw * qz - d.qx * qy + d.qy * qx - d.qz * qw) / (d.qw * d.qw + d.qx * d.qx + d.qy * d.qy + d.qz * d.qz),
546 (d.qw * qw + d.qx * qx + d.qy * qy + d.qz * qz) / (d.qw * d.qw + d.qx * d.qx + d.qy * d.qy + d.qz * d.qz));
547 }
548 return retVal;
549 }
550
551
553 inline TQuaternion<DT> mult(DT s) const
554 {
555 return TQuaternion<DT>(qx * s, qy * s, qz * s, qw * s);
556 }
557
559 inline TQuaternion<DT> add(const TQuaternion<DT>& b) const
560 {
561 return TQuaternion<DT>(qx + b.qx, qy + b.qy, qz + b.qz, qw + b.qw);
562 }
563
566 inline TQuaternion<DT> add(DT s) const
567 {
568 return TQuaternion<DT>(qx, qy, qz, qw + s);
569 }
570
573 {
574 return TQuaternion<DT>(-qx, -qy, -qz, qw);
575 }
576
578 inline TQuaternion<DT> negate() const
579 {
580 return TQuaternion<DT>(-qx, -qy, -qz, -qw);
581 }
582
584 inline DT norm() const
585 {
586 return qx * qx + qy * qy + qz * qz + qw * qw;
587 }
588
589
592 inline DT norm2() const
593 {
594 return static_cast<DT>(::sqrt(qx * qx + qy * qy + qz * qz + qw * qw));
595 }
596
599 inline DT absoluteValue() const
600 {
601 return static_cast<DT>(::sqrt(norm()));
602 }
603
608 inline TQuaternion<DT> normalize(bool* isError = nullptr) const
609 {
610 // Initialize return value with default quaternion.
611 TQuaternion<DT> retVal(0, 0, 0, 1);
612 const DT n = norm2();
613 if (MLValueIs0WOM(n)) {
614 // Error! Check whether we have a pointer to a return error code value.
615 if (isError == nullptr) {
616 // No, post an error.
617 printTemplateError("TQuaternion::normalize() const, quaternion cannot be normalized", ML_BAD_PARAMETER,
618 "Returning default quaternion");
619 }
620 else {
621 // Yes, set return code.
622 *isError = true;
623 }
624 }
625 else {
626 // Non zero => no error.
627 if (isError) {
628 *isError = false;
629 }
630 retVal.set(qx / n, qy / n, qz / n, qw / n);
631 }
632 return retVal;
633 }
634
641 inline TQuaternion<DT> arg(bool* isError = nullptr) const
642 {
643 // Initialize return value with default quaternion.
644 TQuaternion<DT> retVal(0, 0, 0, 1);
645 // Norm2 must be non-zero to avoid divisions by zero.
646 const DT n = norm2();
647 if (MLValueIs0WOM(n)) {
648 // Error! Check whether we have a pointer to a return error code value.
649 if (isError == nullptr) {
650 // No, post an error.
651 printTemplateError("TQuaternion::arg() const, quaternion cannot be normalized", ML_BAD_PARAMETER,
652 "Returning default quaternion");
653 }
654 else {
655 // Yes, set return code.
656 *isError = true;
657 }
658 }
659 else {
660 // Non zero => no error.
661 if (isError != nullptr) {
662 *isError = false;
663 }
664 retVal = acos(qw / n);
665 }
666 return retVal;
667 }
668
673 inline TQuaternion<DT> sgn(bool* isError = nullptr) const
674 {
675 // Initialize return value with default quaternion.
676 TQuaternion<DT> retVal(0, 0, 0, 1);
677 const DT absVal = absoluteValue();
678 if (isError != nullptr) {
679 // Set error return value.
680 if (absVal == 0) {
681 *isError = true;
682 }
683 else {
684 // No error.
685 *isError = false;
686 retVal = *this / absVal;
687 }
688 }
689 else {
690 // Post error is absVal == 0.
692 retVal = *this / absVal;
693 }
694 return retVal;
695 }
696
702 inline TQuaternion<DT> inverse(bool* isInvertible = nullptr) const
703 {
704 // Initialize return value with default quaternion.
705 TQuaternion<DT> retVal(0, 0, 0, 1);
706 const DT n = norm();
707 if (MLValueIs0WOM(n)) {
708 if (isInvertible == nullptr) {
709 printTemplateError("TQuaternion::() const, quaternion is not invertable", ML_BAD_PARAMETER,
710 "Returning default quaternion");
711 }
712 else {
713 *isInvertible = false;
714 }
716 }
717 else {
718 if (isInvertible != nullptr) {
719 *isInvertible = true;
720 }
721 retVal = conjugate().mult(1 / n);
722 }
723 return retVal;
724 }
725
730 // return TQuaternion(::sqrt(norm2())*::sin(0.5*acos(qw/norm2()))*(1/qv().length())*Tvec3<DT>(qx,qy,qz),
731 inline TQuaternion<DT> sqrt(bool* isError = nullptr) const
732 {
733 // Initialize return value with default quaternion.
734 TQuaternion<DT> retVal(0, 0, 0, 1);
735 // Precalculate constants and check for 0 division.
736 const DT n2 = norm2();
737 const DT lqv = qv().length();
739 // Error! Check whether we have a pointer to a return error code value.
740 if (isError == nullptr) {
741 // No, post an error.
742 printTemplateError("TQuaternion::sqrt() const, sqrt of quaternion cannot be calculated", ML_BAD_PARAMETER,
743 "Returning default quaternion");
744 }
745 else {
746 // Yes, set return code.
747 *isError = true;
748 }
749 }
750 else {
751 // Calculate sqrt, lqv and n2 are non 0 here.
752 const DT sqrtSin = ::sqrt(n2) * ::sin(0.5 * acos(qw / n2));
753 const DT sqrtSinF = sqrtSin * (1 / lqv);
754 retVal.set(sqrtSinF * qx, sqrtSinF * qy, sqrtSinF * qz, ::sqrt(n2) * ::cos(0.5 * acos(qw / n2)));
755
756 // Non zero => no error.
757 if (isError != nullptr) {
758 *isError = false;
759 }
760 }
761 return retVal;
762 }
763
764
768
770 inline TQuaternion<DT> exp() const
771 {
772 // Initialize return value with default quaternion.
773 TQuaternion<DT> retVal(0, 0, 0, 1);
774 const DT len = qv().length();
775 if (MLValueIs0WOM(len)) {
776 retVal = TQuaternion<DT>(0, 0, 0, ::cos(len)).mult(::exp(qw));
777 }
778 else {
779 retVal = TQuaternion<DT>((qv() / len) * static_cast<DT>(::sin(len)), ::cos(len)).mult(::exp(qw));
780 }
781 return retVal;
782 }
783
784
786 inline TQuaternion<DT> ln() const
787 {
788 // Initialize return value with default quaternion.
789 TQuaternion<DT> retVal(0, 0, 0, 1);
790 const DT n2 = norm2();
791 const DT qvLen = qv().length();
792 if (MLValueIs0WOM(qvLen)) {
793 retVal.set(0, 0, 0, log10(n2));
794 }
795 else {
796 // n2 is always non 0 if qvLen is non-zero, thus no check is necessary.
797 retVal.set(qv() / qvLen * static_cast<DT>(acos(qw / n2)), log10(n2));
798 }
799 return retVal;
800 }
801
802
805 {
806 return ln().mult(quat).exp();
807 }
808
809
813
816 {
818 const Tvec3<DT>& v = qv();
819 const DT len = v.length();
820 // In case of len == 0 be sure not to divide by zero but setting vector to 0 vector.
821 // Then the quaternion method behaves like the normal function on real values.
822 retVal.set(
823 (!MLValueIs0WOM(len) ? static_cast<DT>(::cos(qw)) * v / len * static_cast<DT>(::sinh(len)) : Tvec3<DT>(0.0)),
824 ::sin(qw) * ::cosh(len));
825 return retVal;
826 }
827
830 {
832 const Tvec3<DT>& v = qv();
833 const DT len = v.length();
834 // In case of len == 0 be sure not to divide by zero but setting vector to 0 vector.
835 // Then the quaternion method behaves like the normal function on real values.
837 ? static_cast<DT>(-1) * static_cast<DT>(::sin(qw)) * qv() / len * static_cast<DT>(::sinh(len))
838 : Tvec3<DT>(0.0)),
839 ::cos(qw) * ::cosh(len));
840 return retVal;
841 }
842
844 inline TQuaternion<DT> tan(bool* isError = nullptr) const
845 {
846 return sin().div(cos(), isError);
847 }
848
850 inline TQuaternion<DT> cotan(bool* isError = nullptr) const
851 {
852 return cos().div(sin(), isError);
853 }
854
855
859
862 {
864 const Tvec3<DT>& v = qv();
865 const DT len = v.length();
866 // In case of len == 0 be sure not to divide by zero but setting vector to 0 vector.
867 // Then the quaternion method behaves like the normal function on real values.
868 retVal.set(
869 (!MLValueIs0WOM(len) ? static_cast<DT>(::cosh(qw)) * v / len * static_cast<DT>(::sin(len)) : Tvec3<DT>(0.0)),
870 ::sinh(qw) * ::cos(len));
871 return retVal;
872 }
873
876 {
878 const Tvec3<DT>& v = qv();
879 const DT len = v.length();
880 // In case of len == 0 be sure not to divide by zero but setting vector to 0 vector.
881 // Then the quaternion method behaves like the normal function on real values.
882 retVal.set(
883 (!MLValueIs0WOM(len) ? static_cast<DT>(::sinh(qw)) * v / len * static_cast<DT>(::sin(len)) : Tvec3<DT>(0.0)),
884 ::cosh(qw) * ::cos(len));
885 return retVal;
886 }
887
889 inline TQuaternion<DT> tanh(bool* isError = nullptr) const
890 {
891 return sinh().div(cosh(), isError);
892 }
893
895 inline TQuaternion<DT> cotanh(bool* isError = nullptr) const
896 {
897 return cosh().div(sinh(), isError);
898 }
899
900
904
907 {
908 return add(mult(*this).add(TQuaternion<DT>(1)).sqrt()).ln();
909 }
910
913 {
914 return add(mult(*this).add(TQuaternion<DT>(-1)).sqrt()).ln();
915 }
916
919 {
920 return TQuaternion<DT>((TQuaternion<DT>(qx, qy, qz, qw + 1).ln() - TQuaternion<DT>(-qx, -qy, -qz, 1 - qw).ln()) /
921 0.5);
922 }
923
924
934#define _ML_QUATERNION_CALC_CHECKED(FUNC_NAME, CALC_EXPR) \
935 TQuaternion<DT> retVal; \
936 { \
937 const Tvec3<DT>& v = qv(); \
938 const DT len = v.length(); \
939 if (MLValueIs0WOM(len)) { \
940 /* Error! Check whether we have a pointer to a return error code value. */ \
941 if (isError == NULL) { \
942 /* No, post an error. */ \
943 printTemplateError("TQuaternion::" FUNC_NAME "() const, " FUNC_NAME " of quaternion cannot be calculated", \
944 ML_BAD_PARAMETER, "Returning default quaternion"); \
945 } \
946 else { \
947 /* Yes, set return code, retVal is left on default. */ \
948 *isError = true; \
949 } \
950 } \
951 else { \
952 /* Define return values. */ \
953 if (isError != NULL) { \
954 *isError = false; \
955 } \
956 retVal = CALC_EXPR; \
957 } \
958 } \
959 return retVal;
960
961
963#define _ML_QUAT_CALC_EXP TQuaternion<DT>(-v / len, 0).mult(mult(TQuaternion<DT>(-v / len, 0)).arcsinh());
967
969 TQuaternion<DT> arcsin(bool* isError = nullptr) const
970 {
972 }
973#undef _ML_QUAT_CALC_EXP
974
975
977#define _ML_QUAT_CALC_EXP TQuaternion<DT>(-v / len, 0).mult(arccosh());
979 TQuaternion<DT> arccos(bool* isError = nullptr) const
980 {
982 }
983#undef _ML_QUAT_CALC_EXP
984
985
987#define _ML_QUAT_CALC_EXP TQuaternion<DT>(-v / len, 0).mult(mult(TQuaternion<DT>(-v / len, 0)).arctanh());
989 TQuaternion<DT> arctan(bool* isError = nullptr) const
990 {
992 }
993#undef _ML_QUAT_CALC_EXP
994
995
996// Undefines _ML_QUATERNION_CALC_CHECKED, it is not needed anymore.
997#undef _ML_QUATERNION_CALC_CHECKED
998};
999
1000
1001//-----------------------------------------------------------------------------------
1004//-----------------------------------------------------------------------------------
1007
1010
1013
1017
1018
1020
1021
1022//-------------------------------------------------------------------------
1029//-------------------------------------------------------------------------
1030#ifdef _ML_IMPLEMENT_GLOBAL_QUATERNION_OPERATORS
1031#ifndef _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCS_AND_OPS_IN_GLOBAL_NAMESPACE
1033#endif
1034
1035//-----------------------------
1036// Addition
1037//-----------------------------
1039template <typename DT>
1040inline ML_LA_NAMESPACE::TQuaternion<DT> operator+(DT d, const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1041{
1042 return ML_LA_NAMESPACE::TQuaternion<DT>(q.qx, q.qy, q.qz, q.qw + d);
1043}
1044
1046template <typename DT>
1047inline ML_LA_NAMESPACE::TQuaternion<DT> operator+(const ML_LA_NAMESPACE::TQuaternion<DT>& q, DT d)
1048{
1049 return ML_LA_NAMESPACE::TQuaternion<DT>(q.qx, q.qy, q.qz, q.qw + d);
1050}
1051
1053template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> operator+(const ML_LA_NAMESPACE::TQuaternion<DT>& qa,
1054 const ML_LA_NAMESPACE::TQuaternion<DT>& qb)
1055{
1056 return qa.operator+(qb);
1057}
1058
1059
1060//-----------------------------
1061// Subtraction
1062//-----------------------------
1064template <typename DT>
1065inline ML_LA_NAMESPACE::TQuaternion<DT> operator-(DT d, const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1066{
1067 return ML_LA_NAMESPACE::TQuaternion<DT>(-q.qx, -q.qy, -q.qz, d - q.qw);
1068}
1069
1071template <typename DT>
1072inline ML_LA_NAMESPACE::TQuaternion<DT> operator-(const ML_LA_NAMESPACE::TQuaternion<DT>& q, DT d)
1073{
1074 return ML_LA_NAMESPACE::TQuaternion<DT>(q.qx, q.qy, q.qz, q.qw - d);
1075}
1076
1078template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> operator-(const ML_LA_NAMESPACE::TQuaternion<DT>& qa,
1079 const ML_LA_NAMESPACE::TQuaternion<DT>& qb)
1080{
1081 return qa.operator-(qb);
1082}
1083
1084
1086template <typename DT>
1087inline ML_LA_NAMESPACE::TQuaternion<DT> operator*(DT d, const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1088{
1089 return q.mult(d);
1090}
1091
1093template <typename DT>
1094inline ML_LA_NAMESPACE::TQuaternion<DT> operator*(const ML_LA_NAMESPACE::TQuaternion<DT>& q, DT d)
1095{
1096 return q.mult(d);
1097}
1098
1100template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> operator*(const ML_LA_NAMESPACE::TQuaternion<DT>& qa,
1101 const ML_LA_NAMESPACE::TQuaternion<DT>& qb)
1102{
1103 return qa.operator*(qb);
1104}
1106
1107#ifndef _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCS_AND_OPS_IN_GLOBAL_NAMESPACE
1109#endif
1110
1111#endif // _ML_IMPLEMENT_GLOBAL_QUATERNION_OPERATORS
1112
1113
1114//-------------------------------------------------------------------------
1121//-------------------------------------------------------------------------
1122#ifdef _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCTIONS
1123#ifndef _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCS_AND_OPS_IN_GLOBAL_NAMESPACE
1125#endif
1126
1129#ifdef _ML_IMPLEMENT_QUATERNION_SQRT
1130template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> sqrt(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1131{
1132 return q.sqrt();
1133}
1134#endif
1135
1137template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> exp(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1138{
1139 return q.exp();
1140}
1141
1143template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> ln(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1144{
1145 return q.ln();
1146}
1147
1149template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> sin(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1150{
1151 return q.sin();
1152}
1153
1155template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> cos(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1156{
1157 return q.cos();
1158}
1159
1161template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> tan(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1162{
1163 return q.tan();
1164}
1165
1167template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> cotan(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1168{
1169 return q.cotan();
1170}
1171
1173template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> sinh(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1174{
1175 return q.sinh();
1176}
1177
1179template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> cosh(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1180{
1181 return q.cosh();
1182}
1183
1185template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> tanh(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1186{
1187 return q.tanh();
1188}
1189
1191template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> cotanh(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1192{
1193 return q.cotanh();
1194}
1195
1197template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> arcsinh(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1198{
1199 return q.arcsinh();
1200}
1201
1203template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> arccosh(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1204{
1205 return q.arccosh();
1206}
1207
1209template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> arctanh(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1210{
1211 return q.arctanh();
1212}
1213
1215template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> arcsin(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1216{
1217 return q.arcsin();
1218}
1219
1221template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> arccos(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1222{
1223 return q.arccos();
1224}
1225
1227template <typename DT> inline ML_LA_NAMESPACE::TQuaternion<DT> arctan(const ML_LA_NAMESPACE::TQuaternion<DT>& q)
1228{
1229 return q.arctan();
1230}
1231
1232
1233#ifndef _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCS_AND_OPS_IN_GLOBAL_NAMESPACE
1235#endif
1236
1237#endif // _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCTIONS
1239
1240
1241//-----------------------------------------------------------------------------------
1242// Stream output for std::ostream and dyadic multiplication of TQuaternion and scalar.
1243//-----------------------------------------------------------------------------------
1244namespace std
1245{
1246
1248 template <typename DT> inline ostream& operator<<(ostream& s, const ML_NAMESPACE::TQuaternion<DT>& v)
1249 {
1250 return s << "(" << v.qx << "," << v.qy << "," << v.qz << "," << v.qw << ")";
1251 }
1252
1253} // namespace std
1254
1255
1256#endif // __mlQuaternion_H
Template class for vector arithmetic with floating point datatypes.
T length() const
Returns the length of the vector, i.e., norm2().
Declaration of complex type traits.
TQuaternion< DT > arctan(bool *isError=nullptr) const
Inverse tangent; for error handling see _ML_QUATERNION_CALC_CHECKED.
TQuaternion(DT d)
Builds a quaternion from a scalar by setting all elements to 0 and qw to the scalar; use explicitly t...
TQuaternion< DT > arccos(bool *isError=nullptr) const
Inverse cosine; for error handling see _ML_QUATERNION_CALC_CHECKED.
TQuaternion< DT > euclideanMult(const TQuaternion< DT > &q) const
Euclidean product of this quaternion with another one (which is non-commutative).
bool operator!=(const TQuaternion< DT > &b) const
Inequality operator: Returns whether any component differs from its corresponding component of b.
DT dot(const TQuaternion< DT > &p) const
Dot product, equivalent to four component vector dot product.
TQuaternion< DT > arg(bool *isError=nullptr) const
Computes the quaternion argument.
TQuaternion< DT > ln() const
Computes the natural logarithm.
TQuaternion(DT x, DT y, DT z, DT w)
Creates a new TQuaternion with the DT values given.
TQuaternion< DT > arccosh() const
Computes the inverse hyperbolic cosine.
DT qx
First element of imaginary part.
TQuaternion< DT > operator/(const TQuaternion< DT > &q) const
Divides by a quaternion by another.
TQuaternion< DT > sinh() const
Hyperbolic functions: Hyperbolic sine, hyperbolic cosine, hyperbolic tangent https://en....
TQuaternion< DT > operator/(DT d) const
Divides by a scalar d. The caller must guarantee that d is non-zero.
DT absoluteValue() const
Returns the absolute value of a quaternion, which is the scalar quantity that determines the length o...
TQuaternion< DT > tan(bool *isError=nullptr) const
Computes the tangent; for error handling see _ML_QUATERNION_CALC_CHECKED.
TQuaternion< DT > & operator+=(const TQuaternion< DT > &b)
Adds the corresponding component from b to each component.
TQuaternion< DT > add(const TQuaternion< DT > &b) const
Adds another quaternion b to this one and returns the sum.
TQuaternion< DT > tanh(bool *isError=nullptr) const
Computes the hyperbolic tangent; for error handling see _ML_QUATERNION_CALC_CHECKED.
FloatingPointVector< DT, 3, Vector3DataContainer< DT > > getImaginaryPart() const
Returns a copy of the quaternion's imaginary part.
TQuaternion< DT > sqrt(bool *isError=nullptr) const
Computes the square root of a quaternion.
void set(const FloatingPointVector< DT, 3, Vector3DataContainer< DT > > &v, const DT w)
Sets all quaternion elements from a vector and DT parameter.
TQuaternion< DT > operator*(const TQuaternion< DT > &b) const
Multiplies each components with its corresponding components from b.
TQuaternion< DT > div(const TQuaternion< DT > &d, bool *isError=nullptr) const
Divides a quaternion by another quaternion.
TQuaternion< DT > operator+(const TQuaternion< DT > &b) const
Adds the corresponding component from b to each component.
TQuaternion< DT > sgn(bool *isError=nullptr) const
Returns the sign of the quaternion.
const FloatingPointVector< DT, 3, Vector3DataContainer< DT > > & qv() const
Returns a constant reference to the quaternion's imaginary part as vector object.
TQuaternion< DT > cotan(bool *isError=nullptr) const
Computes the cotangent; for error handling see _ML_QUATERNION_CALC_CHECKED.
FloatingPointVector< DT, 3, Vector3DataContainer< DT > > & qv()
Returns a modifiable reference to the quaternion's imaginary part as vector object,...
TQuaternion< DT > sin() const
Trigonometric functions: sine, cosine, tangent, cotangent.
TQuaternion< DT > cosh() const
Computes the hyperbolic cosine.
TQuaternion< DT > arcsin(bool *isError=nullptr) const
Inverse trigonometric functions: Inverse sine, inverse cosine, inverse tangent.
bool operator==(const TQuaternion< DT > &b) const
Equality operator: Returns whether each component of this is equal to its corresponding component of ...
TQuaternion< DT > exp() const
Exponential and logarithmic functions: natural exponential, natural logarithm, power.
TQuaternion< DT > negate() const
Computes the negation.
TQuaternion< DT > & operator-=(const TQuaternion< DT > &b)
Subtracts the corresponding component from b from each component.
DT norm() const
Computes the d-norm.
TQuaternion< DT > arctanh() const
Computes the inverse hyperbolic tangent.
TQuaternion< DT > arcsinh() const
Inverse hyperbolic function: arcsinh, arccosh, arctanh.
TQuaternion< DT > mult(const TQuaternion< DT > &q) const
Multiplies this quaternion with another one (which is non-commutative). It is also called Grassman pr...
TQuaternion< DT > compDiv(const TQuaternion< DT > &b, bool *isError=nullptr) const
Divides each component by its corresponding component from b.
TQuaternion()
Standard constructor that is setting all elements to 0.
TQuaternion< DT > inverse(bool *isInvertible=nullptr) const
Computes and returns the inverse.
DT qw
Real part.
DT value_type
Scalar type used for qx, qy, qz, and qw.
DT & operator[](const size_t i)
Indexing operator.
TQuaternion< DT > & operator/=(DT d)
Divides by a scalar d. The caller must guarantee that d is non-zero.
Tmat4< DT > getAsMat4(bool *isConvertible=nullptr) const
Converts this quaternion to a 4x4 matrix.
TQuaternion< DT > operator-(const TQuaternion< DT > &b) const
Subtracts the corresponding component from b from each component.
TQuaternion< DT > & operator=(const TQuaternion< DT > &q)
Assignment operator.
TQuaternion< DT > add(DT s) const
Adds a scalar s to the quaternion and returns the sum.
void set(const DT x, const DT y, const DT z, const DT w)
Sets all quaternion elements qx, qy, qz, and qw from parameters x, y, z, and w, respectively.
DT norm2() const
Computes the magnitude of the quaternion.
TQuaternion< DT > & operator*=(const TQuaternion< DT > &b)
Multiplies each components with its corresponding components from b.
TQuaternion< DT > odd(const TQuaternion< DT > &p) const
Odd product, also known as cross-product or Grassman outer product.
TQuaternion< DT > normalize(bool *isError=nullptr) const
Returns the normalization of the quaternion.
TQuaternion(const TQuaternion< DT2 > &q2)
Copy constructor from another TQuaternion of same or different type.
TQuaternion< DT > conjugate() const
Computes the conjugate.
DT qz
Third element of imaginary part.
TQuaternion< DT > mult(DT s) const
Multiplies this quaternion with a scalar factor s.
TQuaternion(const FloatingPointVector< DT, 4 > &v)
Creates a new TQuaternion from the four-dimensional parameter vector.
TQuaternion< DT > cos() const
Computes the cosine.
DT operator[](const size_t i) const
Constant indexing operator.
TQuaternion< DT > cotanh(bool *isError=nullptr) const
Computes the hyperbolic cotangent; for error handling see _ML_QUATERNION_CALC_CHECKED.
TQuaternion< DT > pow(const TQuaternion< DT > &quat) const
Computes the power of a quaternion.
TQuaternion(const FloatingPointVector< DT, 3, Vector3DataContainer< DT > > &v, DT w)
Constructor with a vector and DT parameter.
TQuaternion(const TQuaternion &q2)=default
DT getRealPart() const
Returns a copy of the quaternion's real part, also called scalar() of a quaternion.
DT qy
Second element of imaginary part.
TQuaternion< DT > even(const TQuaternion< DT > &p) const
Even product, also known as Grassman inner product.
TQuaternion< DT > outer(const TQuaternion< DT > &q) const
Euclidean outer product of this quaternion with another one.
A 4x4 matrix class consisting of four row vectors.
Definition mlMatrix4.h:35
Forward declarations to resolve header file dependencies.
Definition mlVector3.h:66
Specialized base class for the FloatingPointVectorDataContainerBase.
Definition mlVector3.h:33
bool MLValueIs0WOM(MLint8 a)
Returns true if value is 0; otherwise, it returns false.
bool MLValuesAreEqualWOM(MLint8 a, MLint8 b)
Returns true if values a and b are equal; otherwise, it returns false.
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm,...
Definition mlTypeDefs.h:879
#define ML_CHECK_FLOAT(x)
#define _ML_QUAT_CALC_EXP
Internal helper macro - do not use.
#define _ML_QUATERNION_CALC_CHECKED(FUNC_NAME, CALC_EXPR)
Defines a code fragment that checks and handles operations for safe expression calculations.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
double exp(T value)
double sqrt(T value)
ScopeGuard< Functor > operator+(ScopeGuardOnExit, Functor &&fn)
void ML_UTILS_EXPORT printTemplateError(const char *location, MLErrorCode reason, const std::string_view &handling)
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Returns true if yes.
Definition mlMatrix2.h:399
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
FloatingPointVector< T, size, DataContainer > operator-(FloatingPointVector< T, size, DataContainer > lhs, const FloatingPointVector< T, size, DataContainer > &rhs)
Return value is the component-wise subtraction of rhs from lhs.
STL namespace.
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator '<<' for stream output of Field objects.