blob: 615c3a785ce275754a054321ff0e0ebdcdde5c94 [file] [log] [blame]
caryclark@google.com639df892012-01-10 21:46:10 +00001#include "CubicIntersection.h"
2#include "Intersections.h"
3#include "IntersectionUtilities.h"
4#include "LineIntersection.h"
5
6class CubicIntersections : public Intersections {
7public:
8
9CubicIntersections(const Cubic& c1, const Cubic& c2, Intersections& i)
10 : cubic1(c1)
11 , cubic2(c2)
12 , intersections(i)
13 , depth(0)
14 , splits(0) {
15}
16
17bool intersect() {
18 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 }
25 int split;
26 if (maxT1 - minT1 < maxT2 - minT2) {
27 intersections.swap();
28 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);
37}
38
39protected:
40
41bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
42 Cubic smaller, larger;
43 // FIXME: carry last subdivide and reduceOrder result with cubic
44 sub_divide(cubic1, minT1, maxT1, intersections.swapped() ? larger : smaller);
45 sub_divide(cubic2, minT2, maxT2, intersections.swapped() ? smaller : larger);
46 Cubic smallResult;
caryclark@google.com27accef2012-01-25 18:57:23 +000047 if (reduceOrder(smaller, smallResult,
48 kReduceOrder_NoQuadraticsAllowed) <= 2) {
caryclark@google.com639df892012-01-10 21:46:10 +000049 Cubic largeResult;
caryclark@google.com27accef2012-01-25 18:57:23 +000050 if (reduceOrder(larger, largeResult,
51 kReduceOrder_NoQuadraticsAllowed) <= 2) {
caryclark@google.com639df892012-01-10 21:46:10 +000052 _Point pt;
53 const _Line& smallLine = (const _Line&) smallResult;
54 const _Line& largeLine = (const _Line&) largeResult;
55 if (!lineIntersect(smallLine, largeLine, &pt)) {
56 return false;
57 }
58 double smallT = t_at(smallLine, pt);
59 double largeT = t_at(largeLine, pt);
60 if (intersections.swapped()) {
61 smallT = interp(minT2, maxT2, smallT);
62 largeT = interp(minT1, maxT1, largeT);
63 } else {
64 smallT = interp(minT1, maxT1, smallT);
65 largeT = interp(minT2, maxT2, largeT);
66 }
67 intersections.add(smallT, largeT);
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 }
86
87 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;
caryclark@google.com27accef2012-01-25 18:57:23 +000092#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
caryclark@google.com639df892012-01-10 21:46:10 +000098 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;
caryclark@google.com27accef2012-01-25 18:57:23 +0000104#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
caryclark@google.com639df892012-01-10 21:46:10 +0000109 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 intersectStartT(const Cubic& c1, const Cubic& c2, Intersections& i) {
151 CubicIntersections c(c1, c2, i);
152 return c.intersect();
153}
154