Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
PathOpsQuadLineIntersectionThreadedTest.cpp
Go to the documentation of this file.
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 */
19#include "tests/Test.h"
20
21#include <utility>
22
23static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line,
24 bool& flipped) {
25 int result;
26 flipped = false;
27 if (line[0].fX == line[1].fX) {
28 double top = line[0].fY;
29 double bottom = line[1].fY;
30 flipped = top > bottom;
31 if (flipped) {
32 using std::swap;
33 swap(top, bottom);
34 }
35 result = intersections.vertical(quad, top, bottom, line[0].fX, flipped);
36 } else if (line[0].fY == line[1].fY) {
37 double left = line[0].fX;
38 double right = line[1].fX;
39 flipped = left > right;
40 if (flipped) {
41 using std::swap;
42 swap(left, right);
43 }
44 result = intersections.horizontal(quad, left, right, line[0].fY, flipped);
45 } else {
46 intersections.intersect(quad, line);
47 result = intersections.used();
48 }
49 return result;
50}
51
53 const SkDLine& line, const double x, const double y) {
54 SkString pathStr;
55 pathStr.appendf(" path.moveTo(%1.9g, %1.9g);\n", quad[0].fX, quad[0].fY);
56 pathStr.appendf(" path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n", quad[1].fX,
57 quad[1].fY, quad[2].fX, quad[2].fY);
58 pathStr.appendf(" path.moveTo(%1.9g, %1.9g);\n", line[0].fX, line[0].fY);
59 pathStr.appendf(" path.lineTo(%1.9g, %1.9g);\n", line[1].fX, line[1].fY);
60
61 SkIntersections intersections;
62 bool flipped = false;
63 int result = doIntersect(intersections, quad, line, flipped);
64 bool found = false;
65 for (int index = 0; index < result; ++index) {
66 double quadT = intersections[0][index];
67 SkDPoint quadXY = quad.ptAtT(quadT);
68 double lineT = intersections[1][index];
69 SkDPoint lineXY = line.ptAtT(lineT);
70 if (quadXY.approximatelyEqual(lineXY)) {
71 found = true;
72 }
73 }
75}
76
77// find a point on a quad by choosing a t from 0 to 1
78// create a vertical span above and below the point
79// verify that intersecting the vertical span and the quad returns t
80// verify that a vertical span starting at quad[0] intersects at t=0
81// verify that a vertical span starting at quad[2] intersects at t=1
83{
85 REPORTER_ASSERT(state.fReporter, data);
86 int ax = state.fA & 0x03;
87 int ay = state.fA >> 2;
88 int bx = state.fB & 0x03;
89 int by = state.fB >> 2;
90 int cx = state.fC & 0x03;
91 int cy = state.fC >> 2;
92 QuadPts q = {{{(double) ax, (double) ay}, {(double) bx, (double) by},
93 {(double) cx, (double) cy}}};
94 SkDQuad quad;
95 quad.debugSet(q.fPts);
96 SkReduceOrder reducer;
97 int order = reducer.reduce(quad);
98 if (order < 3) {
99 return;
100 }
101 for (int tIndex = 0; tIndex <= 4; ++tIndex) {
102 SkDPoint xy = quad.ptAtT(tIndex / 4.0);
103 for (int h = -2; h <= 2; ++h) {
104 for (int v = -2; v <= 2; ++v) {
105 if (h == v && SkTAbs(h) != 1) {
106 continue;
107 }
108 double x = xy.fX;
109 double y = xy.fY;
110 SkDLine line = {{{x - h, y - v}, {x, y}}};
111 testLineIntersect(state.fReporter, quad, line, x, y);
112 state.fReporter->bumpTestCount();
113 SkDLine line2 = {{{x, y}, {x + h, y + v}}};
114 testLineIntersect(state.fReporter, quad, line2, x, y);
115 state.fReporter->bumpTestCount();
116 SkDLine line3 = {{{x - h, y - v}, {x + h, y + v}}};
117 testLineIntersect(state.fReporter, quad, line3, x, y);
118 state.fReporter->bumpTestCount();
119 }
120 }
121 }
122}
123
124DEF_TEST(PathOpsQuadLineIntersectionThreaded, reporter) {
125 initializeTests(reporter, "testQuadLineIntersect");
127 for (int a = 0; a < 16; ++a) {
128 for (int b = 0 ; b < 16; ++b) {
129 for (int c = 0 ; c < 16; ++c) {
130 *testRunner.fRunnables.append() = new PathOpsThreadedRunnable(
131 &testQuadLineIntersectMain, a, b, c, 0, &testRunner);
132 }
133 if (!reporter->allowExtendedTest()) goto finish;
134 }
135 }
136finish:
137 testRunner.render();
138}
reporter
void initializeTests(skiatest::Reporter *reporter, const char *test)
static int doIntersect(SkIntersections &intersections, const SkDQuad &quad, const SkDLine &line, bool &flipped)
static void testLineIntersect(skiatest::Reporter *reporter, const SkDQuad &quad, const SkDLine &line, const double x, const double y)
static void testQuadLineIntersectMain(PathOpsThreadState *data)
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition SkRefCnt.h:341
static T SkTAbs(T value)
Definition SkTemplates.h:43
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
SkTDArray< PathOpsThreadedRunnable * > fRunnables
int intersect(const SkDLine &, const SkDLine &)
int vertical(const SkDLine &, double top, double bottom, double x, bool flipped)
int horizontal(const SkDLine &, double left, double right, double y, bool flipped)
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:550
T * append()
Definition SkTDArray.h:191
static bool b
struct MyStruct a[10]
AtkStateType state
GAsyncResult * result
double y
double x
SkScalar h
SkDPoint fPts[kPointCount]
bool approximatelyEqual(const SkDPoint &a) const
void debugSet(const SkDPoint *pts)
SkDPoint ptAtT(double t) const
int reduce(const SkDCubic &cubic, Quadratics)