2012-08-27 14:11:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2012-02-03 22:07:47 +00:00
|
|
|
#include "CurveIntersection.h"
|
2012-07-02 20:27:02 +00:00
|
|
|
#include "CurveUtilities.h"
|
2012-08-20 12:43:57 +00:00
|
|
|
#include "EdgeWalker_Test.h"
|
2012-01-25 18:57:23 +00:00
|
|
|
#include "Intersection_Tests.h"
|
|
|
|
#include "Intersections.h"
|
|
|
|
#include "TestUtilities.h"
|
|
|
|
|
|
|
|
struct lineQuad {
|
|
|
|
Quadratic quad;
|
|
|
|
_Line line;
|
2012-08-21 13:13:52 +00:00
|
|
|
int result;
|
|
|
|
_Point expected[2];
|
2012-01-25 18:57:23 +00:00
|
|
|
} lineQuadTests[] = {
|
2012-08-21 13:13:52 +00:00
|
|
|
// quad line results
|
|
|
|
{{{1, 1}, {2, 1}, {0, 2}}, {{0, 0}, {1, 1}}, 1, {{1, 1} }},
|
|
|
|
{{{0, 0}, {1, 1}, {3, 1}}, {{0, 0}, {3, 1}}, 2, {{0, 0}, {3, 1}}},
|
|
|
|
{{{2, 0}, {1, 1}, {2, 2}}, {{0, 0}, {0, 2}}, 0 },
|
2012-08-23 18:14:13 +00:00
|
|
|
{{{4, 0}, {0, 1}, {4, 2}}, {{3, 1}, {4, 1}}, 0, },
|
2012-08-21 13:13:52 +00:00
|
|
|
{{{0, 0}, {0, 1}, {1, 1}}, {{0, 1}, {1, 0}}, 1, {{.25, .75} }},
|
2012-01-25 18:57:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t lineQuadTests_count = sizeof(lineQuadTests) / sizeof(lineQuadTests[0]);
|
|
|
|
|
|
|
|
const int firstLineQuadIntersectionTest = 0;
|
|
|
|
|
2012-08-21 13:13:52 +00:00
|
|
|
static int doIntersect(Intersections& intersections, const Quadratic& quad, const _Line& line, bool& flipped) {
|
|
|
|
int result;
|
|
|
|
flipped = false;
|
|
|
|
if (line[0].x == line[1].x) {
|
|
|
|
double top = line[0].y;
|
|
|
|
double bottom = line[1].y;
|
|
|
|
flipped = top > bottom;
|
|
|
|
if (flipped) {
|
|
|
|
SkTSwap<double>(top, bottom);
|
|
|
|
}
|
|
|
|
result = verticalIntersect(quad, top, bottom, line[0].x, flipped, intersections);
|
|
|
|
} else if (line[0].y == line[1].y) {
|
|
|
|
double left = line[0].x;
|
|
|
|
double right = line[1].x;
|
|
|
|
flipped = left > right;
|
|
|
|
if (flipped) {
|
|
|
|
SkTSwap<double>(left, right);
|
|
|
|
}
|
|
|
|
result = horizontalIntersect(quad, left, right, line[0].y, flipped, intersections);
|
|
|
|
} else {
|
|
|
|
intersect(quad, line, intersections);
|
|
|
|
result = intersections.fUsed;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-01-25 18:57:23 +00:00
|
|
|
void LineQuadraticIntersection_Test() {
|
|
|
|
for (size_t index = firstLineQuadIntersectionTest; index < lineQuadTests_count; ++index) {
|
|
|
|
const Quadratic& quad = lineQuadTests[index].quad;
|
|
|
|
const _Line& line = lineQuadTests[index].line;
|
|
|
|
Quadratic reduce1;
|
|
|
|
_Line reduce2;
|
|
|
|
int order1 = reduceOrder(quad, reduce1);
|
|
|
|
int order2 = reduceOrder(line, reduce2);
|
|
|
|
if (order1 < 3) {
|
2012-08-21 13:13:52 +00:00
|
|
|
SkDebugf("%s [%d] quad order=%d\n", __FUNCTION__, (int) index, order1);
|
|
|
|
SkASSERT(0);
|
2012-01-25 18:57:23 +00:00
|
|
|
}
|
|
|
|
if (order2 < 2) {
|
2012-08-21 13:13:52 +00:00
|
|
|
SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, (int) index, order2);
|
|
|
|
SkASSERT(0);
|
|
|
|
}
|
|
|
|
Intersections intersections;
|
|
|
|
bool flipped = false;
|
|
|
|
int result = doIntersect(intersections, quad, line, flipped);
|
|
|
|
SkASSERT(result == lineQuadTests[index].result);
|
|
|
|
if (!intersections.intersected()) {
|
|
|
|
continue;
|
2012-01-25 18:57:23 +00:00
|
|
|
}
|
2012-08-21 13:13:52 +00:00
|
|
|
for (int pt = 0; pt < result; ++pt) {
|
|
|
|
double tt1 = intersections.fT[0][pt];
|
|
|
|
SkASSERT(tt1 >= 0 && tt1 <= 1);
|
|
|
|
_Point t1, t2;
|
|
|
|
xy_at_t(quad, tt1, t1.x, t1.y);
|
|
|
|
double tt2 = intersections.fT[1][pt];
|
|
|
|
SkASSERT(tt2 >= 0 && tt2 <= 1);
|
|
|
|
xy_at_t(line, tt2, t2.x, t2.y);
|
|
|
|
if (!approximately_equal(t1.x, t2.x)) {
|
|
|
|
SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
|
|
|
|
__FUNCTION__, (int)index, pt, tt1, t1.x, t1.y, tt2, t2.x, t2.y);
|
|
|
|
SkASSERT(0);
|
|
|
|
}
|
|
|
|
if (!approximately_equal(t1.y, t2.y)) {
|
|
|
|
SkDebugf("%s [%d,%d] y!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
|
|
|
|
__FUNCTION__, (int)index, pt, tt1, t1.x, t1.y, tt2, t2.x, t2.y);
|
|
|
|
SkASSERT(0);
|
|
|
|
}
|
|
|
|
if (!t1.approximatelyEqual(lineQuadTests[index].expected[0])
|
|
|
|
&& (lineQuadTests[index].result == 1
|
|
|
|
|| !t1.approximatelyEqual(lineQuadTests[index].expected[1]))) {
|
|
|
|
SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.x, t1.y);
|
|
|
|
SkASSERT(0);
|
2012-01-25 18:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-20 12:43:57 +00:00
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
static void testLineIntersect(State4& state, const Quadratic& quad, const _Line& line,
|
2012-08-20 12:43:57 +00:00
|
|
|
const double x, const double y) {
|
|
|
|
char pathStr[1024];
|
|
|
|
bzero(pathStr, sizeof(pathStr));
|
|
|
|
char* str = pathStr;
|
|
|
|
str += sprintf(str, " path.moveTo(%1.9g, %1.9g);\n", quad[0].x, quad[0].y);
|
|
|
|
str += sprintf(str, " path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n", quad[1].x, quad[1].y, quad[2].x, quad[2].y);
|
|
|
|
str += sprintf(str, " path.moveTo(%1.9g, %1.9g);\n", line[0].x, line[0].y);
|
|
|
|
str += sprintf(str, " path.lineTo(%1.9g, %1.9g);\n", line[1].x, line[1].y);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-08-20 12:43:57 +00:00
|
|
|
Intersections intersections;
|
|
|
|
bool flipped = false;
|
2012-08-21 13:13:52 +00:00
|
|
|
int result = doIntersect(intersections, quad, line, flipped);
|
2012-08-20 12:43:57 +00:00
|
|
|
bool found = false;
|
|
|
|
for (int index = 0; index < result; ++index) {
|
|
|
|
double quadT = intersections.fT[0][index];
|
|
|
|
double quadX, quadY;
|
|
|
|
xy_at_t(quad, quadT, quadX, quadY);
|
|
|
|
double lineT = intersections.fT[1][index];
|
|
|
|
double lineX, lineY;
|
|
|
|
xy_at_t(line, lineT, lineX, lineY);
|
|
|
|
if (fabs(quadX - lineX) < FLT_EPSILON && fabs(quadY - lineY) < FLT_EPSILON
|
|
|
|
&& fabs(x - lineX) < FLT_EPSILON && fabs(y - lineY) < FLT_EPSILON) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkASSERT(found);
|
|
|
|
state.testsRun++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// find a point on a quad by choosing a t from 0 to 1
|
|
|
|
// create a vertical span above and below the point
|
|
|
|
// verify that intersecting the vertical span and the quad returns t
|
|
|
|
// verify that a vertical span starting at quad[0] intersects at t=0
|
|
|
|
// verify that a vertical span starting at quad[2] intersects at t=1
|
|
|
|
static void* testQuadLineIntersectMain(void* data)
|
|
|
|
{
|
|
|
|
SkASSERT(data);
|
|
|
|
State4& state = *(State4*) data;
|
|
|
|
do {
|
|
|
|
int ax = state.a & 0x03;
|
|
|
|
int ay = state.a >> 2;
|
|
|
|
int bx = state.b & 0x03;
|
|
|
|
int by = state.b >> 2;
|
|
|
|
int cx = state.c & 0x03;
|
|
|
|
int cy = state.c >> 2;
|
|
|
|
Quadratic quad = {{ax, ay}, {bx, by}, {cx, cy}};
|
|
|
|
Quadratic reduced;
|
|
|
|
int order = reduceOrder(quad, reduced);
|
|
|
|
if (order < 3) {
|
|
|
|
continue; // skip degenerates
|
|
|
|
}
|
|
|
|
for (int tIndex = 0; tIndex <= 4; ++tIndex) {
|
|
|
|
double x, y;
|
|
|
|
xy_at_t(quad, tIndex / 4.0, x, y);
|
|
|
|
for (int h = -2; h <= 2; ++h) {
|
|
|
|
for (int v = -2; v <= 2; ++v) {
|
|
|
|
if (h == v && abs(h) != 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_Line line = {{x - h, y - v}, {x, y}};
|
|
|
|
testLineIntersect(state, quad, line, x, y);
|
|
|
|
_Line line2 = {{x, y}, {x + h, y + v}};
|
|
|
|
testLineIntersect(state, quad, line2, x, y);
|
|
|
|
_Line line3 = {{x - h, y - v}, {x + h, y + v}};
|
|
|
|
testLineIntersect(state, quad, line3, x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (runNextTestSet(state));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuadLineIntersectThreaded_Test(int& testsRun)
|
|
|
|
{
|
|
|
|
SkDebugf("%s\n", __FUNCTION__);
|
|
|
|
const char testStr[] = "testQuadLineIntersect";
|
|
|
|
initializeTests(testStr, sizeof(testStr));
|
|
|
|
int testsStart = testsRun;
|
|
|
|
for (int a = 0; a < 16; ++a) {
|
|
|
|
for (int b = 0 ; b < 16; ++b) {
|
|
|
|
for (int c = 0 ; c < 16; ++c) {
|
|
|
|
testsRun += dispatchTest4(testQuadLineIntersectMain,
|
|
|
|
a, b, c, 0);
|
|
|
|
}
|
|
|
|
if (!gRunTestsInOneThread) SkDebugf(".");
|
|
|
|
}
|
|
|
|
if (!gRunTestsInOneThread) SkDebugf("%d", a);
|
|
|
|
}
|
|
|
|
testsRun += waitForCompletion();
|
|
|
|
SkDebugf("\n%s tests=%d total=%d\n", __FUNCTION__, testsRun - testsStart, testsRun);
|
|
|
|
}
|