blob: 003b8c831721f0362280de0e40bfccb662cb12ae [file] [log] [blame]
caryclark@google.comad65a3e2013-04-15 19:13:59 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "SkOpSegment.h"
8#include "Test.h"
9
10static const SkPoint cubics[][4] = {
reed@google.com277c3f82013-05-31 15:17:50 +000011/* 0 */ {{0, 1}, {2, 6}, {4, 2}, {5, 3}},
12/* 1 */ {{10, 234}, {10, 229.581726f}, {13.5817204f, 226}, {18, 226}},
13/* 2 */ {{132, 11419}, {130.89543151855469f, 11419}, {130, 11418.1044921875f}, {130, 11417}},
14/* 3 */ {{130.04275512695312f, 11417.4130859375f}, {130.23307800292969f, 11418.3193359375f},
15 {131.03709411621094f, 11419}, {132, 11419}},
16/* 4 */ {{0,1}, {0,5}, {4,1}, {6,4}},
17/* 5 */ {{1,5}, {4,6}, {1,0}, {4,0}},
18/* 6 */ {{0,1}, {0,4}, {5,1}, {6,4}},
19/* 7 */ {{0,1}, {1,2}, {1,0}, {6,1}},
20/* 8 */ {{0,3}, {0,1}, {2,0}, {1,0}},
21/* 9 */ {{189,7}, {189,5.3431458473205566f}, {190.3431396484375f,4}, {192,4}},
22/* 10 */ {{0,1}, {1,3}, {1,0}, {6,4}},
23/* 11 */ {{0,1}, {2,3}, {2,1}, {4,3}},
24/* 12 */ {{1,2}, {3,4}, {1,0}, {3,2}},
caryclark@google.coma5e55922013-05-07 18:51:31 +000025};
26
27static const SkPoint quads[][3] = {
reed@google.com277c3f82013-05-31 15:17:50 +000028/* 0 */ {{12.3423996f, 228.342407f}, {10, 230.686295f}, {10, 234}},
29/* 1 */ {{304.24319458007812f,591.75677490234375f}, {306,593.51470947265625f}, {306,596}},
caryclark@google.comad65a3e2013-04-15 19:13:59 +000030};
31
32static const SkPoint lines[][2] = {
reed@google.com277c3f82013-05-31 15:17:50 +000033/* 0 */ {{6, 2}, {2, 4}},
34/* 1 */ {{306,617}, {306,590}},
35/* 2 */ {{306,596}, {306,617}},
36/* 3 */ {{6,4}, {0,1}},
37/* 4 */ {{6,1}, {0,1}},
38/* 5 */ {{1,0}, {0,3}},
39/* 6 */ {{246,4}, {189,4}},
40/* 7 */ {{192,4}, {243,4}},
41/* 8 */ {{4,3}, {0,1}},
42/* 9 */ {{3,2}, {1,2}},
caryclark@google.comad65a3e2013-04-15 19:13:59 +000043};
44
45struct SortSet {
46 const SkPoint* ptData;
47 int ptCount;
48 double tStart;
49 double tEnd;
50};
51
52static const SortSet set1[] = {
caryclark@google.comb3f09212013-04-17 15:49:16 +000053 {cubics[0], 4, 0.66666987081928919, 0.875},
54 {lines[0], 2, 0.574070336, 0.388888889},
55 {cubics[0], 4, 0.66666987081928919, 0.4050371120499307 },
56 {lines[0], 2, 0.574070336, 0.9140625},
caryclark@google.comad65a3e2013-04-15 19:13:59 +000057};
58
59static const SortSet set2[] = {
caryclark@google.comb3f09212013-04-17 15:49:16 +000060 {cubics[0], 4, 0.666666667, 0.875},
caryclark@google.comad65a3e2013-04-15 19:13:59 +000061 {lines[0], 2, 0.574074074, 0.388888889},
62 {cubics[0], 4, 0.666666667, 0.405037112},
63 {lines[0], 2, 0.574074074, 0.9140625},
caryclark@google.comad65a3e2013-04-15 19:13:59 +000064};
65
caryclark@google.coma5e55922013-05-07 18:51:31 +000066static const SortSet set3[] = {
67 {cubics[1], 4, 0, 1},
68 {quads[0], 3, 1, 0},
69};
70
reed@google.com277c3f82013-05-31 15:17:50 +000071static const SortSet set4[] = {
72 {cubics[2], 4, 0.812114222, 1},
73 {cubics[3], 4, 0.0684734759, 0},
74};
75
76static const SortSet set5[] = {
77 {lines[1], 2, 0.777777778, 1},
78 {quads[1], 3, 1, 4.34137342e-06},
79 {lines[2], 2, 0, 1},
80};
81
82static const SortSet set6[] = {
83 {lines[3], 2, 0.407407407, 0.554627832},
84 {cubics[4], 4, 0.666666667, 0.548022446},
85 {lines[3], 2, 0.407407407, 0},
86 {cubics[4], 4, 0.666666667, 1},
87};
88
89static const SortSet set7[] = {
90 {cubics[5], 4, 0.545233342, 0.545454545},
91 {cubics[6], 4, 0.484938134, 0.484805744},
92 {cubics[5], 4, 0.545233342, 0},
93 {cubics[6], 4, 0.484938134, 0.545454545},
94};
95
96static const SortSet set8[] = {
97 {cubics[7], 4, 0.5, 0.522986744 },
98 {lines[4], 2, 0.75, 1},
99 {cubics[7], 4, 0.5, 0},
100 {lines[4], 2, 0.75, 0.737654321},
101};
102
103static const SortSet set9[] = {
104 {cubics[8], 4, 0.4, 1},
105 {lines[5], 2, 0.36, 0},
106 {cubics[8], 4, 0.4, 0.394675838},
107 {lines[5], 2, 0.36, 0.363999782},
108};
109
110static const SortSet set10[] = {
111 {lines[6], 2, 0.947368421, 1},
112 {cubics[9], 4, 1, 0.500000357},
113 {lines[7], 2, 0, 1},
114};
115
116static const SortSet set11[] = {
117 {lines[3], 2, 0.75, 1},
118 {cubics[10], 4, 0.5, 0.228744269},
119 {lines[3], 2, 0.75, 0.627112191},
120 {cubics[10], 4, 0.5, 0.6339746},
121};
122
123static const SortSet set12[] = {
124 {cubics[12], 4, 0.5, 1},
125 {lines[8], 2, 0.5, 1},
126 {cubics[11], 4, 0.5, 0},
127 {lines[9], 2, 0.5, 1},
128 {cubics[12], 4, 0.5, 0},
129 {lines[8], 2, 0.5, 0},
130 {cubics[11], 4, 0.5, 1},
131 {lines[9], 2, 0.5, 0},
132};
133
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000134struct SortSetTests {
135 const SortSet* set;
136 size_t count;
137};
138
139static const SortSetTests tests[] = {
reed@google.com277c3f82013-05-31 15:17:50 +0000140 { set12, SK_ARRAY_COUNT(set12) },
141 { set11, SK_ARRAY_COUNT(set11) },
142 { set10, SK_ARRAY_COUNT(set10) },
143 { set9, SK_ARRAY_COUNT(set9) },
144 { set8, SK_ARRAY_COUNT(set8) },
145 { set7, SK_ARRAY_COUNT(set7) },
146 { set6, SK_ARRAY_COUNT(set6) },
reed@google.comfa2f2a42013-05-30 15:29:48 +0000147 { set2, SK_ARRAY_COUNT(set2) },
reed@google.com277c3f82013-05-31 15:17:50 +0000148 { set5, SK_ARRAY_COUNT(set5) },
149 { set4, SK_ARRAY_COUNT(set4) },
150 { set3, SK_ARRAY_COUNT(set3) },
caryclark@google.coma5e55922013-05-07 18:51:31 +0000151 { set1, SK_ARRAY_COUNT(set1) },
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000152};
153
skia.committer@gmail.com7841c632013-04-16 07:01:17 +0000154static void setup(const SortSet* set, const size_t idx, SkPoint const ** data,
caryclark@google.comb3f09212013-04-17 15:49:16 +0000155 SkOpSegment* seg, int* ts) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000156 SkPoint start, end;
caryclark@google.coma5e55922013-05-07 18:51:31 +0000157 *data = set[idx].ptData;
158 switch(set[idx].ptCount) {
159 case 2: {
160 seg->addLine(*data, false, false);
161 SkDLine dLine;
162 dLine.set(set[idx].ptData);
163 start = dLine.xyAtT(set[idx].tStart).asSkPoint();
164 end = dLine.xyAtT(set[idx].tEnd).asSkPoint();
165 } break;
166 case 3: {
167 seg->addQuad(*data, false, false);
168 SkDQuad dQuad;
169 dQuad.set(set[idx].ptData);
170 start = dQuad.xyAtT(set[idx].tStart).asSkPoint();
171 end = dQuad.xyAtT(set[idx].tEnd).asSkPoint();
172 } break;
173 case 4: {
174 seg->addCubic(*data, false, false);
175 SkDCubic dCubic;
176 dCubic.set(set[idx].ptData);
177 start = dCubic.xyAtT(set[idx].tStart).asSkPoint();
178 end = dCubic.xyAtT(set[idx].tEnd).asSkPoint();
179 } break;
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000180 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000181 double tStart = set[idx].tStart;
182 double tEnd = set[idx].tEnd;
183 seg->addT(NULL, start, tStart);
184 seg->addT(NULL, end, tEnd);
185 double tLeft = tStart < tEnd ? 0 : 1;
186 if (tStart != tLeft && tEnd != tLeft) {
187 seg->addT(NULL, set[idx].ptData[0], tLeft);
188 }
189 double tRight = tStart < tEnd ? 1 : 0;
190 if (tStart != tRight && tEnd != tRight) {
191 seg->addT(NULL, set[idx].ptData[set[idx].ptCount - 1], tRight);
192 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000193 int tIndex = 0;
194 do {
195 if (seg->t(tIndex) == set[idx].tStart) {
196 ts[0] = tIndex;
197 }
198 if (seg->t(tIndex) == set[idx].tEnd) {
199 ts[1] = tIndex;
200 }
201 if (seg->t(tIndex) >= 1) {
202 break;
203 }
204 } while (++tIndex);
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000205}
206
207static void PathOpsAngleTest(skiatest::Reporter* reporter) {
208 for (size_t index = 0; index < SK_ARRAY_COUNT(tests); ++index) {
209 const SortSetTests& test = tests[index];
210 for (size_t idxL = 0; idxL < test.count - 1; ++idxL) {
211 SkOpSegment lesser, greater;
caryclark@google.comb3f09212013-04-17 15:49:16 +0000212 int lesserTs[2], greaterTs[2];
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000213 const SkPoint* lesserData, * greaterData;
214 const SortSet* set = test.set;
caryclark@google.comb3f09212013-04-17 15:49:16 +0000215 setup(set, idxL, &lesserData, &lesser, lesserTs);
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000216 size_t idxG = idxL + 1;
caryclark@google.comb3f09212013-04-17 15:49:16 +0000217 setup(set, idxG, &greaterData, &greater, greaterTs);
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000218 SkOpAngle first, second;
reed@google.com277c3f82013-05-31 15:17:50 +0000219 first.set(lesserData, SkPathOpsPointsToVerb(set[idxL].ptCount - 1), &lesser,
caryclark@google.comb3f09212013-04-17 15:49:16 +0000220 lesserTs[0], lesserTs[1], lesser.spans());
reed@google.com277c3f82013-05-31 15:17:50 +0000221 second.set(greaterData, SkPathOpsPointsToVerb(set[idxG].ptCount - 1), &greater,
caryclark@google.comb3f09212013-04-17 15:49:16 +0000222 greaterTs[0], greaterTs[1], greater.spans());
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000223 bool compare = first < second;
224 if (!compare) {
225 SkDebugf("%s test[%d]: lesser[%d] > greater[%d]\n", __FUNCTION__,
226 index, idxL, idxG);
reed@google.com277c3f82013-05-31 15:17:50 +0000227 compare = first < second;
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000228 }
229 REPORTER_ASSERT(reporter, compare);
reed@google.com277c3f82013-05-31 15:17:50 +0000230 reporter->bumpTestCount();
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000231 }
232 }
233}
234
235#include "TestClassDef.h"
236DEFINE_TESTCLASS_SHORT(PathOpsAngleTest)