blob: 3702ead69491281a5942dad09d4c024fac6c4523 [file] [log] [blame]
caryclark@google.com9166dcb2013-04-08 11:50:00 +00001/*
2 * Copyright 2012 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 "PathOpsCubicIntersectionTestData.h"
8#include "PathOpsQuadIntersectionTestData.h"
caryclark@google.com8d0a5242013-07-16 16:11:16 +00009#include "PathOpsTestCommon.h"
caryclark@google.com9166dcb2013-04-08 11:50:00 +000010#include "SkIntersections.h"
11#include "SkPathOpsRect.h"
12#include "SkReduceOrder.h"
13#include "Test.h"
14
15static bool controls_inside(const SkDCubic& cubic) {
16 return between(cubic[0].fX, cubic[1].fX, cubic[3].fX)
17 && between(cubic[0].fX, cubic[2].fX, cubic[3].fX)
18 && between(cubic[0].fY, cubic[1].fY, cubic[3].fY)
19 && between(cubic[0].fY, cubic[2].fY, cubic[3].fY);
20}
21
22static bool tiny(const SkDCubic& cubic) {
23 int index, minX, maxX, minY, maxY;
24 minX = maxX = minY = maxY = 0;
25 for (index = 1; index < 4; ++index) {
26 if (cubic[minX].fX > cubic[index].fX) {
27 minX = index;
28 }
29 if (cubic[minY].fY > cubic[index].fY) {
30 minY = index;
31 }
32 if (cubic[maxX].fX < cubic[index].fX) {
33 maxX = index;
34 }
35 if (cubic[maxY].fY < cubic[index].fY) {
36 maxY = index;
37 }
38 }
39 return approximately_equal(cubic[maxX].fX, cubic[minX].fX)
40 && approximately_equal(cubic[maxY].fY, cubic[minY].fY);
41}
42
43static void find_tight_bounds(const SkDCubic& cubic, SkDRect& bounds) {
44 SkDCubicPair cubicPair = cubic.chopAt(0.5);
45 if (!tiny(cubicPair.first()) && !controls_inside(cubicPair.first())) {
46 find_tight_bounds(cubicPair.first(), bounds);
47 } else {
48 bounds.add(cubicPair.first()[0]);
49 bounds.add(cubicPair.first()[3]);
50 }
51 if (!tiny(cubicPair.second()) && !controls_inside(cubicPair.second())) {
52 find_tight_bounds(cubicPair.second(), bounds);
53 } else {
54 bounds.add(cubicPair.second()[0]);
55 bounds.add(cubicPair.second()[3]);
56 }
57}
58
caryclark@google.comad65a3e2013-04-15 19:13:59 +000059static void PathOpsReduceOrderCubicTest(skiatest::Reporter* reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000060 size_t index;
61 SkReduceOrder reducer;
62 int order;
63 enum {
64 RunAll,
65 RunPointDegenerates,
66 RunNotPointDegenerates,
67 RunLines,
68 RunNotLines,
69 RunModEpsilonLines,
70 RunLessEpsilonLines,
71 RunNegEpsilonLines,
72 RunQuadraticLines,
73 RunQuadraticPoints,
74 RunQuadraticModLines,
75 RunComputedLines,
76 RunNone
77 } run = RunAll;
78 int firstTestIndex = 0;
79#if 0
80 run = RunComputedLines;
81 firstTestIndex = 18;
82#endif
83 int firstPointDegeneratesTest = run == RunAll ? 0 : run == RunPointDegenerates
84 ? firstTestIndex : SK_MaxS32;
85 int firstNotPointDegeneratesTest = run == RunAll ? 0 : run == RunNotPointDegenerates
86 ? firstTestIndex : SK_MaxS32;
87 int firstLinesTest = run == RunAll ? 0 : run == RunLines ? firstTestIndex : SK_MaxS32;
88 int firstNotLinesTest = run == RunAll ? 0 : run == RunNotLines ? firstTestIndex : SK_MaxS32;
89 int firstModEpsilonTest = run == RunAll ? 0 : run == RunModEpsilonLines
90 ? firstTestIndex : SK_MaxS32;
91 int firstLessEpsilonTest = run == RunAll ? 0 : run == RunLessEpsilonLines
92 ? firstTestIndex : SK_MaxS32;
93 int firstNegEpsilonTest = run == RunAll ? 0 : run == RunNegEpsilonLines
94 ? firstTestIndex : SK_MaxS32;
95 int firstQuadraticPointTest = run == RunAll ? 0 : run == RunQuadraticPoints
96 ? firstTestIndex : SK_MaxS32;
97 int firstQuadraticLineTest = run == RunAll ? 0 : run == RunQuadraticLines
98 ? firstTestIndex : SK_MaxS32;
99 int firstQuadraticModLineTest = run == RunAll ? 0 : run == RunQuadraticModLines
100 ? firstTestIndex : SK_MaxS32;
caryclark@google.com927b7022013-11-25 14:18:21 +0000101#if 0
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000102 int firstComputedLinesTest = run == RunAll ? 0 : run == RunComputedLines
103 ? firstTestIndex : SK_MaxS32;
caryclark@google.com927b7022013-11-25 14:18:21 +0000104#endif
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000105 for (index = firstPointDegeneratesTest; index < pointDegenerates_count; ++index) {
106 const SkDCubic& cubic = pointDegenerates[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000107 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000108 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000109 if (order != 1) {
110 SkDebugf("[%d] pointDegenerates order=%d\n", static_cast<int>(index), order);
111 REPORTER_ASSERT(reporter, 0);
112 }
113 }
114 for (index = firstNotPointDegeneratesTest; index < notPointDegenerates_count; ++index) {
115 const SkDCubic& cubic = notPointDegenerates[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000116 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000117 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000118 if (order == 1) {
119 SkDebugf("[%d] notPointDegenerates order=%d\n", static_cast<int>(index), order);
caryclark@google.com927b7022013-11-25 14:18:21 +0000120 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000121 REPORTER_ASSERT(reporter, 0);
122 }
123 }
124 for (index = firstLinesTest; index < lines_count; ++index) {
125 const SkDCubic& cubic = lines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000126 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000127 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000128 if (order != 2) {
129 SkDebugf("[%d] lines order=%d\n", static_cast<int>(index), order);
130 REPORTER_ASSERT(reporter, 0);
131 }
132 }
133 for (index = firstNotLinesTest; index < notLines_count; ++index) {
134 const SkDCubic& cubic = notLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000135 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000136 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000137 if (order == 2) {
138 SkDebugf("[%d] notLines order=%d\n", static_cast<int>(index), order);
139 REPORTER_ASSERT(reporter, 0);
140 }
141 }
142 for (index = firstModEpsilonTest; index < modEpsilonLines_count; ++index) {
143 const SkDCubic& cubic = modEpsilonLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000144 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000145 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000146 if (order == 2) {
147 SkDebugf("[%d] line mod by epsilon order=%d\n", static_cast<int>(index), order);
148 REPORTER_ASSERT(reporter, 0);
149 }
150 }
151 for (index = firstLessEpsilonTest; index < lessEpsilonLines_count; ++index) {
152 const SkDCubic& cubic = lessEpsilonLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000153 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000154 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000155 if (order != 2) {
156 SkDebugf("[%d] line less by epsilon/2 order=%d\n", static_cast<int>(index), order);
caryclark@google.com927b7022013-11-25 14:18:21 +0000157 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000158 REPORTER_ASSERT(reporter, 0);
159 }
160 }
161 for (index = firstNegEpsilonTest; index < negEpsilonLines_count; ++index) {
162 const SkDCubic& cubic = negEpsilonLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000163 SkASSERT(ValidCubic(cubic));
caryclark@google.com927b7022013-11-25 14:18:21 +0000164 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000165 if (order != 2) {
166 SkDebugf("[%d] line neg by epsilon/2 order=%d\n", static_cast<int>(index), order);
167 REPORTER_ASSERT(reporter, 0);
168 }
169 }
170 for (index = firstQuadraticPointTest; index < quadraticPoints_count; ++index) {
171 const SkDQuad& quad = quadraticPoints[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000172 SkASSERT(ValidQuad(quad));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000173 SkDCubic cubic = quad.toCubic();
caryclark@google.com927b7022013-11-25 14:18:21 +0000174 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000175 if (order != 1) {
176 SkDebugf("[%d] point quad order=%d\n", static_cast<int>(index), order);
177 REPORTER_ASSERT(reporter, 0);
178 }
179 }
180 for (index = firstQuadraticLineTest; index < quadraticLines_count; ++index) {
181 const SkDQuad& quad = quadraticLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000182 SkASSERT(ValidQuad(quad));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000183 SkDCubic cubic = quad.toCubic();
caryclark@google.com927b7022013-11-25 14:18:21 +0000184 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000185 if (order != 2) {
186 SkDebugf("[%d] line quad order=%d\n", static_cast<int>(index), order);
187 REPORTER_ASSERT(reporter, 0);
188 }
189 }
190 for (index = firstQuadraticModLineTest; index < quadraticModEpsilonLines_count; ++index) {
191 const SkDQuad& quad = quadraticModEpsilonLines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000192 SkASSERT(ValidQuad(quad));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000193 SkDCubic cubic = quad.toCubic();
caryclark@google.com927b7022013-11-25 14:18:21 +0000194 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000195 if (order != 3) {
196 SkDebugf("[%d] line mod quad order=%d\n", static_cast<int>(index), order);
197 REPORTER_ASSERT(reporter, 0);
198 }
199 }
200
caryclark@google.com927b7022013-11-25 14:18:21 +0000201#if 0 // disable test until stroke reduction is supported
202// test if computed line end points are valid
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000203 for (index = firstComputedLinesTest; index < lines_count; ++index) {
204 const SkDCubic& cubic = lines[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000205 SkASSERT(ValidCubic(cubic));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000206 bool controlsInside = controls_inside(cubic);
207 order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics,
208 SkReduceOrder::kStroke_Style);
209 if (order == 2 && reducer.fLine[0] == reducer.fLine[1]) {
210 SkDebugf("[%d] line computed ends match order=%d\n", static_cast<int>(index), order);
211 REPORTER_ASSERT(reporter, 0);
212 }
213 if (controlsInside) {
214 if ( (reducer.fLine[0].fX != cubic[0].fX && reducer.fLine[0].fX != cubic[3].fX)
215 || (reducer.fLine[0].fY != cubic[0].fY && reducer.fLine[0].fY != cubic[3].fY)
216 || (reducer.fLine[1].fX != cubic[0].fX && reducer.fLine[1].fX != cubic[3].fX)
217 || (reducer.fLine[1].fY != cubic[0].fY && reducer.fLine[1].fY != cubic[3].fY)) {
218 SkDebugf("[%d] line computed ends order=%d\n", static_cast<int>(index), order);
219 REPORTER_ASSERT(reporter, 0);
220 }
221 } else {
222 // binary search for extrema, compare against actual results
223 // while a control point is outside of bounding box formed by end points, split
224 SkDRect bounds = {DBL_MAX, DBL_MAX, -DBL_MAX, -DBL_MAX};
225 find_tight_bounds(cubic, bounds);
226 if ( (!AlmostEqualUlps(reducer.fLine[0].fX, bounds.fLeft)
227 && !AlmostEqualUlps(reducer.fLine[0].fX, bounds.fRight))
228 || (!AlmostEqualUlps(reducer.fLine[0].fY, bounds.fTop)
229 && !AlmostEqualUlps(reducer.fLine[0].fY, bounds.fBottom))
230 || (!AlmostEqualUlps(reducer.fLine[1].fX, bounds.fLeft)
231 && !AlmostEqualUlps(reducer.fLine[1].fX, bounds.fRight))
232 || (!AlmostEqualUlps(reducer.fLine[1].fY, bounds.fTop)
233 && !AlmostEqualUlps(reducer.fLine[1].fY, bounds.fBottom))) {
234 SkDebugf("[%d] line computed tight bounds order=%d\n", static_cast<int>(index), order);
235 REPORTER_ASSERT(reporter, 0);
236 }
237 }
238 }
caryclark@google.com927b7022013-11-25 14:18:21 +0000239#endif
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000240}
241
242#include "TestClassDef.h"
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000243
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000244DEFINE_TESTCLASS_SHORT(PathOpsReduceOrderCubicTest)