blob: 1a2e18858916e73adaa1ceabac2754dfec12ad32 [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
commit-bot@chromium.org91fc81c2014-04-30 13:37:48 +000014struct lineCubic {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000015 SkDCubic cubic;
16 SkDLine line;
commit-bot@chromium.org91fc81c2014-04-30 13:37:48 +000017};
18
19static lineCubic failLineCubicTests[] = {
20 {{{{37.5273438,-1.44140625}, {37.8736992,-1.69921875}, {38.1640625,-2.140625},
21 {38.3984375,-2.765625}}},
22 {{{40.625,-5.7890625}, {37.7109375,1.3515625}}}},
23};
24
25static const size_t failLineCubicTests_count = SK_ARRAY_COUNT(failLineCubicTests);
26
27static void testFail(skiatest::Reporter* reporter, int iIndex) {
28 const SkDCubic& cubic = failLineCubicTests[iIndex].cubic;
29 SkASSERT(ValidCubic(cubic));
30 const SkDLine& line = failLineCubicTests[iIndex].line;
31 SkASSERT(ValidLine(line));
32 SkReduceOrder reduce1;
33 SkReduceOrder reduce2;
34 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
35 int order2 = reduce2.reduce(line);
36 if (order1 < 4) {
37 SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
38 REPORTER_ASSERT(reporter, 0);
39 }
40 if (order2 < 2) {
41 SkDebugf("[%d] line order=%d\n", iIndex, order2);
42 REPORTER_ASSERT(reporter, 0);
43 }
44 if (order1 == 4 && order2 == 2) {
45 SkIntersections i;
46 int roots = i.intersect(cubic, line);
47 REPORTER_ASSERT(reporter, roots == 0);
48 }
49}
50
51static lineCubic lineCubicTests[] = {
52
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000053 {{{{421, 378}, {421, 380.209137f}, {418.761414f, 382}, {416, 382}}},
54 {{{320, 378}, {421, 378.000031f}}}},
55
56 {{{{416, 383}, {418.761414f, 383}, {421, 380.761414f}, {421, 378}}},
57 {{{320, 378}, {421, 378.000031f}}}},
58
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000059 {{{{154,715}, {151.238571,715}, {149,712.761414}, {149,710}}},
60 {{{149,675}, {149,710.001465}}}},
61
skia.committer@gmail.comb0a05892013-10-03 07:01:37 +000062 {{{{0,1}, {1,6}, {4,1}, {4,3}}},
63 {{{6,1}, {1,4}}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000064
65 {{{{0,1}, {2,6}, {4,1}, {5,4}}},
66 {{{6,2}, {1,4}}}},
67
caryclark@google.com570863f2013-09-16 15:55:01 +000068 {{{{0,4}, {3,4}, {6,2}, {5,2}}},
69 {{{4,3}, {2,6}}}},
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000070#if 0
71 {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}},
72 {{{259.82843, 125.17157}, {261.535522, 123.46447}}}},
73#endif
caryclark@google.com07e97fc2013-07-08 17:17:02 +000074 {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125},
75 {1030.318359375,321}}},
76 {{{979.30487060546875,561}, {1036.695068359375,291}}}},
77 {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875},
78 {235.68154907226562,531}}},
79 {{{286.69512939453125,291}, {229.30485534667969,561}}}},
caryclark@google.com9166dcb2013-04-08 11:50:00 +000080 {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}},
81 {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}},
82};
83
caryclark@google.comad65a3e2013-04-15 19:13:59 +000084static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000085
caryclark@google.com07e97fc2013-07-08 17:17:02 +000086static void testOne(skiatest::Reporter* reporter, int iIndex) {
87 const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
caryclark@google.com8d0a5242013-07-16 16:11:16 +000088 SkASSERT(ValidCubic(cubic));
caryclark@google.com07e97fc2013-07-08 17:17:02 +000089 const SkDLine& line = lineCubicTests[iIndex].line;
caryclark@google.com8d0a5242013-07-16 16:11:16 +000090 SkASSERT(ValidLine(line));
caryclark@google.com07e97fc2013-07-08 17:17:02 +000091 SkReduceOrder reduce1;
92 SkReduceOrder reduce2;
caryclark@google.com927b7022013-11-25 14:18:21 +000093 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
caryclark@google.com07e97fc2013-07-08 17:17:02 +000094 int order2 = reduce2.reduce(line);
95 if (order1 < 4) {
96 SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
97 REPORTER_ASSERT(reporter, 0);
98 }
99 if (order2 < 2) {
100 SkDebugf("[%d] line order=%d\n", iIndex, order2);
101 REPORTER_ASSERT(reporter, 0);
102 }
103 if (order1 == 4 && order2 == 2) {
104 SkIntersections i;
105 int roots = i.intersect(cubic, line);
106 for (int pt = 0; pt < roots; ++pt) {
107 double tt1 = i[0][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000108 SkDPoint xy1 = cubic.ptAtT(tt1);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000109 double tt2 = i[1][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000110 SkDPoint xy2 = line.ptAtT(tt2);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000111 if (!xy1.approximatelyEqual(xy2)) {
112 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
113 __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000114 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000115 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000116 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000117#if ONE_OFF_DEBUG
118 double cubicT = i[0][0];
119 SkDPoint prev = cubic.ptAtT(cubicT * 2 - 1);
120 SkDPoint sect = cubic.ptAtT(cubicT);
121 double left[3] = { line.isLeft(prev), line.isLeft(sect), line.isLeft(cubic[3]) };
122 SkDebugf("cubic=(%1.9g, %1.9g, %1.9g)\n", left[0], left[1], left[2]);
123 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY);
124 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY);
125 SkDPoint prevL = line.ptAtT(i[1][0] - 0.0000007);
126 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY);
127 SkDPoint nextL = line.ptAtT(i[1][0] + 0.0000007);
128 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY);
129 SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL),
130 sect.distance(i.pt(0)), cubic[3].distance(prevL));
131#endif
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000132 }
133}
134
commit-bot@chromium.org91fc81c2014-04-30 13:37:48 +0000135DEF_TEST(PathOpsFailCubicLineIntersection, reporter) {
136 for (size_t index = 0; index < failLineCubicTests_count; ++index) {
137 int iIndex = static_cast<int>(index);
138 testFail(reporter, iIndex);
139 reporter->bumpTestCount();
140 }
141}
142
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000143DEF_TEST(PathOpsCubicLineIntersection, reporter) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000144 for (size_t index = 0; index < lineCubicTests_count; ++index) {
145 int iIndex = static_cast<int>(index);
146 testOne(reporter, iIndex);
147 reporter->bumpTestCount();
148 }
149}
150
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000151DEF_TEST(PathOpsCubicLineIntersectionOneOff, reporter) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000152 int iIndex = 0;
153 testOne(reporter, iIndex);
154 const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
155 const SkDLine& line = lineCubicTests[iIndex].line;
156 SkIntersections i;
157 i.intersect(cubic, line);
158 SkASSERT(i.used() == 1);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000159}