2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #include "SkPathOpsCubic.h"
8 #include "SkPathOpsLine.h"
9 #include "SkPathOpsQuad.h"
12 // computer-aided design - volume 22 number 9 november 1990 pp 538 - 549
13 // online at http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf
15 // This turns a line segment into a parameterized line, of the form
17 // When a^2 + b^2 == 1, the line is normalized.
18 // The distance to the line for (x, y) is d(x,y) = ax + by + c
20 // Note that the distances below are not necessarily normalized. To get the true
21 // distance, it's necessary to either call normalize() after xxxEndPoints(), or
22 // divide the result of xxxDistance() by sqrt(normalSquared())
24 class SkLineParameters {
27 bool cubicEndPoints(const SkDCubic& pts) {
29 cubicEndPoints(pts, 0, endIndex);
34 cubicEndPoints(pts, 0, ++endIndex);
35 SkASSERT(endIndex == 2);
40 cubicEndPoints(pts, 0, ++endIndex); // line
41 SkASSERT(endIndex == 3);
45 // FIXME: after switching to round sort, remove bumping fA
46 if (dx() < 0) { // only worry about y bias when breaking cw/ccw tie
49 // if cubic tangent is on x axis, look at next control point to break tie
50 // control point may be approximate, so it must move significantly to account for error
51 if (NotAlmostEqualUlps(pts[0].fY, pts[++endIndex].fY)) {
52 if (pts[0].fY > pts[endIndex].fY) {
53 fA = DBL_EPSILON; // push it from 0 to slightly negative (y() returns -a)
60 SkASSERT(endIndex == 2);
61 if (pts[0].fY > pts[3].fY) {
62 fA = DBL_EPSILON; // push it from 0 to slightly negative (y() returns -a)
67 void cubicEndPoints(const SkDCubic& pts, int s, int e) {
68 fA = pts[s].fY - pts[e].fY;
69 fB = pts[e].fX - pts[s].fX;
70 fC = pts[s].fX * pts[e].fY - pts[e].fX * pts[s].fY;
73 double cubicPart(const SkDCubic& part) {
75 if (part[0] == part[1] || ((const SkDLine& ) part[0]).nearRay(part[2])) {
76 return pointDistance(part[3]);
78 return pointDistance(part[2]);
81 void lineEndPoints(const SkDLine& pts) {
82 fA = pts[0].fY - pts[1].fY;
83 fB = pts[1].fX - pts[0].fX;
84 fC = pts[0].fX * pts[1].fY - pts[1].fX * pts[0].fY;
87 bool quadEndPoints(const SkDQuad& pts) {
88 quadEndPoints(pts, 0, 1);
93 quadEndPoints(pts, 0, 2);
96 if (dx() < 0) { // only worry about y bias when breaking cw/ccw tie
99 // FIXME: after switching to round sort, remove this
100 if (pts[0].fY > pts[2].fY) {
106 void quadEndPoints(const SkDQuad& pts, int s, int e) {
107 fA = pts[s].fY - pts[e].fY;
108 fB = pts[e].fX - pts[s].fX;
109 fC = pts[s].fX * pts[e].fY - pts[e].fX * pts[s].fY;
112 double quadPart(const SkDQuad& part) {
114 return pointDistance(part[2]);
117 double normalSquared() const {
118 return fA * fA + fB * fB;
122 double normal = sqrt(normalSquared());
123 if (approximately_zero(normal)) {
127 double reciprocal = 1 / normal;
134 void cubicDistanceY(const SkDCubic& pts, SkDCubic& distance) const {
135 double oneThird = 1 / 3.0;
136 for (int index = 0; index < 4; ++index) {
137 distance[index].fX = index * oneThird;
138 distance[index].fY = fA * pts[index].fX + fB * pts[index].fY + fC;
142 void quadDistanceY(const SkDQuad& pts, SkDQuad& distance) const {
143 double oneHalf = 1 / 2.0;
144 for (int index = 0; index < 3; ++index) {
145 distance[index].fX = index * oneHalf;
146 distance[index].fY = fA * pts[index].fX + fB * pts[index].fY + fC;
150 double controlPtDistance(const SkDCubic& pts, int index) const {
151 SkASSERT(index == 1 || index == 2);
152 return fA * pts[index].fX + fB * pts[index].fY + fC;
155 double controlPtDistance(const SkDQuad& pts) const {
156 return fA * pts[1].fX + fB * pts[1].fY + fC;
159 double pointDistance(const SkDPoint& pt) const {
160 return fA * pt.fX + fB * pt.fY + fC;