rsMatrix4x4.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "rsMatrix2x2.h"
  17. #include "rsMatrix3x3.h"
  18. #include "rsMatrix4x4.h"
  19. #include "stdlib.h"
  20. #include "string.h"
  21. #include "math.h"
  22. namespace android {
  23. namespace renderscript {
  24. //////////////////////////////////////////////////////////////////////////////
  25. // Heavy math functions
  26. //////////////////////////////////////////////////////////////////////////////
  27. // Returns true if the matrix was successfully inversed
  28. bool Matrix4x4::inverse() {
  29. rs_matrix4x4 result;
  30. int i, j;
  31. for (i = 0; i < 4; ++i) {
  32. for (j = 0; j < 4; ++j) {
  33. // computeCofactor for int i, int j
  34. int c0 = (i+1) % 4;
  35. int c1 = (i+2) % 4;
  36. int c2 = (i+3) % 4;
  37. int r0 = (j+1) % 4;
  38. int r1 = (j+2) % 4;
  39. int r2 = (j+3) % 4;
  40. float minor =
  41. (m[c0 + 4*r0] * (m[c1 + 4*r1] * m[c2 + 4*r2] - m[c1 + 4*r2] * m[c2 + 4*r1]))
  42. - (m[c0 + 4*r1] * (m[c1 + 4*r0] * m[c2 + 4*r2] - m[c1 + 4*r2] * m[c2 + 4*r0]))
  43. + (m[c0 + 4*r2] * (m[c1 + 4*r0] * m[c2 + 4*r1] - m[c1 + 4*r1] * m[c2 + 4*r0]));
  44. float cofactor = (i+j) & 1 ? -minor : minor;
  45. result.m[4*i + j] = cofactor;
  46. }
  47. }
  48. // Dot product of 0th column of source and 0th row of result
  49. float det = m[0]*result.m[0] + m[4]*result.m[1] +
  50. m[8]*result.m[2] + m[12]*result.m[3];
  51. if (fabs(det) < 1e-6) {
  52. return false;
  53. }
  54. det = 1.0f / det;
  55. for (i = 0; i < 16; ++i) {
  56. m[i] = result.m[i] * det;
  57. }
  58. return true;
  59. }
  60. // Returns true if the matrix was successfully inversed
  61. bool Matrix4x4::inverseTranspose() {
  62. rs_matrix4x4 result;
  63. int i, j;
  64. for (i = 0; i < 4; ++i) {
  65. for (j = 0; j < 4; ++j) {
  66. // computeCofactor for int i, int j
  67. int c0 = (i+1) % 4;
  68. int c1 = (i+2) % 4;
  69. int c2 = (i+3) % 4;
  70. int r0 = (j+1) % 4;
  71. int r1 = (j+2) % 4;
  72. int r2 = (j+3) % 4;
  73. float minor = (m[c0 + 4*r0] * (m[c1 + 4*r1] * m[c2 + 4*r2] - m[c1 + 4*r2] * m[c2 + 4*r1]))
  74. - (m[c0 + 4*r1] * (m[c1 + 4*r0] * m[c2 + 4*r2] - m[c1 + 4*r2] * m[c2 + 4*r0]))
  75. + (m[c0 + 4*r2] * (m[c1 + 4*r0] * m[c2 + 4*r1] - m[c1 + 4*r1] * m[c2 + 4*r0]));
  76. float cofactor = (i+j) & 1 ? -minor : minor;
  77. result.m[4*j + i] = cofactor;
  78. }
  79. }
  80. // Dot product of 0th column of source and 0th column of result
  81. float det = m[0]*result.m[0] + m[4]*result.m[4] +
  82. m[8]*result.m[8] + m[12]*result.m[12];
  83. if (fabs(det) < 1e-6) {
  84. return false;
  85. }
  86. det = 1.0f / det;
  87. for (i = 0; i < 16; ++i) {
  88. m[i] = result.m[i] * det;
  89. }
  90. return true;
  91. }
  92. void Matrix4x4::transpose() {
  93. int i, j;
  94. float temp;
  95. for (i = 0; i < 3; ++i) {
  96. for (j = i + 1; j < 4; ++j) {
  97. temp = m[i*4 + j];
  98. m[i*4 + j] = m[j*4 + i];
  99. m[j*4 + i] = temp;
  100. }
  101. }
  102. }
  103. ///////////////////////////////////////////////////////////////////////////////////
  104. void Matrix4x4::loadIdentity() {
  105. m[0] = 1.f;
  106. m[1] = 0.f;
  107. m[2] = 0.f;
  108. m[3] = 0.f;
  109. m[4] = 0.f;
  110. m[5] = 1.f;
  111. m[6] = 0.f;
  112. m[7] = 0.f;
  113. m[8] = 0.f;
  114. m[9] = 0.f;
  115. m[10] = 1.f;
  116. m[11] = 0.f;
  117. m[12] = 0.f;
  118. m[13] = 0.f;
  119. m[14] = 0.f;
  120. m[15] = 1.f;
  121. }
  122. void Matrix4x4::load(const float *v) {
  123. memcpy(m, v, sizeof(m));
  124. }
  125. void Matrix4x4::load(const rs_matrix4x4 *v) {
  126. memcpy(m, v->m, sizeof(m));
  127. }
  128. void Matrix4x4::load(const rs_matrix3x3 *v) {
  129. m[0] = v->m[0];
  130. m[1] = v->m[1];
  131. m[2] = v->m[2];
  132. m[3] = 0.f;
  133. m[4] = v->m[3];
  134. m[5] = v->m[4];
  135. m[6] = v->m[5];
  136. m[7] = 0.f;
  137. m[8] = v->m[6];
  138. m[9] = v->m[7];
  139. m[10] = v->m[8];
  140. m[11] = 0.f;
  141. m[12] = 0.f;
  142. m[13] = 0.f;
  143. m[14] = 0.f;
  144. m[15] = 1.f;
  145. }
  146. void Matrix4x4::load(const rs_matrix2x2 *v) {
  147. m[0] = v->m[0];
  148. m[1] = v->m[1];
  149. m[2] = 0.f;
  150. m[3] = 0.f;
  151. m[4] = v->m[2];
  152. m[5] = v->m[3];
  153. m[6] = 0.f;
  154. m[7] = 0.f;
  155. m[8] = 0.f;
  156. m[9] = 0.f;
  157. m[10] = 1.f;
  158. m[11] = 0.f;
  159. m[12] = 0.f;
  160. m[13] = 0.f;
  161. m[14] = 0.f;
  162. m[15] = 1.f;
  163. }
  164. void Matrix4x4::loadRotate(float rot, float x, float y, float z) {
  165. float c, s;
  166. m[3] = 0;
  167. m[7] = 0;
  168. m[11]= 0;
  169. m[12]= 0;
  170. m[13]= 0;
  171. m[14]= 0;
  172. m[15]= 1;
  173. rot *= float(M_PI / 180.0f);
  174. c = cosf(rot);
  175. s = sinf(rot);
  176. const float len = x*x + y*y + z*z;
  177. if (len != 1) {
  178. const float recipLen = 1.f / sqrtf(len);
  179. x *= recipLen;
  180. y *= recipLen;
  181. z *= recipLen;
  182. }
  183. const float nc = 1.0f - c;
  184. const float xy = x * y;
  185. const float yz = y * z;
  186. const float zx = z * x;
  187. const float xs = x * s;
  188. const float ys = y * s;
  189. const float zs = z * s;
  190. m[ 0] = x*x*nc + c;
  191. m[ 4] = xy*nc - zs;
  192. m[ 8] = zx*nc + ys;
  193. m[ 1] = xy*nc + zs;
  194. m[ 5] = y*y*nc + c;
  195. m[ 9] = yz*nc - xs;
  196. m[ 2] = zx*nc - ys;
  197. m[ 6] = yz*nc + xs;
  198. m[10] = z*z*nc + c;
  199. }
  200. void Matrix4x4::loadScale(float x, float y, float z) {
  201. loadIdentity();
  202. set(0, 0, x);
  203. set(1, 1, y);
  204. set(2, 2, z);
  205. }
  206. void Matrix4x4::loadTranslate(float x, float y, float z) {
  207. loadIdentity();
  208. m[12] = x;
  209. m[13] = y;
  210. m[14] = z;
  211. }
  212. void Matrix4x4::loadMultiply(const rs_matrix4x4 *lhs, const rs_matrix4x4 *rhs) {
  213. // Use a temporary variable to support the case where one of the inputs
  214. // is also the destination, e.g. left.loadMultiply(left, right);
  215. Matrix4x4 temp;
  216. for (int i=0 ; i<4 ; i++) {
  217. float ri0 = 0;
  218. float ri1 = 0;
  219. float ri2 = 0;
  220. float ri3 = 0;
  221. for (int j=0 ; j<4 ; j++) {
  222. const float rhs_ij = ((const Matrix4x4 *)rhs)->get(i,j);
  223. ri0 += ((const Matrix4x4 *)lhs)->get(j,0) * rhs_ij;
  224. ri1 += ((const Matrix4x4 *)lhs)->get(j,1) * rhs_ij;
  225. ri2 += ((const Matrix4x4 *)lhs)->get(j,2) * rhs_ij;
  226. ri3 += ((const Matrix4x4 *)lhs)->get(j,3) * rhs_ij;
  227. }
  228. temp.set(i,0, ri0);
  229. temp.set(i,1, ri1);
  230. temp.set(i,2, ri2);
  231. temp.set(i,3, ri3);
  232. }
  233. load(&temp);
  234. }
  235. void Matrix4x4::loadOrtho(float left, float right, float bottom, float top, float near, float far) {
  236. loadIdentity();
  237. m[0] = 2.f / (right - left);
  238. m[5] = 2.f / (top - bottom);
  239. m[10]= -2.f / (far - near);
  240. m[12]= -(right + left) / (right - left);
  241. m[13]= -(top + bottom) / (top - bottom);
  242. m[14]= -(far + near) / (far - near);
  243. }
  244. void Matrix4x4::loadFrustum(float left, float right, float bottom, float top, float near, float far) {
  245. loadIdentity();
  246. m[0] = 2.f * near / (right - left);
  247. m[5] = 2.f * near / (top - bottom);
  248. m[8] = (right + left) / (right - left);
  249. m[9] = (top + bottom) / (top - bottom);
  250. m[10]= -(far + near) / (far - near);
  251. m[11]= -1.f;
  252. m[14]= -2.f * far * near / (far - near);
  253. m[15]= 0.f;
  254. }
  255. void Matrix4x4::loadPerspective(float fovy, float aspect, float near, float far) {
  256. float top = near * tan((float) (fovy * M_PI / 360.0f));
  257. float bottom = -top;
  258. float left = bottom * aspect;
  259. float right = top * aspect;
  260. loadFrustum(left, right, bottom, top, near, far);
  261. }
  262. // Note: This assumes that the input vector (in) is of length 3.
  263. void Matrix4x4::vectorMultiply(float *out, const float *in) const {
  264. out[0] = (m[0] * in[0]) + (m[4] * in[1]) + (m[8] * in[2]) + m[12];
  265. out[1] = (m[1] * in[0]) + (m[5] * in[1]) + (m[9] * in[2]) + m[13];
  266. out[2] = (m[2] * in[0]) + (m[6] * in[1]) + (m[10] * in[2]) + m[14];
  267. out[3] = (m[3] * in[0]) + (m[7] * in[1]) + (m[11] * in[2]) + m[15];
  268. }
  269. void Matrix4x4::logv(const char *s) const {
  270. ALOGV("%s {%f, %f, %f, %f", s, m[0], m[4], m[8], m[12]);
  271. ALOGV("%s %f, %f, %f, %f", s, m[1], m[5], m[9], m[13]);
  272. ALOGV("%s %f, %f, %f, %f", s, m[2], m[6], m[10], m[14]);
  273. ALOGV("%s %f, %f, %f, %f}", s, m[3], m[7], m[11], m[15]);
  274. }
  275. } // namespace renderscript
  276. } // namespace android