o3dgcVector.inl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #ifndef O3DGC_VECTOR_INL
  21. #define O3DGC_VECTOR_INL
  22. namespace o3dgc
  23. {
  24. template <typename T>
  25. inline Vec3<T> operator*(T lhs, const Vec3<T> & rhs)
  26. {
  27. return Vec3<T>(lhs * rhs.X(), lhs * rhs.Y(), lhs * rhs.Z());
  28. }
  29. template <typename T>
  30. inline T & Vec3<T>::X()
  31. {
  32. return m_data[0];
  33. }
  34. template <typename T>
  35. inline T & Vec3<T>::Y()
  36. {
  37. return m_data[1];
  38. }
  39. template <typename T>
  40. inline T & Vec3<T>::Z()
  41. {
  42. return m_data[2];
  43. }
  44. template <typename T>
  45. inline const T & Vec3<T>::X() const
  46. {
  47. return m_data[0];
  48. }
  49. template <typename T>
  50. inline const T & Vec3<T>::Y() const
  51. {
  52. return m_data[1];
  53. }
  54. template <typename T>
  55. inline const T & Vec3<T>::Z() const
  56. {
  57. return m_data[2];
  58. }
  59. template <typename T>
  60. inline double Vec3<T>::GetNorm() const
  61. {
  62. double a = (double) (m_data[0]);
  63. double b = (double) (m_data[1]);
  64. double c = (double) (m_data[2]);
  65. return sqrt(a*a+b*b+c*c);
  66. }
  67. template <typename T>
  68. inline void Vec3<T>::operator= (const Vec3 & rhs)
  69. {
  70. this->m_data[0] = rhs.m_data[0];
  71. this->m_data[1] = rhs.m_data[1];
  72. this->m_data[2] = rhs.m_data[2];
  73. }
  74. template <typename T>
  75. inline void Vec3<T>::operator+=(const Vec3 & rhs)
  76. {
  77. this->m_data[0] += rhs.m_data[0];
  78. this->m_data[1] += rhs.m_data[1];
  79. this->m_data[2] += rhs.m_data[2];
  80. }
  81. template <typename T>
  82. inline void Vec3<T>::operator-=(const Vec3 & rhs)
  83. {
  84. this->m_data[0] -= rhs.m_data[0];
  85. this->m_data[1] -= rhs.m_data[1];
  86. this->m_data[2] -= rhs.m_data[2];
  87. }
  88. template <typename T>
  89. inline void Vec3<T>::operator-=(T a)
  90. {
  91. this->m_data[0] -= a;
  92. this->m_data[1] -= a;
  93. this->m_data[2] -= a;
  94. }
  95. template <typename T>
  96. inline void Vec3<T>::operator+=(T a)
  97. {
  98. this->m_data[0] += a;
  99. this->m_data[1] += a;
  100. this->m_data[2] += a;
  101. }
  102. template <typename T>
  103. inline void Vec3<T>::operator/=(T a)
  104. {
  105. this->m_data[0] /= a;
  106. this->m_data[1] /= a;
  107. this->m_data[2] /= a;
  108. }
  109. template <typename T>
  110. inline void Vec3<T>::operator*=(T a)
  111. {
  112. this->m_data[0] *= a;
  113. this->m_data[1] *= a;
  114. this->m_data[2] *= a;
  115. }
  116. template <typename T>
  117. inline Vec3<T> Vec3<T>::operator^ (const Vec3<T> & rhs) const
  118. {
  119. return Vec3<T>(m_data[1] * rhs.m_data[2] - m_data[2] * rhs.m_data[1],
  120. m_data[2] * rhs.m_data[0] - m_data[0] * rhs.m_data[2],
  121. m_data[0] * rhs.m_data[1] - m_data[1] * rhs.m_data[0]);
  122. }
  123. template <typename T>
  124. inline T Vec3<T>::operator*(const Vec3<T> & rhs) const
  125. {
  126. return (m_data[0] * rhs.m_data[0] + m_data[1] * rhs.m_data[1] + m_data[2] * rhs.m_data[2]);
  127. }
  128. template <typename T>
  129. inline Vec3<T> Vec3<T>::operator+(const Vec3<T> & rhs) const
  130. {
  131. return Vec3<T>(m_data[0] + rhs.m_data[0],m_data[1] + rhs.m_data[1],m_data[2] + rhs.m_data[2]);
  132. }
  133. template <typename T>
  134. inline Vec3<T> Vec3<T>::operator-(const Vec3<T> & rhs) const
  135. {
  136. return Vec3<T>(m_data[0] - rhs.m_data[0],m_data[1] - rhs.m_data[1],m_data[2] - rhs.m_data[2]);
  137. }
  138. template <typename T>
  139. inline Vec3<T> Vec3<T>::operator-() const
  140. {
  141. return Vec3<T>(-m_data[0],-m_data[1],-m_data[2]);
  142. }
  143. template <typename T>
  144. inline Vec3<T> Vec3<T>::operator*(T rhs) const
  145. {
  146. return Vec3<T>(rhs * this->m_data[0], rhs * this->m_data[1], rhs * this->m_data[2]);
  147. }
  148. template <typename T>
  149. inline Vec3<T> Vec3<T>::operator/ (T rhs) const
  150. {
  151. return Vec3<T>(m_data[0] / rhs, m_data[1] / rhs, m_data[2] / rhs);
  152. }
  153. template <typename T>
  154. inline Vec3<T>::Vec3(T a)
  155. {
  156. m_data[0] = m_data[1] = m_data[2] = a;
  157. }
  158. template <typename T>
  159. inline Vec3<T>::Vec3(T x, T y, T z)
  160. {
  161. m_data[0] = x;
  162. m_data[1] = y;
  163. m_data[2] = z;
  164. }
  165. template <typename T>
  166. inline Vec3<T>::Vec3(const Vec3 & rhs)
  167. {
  168. m_data[0] = rhs.m_data[0];
  169. m_data[1] = rhs.m_data[1];
  170. m_data[2] = rhs.m_data[2];
  171. }
  172. template <typename T>
  173. inline Vec3<T>::~Vec3(void){}
  174. template <typename T>
  175. inline Vec3<T>::Vec3() {}
  176. template <typename T>
  177. inline Vec2<T> operator*(T lhs, const Vec2<T> & rhs)
  178. {
  179. return Vec2<T>(lhs * rhs.X(), lhs * rhs.Y());
  180. }
  181. template <typename T>
  182. inline T & Vec2<T>::X()
  183. {
  184. return m_data[0];
  185. }
  186. template <typename T>
  187. inline T & Vec2<T>::Y()
  188. {
  189. return m_data[1];
  190. }
  191. template <typename T>
  192. inline const T & Vec2<T>::X() const
  193. {
  194. return m_data[0];
  195. }
  196. template <typename T>
  197. inline const T & Vec2<T>::Y() const
  198. {
  199. return m_data[1];
  200. }
  201. template <typename T>
  202. inline double Vec2<T>::GetNorm() const
  203. {
  204. double a = (double) (m_data[0]);
  205. double b = (double) (m_data[1]);
  206. return sqrt(a*a+b*b);
  207. }
  208. template <typename T>
  209. inline void Vec2<T>::operator= (const Vec2 & rhs)
  210. {
  211. this->m_data[0] = rhs.m_data[0];
  212. this->m_data[1] = rhs.m_data[1];
  213. }
  214. template <typename T>
  215. inline void Vec2<T>::operator+=(const Vec2 & rhs)
  216. {
  217. this->m_data[0] += rhs.m_data[0];
  218. this->m_data[1] += rhs.m_data[1];
  219. }
  220. template <typename T>
  221. inline void Vec2<T>::operator-=(const Vec2 & rhs)
  222. {
  223. this->m_data[0] -= rhs.m_data[0];
  224. this->m_data[1] -= rhs.m_data[1];
  225. }
  226. template <typename T>
  227. inline void Vec2<T>::operator-=(T a)
  228. {
  229. this->m_data[0] -= a;
  230. this->m_data[1] -= a;
  231. }
  232. template <typename T>
  233. inline void Vec2<T>::operator+=(T a)
  234. {
  235. this->m_data[0] += a;
  236. this->m_data[1] += a;
  237. }
  238. template <typename T>
  239. inline void Vec2<T>::operator/=(T a)
  240. {
  241. this->m_data[0] /= a;
  242. this->m_data[1] /= a;
  243. }
  244. template <typename T>
  245. inline void Vec2<T>::operator*=(T a)
  246. {
  247. this->m_data[0] *= a;
  248. this->m_data[1] *= a;
  249. }
  250. template <typename T>
  251. inline T Vec2<T>::operator^ (const Vec2<T> & rhs) const
  252. {
  253. return m_data[0] * rhs.m_data[1] - m_data[1] * rhs.m_data[0];
  254. }
  255. template <typename T>
  256. inline T Vec2<T>::operator*(const Vec2<T> & rhs) const
  257. {
  258. return (m_data[0] * rhs.m_data[0] + m_data[1] * rhs.m_data[1]);
  259. }
  260. template <typename T>
  261. inline Vec2<T> Vec2<T>::operator+(const Vec2<T> & rhs) const
  262. {
  263. return Vec2<T>(m_data[0] + rhs.m_data[0],m_data[1] + rhs.m_data[1]);
  264. }
  265. template <typename T>
  266. inline Vec2<T> Vec2<T>::operator-(const Vec2<T> & rhs) const
  267. {
  268. return Vec2<T>(m_data[0] - rhs.m_data[0],m_data[1] - rhs.m_data[1]);
  269. }
  270. template <typename T>
  271. inline Vec2<T> Vec2<T>::operator-() const
  272. {
  273. return Vec2<T>(-m_data[0],-m_data[1]) ;
  274. }
  275. template <typename T>
  276. inline Vec2<T> Vec2<T>::operator*(T rhs) const
  277. {
  278. return Vec2<T>(rhs * this->m_data[0], rhs * this->m_data[1]);
  279. }
  280. template <typename T>
  281. inline Vec2<T> Vec2<T>::operator/ (T rhs) const
  282. {
  283. return Vec2<T>(m_data[0] / rhs, m_data[1] / rhs);
  284. }
  285. template <typename T>
  286. inline Vec2<T>::Vec2(T a)
  287. {
  288. m_data[0] = m_data[1] = a;
  289. }
  290. template <typename T>
  291. inline Vec2<T>::Vec2(T x, T y)
  292. {
  293. m_data[0] = x;
  294. m_data[1] = y;
  295. }
  296. template <typename T>
  297. inline Vec2<T>::Vec2(const Vec2 & rhs)
  298. {
  299. m_data[0] = rhs.m_data[0];
  300. m_data[1] = rhs.m_data[1];
  301. }
  302. template <typename T>
  303. inline Vec2<T>::~Vec2(void){}
  304. template <typename T>
  305. inline Vec2<T>::Vec2() {}
  306. }
  307. #endif //O3DGC_VECTOR_INL