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