blob: 27c7a29bf299be19e9fc2d1dce32f2ba587f919c [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
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 */
caryclark@google.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com639df892012-01-10 21:46:10 +00008#include "Extrema.h"
9#include "IntersectionUtilities.h"
10#include "LineParameters.h"
11
12static double interp_quad_coords(double a, double b, double c, double t)
13{
14 double ab = interp(a, b, t);
15 double bc = interp(b, c, t);
16 return interp(ab, bc, t);
17}
18
19static int coincident_line(const Quadratic& quad, Quadratic& reduction) {
20 reduction[0] = reduction[1] = quad[0];
21 return 1;
22}
23
caryclark@google.com47d73da2013-02-17 01:41:25 +000024static int vertical_line(const Quadratic& quad, ReduceOrder_Styles reduceStyle,
25 Quadratic& reduction) {
caryclark@google.com639df892012-01-10 21:46:10 +000026 double tValue;
27 reduction[0] = quad[0];
28 reduction[1] = quad[2];
caryclark@google.com47d73da2013-02-17 01:41:25 +000029 if (reduceStyle == kReduceOrder_TreatAsFill) {
30 return 2;
31 }
caryclark@google.com639df892012-01-10 21:46:10 +000032 int smaller = reduction[1].y > reduction[0].y;
33 int larger = smaller ^ 1;
caryclark@google.comfa0588f2012-04-26 21:01:06 +000034 if (findExtrema(quad[0].y, quad[1].y, quad[2].y, &tValue)) {
caryclark@google.com639df892012-01-10 21:46:10 +000035 double yExtrema = interp_quad_coords(quad[0].y, quad[1].y, quad[2].y, tValue);
36 if (reduction[smaller].y > yExtrema) {
37 reduction[smaller].y = yExtrema;
38 } else if (reduction[larger].y < yExtrema) {
39 reduction[larger].y = yExtrema;
40 }
41 }
42 return 2;
43}
44
caryclark@google.com47d73da2013-02-17 01:41:25 +000045static int horizontal_line(const Quadratic& quad, ReduceOrder_Styles reduceStyle,
46 Quadratic& reduction) {
caryclark@google.com639df892012-01-10 21:46:10 +000047 double tValue;
48 reduction[0] = quad[0];
49 reduction[1] = quad[2];
caryclark@google.com47d73da2013-02-17 01:41:25 +000050 if (reduceStyle == kReduceOrder_TreatAsFill) {
51 return 2;
52 }
caryclark@google.com639df892012-01-10 21:46:10 +000053 int smaller = reduction[1].x > reduction[0].x;
54 int larger = smaller ^ 1;
caryclark@google.comfa0588f2012-04-26 21:01:06 +000055 if (findExtrema(quad[0].x, quad[1].x, quad[2].x, &tValue)) {
caryclark@google.com639df892012-01-10 21:46:10 +000056 double xExtrema = interp_quad_coords(quad[0].x, quad[1].x, quad[2].x, tValue);
57 if (reduction[smaller].x > xExtrema) {
58 reduction[smaller].x = xExtrema;
59 } else if (reduction[larger].x < xExtrema) {
60 reduction[larger].x = xExtrema;
61 }
62 }
63 return 2;
64}
65
caryclark@google.com47d73da2013-02-17 01:41:25 +000066static int check_linear(const Quadratic& quad, ReduceOrder_Styles reduceStyle,
67 int minX, int maxX, int minY, int maxY, Quadratic& reduction) {
caryclark@google.com639df892012-01-10 21:46:10 +000068 int startIndex = 0;
69 int endIndex = 2;
70 while (quad[startIndex].approximatelyEqual(quad[endIndex])) {
71 --endIndex;
72 if (endIndex == 0) {
73 printf("%s shouldn't get here if all four points are about equal", __FUNCTION__);
caryclark@google.comaa358312013-01-29 20:28:49 +000074 SkASSERT(0);
caryclark@google.com639df892012-01-10 21:46:10 +000075 }
76 }
caryclark@google.com15fa1382012-05-07 20:49:36 +000077 if (!isLinear(quad, startIndex, endIndex)) {
caryclark@google.com639df892012-01-10 21:46:10 +000078 return 0;
79 }
80 // four are colinear: return line formed by outside
81 reduction[0] = quad[0];
82 reduction[1] = quad[2];
caryclark@google.com47d73da2013-02-17 01:41:25 +000083 if (reduceStyle == kReduceOrder_TreatAsFill) {
84 return 2;
85 }
caryclark@google.com639df892012-01-10 21:46:10 +000086 int sameSide;
87 bool useX = quad[maxX].x - quad[minX].x >= quad[maxY].y - quad[minY].y;
88 if (useX) {
89 sameSide = sign(quad[0].x - quad[1].x) + sign(quad[2].x - quad[1].x);
90 } else {
91 sameSide = sign(quad[0].y - quad[1].y) + sign(quad[2].y - quad[1].y);
92 }
93 if ((sameSide & 3) != 2) {
94 return 2;
95 }
96 double tValue;
97 int root;
98 if (useX) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +000099 root = findExtrema(quad[0].x, quad[1].x, quad[2].x, &tValue);
caryclark@google.com639df892012-01-10 21:46:10 +0000100 } else {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000101 root = findExtrema(quad[0].y, quad[1].y, quad[2].y, &tValue);
caryclark@google.com639df892012-01-10 21:46:10 +0000102 }
103 if (root) {
104 _Point extrema;
105 extrema.x = interp_quad_coords(quad[0].x, quad[1].x, quad[2].x, tValue);
caryclark@google.com9f602912013-01-24 21:47:16 +0000106 extrema.y = interp_quad_coords(quad[0].y, quad[1].y, quad[2].y, tValue);
caryclark@google.com639df892012-01-10 21:46:10 +0000107 // sameSide > 0 means mid is smaller than either [0] or [2], so replace smaller
108 int replace;
109 if (useX) {
110 if (extrema.x < quad[0].x ^ extrema.x < quad[2].x) {
111 return 2;
112 }
113 replace = (extrema.x < quad[0].x | extrema.x < quad[2].x)
caryclark@google.com9f3e9a52012-12-10 12:50:53 +0000114 ^ (quad[0].x < quad[2].x);
caryclark@google.com639df892012-01-10 21:46:10 +0000115 } else {
116 if (extrema.y < quad[0].y ^ extrema.y < quad[2].y) {
117 return 2;
118 }
119 replace = (extrema.y < quad[0].y | extrema.y < quad[2].y)
caryclark@google.com9f3e9a52012-12-10 12:50:53 +0000120 ^ (quad[0].y < quad[2].y);
caryclark@google.com639df892012-01-10 21:46:10 +0000121 }
122 reduction[replace] = extrema;
123 }
124 return 2;
125}
126
caryclark@google.com15fa1382012-05-07 20:49:36 +0000127bool isLinear(const Quadratic& quad, int startIndex, int endIndex) {
128 LineParameters lineParameters;
129 lineParameters.quadEndPoints(quad, startIndex, endIndex);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000130 // FIXME: maybe it's possible to avoid this and compare non-normalized
131 lineParameters.normalize();
132 double distance = lineParameters.controlPtDistance(quad);
133 return approximately_zero(distance);
caryclark@google.com15fa1382012-05-07 20:49:36 +0000134}
135
caryclark@google.com639df892012-01-10 21:46:10 +0000136// reduce to a quadratic or smaller
137// look for identical points
rmistry@google.comd6176b02012-08-23 18:14:13 +0000138// look for all four points in a line
caryclark@google.com639df892012-01-10 21:46:10 +0000139 // note that three points in a line doesn't simplify a cubic
140// look for approximation with single quadratic
141 // save approximation with multiple quadratics for later
caryclark@google.com47d73da2013-02-17 01:41:25 +0000142int reduceOrder(const Quadratic& quad, Quadratic& reduction, ReduceOrder_Styles reduceStyle) {
caryclark@google.com639df892012-01-10 21:46:10 +0000143 int index, minX, maxX, minY, maxY;
144 int minXSet, minYSet;
145 minX = maxX = minY = maxY = 0;
146 minXSet = minYSet = 0;
147 for (index = 1; index < 3; ++index) {
148 if (quad[minX].x > quad[index].x) {
149 minX = index;
150 }
151 if (quad[minY].y > quad[index].y) {
152 minY = index;
153 }
154 if (quad[maxX].x < quad[index].x) {
155 maxX = index;
156 }
157 if (quad[maxY].y < quad[index].y) {
158 maxY = index;
159 }
160 }
161 for (index = 0; index < 3; ++index) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000162 if (AlmostEqualUlps(quad[index].x, quad[minX].x)) {
caryclark@google.com639df892012-01-10 21:46:10 +0000163 minXSet |= 1 << index;
164 }
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000165 if (AlmostEqualUlps(quad[index].y, quad[minY].y)) {
caryclark@google.com639df892012-01-10 21:46:10 +0000166 minYSet |= 1 << index;
167 }
168 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000169 if (minXSet == 0x7) { // test for vertical line
170 if (minYSet == 0x7) { // return 1 if all four are coincident
caryclark@google.com639df892012-01-10 21:46:10 +0000171 return coincident_line(quad, reduction);
172 }
caryclark@google.com47d73da2013-02-17 01:41:25 +0000173 return vertical_line(quad, reduceStyle, reduction);
caryclark@google.com639df892012-01-10 21:46:10 +0000174 }
175 if (minYSet == 0xF) { // test for horizontal line
caryclark@google.com47d73da2013-02-17 01:41:25 +0000176 return horizontal_line(quad, reduceStyle, reduction);
caryclark@google.com639df892012-01-10 21:46:10 +0000177 }
caryclark@google.com47d73da2013-02-17 01:41:25 +0000178 int result = check_linear(quad, reduceStyle, minX, maxX, minY, maxY, reduction);
caryclark@google.com639df892012-01-10 21:46:10 +0000179 if (result) {
180 return result;
181 }
182 memcpy(reduction, quad, sizeof(Quadratic));
183 return 3;
184}