TVecHelpers.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef TVEC_IMPLEMENTATION
  17. #error "Don't include TVecHelpers.h directly. use ui/vec*.h instead"
  18. #else
  19. #undef TVEC_IMPLEMENTATION
  20. #endif
  21. #ifndef UI_TVEC_HELPERS_H
  22. #define UI_TVEC_HELPERS_H
  23. #include <stdint.h>
  24. #include <sys/types.h>
  25. #define PURE __attribute__((pure))
  26. namespace android {
  27. // -------------------------------------------------------------------------------------
  28. /*
  29. * No user serviceable parts here.
  30. *
  31. * Don't use this file directly, instead include ui/vec{2|3|4}.h
  32. */
  33. /*
  34. * This class casts itself into anything and assign itself from anything!
  35. * Use with caution!
  36. */
  37. template <typename TYPE>
  38. struct Impersonator {
  39. Impersonator& operator = (const TYPE& rhs) {
  40. reinterpret_cast<TYPE&>(*this) = rhs;
  41. return *this;
  42. }
  43. operator TYPE& () {
  44. return reinterpret_cast<TYPE&>(*this);
  45. }
  46. operator TYPE const& () const {
  47. return reinterpret_cast<TYPE const&>(*this);
  48. }
  49. };
  50. /*
  51. * TVec{Add|Product}Operators implements basic arithmetic and basic compound assignments
  52. * operators on a vector of type BASE<T>.
  53. *
  54. * BASE only needs to implement operator[] and size().
  55. * By simply inheriting from TVec{Add|Product}Operators<BASE, T> BASE will automatically
  56. * get all the functionality here.
  57. */
  58. template <template<typename T> class BASE, typename T>
  59. class TVecAddOperators {
  60. public:
  61. /* compound assignment from a another vector of the same size but different
  62. * element type.
  63. */
  64. template <typename OTHER>
  65. BASE<T>& operator += (const BASE<OTHER>& v) {
  66. BASE<T>& rhs = static_cast<BASE<T>&>(*this);
  67. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  68. rhs[i] += v[i];
  69. }
  70. return rhs;
  71. }
  72. template <typename OTHER>
  73. BASE<T>& operator -= (const BASE<OTHER>& v) {
  74. BASE<T>& rhs = static_cast<BASE<T>&>(*this);
  75. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  76. rhs[i] -= v[i];
  77. }
  78. return rhs;
  79. }
  80. /* compound assignment from a another vector of the same type.
  81. * These operators can be used for implicit conversion and handle operations
  82. * like "vector *= scalar" by letting the compiler implicitly convert a scalar
  83. * to a vector (assuming the BASE<T> allows it).
  84. */
  85. BASE<T>& operator += (const BASE<T>& v) {
  86. BASE<T>& rhs = static_cast<BASE<T>&>(*this);
  87. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  88. rhs[i] += v[i];
  89. }
  90. return rhs;
  91. }
  92. BASE<T>& operator -= (const BASE<T>& v) {
  93. BASE<T>& rhs = static_cast<BASE<T>&>(*this);
  94. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  95. rhs[i] -= v[i];
  96. }
  97. return rhs;
  98. }
  99. /*
  100. * NOTE: the functions below ARE NOT member methods. They are friend functions
  101. * with they definition inlined with their declaration. This makes these
  102. * template functions available to the compiler when (and only when) this class
  103. * is instantiated, at which point they're only templated on the 2nd parameter
  104. * (the first one, BASE<T> being known).
  105. */
  106. /* The operators below handle operation between vectors of the same side
  107. * but of a different element type.
  108. */
  109. template<typename RT>
  110. friend inline
  111. BASE<T> PURE operator +(const BASE<T>& lv, const BASE<RT>& rv) {
  112. return BASE<T>(lv) += rv;
  113. }
  114. template<typename RT>
  115. friend inline
  116. BASE<T> PURE operator -(const BASE<T>& lv, const BASE<RT>& rv) {
  117. return BASE<T>(lv) -= rv;
  118. }
  119. /* The operators below (which are not templates once this class is instanced,
  120. * i.e.: BASE<T> is known) can be used for implicit conversion on both sides.
  121. * These handle operations like "vector * scalar" and "scalar * vector" by
  122. * letting the compiler implicitly convert a scalar to a vector (assuming
  123. * the BASE<T> allows it).
  124. */
  125. friend inline
  126. BASE<T> PURE operator +(const BASE<T>& lv, const BASE<T>& rv) {
  127. return BASE<T>(lv) += rv;
  128. }
  129. friend inline
  130. BASE<T> PURE operator -(const BASE<T>& lv, const BASE<T>& rv) {
  131. return BASE<T>(lv) -= rv;
  132. }
  133. };
  134. template <template<typename T> class BASE, typename T>
  135. class TVecProductOperators {
  136. public:
  137. /* compound assignment from a another vector of the same size but different
  138. * element type.
  139. */
  140. template <typename OTHER>
  141. BASE<T>& operator *= (const BASE<OTHER>& v) {
  142. BASE<T>& rhs = static_cast<BASE<T>&>(*this);
  143. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  144. rhs[i] *= v[i];
  145. }
  146. return rhs;
  147. }
  148. template <typename OTHER>
  149. BASE<T>& operator /= (const BASE<OTHER>& v) {
  150. BASE<T>& rhs = static_cast<BASE<T>&>(*this);
  151. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  152. rhs[i] /= v[i];
  153. }
  154. return rhs;
  155. }
  156. /* compound assignment from a another vector of the same type.
  157. * These operators can be used for implicit conversion and handle operations
  158. * like "vector *= scalar" by letting the compiler implicitly convert a scalar
  159. * to a vector (assuming the BASE<T> allows it).
  160. */
  161. BASE<T>& operator *= (const BASE<T>& v) {
  162. BASE<T>& rhs = static_cast<BASE<T>&>(*this);
  163. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  164. rhs[i] *= v[i];
  165. }
  166. return rhs;
  167. }
  168. BASE<T>& operator /= (const BASE<T>& v) {
  169. BASE<T>& rhs = static_cast<BASE<T>&>(*this);
  170. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  171. rhs[i] /= v[i];
  172. }
  173. return rhs;
  174. }
  175. /*
  176. * NOTE: the functions below ARE NOT member methods. They are friend functions
  177. * with they definition inlined with their declaration. This makes these
  178. * template functions available to the compiler when (and only when) this class
  179. * is instantiated, at which point they're only templated on the 2nd parameter
  180. * (the first one, BASE<T> being known).
  181. */
  182. /* The operators below handle operation between vectors of the same side
  183. * but of a different element type.
  184. */
  185. template<typename RT>
  186. friend inline
  187. BASE<T> PURE operator *(const BASE<T>& lv, const BASE<RT>& rv) {
  188. return BASE<T>(lv) *= rv;
  189. }
  190. template<typename RT>
  191. friend inline
  192. BASE<T> PURE operator /(const BASE<T>& lv, const BASE<RT>& rv) {
  193. return BASE<T>(lv) /= rv;
  194. }
  195. /* The operators below (which are not templates once this class is instanced,
  196. * i.e.: BASE<T> is known) can be used for implicit conversion on both sides.
  197. * These handle operations like "vector * scalar" and "scalar * vector" by
  198. * letting the compiler implicitly convert a scalar to a vector (assuming
  199. * the BASE<T> allows it).
  200. */
  201. friend inline
  202. BASE<T> PURE operator *(const BASE<T>& lv, const BASE<T>& rv) {
  203. return BASE<T>(lv) *= rv;
  204. }
  205. friend inline
  206. BASE<T> PURE operator /(const BASE<T>& lv, const BASE<T>& rv) {
  207. return BASE<T>(lv) /= rv;
  208. }
  209. };
  210. /*
  211. * TVecUnaryOperators implements unary operators on a vector of type BASE<T>.
  212. *
  213. * BASE only needs to implement operator[] and size().
  214. * By simply inheriting from TVecUnaryOperators<BASE, T> BASE will automatically
  215. * get all the functionality here.
  216. *
  217. * These operators are implemented as friend functions of TVecUnaryOperators<BASE, T>
  218. */
  219. template <template<typename T> class BASE, typename T>
  220. class TVecUnaryOperators {
  221. public:
  222. BASE<T>& operator ++ () {
  223. BASE<T>& rhs = static_cast<BASE<T>&>(*this);
  224. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  225. ++rhs[i];
  226. }
  227. return rhs;
  228. }
  229. BASE<T>& operator -- () {
  230. BASE<T>& rhs = static_cast<BASE<T>&>(*this);
  231. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  232. --rhs[i];
  233. }
  234. return rhs;
  235. }
  236. BASE<T> operator - () const {
  237. BASE<T> r(BASE<T>::NO_INIT);
  238. BASE<T> const& rv(static_cast<BASE<T> const&>(*this));
  239. for (size_t i=0 ; i<BASE<T>::size() ; i++) {
  240. r[i] = -rv[i];
  241. }
  242. return r;
  243. }
  244. };
  245. /*
  246. * TVecComparisonOperators implements relational/comparison operators
  247. * on a vector of type BASE<T>.
  248. *
  249. * BASE only needs to implement operator[] and size().
  250. * By simply inheriting from TVecComparisonOperators<BASE, T> BASE will automatically
  251. * get all the functionality here.
  252. */
  253. template <template<typename T> class BASE, typename T>
  254. class TVecComparisonOperators {
  255. public:
  256. /*
  257. * NOTE: the functions below ARE NOT member methods. They are friend functions
  258. * with they definition inlined with their declaration. This makes these
  259. * template functions available to the compiler when (and only when) this class
  260. * is instantiated, at which point they're only templated on the 2nd parameter
  261. * (the first one, BASE<T> being known).
  262. */
  263. template<typename RT>
  264. friend inline
  265. bool PURE operator ==(const BASE<T>& lv, const BASE<RT>& rv) {
  266. for (size_t i = 0; i < BASE<T>::size(); i++)
  267. if (lv[i] != rv[i])
  268. return false;
  269. return true;
  270. }
  271. template<typename RT>
  272. friend inline
  273. bool PURE operator !=(const BASE<T>& lv, const BASE<RT>& rv) {
  274. return !operator ==(lv, rv);
  275. }
  276. template<typename RT>
  277. friend inline
  278. bool PURE operator >(const BASE<T>& lv, const BASE<RT>& rv) {
  279. for (size_t i = 0; i < BASE<T>::size(); i++)
  280. if (lv[i] <= rv[i])
  281. return false;
  282. return true;
  283. }
  284. template<typename RT>
  285. friend inline
  286. bool PURE operator <=(const BASE<T>& lv, const BASE<RT>& rv) {
  287. return !(lv > rv);
  288. }
  289. template<typename RT>
  290. friend inline
  291. bool PURE operator <(const BASE<T>& lv, const BASE<RT>& rv) {
  292. for (size_t i = 0; i < BASE<T>::size(); i++)
  293. if (lv[i] >= rv[i])
  294. return false;
  295. return true;
  296. }
  297. template<typename RT>
  298. friend inline
  299. bool PURE operator >=(const BASE<T>& lv, const BASE<RT>& rv) {
  300. return !(lv < rv);
  301. }
  302. };
  303. /*
  304. * TVecFunctions implements functions on a vector of type BASE<T>.
  305. *
  306. * BASE only needs to implement operator[] and size().
  307. * By simply inheriting from TVecFunctions<BASE, T> BASE will automatically
  308. * get all the functionality here.
  309. */
  310. template <template<typename T> class BASE, typename T>
  311. class TVecFunctions {
  312. public:
  313. /*
  314. * NOTE: the functions below ARE NOT member methods. They are friend functions
  315. * with they definition inlined with their declaration. This makes these
  316. * template functions available to the compiler when (and only when) this class
  317. * is instantiated, at which point they're only templated on the 2nd parameter
  318. * (the first one, BASE<T> being known).
  319. */
  320. template<typename RT>
  321. friend inline
  322. T PURE dot(const BASE<T>& lv, const BASE<RT>& rv) {
  323. T r(0);
  324. for (size_t i = 0; i < BASE<T>::size(); i++)
  325. r += lv[i]*rv[i];
  326. return r;
  327. }
  328. friend inline
  329. T PURE length(const BASE<T>& lv) {
  330. return sqrt( dot(lv, lv) );
  331. }
  332. template<typename RT>
  333. friend inline
  334. T PURE distance(const BASE<T>& lv, const BASE<RT>& rv) {
  335. return length(rv - lv);
  336. }
  337. friend inline
  338. BASE<T> PURE normalize(const BASE<T>& lv) {
  339. return lv * (1 / length(lv));
  340. }
  341. };
  342. #undef PURE
  343. // -------------------------------------------------------------------------------------
  344. }; // namespace android
  345. #endif /* UI_TVEC_HELPERS_H */