blob: d58bcb9002e3c079db4c8475de3e79450af22ee3 [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;
47 if (reduceOrder(smaller, smallResult, kReduceOrder_NoQuadraticsAllowed) <= 2) {
48 Cubic largeResult;
49 if (reduceOrder(larger, largeResult, kReduceOrder_NoQuadraticsAllowed) <= 2) {
50 _Point pt;
51 const _Line& smallLine = (const _Line&) smallResult;
52 const _Line& largeLine = (const _Line&) largeResult;
53 if (!lineIntersect(smallLine, largeLine, &pt)) {
54 return false;
55 }
56 double smallT = t_at(smallLine, pt);
57 double largeT = t_at(largeLine, pt);
58 if (intersections.swapped()) {
59 smallT = interp(minT2, maxT2, smallT);
60 largeT = interp(minT1, maxT1, largeT);
61 } else {
62 smallT = interp(minT1, maxT1, smallT);
63 largeT = interp(minT2, maxT2, largeT);
64 }
65 intersections.add(smallT, largeT);
66 return true;
67 }
68 }
69 double minT, maxT;
70 if (!bezier_clip(smaller, larger, minT, maxT)) {
71 if (minT == maxT) {
72 if (intersections.swapped()) {
73 minT1 = (minT1 + maxT1) / 2;
74 minT2 = interp(minT2, maxT2, minT);
75 } else {
76 minT1 = interp(minT1, maxT1, minT);
77 minT2 = (minT2 + maxT2) / 2;
78 }
79 intersections.add(minT1, minT2);
80 return true;
81 }
82 return false;
83 }
84
85 int split;
86 if (intersections.swapped()) {
87 double newMinT1 = interp(minT1, maxT1, minT);
88 double newMaxT1 = interp(minT1, maxT1, maxT);
89 split = (newMaxT1 - newMinT1 > (maxT1 - minT1) * tClipLimit) << 1;
90 printf("%s d=%d s=%d new1=(%g,%g) old1=(%g,%g) split=%d\n", __FUNCTION__, depth,
91 splits, newMinT1, newMaxT1, minT1, maxT1, split);
92 minT1 = newMinT1;
93 maxT1 = newMaxT1;
94 } else {
95 double newMinT2 = interp(minT2, maxT2, minT);
96 double newMaxT2 = interp(minT2, maxT2, maxT);
97 split = newMaxT2 - newMinT2 > (maxT2 - minT2) * tClipLimit;
98 printf("%s d=%d s=%d new2=(%g,%g) old2=(%g,%g) split=%d\n", __FUNCTION__, depth,
99 splits, newMinT2, newMaxT2, minT2, maxT2, split);
100 minT2 = newMinT2;
101 maxT2 = newMaxT2;
102 }
103 return chop(minT1, maxT1, minT2, maxT2, split);
104}
105
106bool chop(double minT1, double maxT1, double minT2, double maxT2, int split) {
107 ++depth;
108 intersections.swap();
109 if (split) {
110 ++splits;
111 if (split & 2) {
112 double middle1 = (maxT1 + minT1) / 2;
113 intersect(minT1, middle1, minT2, maxT2);
114 intersect(middle1, maxT1, minT2, maxT2);
115 } else {
116 double middle2 = (maxT2 + minT2) / 2;
117 intersect(minT1, maxT1, minT2, middle2);
118 intersect(minT1, maxT1, middle2, maxT2);
119 }
120 --splits;
121 intersections.swap();
122 --depth;
123 return intersections.intersected();
124 }
125 bool result = intersect(minT1, maxT1, minT2, maxT2);
126 intersections.swap();
127 --depth;
128 return result;
129}
130
131private:
132
133static const double tClipLimit = 0.8; // http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf see Multiple intersections
134const Cubic& cubic1;
135const Cubic& cubic2;
136Intersections& intersections;
137int depth;
138int splits;
139};
140
141bool intersectStartT(const Cubic& c1, const Cubic& c2, Intersections& i) {
142 CubicIntersections c(c1, c2, i);
143 return c.intersect();
144}
145