blob: c5f05f6f6d4647b83495c3a70309b08d6ae402ea [file] [log] [blame]
caryclark@google.com9166dcb2013-04-08 11:50:00 +00001/*
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 "SkIntersections.h"
8#include "SkPathOpsCubic.h"
9#include "SkPathOpsLine.h"
10#include "SkReduceOrder.h"
11#include "Test.h"
12
13static struct lineCubic {
14 SkDCubic cubic;
15 SkDLine line;
16} lineCubicTests[] = {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000017 {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125},
18 {1030.318359375,321}}},
19 {{{979.30487060546875,561}, {1036.695068359375,291}}}},
20 {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875},
21 {235.68154907226562,531}}},
22 {{{286.69512939453125,291}, {229.30485534667969,561}}}},
caryclark@google.com9166dcb2013-04-08 11:50:00 +000023 {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}},
24 {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}},
25};
26
caryclark@google.comad65a3e2013-04-15 19:13:59 +000027static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000028
caryclark@google.com07e97fc2013-07-08 17:17:02 +000029static void testOne(skiatest::Reporter* reporter, int iIndex) {
30 const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
31 const SkDLine& line = lineCubicTests[iIndex].line;
32 SkReduceOrder reduce1;
33 SkReduceOrder reduce2;
34 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics,
35 SkReduceOrder::kFill_Style);
36 int order2 = reduce2.reduce(line);
37 if (order1 < 4) {
38 SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
39 REPORTER_ASSERT(reporter, 0);
40 }
41 if (order2 < 2) {
42 SkDebugf("[%d] line order=%d\n", iIndex, order2);
43 REPORTER_ASSERT(reporter, 0);
44 }
45 if (order1 == 4 && order2 == 2) {
46 SkIntersections i;
47 int roots = i.intersect(cubic, line);
48 for (int pt = 0; pt < roots; ++pt) {
49 double tt1 = i[0][pt];
50 SkDPoint xy1 = cubic.xyAtT(tt1);
51 double tt2 = i[1][pt];
52 SkDPoint xy2 = line.xyAtT(tt2);
53 if (!xy1.approximatelyEqual(xy2)) {
54 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
55 __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000056 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +000057 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
caryclark@google.com9166dcb2013-04-08 11:50:00 +000058 }
59 }
60}
61
caryclark@google.com07e97fc2013-07-08 17:17:02 +000062static void PathOpsCubicLineIntersectionTest(skiatest::Reporter* reporter) {
63 for (size_t index = 0; index < lineCubicTests_count; ++index) {
64 int iIndex = static_cast<int>(index);
65 testOne(reporter, iIndex);
66 reporter->bumpTestCount();
67 }
68}
69
70static void PathOpsCubicLineIntersectionTestOne(skiatest::Reporter* reporter) {
71 int iIndex = 0;
72 testOne(reporter, iIndex);
73 const SkDCubic& cubic = lineCubicTests[iIndex].cubic;
74 const SkDLine& line = lineCubicTests[iIndex].line;
75 SkIntersections i;
76 i.intersect(cubic, line);
77 SkASSERT(i.used() == 1);
78#if ONE_OFF_DEBUG
79 double cubicT = i[0][0];
80 SkDPoint prev = cubic.xyAtT(cubicT * 2 - 1);
81 SkDPoint sect = cubic.xyAtT(cubicT);
82 double left[3] = { line.isLeft(prev), line.isLeft(sect), line.isLeft(cubic[3]) };
83 SkDebugf("cubic=(%1.9g, %1.9g, %1.9g)\n", left[0], left[1], left[2]);
84 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY);
85 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY);
86 SkDPoint prevL = line.xyAtT(i[1][0] - 0.0000007);
87 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY);
88 SkDPoint nextL = line.xyAtT(i[1][0] + 0.0000007);
89 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY);
90 SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL),
91 sect.distance(i.pt(0)), cubic[3].distance(prevL));
92#endif
93}
94
caryclark@google.com9166dcb2013-04-08 11:50:00 +000095#include "TestClassDef.h"
caryclark@google.comad65a3e2013-04-15 19:13:59 +000096DEFINE_TESTCLASS_SHORT(PathOpsCubicLineIntersectionTest)
caryclark@google.com07e97fc2013-07-08 17:17:02 +000097
98DEFINE_TESTCLASS_SHORT(PathOpsCubicLineIntersectionTestOne)