OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
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 "SkMath.h" | 8 #include "SkMath.h" |
9 #include "SkMatrix.h" | 9 #include "SkMatrix.h" |
10 #include "SkMatrixUtils.h" | 10 #include "SkMatrixUtils.h" |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 size_t size3 = m2.readFromMemory(buffer, kBufferSize); | 112 size_t size3 = m2.readFromMemory(buffer, kBufferSize); |
113 REPORTER_ASSERT(reporter, size1 == size3); | 113 REPORTER_ASSERT(reporter, size1 == size3); |
114 REPORTER_ASSERT(reporter, are_equal(reporter, m, m2)); | 114 REPORTER_ASSERT(reporter, are_equal(reporter, m, m2)); |
115 | 115 |
116 char buffer2[kBufferSize]; | 116 char buffer2[kBufferSize]; |
117 size3 = m2.writeToMemory(buffer2); | 117 size3 = m2.writeToMemory(buffer2); |
118 REPORTER_ASSERT(reporter, size1 == size3); | 118 REPORTER_ASSERT(reporter, size1 == size3); |
119 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); | 119 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); |
120 } | 120 } |
121 | 121 |
122 static void test_matrix_min_max_stretch(skiatest::Reporter* reporter) { | 122 static void test_matrix_min_max_scale(skiatest::Reporter* reporter) { |
123 SkMatrix identity; | 123 SkMatrix identity; |
124 identity.reset(); | 124 identity.reset(); |
125 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMinStretch()); | 125 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMinScale()); |
126 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch()); | 126 REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxScale()); |
127 | 127 |
128 SkMatrix scale; | 128 SkMatrix scale; |
129 scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4); | 129 scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4); |
130 REPORTER_ASSERT(reporter, SK_Scalar1 * 2 == scale.getMinStretch()); | 130 REPORTER_ASSERT(reporter, SK_Scalar1 * 2 == scale.getMinScale()); |
131 REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxStretch()); | 131 REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxScale()); |
132 | 132 |
133 SkMatrix rot90Scale; | 133 SkMatrix rot90Scale; |
134 rot90Scale.setRotate(90 * SK_Scalar1); | 134 rot90Scale.setRotate(90 * SK_Scalar1); |
135 rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2); | 135 rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2); |
136 REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinStretch()); | 136 REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinScale()); |
137 REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxStretch()); | 137 REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxScale()); |
138 | 138 |
139 SkMatrix rotate; | 139 SkMatrix rotate; |
140 rotate.setRotate(128 * SK_Scalar1); | 140 rotate.setRotate(128 * SK_Scalar1); |
141 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMinStret
ch() ,SK_ScalarNearlyZero)); | 141 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMinScale
() ,SK_ScalarNearlyZero)); |
142 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMaxStret
ch(), SK_ScalarNearlyZero)); | 142 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SK_Scalar1, rotate.getMaxScale
(), SK_ScalarNearlyZero)); |
143 | 143 |
144 SkMatrix translate; | 144 SkMatrix translate; |
145 translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1); | 145 translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1); |
146 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMinStretch()); | 146 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMinScale()); |
147 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxStretch()); | 147 REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxScale()); |
148 | 148 |
149 SkMatrix perspX; | 149 SkMatrix perspX; |
150 perspX.reset(); | 150 perspX.reset(); |
151 perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000)); | 151 perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000)); |
152 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMinStretch()); | 152 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMinScale()); |
153 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxStretch()); | 153 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxScale()); |
154 | 154 |
155 SkMatrix perspY; | 155 SkMatrix perspY; |
156 perspY.reset(); | 156 perspY.reset(); |
157 perspY.setPerspY(SkScalarToPersp(-SK_Scalar1 / 500)); | 157 perspY.setPerspY(SkScalarToPersp(-SK_Scalar1 / 500)); |
158 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinStretch()); | 158 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMinScale()); |
159 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxStretch()); | 159 REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxScale()); |
160 | 160 |
161 SkMatrix baseMats[] = {scale, rot90Scale, rotate, | 161 SkMatrix baseMats[] = {scale, rot90Scale, rotate, |
162 translate, perspX, perspY}; | 162 translate, perspX, perspY}; |
163 SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)]; | 163 SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)]; |
164 for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) { | 164 for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) { |
165 mats[i] = baseMats[i]; | 165 mats[i] = baseMats[i]; |
166 bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]); | 166 bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]); |
167 REPORTER_ASSERT(reporter, invertable); | 167 REPORTER_ASSERT(reporter, invertable); |
168 } | 168 } |
169 SkRandom rand; | 169 SkRandom rand; |
170 for (int m = 0; m < 1000; ++m) { | 170 for (int m = 0; m < 1000; ++m) { |
171 SkMatrix mat; | 171 SkMatrix mat; |
172 mat.reset(); | 172 mat.reset(); |
173 for (int i = 0; i < 4; ++i) { | 173 for (int i = 0; i < 4; ++i) { |
174 int x = rand.nextU() % SK_ARRAY_COUNT(mats); | 174 int x = rand.nextU() % SK_ARRAY_COUNT(mats); |
175 mat.postConcat(mats[x]); | 175 mat.postConcat(mats[x]); |
176 } | 176 } |
177 | 177 |
178 SkScalar minStretch = mat.getMinStretch(); | 178 SkScalar minScale = mat.getMinScale(); |
179 SkScalar maxStretch = mat.getMaxStretch(); | 179 SkScalar maxScale = mat.getMaxScale(); |
180 REPORTER_ASSERT(reporter, (minStretch < 0) == (maxStretch < 0)); | 180 REPORTER_ASSERT(reporter, (minScale < 0) == (maxScale < 0)); |
181 REPORTER_ASSERT(reporter, (maxStretch < 0) == mat.hasPerspective()); | 181 REPORTER_ASSERT(reporter, (maxScale < 0) == mat.hasPerspective()); |
182 | 182 |
183 if (mat.hasPerspective()) { | 183 if (mat.hasPerspective()) { |
184 m -= 1; // try another non-persp matrix | 184 m -= 1; // try another non-persp matrix |
185 continue; | 185 continue; |
186 } | 186 } |
187 | 187 |
188 // test a bunch of vectors. All should be scaled by between minStretch a
nd maxStretch | 188 // test a bunch of vectors. All should be scaled by between minScale and
maxScale |
189 // (modulo some error) and we should find a vector that is scaled by alm
ost each. | 189 // (modulo some error) and we should find a vector that is scaled by alm
ost each. |
190 static const SkScalar gVectorStretchTol = (105 * SK_Scalar1) / 100; | 190 static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100; |
191 static const SkScalar gClosestStretchTol = (97 * SK_Scalar1) / 100; | 191 static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100; |
192 SkScalar max = 0, min = SK_ScalarMax; | 192 SkScalar max = 0, min = SK_ScalarMax; |
193 SkVector vectors[1000]; | 193 SkVector vectors[1000]; |
194 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { | 194 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
195 vectors[i].fX = rand.nextSScalar1(); | 195 vectors[i].fX = rand.nextSScalar1(); |
196 vectors[i].fY = rand.nextSScalar1(); | 196 vectors[i].fY = rand.nextSScalar1(); |
197 if (!vectors[i].normalize()) { | 197 if (!vectors[i].normalize()) { |
198 i -= 1; | 198 i -= 1; |
199 continue; | 199 continue; |
200 } | 200 } |
201 } | 201 } |
202 mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors)); | 202 mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors)); |
203 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { | 203 for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) { |
204 SkScalar d = vectors[i].length(); | 204 SkScalar d = vectors[i].length(); |
205 REPORTER_ASSERT(reporter, SkScalarDiv(d, maxStretch) < gVectorStretc
hTol); | 205 REPORTER_ASSERT(reporter, SkScalarDiv(d, maxScale) < gVectorScaleTol
); |
206 REPORTER_ASSERT(reporter, SkScalarDiv(minStretch, d) < gVectorStretc
hTol); | 206 REPORTER_ASSERT(reporter, SkScalarDiv(minScale, d) < gVectorScaleTol
); |
207 if (max < d) { | 207 if (max < d) { |
208 max = d; | 208 max = d; |
209 } | 209 } |
210 if (min > d) { | 210 if (min > d) { |
211 min = d; | 211 min = d; |
212 } | 212 } |
213 } | 213 } |
214 REPORTER_ASSERT(reporter, SkScalarDiv(max, maxStretch) >= gClosestStretc
hTol); | 214 REPORTER_ASSERT(reporter, SkScalarDiv(max, maxScale) >= gCloseScaleTol); |
215 REPORTER_ASSERT(reporter, SkScalarDiv(minStretch, min) >= gClosestStretc
hTol); | 215 REPORTER_ASSERT(reporter, SkScalarDiv(minScale, min) >= gCloseScaleTol); |
216 } | 216 } |
217 } | 217 } |
218 | 218 |
219 static void test_matrix_is_similarity(skiatest::Reporter* reporter) { | 219 static void test_matrix_is_similarity(skiatest::Reporter* reporter) { |
220 SkMatrix mat; | 220 SkMatrix mat; |
221 | 221 |
222 // identity | 222 // identity |
223 mat.setIdentity(); | 223 mat.setIdentity(); |
224 REPORTER_ASSERT(reporter, mat.isSimilarity()); | 224 REPORTER_ASSERT(reporter, mat.isSimilarity()); |
225 | 225 |
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
772 SkScalar zero = 0; | 772 SkScalar zero = 0; |
773 mat.set(SkMatrix::kMSkewX, -zero); | 773 mat.set(SkMatrix::kMSkewX, -zero); |
774 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); | 774 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
775 | 775 |
776 mat2.reset(); | 776 mat2.reset(); |
777 mat.reset(); | 777 mat.reset(); |
778 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN); | 778 mat.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
779 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN); | 779 mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN); |
780 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); | 780 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); |
781 | 781 |
782 test_matrix_min_max_stretch(reporter); | 782 test_matrix_min_max_scale(reporter); |
783 test_matrix_is_similarity(reporter); | 783 test_matrix_is_similarity(reporter); |
784 test_matrix_recttorect(reporter); | 784 test_matrix_recttorect(reporter); |
785 test_matrix_decomposition(reporter); | 785 test_matrix_decomposition(reporter); |
786 test_matrix_homogeneous(reporter); | 786 test_matrix_homogeneous(reporter); |
787 } | 787 } |
788 | 788 |
789 DEF_TEST(Matrix_Concat, r) { | 789 DEF_TEST(Matrix_Concat, r) { |
790 SkMatrix a; | 790 SkMatrix a; |
791 a.setTranslate(10, 20); | 791 a.setTranslate(10, 20); |
792 | 792 |
793 SkMatrix b; | 793 SkMatrix b; |
794 b.setScale(3, 5); | 794 b.setScale(3, 5); |
795 | 795 |
796 SkMatrix expected; | 796 SkMatrix expected; |
797 expected.setConcat(a,b); | 797 expected.setConcat(a,b); |
798 | 798 |
799 REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b)); | 799 REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b)); |
800 } | 800 } |
OLD | NEW |