Transform.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (C) 2007 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 <math.h>
  17. #include <cutils/compiler.h>
  18. #include <utils/String8.h>
  19. #include <ui/Region.h>
  20. #include "clz.h"
  21. #include "Transform.h"
  22. // ---------------------------------------------------------------------------
  23. namespace android {
  24. // ---------------------------------------------------------------------------
  25. Transform::Transform() {
  26. reset();
  27. }
  28. Transform::Transform(const Transform& other)
  29. : mMatrix(other.mMatrix), mType(other.mType) {
  30. }
  31. Transform::Transform(uint32_t orientation) {
  32. set(orientation, 0, 0);
  33. }
  34. Transform::~Transform() {
  35. }
  36. static const float EPSILON = 0.0f;
  37. bool Transform::isZero(float f) {
  38. return fabs(f) <= EPSILON;
  39. }
  40. bool Transform::absIsOne(float f) {
  41. return isZero(fabs(f) - 1.0f);
  42. }
  43. Transform Transform::operator * (const Transform& rhs) const
  44. {
  45. if (CC_LIKELY(mType == IDENTITY))
  46. return rhs;
  47. Transform r(*this);
  48. if (rhs.mType == IDENTITY)
  49. return r;
  50. // TODO: we could use mType to optimize the matrix multiply
  51. const mat33& A(mMatrix);
  52. const mat33& B(rhs.mMatrix);
  53. mat33& D(r.mMatrix);
  54. for (int i=0 ; i<3 ; i++) {
  55. const float v0 = A[0][i];
  56. const float v1 = A[1][i];
  57. const float v2 = A[2][i];
  58. D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
  59. D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
  60. D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
  61. }
  62. r.mType |= rhs.mType;
  63. // TODO: we could recompute this value from r and rhs
  64. r.mType &= 0xFF;
  65. r.mType |= UNKNOWN_TYPE;
  66. return r;
  67. }
  68. const vec3& Transform::operator [] (size_t i) const {
  69. return mMatrix[i];
  70. }
  71. bool Transform::transformed() const {
  72. return type() > TRANSLATE;
  73. }
  74. float Transform::tx() const {
  75. return mMatrix[2][0];
  76. }
  77. float Transform::ty() const {
  78. return mMatrix[2][1];
  79. }
  80. void Transform::reset() {
  81. mType = IDENTITY;
  82. for(int i=0 ; i<3 ; i++) {
  83. vec3& v(mMatrix[i]);
  84. for (int j=0 ; j<3 ; j++)
  85. v[j] = ((i==j) ? 1.0f : 0.0f);
  86. }
  87. }
  88. void Transform::set(float tx, float ty)
  89. {
  90. mMatrix[2][0] = tx;
  91. mMatrix[2][1] = ty;
  92. mMatrix[2][2] = 1.0f;
  93. if (isZero(tx) && isZero(ty)) {
  94. mType &= ~TRANSLATE;
  95. } else {
  96. mType |= TRANSLATE;
  97. }
  98. }
  99. void Transform::set(float a, float b, float c, float d)
  100. {
  101. mat33& M(mMatrix);
  102. M[0][0] = a; M[1][0] = b;
  103. M[0][1] = c; M[1][1] = d;
  104. M[0][2] = 0; M[1][2] = 0;
  105. mType = UNKNOWN_TYPE;
  106. }
  107. status_t Transform::set(uint32_t flags, float w, float h)
  108. {
  109. if (flags & ROT_INVALID) {
  110. // that's not allowed!
  111. reset();
  112. return BAD_VALUE;
  113. }
  114. Transform H, V, R;
  115. if (flags & ROT_90) {
  116. // w & h are inverted when rotating by 90 degrees
  117. swap(w, h);
  118. }
  119. if (flags & FLIP_H) {
  120. H.mType = (FLIP_H << 8) | SCALE;
  121. H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
  122. mat33& M(H.mMatrix);
  123. M[0][0] = -1;
  124. M[2][0] = w;
  125. }
  126. if (flags & FLIP_V) {
  127. V.mType = (FLIP_V << 8) | SCALE;
  128. V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
  129. mat33& M(V.mMatrix);
  130. M[1][1] = -1;
  131. M[2][1] = h;
  132. }
  133. if (flags & ROT_90) {
  134. const float original_w = h;
  135. R.mType = (ROT_90 << 8) | ROTATE;
  136. R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
  137. mat33& M(R.mMatrix);
  138. M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
  139. M[0][1] = 1; M[1][1] = 0;
  140. }
  141. *this = (R*(H*V));
  142. return NO_ERROR;
  143. }
  144. vec2 Transform::transform(const vec2& v) const {
  145. vec2 r;
  146. const mat33& M(mMatrix);
  147. r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
  148. r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
  149. return r;
  150. }
  151. vec3 Transform::transform(const vec3& v) const {
  152. vec3 r;
  153. const mat33& M(mMatrix);
  154. r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
  155. r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
  156. r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
  157. return r;
  158. }
  159. vec2 Transform::transform(int x, int y) const
  160. {
  161. return transform(vec2(x,y));
  162. }
  163. Rect Transform::makeBounds(int w, int h) const
  164. {
  165. return transform( Rect(w, h) );
  166. }
  167. Rect Transform::transform(const Rect& bounds) const
  168. {
  169. Rect r;
  170. vec2 lt( bounds.left, bounds.top );
  171. vec2 rt( bounds.right, bounds.top );
  172. vec2 lb( bounds.left, bounds.bottom );
  173. vec2 rb( bounds.right, bounds.bottom );
  174. lt = transform(lt);
  175. rt = transform(rt);
  176. lb = transform(lb);
  177. rb = transform(rb);
  178. r.left = floorf(min(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
  179. r.top = floorf(min(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
  180. r.right = floorf(max(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
  181. r.bottom = floorf(max(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
  182. return r;
  183. }
  184. Region Transform::transform(const Region& reg) const
  185. {
  186. Region out;
  187. if (CC_UNLIKELY(transformed())) {
  188. if (CC_LIKELY(preserveRects())) {
  189. Region::const_iterator it = reg.begin();
  190. Region::const_iterator const end = reg.end();
  191. while (it != end) {
  192. out.orSelf(transform(*it++));
  193. }
  194. } else {
  195. out.set(transform(reg.bounds()));
  196. }
  197. } else {
  198. int xpos = floorf(tx() + 0.5f);
  199. int ypos = floorf(ty() + 0.5f);
  200. out = reg.translate(xpos, ypos);
  201. }
  202. return out;
  203. }
  204. uint32_t Transform::type() const
  205. {
  206. if (mType & UNKNOWN_TYPE) {
  207. // recompute what this transform is
  208. const mat33& M(mMatrix);
  209. const float a = M[0][0];
  210. const float b = M[1][0];
  211. const float c = M[0][1];
  212. const float d = M[1][1];
  213. const float x = M[2][0];
  214. const float y = M[2][1];
  215. bool scale = false;
  216. uint32_t flags = ROT_0;
  217. if (isZero(b) && isZero(c)) {
  218. if (a<0) flags |= FLIP_H;
  219. if (d<0) flags |= FLIP_V;
  220. if (!absIsOne(a) || !absIsOne(d)) {
  221. scale = true;
  222. }
  223. } else if (isZero(a) && isZero(d)) {
  224. flags |= ROT_90;
  225. if (b>0) flags |= FLIP_V;
  226. if (c<0) flags |= FLIP_H;
  227. if (!absIsOne(b) || !absIsOne(c)) {
  228. scale = true;
  229. }
  230. } else {
  231. // there is a skew component and/or a non 90 degrees rotation
  232. flags = ROT_INVALID;
  233. }
  234. mType = flags << 8;
  235. if (flags & ROT_INVALID) {
  236. mType |= UNKNOWN;
  237. } else {
  238. if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
  239. mType |= ROTATE;
  240. if (flags & FLIP_H)
  241. mType ^= SCALE;
  242. if (flags & FLIP_V)
  243. mType ^= SCALE;
  244. if (scale)
  245. mType |= SCALE;
  246. }
  247. if (!isZero(x) || !isZero(y))
  248. mType |= TRANSLATE;
  249. }
  250. return mType;
  251. }
  252. Transform Transform::inverse() const {
  253. // our 3x3 matrix is always of the form of a 2x2 transformation
  254. // followed by a translation: T*M, therefore:
  255. // (T*M)^-1 = M^-1 * T^-1
  256. Transform result;
  257. if (mType <= TRANSLATE) {
  258. // 1 0 0
  259. // 0 1 0
  260. // x y 1
  261. result = *this;
  262. result.mMatrix[2][0] = -result.mMatrix[2][0];
  263. result.mMatrix[2][1] = -result.mMatrix[2][1];
  264. } else {
  265. // a c 0
  266. // b d 0
  267. // x y 1
  268. const mat33& M(mMatrix);
  269. const float a = M[0][0];
  270. const float b = M[1][0];
  271. const float c = M[0][1];
  272. const float d = M[1][1];
  273. const float x = M[2][0];
  274. const float y = M[2][1];
  275. const float idet = 1.0 / (a*d - b*c);
  276. result.mMatrix[0][0] = d*idet;
  277. result.mMatrix[0][1] = -c*idet;
  278. result.mMatrix[1][0] = -b*idet;
  279. result.mMatrix[1][1] = a*idet;
  280. result.mType = mType;
  281. vec2 T(-x, -y);
  282. T = result.transform(T);
  283. result.mMatrix[2][0] = T[0];
  284. result.mMatrix[2][1] = T[1];
  285. }
  286. return result;
  287. }
  288. uint32_t Transform::getType() const {
  289. return type() & 0xFF;
  290. }
  291. uint32_t Transform::getOrientation() const
  292. {
  293. return (type() >> 8) & 0xFF;
  294. }
  295. bool Transform::preserveRects() const
  296. {
  297. return (getOrientation() & ROT_INVALID) ? false : true;
  298. }
  299. void Transform::dump(const char* name) const
  300. {
  301. type(); // updates the type
  302. String8 flags, type;
  303. const mat33& m(mMatrix);
  304. uint32_t orient = mType >> 8;
  305. if (orient&ROT_INVALID) {
  306. flags.append("ROT_INVALID ");
  307. } else {
  308. if (orient&ROT_90) {
  309. flags.append("ROT_90 ");
  310. } else {
  311. flags.append("ROT_0 ");
  312. }
  313. if (orient&FLIP_V)
  314. flags.append("FLIP_V ");
  315. if (orient&FLIP_H)
  316. flags.append("FLIP_H ");
  317. }
  318. if (!(mType&(SCALE|ROTATE|TRANSLATE)))
  319. type.append("IDENTITY ");
  320. if (mType&SCALE)
  321. type.append("SCALE ");
  322. if (mType&ROTATE)
  323. type.append("ROTATE ");
  324. if (mType&TRANSLATE)
  325. type.append("TRANSLATE ");
  326. ALOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
  327. ALOGD("%.4f %.4f %.4f", m[0][0], m[1][0], m[2][0]);
  328. ALOGD("%.4f %.4f %.4f", m[0][1], m[1][1], m[2][1]);
  329. ALOGD("%.4f %.4f %.4f", m[0][2], m[1][2], m[2][2]);
  330. }
  331. // ---------------------------------------------------------------------------
  332. }; // namespace android