From bc1e5f7a6c42a0fc44adb6f4c41aece472b394ab Mon Sep 17 00:00:00 2001 From: Shaunak Kishore Date: Thu, 10 Sep 2015 01:48:04 -0400 Subject: [PATCH] Add bridge intersection points to stroke outline --- lib/stroke_extractor.js | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/stroke_extractor.js b/lib/stroke_extractor.js index 9b0a48d5..6c1c4d89 100644 --- a/lib/stroke_extractor.js +++ b/lib/stroke_extractor.js @@ -319,6 +319,16 @@ function extract_stroke(paths, endpoint_map, bridge_adjacency, return endpoint_map[Point.key(index)].point; } + function push_line_segments(points) { + for (var i = 0; i < points.length - 1; i++) { + result.push({ + start: Point.clone(points[i]), + end: Point.clone(points[i + 1]), + control: undefined, + }); + } + } + function select_bridge(endpoint, options) { if (options.length === 1) { // Handle star-shaped strokes where one stroke ends at the intersection @@ -335,22 +345,25 @@ function extract_stroke(paths, endpoint_map, bridge_adjacency, } // Compute the other bridge segment and check if it intersects. var indices2 = [endpoint_map[key].index, bridge_adjacency[key][i]]; + var segment2 = indices2.map(index_to_point); if (Point.equal(indices2[0], indices1[1]) && !extracted_indices[Point.key(indices2[1])]) { + push_line_segments([segment1[0], segment1[1], segment2[1]]); return indices2[1]; } else if (Point.equal(indices2[1], indices1[1]) && !extracted_indices[Point.key(indices2[0])]) { + push_line_segments([segment1[0], segment1[1], segment2[0]]); return indices2[0]; } - var segment2 = indices2.map(index_to_point); var intersection = get_intersection(segment1, segment2); if (intersection !== undefined) { var angle1 = angle(indices1[0], indices1[1]); var angle2 = angle(indices2[0], indices2[1]); - var diff = Angle.subtract(angle2, angle1); - if (diff < 0) { + if (Angle.subtract(angle2, angle1) < 0) { indices2.reverse(); + segment2.reverse(); } + push_line_segments([segment1[0], intersection, segment2[1]]); return indices2[1]; } } @@ -382,12 +395,18 @@ function extract_stroke(paths, endpoint_map, bridge_adjacency, var options = bridge_adjacency[key].sort(function(a, b) { return angle(endpoint.index, a) - angle(endpoint.index, b); }); + // HACK(skishore): The call to select_bridge may update the result. + // When a stroke is formed by computing a bridge intersection, then the + // two bridge fragments are added in select_bridge. + var result_length = result.length; var next = (attempt_one ? options[0] : select_bridge(endpoint, options)); - result.push({ - start: Point.clone(endpoint.point), - end: Point.clone(endpoint_map[Point.key(next)].point), - control: undefined, - }); + if (result.length === result_length) { + result.push({ + start: Point.clone(endpoint.point), + end: Point.clone(endpoint_map[Point.key(next)].point), + control: undefined, + }); + } current = next; } // Check if we have either closed the loop or hit an extracted segment.