blob: 61009148e27dabaa66b8c4cdf4fa14e8b7152e83 [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
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.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com8dcf1142012-07-02 20:27:02 +00008#include "CurveUtilities.h"
caryclark@google.com639df892012-01-10 21:46:10 +00009#include "LineParameters.h"
10#include <algorithm> // used for std::swap
11
caryclark@google.com235f56a2012-09-14 14:19:30 +000012#define DEBUG_BEZIER_CLIP 1
13
caryclark@google.com639df892012-01-10 21:46:10 +000014// 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
16bool 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.comd6176b02012-08-23 18:14:13 +000021
caryclark@google.com639df892012-01-10 21:46:10 +000022 // 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.coma7e483d2012-08-28 20:44:43 +000027 assert(0);
caryclark@google.com639df892012-01-10 21:46:10 +000028 return false;
29 }
30
31 double distance = endLine.controlPtDistance(q1);
rmistry@google.comd6176b02012-08-23 18:14:13 +000032
caryclark@google.com639df892012-01-10 21:46:10 +000033 // 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.comd6176b02012-08-23 18:14:13 +000039
caryclark@google.com639df892012-01-10 21:46:10 +000040 // compute intersecting candidate distance
41 Quadratic distance2y; // points with X of (0, 1/2, 1)
42 endLine.quadDistanceY(q2, distance2y);
rmistry@google.comd6176b02012-08-23 18:14:13 +000043
caryclark@google.com639df892012-01-10 21:46:10 +000044 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.com27accef2012-01-25 18:57:23 +000061 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.comd6176b02012-08-23 18:14:13 +000069 } while (idx);
caryclark@google.com235f56a2012-09-14 14:19:30 +000070#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.com639df892012-01-10 21:46:10 +000082 return minT < maxT; // returns false if distance shows no intersection
83}