blob: 95eb621f5696301cb5d1973e9bec21488da1aab5 [file] [log] [blame]
caryclark@google.com9166dcb2013-04-08 11:50:00 +00001/*
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.com8d0a5242013-07-16 16:11:16 +00007#include "PathOpsTestCommon.h"
caryclark@google.com9166dcb2013-04-08 11:50:00 +00008#include "SkIntersections.h"
9#include "SkPathOpsCubic.h"
10#include "SkPathOpsLine.h"
11#include "SkReduceOrder.h"
12#include "Test.h"
13
14static struct lineCubic {
15 SkDCubic cubic;
16 SkDLine line;
17} lineCubicTests[] = {
caryclark@google.com570863f2013-09-16 15:55:01 +000018 {{{{0,4}, {3,4}, {6,2}, {5,2}}},
19 {{{4,3}, {2,6}}}},
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000020#if 0
21 {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}},
22 {{{259.82843, 125.17157}, {261.535522, 123.46447}}}},
23#endif
caryclark@google.com07e97fc2013-07-08 17:17:02 +000024 {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125},
25 {1030.318359375,321}}},
26 {{{979.30487060546875,561}, {1036.695068359375,291}}}},
27 {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875},
28 {235.68154907226562,531}}},
29 {{{286.69512939453125,291}, {229.30485534667969,561}}}},
caryclark@google.com9166dcb2013-04-08 11:50:00 +000030 {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}},
31 {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}},
32};
33
caryclark@google.comad65a3e2013-04-15 19:13:59 +000034static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000035
caryclark@google.com07e97fc2013-07-08 17:17:02 +000036static void testOne(skiatest::Reporter* reporter, int iIndex) {
37 const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
caryclark@google.com8d0a5242013-07-16 16:11:16 +000038 SkASSERT(ValidCubic(cubic));
caryclark@google.com07e97fc2013-07-08 17:17:02 +000039 const SkDLine& line = lineCubicTests[iIndex].line;
caryclark@google.com8d0a5242013-07-16 16:11:16 +000040 SkASSERT(ValidLine(line));
caryclark@google.com07e97fc2013-07-08 17:17:02 +000041 SkReduceOrder reduce1;
42 SkReduceOrder reduce2;
43 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics,
44 SkReduceOrder::kFill_Style);
45 int order2 = reduce2.reduce(line);
46 if (order1 < 4) {
47 SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
48 REPORTER_ASSERT(reporter, 0);
49 }
50 if (order2 < 2) {
51 SkDebugf("[%d] line order=%d\n", iIndex, order2);
52 REPORTER_ASSERT(reporter, 0);
53 }
54 if (order1 == 4 && order2 == 2) {
55 SkIntersections i;
56 int roots = i.intersect(cubic, line);
57 for (int pt = 0; pt < roots; ++pt) {
58 double tt1 = i[0][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000059 SkDPoint xy1 = cubic.ptAtT(tt1);
caryclark@google.com07e97fc2013-07-08 17:17:02 +000060 double tt2 = i[1][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000061 SkDPoint xy2 = line.ptAtT(tt2);
caryclark@google.com07e97fc2013-07-08 17:17:02 +000062 if (!xy1.approximatelyEqual(xy2)) {
63 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
64 __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000065 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +000066 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
caryclark@google.com9166dcb2013-04-08 11:50:00 +000067 }
68 }
69}
70
caryclark@google.com07e97fc2013-07-08 17:17:02 +000071static void PathOpsCubicLineIntersectionTest(skiatest::Reporter* reporter) {
72 for (size_t index = 0; index < lineCubicTests_count; ++index) {
73 int iIndex = static_cast<int>(index);
74 testOne(reporter, iIndex);
75 reporter->bumpTestCount();
76 }
77}
78
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000079static void PathOpsCubicLineIntersectionOneOffTest(skiatest::Reporter* reporter) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000080 int iIndex = 0;
81 testOne(reporter, iIndex);
82 const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
83 const SkDLine& line = lineCubicTests[iIndex].line;
84 SkIntersections i;
85 i.intersect(cubic, line);
86 SkASSERT(i.used() == 1);
87#if ONE_OFF_DEBUG
88 double cubicT = i[0][0];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000089 SkDPoint prev = cubic.ptAtT(cubicT * 2 - 1);
90 SkDPoint sect = cubic.ptAtT(cubicT);
caryclark@google.com07e97fc2013-07-08 17:17:02 +000091 double left[3] = { line.isLeft(prev), line.isLeft(sect), line.isLeft(cubic[3]) };
92 SkDebugf("cubic=(%1.9g, %1.9g, %1.9g)\n", left[0], left[1], left[2]);
93 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY);
94 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY);
caryclark@google.com4fdbb222013-07-23 15:27:41 +000095 SkDPoint prevL = line.ptAtT(i[1][0] - 0.0000007);
caryclark@google.com07e97fc2013-07-08 17:17:02 +000096 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY);
caryclark@google.com4fdbb222013-07-23 15:27:41 +000097 SkDPoint nextL = line.ptAtT(i[1][0] + 0.0000007);
caryclark@google.com07e97fc2013-07-08 17:17:02 +000098 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY);
skia.committer@gmail.coma4aced42013-07-09 07:00:56 +000099 SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL),
100 sect.distance(i.pt(0)), cubic[3].distance(prevL));
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000101#endif
102}
103
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000104#include "TestClassDef.h"
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000105DEFINE_TESTCLASS_SHORT(PathOpsCubicLineIntersectionTest)
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000106
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000107DEFINE_TESTCLASS_SHORT(PathOpsCubicLineIntersectionOneOffTest)