2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
#include <math.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
#include <cutils/compiler.h>
|
|
|
|
#include <utils/String8.h>
|
|
|
|
#include <ui/Region.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2011-07-19 22:24:46 +00:00
|
|
|
#include "clz.h"
|
2009-03-04 03:31:44 +00:00
|
|
|
#include "Transform.h"
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
namespace android {
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
Transform::Transform() {
|
|
|
|
reset();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Transform::Transform(const Transform& other)
|
2010-02-22 11:15:57 +00:00
|
|
|
: mMatrix(other.mMatrix), mType(other.mType) {
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
Transform::Transform(uint32_t orientation) {
|
|
|
|
set(orientation, 0, 0);
|
2010-01-22 01:31:06 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
Transform::~Transform() {
|
|
|
|
}
|
|
|
|
|
2010-03-10 03:17:47 +00:00
|
|
|
static const float EPSILON = 0.0f;
|
2010-02-22 11:15:57 +00:00
|
|
|
|
|
|
|
bool Transform::isZero(float f) {
|
2010-03-10 03:17:47 +00:00
|
|
|
return fabs(f) <= EPSILON;
|
2010-02-22 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2010-03-10 03:17:47 +00:00
|
|
|
bool Transform::absIsOne(float f) {
|
|
|
|
return isZero(fabs(f) - 1.0f);
|
2010-02-22 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
Transform Transform::operator * (const Transform& rhs) const
|
|
|
|
{
|
2010-02-22 11:15:57 +00:00
|
|
|
if (CC_LIKELY(mType == IDENTITY))
|
2009-03-04 03:31:44 +00:00
|
|
|
return rhs;
|
|
|
|
|
|
|
|
Transform r(*this);
|
2010-02-22 11:15:57 +00:00
|
|
|
if (rhs.mType == IDENTITY)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
// TODO: we could use mType to optimize the matrix multiply
|
|
|
|
const mat33& A(mMatrix);
|
|
|
|
const mat33& B(rhs.mMatrix);
|
|
|
|
mat33& D(r.mMatrix);
|
|
|
|
for (int i=0 ; i<3 ; i++) {
|
|
|
|
const float v0 = A[0][i];
|
|
|
|
const float v1 = A[1][i];
|
|
|
|
const float v2 = A[2][i];
|
|
|
|
D[0][i] = v0*B[0][0] + v1*B[0][1] + v2*B[0][2];
|
|
|
|
D[1][i] = v0*B[1][0] + v1*B[1][1] + v2*B[1][2];
|
|
|
|
D[2][i] = v0*B[2][0] + v1*B[2][1] + v2*B[2][2];
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
r.mType |= rhs.mType;
|
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
// TODO: we could recompute this value from r and rhs
|
|
|
|
r.mType &= 0xFF;
|
|
|
|
r.mType |= UNKNOWN_TYPE;
|
2009-03-04 03:31:44 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-09-02 04:36:12 +00:00
|
|
|
const vec3& Transform::operator [] (size_t i) const {
|
|
|
|
return mMatrix[i];
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Transform::transformed() const {
|
2010-02-22 11:15:57 +00:00
|
|
|
return type() > TRANSLATE;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 01:51:54 +00:00
|
|
|
float Transform::tx() const {
|
|
|
|
return mMatrix[2][0];
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 01:51:54 +00:00
|
|
|
float Transform::ty() const {
|
|
|
|
return mMatrix[2][1];
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Transform::reset() {
|
2010-02-22 11:15:57 +00:00
|
|
|
mType = IDENTITY;
|
|
|
|
for(int i=0 ; i<3 ; i++) {
|
|
|
|
vec3& v(mMatrix[i]);
|
|
|
|
for (int j=0 ; j<3 ; j++)
|
|
|
|
v[j] = ((i==j) ? 1.0f : 0.0f);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
void Transform::set(float tx, float ty)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2010-02-22 11:15:57 +00:00
|
|
|
mMatrix[2][0] = tx;
|
|
|
|
mMatrix[2][1] = ty;
|
|
|
|
mMatrix[2][2] = 1.0f;
|
|
|
|
|
|
|
|
if (isZero(tx) && isZero(ty)) {
|
|
|
|
mType &= ~TRANSLATE;
|
|
|
|
} else {
|
|
|
|
mType |= TRANSLATE;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
void Transform::set(float a, float b, float c, float d)
|
2009-03-28 00:58:20 +00:00
|
|
|
{
|
2010-02-22 11:15:57 +00:00
|
|
|
mat33& M(mMatrix);
|
|
|
|
M[0][0] = a; M[1][0] = b;
|
|
|
|
M[0][1] = c; M[1][1] = d;
|
|
|
|
M[0][2] = 0; M[1][2] = 0;
|
|
|
|
mType = UNKNOWN_TYPE;
|
2009-03-28 00:58:20 +00:00
|
|
|
}
|
|
|
|
|
2010-03-10 03:17:47 +00:00
|
|
|
status_t Transform::set(uint32_t flags, float w, float h)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2010-03-10 03:17:47 +00:00
|
|
|
if (flags & ROT_INVALID) {
|
|
|
|
// that's not allowed!
|
|
|
|
reset();
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2010-10-24 21:53:05 +00:00
|
|
|
Transform H, V, R;
|
2010-10-26 01:29:35 +00:00
|
|
|
if (flags & ROT_90) {
|
|
|
|
// w & h are inverted when rotating by 90 degrees
|
|
|
|
swap(w, h);
|
|
|
|
}
|
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
if (flags & FLIP_H) {
|
2010-10-24 21:53:05 +00:00
|
|
|
H.mType = (FLIP_H << 8) | SCALE;
|
|
|
|
H.mType |= isZero(w) ? IDENTITY : TRANSLATE;
|
|
|
|
mat33& M(H.mMatrix);
|
|
|
|
M[0][0] = -1;
|
|
|
|
M[2][0] = w;
|
2010-02-22 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FLIP_V) {
|
2010-10-24 21:53:05 +00:00
|
|
|
V.mType = (FLIP_V << 8) | SCALE;
|
|
|
|
V.mType |= isZero(h) ? IDENTITY : TRANSLATE;
|
|
|
|
mat33& M(V.mMatrix);
|
|
|
|
M[1][1] = -1;
|
|
|
|
M[2][1] = h;
|
2010-02-22 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-24 21:53:05 +00:00
|
|
|
if (flags & ROT_90) {
|
2010-10-26 01:29:35 +00:00
|
|
|
const float original_w = h;
|
2010-10-24 21:53:05 +00:00
|
|
|
R.mType = (ROT_90 << 8) | ROTATE;
|
2010-10-26 01:29:35 +00:00
|
|
|
R.mType |= isZero(original_w) ? IDENTITY : TRANSLATE;
|
2010-10-24 21:53:05 +00:00
|
|
|
mat33& M(R.mMatrix);
|
2010-10-26 01:29:35 +00:00
|
|
|
M[0][0] = 0; M[1][0] =-1; M[2][0] = original_w;
|
2010-10-24 21:53:05 +00:00
|
|
|
M[0][1] = 1; M[1][1] = 0;
|
2010-02-22 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-26 01:29:35 +00:00
|
|
|
*this = (R*(H*V));
|
2010-03-10 03:17:47 +00:00
|
|
|
return NO_ERROR;
|
2010-02-22 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 04:36:12 +00:00
|
|
|
vec2 Transform::transform(const vec2& v) const {
|
2010-02-22 11:15:57 +00:00
|
|
|
vec2 r;
|
|
|
|
const mat33& M(mMatrix);
|
|
|
|
r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0];
|
|
|
|
r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1];
|
|
|
|
return r;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 04:36:12 +00:00
|
|
|
vec3 Transform::transform(const vec3& v) const {
|
2010-02-22 11:15:57 +00:00
|
|
|
vec3 r;
|
|
|
|
const mat33& M(mMatrix);
|
|
|
|
r[0] = M[0][0]*v[0] + M[1][0]*v[1] + M[2][0]*v[2];
|
|
|
|
r[1] = M[0][1]*v[0] + M[1][1]*v[1] + M[2][1]*v[2];
|
|
|
|
r[2] = M[0][2]*v[0] + M[1][2]*v[1] + M[2][2]*v[2];
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-09-02 04:36:12 +00:00
|
|
|
vec2 Transform::transform(int x, int y) const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2013-09-02 04:36:12 +00:00
|
|
|
return transform(vec2(x,y));
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Rect Transform::makeBounds(int w, int h) const
|
|
|
|
{
|
2010-02-22 11:15:57 +00:00
|
|
|
return transform( Rect(w, h) );
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Rect Transform::transform(const Rect& bounds) const
|
|
|
|
{
|
|
|
|
Rect r;
|
2010-02-22 11:15:57 +00:00
|
|
|
vec2 lt( bounds.left, bounds.top );
|
|
|
|
vec2 rt( bounds.right, bounds.top );
|
|
|
|
vec2 lb( bounds.left, bounds.bottom );
|
|
|
|
vec2 rb( bounds.right, bounds.bottom );
|
|
|
|
|
|
|
|
lt = transform(lt);
|
|
|
|
rt = transform(rt);
|
|
|
|
lb = transform(lb);
|
|
|
|
rb = transform(rb);
|
|
|
|
|
|
|
|
r.left = floorf(min(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
|
|
|
|
r.top = floorf(min(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
|
|
|
|
r.right = floorf(max(lt[0], rt[0], lb[0], rb[0]) + 0.5f);
|
|
|
|
r.bottom = floorf(max(lt[1], rt[1], lb[1], rb[1]) + 0.5f);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
Region Transform::transform(const Region& reg) const
|
|
|
|
{
|
|
|
|
Region out;
|
2010-02-22 11:15:57 +00:00
|
|
|
if (CC_UNLIKELY(transformed())) {
|
|
|
|
if (CC_LIKELY(preserveRects())) {
|
2009-05-11 07:03:41 +00:00
|
|
|
Region::const_iterator it = reg.begin();
|
|
|
|
Region::const_iterator const end = reg.end();
|
|
|
|
while (it != end) {
|
|
|
|
out.orSelf(transform(*it++));
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out.set(transform(reg.bounds()));
|
|
|
|
}
|
|
|
|
} else {
|
2011-08-31 01:51:54 +00:00
|
|
|
int xpos = floorf(tx() + 0.5f);
|
|
|
|
int ypos = floorf(ty() + 0.5f);
|
|
|
|
out = reg.translate(xpos, ypos);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
uint32_t Transform::type() const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2010-02-22 11:15:57 +00:00
|
|
|
if (mType & UNKNOWN_TYPE) {
|
|
|
|
// recompute what this transform is
|
|
|
|
|
|
|
|
const mat33& M(mMatrix);
|
|
|
|
const float a = M[0][0];
|
|
|
|
const float b = M[1][0];
|
|
|
|
const float c = M[0][1];
|
|
|
|
const float d = M[1][1];
|
|
|
|
const float x = M[2][0];
|
|
|
|
const float y = M[2][1];
|
|
|
|
|
|
|
|
bool scale = false;
|
|
|
|
uint32_t flags = ROT_0;
|
|
|
|
if (isZero(b) && isZero(c)) {
|
2010-03-10 03:17:47 +00:00
|
|
|
if (a<0) flags |= FLIP_H;
|
|
|
|
if (d<0) flags |= FLIP_V;
|
|
|
|
if (!absIsOne(a) || !absIsOne(d)) {
|
|
|
|
scale = true;
|
2010-02-22 11:15:57 +00:00
|
|
|
}
|
|
|
|
} else if (isZero(a) && isZero(d)) {
|
2010-03-10 03:17:47 +00:00
|
|
|
flags |= ROT_90;
|
2010-10-26 01:29:35 +00:00
|
|
|
if (b>0) flags |= FLIP_V;
|
|
|
|
if (c<0) flags |= FLIP_H;
|
2010-03-10 03:17:47 +00:00
|
|
|
if (!absIsOne(b) || !absIsOne(c)) {
|
|
|
|
scale = true;
|
2010-02-22 11:15:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-07-12 21:51:45 +00:00
|
|
|
// there is a skew component and/or a non 90 degrees rotation
|
2010-02-22 11:15:57 +00:00
|
|
|
flags = ROT_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
mType = flags << 8;
|
|
|
|
if (flags & ROT_INVALID) {
|
|
|
|
mType |= UNKNOWN;
|
2009-03-04 03:31:44 +00:00
|
|
|
} else {
|
2010-02-22 11:15:57 +00:00
|
|
|
if ((flags & ROT_90) || ((flags & ROT_180) == ROT_180))
|
|
|
|
mType |= ROTATE;
|
|
|
|
if (flags & FLIP_H)
|
|
|
|
mType ^= SCALE;
|
|
|
|
if (flags & FLIP_V)
|
|
|
|
mType ^= SCALE;
|
|
|
|
if (scale)
|
|
|
|
mType |= SCALE;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-02-22 11:15:57 +00:00
|
|
|
|
|
|
|
if (!isZero(x) || !isZero(y))
|
|
|
|
mType |= TRANSLATE;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-02-22 11:15:57 +00:00
|
|
|
return mType;
|
|
|
|
}
|
|
|
|
|
2013-03-01 01:12:07 +00:00
|
|
|
Transform Transform::inverse() const {
|
|
|
|
// our 3x3 matrix is always of the form of a 2x2 transformation
|
|
|
|
// followed by a translation: T*M, therefore:
|
|
|
|
// (T*M)^-1 = M^-1 * T^-1
|
|
|
|
Transform result;
|
|
|
|
if (mType <= TRANSLATE) {
|
2014-11-20 02:32:37 +00:00
|
|
|
// 1 0 0
|
|
|
|
// 0 1 0
|
|
|
|
// x y 1
|
2013-03-01 01:12:07 +00:00
|
|
|
result = *this;
|
|
|
|
result.mMatrix[2][0] = -result.mMatrix[2][0];
|
|
|
|
result.mMatrix[2][1] = -result.mMatrix[2][1];
|
|
|
|
} else {
|
2014-11-20 02:32:37 +00:00
|
|
|
// a c 0
|
|
|
|
// b d 0
|
|
|
|
// x y 1
|
2013-03-01 01:12:07 +00:00
|
|
|
const mat33& M(mMatrix);
|
|
|
|
const float a = M[0][0];
|
|
|
|
const float b = M[1][0];
|
|
|
|
const float c = M[0][1];
|
|
|
|
const float d = M[1][1];
|
|
|
|
const float x = M[2][0];
|
|
|
|
const float y = M[2][1];
|
|
|
|
|
|
|
|
const float idet = 1.0 / (a*d - b*c);
|
2014-11-20 02:32:37 +00:00
|
|
|
result.mMatrix[0][0] = d*idet;
|
|
|
|
result.mMatrix[0][1] = -c*idet;
|
|
|
|
result.mMatrix[1][0] = -b*idet;
|
|
|
|
result.mMatrix[1][1] = a*idet;
|
|
|
|
result.mType = mType;
|
|
|
|
|
|
|
|
vec2 T(-x, -y);
|
|
|
|
T = result.transform(T);
|
|
|
|
result.mMatrix[2][0] = T[0];
|
|
|
|
result.mMatrix[2][1] = T[1];
|
2013-03-01 01:12:07 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-02-22 11:15:57 +00:00
|
|
|
uint32_t Transform::getType() const {
|
|
|
|
return type() & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Transform::getOrientation() const
|
|
|
|
{
|
|
|
|
return (type() >> 8) & 0xFF;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Transform::preserveRects() const
|
|
|
|
{
|
2011-07-25 20:57:16 +00:00
|
|
|
return (getOrientation() & ROT_INVALID) ? false : true;
|
2010-02-22 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Transform::dump(const char* name) const
|
|
|
|
{
|
|
|
|
type(); // updates the type
|
|
|
|
|
|
|
|
String8 flags, type;
|
|
|
|
const mat33& m(mMatrix);
|
|
|
|
uint32_t orient = mType >> 8;
|
|
|
|
|
2010-03-10 03:17:47 +00:00
|
|
|
if (orient&ROT_INVALID) {
|
2010-02-22 11:15:57 +00:00
|
|
|
flags.append("ROT_INVALID ");
|
2010-03-10 03:17:47 +00:00
|
|
|
} else {
|
|
|
|
if (orient&ROT_90) {
|
|
|
|
flags.append("ROT_90 ");
|
|
|
|
} else {
|
|
|
|
flags.append("ROT_0 ");
|
|
|
|
}
|
|
|
|
if (orient&FLIP_V)
|
|
|
|
flags.append("FLIP_V ");
|
|
|
|
if (orient&FLIP_H)
|
|
|
|
flags.append("FLIP_H ");
|
|
|
|
}
|
2010-02-22 11:15:57 +00:00
|
|
|
|
2010-03-10 03:17:47 +00:00
|
|
|
if (!(mType&(SCALE|ROTATE|TRANSLATE)))
|
|
|
|
type.append("IDENTITY ");
|
2010-02-22 11:15:57 +00:00
|
|
|
if (mType&SCALE)
|
|
|
|
type.append("SCALE ");
|
|
|
|
if (mType&ROTATE)
|
|
|
|
type.append("ROTATE ");
|
|
|
|
if (mType&TRANSLATE)
|
|
|
|
type.append("TRANSLATE ");
|
|
|
|
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%s 0x%08x (%s, %s)", name, mType, flags.string(), type.string());
|
|
|
|
ALOGD("%.4f %.4f %.4f", m[0][0], m[1][0], m[2][0]);
|
|
|
|
ALOGD("%.4f %.4f %.4f", m[0][1], m[1][1], m[2][1]);
|
|
|
|
ALOGD("%.4f %.4f %.4f", m[0][2], m[1][2], m[2][2]);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
}; // namespace android
|