Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfQuaternion.h
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
20#ifndef _SOURCE_TYPES_TFQUATERNION_H_
21#define _SOURCE_TYPES_TFQUATERNION_H_
22
23#include <Magnum/Math/Quaternion.h>
24
25#include "tfVector3.h"
26#include "tfMatrix3.h"
27
28
29namespace TissueForge::types {
30
31
32 template<class T> using QuaternionBase = Magnum::Math::Quaternion<T>;
33
34 template<class T>
35 class TQuaternion : public QuaternionBase<T> {
36 public:
39 return QuaternionBase<T>::rotation(Magnum::Math::Rad<T>(angle), normalizedAxis);
40 }
41
44 return QuaternionBase<T>::fromMatrix((const Magnum::Math::Matrix<3, T>&)matrix);
45 }
46
47 constexpr TQuaternion() noexcept: QuaternionBase<T>() {}
48 constexpr TQuaternion(const TVector3<T>& vector, T scalar) noexcept: QuaternionBase<T>(vector, scalar) {}
49 constexpr TQuaternion(T scalar) noexcept: TQuaternion<T>(TVector3<T>(0), scalar) {}
50 constexpr explicit TQuaternion(const TVector3<T>& vector) noexcept: QuaternionBase<T>(vector) {}
51 template<class U> constexpr explicit TQuaternion(const TQuaternion<U>& other) noexcept: QuaternionBase<T>(other) {}
52
53 template<class U = T> TQuaternion(const QuaternionBase<U>& other) noexcept: QuaternionBase<T>(other) {}
54 template<class U = T> TQuaternion(const QuaternionBase<U>* other) noexcept: QuaternionBase<T>(*other) {}
55 template<class U = T> operator QuaternionBase<U>*() { return static_cast<QuaternionBase<U>*>(this); }
56 template<class U = T> operator const QuaternionBase<U>*() { return static_cast<const QuaternionBase<U>*>(this); }
57 template<class U = T> operator QuaternionBase<U>() { return static_cast<QuaternionBase<U>>(*this); }
58 template<class U = T> operator const QuaternionBase<U>() const { return static_cast<const QuaternionBase<U>>(*this); }
59
61 T* data() { return QuaternionBase<T>::data(); }
62
64 constexpr const T* data() const { return QuaternionBase<T>::data(); }
65 bool operator==(const TQuaternion<T>& other) const { return QuaternionBase<T>::operator==(other); }
66 bool operator!=(const TQuaternion<T>& other) const { return QuaternionBase<T>::operator!=(other); }
67
69 bool isNormalized() const { return QuaternionBase<T>::isNormalized(); }
70 #ifndef SWIGPYTHON
71
74
76 T& scalar() { return QuaternionBase<T>::scalar(); }
77 #endif
78
80 constexpr const TVector3<T> vector() const { return QuaternionBase<T>::vector(); }
81
83 constexpr T scalar() const { return QuaternionBase<T>::scalar(); }
84
86 T angle() const { return T(QuaternionBase<T>::angle()); }
87
89 T angle(const TQuaternion& other) const { return T(Magnum::Math::angle(this->normalized(), other.normalized())); }
90
92 TVector3<T> axis() const { return QuaternionBase<T>::axis(); }
93
95 TMatrix3<T> toMatrix() const { return QuaternionBase<T>::toMatrix(); }
96
99 auto v = QuaternionBase<T>::toEuler();
100 return TVector3<T>(T(v[0]), T(v[1]), T(v[2]));;
101 }
102 TQuaternion<T> operator-() const { return QuaternionBase<T>::operator-(); }
103 TQuaternion<T>& operator+=(const TQuaternion<T>& other) { return (TQuaternion<T>&)QuaternionBase<T>::operator+=(other); }
104 TQuaternion<T> operator+(const TQuaternion<T>& other) const { return QuaternionBase<T>::operator+(other); }
105 TQuaternion<T>& operator-=(const TQuaternion<T>& other) { return (TQuaternion<T>&)QuaternionBase<T>::operator-=(other); }
106 TQuaternion<T> operator-(const TQuaternion<T>& other) const { return QuaternionBase<T>::operator-(other); }
107 TQuaternion<T>& operator*=(T scalar) { return (TQuaternion<T>&)QuaternionBase<T>::operator*=(scalar); }
108 TQuaternion<T> operator*(T scalar) const { return QuaternionBase<T>::operator*(scalar); }
109 TQuaternion<T>& operator/=(T scalar) { return (TQuaternion<T>&)QuaternionBase<T>::operator/=(scalar); }
110 TQuaternion<T> operator/(T scalar) const { return QuaternionBase<T>::operator/(scalar); }
111 TQuaternion<T> operator*(const QuaternionBase<T>& other) const { return QuaternionBase<T>::operator*(other); }
112
114 T dot() const { return QuaternionBase<T>::dot(); }
115
117 T length() const { return QuaternionBase<T>::length(); }
118
120 TQuaternion<T> normalized() const { return QuaternionBase<T>::normalized(); }
121
123 TQuaternion<T> conjugated() const { return QuaternionBase<T>::conjugated(); }
124
126 TQuaternion<T> inverted() const { return QuaternionBase<T>::inverted(); }
127
129 TQuaternion<T> invertedNormalized() const { return QuaternionBase<T>::invertedNormalized(); }
130
132 TVector3<T> transformVector(const TVector3<T>& vector) const { return QuaternionBase<T>::transformVector(vector); }
133
135 TVector3<T> transformVectorNormalized(const TVector3<T>& vector) const { return QuaternionBase<T>::transformVectorNormalized(vector); }
136
137 operator std::vector<T>&() const {
138 std::vector<T>& v = vector();
139 std::vector<T> *result = new std::vector<T>(v.begin(), v.end());
140 result->push_back(scalar());
141 return *result;
142 }
143 #ifdef SWIGPYTHON
144 constexpr explicit TQuaternion(const std::vector<T>& vector, T scalar) noexcept: QuaternionBase<T>(TVector3<T>(vector), scalar) {}
145
147 std::vector<T>& asVector() {
148 std::vector<T> *result = new std::vector<T>(*this);
149 return *result;
150 }
151 #endif // SWIGPYTHON
152 };
153
154}
155
156template<typename T>
157inline std::ostream& operator<<(std::ostream& os, const TissueForge::types::TQuaternion<T>& q)
158{
159 auto vec = q.vector();
160 os << std::string("{") << vec[0];
161 for(int i = 1; i < vec.length(); ++i) os << std::string(",") << vec[i];
162 os << "," << q.scalar() << std::string("}");
163 return os;
164}
165
166#endif // _SOURCE_TYPES_TFQUATERNION_H_
Definition tfMatrix3.h:35
Definition tfQuaternion.h:35
bool isNormalized() const
Definition tfQuaternion.h:69
TVector3< T > transformVectorNormalized(const TVector3< T > &vector) const
Definition tfQuaternion.h:135
TQuaternion< T > invertedNormalized() const
Definition tfQuaternion.h:129
static TQuaternion< T > fromMatrix(const TMatrix3< T > &matrix)
Definition tfQuaternion.h:43
TQuaternion< T > conjugated() const
Definition tfQuaternion.h:123
TVector3< T > axis() const
Definition tfQuaternion.h:92
T angle() const
Definition tfQuaternion.h:86
T * data()
Definition tfQuaternion.h:61
T angle(const TQuaternion &other) const
Definition tfQuaternion.h:89
TVector3< T > & vector()
Definition tfQuaternion.h:73
TQuaternion< T > inverted() const
Definition tfQuaternion.h:126
constexpr const TVector3< T > vector() const
Definition tfQuaternion.h:80
T length() const
Definition tfQuaternion.h:117
TMatrix3< T > toMatrix() const
Definition tfQuaternion.h:95
constexpr T scalar() const
Definition tfQuaternion.h:83
T & scalar()
Definition tfQuaternion.h:76
T dot() const
Definition tfQuaternion.h:114
TVector3< T > transformVector(const TVector3< T > &vector) const
Definition tfQuaternion.h:132
constexpr const T * data() const
Definition tfQuaternion.h:64
TQuaternion< T > normalized() const
Definition tfQuaternion.h:120
TVector3< T > toEuler() const
Definition tfQuaternion.h:98
static TQuaternion< T > rotation(T angle, const TVector3< T > &normalizedAxis)
Definition tfQuaternion.h:38
Definition tfVector3.h:35
Native Tissue Forge type definitions.
Definition tfMatrix.h:33