michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkMatrix44.h" michael@0: michael@0: static inline bool eq4(const SkMScalar* SK_RESTRICT a, michael@0: const SkMScalar* SK_RESTRICT b) { michael@0: return (a[0] == b[0]) & (a[1] == b[1]) & (a[2] == b[2]) & (a[3] == b[3]); michael@0: } michael@0: michael@0: bool SkMatrix44::operator==(const SkMatrix44& other) const { michael@0: if (this == &other) { michael@0: return true; michael@0: } michael@0: michael@0: if (this->isTriviallyIdentity() && other.isTriviallyIdentity()) { michael@0: return true; michael@0: } michael@0: michael@0: const SkMScalar* SK_RESTRICT a = &fMat[0][0]; michael@0: const SkMScalar* SK_RESTRICT b = &other.fMat[0][0]; michael@0: michael@0: #if 0 michael@0: for (int i = 0; i < 16; ++i) { michael@0: if (a[i] != b[i]) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: #else michael@0: // to reduce branch instructions, we compare 4 at a time. michael@0: // see bench/Matrix44Bench.cpp for test. michael@0: if (!eq4(&a[0], &b[0])) { michael@0: return false; michael@0: } michael@0: if (!eq4(&a[4], &b[4])) { michael@0: return false; michael@0: } michael@0: if (!eq4(&a[8], &b[8])) { michael@0: return false; michael@0: } michael@0: return eq4(&a[12], &b[12]); michael@0: #endif michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: int SkMatrix44::computeTypeMask() const { michael@0: unsigned mask = 0; michael@0: michael@0: if (0 != perspX() || 0 != perspY() || 0 != perspZ() || 1 != fMat[3][3]) { michael@0: return kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask; michael@0: } michael@0: michael@0: if (0 != transX() || 0 != transY() || 0 != transZ()) { michael@0: mask |= kTranslate_Mask; michael@0: } michael@0: michael@0: if (1 != scaleX() || 1 != scaleY() || 1 != scaleZ()) { michael@0: mask |= kScale_Mask; michael@0: } michael@0: michael@0: if (0 != fMat[1][0] || 0 != fMat[0][1] || 0 != fMat[0][2] || michael@0: 0 != fMat[2][0] || 0 != fMat[1][2] || 0 != fMat[2][1]) { michael@0: mask |= kAffine_Mask; michael@0: } michael@0: michael@0: return mask; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkMatrix44::asColMajorf(float dst[]) const { michael@0: const SkMScalar* src = &fMat[0][0]; michael@0: #ifdef SK_MSCALAR_IS_DOUBLE michael@0: for (int i = 0; i < 16; ++i) { michael@0: dst[i] = SkMScalarToFloat(src[i]); michael@0: } michael@0: #elif defined SK_MSCALAR_IS_FLOAT michael@0: memcpy(dst, src, 16 * sizeof(float)); michael@0: #endif michael@0: } michael@0: michael@0: void SkMatrix44::asColMajord(double dst[]) const { michael@0: const SkMScalar* src = &fMat[0][0]; michael@0: #ifdef SK_MSCALAR_IS_DOUBLE michael@0: memcpy(dst, src, 16 * sizeof(double)); michael@0: #elif defined SK_MSCALAR_IS_FLOAT michael@0: for (int i = 0; i < 16; ++i) { michael@0: dst[i] = SkMScalarToDouble(src[i]); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: void SkMatrix44::asRowMajorf(float dst[]) const { michael@0: const SkMScalar* src = &fMat[0][0]; michael@0: for (int i = 0; i < 4; ++i) { michael@0: dst[0] = SkMScalarToFloat(src[0]); michael@0: dst[4] = SkMScalarToFloat(src[1]); michael@0: dst[8] = SkMScalarToFloat(src[2]); michael@0: dst[12] = SkMScalarToFloat(src[3]); michael@0: src += 4; michael@0: dst += 1; michael@0: } michael@0: } michael@0: michael@0: void SkMatrix44::asRowMajord(double dst[]) const { michael@0: const SkMScalar* src = &fMat[0][0]; michael@0: for (int i = 0; i < 4; ++i) { michael@0: dst[0] = SkMScalarToDouble(src[0]); michael@0: dst[4] = SkMScalarToDouble(src[1]); michael@0: dst[8] = SkMScalarToDouble(src[2]); michael@0: dst[12] = SkMScalarToDouble(src[3]); michael@0: src += 4; michael@0: dst += 1; michael@0: } michael@0: } michael@0: michael@0: void SkMatrix44::setColMajorf(const float src[]) { michael@0: SkMScalar* dst = &fMat[0][0]; michael@0: #ifdef SK_MSCALAR_IS_DOUBLE michael@0: for (int i = 0; i < 16; ++i) { michael@0: dst[i] = SkMScalarToFloat(src[i]); michael@0: } michael@0: #elif defined SK_MSCALAR_IS_FLOAT michael@0: memcpy(dst, src, 16 * sizeof(float)); michael@0: #endif michael@0: michael@0: this->dirtyTypeMask(); michael@0: } michael@0: michael@0: void SkMatrix44::setColMajord(const double src[]) { michael@0: SkMScalar* dst = &fMat[0][0]; michael@0: #ifdef SK_MSCALAR_IS_DOUBLE michael@0: memcpy(dst, src, 16 * sizeof(double)); michael@0: #elif defined SK_MSCALAR_IS_FLOAT michael@0: for (int i = 0; i < 16; ++i) { michael@0: dst[i] = SkDoubleToMScalar(src[i]); michael@0: } michael@0: #endif michael@0: michael@0: this->dirtyTypeMask(); michael@0: } michael@0: michael@0: void SkMatrix44::setRowMajorf(const float src[]) { michael@0: SkMScalar* dst = &fMat[0][0]; michael@0: for (int i = 0; i < 4; ++i) { michael@0: dst[0] = SkMScalarToFloat(src[0]); michael@0: dst[4] = SkMScalarToFloat(src[1]); michael@0: dst[8] = SkMScalarToFloat(src[2]); michael@0: dst[12] = SkMScalarToFloat(src[3]); michael@0: src += 4; michael@0: dst += 1; michael@0: } michael@0: this->dirtyTypeMask(); michael@0: } michael@0: michael@0: void SkMatrix44::setRowMajord(const double src[]) { michael@0: SkMScalar* dst = &fMat[0][0]; michael@0: for (int i = 0; i < 4; ++i) { michael@0: dst[0] = SkDoubleToMScalar(src[0]); michael@0: dst[4] = SkDoubleToMScalar(src[1]); michael@0: dst[8] = SkDoubleToMScalar(src[2]); michael@0: dst[12] = SkDoubleToMScalar(src[3]); michael@0: src += 4; michael@0: dst += 1; michael@0: } michael@0: this->dirtyTypeMask(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: const SkMatrix44& SkMatrix44::I() { michael@0: static const SkMatrix44 gIdentity44(kIdentity_Constructor); michael@0: return gIdentity44; michael@0: } michael@0: michael@0: void SkMatrix44::setIdentity() { michael@0: fMat[0][0] = 1; michael@0: fMat[0][1] = 0; michael@0: fMat[0][2] = 0; michael@0: fMat[0][3] = 0; michael@0: fMat[1][0] = 0; michael@0: fMat[1][1] = 1; michael@0: fMat[1][2] = 0; michael@0: fMat[1][3] = 0; michael@0: fMat[2][0] = 0; michael@0: fMat[2][1] = 0; michael@0: fMat[2][2] = 1; michael@0: fMat[2][3] = 0; michael@0: fMat[3][0] = 0; michael@0: fMat[3][1] = 0; michael@0: fMat[3][2] = 0; michael@0: fMat[3][3] = 1; michael@0: this->setTypeMask(kIdentity_Mask); michael@0: } michael@0: michael@0: void SkMatrix44::set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02, michael@0: SkMScalar m10, SkMScalar m11, SkMScalar m12, michael@0: SkMScalar m20, SkMScalar m21, SkMScalar m22) { michael@0: fMat[0][0] = m00; fMat[0][1] = m01; fMat[0][2] = m02; fMat[0][3] = 0; michael@0: fMat[1][0] = m10; fMat[1][1] = m11; fMat[1][2] = m12; fMat[1][3] = 0; michael@0: fMat[2][0] = m20; fMat[2][1] = m21; fMat[2][2] = m22; fMat[2][3] = 0; michael@0: fMat[3][0] = 0; fMat[3][1] = 0; fMat[3][2] = 0; fMat[3][3] = 1; michael@0: this->dirtyTypeMask(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkMatrix44::setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz) { michael@0: this->setIdentity(); michael@0: michael@0: if (!dx && !dy && !dz) { michael@0: return; michael@0: } michael@0: michael@0: fMat[3][0] = dx; michael@0: fMat[3][1] = dy; michael@0: fMat[3][2] = dz; michael@0: this->setTypeMask(kTranslate_Mask); michael@0: } michael@0: michael@0: void SkMatrix44::preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz) { michael@0: if (!dx && !dy && !dz) { michael@0: return; michael@0: } michael@0: michael@0: for (int i = 0; i < 4; ++i) { michael@0: fMat[3][i] = fMat[0][i] * dx + fMat[1][i] * dy + fMat[2][i] * dz + fMat[3][i]; michael@0: } michael@0: this->dirtyTypeMask(); michael@0: } michael@0: michael@0: void SkMatrix44::postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz) { michael@0: if (!dx && !dy && !dz) { michael@0: return; michael@0: } michael@0: michael@0: if (this->getType() & kPerspective_Mask) { michael@0: for (int i = 0; i < 4; ++i) { michael@0: fMat[i][0] += fMat[i][3] * dx; michael@0: fMat[i][1] += fMat[i][3] * dy; michael@0: fMat[i][2] += fMat[i][3] * dz; michael@0: } michael@0: } else { michael@0: fMat[3][0] += dx; michael@0: fMat[3][1] += dy; michael@0: fMat[3][2] += dz; michael@0: this->dirtyTypeMask(); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkMatrix44::setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz) { michael@0: this->setIdentity(); michael@0: michael@0: if (1 == sx && 1 == sy && 1 == sz) { michael@0: return; michael@0: } michael@0: michael@0: fMat[0][0] = sx; michael@0: fMat[1][1] = sy; michael@0: fMat[2][2] = sz; michael@0: this->setTypeMask(kScale_Mask); michael@0: } michael@0: michael@0: void SkMatrix44::preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz) { michael@0: if (1 == sx && 1 == sy && 1 == sz) { michael@0: return; michael@0: } michael@0: michael@0: // The implementation matrix * pureScale can be shortcut michael@0: // by knowing that pureScale components effectively scale michael@0: // the columns of the original matrix. michael@0: for (int i = 0; i < 4; i++) { michael@0: fMat[0][i] *= sx; michael@0: fMat[1][i] *= sy; michael@0: fMat[2][i] *= sz; michael@0: } michael@0: this->dirtyTypeMask(); michael@0: } michael@0: michael@0: void SkMatrix44::postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz) { michael@0: if (1 == sx && 1 == sy && 1 == sz) { michael@0: return; michael@0: } michael@0: michael@0: for (int i = 0; i < 4; i++) { michael@0: fMat[i][0] *= sx; michael@0: fMat[i][1] *= sy; michael@0: fMat[i][2] *= sz; michael@0: } michael@0: this->dirtyTypeMask(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkMatrix44::setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z, michael@0: SkMScalar radians) { michael@0: double len2 = (double)x * x + (double)y * y + (double)z * z; michael@0: if (1 != len2) { michael@0: if (0 == len2) { michael@0: this->setIdentity(); michael@0: return; michael@0: } michael@0: double scale = 1 / sqrt(len2); michael@0: x = SkDoubleToMScalar(x * scale); michael@0: y = SkDoubleToMScalar(y * scale); michael@0: z = SkDoubleToMScalar(z * scale); michael@0: } michael@0: this->setRotateAboutUnit(x, y, z, radians); michael@0: } michael@0: michael@0: void SkMatrix44::setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z, michael@0: SkMScalar radians) { michael@0: double c = cos(radians); michael@0: double s = sin(radians); michael@0: double C = 1 - c; michael@0: double xs = x * s; michael@0: double ys = y * s; michael@0: double zs = z * s; michael@0: double xC = x * C; michael@0: double yC = y * C; michael@0: double zC = z * C; michael@0: double xyC = x * yC; michael@0: double yzC = y * zC; michael@0: double zxC = z * xC; michael@0: michael@0: // if you're looking at wikipedia, remember that we're column major. michael@0: this->set3x3(SkDoubleToMScalar(x * xC + c), // scale x michael@0: SkDoubleToMScalar(xyC + zs), // skew x michael@0: SkDoubleToMScalar(zxC - ys), // trans x michael@0: michael@0: SkDoubleToMScalar(xyC - zs), // skew y michael@0: SkDoubleToMScalar(y * yC + c), // scale y michael@0: SkDoubleToMScalar(yzC + xs), // trans y michael@0: michael@0: SkDoubleToMScalar(zxC + ys), // persp x michael@0: SkDoubleToMScalar(yzC - xs), // persp y michael@0: SkDoubleToMScalar(z * zC + c)); // persp 2 michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static bool bits_isonly(int value, int mask) { michael@0: return 0 == (value & ~mask); michael@0: } michael@0: michael@0: void SkMatrix44::setConcat(const SkMatrix44& a, const SkMatrix44& b) { michael@0: const SkMatrix44::TypeMask a_mask = a.getType(); michael@0: const SkMatrix44::TypeMask b_mask = b.getType(); michael@0: michael@0: if (kIdentity_Mask == a_mask) { michael@0: *this = b; michael@0: return; michael@0: } michael@0: if (kIdentity_Mask == b_mask) { michael@0: *this = a; michael@0: return; michael@0: } michael@0: michael@0: bool useStorage = (this == &a || this == &b); michael@0: SkMScalar storage[16]; michael@0: SkMScalar* result = useStorage ? storage : &fMat[0][0]; michael@0: michael@0: // Both matrices are at most scale+translate michael@0: if (bits_isonly(a_mask | b_mask, kScale_Mask | kTranslate_Mask)) { michael@0: result[0] = a.fMat[0][0] * b.fMat[0][0]; michael@0: result[1] = result[2] = result[3] = result[4] = 0; michael@0: result[5] = a.fMat[1][1] * b.fMat[1][1]; michael@0: result[6] = result[7] = result[8] = result[9] = 0; michael@0: result[10] = a.fMat[2][2] * b.fMat[2][2]; michael@0: result[11] = 0; michael@0: result[12] = a.fMat[0][0] * b.fMat[3][0] + a.fMat[3][0]; michael@0: result[13] = a.fMat[1][1] * b.fMat[3][1] + a.fMat[3][1]; michael@0: result[14] = a.fMat[2][2] * b.fMat[3][2] + a.fMat[3][2]; michael@0: result[15] = 1; michael@0: } else { michael@0: for (int j = 0; j < 4; j++) { michael@0: for (int i = 0; i < 4; i++) { michael@0: double value = 0; michael@0: for (int k = 0; k < 4; k++) { michael@0: value += SkMScalarToDouble(a.fMat[k][i]) * b.fMat[j][k]; michael@0: } michael@0: *result++ = SkDoubleToMScalar(value); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (useStorage) { michael@0: memcpy(fMat, storage, sizeof(storage)); michael@0: } michael@0: this->dirtyTypeMask(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** We always perform the calculation in doubles, to avoid prematurely losing michael@0: precision along the way. This relies on the compiler automatically michael@0: promoting our SkMScalar values to double (if needed). michael@0: */ michael@0: double SkMatrix44::determinant() const { michael@0: if (this->isIdentity()) { michael@0: return 1; michael@0: } michael@0: if (this->isScaleTranslate()) { michael@0: return fMat[0][0] * fMat[1][1] * fMat[2][2] * fMat[3][3]; michael@0: } michael@0: michael@0: double a00 = fMat[0][0]; michael@0: double a01 = fMat[0][1]; michael@0: double a02 = fMat[0][2]; michael@0: double a03 = fMat[0][3]; michael@0: double a10 = fMat[1][0]; michael@0: double a11 = fMat[1][1]; michael@0: double a12 = fMat[1][2]; michael@0: double a13 = fMat[1][3]; michael@0: double a20 = fMat[2][0]; michael@0: double a21 = fMat[2][1]; michael@0: double a22 = fMat[2][2]; michael@0: double a23 = fMat[2][3]; michael@0: double a30 = fMat[3][0]; michael@0: double a31 = fMat[3][1]; michael@0: double a32 = fMat[3][2]; michael@0: double a33 = fMat[3][3]; michael@0: michael@0: double b00 = a00 * a11 - a01 * a10; michael@0: double b01 = a00 * a12 - a02 * a10; michael@0: double b02 = a00 * a13 - a03 * a10; michael@0: double b03 = a01 * a12 - a02 * a11; michael@0: double b04 = a01 * a13 - a03 * a11; michael@0: double b05 = a02 * a13 - a03 * a12; michael@0: double b06 = a20 * a31 - a21 * a30; michael@0: double b07 = a20 * a32 - a22 * a30; michael@0: double b08 = a20 * a33 - a23 * a30; michael@0: double b09 = a21 * a32 - a22 * a31; michael@0: double b10 = a21 * a33 - a23 * a31; michael@0: double b11 = a22 * a33 - a23 * a32; michael@0: michael@0: // Calculate the determinant michael@0: return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkMatrix44::invert(SkMatrix44* inverse) const { michael@0: if (this->isIdentity()) { michael@0: if (inverse) { michael@0: inverse->setIdentity(); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: if (this->isTranslate()) { michael@0: if (inverse) { michael@0: inverse->setTranslate(-fMat[3][0], -fMat[3][1], -fMat[3][2]); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: if (this->isScaleTranslate()) { michael@0: if (0 == fMat[0][0] * fMat[1][1] * fMat[2][2]) { michael@0: return false; michael@0: } michael@0: michael@0: if (inverse) { michael@0: double invXScale = 1 / fMat[0][0]; michael@0: double invYScale = 1 / fMat[1][1]; michael@0: double invZScale = 1 / fMat[2][2]; michael@0: michael@0: inverse->fMat[0][0] = invXScale; michael@0: inverse->fMat[0][1] = 0; michael@0: inverse->fMat[0][2] = 0; michael@0: inverse->fMat[0][3] = 0; michael@0: michael@0: inverse->fMat[1][0] = 0; michael@0: inverse->fMat[1][1] = invYScale; michael@0: inverse->fMat[1][2] = 0; michael@0: inverse->fMat[1][3] = 0; michael@0: michael@0: inverse->fMat[2][0] = 0; michael@0: inverse->fMat[2][1] = 0; michael@0: inverse->fMat[2][2] = invZScale; michael@0: inverse->fMat[2][3] = 0; michael@0: michael@0: inverse->fMat[3][0] = -fMat[3][0] * invXScale; michael@0: inverse->fMat[3][1] = -fMat[3][1] * invYScale; michael@0: inverse->fMat[3][2] = -fMat[3][2] * invZScale; michael@0: inverse->fMat[3][3] = 1; michael@0: michael@0: inverse->setTypeMask(this->getType()); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: double a00 = fMat[0][0]; michael@0: double a01 = fMat[0][1]; michael@0: double a02 = fMat[0][2]; michael@0: double a03 = fMat[0][3]; michael@0: double a10 = fMat[1][0]; michael@0: double a11 = fMat[1][1]; michael@0: double a12 = fMat[1][2]; michael@0: double a13 = fMat[1][3]; michael@0: double a20 = fMat[2][0]; michael@0: double a21 = fMat[2][1]; michael@0: double a22 = fMat[2][2]; michael@0: double a23 = fMat[2][3]; michael@0: double a30 = fMat[3][0]; michael@0: double a31 = fMat[3][1]; michael@0: double a32 = fMat[3][2]; michael@0: double a33 = fMat[3][3]; michael@0: michael@0: if (!(this->getType() & kPerspective_Mask)) { michael@0: // If we know the matrix has no perspective, then the perspective michael@0: // component is (0, 0, 0, 1). We can use this information to save a lot michael@0: // of arithmetic that would otherwise be spent to compute the inverse michael@0: // of a general matrix. michael@0: michael@0: SkASSERT(a03 == 0); michael@0: SkASSERT(a13 == 0); michael@0: SkASSERT(a23 == 0); michael@0: SkASSERT(a33 == 1); michael@0: michael@0: double b00 = a00 * a11 - a01 * a10; michael@0: double b01 = a00 * a12 - a02 * a10; michael@0: double b03 = a01 * a12 - a02 * a11; michael@0: double b06 = a20 * a31 - a21 * a30; michael@0: double b07 = a20 * a32 - a22 * a30; michael@0: double b08 = a20; michael@0: double b09 = a21 * a32 - a22 * a31; michael@0: double b10 = a21; michael@0: double b11 = a22; michael@0: michael@0: // Calculate the determinant michael@0: double det = b00 * b11 - b01 * b10 + b03 * b08; michael@0: michael@0: double invdet = 1.0 / det; michael@0: // If det is zero, we want to return false. However, we also want to return false michael@0: // if 1/det overflows to infinity (i.e. det is denormalized). Both of these are michael@0: // handled by checking that 1/det is finite. michael@0: if (!sk_float_isfinite(invdet)) { michael@0: return false; michael@0: } michael@0: if (NULL == inverse) { michael@0: return true; michael@0: } michael@0: michael@0: b00 *= invdet; michael@0: b01 *= invdet; michael@0: b03 *= invdet; michael@0: b06 *= invdet; michael@0: b07 *= invdet; michael@0: b08 *= invdet; michael@0: b09 *= invdet; michael@0: b10 *= invdet; michael@0: b11 *= invdet; michael@0: michael@0: inverse->fMat[0][0] = SkDoubleToMScalar(a11 * b11 - a12 * b10); michael@0: inverse->fMat[0][1] = SkDoubleToMScalar(a02 * b10 - a01 * b11); michael@0: inverse->fMat[0][2] = SkDoubleToMScalar(b03); michael@0: inverse->fMat[0][3] = 0; michael@0: inverse->fMat[1][0] = SkDoubleToMScalar(a12 * b08 - a10 * b11); michael@0: inverse->fMat[1][1] = SkDoubleToMScalar(a00 * b11 - a02 * b08); michael@0: inverse->fMat[1][2] = SkDoubleToMScalar(-b01); michael@0: inverse->fMat[1][3] = 0; michael@0: inverse->fMat[2][0] = SkDoubleToMScalar(a10 * b10 - a11 * b08); michael@0: inverse->fMat[2][1] = SkDoubleToMScalar(a01 * b08 - a00 * b10); michael@0: inverse->fMat[2][2] = SkDoubleToMScalar(b00); michael@0: inverse->fMat[2][3] = 0; michael@0: inverse->fMat[3][0] = SkDoubleToMScalar(a11 * b07 - a10 * b09 - a12 * b06); michael@0: inverse->fMat[3][1] = SkDoubleToMScalar(a00 * b09 - a01 * b07 + a02 * b06); michael@0: inverse->fMat[3][2] = SkDoubleToMScalar(a31 * b01 - a30 * b03 - a32 * b00); michael@0: inverse->fMat[3][3] = 1; michael@0: michael@0: inverse->setTypeMask(this->getType()); michael@0: return true; michael@0: } michael@0: michael@0: double b00 = a00 * a11 - a01 * a10; michael@0: double b01 = a00 * a12 - a02 * a10; michael@0: double b02 = a00 * a13 - a03 * a10; michael@0: double b03 = a01 * a12 - a02 * a11; michael@0: double b04 = a01 * a13 - a03 * a11; michael@0: double b05 = a02 * a13 - a03 * a12; michael@0: double b06 = a20 * a31 - a21 * a30; michael@0: double b07 = a20 * a32 - a22 * a30; michael@0: double b08 = a20 * a33 - a23 * a30; michael@0: double b09 = a21 * a32 - a22 * a31; michael@0: double b10 = a21 * a33 - a23 * a31; michael@0: double b11 = a22 * a33 - a23 * a32; michael@0: michael@0: // Calculate the determinant michael@0: double det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; michael@0: michael@0: double invdet = 1.0 / det; michael@0: // If det is zero, we want to return false. However, we also want to return false michael@0: // if 1/det overflows to infinity (i.e. det is denormalized). Both of these are michael@0: // handled by checking that 1/det is finite. michael@0: if (!sk_float_isfinite(invdet)) { michael@0: return false; michael@0: } michael@0: if (NULL == inverse) { michael@0: return true; michael@0: } michael@0: michael@0: b00 *= invdet; michael@0: b01 *= invdet; michael@0: b02 *= invdet; michael@0: b03 *= invdet; michael@0: b04 *= invdet; michael@0: b05 *= invdet; michael@0: b06 *= invdet; michael@0: b07 *= invdet; michael@0: b08 *= invdet; michael@0: b09 *= invdet; michael@0: b10 *= invdet; michael@0: b11 *= invdet; michael@0: michael@0: inverse->fMat[0][0] = SkDoubleToMScalar(a11 * b11 - a12 * b10 + a13 * b09); michael@0: inverse->fMat[0][1] = SkDoubleToMScalar(a02 * b10 - a01 * b11 - a03 * b09); michael@0: inverse->fMat[0][2] = SkDoubleToMScalar(a31 * b05 - a32 * b04 + a33 * b03); michael@0: inverse->fMat[0][3] = SkDoubleToMScalar(a22 * b04 - a21 * b05 - a23 * b03); michael@0: inverse->fMat[1][0] = SkDoubleToMScalar(a12 * b08 - a10 * b11 - a13 * b07); michael@0: inverse->fMat[1][1] = SkDoubleToMScalar(a00 * b11 - a02 * b08 + a03 * b07); michael@0: inverse->fMat[1][2] = SkDoubleToMScalar(a32 * b02 - a30 * b05 - a33 * b01); michael@0: inverse->fMat[1][3] = SkDoubleToMScalar(a20 * b05 - a22 * b02 + a23 * b01); michael@0: inverse->fMat[2][0] = SkDoubleToMScalar(a10 * b10 - a11 * b08 + a13 * b06); michael@0: inverse->fMat[2][1] = SkDoubleToMScalar(a01 * b08 - a00 * b10 - a03 * b06); michael@0: inverse->fMat[2][2] = SkDoubleToMScalar(a30 * b04 - a31 * b02 + a33 * b00); michael@0: inverse->fMat[2][3] = SkDoubleToMScalar(a21 * b02 - a20 * b04 - a23 * b00); michael@0: inverse->fMat[3][0] = SkDoubleToMScalar(a11 * b07 - a10 * b09 - a12 * b06); michael@0: inverse->fMat[3][1] = SkDoubleToMScalar(a00 * b09 - a01 * b07 + a02 * b06); michael@0: inverse->fMat[3][2] = SkDoubleToMScalar(a31 * b01 - a30 * b03 - a32 * b00); michael@0: inverse->fMat[3][3] = SkDoubleToMScalar(a20 * b03 - a21 * b01 + a22 * b00); michael@0: inverse->dirtyTypeMask(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkMatrix44::transpose() { michael@0: SkTSwap(fMat[0][1], fMat[1][0]); michael@0: SkTSwap(fMat[0][2], fMat[2][0]); michael@0: SkTSwap(fMat[0][3], fMat[3][0]); michael@0: SkTSwap(fMat[1][2], fMat[2][1]); michael@0: SkTSwap(fMat[1][3], fMat[3][1]); michael@0: SkTSwap(fMat[2][3], fMat[3][2]); michael@0: michael@0: if (!this->isTriviallyIdentity()) { michael@0: this->dirtyTypeMask(); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkMatrix44::mapScalars(const SkScalar src[4], SkScalar dst[4]) const { michael@0: SkScalar storage[4]; michael@0: SkScalar* result = (src == dst) ? storage : dst; michael@0: michael@0: for (int i = 0; i < 4; i++) { michael@0: SkMScalar value = 0; michael@0: for (int j = 0; j < 4; j++) { michael@0: value += fMat[j][i] * src[j]; michael@0: } michael@0: result[i] = SkMScalarToScalar(value); michael@0: } michael@0: michael@0: if (storage == result) { michael@0: memcpy(dst, storage, sizeof(storage)); michael@0: } michael@0: } michael@0: michael@0: #ifdef SK_MSCALAR_IS_DOUBLE michael@0: michael@0: void SkMatrix44::mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const { michael@0: SkMScalar storage[4]; michael@0: SkMScalar* result = (src == dst) ? storage : dst; michael@0: michael@0: for (int i = 0; i < 4; i++) { michael@0: SkMScalar value = 0; michael@0: for (int j = 0; j < 4; j++) { michael@0: value += fMat[j][i] * src[j]; michael@0: } michael@0: result[i] = value; michael@0: } michael@0: michael@0: if (storage == result) { michael@0: memcpy(dst, storage, sizeof(storage)); michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: michael@0: typedef void (*Map2Procf)(const SkMScalar mat[][4], const float src2[], int count, float dst4[]); michael@0: typedef void (*Map2Procd)(const SkMScalar mat[][4], const double src2[], int count, double dst4[]); michael@0: michael@0: static void map2_if(const SkMScalar mat[][4], const float* SK_RESTRICT src2, michael@0: int count, float* SK_RESTRICT dst4) { michael@0: for (int i = 0; i < count; ++i) { michael@0: dst4[0] = src2[0]; michael@0: dst4[1] = src2[1]; michael@0: dst4[2] = 0; michael@0: dst4[3] = 1; michael@0: src2 += 2; michael@0: dst4 += 4; michael@0: } michael@0: } michael@0: michael@0: static void map2_id(const SkMScalar mat[][4], const double* SK_RESTRICT src2, michael@0: int count, double* SK_RESTRICT dst4) { michael@0: for (int i = 0; i < count; ++i) { michael@0: dst4[0] = src2[0]; michael@0: dst4[1] = src2[1]; michael@0: dst4[2] = 0; michael@0: dst4[3] = 1; michael@0: src2 += 2; michael@0: dst4 += 4; michael@0: } michael@0: } michael@0: michael@0: static void map2_tf(const SkMScalar mat[][4], const float* SK_RESTRICT src2, michael@0: int count, float* SK_RESTRICT dst4) { michael@0: const float mat30 = SkMScalarToFloat(mat[3][0]); michael@0: const float mat31 = SkMScalarToFloat(mat[3][1]); michael@0: const float mat32 = SkMScalarToFloat(mat[3][2]); michael@0: for (int n = 0; n < count; ++n) { michael@0: dst4[0] = src2[0] + mat30; michael@0: dst4[1] = src2[1] + mat31; michael@0: dst4[2] = mat32; michael@0: dst4[3] = 1; michael@0: src2 += 2; michael@0: dst4 += 4; michael@0: } michael@0: } michael@0: michael@0: static void map2_td(const SkMScalar mat[][4], const double* SK_RESTRICT src2, michael@0: int count, double* SK_RESTRICT dst4) { michael@0: for (int n = 0; n < count; ++n) { michael@0: dst4[0] = src2[0] + mat[3][0]; michael@0: dst4[1] = src2[1] + mat[3][1]; michael@0: dst4[2] = mat[3][2]; michael@0: dst4[3] = 1; michael@0: src2 += 2; michael@0: dst4 += 4; michael@0: } michael@0: } michael@0: michael@0: static void map2_sf(const SkMScalar mat[][4], const float* SK_RESTRICT src2, michael@0: int count, float* SK_RESTRICT dst4) { michael@0: const float mat32 = SkMScalarToFloat(mat[3][2]); michael@0: for (int n = 0; n < count; ++n) { michael@0: dst4[0] = SkMScalarToFloat(mat[0][0] * src2[0] + mat[3][0]); michael@0: dst4[1] = SkMScalarToFloat(mat[1][1] * src2[1] + mat[3][1]); michael@0: dst4[2] = mat32; michael@0: dst4[3] = 1; michael@0: src2 += 2; michael@0: dst4 += 4; michael@0: } michael@0: } michael@0: michael@0: static void map2_sd(const SkMScalar mat[][4], const double* SK_RESTRICT src2, michael@0: int count, double* SK_RESTRICT dst4) { michael@0: for (int n = 0; n < count; ++n) { michael@0: dst4[0] = mat[0][0] * src2[0] + mat[3][0]; michael@0: dst4[1] = mat[1][1] * src2[1] + mat[3][1]; michael@0: dst4[2] = mat[3][2]; michael@0: dst4[3] = 1; michael@0: src2 += 2; michael@0: dst4 += 4; michael@0: } michael@0: } michael@0: michael@0: static void map2_af(const SkMScalar mat[][4], const float* SK_RESTRICT src2, michael@0: int count, float* SK_RESTRICT dst4) { michael@0: SkMScalar r; michael@0: for (int n = 0; n < count; ++n) { michael@0: SkMScalar sx = SkFloatToMScalar(src2[0]); michael@0: SkMScalar sy = SkFloatToMScalar(src2[1]); michael@0: r = mat[0][0] * sx + mat[1][0] * sy + mat[3][0]; michael@0: dst4[0] = SkMScalarToFloat(r); michael@0: r = mat[0][1] * sx + mat[1][1] * sy + mat[3][1]; michael@0: dst4[1] = SkMScalarToFloat(r); michael@0: r = mat[0][2] * sx + mat[1][2] * sy + mat[3][2]; michael@0: dst4[2] = SkMScalarToFloat(r); michael@0: dst4[3] = 1; michael@0: src2 += 2; michael@0: dst4 += 4; michael@0: } michael@0: } michael@0: michael@0: static void map2_ad(const SkMScalar mat[][4], const double* SK_RESTRICT src2, michael@0: int count, double* SK_RESTRICT dst4) { michael@0: for (int n = 0; n < count; ++n) { michael@0: double sx = src2[0]; michael@0: double sy = src2[1]; michael@0: dst4[0] = mat[0][0] * sx + mat[1][0] * sy + mat[3][0]; michael@0: dst4[1] = mat[0][1] * sx + mat[1][1] * sy + mat[3][1]; michael@0: dst4[2] = mat[0][2] * sx + mat[1][2] * sy + mat[3][2]; michael@0: dst4[3] = 1; michael@0: src2 += 2; michael@0: dst4 += 4; michael@0: } michael@0: } michael@0: michael@0: static void map2_pf(const SkMScalar mat[][4], const float* SK_RESTRICT src2, michael@0: int count, float* SK_RESTRICT dst4) { michael@0: SkMScalar r; michael@0: for (int n = 0; n < count; ++n) { michael@0: SkMScalar sx = SkFloatToMScalar(src2[0]); michael@0: SkMScalar sy = SkFloatToMScalar(src2[1]); michael@0: for (int i = 0; i < 4; i++) { michael@0: r = mat[0][i] * sx + mat[1][i] * sy + mat[3][i]; michael@0: dst4[i] = SkMScalarToFloat(r); michael@0: } michael@0: src2 += 2; michael@0: dst4 += 4; michael@0: } michael@0: } michael@0: michael@0: static void map2_pd(const SkMScalar mat[][4], const double* SK_RESTRICT src2, michael@0: int count, double* SK_RESTRICT dst4) { michael@0: for (int n = 0; n < count; ++n) { michael@0: double sx = src2[0]; michael@0: double sy = src2[1]; michael@0: for (int i = 0; i < 4; i++) { michael@0: dst4[i] = mat[0][i] * sx + mat[1][i] * sy + mat[3][i]; michael@0: } michael@0: src2 += 2; michael@0: dst4 += 4; michael@0: } michael@0: } michael@0: michael@0: void SkMatrix44::map2(const float src2[], int count, float dst4[]) const { michael@0: static const Map2Procf gProc[] = { michael@0: map2_if, map2_tf, map2_sf, map2_sf, map2_af, map2_af, map2_af, map2_af michael@0: }; michael@0: michael@0: TypeMask mask = this->getType(); michael@0: Map2Procf proc = (mask & kPerspective_Mask) ? map2_pf : gProc[mask]; michael@0: proc(fMat, src2, count, dst4); michael@0: } michael@0: michael@0: void SkMatrix44::map2(const double src2[], int count, double dst4[]) const { michael@0: static const Map2Procd gProc[] = { michael@0: map2_id, map2_td, map2_sd, map2_sd, map2_ad, map2_ad, map2_ad, map2_ad michael@0: }; michael@0: michael@0: TypeMask mask = this->getType(); michael@0: Map2Procd proc = (mask & kPerspective_Mask) ? map2_pd : gProc[mask]; michael@0: proc(fMat, src2, count, dst4); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkMatrix44::dump() const { michael@0: static const char* format = michael@0: "[%g %g %g %g][%g %g %g %g][%g %g %g %g][%g %g %g %g]\n"; michael@0: #if 0 michael@0: SkDebugf(format, michael@0: fMat[0][0], fMat[1][0], fMat[2][0], fMat[3][0], michael@0: fMat[0][1], fMat[1][1], fMat[2][1], fMat[3][1], michael@0: fMat[0][2], fMat[1][2], fMat[2][2], fMat[3][2], michael@0: fMat[0][3], fMat[1][3], fMat[2][3], fMat[3][3]); michael@0: #else michael@0: SkDebugf(format, michael@0: fMat[0][0], fMat[0][1], fMat[0][2], fMat[0][3], michael@0: fMat[1][0], fMat[1][1], fMat[1][2], fMat[1][3], michael@0: fMat[2][0], fMat[2][1], fMat[2][2], fMat[2][3], michael@0: fMat[3][0], fMat[3][1], fMat[3][2], fMat[3][3]); michael@0: #endif michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static void initFromMatrix(SkMScalar dst[4][4], const SkMatrix& src) { michael@0: dst[0][0] = SkScalarToMScalar(src[SkMatrix::kMScaleX]); michael@0: dst[1][0] = SkScalarToMScalar(src[SkMatrix::kMSkewX]); michael@0: dst[2][0] = 0; michael@0: dst[3][0] = SkScalarToMScalar(src[SkMatrix::kMTransX]); michael@0: dst[0][1] = SkScalarToMScalar(src[SkMatrix::kMSkewY]); michael@0: dst[1][1] = SkScalarToMScalar(src[SkMatrix::kMScaleY]); michael@0: dst[2][1] = 0; michael@0: dst[3][1] = SkScalarToMScalar(src[SkMatrix::kMTransY]); michael@0: dst[0][2] = 0; michael@0: dst[1][2] = 0; michael@0: dst[2][2] = 1; michael@0: dst[3][2] = 0; michael@0: dst[0][3] = SkScalarToMScalar(src[SkMatrix::kMPersp0]); michael@0: dst[1][3] = SkScalarToMScalar(src[SkMatrix::kMPersp1]); michael@0: dst[2][3] = 0; michael@0: dst[3][3] = SkScalarToMScalar(src[SkMatrix::kMPersp2]); michael@0: } michael@0: michael@0: SkMatrix44::SkMatrix44(const SkMatrix& src) { michael@0: initFromMatrix(fMat, src); michael@0: } michael@0: michael@0: SkMatrix44& SkMatrix44::operator=(const SkMatrix& src) { michael@0: initFromMatrix(fMat, src); michael@0: michael@0: if (src.isIdentity()) { michael@0: this->setTypeMask(kIdentity_Mask); michael@0: } else { michael@0: this->dirtyTypeMask(); michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: SkMatrix44::operator SkMatrix() const { michael@0: SkMatrix dst; michael@0: michael@0: dst[SkMatrix::kMScaleX] = SkMScalarToScalar(fMat[0][0]); michael@0: dst[SkMatrix::kMSkewX] = SkMScalarToScalar(fMat[1][0]); michael@0: dst[SkMatrix::kMTransX] = SkMScalarToScalar(fMat[3][0]); michael@0: michael@0: dst[SkMatrix::kMSkewY] = SkMScalarToScalar(fMat[0][1]); michael@0: dst[SkMatrix::kMScaleY] = SkMScalarToScalar(fMat[1][1]); michael@0: dst[SkMatrix::kMTransY] = SkMScalarToScalar(fMat[3][1]); michael@0: michael@0: dst[SkMatrix::kMPersp0] = SkMScalarToScalar(fMat[0][3]); michael@0: dst[SkMatrix::kMPersp1] = SkMScalarToScalar(fMat[1][3]); michael@0: dst[SkMatrix::kMPersp2] = SkMScalarToScalar(fMat[3][3]); michael@0: michael@0: return dst; michael@0: }