PageRenderTime 25ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/mcucpp/math/Vector3d.h

https://github.com/KonstantinChizhov/Mcucpp
C Header | 215 lines | 180 code | 33 blank | 2 comment | 0 complexity | b9d7c9c052d13cf7c10884ba1b11c115 MD5 | raw file
  1. #pragma once
  2. #include <math.h>
  3. namespace Mcucpp
  4. {
  5. template<class T>
  6. class Vec3d
  7. {
  8. public:
  9. typedef T Type;
  10. Type x, y, z;
  11. Vec3d ()
  12. :x(0), y(0), z(0)
  13. {
  14. }
  15. template<class T2>
  16. Vec3d (T2 v)
  17. {
  18. x = y = z = v;
  19. }
  20. template<class T2>
  21. Vec3d (const Vec3d<T2>& v)
  22. {
  23. x = v.x;
  24. y = v.y;
  25. z = v.z;
  26. }
  27. template<class T2>
  28. Vec3d (T2 vx, T2 vy, T2 vz)
  29. {
  30. x = vx;
  31. y = vy;
  32. z = vz;
  33. }
  34. Vec3d& operator = ( const Vec3d& v )
  35. {
  36. x = v.x;
  37. y = v.y;
  38. z = v.z;
  39. return *this;
  40. }
  41. template<class T2>
  42. Vec3d& operator = (T2 f)
  43. {
  44. x = y = z = f;
  45. return *this;
  46. }
  47. Vec3d operator - () const;
  48. Vec3d& operator += ( const Vec3d& );
  49. Vec3d& operator -= ( const Vec3d& );
  50. Vec3d& operator *= ( const Vec3d& );
  51. template<class T2>
  52. inline Vec3d<T>& operator*=(T2 v)
  53. {
  54. x *= v;
  55. y *= v;
  56. z *= v;
  57. return *this;
  58. }
  59. template<class T2>
  60. inline Vec3d<T>& operator /= (T2 v)
  61. {
  62. x /= v;
  63. y /= v;
  64. z /= v;
  65. return *this;
  66. }
  67. //dot product
  68. Type DotProduct(const Vec3d& v)
  69. {
  70. return x*v.x + y*v.y + z*v.z;
  71. }
  72. // cross product
  73. Vec3d CrossProduct(const Vec3d& v)
  74. {
  75. return Vec3d(
  76. y*v.z - z*v.y,
  77. z*v.x - x*v.z,
  78. x*v.y - y*v.x);
  79. }
  80. Type operator! () const
  81. {
  82. return sqrt(x*x + y*y + z*z);
  83. }
  84. inline Vec3d RotateX(float a)
  85. {
  86. float ca=cos(a), sa=sin(a);
  87. float zn=z*ca+y*sa;
  88. float yn=y*ca-z*sa;
  89. return Vec3d(x, yn, zn);
  90. }
  91. inline Vec3d RotateY(float a)
  92. {
  93. float ca=cos(a), sa=sin(a);
  94. float xn=x*ca+z*sa;
  95. float zn=z*ca-x*sa;
  96. return Vec3d(xn, y, zn);
  97. }
  98. inline Vec3d RotateZ(float a)
  99. {
  100. float ca=cos(a), sa=sin(a);
  101. float xn=x*ca+y*sa;
  102. float yn=y*ca-x*sa;
  103. return Vec3d(xn, yn, z);
  104. }
  105. inline Vec3d Rotate(const Vec3d &v, Type angel)
  106. {
  107. Vec3d r=v*Q_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z)*angel, res;
  108. res=RotateX(r.x);
  109. res=res.RotateY(r.y);
  110. res=res.RotateZ(r.z);
  111. return res;
  112. }
  113. inline Vec3d Normalize()
  114. {
  115. return *this / sqrt(x*x + y*y + z*z);
  116. }
  117. };
  118. template<class T>
  119. inline Vec3d<T> Vec3d<T>::operator- () const
  120. {
  121. return Vec3d<T>( -x, -y, -z );
  122. }
  123. template<class T>
  124. inline Vec3d<T> operator+ ( const Vec3d<T>& u, const Vec3d<T>& v )
  125. {
  126. return Vec3d<T>( u.x + v.x, u.y + v.y, u.z + v.z );
  127. }
  128. template<class T>
  129. inline Vec3d<T> operator- ( const Vec3d<T>& u, const Vec3d<T>& v )
  130. {
  131. return Vec3d<T>( u.x - v.x, u.y - v.y, u.z - v.z );
  132. }
  133. template<class T>
  134. inline Vec3d<T> operator* ( const Vec3d<T>& u, const Vec3d<T>& v )
  135. {
  136. return Vec3d<T>( u.x * v.x, u.y * v.y, u.z * v.z );
  137. }
  138. template<class T, class T2>
  139. inline Vec3d<T> operator* ( const Vec3d<T>& u, T2 f )
  140. {
  141. return Vec3d<T>( u.x * f, u.y * f, u.z * f );
  142. }
  143. template<class T, class T2>
  144. inline Vec3d<T> operator* ( T2 f, const Vec3d<T>& v )
  145. {
  146. return Vec3d<T>( f * v.x, f * v.y, f * v.z );
  147. }
  148. template<class T, class T2>
  149. inline Vec3d<T> operator/ ( const Vec3d<T>& v, T2 f )
  150. {
  151. return Vec3d<T>( v.x / f, v.y / f, v.z / f);
  152. }
  153. template<class T>
  154. inline Vec3d<T> operator/ (const Vec3d<T>& u, const Vec3d<T>& v )
  155. {
  156. return Vec3d<T>( u.x / v.x, u.y / v.y, u.z / v.z );
  157. }
  158. template<class T>
  159. inline Vec3d<T>& Vec3d<T>::operator+=(const Vec3d<T>& v)
  160. {
  161. x += v.x;
  162. y += v.y;
  163. z += v.z;
  164. return *this;
  165. }
  166. template<class T>
  167. inline Vec3d<T>& Vec3d<T>::operator-=( const Vec3d<T>& v)
  168. {
  169. x -= v.x;
  170. y -= v.y;
  171. z -= v.z;
  172. return *this;
  173. }
  174. template<class T>
  175. inline Vec3d<T>& Vec3d<T>::operator*=(const Vec3d<T>& v)
  176. {
  177. x *= v.x;
  178. y *= v.y;
  179. z *= v.z;
  180. return *this;
  181. }
  182. }