blob: 594e12c9f215845d81b24a1304149d21108c6a8a [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "CurveIntersection.h"
caryclark@google.com639df892012-01-10 21:46:10 +00002#include "Intersections.h"
caryclark@google.comc6825902012-02-03 22:07:47 +00003#include "IntersectionUtilities.h"
caryclark@google.com639df892012-01-10 21:46:10 +00004#include "LineIntersection.h"
5
caryclark@google.comc6825902012-02-03 22:07:47 +00006class CubicIntersections : public Intersections {
7public:
8
rmistry@google.comd6176b02012-08-23 18:14:13 +00009CubicIntersections(const Cubic& c1, const Cubic& c2, Intersections& i)
caryclark@google.comc6825902012-02-03 22:07:47 +000010 : cubic1(c1)
11 , cubic2(c2)
12 , intersections(i)
rmistry@google.comd6176b02012-08-23 18:14:13 +000013 , depth(0)
caryclark@google.comc6825902012-02-03 22:07:47 +000014 , splits(0) {
caryclark@google.com639df892012-01-10 21:46:10 +000015}
16
caryclark@google.comc6825902012-02-03 22:07:47 +000017bool intersect() {
caryclark@google.com639df892012-01-10 21:46:10 +000018 double minT1, minT2, maxT1, maxT2;
19 if (!bezier_clip(cubic2, cubic1, minT1, maxT1)) {
20 return false;
21 }
22 if (!bezier_clip(cubic1, cubic2, minT2, maxT2)) {
23 return false;
24 }
caryclark@google.comc6825902012-02-03 22:07:47 +000025 int split;
caryclark@google.com639df892012-01-10 21:46:10 +000026 if (maxT1 - minT1 < maxT2 - minT2) {
27 intersections.swap();
caryclark@google.comc6825902012-02-03 22:07:47 +000028 minT2 = 0;
29 maxT2 = 1;
30 split = maxT1 - minT1 > tClipLimit;
31 } else {
32 minT1 = 0;
33 maxT1 = 1;
34 split = (maxT2 - minT2 > tClipLimit) << 1;
35 }
36 return chop(minT1, maxT1, minT2, maxT2, split);
caryclark@google.com639df892012-01-10 21:46:10 +000037}
caryclark@google.comc6825902012-02-03 22:07:47 +000038
39protected:
rmistry@google.comd6176b02012-08-23 18:14:13 +000040
caryclark@google.comc6825902012-02-03 22:07:47 +000041bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
42 Cubic smaller, larger;
rmistry@google.comd6176b02012-08-23 18:14:13 +000043 // FIXME: carry last subdivide and reduceOrder result with cubic
caryclark@google.comc6825902012-02-03 22:07:47 +000044 sub_divide(cubic1, minT1, maxT1, intersections.swapped() ? larger : smaller);
45 sub_divide(cubic2, minT2, maxT2, intersections.swapped() ? smaller : larger);
46 Cubic smallResult;
47 if (reduceOrder(smaller, smallResult,
48 kReduceOrder_NoQuadraticsAllowed) <= 2) {
49 Cubic largeResult;
50 if (reduceOrder(larger, largeResult,
51 kReduceOrder_NoQuadraticsAllowed) <= 2) {
52 const _Line& smallLine = (const _Line&) smallResult;
53 const _Line& largeLine = (const _Line&) largeResult;
54 double smallT[2];
55 double largeT[2];
56 // FIXME: this doesn't detect or deal with coincident lines
57 if (!::intersect(smallLine, largeLine, smallT, largeT)) {
58 return false;
59 }
60 if (intersections.swapped()) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000061 smallT[0] = interp(minT2, maxT2, smallT[0]);
62 largeT[0] = interp(minT1, maxT1, largeT[0]);
caryclark@google.comc6825902012-02-03 22:07:47 +000063 } else {
rmistry@google.comd6176b02012-08-23 18:14:13 +000064 smallT[0] = interp(minT1, maxT1, smallT[0]);
65 largeT[0] = interp(minT2, maxT2, largeT[0]);
caryclark@google.comc6825902012-02-03 22:07:47 +000066 }
67 intersections.add(smallT[0], largeT[0]);
68 return true;
69 }
70 }
71 double minT, maxT;
72 if (!bezier_clip(smaller, larger, minT, maxT)) {
73 if (minT == maxT) {
74 if (intersections.swapped()) {
75 minT1 = (minT1 + maxT1) / 2;
76 minT2 = interp(minT2, maxT2, minT);
77 } else {
78 minT1 = interp(minT1, maxT1, minT);
79 minT2 = (minT2 + maxT2) / 2;
80 }
81 intersections.add(minT1, minT2);
82 return true;
83 }
84 return false;
85 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000086
caryclark@google.comc6825902012-02-03 22:07:47 +000087 int split;
88 if (intersections.swapped()) {
89 double newMinT1 = interp(minT1, maxT1, minT);
90 double newMaxT1 = interp(minT1, maxT1, maxT);
91 split = (newMaxT1 - newMinT1 > (maxT1 - minT1) * tClipLimit) << 1;
92#define VERBOSE 0
93#if VERBOSE
94 printf("%s d=%d s=%d new1=(%g,%g) old1=(%g,%g) split=%d\n",
95 __FUNCTION__, depth, splits, newMinT1, newMaxT1, minT1, maxT1,
96 split);
97#endif
98 minT1 = newMinT1;
99 maxT1 = newMaxT1;
100 } else {
101 double newMinT2 = interp(minT2, maxT2, minT);
102 double newMaxT2 = interp(minT2, maxT2, maxT);
103 split = newMaxT2 - newMinT2 > (maxT2 - minT2) * tClipLimit;
104#if VERBOSE
105 printf("%s d=%d s=%d new2=(%g,%g) old2=(%g,%g) split=%d\n",
106 __FUNCTION__, depth, splits, newMinT2, newMaxT2, minT2, maxT2,
107 split);
108#endif
109 minT2 = newMinT2;
110 maxT2 = newMaxT2;
111 }
112 return chop(minT1, maxT1, minT2, maxT2, split);
113}
114
115bool chop(double minT1, double maxT1, double minT2, double maxT2, int split) {
116 ++depth;
117 intersections.swap();
118 if (split) {
119 ++splits;
120 if (split & 2) {
121 double middle1 = (maxT1 + minT1) / 2;
122 intersect(minT1, middle1, minT2, maxT2);
123 intersect(middle1, maxT1, minT2, maxT2);
124 } else {
125 double middle2 = (maxT2 + minT2) / 2;
126 intersect(minT1, maxT1, minT2, middle2);
127 intersect(minT1, maxT1, middle2, maxT2);
128 }
129 --splits;
130 intersections.swap();
131 --depth;
132 return intersections.intersected();
133 }
134 bool result = intersect(minT1, maxT1, minT2, maxT2);
135 intersections.swap();
136 --depth;
137 return result;
138}
139
140private:
141
142static const double tClipLimit = 0.8; // http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf see Multiple intersections
143const Cubic& cubic1;
144const Cubic& cubic2;
145Intersections& intersections;
146int depth;
147int splits;
148};
149
150bool intersect(const Cubic& c1, const Cubic& c2, Intersections& i) {
151 CubicIntersections c(c1, c2, i);
152 return c.intersect();
153}
154