blob: a6ae5e644109e187b288aace6c7a283e6bf6ee55 [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 {
caryclarka35ab3e2016-10-20 08:32:18 -070015 CubicPts cubic;
caryclark@google.com9166dcb2013-04-08 11:50:00 +000016 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) {
caryclarka35ab3e2016-10-20 08:32:18 -070028 const CubicPts& cuPts = failLineCubicTests[iIndex].cubic;
29 SkDCubic cubic;
30 cubic.debugSet(cuPts.fPts);
commit-bot@chromium.org91fc81c2014-04-30 13:37:48 +000031 SkASSERT(ValidCubic(cubic));
32 const SkDLine& line = failLineCubicTests[iIndex].line;
33 SkASSERT(ValidLine(line));
34 SkReduceOrder reduce1;
35 SkReduceOrder reduce2;
36 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
37 int order2 = reduce2.reduce(line);
38 if (order1 < 4) {
39 SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
40 REPORTER_ASSERT(reporter, 0);
41 }
42 if (order2 < 2) {
43 SkDebugf("[%d] line order=%d\n", iIndex, order2);
44 REPORTER_ASSERT(reporter, 0);
45 }
46 if (order1 == 4 && order2 == 2) {
47 SkIntersections i;
48 int roots = i.intersect(cubic, line);
49 REPORTER_ASSERT(reporter, roots == 0);
50 }
51}
52
53static lineCubic lineCubicTests[] = {
caryclarkaec25102015-04-29 08:28:30 -070054 {{{{0, 6}, {1.0851458311080933, 4.3722810745239258}, {1.5815209150314331, 3.038947582244873}, {1.9683018922805786, 1.9999997615814209}}},
55 {{{3,2}, {1,2}}}},
56
caryclark54359292015-03-26 07:52:43 -070057 {{{{0.468027353,4}, {1.06734705,1.33333337}, {1.36700678,0}, {3,0}}},
58 {{{2,1}, {0,1}}}},
59
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000060 {{{{-634.60540771484375, -481.262939453125}, {266.2696533203125, -752.70867919921875},
61 {-751.8370361328125, -317.37921142578125}, {-969.7427978515625, 824.7255859375}}},
62 {{{-287.9506133720805678, -557.1376476615772617},
63 {-285.9506133720805678, -557.1376476615772617}}}},
64
65 {{{{36.7184372,0.888650894}, {36.7184372,0.888650894}, {35.1233864,0.554015458},
66 {34.5114098,-0.115255356}}}, {{{35.4531212,0}, {31.9375,0}}}},
commit-bot@chromium.org91fc81c2014-04-30 13:37:48 +000067
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000068 {{{{421, 378}, {421, 380.209137f}, {418.761414f, 382}, {416, 382}}},
69 {{{320, 378}, {421, 378.000031f}}}},
70
71 {{{{416, 383}, {418.761414f, 383}, {421, 380.761414f}, {421, 378}}},
72 {{{320, 378}, {421, 378.000031f}}}},
73
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000074 {{{{154,715}, {151.238571,715}, {149,712.761414}, {149,710}}},
75 {{{149,675}, {149,710.001465}}}},
76
skia.committer@gmail.comb0a05892013-10-03 07:01:37 +000077 {{{{0,1}, {1,6}, {4,1}, {4,3}}},
78 {{{6,1}, {1,4}}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000079
80 {{{{0,1}, {2,6}, {4,1}, {5,4}}},
81 {{{6,2}, {1,4}}}},
82
caryclark@google.com570863f2013-09-16 15:55:01 +000083 {{{{0,4}, {3,4}, {6,2}, {5,2}}},
84 {{{4,3}, {2,6}}}},
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000085#if 0
86 {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}},
87 {{{259.82843, 125.17157}, {261.535522, 123.46447}}}},
88#endif
caryclark@google.com07e97fc2013-07-08 17:17:02 +000089 {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125},
90 {1030.318359375,321}}},
91 {{{979.30487060546875,561}, {1036.695068359375,291}}}},
92 {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875},
93 {235.68154907226562,531}}},
94 {{{286.69512939453125,291}, {229.30485534667969,561}}}},
caryclark@google.com9166dcb2013-04-08 11:50:00 +000095 {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}},
96 {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}},
97};
98
caryclark@google.comad65a3e2013-04-15 19:13:59 +000099static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000100
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000101static int doIntersect(SkIntersections& intersections, const SkDCubic& cubic, const SkDLine& line) {
102 int result;
103 bool flipped = false;
104 if (line[0].fX == line[1].fX) {
105 double top = line[0].fY;
106 double bottom = line[1].fY;
107 flipped = top > bottom;
108 if (flipped) {
109 SkTSwap<double>(top, bottom);
110 }
111 result = intersections.vertical(cubic, top, bottom, line[0].fX, flipped);
112 } else if (line[0].fY == line[1].fY) {
113 double left = line[0].fX;
114 double right = line[1].fX;
115 flipped = left > right;
116 if (flipped) {
117 SkTSwap<double>(left, right);
118 }
119 result = intersections.horizontal(cubic, left, right, line[0].fY, flipped);
120 } else {
121 intersections.intersect(cubic, line);
122 result = intersections.used();
123 }
124 return result;
125}
126
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000127static void testOne(skiatest::Reporter* reporter, int iIndex) {
caryclarka35ab3e2016-10-20 08:32:18 -0700128 const CubicPts& cuPts = lineCubicTests[iIndex].cubic;
129 SkDCubic cubic;
130 cubic.debugSet(cuPts.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000131 SkASSERT(ValidCubic(cubic));
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000132 const SkDLine& line = lineCubicTests[iIndex].line;
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000133 SkASSERT(ValidLine(line));
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000134 SkReduceOrder reduce1;
135 SkReduceOrder reduce2;
caryclark@google.com927b7022013-11-25 14:18:21 +0000136 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000137 int order2 = reduce2.reduce(line);
138 if (order1 < 4) {
139 SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
140 REPORTER_ASSERT(reporter, 0);
141 }
142 if (order2 < 2) {
143 SkDebugf("[%d] line order=%d\n", iIndex, order2);
144 REPORTER_ASSERT(reporter, 0);
145 }
146 if (order1 == 4 && order2 == 2) {
147 SkIntersections i;
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000148 int roots = doIntersect(i, cubic, line);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000149 for (int pt = 0; pt < roots; ++pt) {
150 double tt1 = i[0][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000151 SkDPoint xy1 = cubic.ptAtT(tt1);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000152 double tt2 = i[1][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000153 SkDPoint xy2 = line.ptAtT(tt2);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000154 if (!xy1.approximatelyEqual(xy2)) {
155 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
156 __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000157 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000158 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000159 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000160#if ONE_OFF_DEBUG
161 double cubicT = i[0][0];
162 SkDPoint prev = cubic.ptAtT(cubicT * 2 - 1);
163 SkDPoint sect = cubic.ptAtT(cubicT);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000164 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY);
165 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY);
166 SkDPoint prevL = line.ptAtT(i[1][0] - 0.0000007);
167 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY);
168 SkDPoint nextL = line.ptAtT(i[1][0] + 0.0000007);
169 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY);
170 SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL),
171 sect.distance(i.pt(0)), cubic[3].distance(prevL));
172#endif
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000173 }
174}
175
commit-bot@chromium.org91fc81c2014-04-30 13:37:48 +0000176DEF_TEST(PathOpsFailCubicLineIntersection, reporter) {
177 for (size_t index = 0; index < failLineCubicTests_count; ++index) {
178 int iIndex = static_cast<int>(index);
179 testFail(reporter, iIndex);
180 reporter->bumpTestCount();
181 }
182}
183
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000184DEF_TEST(PathOpsCubicLineIntersection, reporter) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000185 for (size_t index = 0; index < lineCubicTests_count; ++index) {
186 int iIndex = static_cast<int>(index);
187 testOne(reporter, iIndex);
188 reporter->bumpTestCount();
189 }
190}
191
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000192DEF_TEST(PathOpsCubicLineIntersectionOneOff, reporter) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000193 int iIndex = 0;
194 testOne(reporter, iIndex);
caryclarka35ab3e2016-10-20 08:32:18 -0700195 const CubicPts& cuPts = lineCubicTests[iIndex].cubic;
196 SkDCubic cubic;
197 cubic.debugSet(cuPts.fPts);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000198 const SkDLine& line = lineCubicTests[iIndex].line;
199 SkIntersections i;
200 i.intersect(cubic, line);
201 SkASSERT(i.used() == 1);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000202}