caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +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 | */ |
| 7 | #include "SkIntersections.h" |
| 8 | #include "SkPathOpsLine.h" |
| 9 | #include "Test.h" |
| 10 | |
| 11 | // FIXME: add tests for intersecting, non-intersecting, degenerate, coincident |
| 12 | static const SkDLine tests[][2] = { |
| 13 | {{{{0, 0}, {1, 0}}}, {{{1, 0}, {0, 0}}}}, |
| 14 | {{{{0, 0}, {0, 0}}}, {{{0, 0}, {1, 0}}}}, |
| 15 | {{{{0, 1}, {0, 1}}}, {{{0, 0}, {0, 2}}}}, |
| 16 | {{{{0, 0}, {1, 0}}}, {{{0, 0}, {2, 0}}}}, |
| 17 | {{{{1, 1}, {2, 2}}}, {{{0, 0}, {3, 3}}}}, |
| 18 | {{{{166.86950047022856, 112.69654129527828}, {166.86948801592692, 112.69655741235339}}}, |
| 19 | {{{166.86960700313026, 112.6965477747386}, {166.86925794355412, 112.69656471103423}}}} |
| 20 | }; |
| 21 | |
| 22 | static const size_t tests_count = sizeof(tests) / sizeof(tests[0]); |
| 23 | |
| 24 | static const SkDLine noIntersect[][2] = { |
| 25 | {{{{0, 0}, {1, 0}}}, {{{3, 0}, {2, 0}}}}, |
| 26 | {{{{0, 0}, {0, 0}}}, {{{1, 0}, {2, 0}}}}, |
| 27 | {{{{0, 1}, {0, 1}}}, {{{0, 3}, {0, 2}}}}, |
| 28 | {{{{0, 0}, {1, 0}}}, {{{2, 0}, {3, 0}}}}, |
| 29 | {{{{1, 1}, {2, 2}}}, {{{4, 4}, {3, 3}}}}, |
| 30 | }; |
| 31 | |
| 32 | static const size_t noIntersect_count = sizeof(noIntersect) / sizeof(noIntersect[0]); |
| 33 | |
| 34 | static void TestLineIntersection(skiatest::Reporter* reporter) { |
| 35 | size_t index; |
| 36 | for (index = 0; index < tests_count; ++index) { |
| 37 | const SkDLine& line1 = tests[index][0]; |
| 38 | const SkDLine& line2 = tests[index][1]; |
| 39 | SkIntersections ts; |
| 40 | int pts = ts.intersect(line1, line2); |
| 41 | REPORTER_ASSERT(reporter, pts); |
| 42 | for (int i = 0; i < pts; ++i) { |
| 43 | SkDPoint result1 = line1.xyAtT(ts[0][i]); |
| 44 | SkDPoint result2 = line2.xyAtT(ts[1][i]); |
| 45 | if (!result1.approximatelyEqual(result2)) { |
| 46 | REPORTER_ASSERT(reporter, pts != 1); |
| 47 | result2 = line2.xyAtT(ts[1][i ^ 1]); |
| 48 | REPORTER_ASSERT(reporter, result1.approximatelyEqual(result2)); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | for (index = 0; index < noIntersect_count; ++index) { |
| 53 | const SkDLine& line1 = noIntersect[index][0]; |
| 54 | const SkDLine& line2 = noIntersect[index][1]; |
| 55 | SkIntersections ts; |
| 56 | int pts = ts.intersect(line1, line2); |
| 57 | REPORTER_ASSERT(reporter, !pts); } |
| 58 | } |
| 59 | |
| 60 | #include "TestClassDef.h" |
| 61 | DEFINE_TESTCLASS("PathOpsLineIntersection", LineIntersectionTestClass, TestLineIntersection) |