blob: e077bd5f9340712c0d523620899f596018da70f3 [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +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 */
caryclark@google.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com8dcf1142012-07-02 20:27:02 +00008#include "CurveUtilities.h"
caryclark@google.com27accef2012-01-25 18:57:23 +00009#include "Intersection_Tests.h"
10#include "Intersections.h"
11#include "QuadraticIntersection_TestData.h"
12#include "TestUtilities.h"
caryclark@google.comb45a1b42012-05-18 20:50:33 +000013#include "SkTypes.h"
caryclark@google.com27accef2012-01-25 18:57:23 +000014
15const int firstQuadIntersectionTest = 9;
16
caryclark@google.comb45a1b42012-05-18 20:50:33 +000017static void standardTestCases() {
caryclark@google.com27accef2012-01-25 18:57:23 +000018 for (size_t index = firstQuadIntersectionTest; index < quadraticTests_count; ++index) {
19 const Quadratic& quad1 = quadraticTests[index][0];
20 const Quadratic& quad2 = quadraticTests[index][1];
21 Quadratic reduce1, reduce2;
22 int order1 = reduceOrder(quad1, reduce1);
23 int order2 = reduceOrder(quad2, reduce2);
24 if (order1 < 3) {
25 printf("[%d] quad1 order=%d\n", (int) index, order1);
26 }
27 if (order2 < 3) {
28 printf("[%d] quad2 order=%d\n", (int) index, order2);
29 }
30 if (order1 == 3 && order2 == 3) {
caryclark@google.com235f56a2012-09-14 14:19:30 +000031 Intersections intersections, intersections2;
caryclark@google.comc6825902012-02-03 22:07:47 +000032 intersect(reduce1, reduce2, intersections);
caryclark@google.com235f56a2012-09-14 14:19:30 +000033 intersect2(reduce1, reduce2, intersections2);
34 SkASSERT(intersections.used() == intersections2.used());
caryclark@google.com27accef2012-01-25 18:57:23 +000035 if (intersections.intersected()) {
36 for (int pt = 0; pt < intersections.used(); ++pt) {
37 double tt1 = intersections.fT[0][pt];
38 double tx1, ty1;
39 xy_at_t(quad1, tt1, tx1, ty1);
40 double tt2 = intersections.fT[1][pt];
41 double tx2, ty2;
42 xy_at_t(quad2, tt2, tx2, ty2);
43 if (!approximately_equal(tx1, tx2)) {
44 printf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
45 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
46 }
47 if (!approximately_equal(ty1, ty2)) {
48 printf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
49 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
50 }
caryclark@google.com235f56a2012-09-14 14:19:30 +000051 tt1 = intersections2.fT[0][pt];
52 SkASSERT(approximately_equal(intersections.fT[0][pt], tt1));
53 tt2 = intersections2.fT[1][pt];
54 SkASSERT(approximately_equal(intersections.fT[1][pt], tt2));
caryclark@google.com27accef2012-01-25 18:57:23 +000055 }
56 }
57 }
58 }
59}
60
caryclark@google.comb45a1b42012-05-18 20:50:33 +000061static const Quadratic testSet[] = {
caryclark@google.com6aea33f2012-10-09 14:11:58 +000062 {{400.121704, 149.468719}, {391.949493, 161.037186}, {391.949493, 181.202423}},
63 {{391.946747, 181.839218}, {391.946747, 155.62442}, {406.115479, 138.855438}},
64 {{360.048828125, 229.2578125}, {360.048828125, 224.4140625}, {362.607421875, 221.3671875}},
65 {{362.607421875, 221.3671875}, {365.166015625, 218.3203125}, {369.228515625, 218.3203125}},
caryclark@google.comb45a1b42012-05-18 20:50:33 +000066 {{8, 8}, {10, 10}, {8, -10}},
67 {{8, 8}, {12, 12}, {14, 4}},
68 {{8, 8}, {9, 9}, {10, 8}}
69};
70
71const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]);
72
73static void oneOffTest() {
caryclark@google.comf25edfe2012-06-01 18:20:10 +000074 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
75 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +000076 const Quadratic& quad1 = testSet[outer];
77 const Quadratic& quad2 = testSet[inner];
caryclark@google.com6aea33f2012-10-09 14:11:58 +000078 double tt1, tt2;
79 #if 0 // enable to test bezier clip style intersection
caryclark@google.comb45a1b42012-05-18 20:50:33 +000080 Intersections intersections;
81 intersect(quad1, quad2, intersections);
82 if (!intersections.intersected()) {
83 SkDebugf("%s no intersection!\n", __FUNCTION__);
84 }
85 for (int pt = 0; pt < intersections.used(); ++pt) {
caryclark@google.com235f56a2012-09-14 14:19:30 +000086 tt1 = intersections.fT[0][pt];
caryclark@google.comb45a1b42012-05-18 20:50:33 +000087 double tx1, ty1;
88 xy_at_t(quad1, tt1, tx1, ty1);
caryclark@google.com235f56a2012-09-14 14:19:30 +000089 tt2 = intersections.fT[1][pt];
caryclark@google.comb45a1b42012-05-18 20:50:33 +000090 double tx2, ty2;
91 xy_at_t(quad2, tt2, tx2, ty2);
92 if (!approximately_equal(tx1, tx2)) {
93 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
94 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
95 SkASSERT(0);
96 }
97 if (!approximately_equal(ty1, ty2)) {
98 SkDebugf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
99 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
100 SkASSERT(0);
101 }
102 SkDebugf("%s [%d][%d] t1=%1.9g (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
103 outer, inner, tt1, tx1, tx2, tt2);
104 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000105 #endif
caryclark@google.com235f56a2012-09-14 14:19:30 +0000106 Intersections intersections2;
107 intersect2(quad1, quad2, intersections2);
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000108 #if 0
caryclark@google.com235f56a2012-09-14 14:19:30 +0000109 SkASSERT(intersections.used() == intersections2.used());
110 for (int pt = 0; pt < intersections2.used(); ++pt) {
111 tt1 = intersections2.fT[0][pt];
112 SkASSERT(approximately_equal(intersections.fT[0][pt], tt1));
113 tt2 = intersections2.fT[1][pt];
114 SkASSERT(approximately_equal(intersections.fT[1][pt], tt2));
115 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000116 #endif
117 for (int pt = 0; pt < intersections2.used(); ++pt) {
118 tt1 = intersections2.fT[0][pt];
119 double tx1, ty1;
120 xy_at_t(quad1, tt1, tx1, ty1);
121 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
122 tt2 = intersections2.fT[1][pt2];
123 double tx2, ty2;
124 xy_at_t(quad2, tt2, tx2, ty2);
125 if (!approximately_equal(tx1, tx2)) {
126 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
127 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
128 SkASSERT(0);
129 }
130 if (!approximately_equal(ty1, ty2)) {
131 SkDebugf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
132 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
133 SkASSERT(0);
134 }
135 SkDebugf("%s [%d][%d] t1=%1.9g (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
136 outer, inner, tt1, tx1, tx2, tt2);
137 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000138 }
139 }
140}
141
caryclark@google.com32546db2012-08-31 20:55:07 +0000142static const Quadratic coincidentTestSet[] = {
143 {{8, 8}, {10, 10}, {8, -10}},
144 {{8, -10}, {10, 10}, {8, 8}},
145};
146
147const size_t coincidentTestSetCount = sizeof(coincidentTestSet) / sizeof(coincidentTestSet[0]);
148
149static void coincidentTest() {
150 for (size_t testIndex = 0; testIndex < coincidentTestSetCount - 1; testIndex += 2) {
151 const Quadratic& quad1 = coincidentTestSet[testIndex];
152 const Quadratic& quad2 = coincidentTestSet[testIndex + 1];
caryclark@google.com235f56a2012-09-14 14:19:30 +0000153 Intersections intersections, intersections2;
caryclark@google.com32546db2012-08-31 20:55:07 +0000154 intersect(quad1, quad2, intersections);
155 SkASSERT(intersections.coincidentUsed() == 2);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000156 int pt;
157 double tt1, tt2;
158 for (pt = 0; pt < intersections.coincidentUsed(); ++pt) {
159 tt1 = intersections.fT[0][pt];
caryclark@google.com32546db2012-08-31 20:55:07 +0000160 double tx1, ty1;
161 xy_at_t(quad1, tt1, tx1, ty1);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000162 tt2 = intersections.fT[1][pt];
caryclark@google.com32546db2012-08-31 20:55:07 +0000163 double tx2, ty2;
164 xy_at_t(quad2, tt2, tx2, ty2);
165 SkDebugf("%s [%d,%d] t1=%g (%g,%g) t2=%g (%g,%g)\n",
166 __FUNCTION__, (int)testIndex, pt, tt1, tx1, ty1, tt2, tx2, ty2);
167 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000168 intersect2(quad1, quad2, intersections2);
169 SkASSERT(intersections2.coincidentUsed() == 2);
170 for (pt = 0; pt < intersections2.coincidentUsed(); ++pt) {
171 tt1 = intersections2.fT[0][pt];
172 SkASSERT(approximately_equal(intersections.fT[0][pt], tt1));
173 tt2 = intersections2.fT[1][pt];
174 SkASSERT(approximately_equal(intersections.fT[1][pt], tt2));
175 }
caryclark@google.com32546db2012-08-31 20:55:07 +0000176 }
177}
178
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000179void QuadraticIntersection_Test() {
180 oneOffTest();
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000181 coincidentTest();
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000182 standardTestCases();
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000183}