discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

intersecting 3D lines

K
kitwallace
Fri, Feb 26, 2021 12:08 PM

Sorry -being lazy- but is there code somewhere for computing the
intersection, if any, of 2 3-d lines?

--
Sent from: http://forum.openscad.org/

Sorry -being lazy- but is there code somewhere for computing the intersection, if any, of 2 3-d lines? -- Sent from: http://forum.openscad.org/
RW
Ron Wheeler
Fri, Feb 26, 2021 2:06 PM

In case no one has code...

Not code but some formulas and videos can be found using Google
"intersection of lines in 3D"

https://socratic.org/questions/how-do-i-find-the-intersection-of-two-lines-in-three-dimensional-space
https://math.stackexchange.com/questions/270767/find-intersection-of-two-3d-lines/271366

http://paulbourke.net/geometry/pointlineplane/calclineline.cs  has some
code in C.

I hope that this gets you started.

On 2021-02-26 7:08 a.m., kitwallace wrote:

Sorry -being lazy- but is there code somewhere for computing the
intersection, if any, of 2 3-d lines?

Sent from the OpenSCAD mailing list archive
http://forum.openscad.org/ at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

--
Ron Wheeler
Artifact Software
438-345-3369
rwheeler@artifact-software.com

In case no one has code... Not code but some formulas and videos can be found using Google "intersection of lines in 3D" https://socratic.org/questions/how-do-i-find-the-intersection-of-two-lines-in-three-dimensional-space https://math.stackexchange.com/questions/270767/find-intersection-of-two-3d-lines/271366 http://paulbourke.net/geometry/pointlineplane/calclineline.cs  has some code in C. I hope that this gets you started. On 2021-02-26 7:08 a.m., kitwallace wrote: > Sorry -being lazy- but is there code somewhere for computing the > intersection, if any, of 2 3-d lines? > ------------------------------------------------------------------------ > Sent from the OpenSCAD mailing list archive > <http://forum.openscad.org/> at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- Ron Wheeler Artifact Software 438-345-3369 rwheeler@artifact-software.com
K
kitwallace
Sat, Feb 27, 2021 12:08 AM

Thanks  - I used Pauls' code -  here it is in OpenSCAD in case anyone looks :

function intersection (p1,p2,p3,p4,eps=0.0001) =
let (p13=p1-p3,
p43=p4-p3)
norm(p43) <eps
? undef
:
let (p21=p2-p1)
norm(p21) < eps
? undef
:
let (
d1343= p13p43,
d4321= p43
p21,
d1321= p13p21,
d4343= p43
p43,
d2121= p21p21,
denom=d2121 * d4343 - d4321 * d4321)
abs(denom) < eps
?  undef
:  let (numer=d1343 * d4321 - d1321 * d4343,
mua = numer / denom,
mub = (d1343 + d4321 mua ) / d4343)
[p1 + mua
p21,
p3 + mub
p43];

--
Sent from: http://forum.openscad.org/

Thanks - I used Pauls' code - here it is in OpenSCAD in case anyone looks : function intersection (p1,p2,p3,p4,eps=0.0001) = let (p13=p1-p3, p43=p4-p3) norm(p43) <eps ? undef : let (p21=p2-p1) norm(p21) < eps ? undef : let ( d1343= p13*p43, d4321= p43*p21, d1321= p13*p21, d4343= p43*p43, d2121= p21*p21, denom=d2121 * d4343 - d4321 * d4321) abs(denom) < eps ? undef : let (numer=d1343 * d4321 - d1321 * d4343, mua = numer / denom, mub = (d1343 + d4321 *mua ) / d4343) [p1 + mua*p21, p3 + mub*p43]; -- Sent from: http://forum.openscad.org/
RP
Ronaldo Persiano
Sat, Feb 27, 2021 12:28 AM

Anyway, here is my version that seems to be about the same:

function 3D_line_intersection( line1, line2, prec=1e-6) =
assert(norm(p1-p0)>prec) // assert well defined lines
assert(norm(q1-q0)>prec)
let(
p0 = line1[0],
p1 = line1[1],
q0 = line2[0],
q1 = line2[1],
d  = cross( p1-p0, q1-q0 ) // normal to lines if non zero
)
norm(d)<prec // (nearly) coincident or parallel lines ?
? norm(cross( q1-p0, p1-p0 ))<=precnorm(q1-p0)norm(p1-p0)
? undef  // coincident lines
: []    // disjoint parallel lines
: let( d = d/norm(d) )
abs(d
(p1-q1)) > prec // are the lines disjoint ?
? [] // disjoint lines
: let( // computes the crossing points
crxq = cross(d, q1-q0),
crxp = cross(d, p1-p0),
u = crxq
(q0-p0)/(crxq*(p1-p0)),
v = crxp*(p0-q0)/(crxp*(q1-q0))
)
[p0 + u*(p1-p0), q0 + v*(q1-q0)];

It returns undef when the lines are nearly coincident and [] if they are
parallel. When crossing, it returns two points, one in each line, whose
distance is less than prec.

> > Anyway, here is my version that seems to be about the same: function 3D_line_intersection( line1, line2, prec=1e-6) = assert(norm(p1-p0)>prec) // assert well defined lines assert(norm(q1-q0)>prec) let( p0 = line1[0], p1 = line1[1], q0 = line2[0], q1 = line2[1], d = cross( p1-p0, q1-q0 ) // normal to lines if non zero ) norm(d)<prec // (nearly) coincident or parallel lines ? ? norm(cross( q1-p0, p1-p0 ))<=prec*norm(q1-p0)*norm(p1-p0) ? undef // coincident lines : [] // disjoint parallel lines : let( d = d/norm(d) ) abs(d*(p1-q1)) > prec // are the lines disjoint ? ? [] // disjoint lines : let( // computes the crossing points crxq = cross(d, q1-q0), crxp = cross(d, p1-p0), u = crxq*(q0-p0)/(crxq*(p1-p0)), v = crxp*(p0-q0)/(crxp*(q1-q0)) ) [p0 + u*(p1-p0), q0 + v*(q1-q0)]; It returns *undef* when the lines are nearly coincident and [] if they are parallel. When crossing, it returns two points, one in each line, whose distance is less than prec.
K
kitwallace
Sat, Feb 27, 2021 7:45 AM

Thanks Ronaldo - that's a bit bit more vectory  - I should use assert more -
although just to note that the first two asserts are too early :)

--
Sent from: http://forum.openscad.org/

Thanks Ronaldo - that's a bit bit more vectory - I should use assert more - although just to note that the first two asserts are too early :) -- Sent from: http://forum.openscad.org/