1 /*
2 * Copyright 2015 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 "include/core/SkTypes.h"
8 #include "include/private/base/SkDebug.h"
9 #include "src/pathops/SkIntersections.h"
10 #include "src/pathops/SkPathOpsConic.h"
11 #include "src/pathops/SkPathOpsCubic.h"
12 #include "src/pathops/SkPathOpsPoint.h"
13 #include "src/pathops/SkReduceOrder.h"
14 #include "tests/PathOpsTestCommon.h"
15 #include "tests/Test.h"
16
17 #include <iterator>
18
19 static struct cubicConic {
20 CubicPts cubic;
21 ConicPts conic;
22 } cubicConicTests[] = {
23 #if 0
24 // FIXME: this triggers an assert in bool SkTSect::extractCoincident() at
25 // SkOPASSERT(oppStartT < oppEndT);
26 // Throwing an error here breaks one test, but only in release.
27 // More work to be done to figure this out.
28 {{{{2.1883804947719909e-05, 3.6366123822517693e-05 },
29 {2.9145950975362211e-05, 2.9117207304807380e-05 },
30 {2.9113532946212217e-05, 2.9173743314458989e-05 },
31 {0.00000000000000000, 5.8282588724978268e-05 }}},
32 {{{{0.00000000000000000, 5.8282581449020654e-05 },
33 {0.00000000000000000, 5.8282563259126619e-05 },
34 {5.8282588724978268e-05, 0.00000000000000000 }}}, 53684.6563f}},
35 #endif
36
37 {{{{188.60000610351562, 2041.5999755859375}, {188.60000610351562, 2065.39990234375},
38 {208, 2084.800048828125}, {231.80000305175781, 2084.800048828125}}},
39 {{{{231.80000305175781, 2084.800048828125}, {188.60000610351562, 2084.800048828125},
40 {188.60000610351562, 2041.5999755859375}}}, 0.707107008f}},
41
42 {{{{231.80000305175781, 2084.800048828125}, {255.60000610351562, 2084.800048828125},
43 {275, 2065.39990234375}, {275, 2041.5999755859375}}},
44 {{{{275, 2041.5999755859375}, {275, 2084.800048828125},
45 {231.80000305175781, 2084.800048828125}}}, 0.707107008f}},
46 };
47
48 static const int cubicConicTests_count = (int) std::size(cubicConicTests);
49
cubicConicIntersection(skiatest::Reporter * reporter,int index)50 static void cubicConicIntersection(skiatest::Reporter* reporter, int index) {
51 const CubicPts& cu = cubicConicTests[index].cubic;
52 SkDCubic cubic;
53 cubic.debugSet(cu.fPts);
54 SkASSERT(ValidCubic(cubic));
55 const ConicPts& co = cubicConicTests[index].conic;
56 SkDConic conic;
57 conic.debugSet(co.fPts.fPts, co.fWeight);
58 SkASSERT(ValidConic(conic));
59 SkReduceOrder reduce1;
60 SkReduceOrder reduce2;
61 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
62 int order2 = reduce2.reduce(conic.fPts);
63 if (order1 != 4) {
64 SkDebugf("[%d] cubic order=%d\n", index, order1);
65 REPORTER_ASSERT(reporter, 0);
66 }
67 if (order2 != 3) {
68 SkDebugf("[%d] conic order=%d\n", index, order2);
69 REPORTER_ASSERT(reporter, 0);
70 }
71 SkIntersections i;
72 int roots = i.intersect(cubic, conic);
73 for (int pt = 0; pt < roots; ++pt) {
74 double tt1 = i[0][pt];
75 SkDPoint xy1 = cubic.ptAtT(tt1);
76 double tt2 = i[1][pt];
77 SkDPoint xy2 = conic.ptAtT(tt2);
78 if (!xy1.approximatelyEqual(xy2)) {
79 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
80 __FUNCTION__, index, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
81 }
82 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
83 }
84 reporter->bumpTestCount();
85 }
86
DEF_TEST(PathOpsCubicConicIntersection,reporter)87 DEF_TEST(PathOpsCubicConicIntersection, reporter) {
88 for (int index = 0; index < cubicConicTests_count; ++index) {
89 cubicConicIntersection(reporter, index);
90 reporter->bumpTestCount();
91 }
92 }
93
DEF_TEST(PathOpsCubicConicIntersectionOneOff,reporter)94 DEF_TEST(PathOpsCubicConicIntersectionOneOff, reporter) {
95 cubicConicIntersection(reporter, 0);
96 }
97