blob: 7aeb58033531996c3348f404d528051261538bbd [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "CurveIntersection.h"
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002#include "CurveUtilities.h"
caryclark@google.com27accef2012-01-25 18:57:23 +00003#include "Intersection_Tests.h"
4#include "Intersections.h"
5#include "QuadraticIntersection_TestData.h"
6#include "TestUtilities.h"
caryclark@google.comb45a1b42012-05-18 20:50:33 +00007#include "SkTypes.h"
caryclark@google.com27accef2012-01-25 18:57:23 +00008
9const int firstQuadIntersectionTest = 9;
10
caryclark@google.comb45a1b42012-05-18 20:50:33 +000011static void standardTestCases() {
caryclark@google.com27accef2012-01-25 18:57:23 +000012 for (size_t index = firstQuadIntersectionTest; index < quadraticTests_count; ++index) {
13 const Quadratic& quad1 = quadraticTests[index][0];
14 const Quadratic& quad2 = quadraticTests[index][1];
15 Quadratic reduce1, reduce2;
16 int order1 = reduceOrder(quad1, reduce1);
17 int order2 = reduceOrder(quad2, reduce2);
18 if (order1 < 3) {
19 printf("[%d] quad1 order=%d\n", (int) index, order1);
20 }
21 if (order2 < 3) {
22 printf("[%d] quad2 order=%d\n", (int) index, order2);
23 }
24 if (order1 == 3 && order2 == 3) {
25 Intersections intersections;
caryclark@google.comc6825902012-02-03 22:07:47 +000026 intersect(reduce1, reduce2, intersections);
caryclark@google.com27accef2012-01-25 18:57:23 +000027 if (intersections.intersected()) {
28 for (int pt = 0; pt < intersections.used(); ++pt) {
29 double tt1 = intersections.fT[0][pt];
30 double tx1, ty1;
31 xy_at_t(quad1, tt1, tx1, ty1);
32 double tt2 = intersections.fT[1][pt];
33 double tx2, ty2;
34 xy_at_t(quad2, tt2, tx2, ty2);
35 if (!approximately_equal(tx1, tx2)) {
36 printf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
37 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
38 }
39 if (!approximately_equal(ty1, ty2)) {
40 printf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
41 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
42 }
43 }
44 }
45 }
46 }
47}
48
caryclark@google.comb45a1b42012-05-18 20:50:33 +000049static const Quadratic testSet[] = {
50 {{8, 8}, {10, 10}, {8, -10}},
51 {{8, 8}, {12, 12}, {14, 4}},
52 {{8, 8}, {9, 9}, {10, 8}}
53};
54
55const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]);
56
57static void oneOffTest() {
caryclark@google.comf25edfe2012-06-01 18:20:10 +000058 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
59 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +000060 const Quadratic& quad1 = testSet[outer];
61 const Quadratic& quad2 = testSet[inner];
62 Intersections intersections;
63 intersect(quad1, quad2, intersections);
64 if (!intersections.intersected()) {
65 SkDebugf("%s no intersection!\n", __FUNCTION__);
66 }
67 for (int pt = 0; pt < intersections.used(); ++pt) {
68 double tt1 = intersections.fT[0][pt];
69 double tx1, ty1;
70 xy_at_t(quad1, tt1, tx1, ty1);
71 double tt2 = intersections.fT[1][pt];
72 double tx2, ty2;
73 xy_at_t(quad2, tt2, tx2, ty2);
74 if (!approximately_equal(tx1, tx2)) {
75 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
76 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
77 SkASSERT(0);
78 }
79 if (!approximately_equal(ty1, ty2)) {
80 SkDebugf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
81 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
82 SkASSERT(0);
83 }
84 SkDebugf("%s [%d][%d] t1=%1.9g (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
85 outer, inner, tt1, tx1, tx2, tt2);
86 }
87 }
88 }
89}
90
91void QuadraticIntersection_Test() {
92 oneOffTest();
93 standardTestCases();
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000094}