blob: 04aea1afa64fbd2afe2f9eee25ce1d5524be5e08 [file] [log] [blame]
caryclark@google.com639df892012-01-10 21:46:10 +00001#include "CubicIntersection.h"
2#include "CubicIntersection_TestData.h"
3#include "CubicIntersection_Tests.h"
4#include "TestUtilities.h"
5
6void ReduceOrder_Test() {
7 size_t index;
8 Cubic reduce;
9 int order;
10 enum {
11 RunAll,
12 RunPointDegenerates,
13 RunNotPointDegenerates,
14 RunLines,
15 RunNotLines,
16 RunModEpsilonLines,
17 RunLessEpsilonLines,
18 RunNegEpsilonLines,
19 RunQuadraticLines,
20 RunQuadraticModLines,
21 RunComputedLines,
22 RunNone
23 } run = RunAll;
24 int firstTestIndex = 0;
25#if 0
26 run = RunComputedLines;
27 firstTestIndex = 18;
28#endif
29 int firstPointDegeneratesTest = run == RunAll ? 0 : run == RunPointDegenerates ? firstTestIndex : INT_MAX;
30 int firstNotPointDegeneratesTest = run == RunAll ? 0 : run == RunNotPointDegenerates ? firstTestIndex : INT_MAX;
31 int firstLinesTest = run == RunAll ? 0 : run == RunLines ? firstTestIndex : INT_MAX;
32 int firstNotLinesTest = run == RunAll ? 0 : run == RunNotLines ? firstTestIndex : INT_MAX;
33 int firstModEpsilonTest = run == RunAll ? 0 : run == RunModEpsilonLines ? firstTestIndex : INT_MAX;
34 int firstLessEpsilonTest = run == RunAll ? 0 : run == RunLessEpsilonLines ? firstTestIndex : INT_MAX;
35 int firstNegEpsilonTest = run == RunAll ? 0 : run == RunNegEpsilonLines ? firstTestIndex : INT_MAX;
36 int firstQuadraticLineTest = run == RunAll ? 0 : run == RunQuadraticLines ? firstTestIndex : INT_MAX;
37 int firstQuadraticModLineTest = run == RunAll ? 0 : run == RunQuadraticModLines ? firstTestIndex : INT_MAX;
38 int firstComputedLinesTest = run == RunAll ? 0 : run == RunComputedLines ? firstTestIndex : INT_MAX;
39
40 for (index = firstPointDegeneratesTest; index < pointDegenerates_count; ++index) {
41 const Cubic& cubic = pointDegenerates[index];
42 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
43 if (order != 1) {
44 printf("[%d] pointDegenerates order=%d\n", (int) index, order);
45 }
46 }
47 for (index = firstNotPointDegeneratesTest; index < notPointDegenerates_count; ++index) {
48 const Cubic& cubic = notPointDegenerates[index];
49 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
50 if (order == 1) {
51 printf("[%d] notPointDegenerates order=%d\n", (int) index, order);
52 }
53 }
54 for (index = firstLinesTest; index < lines_count; ++index) {
55 const Cubic& cubic = lines[index];
56 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
57 if (order != 2) {
58 printf("[%d] lines order=%d\n", (int) index, order);
59 }
60 }
61 for (index = firstNotLinesTest; index < notLines_count; ++index) {
62 const Cubic& cubic = notLines[index];
63 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
64 if (order == 2) {
65 printf("[%d] notLines order=%d\n", (int) index, order);
66 }
67 }
68 for (index = firstModEpsilonTest; index < modEpsilonLines_count; ++index) {
69 const Cubic& cubic = modEpsilonLines[index];
70 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
71 if (order == 2) {
72 printf("[%d] line mod by epsilon order=%d\n", (int) index, order);
73 }
74 }
75 for (index = firstLessEpsilonTest; index < lessEpsilonLines_count; ++index) {
76 const Cubic& cubic = lessEpsilonLines[index];
77 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
78 if (order != 2) {
79 printf("[%d] line less by epsilon/2 order=%d\n", (int) index, order);
80 }
81 }
82 for (index = firstNegEpsilonTest; index < negEpsilonLines_count; ++index) {
83 const Cubic& cubic = negEpsilonLines[index];
84 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
85 if (order != 2) {
86 printf("[%d] line neg by epsilon/2 order=%d\n", (int) index, order);
87 }
88 }
89 for (index = firstQuadraticLineTest; index < quadraticLines_count; ++index) {
90 const Quadratic& quad = quadraticLines[index];
91 Cubic cubic;
92 quad_to_cubic(quad, cubic);
93 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
94 if (order != 2) {
95 printf("[%d] line quad order=%d\n", (int) index, order);
96 }
97 }
98 for (index = firstQuadraticModLineTest; index < quadraticModEpsilonLines_count; ++index) {
99 const Quadratic& quad = quadraticModEpsilonLines[index];
100 Cubic cubic;
101 quad_to_cubic(quad, cubic);
102 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
103 if (order != 3) {
104 printf("[%d] line mod quad order=%d\n", (int) index, order);
105 }
106 }
107
108 // test if computed line end points are valid
109 for (index = firstComputedLinesTest; index < lines_count; ++index) {
110 const Cubic& cubic = lines[index];
111 bool controlsInside = controls_inside(cubic);
112 order = reduceOrder(cubic, reduce, kReduceOrder_QuadraticsAllowed);
113 if (reduce[0].x == reduce[1].x && reduce[0].y == reduce[1].y) {
114 printf("[%d] line computed ends match order=%d\n", (int) index, order);
115 }
116 if (controlsInside) {
117 if ( reduce[0].x != cubic[0].x && reduce[0].x != cubic[3].x
118 || reduce[0].y != cubic[0].y && reduce[0].y != cubic[3].y
119 || reduce[1].x != cubic[0].x && reduce[1].x != cubic[3].x
120 || reduce[1].y != cubic[0].y && reduce[1].y != cubic[3].y) {
121 printf("[%d] line computed ends order=%d\n", (int) index, order);
122 }
123 } else {
124 // binary search for extrema, compare against actual results
125 // while a control point is outside of bounding box formed by end points, split
126 _Rect bounds = {DBL_MAX, DBL_MAX, -DBL_MAX, -DBL_MAX};
127 find_tight_bounds(cubic, bounds);
128 if ( !approximately_equal(reduce[0].x, bounds.left) && !approximately_equal(reduce[0].x, bounds.right)
129 || !approximately_equal(reduce[0].y, bounds.top) && !approximately_equal(reduce[0].y, bounds.bottom)
130 || !approximately_equal(reduce[1].x, bounds.left) && !approximately_equal(reduce[1].x, bounds.right)
131 || !approximately_equal(reduce[1].y, bounds.top) && !approximately_equal(reduce[1].y, bounds.bottom)) {
132 printf("[%d] line computed tight bounds order=%d\n", (int) index, order);
133 }
134
135 }
136 }
137}