PathOpsCubicLineIntersectionTest.cpp revision 78e7b4e1b928fa69f672be3c743df6d6c3ecbced
1/*
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 */
7#include "PathOpsTestCommon.h"
8#include "SkIntersections.h"
9#include "SkPathOpsCubic.h"
10#include "SkPathOpsLine.h"
11#include "SkReduceOrder.h"
12#include "Test.h"
13#include "TestClassDef.h"
14
15static struct lineCubic {
16    SkDCubic cubic;
17    SkDLine line;
18} lineCubicTests[] = {
19    {{{{154,715}, {151.238571,715}, {149,712.761414}, {149,710}}},
20            {{{149,675}, {149,710.001465}}}},
21
22    {{{{0,1}, {1,6}, {4,1}, {4,3}}},
23            {{{6,1}, {1,4}}}},
24
25    {{{{0,1}, {2,6}, {4,1}, {5,4}}},
26            {{{6,2}, {1,4}}}},
27
28    {{{{0,4}, {3,4}, {6,2}, {5,2}}},
29            {{{4,3}, {2,6}}}},
30#if 0
31    {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}},
32            {{{259.82843, 125.17157}, {261.535522, 123.46447}}}},
33#endif
34    {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125},
35            {1030.318359375,321}}},
36            {{{979.30487060546875,561}, {1036.695068359375,291}}}},
37    {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875},
38            {235.68154907226562,531}}},
39            {{{286.69512939453125,291}, {229.30485534667969,561}}}},
40    {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}},
41    {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}},
42};
43
44static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests);
45
46static void testOne(skiatest::Reporter* reporter, int iIndex) {
47    const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
48    SkASSERT(ValidCubic(cubic));
49    const SkDLine& line = lineCubicTests[iIndex].line;
50    SkASSERT(ValidLine(line));
51    SkReduceOrder reduce1;
52    SkReduceOrder reduce2;
53    int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
54    int order2 = reduce2.reduce(line);
55    if (order1 < 4) {
56        SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
57        REPORTER_ASSERT(reporter, 0);
58    }
59    if (order2 < 2) {
60        SkDebugf("[%d] line order=%d\n", iIndex, order2);
61        REPORTER_ASSERT(reporter, 0);
62    }
63    if (order1 == 4 && order2 == 2) {
64        SkIntersections i;
65        int roots = i.intersect(cubic, line);
66        for (int pt = 0; pt < roots; ++pt) {
67            double tt1 = i[0][pt];
68            SkDPoint xy1 = cubic.ptAtT(tt1);
69            double tt2 = i[1][pt];
70            SkDPoint xy2 = line.ptAtT(tt2);
71            if (!xy1.approximatelyEqual(xy2)) {
72                SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
73                    __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
74            }
75            REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
76        }
77    }
78}
79
80DEF_TEST(PathOpsCubicLineIntersection, reporter) {
81    for (size_t index = 0; index < lineCubicTests_count; ++index) {
82        int iIndex = static_cast<int>(index);
83        testOne(reporter, iIndex);
84        reporter->bumpTestCount();
85    }
86}
87
88DEF_TEST(PathOpsCubicLineIntersectionOneOff, reporter) {
89    int iIndex = 0;
90    testOne(reporter, iIndex);
91    const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
92    const SkDLine& line = lineCubicTests[iIndex].line;
93    SkIntersections i;
94    i.intersect(cubic, line);
95    SkASSERT(i.used() == 1);
96#if ONE_OFF_DEBUG
97    double cubicT = i[0][0];
98    SkDPoint prev = cubic.ptAtT(cubicT * 2 - 1);
99    SkDPoint sect = cubic.ptAtT(cubicT);
100    double left[3] = { line.isLeft(prev), line.isLeft(sect), line.isLeft(cubic[3]) };
101    SkDebugf("cubic=(%1.9g, %1.9g, %1.9g)\n", left[0], left[1], left[2]);
102    SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY);
103    SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY);
104    SkDPoint prevL = line.ptAtT(i[1][0] - 0.0000007);
105    SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY);
106    SkDPoint nextL = line.ptAtT(i[1][0] + 0.0000007);
107    SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY);
108    SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL),
109            sect.distance(i.pt(0)), cubic[3].distance(prevL));
110#endif
111}
112