bool isLinear(const Cubic& cubic, int startIndex, int endIndex) { LineParameters lineParameters; lineParameters.cubicEndPoints(cubic, startIndex, endIndex); double normalSquared = lineParameters.normalSquared(); double distance[2]; // distance is not normalized int mask = other_two(startIndex, endIndex); int inner1 = startIndex ^ mask; int inner2 = endIndex ^ mask; lineParameters.controlPtDistance(cubic, inner1, inner2, distance); double limit = normalSquared; int index; for (index = 0; index < 2; ++index) { double distSq = distance[index]; distSq *= distSq; if (approximately_greater(distSq, limit)) { return false; } } return true; }
void LineParameter_Test() { for (size_t index = firstLineParameterTest; index < tests_count; ++index) { LineParameters lineParameters; const Cubic& cubic = tests[index]; lineParameters.cubicEndPoints(cubic); double denormalizedDistance[2]; denormalizedDistance[0] = lineParameters.controlPtDistance(cubic, 1); denormalizedDistance[1] = lineParameters.controlPtDistance(cubic, 2); double normalSquared = lineParameters.normalSquared(); size_t inner; for (inner = 0; inner < 2; ++inner) { double distSq = denormalizedDistance[inner]; distSq *= distSq; double answersSq = answers[index][inner]; answersSq *= answersSq; if (AlmostEqualUlps(distSq, normalSquared * answersSq)) { continue; } SkDebugf("%s [%d,%d] denormalizedDistance:%g != answer:%g" " distSq:%g answerSq:%g normalSquared:%g\n", __FUNCTION__, (int)index, (int)inner, denormalizedDistance[inner], answers[index][inner], distSq, answersSq, normalSquared); } lineParameters.normalize(); double normalizedDistance[2]; normalizedDistance[0] = lineParameters.controlPtDistance(cubic, 1); normalizedDistance[1] = lineParameters.controlPtDistance(cubic, 2); for (inner = 0; inner < 2; ++inner) { if (AlmostEqualUlps(fabs(normalizedDistance[inner]), answers[index][inner])) { continue; } SkDebugf("%s [%d,%d] normalizedDistance:%1.10g != answer:%g\n", __FUNCTION__, (int)index, (int)inner, normalizedDistance[inner], answers[index][inner]); } } }
static int check_linear(const Cubic& cubic, Cubic& reduction, int minX, int maxX, int minY, int maxY) { int startIndex = 0; int endIndex = 3; while (cubic[startIndex].approximatelyEqual(cubic[endIndex])) { --endIndex; if (endIndex == 0) { printf("%s shouldn't get here if all four points are about equal", __FUNCTION__); assert(0); } } LineParameters lineParameters; lineParameters.cubicEndPoints(cubic, startIndex, endIndex); double normalSquared = lineParameters.normalSquared(); double distance[2]; // distance is not normalized int mask = other_two(startIndex, endIndex); int inner1 = startIndex ^ mask; int inner2 = endIndex ^ mask; lineParameters.controlPtDistance(cubic, inner1, inner2, distance); double limit = normalSquared * SquaredEpsilon; int index; for (index = 0; index < 2; ++index) { double distSq = distance[index]; distSq *= distSq; if (distSq > limit) { return 0; } } // four are colinear: return line formed by outside reduction[0] = cubic[0]; reduction[1] = cubic[3]; int sameSide1; int sameSide2; bool useX = cubic[maxX].x - cubic[minX].x >= cubic[maxY].y - cubic[minY].y; if (useX) { sameSide1 = sign(cubic[0].x - cubic[1].x) + sign(cubic[3].x - cubic[1].x); sameSide2 = sign(cubic[0].x - cubic[2].x) + sign(cubic[3].x - cubic[2].x); } else { sameSide1 = sign(cubic[0].y - cubic[1].y) + sign(cubic[3].y - cubic[1].y); sameSide2 = sign(cubic[0].y - cubic[2].y) + sign(cubic[3].y - cubic[2].y); } if (sameSide1 == sameSide2 && (sameSide1 & 3) != 2) { return 2; } double tValues[2]; int roots; if (useX) { roots = SkFindCubicExtrema(cubic[0].x, cubic[1].x, cubic[2].x, cubic[3].x, tValues); } else { roots = SkFindCubicExtrema(cubic[0].y, cubic[1].y, cubic[2].y, cubic[3].y, tValues); } for (index = 0; index < roots; ++index) { _Point extrema; extrema.x = interp_cubic_coords(&cubic[0].x, tValues[index]); extrema.y = interp_cubic_coords(&cubic[0].y, tValues[index]); // sameSide > 0 means mid is smaller than either [0] or [3], so replace smaller int replace; if (useX) { if (extrema.x < cubic[0].x ^ extrema.x < cubic[3].x) { continue; } replace = (extrema.x < cubic[0].x | extrema.x < cubic[3].x) ^ cubic[0].x < cubic[3].x; } else { if (extrema.y < cubic[0].y ^ extrema.y < cubic[3].y) { continue; } replace = (extrema.y < cubic[0].y | extrema.y < cubic[3].y) ^ cubic[0].y < cubic[3].y; } reduction[replace] = extrema; } return 2; }