blob: 33779509890ffd3a11509ba4a4366b6621e19c55 [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) {
31 Intersections intersections;
caryclark@google.comc6825902012-02-03 22:07:47 +000032 intersect(reduce1, reduce2, intersections);
caryclark@google.com27accef2012-01-25 18:57:23 +000033 if (intersections.intersected()) {
34 for (int pt = 0; pt < intersections.used(); ++pt) {
35 double tt1 = intersections.fT[0][pt];
36 double tx1, ty1;
37 xy_at_t(quad1, tt1, tx1, ty1);
38 double tt2 = intersections.fT[1][pt];
39 double tx2, ty2;
40 xy_at_t(quad2, tt2, tx2, ty2);
41 if (!approximately_equal(tx1, tx2)) {
42 printf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
43 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
44 }
45 if (!approximately_equal(ty1, ty2)) {
46 printf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
47 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
48 }
49 }
50 }
51 }
52 }
53}
54
caryclark@google.comb45a1b42012-05-18 20:50:33 +000055static const Quadratic testSet[] = {
56 {{8, 8}, {10, 10}, {8, -10}},
57 {{8, 8}, {12, 12}, {14, 4}},
58 {{8, 8}, {9, 9}, {10, 8}}
59};
60
61const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]);
62
63static void oneOffTest() {
caryclark@google.comf25edfe2012-06-01 18:20:10 +000064 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
65 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +000066 const Quadratic& quad1 = testSet[outer];
67 const Quadratic& quad2 = testSet[inner];
68 Intersections intersections;
69 intersect(quad1, quad2, intersections);
70 if (!intersections.intersected()) {
71 SkDebugf("%s no intersection!\n", __FUNCTION__);
72 }
73 for (int pt = 0; pt < intersections.used(); ++pt) {
74 double tt1 = intersections.fT[0][pt];
75 double tx1, ty1;
76 xy_at_t(quad1, tt1, tx1, ty1);
77 double tt2 = intersections.fT[1][pt];
78 double tx2, ty2;
79 xy_at_t(quad2, tt2, tx2, ty2);
80 if (!approximately_equal(tx1, tx2)) {
81 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
82 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
83 SkASSERT(0);
84 }
85 if (!approximately_equal(ty1, ty2)) {
86 SkDebugf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
87 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
88 SkASSERT(0);
89 }
90 SkDebugf("%s [%d][%d] t1=%1.9g (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
91 outer, inner, tt1, tx1, tx2, tt2);
92 }
93 }
94 }
95}
96
97void QuadraticIntersection_Test() {
98 oneOffTest();
99 standardTestCases();
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000100}