1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2012 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci#include "src/pathops/SkIntersections.h"
8cb93a386Sopenharmony_ci#include "src/pathops/SkPathOpsCubic.h"
9cb93a386Sopenharmony_ci#include "src/pathops/SkPathOpsLine.h"
10cb93a386Sopenharmony_ci#include "src/pathops/SkReduceOrder.h"
11cb93a386Sopenharmony_ci#include "tests/PathOpsTestCommon.h"
12cb93a386Sopenharmony_ci#include "tests/Test.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci#include <utility>
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_cistruct lineCubic {
17cb93a386Sopenharmony_ci    CubicPts cubic;
18cb93a386Sopenharmony_ci    SkDLine line;
19cb93a386Sopenharmony_ci};
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_cistatic lineCubic failLineCubicTests[] = {
22cb93a386Sopenharmony_ci    {{{{37.5273438,-1.44140625}, {37.8736992,-1.69921875}, {38.1640625,-2.140625},
23cb93a386Sopenharmony_ci            {38.3984375,-2.765625}}},
24cb93a386Sopenharmony_ci            {{{40.625,-5.7890625}, {37.7109375,1.3515625}}}},
25cb93a386Sopenharmony_ci};
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_cistatic const size_t failLineCubicTests_count = SK_ARRAY_COUNT(failLineCubicTests);
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_cistatic void testFail(skiatest::Reporter* reporter, int iIndex) {
30cb93a386Sopenharmony_ci    const CubicPts& cuPts = failLineCubicTests[iIndex].cubic;
31cb93a386Sopenharmony_ci    SkDCubic cubic;
32cb93a386Sopenharmony_ci    cubic.debugSet(cuPts.fPts);
33cb93a386Sopenharmony_ci    SkASSERT(ValidCubic(cubic));
34cb93a386Sopenharmony_ci    const SkDLine& line = failLineCubicTests[iIndex].line;
35cb93a386Sopenharmony_ci    SkASSERT(ValidLine(line));
36cb93a386Sopenharmony_ci    SkReduceOrder reduce1;
37cb93a386Sopenharmony_ci    SkReduceOrder reduce2;
38cb93a386Sopenharmony_ci    int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
39cb93a386Sopenharmony_ci    int order2 = reduce2.reduce(line);
40cb93a386Sopenharmony_ci    if (order1 < 4) {
41cb93a386Sopenharmony_ci        SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
42cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, 0);
43cb93a386Sopenharmony_ci    }
44cb93a386Sopenharmony_ci    if (order2 < 2) {
45cb93a386Sopenharmony_ci        SkDebugf("[%d] line order=%d\n", iIndex, order2);
46cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, 0);
47cb93a386Sopenharmony_ci    }
48cb93a386Sopenharmony_ci    if (order1 == 4 && order2 == 2) {
49cb93a386Sopenharmony_ci        SkIntersections i;
50cb93a386Sopenharmony_ci        int roots = i.intersect(cubic, line);
51cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, roots == 0);
52cb93a386Sopenharmony_ci    }
53cb93a386Sopenharmony_ci}
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_cistatic lineCubic lineCubicTests[] = {
56cb93a386Sopenharmony_ci    {{{{0, 6}, {1.0851458311080933, 4.3722810745239258}, {1.5815209150314331, 3.038947582244873}, {1.9683018922805786, 1.9999997615814209}}},
57cb93a386Sopenharmony_ci     {{{3,2}, {1,2}}}},
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_ci    {{{{0.468027353,4}, {1.06734705,1.33333337}, {1.36700678,0}, {3,0}}},
60cb93a386Sopenharmony_ci    {{{2,1}, {0,1}}}},
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci    {{{{-634.60540771484375, -481.262939453125}, {266.2696533203125, -752.70867919921875},
63cb93a386Sopenharmony_ci            {-751.8370361328125, -317.37921142578125}, {-969.7427978515625, 824.7255859375}}},
64cb93a386Sopenharmony_ci            {{{-287.9506133720805678, -557.1376476615772617},
65cb93a386Sopenharmony_ci            {-285.9506133720805678, -557.1376476615772617}}}},
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci    {{{{36.7184372,0.888650894}, {36.7184372,0.888650894}, {35.1233864,0.554015458},
68cb93a386Sopenharmony_ci            {34.5114098,-0.115255356}}}, {{{35.4531212,0}, {31.9375,0}}}},
69cb93a386Sopenharmony_ci
70cb93a386Sopenharmony_ci    {{{{421, 378}, {421, 380.209137f}, {418.761414f, 382}, {416, 382}}},
71cb93a386Sopenharmony_ci            {{{320, 378}, {421, 378.000031f}}}},
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci    {{{{416, 383}, {418.761414f, 383}, {421, 380.761414f}, {421, 378}}},
74cb93a386Sopenharmony_ci            {{{320, 378}, {421, 378.000031f}}}},
75cb93a386Sopenharmony_ci
76cb93a386Sopenharmony_ci    {{{{154,715}, {151.238571,715}, {149,712.761414}, {149,710}}},
77cb93a386Sopenharmony_ci            {{{149,675}, {149,710.001465}}}},
78cb93a386Sopenharmony_ci
79cb93a386Sopenharmony_ci    {{{{0,1}, {1,6}, {4,1}, {4,3}}},
80cb93a386Sopenharmony_ci            {{{6,1}, {1,4}}}},
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ci    {{{{0,1}, {2,6}, {4,1}, {5,4}}},
83cb93a386Sopenharmony_ci            {{{6,2}, {1,4}}}},
84cb93a386Sopenharmony_ci
85cb93a386Sopenharmony_ci    {{{{0,4}, {3,4}, {6,2}, {5,2}}},
86cb93a386Sopenharmony_ci            {{{4,3}, {2,6}}}},
87cb93a386Sopenharmony_ci#if 0
88cb93a386Sopenharmony_ci    {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}},
89cb93a386Sopenharmony_ci            {{{259.82843, 125.17157}, {261.535522, 123.46447}}}},
90cb93a386Sopenharmony_ci#endif
91cb93a386Sopenharmony_ci    {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125},
92cb93a386Sopenharmony_ci            {1030.318359375,321}}},
93cb93a386Sopenharmony_ci            {{{979.30487060546875,561}, {1036.695068359375,291}}}},
94cb93a386Sopenharmony_ci    {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875},
95cb93a386Sopenharmony_ci            {235.68154907226562,531}}},
96cb93a386Sopenharmony_ci            {{{286.69512939453125,291}, {229.30485534667969,561}}}},
97cb93a386Sopenharmony_ci    {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}},
98cb93a386Sopenharmony_ci    {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}},
99cb93a386Sopenharmony_ci};
100cb93a386Sopenharmony_ci
101cb93a386Sopenharmony_cistatic const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests);
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_cistatic int doIntersect(SkIntersections& intersections, const SkDCubic& cubic, const SkDLine& line) {
104cb93a386Sopenharmony_ci    int result;
105cb93a386Sopenharmony_ci    bool flipped = false;
106cb93a386Sopenharmony_ci    if (line[0].fX == line[1].fX) {
107cb93a386Sopenharmony_ci        double top = line[0].fY;
108cb93a386Sopenharmony_ci        double bottom = line[1].fY;
109cb93a386Sopenharmony_ci        flipped = top > bottom;
110cb93a386Sopenharmony_ci        if (flipped) {
111cb93a386Sopenharmony_ci            using std::swap;
112cb93a386Sopenharmony_ci            swap(top, bottom);
113cb93a386Sopenharmony_ci        }
114cb93a386Sopenharmony_ci        result = intersections.vertical(cubic, top, bottom, line[0].fX, flipped);
115cb93a386Sopenharmony_ci    } else if (line[0].fY == line[1].fY) {
116cb93a386Sopenharmony_ci        double left = line[0].fX;
117cb93a386Sopenharmony_ci        double right = line[1].fX;
118cb93a386Sopenharmony_ci        flipped = left > right;
119cb93a386Sopenharmony_ci        if (flipped) {
120cb93a386Sopenharmony_ci            using std::swap;
121cb93a386Sopenharmony_ci            swap(left, right);
122cb93a386Sopenharmony_ci        }
123cb93a386Sopenharmony_ci        result = intersections.horizontal(cubic, left, right, line[0].fY, flipped);
124cb93a386Sopenharmony_ci    } else {
125cb93a386Sopenharmony_ci        intersections.intersect(cubic, line);
126cb93a386Sopenharmony_ci        result = intersections.used();
127cb93a386Sopenharmony_ci    }
128cb93a386Sopenharmony_ci    return result;
129cb93a386Sopenharmony_ci}
130cb93a386Sopenharmony_ci
131cb93a386Sopenharmony_cistatic void testOne(skiatest::Reporter* reporter, int iIndex) {
132cb93a386Sopenharmony_ci    const CubicPts& cuPts = lineCubicTests[iIndex].cubic;
133cb93a386Sopenharmony_ci    SkDCubic cubic;
134cb93a386Sopenharmony_ci    cubic.debugSet(cuPts.fPts);
135cb93a386Sopenharmony_ci    SkASSERT(ValidCubic(cubic));
136cb93a386Sopenharmony_ci    const SkDLine& line = lineCubicTests[iIndex].line;
137cb93a386Sopenharmony_ci    SkASSERT(ValidLine(line));
138cb93a386Sopenharmony_ci    SkReduceOrder reduce1;
139cb93a386Sopenharmony_ci    SkReduceOrder reduce2;
140cb93a386Sopenharmony_ci    int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
141cb93a386Sopenharmony_ci    int order2 = reduce2.reduce(line);
142cb93a386Sopenharmony_ci    if (order1 < 4) {
143cb93a386Sopenharmony_ci        SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
144cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, 0);
145cb93a386Sopenharmony_ci    }
146cb93a386Sopenharmony_ci    if (order2 < 2) {
147cb93a386Sopenharmony_ci        SkDebugf("[%d] line order=%d\n", iIndex, order2);
148cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, 0);
149cb93a386Sopenharmony_ci    }
150cb93a386Sopenharmony_ci    if (order1 == 4 && order2 == 2) {
151cb93a386Sopenharmony_ci        SkIntersections i;
152cb93a386Sopenharmony_ci        int roots = doIntersect(i, cubic, line);
153cb93a386Sopenharmony_ci        for (int pt = 0; pt < roots; ++pt) {
154cb93a386Sopenharmony_ci            double tt1 = i[0][pt];
155cb93a386Sopenharmony_ci            SkDPoint xy1 = cubic.ptAtT(tt1);
156cb93a386Sopenharmony_ci            double tt2 = i[1][pt];
157cb93a386Sopenharmony_ci            SkDPoint xy2 = line.ptAtT(tt2);
158cb93a386Sopenharmony_ci            if (!xy1.approximatelyEqual(xy2)) {
159cb93a386Sopenharmony_ci                SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
160cb93a386Sopenharmony_ci                    __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
161cb93a386Sopenharmony_ci            }
162cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
163cb93a386Sopenharmony_ci        }
164cb93a386Sopenharmony_ci#if ONE_OFF_DEBUG
165cb93a386Sopenharmony_ci        double cubicT = i[0][0];
166cb93a386Sopenharmony_ci        SkDPoint prev = cubic.ptAtT(cubicT * 2 - 1);
167cb93a386Sopenharmony_ci        SkDPoint sect = cubic.ptAtT(cubicT);
168cb93a386Sopenharmony_ci        SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY);
169cb93a386Sopenharmony_ci        SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY);
170cb93a386Sopenharmony_ci        SkDPoint prevL = line.ptAtT(i[1][0] - 0.0000007);
171cb93a386Sopenharmony_ci        SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY);
172cb93a386Sopenharmony_ci        SkDPoint nextL = line.ptAtT(i[1][0] + 0.0000007);
173cb93a386Sopenharmony_ci        SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY);
174cb93a386Sopenharmony_ci        SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL),
175cb93a386Sopenharmony_ci                sect.distance(i.pt(0)), cubic[3].distance(prevL));
176cb93a386Sopenharmony_ci#endif
177cb93a386Sopenharmony_ci    }
178cb93a386Sopenharmony_ci}
179cb93a386Sopenharmony_ci
180cb93a386Sopenharmony_ciDEF_TEST(PathOpsFailCubicLineIntersection, reporter) {
181cb93a386Sopenharmony_ci    for (size_t index = 0; index < failLineCubicTests_count; ++index) {
182cb93a386Sopenharmony_ci        int iIndex = static_cast<int>(index);
183cb93a386Sopenharmony_ci        testFail(reporter, iIndex);
184cb93a386Sopenharmony_ci        reporter->bumpTestCount();
185cb93a386Sopenharmony_ci    }
186cb93a386Sopenharmony_ci}
187cb93a386Sopenharmony_ci
188cb93a386Sopenharmony_ciDEF_TEST(PathOpsCubicLineIntersection, reporter) {
189cb93a386Sopenharmony_ci    for (size_t index = 0; index < lineCubicTests_count; ++index) {
190cb93a386Sopenharmony_ci        int iIndex = static_cast<int>(index);
191cb93a386Sopenharmony_ci        testOne(reporter, iIndex);
192cb93a386Sopenharmony_ci        reporter->bumpTestCount();
193cb93a386Sopenharmony_ci    }
194cb93a386Sopenharmony_ci}
195cb93a386Sopenharmony_ci
196cb93a386Sopenharmony_ciDEF_TEST(PathOpsCubicLineIntersectionOneOff, reporter) {
197cb93a386Sopenharmony_ci    int iIndex = 0;
198cb93a386Sopenharmony_ci    testOne(reporter, iIndex);
199cb93a386Sopenharmony_ci    const CubicPts& cuPts = lineCubicTests[iIndex].cubic;
200cb93a386Sopenharmony_ci    SkDCubic cubic;
201cb93a386Sopenharmony_ci    cubic.debugSet(cuPts.fPts);
202cb93a386Sopenharmony_ci    const SkDLine& line = lineCubicTests[iIndex].line;
203cb93a386Sopenharmony_ci    SkIntersections i;
204cb93a386Sopenharmony_ci    i.intersect(cubic, line);
205cb93a386Sopenharmony_ci    SkASSERT(i.used() == 1);
206cb93a386Sopenharmony_ci}
207