blob: 245f8a667450c6bc02a5260c4ca2a6838f9730d2 [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 */
7#include "SkIntersections.h"
8#include "SkPathOpsCubic.h"
9#include "SkPathOpsLine.h"
10#include "SkReduceOrder.h"
11#include "Test.h"
12
13static struct lineCubic {
14 SkDCubic cubic;
15 SkDLine line;
16} lineCubicTests[] = {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000017#if 0
18 {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}},
19 {{{259.82843, 125.17157}, {261.535522, 123.46447}}}},
20#endif
caryclark@google.com07e97fc2013-07-08 17:17:02 +000021 {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125},
22 {1030.318359375,321}}},
23 {{{979.30487060546875,561}, {1036.695068359375,291}}}},
24 {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875},
25 {235.68154907226562,531}}},
26 {{{286.69512939453125,291}, {229.30485534667969,561}}}},
caryclark@google.com9166dcb2013-04-08 11:50:00 +000027 {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}},
28 {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}},
29};
30
caryclark@google.comad65a3e2013-04-15 19:13:59 +000031static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000032
caryclark@google.com07e97fc2013-07-08 17:17:02 +000033static void testOne(skiatest::Reporter* reporter, int iIndex) {
34 const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
35 const SkDLine& line = lineCubicTests[iIndex].line;
36 SkReduceOrder reduce1;
37 SkReduceOrder reduce2;
38 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics,
39 SkReduceOrder::kFill_Style);
40 int order2 = reduce2.reduce(line);
41 if (order1 < 4) {
42 SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
43 REPORTER_ASSERT(reporter, 0);
44 }
45 if (order2 < 2) {
46 SkDebugf("[%d] line order=%d\n", iIndex, order2);
47 REPORTER_ASSERT(reporter, 0);
48 }
49 if (order1 == 4 && order2 == 2) {
50 SkIntersections i;
51 int roots = i.intersect(cubic, line);
52 for (int pt = 0; pt < roots; ++pt) {
53 double tt1 = i[0][pt];
54 SkDPoint xy1 = cubic.xyAtT(tt1);
55 double tt2 = i[1][pt];
56 SkDPoint xy2 = line.xyAtT(tt2);
57 if (!xy1.approximatelyEqual(xy2)) {
58 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
59 __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000060 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +000061 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
caryclark@google.com9166dcb2013-04-08 11:50:00 +000062 }
63 }
64}
65
caryclark@google.com07e97fc2013-07-08 17:17:02 +000066static void PathOpsCubicLineIntersectionTest(skiatest::Reporter* reporter) {
67 for (size_t index = 0; index < lineCubicTests_count; ++index) {
68 int iIndex = static_cast<int>(index);
69 testOne(reporter, iIndex);
70 reporter->bumpTestCount();
71 }
72}
73
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000074static void PathOpsCubicLineIntersectionOneOffTest(skiatest::Reporter* reporter) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000075 int iIndex = 0;
76 testOne(reporter, iIndex);
77 const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
78 const SkDLine& line = lineCubicTests[iIndex].line;
79 SkIntersections i;
80 i.intersect(cubic, line);
81 SkASSERT(i.used() == 1);
82#if ONE_OFF_DEBUG
83 double cubicT = i[0][0];
84 SkDPoint prev = cubic.xyAtT(cubicT * 2 - 1);
85 SkDPoint sect = cubic.xyAtT(cubicT);
86 double left[3] = { line.isLeft(prev), line.isLeft(sect), line.isLeft(cubic[3]) };
87 SkDebugf("cubic=(%1.9g, %1.9g, %1.9g)\n", left[0], left[1], left[2]);
88 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY);
89 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY);
90 SkDPoint prevL = line.xyAtT(i[1][0] - 0.0000007);
91 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY);
92 SkDPoint nextL = line.xyAtT(i[1][0] + 0.0000007);
93 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 +000094 SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL),
95 sect.distance(i.pt(0)), cubic[3].distance(prevL));
caryclark@google.com07e97fc2013-07-08 17:17:02 +000096#endif
97}
98
caryclark@google.com9166dcb2013-04-08 11:50:00 +000099#include "TestClassDef.h"
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000100DEFINE_TESTCLASS_SHORT(PathOpsCubicLineIntersectionTest)
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000101
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000102DEFINE_TESTCLASS_SHORT(PathOpsCubicLineIntersectionOneOffTest)