2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <cutils/compiler.h>
20 #include <utils/String8.h>
21 #include <ui/Region.h>
24 #include "Transform.h"
26 // ---------------------------------------------------------------------------
30 // ---------------------------------------------------------------------------
32 Transform::Transform() {
36 Transform::Transform(const Transform& other)
37 : mMatrix(other.mMatrix), mType(other.mType) {
40 Transform::Transform(uint32_t orientation) {
41 set(orientation, 0, 0);
44 Transform::~Transform() {
47 static const float EPSILON = 0.0f;
49 bool Transform::isZero(float f) {
50 return fabs(f) <= EPSILON;
53 bool Transform::absIsOne(float f) {
54 return isZero(fabs(f) - 1.0f);
57 Transform Transform::operator * (const Transform& rhs) const
59 if (CC_LIKELY(mType == IDENTITY))
63 if (rhs.mType == IDENTITY)
66 // TODO: we could use mType to optimize the matrix multiply
67 const mat33& A(mMatrix);
68 const mat33& B(rhs.mMatrix);
70 for (int i=0 ; i<3 ; i++) {
71 const float v0 = A[0][i];
72 const float v1 = A[1][i];
73 const float v2 = A[2][i];
74 D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
75 D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
76 D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
80 // TODO: we could recompute this value from r and rhs
82 r.mType |= UNKNOWN_TYPE;
86 const vec3& Transform::operator [] (size_t i) const {
90 bool Transform::transformed() const {
91 return type() > TRANSLATE;
94 float Transform::tx() const {
98 float Transform::ty() const {
102 void Transform::reset() {
104 for(int i=0 ; i<3 ; i++) {
106 for (int j=0 ; j<3 ; j++)
107 v[j] = ((i==j) ? 1.0f : 0.0f);
111 void Transform::set(float tx, float ty)
115 mMatrix[2][2] = 1.0f;
117 if (isZero(tx) && isZero(ty)) {
124 void Transform::set(float a, float b, float c, float d)
127 M[0][0] = a; M[1][0] = b;
128 M[0][1] = c; M[1][1] = d;
129 M[0][2] = 0; M[1][2] = 0;
130 mType = UNKNOWN_TYPE;
133 status_t Transform::set(uint32_t flags, float w, float h)
135 if (flags & ROT_INVALID) {
136 // that's not allowed!
142 if (flags & ROT_90) {
143 // w & h are inverted when rotating by 90 degrees
147 if (flags & FLIP_H) {
148 H.mType = (FLIP_H << 8) | SCALE;
149 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
155 if (flags & FLIP_V) {
156 V.mType = (FLIP_V << 8) | SCALE;
157 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
163 if (flags & ROT_90) {
164 const float original_w = h;
165 R.mType = (ROT_90 << 8) | ROTATE;
166 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
168 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
169 M[0][1] = 1; M[1][1] = 0;
176 vec2 Transform::transform(const vec2& v) const {
178 const mat33& M(mMatrix);
179 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
180 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
184 vec3 Transform::transform(const vec3& v) const {
186 const mat33& M(mMatrix);
187 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
188 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
189 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
193 vec2 Transform::transform(int x, int y) const
195 return transform(vec2(x,y));
198 Rect Transform::makeBounds(int w, int h) const
200 return transform( Rect(w, h) );
203 Rect Transform::transform(const Rect& bounds) const
206 vec2 lt( bounds.left, bounds.top );
207 vec2 rt( bounds.right, bounds.top );
208 vec2 lb( bounds.left, bounds.bottom );
209 vec2 rb( bounds.right, bounds.bottom );
216 r.left = floorf(min(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
217 r.top = floorf(min(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
218 r.right = floorf(max(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
219 r.bottom = floorf(max(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
224 Region Transform::transform(const Region& reg) const
227 if (CC_UNLIKELY(transformed())) {
228 if (CC_LIKELY(preserveRects())) {
229 Region::const_iterator it = reg.begin();
230 Region::const_iterator const end = reg.end();
232 out.orSelf(transform(*it++));
235 out.set(transform(reg.bounds()));
238 int xpos = floorf(tx() + 0.5f);
239 int ypos = floorf(ty() + 0.5f);
240 out = reg.translate(xpos, ypos);
245 uint32_t Transform::type() const
247 if (mType & UNKNOWN_TYPE) {
248 // recompute what this transform is
250 const mat33& M(mMatrix);
251 const float a = M[0][0];
252 const float b = M[1][0];
253 const float c = M[0][1];
254 const float d = M[1][1];
255 const float x = M[2][0];
256 const float y = M[2][1];
259 uint32_t flags = ROT_0;
260 if (isZero(b) && isZero(c)) {
261 if (a<0) flags |= FLIP_H;
262 if (d<0) flags |= FLIP_V;
263 if (!absIsOne(a) || !absIsOne(d)) {
266 } else if (isZero(a) && isZero(d)) {
268 if (b>0) flags |= FLIP_V;
269 if (c<0) flags |= FLIP_H;
270 if (!absIsOne(b) || !absIsOne(c)) {
274 // there is a skew component and/or a non 90 degrees rotation
279 if (flags & ROT_INVALID) {
282 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
292 if (!isZero(x) || !isZero(y))
298 Transform Transform::inverse() const {
299 // our 3x3 matrix is always of the form of a 2x2 transformation
300 // followed by a translation: T*M, therefore:
301 // (T*M)^-1 = M^-1 * T^-1
303 if (mType <= TRANSLATE) {
308 result.mMatrix[2][0] = -result.mMatrix[2][0];
309 result.mMatrix[2][1] = -result.mMatrix[2][1];
314 const mat33& M(mMatrix);
315 const float a = M[0][0];
316 const float b = M[1][0];
317 const float c = M[0][1];
318 const float d = M[1][1];
319 const float x = M[2][0];
320 const float y = M[2][1];
323 const float idet = 1.0 / (a*d - b*c);
324 R.mMatrix[0][0] = d*idet; R.mMatrix[0][1] = -c*idet;
325 R.mMatrix[1][0] = -b*idet; R.mMatrix[1][1] = a*idet;
326 R.mType = mType &= ~TRANSLATE;
328 T.mMatrix[2][0] = -x;
329 T.mMatrix[2][1] = -y;
336 uint32_t Transform::getType() const {
337 return type() & 0xFF;
340 uint32_t Transform::getOrientation() const
342 return (type() >> 8) & 0xFF;
345 bool Transform::preserveRects() const
347 return (getOrientation() & ROT_INVALID) ? false : true;
350 void Transform::dump(const char* name) const
352 type(); // updates the type
355 const mat33& m(mMatrix);
356 uint32_t orient = mType >> 8;
358 if (orient&ROT_INVALID) {
359 flags.append("ROT_INVALID ");
362 flags.append("ROT_90 ");
364 flags.append("ROT_0 ");
367 flags.append("FLIP_V ");
369 flags.append("FLIP_H ");
372 if (!(mType&(SCALE|ROTATE|TRANSLATE)))
373 type.append("IDENTITY ");
375 type.append("SCALE ");
377 type.append("ROTATE ");
379 type.append("TRANSLATE ");
381 ALOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
382 ALOGD("%.4f %.4f %.4f", m[0][0], m[1][0], m[2][0]);
383 ALOGD("%.4f %.4f %.4f", m[0][1], m[1][1], m[2][1]);
384 ALOGD("%.4f %.4f %.4f", m[0][2], m[1][2], m[2][2]);
387 // ---------------------------------------------------------------------------
389 }; // namespace android