mat.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (C) 2011 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 ANDROID_MAT_H
  17. #define ANDROID_MAT_H
  18. #include "vec.h"
  19. #include "traits.h"
  20. // -----------------------------------------------------------------------
  21. namespace android {
  22. template <typename TYPE, size_t C, size_t R>
  23. class mat;
  24. namespace helpers {
  25. template <typename TYPE, size_t C, size_t R>
  26. mat<TYPE, C, R>& doAssign(
  27. mat<TYPE, C, R>& lhs,
  28. typename TypeTraits<TYPE>::ParameterType rhs) {
  29. for (size_t i=0 ; i<C ; i++)
  30. for (size_t j=0 ; j<R ; j++)
  31. lhs[i][j] = (i==j) ? rhs : 0;
  32. return lhs;
  33. }
  34. template <typename TYPE, size_t C, size_t R, size_t D>
  35. mat<TYPE, C, R> PURE doMul(
  36. const mat<TYPE, D, R>& lhs,
  37. const mat<TYPE, C, D>& rhs)
  38. {
  39. mat<TYPE, C, R> res;
  40. for (size_t c=0 ; c<C ; c++) {
  41. for (size_t r=0 ; r<R ; r++) {
  42. TYPE v(0);
  43. for (size_t k=0 ; k<D ; k++) {
  44. v += lhs[k][r] * rhs[c][k];
  45. }
  46. res[c][r] = v;
  47. }
  48. }
  49. return res;
  50. }
  51. template <typename TYPE, size_t R, size_t D>
  52. vec<TYPE, R> PURE doMul(
  53. const mat<TYPE, D, R>& lhs,
  54. const vec<TYPE, D>& rhs)
  55. {
  56. vec<TYPE, R> res;
  57. for (size_t r=0 ; r<R ; r++) {
  58. TYPE v(0);
  59. for (size_t k=0 ; k<D ; k++) {
  60. v += lhs[k][r] * rhs[k];
  61. }
  62. res[r] = v;
  63. }
  64. return res;
  65. }
  66. template <typename TYPE, size_t C, size_t R>
  67. mat<TYPE, C, R> PURE doMul(
  68. const vec<TYPE, R>& lhs,
  69. const mat<TYPE, C, 1>& rhs)
  70. {
  71. mat<TYPE, C, R> res;
  72. for (size_t c=0 ; c<C ; c++) {
  73. for (size_t r=0 ; r<R ; r++) {
  74. res[c][r] = lhs[r] * rhs[c][0];
  75. }
  76. }
  77. return res;
  78. }
  79. template <typename TYPE, size_t C, size_t R>
  80. mat<TYPE, C, R> PURE doMul(
  81. const mat<TYPE, C, R>& rhs,
  82. typename TypeTraits<TYPE>::ParameterType v)
  83. {
  84. mat<TYPE, C, R> res;
  85. for (size_t c=0 ; c<C ; c++) {
  86. for (size_t r=0 ; r<R ; r++) {
  87. res[c][r] = rhs[c][r] * v;
  88. }
  89. }
  90. return res;
  91. }
  92. template <typename TYPE, size_t C, size_t R>
  93. mat<TYPE, C, R> PURE doMul(
  94. typename TypeTraits<TYPE>::ParameterType v,
  95. const mat<TYPE, C, R>& rhs)
  96. {
  97. mat<TYPE, C, R> res;
  98. for (size_t c=0 ; c<C ; c++) {
  99. for (size_t r=0 ; r<R ; r++) {
  100. res[c][r] = v * rhs[c][r];
  101. }
  102. }
  103. return res;
  104. }
  105. }; // namespace helpers
  106. // -----------------------------------------------------------------------
  107. template <typename TYPE, size_t C, size_t R>
  108. class mat : public vec< vec<TYPE, R>, C > {
  109. typedef typename TypeTraits<TYPE>::ParameterType pTYPE;
  110. typedef vec< vec<TYPE, R>, C > base;
  111. public:
  112. // STL-like interface.
  113. typedef TYPE value_type;
  114. typedef TYPE& reference;
  115. typedef TYPE const& const_reference;
  116. typedef size_t size_type;
  117. size_type size() const { return R*C; }
  118. enum { ROWS = R, COLS = C };
  119. // -----------------------------------------------------------------------
  120. // default constructors
  121. mat() { }
  122. mat(const mat& rhs) : base(rhs) { }
  123. mat(const base& rhs) : base(rhs) { }
  124. // -----------------------------------------------------------------------
  125. // conversion constructors
  126. // sets the diagonal to the value, off-diagonal to zero
  127. mat(pTYPE rhs) {
  128. helpers::doAssign(*this, rhs);
  129. }
  130. // -----------------------------------------------------------------------
  131. // Assignment
  132. mat& operator=(const mat& rhs) {
  133. base::operator=(rhs);
  134. return *this;
  135. }
  136. mat& operator=(const base& rhs) {
  137. base::operator=(rhs);
  138. return *this;
  139. }
  140. mat& operator=(pTYPE rhs) {
  141. return helpers::doAssign(*this, rhs);
  142. }
  143. // -----------------------------------------------------------------------
  144. // non-member function declaration and definition
  145. friend inline mat PURE operator + (const mat& lhs, const mat& rhs) {
  146. return helpers::doAdd(
  147. static_cast<const base&>(lhs),
  148. static_cast<const base&>(rhs));
  149. }
  150. friend inline mat PURE operator - (const mat& lhs, const mat& rhs) {
  151. return helpers::doSub(
  152. static_cast<const base&>(lhs),
  153. static_cast<const base&>(rhs));
  154. }
  155. // matrix*matrix
  156. template <size_t D>
  157. friend mat PURE operator * (
  158. const mat<TYPE, D, R>& lhs,
  159. const mat<TYPE, C, D>& rhs) {
  160. return helpers::doMul(lhs, rhs);
  161. }
  162. // matrix*vector
  163. friend vec<TYPE, R> PURE operator * (
  164. const mat& lhs, const vec<TYPE, C>& rhs) {
  165. return helpers::doMul(lhs, rhs);
  166. }
  167. // vector*matrix
  168. friend mat PURE operator * (
  169. const vec<TYPE, R>& lhs, const mat<TYPE, C, 1>& rhs) {
  170. return helpers::doMul(lhs, rhs);
  171. }
  172. // matrix*scalar
  173. friend inline mat PURE operator * (const mat& lhs, pTYPE v) {
  174. return helpers::doMul(lhs, v);
  175. }
  176. // scalar*matrix
  177. friend inline mat PURE operator * (pTYPE v, const mat& rhs) {
  178. return helpers::doMul(v, rhs);
  179. }
  180. // -----------------------------------------------------------------------
  181. // streaming operator to set the columns of the matrix:
  182. // example:
  183. // mat33_t m;
  184. // m << v0 << v1 << v2;
  185. // column_builder<> stores the matrix and knows which column to set
  186. template<size_t PREV_COLUMN>
  187. struct column_builder {
  188. mat& matrix;
  189. column_builder(mat& matrix) : matrix(matrix) { }
  190. };
  191. // operator << is not a method of column_builder<> so we can
  192. // overload it for unauthorized values (partial specialization
  193. // not allowed in class-scope).
  194. // we just set the column and return the next column_builder<>
  195. template<size_t PREV_COLUMN>
  196. friend column_builder<PREV_COLUMN+1> operator << (
  197. const column_builder<PREV_COLUMN>& lhs,
  198. const vec<TYPE, R>& rhs) {
  199. lhs.matrix[PREV_COLUMN+1] = rhs;
  200. return column_builder<PREV_COLUMN+1>(lhs.matrix);
  201. }
  202. // we return void here so we get a compile-time error if the
  203. // user tries to set too many columns
  204. friend void operator << (
  205. const column_builder<C-2>& lhs,
  206. const vec<TYPE, R>& rhs) {
  207. lhs.matrix[C-1] = rhs;
  208. }
  209. // this is where the process starts. we set the first columns and
  210. // return the next column_builder<>
  211. column_builder<0> operator << (const vec<TYPE, R>& rhs) {
  212. (*this)[0] = rhs;
  213. return column_builder<0>(*this);
  214. }
  215. };
  216. // Specialize column matrix so they're exactly equivalent to a vector
  217. template <typename TYPE, size_t R>
  218. class mat<TYPE, 1, R> : public vec<TYPE, R> {
  219. typedef vec<TYPE, R> base;
  220. public:
  221. // STL-like interface.
  222. typedef TYPE value_type;
  223. typedef TYPE& reference;
  224. typedef TYPE const& const_reference;
  225. typedef size_t size_type;
  226. size_type size() const { return R; }
  227. enum { ROWS = R, COLS = 1 };
  228. mat() { }
  229. mat(const base& rhs) : base(rhs) { }
  230. mat(const mat& rhs) : base(rhs) { }
  231. mat(const TYPE& rhs) { helpers::doAssign(*this, rhs); }
  232. mat& operator=(const mat& rhs) { base::operator=(rhs); return *this; }
  233. mat& operator=(const base& rhs) { base::operator=(rhs); return *this; }
  234. mat& operator=(const TYPE& rhs) { return helpers::doAssign(*this, rhs); }
  235. // we only have one column, so ignore the index
  236. const base& operator[](size_t) const { return *this; }
  237. base& operator[](size_t) { return *this; }
  238. void operator << (const vec<TYPE, R>& rhs) { base::operator[](0) = rhs; }
  239. };
  240. // -----------------------------------------------------------------------
  241. // matrix functions
  242. // transpose. this handles matrices of matrices
  243. inline int PURE transpose(int v) { return v; }
  244. inline float PURE transpose(float v) { return v; }
  245. inline double PURE transpose(double v) { return v; }
  246. // Transpose a matrix
  247. template <typename TYPE, size_t C, size_t R>
  248. mat<TYPE, R, C> PURE transpose(const mat<TYPE, C, R>& m) {
  249. mat<TYPE, R, C> r;
  250. for (size_t i=0 ; i<R ; i++)
  251. for (size_t j=0 ; j<C ; j++)
  252. r[i][j] = transpose(m[j][i]);
  253. return r;
  254. }
  255. // Calculate the trace of a matrix
  256. template <typename TYPE, size_t C> static TYPE trace(const mat<TYPE, C, C>& m) {
  257. TYPE t;
  258. for (size_t i=0 ; i<C ; i++)
  259. t += m[i][i];
  260. return t;
  261. }
  262. // Test positive-semidefiniteness of a matrix
  263. template <typename TYPE, size_t C>
  264. static bool isPositiveSemidefinite(const mat<TYPE, C, C>& m, TYPE tolerance) {
  265. for (size_t i=0 ; i<C ; i++)
  266. if (m[i][i] < 0)
  267. return false;
  268. for (size_t i=0 ; i<C ; i++)
  269. for (size_t j=i+1 ; j<C ; j++)
  270. if (fabs(m[i][j] - m[j][i]) > tolerance)
  271. return false;
  272. return true;
  273. }
  274. // Transpose a vector
  275. template <
  276. template<typename T, size_t S> class VEC,
  277. typename TYPE,
  278. size_t SIZE
  279. >
  280. mat<TYPE, SIZE, 1> PURE transpose(const VEC<TYPE, SIZE>& v) {
  281. mat<TYPE, SIZE, 1> r;
  282. for (size_t i=0 ; i<SIZE ; i++)
  283. r[i][0] = transpose(v[i]);
  284. return r;
  285. }
  286. // -----------------------------------------------------------------------
  287. // "dumb" matrix inversion
  288. template<typename T, size_t N>
  289. mat<T, N, N> PURE invert(const mat<T, N, N>& src) {
  290. T t;
  291. size_t swap;
  292. mat<T, N, N> tmp(src);
  293. mat<T, N, N> inverse(1);
  294. for (size_t i=0 ; i<N ; i++) {
  295. // look for largest element in column
  296. swap = i;
  297. for (size_t j=i+1 ; j<N ; j++) {
  298. if (fabs(tmp[j][i]) > fabs(tmp[i][i])) {
  299. swap = j;
  300. }
  301. }
  302. if (swap != i) {
  303. /* swap rows. */
  304. for (size_t k=0 ; k<N ; k++) {
  305. t = tmp[i][k];
  306. tmp[i][k] = tmp[swap][k];
  307. tmp[swap][k] = t;
  308. t = inverse[i][k];
  309. inverse[i][k] = inverse[swap][k];
  310. inverse[swap][k] = t;
  311. }
  312. }
  313. t = 1 / tmp[i][i];
  314. for (size_t k=0 ; k<N ; k++) {
  315. tmp[i][k] *= t;
  316. inverse[i][k] *= t;
  317. }
  318. for (size_t j=0 ; j<N ; j++) {
  319. if (j != i) {
  320. t = tmp[j][i];
  321. for (size_t k=0 ; k<N ; k++) {
  322. tmp[j][k] -= tmp[i][k] * t;
  323. inverse[j][k] -= inverse[i][k] * t;
  324. }
  325. }
  326. }
  327. }
  328. return inverse;
  329. }
  330. // -----------------------------------------------------------------------
  331. typedef mat<float, 2, 2> mat22_t;
  332. typedef mat<float, 3, 3> mat33_t;
  333. typedef mat<float, 4, 4> mat44_t;
  334. // -----------------------------------------------------------------------
  335. }; // namespace android
  336. #endif /* ANDROID_MAT_H */