caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 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.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | #include "CurveIntersection.h" |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 8 | #include "CurveUtilities.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 9 | #include "LineParameters.h" |
| 10 | #include <algorithm> // used for std::swap |
| 11 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame^] | 12 | #define DEBUG_BEZIER_CLIP 1 |
| 13 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 14 | // return false if unable to clip (e.g., unable to create implicit line) |
| 15 | // caller should subdivide, or create degenerate if the values are too small |
| 16 | bool bezier_clip(const Quadratic& q1, const Quadratic& q2, double& minT, double& maxT) { |
| 17 | minT = 1; |
| 18 | maxT = 0; |
| 19 | // determine normalized implicit line equation for pt[0] to pt[3] |
| 20 | // of the form ax + by + c = 0, where a*a + b*b == 1 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 21 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 22 | // find the implicit line equation parameters |
| 23 | LineParameters endLine; |
| 24 | endLine.quadEndPoints(q1); |
| 25 | if (!endLine.normalize()) { |
| 26 | printf("line cannot be normalized: need more code here\n"); |
caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame] | 27 | assert(0); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 28 | return false; |
| 29 | } |
| 30 | |
| 31 | double distance = endLine.controlPtDistance(q1); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 32 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 33 | // find fat line |
| 34 | double top = 0; |
| 35 | double bottom = distance / 2; // http://students.cs.byu.edu/~tom/557/text/cic.pdf (7.6) |
| 36 | if (top > bottom) { |
| 37 | std::swap(top, bottom); |
| 38 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 39 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 40 | // compute intersecting candidate distance |
| 41 | Quadratic distance2y; // points with X of (0, 1/2, 1) |
| 42 | endLine.quadDistanceY(q2, distance2y); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 43 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 44 | int flags = 0; |
| 45 | if (approximately_lesser(distance2y[0].y, top)) { |
| 46 | flags |= kFindTopMin; |
| 47 | } else if (approximately_greater(distance2y[0].y, bottom)) { |
| 48 | flags |= kFindBottomMin; |
| 49 | } else { |
| 50 | minT = 0; |
| 51 | } |
| 52 | |
| 53 | if (approximately_lesser(distance2y[2].y, top)) { |
| 54 | flags |= kFindTopMax; |
| 55 | } else if (approximately_greater(distance2y[2].y, bottom)) { |
| 56 | flags |= kFindBottomMax; |
| 57 | } else { |
| 58 | maxT = 1; |
| 59 | } |
| 60 | // Find the intersection of distance convex hull and fat line. |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 61 | int idx = 0; |
| 62 | do { |
| 63 | int next = idx + 1; |
| 64 | if (next == 3) { |
| 65 | next = 0; |
| 66 | } |
| 67 | x_at(distance2y[idx], distance2y[next], top, bottom, flags, minT, maxT); |
| 68 | idx = next; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 69 | } while (idx); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame^] | 70 | #if DEBUG_BEZIER_CLIP |
| 71 | _Rect r1, r2; |
| 72 | r1.setBounds(q1); |
| 73 | r2.setBounds(q2); |
| 74 | _Point testPt = {0.487, 0.337}; |
| 75 | if (r1.contains(testPt) && r2.contains(testPt)) { |
| 76 | printf("%s q1=(%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g)" |
| 77 | " q2=(%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g) minT=%1.9g maxT=%1.9g\n", |
| 78 | __FUNCTION__, q1[0].x, q1[0].y, q1[1].x, q1[1].y, q1[2].x, q1[2].y, |
| 79 | q2[0].x, q2[0].y, q2[1].x, q2[1].y, q2[2].x, q2[2].y, minT, maxT); |
| 80 | } |
| 81 | #endif |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 82 | return minT < maxT; // returns false if distance shows no intersection |
| 83 | } |