1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-06 14:56:47 +02:00

refactor lines_intersection

This commit is contained in:
Justin Lin
2022-08-22 11:01:17 +08:00
parent 2602a90388
commit 87ec19fb7a
3 changed files with 28 additions and 16 deletions

View File

@@ -1,4 +1,4 @@
function __line_intersection2(line_pts1, line_pts2, epsilon = 0.0001) = function __line_intersection2(line_pts1, line_pts2, ext, epsilon = 0.0001) =
let( let(
a1 = line_pts1[0], a1 = line_pts1[0],
a2 = line_pts1[1], a2 = line_pts1[1],
@@ -7,23 +7,35 @@ function __line_intersection2(line_pts1, line_pts2, epsilon = 0.0001) =
a = a2 - a1, a = a2 - a1,
b = b2 - b1, b = b2 - b1,
c = cross(a, b) c = cross(a, b)
) )
abs(c) < epsilon ? [] : // they are parallel or conincident edges abs(c) < epsilon ? [] : // they are parallel or conincident edges
a1 + a * cross(b1 - a1, b) / c; let(
t = cross(b1 - a1, b) / c,
p = a1 + a * t
)
ext ? p :
let(u = -cross(a1 - b1, a) / c)
t >= 0 && t <= 1 && u >= 0 && u <= 1 ? p : [];
function __line_intersection3(line_pts1, line_pts2, epsilon = 0.0001) = function v_scalar(v1, v2) =
let(s = norm(v1) / norm(v2))
s * v2 == v1 ? s : -s;
function __line_intersection3(line_pts1, line_pts2, ext, epsilon = 0.0001) =
let( let(
a1 = line_pts1[0], a1 = line_pts1[0],
a2 = line_pts1[1], a2 = line_pts1[1],
b1 = line_pts2[0], b1 = line_pts2[0],
b2 = line_pts2[1], b2 = line_pts2[1],
a = a2 - a1, a = a2 - a1,
b = b2 - b1, b = b2 - b1,
s = b1 - a1, c = cross(a, b)
n1 = cross(a, b), )
n1p = n1 * n1 norm(c) < epsilon ? [] : // they are parallel or conincident edges
let(
t = v_scalar(cross(b1 - a1, b), c),
p = a1 + a * t
) )
cross(a, s) * (b2 - a1) != 0 || // they aren't coplanar ext ? p :
n1p < epsilon ^ 2 ? [] : // they are parallel or conincident edges let(u = v_scalar(cross(a1 - b1, a), -c))
let(n2 = cross(s, b), v = a * sqrt(n2 * n2 / n1p)) t >= 0 && t <= 1 && u >= 0 && u <= 1 ? p : [];
n1 * n2 >= 0 ? a1 + v : a1 - v;

View File

@@ -19,7 +19,7 @@ function _bijection_offset_impl(pts, d, epsilon) =
es = __lines_from(pts, true), es = __lines_from(pts, true),
offset_es = _offset_edges(es, d), offset_es = _offset_edges(es, d),
leng_minus_one = len(offset_es) - 1, leng_minus_one = len(offset_es) - 1,
last_p = __line_intersection2(offset_es[leng_minus_one], offset_es[0], epsilon) last_p = __line_intersection2(offset_es[leng_minus_one], offset_es[0], true, epsilon)
) )
[ [
// p == p to avoid [nan, nan], because [nan, nan] != [nan, nan] // p == p to avoid [nan, nan], because [nan, nan] != [nan, nan]
@@ -29,7 +29,7 @@ function _bijection_offset_impl(pts, d, epsilon) =
let( let(
this_edge = offset_es[i], this_edge = offset_es[i],
next_edge = offset_es[i + 1], next_edge = offset_es[i + 1],
p = __line_intersection2(this_edge, next_edge, epsilon) p = __line_intersection2(this_edge, next_edge, true, epsilon)
) )
assert(p != [] && p == p, "bijection_offset failed. Parallel or conincident edges found") assert(p != [] && p == p, "bijection_offset failed. Parallel or conincident edges found")
p p

View File

@@ -3,8 +3,8 @@ use <../__comm__/__in_line.scad>
use <../__comm__/__lines_from.scad> use <../__comm__/__lines_from.scad>
function _any_intersection_sub(lines, line, lines_leng, i, epsilon) = function _any_intersection_sub(lines, line, lines_leng, i, epsilon) =
let(p = __line_intersection2(lines[i], line, epsilon)) let(p = __line_intersection2(lines[i], line, false, epsilon))
(p != [] && __in_line(line, p, epsilon) && __in_line(lines[i], p, epsilon)) ? [i, p] : _any_intersection(lines, line, lines_leng, i + 1, epsilon); p != [] ? [i, p] : _any_intersection(lines, line, lines_leng, i + 1, epsilon);
// return [idx, [x, y]] or [] // return [idx, [x, y]] or []
function _any_intersection(lines, line, lines_leng, i, epsilon) = function _any_intersection(lines, line, lines_leng, i, epsilon) =