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