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) { |
| 31 | Intersections intersections; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 32 | intersect(reduce1, reduce2, intersections); |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 33 | 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.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 55 | static 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 | |
| 61 | const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]); |
| 62 | |
| 63 | static void oneOffTest() { |
caryclark@google.com | f25edfe | 2012-06-01 18:20:10 +0000 | [diff] [blame] | 64 | for (size_t outer = 0; outer < testSetCount - 1; ++outer) { |
| 65 | for (size_t inner = outer + 1; inner < testSetCount; ++inner) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 66 | 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 | |
| 97 | void QuadraticIntersection_Test() { |
| 98 | oneOffTest(); |
| 99 | standardTestCases(); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 100 | } |