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>
23 #include "Transform.h"
25 // ---------------------------------------------------------------------------
29 // ---------------------------------------------------------------------------
32 static inline T min(T a, T b) {
36 static inline T min(T a, T b, T c) {
37 return min(a, min(b, c));
40 static inline T min(T a, T b, T c, T d) {
41 return min(a, b, min(c, d));
45 static inline T max(T a, T b) {
49 static inline T max(T a, T b, T c) {
50 return max(a, max(b, c));
53 static inline T max(T a, T b, T c, T d) {
54 return max(a, b, max(c, d));
59 void swap(T& a, T& b) {
65 // ---------------------------------------------------------------------------
67 Transform::Transform() {
71 Transform::Transform(const Transform& other)
72 : mMatrix(other.mMatrix), mType(other.mType) {
75 Transform::Transform(uint32_t orientation) {
76 set(orientation, 0, 0);
79 Transform::~Transform() {
82 static const float EPSILON = 0.0f;
84 bool Transform::isZero(float f) {
85 return fabs(f) <= EPSILON;
88 bool Transform::absIsOne(float f) {
89 return isZero(fabs(f) - 1.0f);
92 Transform Transform::operator * (const Transform& rhs) const
94 if (CC_LIKELY(mType == IDENTITY))
98 if (rhs.mType == IDENTITY)
101 // TODO: we could use mType to optimize the matrix multiply
102 const mat33& A(mMatrix);
103 const mat33& B(rhs.mMatrix);
105 for (int i=0 ; i<3 ; i++) {
106 const float v0 = A[0][i];
107 const float v1 = A[1][i];
108 const float v2 = A[2][i];
109 D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
110 D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
111 D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
113 r.mType |= rhs.mType;
115 // TODO: we could recompute this value from r and rhs
117 r.mType |= UNKNOWN_TYPE;
121 float const* Transform::operator [] (int i) const {
125 bool Transform::transformed() const {
126 return type() > TRANSLATE;
129 int Transform::tx() const {
130 return floorf(mMatrix[2][0] + 0.5f);
133 int Transform::ty() const {
134 return floorf(mMatrix[2][1] + 0.5f);
137 void Transform::reset() {
139 for(int i=0 ; i<3 ; i++) {
141 for (int j=0 ; j<3 ; j++)
142 v[j] = ((i==j) ? 1.0f : 0.0f);
146 void Transform::set(float tx, float ty)
150 mMatrix[2][2] = 1.0f;
152 if (isZero(tx) && isZero(ty)) {
159 void Transform::set(float a, float b, float c, float d)
162 M[0][0] = a; M[1][0] = b;
163 M[0][1] = c; M[1][1] = d;
164 M[0][2] = 0; M[1][2] = 0;
165 mType = UNKNOWN_TYPE;
168 status_t Transform::set(uint32_t flags, float w, float h)
170 if (flags & ROT_INVALID) {
171 // that's not allowed!
177 if (flags & ROT_90) {
178 // w & h are inverted when rotating by 90 degrees
182 if (flags & FLIP_H) {
183 H.mType = (FLIP_H << 8) | SCALE;
184 H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
190 if (flags & FLIP_V) {
191 V.mType = (FLIP_V << 8) | SCALE;
192 V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
198 if (flags & ROT_90) {
199 const float original_w = h;
200 R.mType = (ROT_90 << 8) | ROTATE;
201 R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
203 M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
204 M[0][1] = 1; M[1][1] = 0;
211 Transform::vec2 Transform::transform(const vec2& v) const {
213 const mat33& M(mMatrix);
214 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
215 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
219 Transform::vec3 Transform::transform(const vec3& v) const {
221 const mat33& M(mMatrix);
222 r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
223 r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
224 r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
228 void Transform::transform(float* point, int x, int y) const
230 const mat33& M(mMatrix);
237 Rect Transform::makeBounds(int w, int h) const
239 return transform( Rect(w, h) );
242 Rect Transform::transform(const Rect& bounds) const
245 vec2 lt( bounds.left, bounds.top );
246 vec2 rt( bounds.right, bounds.top );
247 vec2 lb( bounds.left, bounds.bottom );
248 vec2 rb( bounds.right, bounds.bottom );
255 r.left = floorf(min(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
256 r.top = floorf(min(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
257 r.right = floorf(max(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
258 r.bottom = floorf(max(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
263 Region Transform::transform(const Region& reg) const
266 if (CC_UNLIKELY(transformed())) {
267 if (CC_LIKELY(preserveRects())) {
268 Region::const_iterator it = reg.begin();
269 Region::const_iterator const end = reg.end();
271 out.orSelf(transform(*it++));
274 out.set(transform(reg.bounds()));
277 out = reg.translate(tx(), ty());
282 uint32_t Transform::type() const
284 if (mType & UNKNOWN_TYPE) {
285 // recompute what this transform is
287 const mat33& M(mMatrix);
288 const float a = M[0][0];
289 const float b = M[1][0];
290 const float c = M[0][1];
291 const float d = M[1][1];
292 const float x = M[2][0];
293 const float y = M[2][1];
296 uint32_t flags = ROT_0;
297 if (isZero(b) && isZero(c)) {
298 if (a<0) flags |= FLIP_H;
299 if (d<0) flags |= FLIP_V;
300 if (!absIsOne(a) || !absIsOne(d)) {
303 } else if (isZero(a) && isZero(d)) {
305 if (b>0) flags |= FLIP_V;
306 if (c<0) flags |= FLIP_H;
307 if (!absIsOne(b) || !absIsOne(c)) {
315 if (flags & ROT_INVALID) {
318 if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
328 if (!isZero(x) || !isZero(y))
334 uint32_t Transform::getType() const {
335 return type() & 0xFF;
338 uint32_t Transform::getOrientation() const
340 return (type() >> 8) & 0xFF;
343 bool Transform::preserveRects() const
345 return (type() & ROT_INVALID) ? false : true;
348 void Transform::dump(const char* name) const
350 type(); // updates the type
353 const mat33& m(mMatrix);
354 uint32_t orient = mType >> 8;
356 if (orient&ROT_INVALID) {
357 flags.append("ROT_INVALID ");
360 flags.append("ROT_90 ");
362 flags.append("ROT_0 ");
365 flags.append("FLIP_V ");
367 flags.append("FLIP_H ");
370 if (!(mType&(SCALE|ROTATE|TRANSLATE)))
371 type.append("IDENTITY ");
373 type.append("SCALE ");
375 type.append("ROTATE ");
377 type.append("TRANSLATE ");
379 LOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
380 LOGD("%.4f %.4f %.4f", m[0][0], m[1][0], m[2][0]);
381 LOGD("%.4f %.4f %.4f", m[0][1], m[1][1], m[2][1]);
382 LOGD("%.4f %.4f %.4f", m[0][2], m[1][2], m[2][2]);
385 // ---------------------------------------------------------------------------
387 }; // namespace android