caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1 | #include "CurveIntersection.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 2 | #include "Intersections.h" |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 3 | #include "IntersectionUtilities.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 4 | #include "LineIntersection.h" |
| 5 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 6 | class CubicIntersections : public Intersections { |
| 7 | public: |
| 8 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 9 | CubicIntersections(const Cubic& c1, const Cubic& c2, Intersections& i) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 10 | : cubic1(c1) |
| 11 | , cubic2(c2) |
| 12 | , intersections(i) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 13 | , depth(0) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 14 | , splits(0) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 15 | } |
| 16 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 17 | bool intersect() { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 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 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 25 | int split; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 26 | if (maxT1 - minT1 < maxT2 - minT2) { |
| 27 | intersections.swap(); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 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); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 37 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 38 | |
| 39 | protected: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 40 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 41 | bool intersect(double minT1, double maxT1, double minT2, double maxT2) { |
| 42 | Cubic smaller, larger; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 43 | // FIXME: carry last subdivide and reduceOrder result with cubic |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 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, |
| 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 61 | smallT[0] = interp(minT2, maxT2, smallT[0]); |
| 62 | largeT[0] = interp(minT1, maxT1, largeT[0]); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 63 | } else { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 64 | smallT[0] = interp(minT1, maxT1, smallT[0]); |
| 65 | largeT[0] = interp(minT2, maxT2, largeT[0]); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 66 | } |
| 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 86 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 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; |
| 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 | |
| 115 | bool 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 | |
| 140 | private: |
| 141 | |
| 142 | static const double tClipLimit = 0.8; // http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf see Multiple intersections |
| 143 | const Cubic& cubic1; |
| 144 | const Cubic& cubic2; |
| 145 | Intersections& intersections; |
| 146 | int depth; |
| 147 | int splits; |
| 148 | }; |
| 149 | |
| 150 | bool intersect(const Cubic& c1, const Cubic& c2, Intersections& i) { |
| 151 | CubicIntersections c(c1, c2, i); |
| 152 | return c.intersect(); |
| 153 | } |
| 154 | |