blob: c9da2c737829b7fde24531172b90ea74419add18 [file] [log] [blame]
caryclark@google.coma5e55922013-05-07 18:51:31 +00001/*
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.com8d0a5242013-07-16 16:11:16 +00007#include "PathOpsTestCommon.h"
caryclark@google.coma5e55922013-05-07 18:51:31 +00008#include "SkIntersections.h"
9#include "SkPathOpsCubic.h"
10#include "SkPathOpsQuad.h"
11#include "SkReduceOrder.h"
12#include "Test.h"
13
14static struct lineCubic {
15 SkDCubic cubic;
16 SkDQuad quad;
17 int answerCount;
18 SkDPoint answers[2];
19} quadCubicTests[] = {
skia.committer@gmail.comb0a05892013-10-03 07:01:37 +000020 {{{{49, 47}, {49, 74.614250183105469}, {26.614250183105469, 97}, {-1, 97}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000021 {{{-8.659739592076221e-015, 96.991401672363281}, {20.065492630004883, 96.645187377929688},
22 {34.355339050292969, 82.355339050292969}}}, 2,
23 {{34.355339050292969,82.355339050292969}, {34.306797674910243,82.403823585863449}}},
24
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000025 {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000026 {{{18,226}, {14.686291694641113,226}, {12.342399597167969,228.3424072265625}}}, 1,
27 {{18,226}, {0,0}}},
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000028 {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000029 {{{12.342399597167969,228.3424072265625}, {10,230.68629455566406}, {10,234}}}, 1,
30 {{10,234}, {0,0}}},
caryclark@google.coma5e55922013-05-07 18:51:31 +000031};
32
33static const size_t quadCubicTests_count = SK_ARRAY_COUNT(quadCubicTests);
34
35static void PathOpsCubicQuadIntersectionTest(skiatest::Reporter* reporter) {
36 for (size_t index = 0; index < quadCubicTests_count; ++index) {
37 int iIndex = static_cast<int>(index);
38 const SkDCubic& cubic = quadCubicTests[index].cubic;
caryclark@google.com8d0a5242013-07-16 16:11:16 +000039 SkASSERT(ValidCubic(cubic));
caryclark@google.coma5e55922013-05-07 18:51:31 +000040 const SkDQuad& quad = quadCubicTests[index].quad;
caryclark@google.com8d0a5242013-07-16 16:11:16 +000041 SkASSERT(ValidQuad(quad));
caryclark@google.coma5e55922013-05-07 18:51:31 +000042 SkReduceOrder reduce1;
43 SkReduceOrder reduce2;
44 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics,
45 SkReduceOrder::kFill_Style);
46 int order2 = reduce2.reduce(quad, SkReduceOrder::kFill_Style);
47 if (order1 != 4) {
48 SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
49 REPORTER_ASSERT(reporter, 0);
50 }
51 if (order2 != 3) {
52 SkDebugf("[%d] quad order=%d\n", iIndex, order2);
53 REPORTER_ASSERT(reporter, 0);
54 }
55 SkIntersections i;
56 int roots = i.intersect(cubic, quad);
57 SkASSERT(roots == quadCubicTests[index].answerCount);
58 for (int pt = 0; pt < roots; ++pt) {
59 double tt1 = i[0][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000060 SkDPoint xy1 = cubic.ptAtT(tt1);
caryclark@google.coma5e55922013-05-07 18:51:31 +000061 double tt2 = i[1][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000062 SkDPoint xy2 = quad.ptAtT(tt2);
caryclark@google.coma5e55922013-05-07 18:51:31 +000063 if (!xy1.approximatelyEqual(xy2)) {
64 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
65 __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
66 }
67 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
68 bool found = false;
69 for (int idx2 = 0; idx2 < quadCubicTests[index].answerCount; ++idx2) {
70 found |= quadCubicTests[index].answers[idx2].approximatelyEqual(xy1);
71 }
72 REPORTER_ASSERT(reporter, found);
73 }
74 reporter->bumpTestCount();
75 }
76}
77
78#include "TestClassDef.h"
79DEFINE_TESTCLASS_SHORT(PathOpsCubicQuadIntersectionTest)