blob: 76ecd01a47aae1bc921864b3d3c1844267d49d36 [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.com2b34fe02013-05-08 07:01:40 +000020 {{{{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.coma5e55922013-05-07 18:51:31 +000026};
27
28static const size_t quadCubicTests_count = SK_ARRAY_COUNT(quadCubicTests);
29
30static 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.com8d0a5242013-07-16 16:11:16 +000034 SkASSERT(ValidCubic(cubic));
caryclark@google.coma5e55922013-05-07 18:51:31 +000035 const SkDQuad& quad = quadCubicTests[index].quad;
caryclark@google.com8d0a5242013-07-16 16:11:16 +000036 SkASSERT(ValidQuad(quad));
caryclark@google.coma5e55922013-05-07 18:51:31 +000037 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.com4fdbb222013-07-23 15:27:41 +000055 SkDPoint xy1 = cubic.ptAtT(tt1);
caryclark@google.coma5e55922013-05-07 18:51:31 +000056 double tt2 = i[1][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000057 SkDPoint xy2 = quad.ptAtT(tt2);
caryclark@google.coma5e55922013-05-07 18:51:31 +000058 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"
74DEFINE_TESTCLASS_SHORT(PathOpsCubicQuadIntersectionTest)