Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: src/core/SkMatrix.cpp

Issue 20303003: Matrix decomposition cleanup: Add is_degenerate_2x2(), and fix some asserts (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Normalize some line endings Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkMatrix.h" 8 #include "SkMatrix.h"
9 #include "Sk64.h" 9 #include "Sk64.h"
10 #include "SkFloatBits.h" 10 #include "SkFloatBits.h"
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 163
164 return ma[0] == mb[0] && ma[1] == mb[1] && ma[2] == mb[2] && 164 return ma[0] == mb[0] && ma[1] == mb[1] && ma[2] == mb[2] &&
165 ma[3] == mb[3] && ma[4] == mb[4] && ma[5] == mb[5] && 165 ma[3] == mb[3] && ma[4] == mb[4] && ma[5] == mb[5] &&
166 ma[6] == mb[6] && ma[7] == mb[7] && ma[8] == mb[8]; 166 ma[6] == mb[6] && ma[7] == mb[7] && ma[8] == mb[8];
167 } 167 }
168 168
169 #endif 169 #endif
170 170
171 /////////////////////////////////////////////////////////////////////////////// 171 ///////////////////////////////////////////////////////////////////////////////
172 172
173 // helper function to determine if upper-left 2x2 of matrix is degenerate
174 static inline bool is_degenerate_2x2(SkScalar scaleX, SkScalar skewX,
175 SkScalar skewY, SkScalar scaleY) {
176 SkScalar perp_dot = scaleX*scaleY - skewX*skewY;
177 return SkScalarNearlyZero(perp_dot, SK_ScalarNearlyZero*SK_ScalarNearlyZero) ;
178 }
179
180 ///////////////////////////////////////////////////////////////////////////////
181
173 bool SkMatrix::isSimilarity(SkScalar tol) const { 182 bool SkMatrix::isSimilarity(SkScalar tol) const {
174 // if identity or translate matrix 183 // if identity or translate matrix
175 TypeMask mask = this->getType(); 184 TypeMask mask = this->getType();
176 if (mask <= kTranslate_Mask) { 185 if (mask <= kTranslate_Mask) {
177 return true; 186 return true;
178 } 187 }
179 if (mask & kPerspective_Mask) { 188 if (mask & kPerspective_Mask) {
180 return false; 189 return false;
181 } 190 }
182 191
183 SkScalar mx = fMat[kMScaleX]; 192 SkScalar mx = fMat[kMScaleX];
184 SkScalar my = fMat[kMScaleY]; 193 SkScalar my = fMat[kMScaleY];
185 // if no skew, can just compare scale factors 194 // if no skew, can just compare scale factors
186 if (!(mask & kAffine_Mask)) { 195 if (!(mask & kAffine_Mask)) {
187 return !SkScalarNearlyZero(mx) && SkScalarNearlyEqual(SkScalarAbs(mx), S kScalarAbs(my)); 196 return !SkScalarNearlyZero(mx) && SkScalarNearlyEqual(SkScalarAbs(mx), S kScalarAbs(my));
188 } 197 }
189 SkScalar sx = fMat[kMSkewX]; 198 SkScalar sx = fMat[kMSkewX];
190 SkScalar sy = fMat[kMSkewY]; 199 SkScalar sy = fMat[kMSkewY];
191 200
192 // TODO: I (rphillips) think there should be an || in here (see preservesRig htAngles) 201 if (is_degenerate_2x2(mx, sx, sy, my)) {
193 // degenerate matrix, non-similarity
194 if (SkScalarNearlyZero(mx) && SkScalarNearlyZero(my)
195 && SkScalarNearlyZero(sx) && SkScalarNearlyZero(sy)) {
196 return false; 202 return false;
197 } 203 }
198 204
199 // it has scales and skews, but it could also be rotation, check it out. 205 // it has scales and skews, but it could also be rotation, check it out.
200 SkVector vec[2]; 206 SkVector vec[2];
201 vec[0].set(mx, sx); 207 vec[0].set(mx, sx);
202 vec[1].set(sy, my); 208 vec[1].set(sy, my);
203 209
204 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) && 210 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
205 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(), 211 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
(...skipping 11 matching lines...) Expand all
217 return false; 223 return false;
218 } 224 }
219 225
220 SkASSERT(mask & kAffine_Mask); 226 SkASSERT(mask & kAffine_Mask);
221 227
222 SkScalar mx = fMat[kMScaleX]; 228 SkScalar mx = fMat[kMScaleX];
223 SkScalar my = fMat[kMScaleY]; 229 SkScalar my = fMat[kMScaleY];
224 SkScalar sx = fMat[kMSkewX]; 230 SkScalar sx = fMat[kMSkewX];
225 SkScalar sy = fMat[kMSkewY]; 231 SkScalar sy = fMat[kMSkewY];
226 232
227 if ((SkScalarNearlyZero(mx) && SkScalarNearlyZero(sx)) || 233 if (is_degenerate_2x2(mx, sx, sy, my)) {
228 (SkScalarNearlyZero(my) && SkScalarNearlyZero(sy))) {
229 // degenerate matrix
230 return false; 234 return false;
231 } 235 }
232 236
233 // it has scales and skews, but it could also be rotation, check it out. 237 // it has scales and skews, but it could also be rotation, check it out.
234 SkVector vec[2]; 238 SkVector vec[2];
235 vec[0].set(mx, sx); 239 vec[0].set(mx, sx);
236 vec[1].set(sy, my); 240 vec[1].set(sy, my);
237 241
238 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) && 242 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
239 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(), 243 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
(...skipping 1732 matching lines...) Expand 10 before | Expand all | Expand 10 after
1972 SkScalar* xScale, SkScalar* yScale, 1976 SkScalar* xScale, SkScalar* yScale,
1973 SkScalar* rotation1) { 1977 SkScalar* rotation1) {
1974 1978
1975 // borrowed from Jim Blinn's article "Consider the Lowly 2x2 Matrix" 1979 // borrowed from Jim Blinn's article "Consider the Lowly 2x2 Matrix"
1976 // Note: he uses row vectors, so we have to do some swapping of terms 1980 // Note: he uses row vectors, so we have to do some swapping of terms
1977 SkScalar A = matrix[SkMatrix::kMScaleX]; 1981 SkScalar A = matrix[SkMatrix::kMScaleX];
1978 SkScalar B = matrix[SkMatrix::kMSkewX]; 1982 SkScalar B = matrix[SkMatrix::kMSkewX];
1979 SkScalar C = matrix[SkMatrix::kMSkewY]; 1983 SkScalar C = matrix[SkMatrix::kMSkewY];
1980 SkScalar D = matrix[SkMatrix::kMScaleY]; 1984 SkScalar D = matrix[SkMatrix::kMScaleY];
1981 1985
1986 if (is_degenerate_2x2(A, B, C, D)) {
1987 return false;
1988 }
1989
1982 SkScalar E = SK_ScalarHalf*(A + D); 1990 SkScalar E = SK_ScalarHalf*(A + D);
1983 SkScalar F = SK_ScalarHalf*(A - D); 1991 SkScalar F = SK_ScalarHalf*(A - D);
1984 SkScalar G = SK_ScalarHalf*(C + B); 1992 SkScalar G = SK_ScalarHalf*(C + B);
1985 SkScalar H = SK_ScalarHalf*(C - B); 1993 SkScalar H = SK_ScalarHalf*(C - B);
1986 1994
1987 SkScalar sqrt0 = SkScalarSqrt(E*E + H*H); 1995 SkScalar sqrt0 = SkScalarSqrt(E*E + H*H);
1988 SkScalar sqrt1 = SkScalarSqrt(F*F + G*G); 1996 SkScalar sqrt1 = SkScalarSqrt(F*F + G*G);
1989 1997
1990 SkScalar xs, ys, r0, r1; 1998 SkScalar xs, ys, r0, r1;
1991 1999
1992 // can't have zero yScale, must be degenerate
1993 if (SkScalarNearlyEqual(sqrt0, sqrt1)) {
1994 return false;
1995 }
1996 xs = sqrt0 + sqrt1; 2000 xs = sqrt0 + sqrt1;
1997 ys = sqrt0 - sqrt1; 2001 ys = sqrt0 - sqrt1;
2002 // can't have zero yScale, must be degenerate
2003 SkASSERT(!SkScalarNearlyZero(ys));
1998 2004
1999 // uniformly scaled rotation 2005 // uniformly scaled rotation
2000 if (SkScalarNearlyZero(F) && SkScalarNearlyZero(G)) { 2006 if (SkScalarNearlyZero(F) && SkScalarNearlyZero(G)) {
2001 SkASSERT(!SkScalarNearlyZero(E)); 2007 SkASSERT(!SkScalarNearlyZero(E) || !SkScalarNearlyZero(H));
2002 r0 = SkScalarATan2(H, E); 2008 r0 = SkScalarATan2(H, E);
2003 r1 = 0; 2009 r1 = 0;
2004 // uniformly scaled reflection 2010 // uniformly scaled reflection
2005 } else if (SkScalarNearlyZero(E) && SkScalarNearlyZero(H)) { 2011 } else if (SkScalarNearlyZero(E) && SkScalarNearlyZero(H)) {
2006 SkASSERT(!SkScalarNearlyZero(F)); 2012 SkASSERT(!SkScalarNearlyZero(F) || !SkScalarNearlyZero(G));
2007 r0 = -SkScalarATan2(G, F); 2013 r0 = -SkScalarATan2(G, F);
2008 r1 = 0; 2014 r1 = 0;
2009 } else { 2015 } else {
2010 SkASSERT(!SkScalarNearlyZero(E)); 2016 SkASSERT(!SkScalarNearlyZero(E) || !SkScalarNearlyZero(H));
2011 SkASSERT(!SkScalarNearlyZero(F)); 2017 SkASSERT(!SkScalarNearlyZero(F) || !SkScalarNearlyZero(G));
2012 2018
2013 SkScalar arctan0 = SkScalarATan2(H, E); 2019 SkScalar arctan0 = SkScalarATan2(H, E);
2014 SkScalar arctan1 = SkScalarATan2(G, F); 2020 SkScalar arctan1 = SkScalarATan2(G, F);
2015 r0 = SK_ScalarHalf*(arctan0 - arctan1); 2021 r0 = SK_ScalarHalf*(arctan0 - arctan1);
2016 r1 = SK_ScalarHalf*(arctan0 + arctan1); 2022 r1 = SK_ScalarHalf*(arctan0 + arctan1);
2017 2023
2018 // simplify the results 2024 // simplify the results
2019 const SkScalar kHalfPI = SK_ScalarHalf*SK_ScalarPI; 2025 const SkScalar kHalfPI = SK_ScalarHalf*SK_ScalarPI;
2020 if (SkScalarNearlyEqual(SkScalarAbs(r0), kHalfPI)) { 2026 if (SkScalarNearlyEqual(SkScalarAbs(r0), kHalfPI)) {
2021 SkScalar tmp = xs; 2027 SkScalar tmp = xs;
(...skipping 20 matching lines...) Expand all
2042 } 2048 }
2043 if (NULL != rotation0) { 2049 if (NULL != rotation0) {
2044 *rotation0 = r0; 2050 *rotation0 = r0;
2045 } 2051 }
2046 if (NULL != rotation1) { 2052 if (NULL != rotation1) {
2047 *rotation1 = r1; 2053 *rotation1 = r1;
2048 } 2054 }
2049 2055
2050 return true; 2056 return true;
2051 } 2057 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698