1/*
2 * Copyright 2014 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 "SkTDArray.h"
10#include "Test.h"
11
12// check intersections for consistency
13
14struct Curve {
15    int ptCount;
16    CubicPts curve;  // largest can hold lines / quads/ cubics
17};
18
19static const Curve testSet0[] = {  // extracted from skpClip2
20    {4, {{{134,11414}, {131.990234,11414}, {130.32666,11415.4824}, {130.042755,11417.4131}}} },
21    {4, {{{130.042755,11417.4131}, {130.233124,11418.3193}, {131.037079,11419}, {132,11419}}} },
22    {4, {{{132,11419}, {130.895432,11419}, {130,11418.1045}, {130,11417}}} },
23};
24
25static const Curve testSet1[] = {  // extracted from cubicOp85i
26    {4, {{{3,4}, {1,5}, {4,3}, {6,4}}} },
27    {1, {{{6,4}, {3,4}}} },
28    {4, {{{3,4}, {4,6}, {4,3}, {5,1}}} },
29    {1, {{{5,1}, {3,4}}} },
30};
31
32static const struct TestSet {
33    const Curve* tests;
34    int testCount;
35} testSets[] = {
36    { testSet0, (int) SK_ARRAY_COUNT(testSet0) },
37    { testSet1, (int) SK_ARRAY_COUNT(testSet1) },
38};
39
40static const int testSetsCount = (int) SK_ARRAY_COUNT(testSets);
41
42static void testSetTest(skiatest::Reporter* reporter, int index) {
43    const TestSet& testSet = testSets[index];
44    int testCount = testSet.testCount;
45    SkASSERT(testCount > 1);
46    SkTDArray<SkIntersections> combos;
47    for (int outer = 0; outer < testCount - 1; ++outer) {
48        const Curve& oTest = testSet.tests[outer];
49        for (int inner = outer + 1; inner < testCount; ++inner) {
50            const Curve& iTest = testSet.tests[inner];
51            SkIntersections* i = combos.append();
52            sk_bzero(i, sizeof(SkIntersections));
53            SkDLine oLine = {{ oTest.curve.fPts[0], oTest.curve.fPts[1] }};
54            SkDLine iLine = {{ iTest.curve.fPts[0], iTest.curve.fPts[1] }};
55            SkDCubic iCurve, oCurve;
56            iCurve.debugSet(iTest.curve.fPts);
57            oCurve.debugSet(oTest.curve.fPts);
58            if (oTest.ptCount == 1 && iTest.ptCount == 1) {
59                i->intersect(oLine, iLine);
60            } else if (oTest.ptCount == 1 && iTest.ptCount == 4) {
61                i->intersect(iCurve, oLine);
62            } else if (oTest.ptCount == 4 && iTest.ptCount == 1) {
63                i->intersect(oCurve, iLine);
64            } else if (oTest.ptCount == 4 && iTest.ptCount == 4) {
65                i->intersect(oCurve, iCurve);
66            } else {
67                SkASSERT(0);
68            }
69//            i->dump();
70        }
71    }
72}
73
74DEF_TEST(PathOpsThreeWay, reporter) {
75    for (int index = 0; index < testSetsCount; ++index) {
76        testSetTest(reporter, index);
77        reporter->bumpTestCount();
78    }
79}
80
81DEF_TEST(PathOpsThreeWayOneOff, reporter) {
82    int index = 0;
83    testSetTest(reporter, index);
84}
85