caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 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.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | #include "CurveIntersection.h" |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 8 | #include "CurveUtilities.h" |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 9 | #include "Intersection_Tests.h" |
| 10 | #include "Intersections.h" |
| 11 | #include "QuadraticIntersection_TestData.h" |
| 12 | #include "TestUtilities.h" |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 14 | |
| 15 | const int firstQuadIntersectionTest = 9; |
| 16 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 17 | static void standardTestCases() { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 18 | 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.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 31 | Intersections intersections, intersections2; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 32 | intersect(reduce1, reduce2, intersections); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 33 | intersect2(reduce1, reduce2, intersections2); |
| 34 | SkASSERT(intersections.used() == intersections2.used()); |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 35 | 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.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 51 | 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.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 61 | static const Quadratic testSet[] = { |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame^] | 62 | {{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.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 66 | {{8, 8}, {10, 10}, {8, -10}}, |
| 67 | {{8, 8}, {12, 12}, {14, 4}}, |
| 68 | {{8, 8}, {9, 9}, {10, 8}} |
| 69 | }; |
| 70 | |
| 71 | const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]); |
| 72 | |
| 73 | static void oneOffTest() { |
caryclark@google.com | f25edfe | 2012-06-01 18:20:10 +0000 | [diff] [blame] | 74 | for (size_t outer = 0; outer < testSetCount - 1; ++outer) { |
| 75 | for (size_t inner = outer + 1; inner < testSetCount; ++inner) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 76 | const Quadratic& quad1 = testSet[outer]; |
| 77 | const Quadratic& quad2 = testSet[inner]; |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame^] | 78 | double tt1, tt2; |
| 79 | #if 0 // enable to test bezier clip style intersection |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 80 | 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.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 86 | tt1 = intersections.fT[0][pt]; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 87 | double tx1, ty1; |
| 88 | xy_at_t(quad1, tt1, tx1, ty1); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 89 | tt2 = intersections.fT[1][pt]; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 90 | 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.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame^] | 105 | #endif |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 106 | Intersections intersections2; |
| 107 | intersect2(quad1, quad2, intersections2); |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame^] | 108 | #if 0 |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 109 | 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.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame^] | 116 | #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.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 142 | static const Quadratic coincidentTestSet[] = { |
| 143 | {{8, 8}, {10, 10}, {8, -10}}, |
| 144 | {{8, -10}, {10, 10}, {8, 8}}, |
| 145 | }; |
| 146 | |
| 147 | const size_t coincidentTestSetCount = sizeof(coincidentTestSet) / sizeof(coincidentTestSet[0]); |
| 148 | |
| 149 | static 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.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 153 | Intersections intersections, intersections2; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 154 | intersect(quad1, quad2, intersections); |
| 155 | SkASSERT(intersections.coincidentUsed() == 2); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 156 | int pt; |
| 157 | double tt1, tt2; |
| 158 | for (pt = 0; pt < intersections.coincidentUsed(); ++pt) { |
| 159 | tt1 = intersections.fT[0][pt]; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 160 | double tx1, ty1; |
| 161 | xy_at_t(quad1, tt1, tx1, ty1); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 162 | tt2 = intersections.fT[1][pt]; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 163 | 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.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 168 | 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.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 179 | void QuadraticIntersection_Test() { |
| 180 | oneOffTest(); |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame^] | 181 | coincidentTest(); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 182 | standardTestCases(); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 183 | } |