caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 7 | #include "PathOpsTestCommon.h" |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 8 | #include "SkIntersections.h" |
| 9 | #include "SkPathOpsCubic.h" |
| 10 | #include "SkPathOpsQuad.h" |
| 11 | #include "SkReduceOrder.h" |
| 12 | #include "Test.h" |
| 13 | |
| 14 | static struct lineCubic { |
| 15 | SkDCubic cubic; |
| 16 | SkDQuad quad; |
| 17 | int answerCount; |
| 18 | SkDPoint answers[2]; |
| 19 | } quadCubicTests[] = { |
skia.committer@gmail.com | 2b34fe0 | 2013-05-08 07:01:40 +0000 | [diff] [blame] | 20 | {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}}, |
| 21 | {{{18,226}, {14.686291694641113,226}, {12.342399597167969,228.3424072265625}}}, 1, |
| 22 | {{18,226}, {0,0}}}, |
| 23 | {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}}, |
| 24 | {{{12.342399597167969,228.3424072265625}, {10,230.68629455566406}, {10,234}}}, 1, |
| 25 | {{10,234}, {0,0}}}, |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | static const size_t quadCubicTests_count = SK_ARRAY_COUNT(quadCubicTests); |
| 29 | |
| 30 | static void PathOpsCubicQuadIntersectionTest(skiatest::Reporter* reporter) { |
| 31 | for (size_t index = 0; index < quadCubicTests_count; ++index) { |
| 32 | int iIndex = static_cast<int>(index); |
| 33 | const SkDCubic& cubic = quadCubicTests[index].cubic; |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 34 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 35 | const SkDQuad& quad = quadCubicTests[index].quad; |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 36 | SkASSERT(ValidQuad(quad)); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 37 | SkReduceOrder reduce1; |
| 38 | SkReduceOrder reduce2; |
| 39 | int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics, |
| 40 | SkReduceOrder::kFill_Style); |
| 41 | int order2 = reduce2.reduce(quad, SkReduceOrder::kFill_Style); |
| 42 | if (order1 != 4) { |
| 43 | SkDebugf("[%d] cubic order=%d\n", iIndex, order1); |
| 44 | REPORTER_ASSERT(reporter, 0); |
| 45 | } |
| 46 | if (order2 != 3) { |
| 47 | SkDebugf("[%d] quad order=%d\n", iIndex, order2); |
| 48 | REPORTER_ASSERT(reporter, 0); |
| 49 | } |
| 50 | SkIntersections i; |
| 51 | int roots = i.intersect(cubic, quad); |
| 52 | SkASSERT(roots == quadCubicTests[index].answerCount); |
| 53 | for (int pt = 0; pt < roots; ++pt) { |
| 54 | double tt1 = i[0][pt]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 55 | SkDPoint xy1 = cubic.ptAtT(tt1); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 56 | double tt2 = i[1][pt]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 57 | SkDPoint xy2 = quad.ptAtT(tt2); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 58 | if (!xy1.approximatelyEqual(xy2)) { |
| 59 | SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n", |
| 60 | __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY); |
| 61 | } |
| 62 | REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2)); |
| 63 | bool found = false; |
| 64 | for (int idx2 = 0; idx2 < quadCubicTests[index].answerCount; ++idx2) { |
| 65 | found |= quadCubicTests[index].answers[idx2].approximatelyEqual(xy1); |
| 66 | } |
| 67 | REPORTER_ASSERT(reporter, found); |
| 68 | } |
| 69 | reporter->bumpTestCount(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #include "TestClassDef.h" |
| 74 | DEFINE_TESTCLASS_SHORT(PathOpsCubicQuadIntersectionTest) |