3Gear Systems SDK  v0.9.34
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
VecMath.h
1 #pragma once
2 
3 #include <algorithm>
4 #include <iosfwd>
5 #include <cstdint>
6 #include <cmath>
7 
8 namespace HandTrackingClient
9 {
10 
16 template <typename T>
17 class Vector3
18 {
19 public:
21  T x;
22 
24  T y;
25 
27  T z;
28 
30  Vector3<T>() : x(0), y(0), z(0) {}
31 
33  Vector3<T>(T x_in, T y_in, T z_in) : x(x_in), y(y_in), z(z_in) {}
34 
36  template <typename T2>
37  Vector3<T2> cast() const { return Vector3<T2> ((T2) x, (T2) y, (T2) z); }
38 
40  Vector3<T>& operator+= (const Vector3<T>& rhs) { x += rhs.x; y += rhs.y; z += rhs.z; return *this; }
41 
43  Vector3<T>& operator-= (const Vector3<T>& rhs) { x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this; }
44 
46  Vector3<T> operator-() const { return Vector3<T>(-x, -y, -z); }
47 
49  Vector3<T>& operator*= (const T rhs) { x *= rhs; y *= rhs; z *= rhs; return *this; }
50 
52  Vector3<T>& operator/= (const T rhs) { x /= rhs; y /= rhs; z /= rhs; return *this; }
53 
55  T dot (const Vector3<T>& rhs) const { return x*rhs.x + y*rhs.y + z*rhs.z; }
56 
58  Vector3<T> cross (const Vector3<T>& rhs) const;
59 
61  bool operator== (const Vector3<T>& rhs) const { return (x == rhs.x) && (y == rhs.y) && (z == rhs.z); }
62 
64  bool operator!= (const Vector3<T>& rhs) const { return !(*this == rhs); }
65 
67  T squaredNorm() const { return x*x + y*y + z*z; }
68 
70  T lInfNorm() const { return std::max (std::max (std::abs(x), std::abs(y)), std::abs(z)); }
71 
73  T norm() const { return std::sqrt(squaredNorm()); }
74 
77  void normalize();
78 
80  Vector3<T> normalized() const { Vector3<T> result(*this); result.normalize(); return result; }
81 };
82 
84 template <typename T>
85 Vector3<T> operator* (const T lhs, const Vector3<T>& rhs) { Vector3<T> result(rhs); result *= lhs; return result; }
86 
88 template <typename T>
89 inline Vector3<T> operator* (const Vector3<T>& lhs, const T rhs) { Vector3<T> result(lhs); result *= rhs; return result; }
90 
92 template <typename T>
93 inline Vector3<T> operator/ (const Vector3<T>& lhs, const T rhs) { Vector3<T> result(lhs); result /= rhs; return result; }
94 
96 template <typename T>
97 inline Vector3<T> operator+ (const Vector3<T>& lhs, const Vector3<T>& rhs) { Vector3<T> result(lhs); result += rhs; return result; }
98 
100 template <typename T>
101 inline Vector3<T> operator- (const Vector3<T>& lhs, const Vector3<T>& rhs) { Vector3<T> result(lhs); result -= rhs; return result; }
102 
104 template <typename T>
105 std::ostream& operator<< (std::ostream& os, const Vector3<T>& rhs);
106 
107 typedef Vector3<float> Vector3f;
108 typedef Vector3<double> Vector3d;
109 
115 template <typename T>
117 {
118 public:
121 
123  T w;
124 
126  Quaternion<T>() : v(0, 0, 0), w(1) {}
127 
129  Quaternion<T>(T x_in, T y_in, T z_in, T w_in)
130  : v(x_in, y_in, z_in), w(w_in) {}
131 
139  Quaternion<T>(const Vector3<T>& v_in, T w_in)
140  : v(v_in), w(w_in) {}
141 
143  template <typename T2>
144  Quaternion<T2> cast() { return Quaternion<T2> (v.template cast<T2>, (T2) w); }
145 
150  static Quaternion<T> fromAxisAngle (const Vector3<T> axis, const T angle);
151 
156  Quaternion<T> conjugate() const;
157 
159  Vector3<T> rotate (const Vector3<T>& v) const;
160 
162  void normalize();
163 
165  Quaternion<T> normalized() const { Quaternion<T> result (*this); result.normalize(); return result; }
166 
168  bool operator== (const Quaternion<T>& rhs) const { return (v == rhs.v) && (w == rhs.w); }
169 
171  bool operator!= (const Quaternion<T>& rhs) const { return !(*this == rhs); }
172 };
173 
175 template <typename T>
177 {
178  // From Wikipedia:
179  // http://en.wikipedia.org/wiki/Quaternion#Scalar_and_vector_parts
180  return Quaternion<T> (a.w*b.v + b.w*a.v + a.v.cross(b.v),
181  a.w*b.w - a.v.dot(b.v));
182 }
183 
184 
186 template <typename T>
187 std::ostream& operator<< (std::ostream& os, const Quaternion<T>& rhs);
188 
189 typedef Quaternion<float> Quaternionf;
190 typedef Quaternion<double> Quaterniond;
191 
202 template <typename T>
204 {
205 public:
208 
211 
213  Transform (const Quaternion<T>& rotation_in, const Vector3<T>& translation_in)
214  : rotation (rotation_in), translation (translation_in) {}
215 
216  Transform() {}
217 
219  void invert();
220 
222  Transform<T> inverse() const { Transform<T> result(*this); result.invert(); return result; }
223 };
224 
225 template <typename T>
226 Transform<T> operator* (const Transform<T>& lhs, const Transform<T>& rhs)
227 {
228  // [ R_1 t_1 ] * [ R_2 t_2 ] = [ R_1*R_2 R_1*t_2 + t_1 ]
229  // [ 0 1 ] [ 0 1 ] [ 0 1 ]
230  return Transform<T> (lhs.rotation * rhs.rotation,
231  lhs.translation + lhs.rotation.rotate(rhs.translation));
232 }
233 
234 template <typename T>
235 Vector3<T> operator* (const Transform<T>& lhs, const Vector3<T>& rhs)
236 {
237  return lhs.translation + lhs.rotation.rotate (rhs);
238 }
239 
240 typedef Transform<float> Transformf;
241 typedef Transform<double> Transformd;
242 
248 template <typename T>
249 class Matrix4
250 {
251 public:
253  T data[16];
254 
256  Matrix4() { std::fill(data, data+16, T(0)); }
257 
258  static Matrix4 identity();
259 
264 
266  const T& operator() (unsigned iRow, unsigned jCol) const { return data[jCol * 4 + iRow]; }
267 
269  T& operator() (unsigned iRow, unsigned jCol) { return data[jCol * 4 + iRow]; }
270 };
271 
272 typedef Matrix4<float> Matrix4f;
273 typedef Matrix4<double> Matrix4d;
274 
275 } // namespace HandTrackingClient
276