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