|
Katana Plug-in APIs 0.1
|
00001 // Copyright (c) 2017 The Foundry Visionmongers Ltd. All Rights Reserved. 00002 00003 #ifndef FNVIEWER_MATHTYPES_H 00004 #define FNVIEWER_MATHTYPES_H 00005 00006 #include <cassert> 00007 #include <cmath> 00008 #include <cstring> 00009 00010 namespace Foundry 00011 { 00012 namespace Katana 00013 { 00014 namespace ViewerAPI 00015 { 00016 00028 template <class T> struct Matrix44 00029 { 00030 public: 00031 // @cond FN_INTERNAL_DEV 00032 // Unfortunately, Breathe/Sphinx v1.4.1 is unable to parse T data[16]. 00033 00037 T data[16]; 00038 00039 // @endcond 00040 00041 Matrix44(); 00042 Matrix44(const T matrix[16]); 00043 Matrix44(T m00, T m01, T m02, T m03, 00044 T m10, T m11, T m12, T m13, 00045 T m20, T m21, T m22, T m23, 00046 T m30, T m31, T m32, T m33); 00047 00048 const T* getData() const; 00049 }; 00050 00051 template <class T> struct Vec2 00052 { 00053 T x; 00054 T y; 00055 00056 Vec2(); 00057 Vec2(T x, T y); 00058 00059 Vec2 operator-() const; 00060 Vec2 operator+(const Vec2 &rhs) const; 00061 Vec2 operator-(const Vec2 &rhs) const; 00062 T dot(const Vec2 &rhs) const; 00063 T length() const; 00064 Vec2 normalized() const; 00065 bool isParallelTo(const Vec2 &rhs) const; 00066 Vec2<T> operator*(const T scalar) const; 00067 Vec2<T> operator/(const T scalar) const; 00068 }; 00069 00070 template <class T> struct Vec3 00071 { 00072 T x; 00073 T y; 00074 T z; 00075 00076 Vec3(); 00077 Vec3(T x, T y, T z); 00078 00079 Vec3 operator-() const; 00080 Vec3 operator+(const Vec3 &rhs) const; 00081 Vec3 operator-(const Vec3 &rhs) const; 00082 T dot(const Vec3 &rhs) const; 00083 T length() const; 00084 Vec3 normalized() const; 00085 Vec3 cross(const Vec3 &rhs) const; 00086 bool isParallelTo(const Vec3 &rhs) const; 00087 Vec3<T> operator*(const T scalar) const; 00088 Vec3<T> operator/(const T scalar) const; 00089 }; 00090 00091 template <class T> struct Vec4 00092 { 00093 T x; 00094 T y; 00095 T z; 00096 T w; 00097 00098 Vec4(); 00099 Vec4(T x, T y, T z, T w); 00100 }; 00101 00102 typedef Matrix44<float> Matrix44f; 00103 typedef Matrix44<double> Matrix44d; 00104 00105 typedef Vec2<int> Vec2i; 00106 typedef Vec2<float> Vec2f; 00107 typedef Vec2<double> Vec2d; 00108 00109 typedef Vec3<int> Vec3i; 00110 typedef Vec3<float> Vec3f; 00111 typedef Vec3<double> Vec3d; 00112 00113 typedef Vec4<int> Vec4i; 00114 typedef Vec4<float> Vec4f; 00115 typedef Vec4<double> Vec4d; 00116 00119 //----------------- 00120 // Implementation 00121 //----------------- 00122 00123 00124 template <class T> 00125 inline Matrix44<T>::Matrix44() 00126 { 00127 // Set to identity 00128 memset (data, 0, sizeof(data)); 00129 data[0] = 1; 00130 data[5] = 1; 00131 data[10] = 1; 00132 data[15] = 1; 00133 } 00134 00135 template <class T> 00136 inline Matrix44<T>::Matrix44(const T matrix[16]) 00137 { 00138 memcpy (data, matrix, sizeof (data)); 00139 } 00140 00141 template <class T> 00142 inline 00143 Matrix44<T>::Matrix44(T m00, T m01, T m02, T m03, 00144 T m10, T m11, T m12, T m13, 00145 T m20, T m21, T m22, T m23, 00146 T m30, T m31, T m32, T m33) 00147 { 00148 data[0] = m00; 00149 data[1] = m01; 00150 data[2] = m02; 00151 data[3] = m03; 00152 data[4] = m10; 00153 data[5] = m11; 00154 data[6] = m12; 00155 data[7] = m13; 00156 data[8] = m20; 00157 data[9] = m21; 00158 data[10] = m22; 00159 data[11] = m23; 00160 data[12] = m30; 00161 data[13] = m31; 00162 data[14] = m32; 00163 data[15] = m33; 00164 } 00165 00166 template <class T> 00167 inline const T* Matrix44<T>::getData() const 00168 { 00169 return &data[0]; 00170 } 00171 00172 00173 template <class T> 00174 inline Vec2<T>::Vec2() 00175 : x(0), y(0) {} 00176 00177 template <class T> 00178 inline Vec2<T>::Vec2(T _x, T _y) 00179 : x(_x), y(_y) {} 00180 00181 template <class T> 00182 inline Vec2<T> Vec2<T>::operator-() const 00183 { 00184 Vec2 result; 00185 result.x = -x; 00186 result.y = -y; 00187 return result; 00188 } 00189 00190 template <class T> 00191 inline Vec2<T> Vec2<T>::operator+(const Vec2 &rhs) const 00192 { 00193 Vec2 result(*this); 00194 result.x += rhs.x; 00195 result.y += rhs.y; 00196 return result; 00197 } 00198 00199 template <class T> 00200 inline Vec2<T> Vec2<T>::operator-(const Vec2 &rhs) const 00201 { 00202 Vec2 result(*this); 00203 result.x -= rhs.x; 00204 result.y -= rhs.y; 00205 return result; 00206 } 00207 00208 template <class T> 00209 inline T Vec2<T>::dot(const Vec2 &rhs) const 00210 { 00211 return x * rhs.x + y * rhs.y; 00212 } 00213 00214 template <class T> 00215 inline T Vec2<T>::length() const 00216 { 00217 const T dot_self = this->dot(*this); 00218 return sqrt(dot_self); 00219 } 00220 00221 template <class T> 00222 inline Vec2<T> Vec2<T>::normalized() const 00223 { 00224 const T recip_length = 1 / this->length(); 00225 Vec2 result(*this); 00226 result.x *= recip_length; 00227 result.y *= recip_length; 00228 return result; 00229 } 00230 00231 template <class T> 00232 inline bool Vec2<T>::isParallelTo(const Vec2 &rhs) const 00233 { 00234 const T epsilon = 1.0e-6; 00235 const Vec2 thisNormalized = this->normalized(); 00236 const Vec2 rhsNormalized = rhs.normalized(); 00237 const T dp = thisNormalized.dot(rhsNormalized); 00238 // parallel normalized vectors have a dot product of +1 or -1 00239 bool parallel = fabs((fabs(dp) - 1)) < epsilon; 00240 return parallel; 00241 } 00242 00243 template <class T> 00244 inline Vec2<T> Vec2<T>::operator*(const T scalar) const 00245 { 00246 Vec2 result(*this); 00247 result.x *= scalar; 00248 result.y *= scalar; 00249 return result; 00250 } 00251 00252 template <class T> 00253 inline Vec2<T> Vec2<T>::operator/(const T scalar) const 00254 { 00255 Vec2 result(*this); 00256 result.x /= scalar; 00257 result.y /= scalar; 00258 return result; 00259 } 00260 00261 00262 template <class T> 00263 inline Vec3<T>::Vec3() 00264 : x(0), y(0), z(0) {} 00265 00266 template <class T> 00267 inline Vec3<T>::Vec3(T _x, T _y, T _z) 00268 : x(_x), y(_y), z(_z) {} 00269 00270 template <class T> 00271 inline Vec3<T> Vec3<T>::operator-() const 00272 { 00273 Vec3 result; 00274 result.x = -x; 00275 result.y = -y; 00276 result.z = -z; 00277 return result; 00278 } 00279 00280 template <class T> 00281 inline Vec3<T> Vec3<T>::operator+(const Vec3 &rhs) const 00282 { 00283 Vec3 result(*this); 00284 result.x += rhs.x; 00285 result.y += rhs.y; 00286 result.z += rhs.z; 00287 return result; 00288 } 00289 00290 template <class T> 00291 inline Vec3<T> Vec3<T>::operator-(const Vec3 &rhs) const 00292 { 00293 Vec3 result(*this); 00294 result.x -= rhs.x; 00295 result.y -= rhs.y; 00296 result.z -= rhs.z; 00297 return result; 00298 } 00299 00300 template <class T> 00301 inline T Vec3<T>::dot(const Vec3 &rhs) const 00302 { 00303 return x * rhs.x + y * rhs.y + z * rhs.z; 00304 } 00305 00306 template <class T> 00307 inline T Vec3<T>::length() const 00308 { 00309 const T dot_self = this->dot(*this); 00310 return sqrt(dot_self); 00311 } 00312 00313 template <class T> 00314 inline Vec3<T> Vec3<T>::normalized() const 00315 { 00316 const T recip_length = 1 / this->length(); 00317 Vec3 result(*this); 00318 result.x *= recip_length; 00319 result.y *= recip_length; 00320 result.z *= recip_length; 00321 return result; 00322 } 00323 00324 template <class T> 00325 inline Vec3<T> Vec3<T>::cross(const Vec3 &rhs) const 00326 { 00327 Vec3 result; 00328 result.x = y * rhs.z - z * rhs.y; 00329 result.y = -(x * rhs.z - z * rhs.x); 00330 result.z = x * rhs.y - y * rhs.x; 00331 return result; 00332 } 00333 00334 template <class T> 00335 inline bool Vec3<T>::isParallelTo(const Vec3 &rhs) const 00336 { 00337 const T epsilon = 1.0e-6; 00338 const Vec3 thisNormalized = this->normalized(); 00339 const Vec3 rhsNormalized = rhs.normalized(); 00340 const T dp = thisNormalized.dot(rhsNormalized); 00341 // parallel normalized vectors have a dot product of +1 or -1 00342 bool parallel = fabs((fabs(dp) - 1)) < epsilon; 00343 return parallel; 00344 } 00345 00346 template <class T> 00347 inline Vec3<T> Vec3<T>::operator*(const T scalar) const 00348 { 00349 Vec3 result(*this); 00350 result.x *= scalar; 00351 result.y *= scalar; 00352 result.z *= scalar; 00353 return result; 00354 } 00355 00356 template <class T> 00357 inline Vec3<T> Vec3<T>::operator/(const T scalar) const 00358 { 00359 Vec3 result(*this); 00360 result.x /= scalar; 00361 result.y /= scalar; 00362 result.z /= scalar; 00363 return result; 00364 } 00365 00366 template <class T> 00367 inline Vec4<T>::Vec4() 00368 : x(0), y(0), z(0), w(0) {} 00369 00370 template <class T> 00371 inline Vec4<T>::Vec4(T _x, T _y, T _z, T _w) 00372 : x(_x), y(_y), z(_z), w(_w) {} 00373 00374 } // ViewerAPI 00375 } // Katana 00376 } // Foundry 00377 00378 00379 #endif
1.7.3