michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkPathOpsLine.h" michael@0: michael@0: SkDLine SkDLine::subDivide(double t1, double t2) const { michael@0: SkDVector delta = tangent(); michael@0: SkDLine dst = {{{ michael@0: fPts[0].fX - t1 * delta.fX, fPts[0].fY - t1 * delta.fY}, { michael@0: fPts[0].fX - t2 * delta.fX, fPts[0].fY - t2 * delta.fY}}}; michael@0: return dst; michael@0: } michael@0: michael@0: // may have this below somewhere else already: michael@0: // copying here because I thought it was clever michael@0: michael@0: // Copyright 2001, softSurfer (www.softsurfer.com) michael@0: // This code may be freely used and modified for any purpose michael@0: // providing that this copyright notice is included with it. michael@0: // SoftSurfer makes no warranty for this code, and cannot be held michael@0: // liable for any real or imagined damage resulting from its use. michael@0: // Users of this code must verify correctness for their application. michael@0: michael@0: // Assume that a class is already given for the object: michael@0: // Point with coordinates {float x, y;} michael@0: //=================================================================== michael@0: michael@0: // isLeft(): tests if a point is Left|On|Right of an infinite line. michael@0: // Input: three points P0, P1, and P2 michael@0: // Return: >0 for P2 left of the line through P0 and P1 michael@0: // =0 for P2 on the line michael@0: // <0 for P2 right of the line michael@0: // See: the January 2001 Algorithm on Area of Triangles michael@0: // return (float) ((P1.x - P0.x)*(P2.y - P0.y) - (P2.x - P0.x)*(P1.y - P0.y)); michael@0: double SkDLine::isLeft(const SkDPoint& pt) const { michael@0: SkDVector p0 = fPts[1] - fPts[0]; michael@0: SkDVector p2 = pt - fPts[0]; michael@0: return p0.cross(p2); michael@0: } michael@0: michael@0: SkDPoint SkDLine::ptAtT(double t) const { michael@0: if (0 == t) { michael@0: return fPts[0]; michael@0: } michael@0: if (1 == t) { michael@0: return fPts[1]; michael@0: } michael@0: double one_t = 1 - t; michael@0: SkDPoint result = { one_t * fPts[0].fX + t * fPts[1].fX, one_t * fPts[0].fY + t * fPts[1].fY }; michael@0: return result; michael@0: } michael@0: michael@0: double SkDLine::exactPoint(const SkDPoint& xy) const { michael@0: if (xy == fPts[0]) { // do cheapest test first michael@0: return 0; michael@0: } michael@0: if (xy == fPts[1]) { michael@0: return 1; michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: double SkDLine::nearPoint(const SkDPoint& xy) const { michael@0: if (!AlmostBetweenUlps(fPts[0].fX, xy.fX, fPts[1].fX) michael@0: || !AlmostBetweenUlps(fPts[0].fY, xy.fY, fPts[1].fY)) { michael@0: return -1; michael@0: } michael@0: // project a perpendicular ray from the point to the line; find the T on the line michael@0: SkDVector len = fPts[1] - fPts[0]; // the x/y magnitudes of the line michael@0: double denom = len.fX * len.fX + len.fY * len.fY; // see DLine intersectRay michael@0: SkDVector ab0 = xy - fPts[0]; michael@0: double numer = len.fX * ab0.fX + ab0.fY * len.fY; michael@0: if (!between(0, numer, denom)) { michael@0: return -1; michael@0: } michael@0: double t = numer / denom; michael@0: SkDPoint realPt = ptAtT(t); michael@0: double dist = realPt.distance(xy); // OPTIMIZATION: can we compare against distSq instead ? michael@0: // find the ordinal in the original line with the largest unsigned exponent michael@0: double tiniest = SkTMin(SkTMin(SkTMin(fPts[0].fX, fPts[0].fY), fPts[1].fX), fPts[1].fY); michael@0: double largest = SkTMax(SkTMax(SkTMax(fPts[0].fX, fPts[0].fY), fPts[1].fX), fPts[1].fY); michael@0: largest = SkTMax(largest, -tiniest); michael@0: if (!AlmostEqualUlps(largest, largest + dist)) { // is the dist within ULPS tolerance? michael@0: return -1; michael@0: } michael@0: t = SkPinT(t); michael@0: SkASSERT(between(0, t, 1)); michael@0: return t; michael@0: } michael@0: michael@0: bool SkDLine::nearRay(const SkDPoint& xy) const { michael@0: // project a perpendicular ray from the point to the line; find the T on the line michael@0: SkDVector len = fPts[1] - fPts[0]; // the x/y magnitudes of the line michael@0: double denom = len.fX * len.fX + len.fY * len.fY; // see DLine intersectRay michael@0: SkDVector ab0 = xy - fPts[0]; michael@0: double numer = len.fX * ab0.fX + ab0.fY * len.fY; michael@0: double t = numer / denom; michael@0: SkDPoint realPt = ptAtT(t); michael@0: double dist = realPt.distance(xy); // OPTIMIZATION: can we compare against distSq instead ? michael@0: // find the ordinal in the original line with the largest unsigned exponent michael@0: double tiniest = SkTMin(SkTMin(SkTMin(fPts[0].fX, fPts[0].fY), fPts[1].fX), fPts[1].fY); michael@0: double largest = SkTMax(SkTMax(SkTMax(fPts[0].fX, fPts[0].fY), fPts[1].fX), fPts[1].fY); michael@0: largest = SkTMax(largest, -tiniest); michael@0: return RoughlyEqualUlps(largest, largest + dist); // is the dist within ULPS tolerance? michael@0: } michael@0: michael@0: // Returns true if a ray from (0,0) to (x1,y1) is coincident with a ray (0,0) to (x2,y2) michael@0: // OPTIMIZE: a specialty routine could speed this up -- may not be called very often though michael@0: bool SkDLine::NearRay(double x1, double y1, double x2, double y2) { michael@0: double denom1 = x1 * x1 + y1 * y1; michael@0: double denom2 = x2 * x2 + y2 * y2; michael@0: SkDLine line = {{{0, 0}, {x1, y1}}}; michael@0: SkDPoint pt = {x2, y2}; michael@0: if (denom2 > denom1) { michael@0: SkTSwap(line[1], pt); michael@0: } michael@0: return line.nearRay(pt); michael@0: } michael@0: michael@0: double SkDLine::ExactPointH(const SkDPoint& xy, double left, double right, double y) { michael@0: if (xy.fY == y) { michael@0: if (xy.fX == left) { michael@0: return 0; michael@0: } michael@0: if (xy.fX == right) { michael@0: return 1; michael@0: } michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: double SkDLine::NearPointH(const SkDPoint& xy, double left, double right, double y) { michael@0: if (!AlmostBequalUlps(xy.fY, y)) { michael@0: return -1; michael@0: } michael@0: if (!AlmostBetweenUlps(left, xy.fX, right)) { michael@0: return -1; michael@0: } michael@0: double t = (xy.fX - left) / (right - left); michael@0: t = SkPinT(t); michael@0: SkASSERT(between(0, t, 1)); michael@0: double realPtX = (1 - t) * left + t * right; michael@0: SkDVector distU = {xy.fY - y, xy.fX - realPtX}; michael@0: double distSq = distU.fX * distU.fX + distU.fY * distU.fY; michael@0: double dist = sqrt(distSq); // OPTIMIZATION: can we compare against distSq instead ? michael@0: double tiniest = SkTMin(SkTMin(y, left), right); michael@0: double largest = SkTMax(SkTMax(y, left), right); michael@0: largest = SkTMax(largest, -tiniest); michael@0: if (!AlmostEqualUlps(largest, largest + dist)) { // is the dist within ULPS tolerance? michael@0: return -1; michael@0: } michael@0: return t; michael@0: } michael@0: michael@0: double SkDLine::ExactPointV(const SkDPoint& xy, double top, double bottom, double x) { michael@0: if (xy.fX == x) { michael@0: if (xy.fY == top) { michael@0: return 0; michael@0: } michael@0: if (xy.fY == bottom) { michael@0: return 1; michael@0: } michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: double SkDLine::NearPointV(const SkDPoint& xy, double top, double bottom, double x) { michael@0: if (!AlmostBequalUlps(xy.fX, x)) { michael@0: return -1; michael@0: } michael@0: if (!AlmostBetweenUlps(top, xy.fY, bottom)) { michael@0: return -1; michael@0: } michael@0: double t = (xy.fY - top) / (bottom - top); michael@0: t = SkPinT(t); michael@0: SkASSERT(between(0, t, 1)); michael@0: double realPtY = (1 - t) * top + t * bottom; michael@0: SkDVector distU = {xy.fX - x, xy.fY - realPtY}; michael@0: double distSq = distU.fX * distU.fX + distU.fY * distU.fY; michael@0: double dist = sqrt(distSq); // OPTIMIZATION: can we compare against distSq instead ? michael@0: double tiniest = SkTMin(SkTMin(x, top), bottom); michael@0: double largest = SkTMax(SkTMax(x, top), bottom); michael@0: largest = SkTMax(largest, -tiniest); michael@0: if (!AlmostEqualUlps(largest, largest + dist)) { // is the dist within ULPS tolerance? michael@0: return -1; michael@0: } michael@0: return t; michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void SkDLine::dump() { michael@0: SkDebugf("{{"); michael@0: fPts[0].dump(); michael@0: SkDebugf(", "); michael@0: fPts[1].dump(); michael@0: SkDebugf("}}\n"); michael@0: } michael@0: #endif