Apache Commons - Math : Line::Intersection

Sep 29, 2004
18,656
67
91
http://commons.apache.org/proper/co....commons.math3.geometry.euclidean.threed.Line)

Can someone explain to me why this does not return a point object and how I am supposed to used a Vector?

Class Line :: public Vector3D intersection(Line line)

Get the intersection point of the instance and another line.
Parameters:line - other lineReturns:intersection point of the instance and the other line or null if there are no intersection points

Code:
258    /** Get the intersection point of the instance and another line.
259     * @param line other line
260     * @return intersection point of the instance and the other line
261     * or null if there are no intersection points
262     */
263    public Vector3D intersection(final Line line) {
264        final Vector3D closest = closestPoint(line);
265        return line.contains(closest) ? closest : null;
266    }

241    public Vector3D closestPoint(final Line line) {
242
243        final double cos = direction.dotProduct(line.direction);
244        final double n = 1 - cos * cos;
245        if (n < Precision.EPSILON) {
246            // the lines are parallel
247            return zero;
248        }
249
250        final Vector3D delta0 = line.zero.subtract(zero);
251        final double a        = delta0.dotProduct(direction);
252        final double b        = delta0.dotProduct(line.direction);
253
254        return new Vector3D(1, zero, (a - b * cos) / n, direction);
255
256    }

049    /** Line point closest to the origin. */
050    private Vector3D zero;

As I look more, a bunch of methods say they return a Point but return a Vector.
 
Last edited:
Sep 29, 2004
18,656
67
91
Ya ... ran some tests to see that the Javadoc is correct. The returned objects are vectors but are really 3d coordinates.

But the new problem is that line1.intersects(line2) does the calculation assuming infinite length lines. (null returned if there is no intersection)

So, I have each line and the intersection point. But now I need to find out if the point is on both lines. So, I am going to see if the distance(Vector3D) method works as hoped. Just ran distance(Vector3d) and it assumes infinite lengths. ARGGHHHH.
 
Sep 29, 2004
18,656
67
91
Line.wholeLine() returns a SubLine.

SubLine has this method:public Vector3D intersection(SubLine subLine,
boolean includeEndPoints)

Get the intersection of the instance and another sub-line.
This method is related to the intersection method in the Line class, but in addition to compute the point along infinite lines, it also checks the point lies on both sub-line ranges.
Parameters:subLine - other sub-line which may intersect instanceincludeEndPoints - if true, endpoints are considered to belong to instance (i.e. they are closed sets) and may be returned, otherwise endpoints are considered to not belong to instance (i.e. they are open sets) and intersection occurring on endpoints lead to null being returnedReturns:the intersection point if there is one, null if the sub-lines don't intersect.


My tests show that includeEndPoints is ignored. AHHHHHHH.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,623
4,544
75
So, I have each line and the intersection point. But now I need to find out if the point is on both lines. So, I am going to see if the distance(Vector3D) method works as hoped. Just ran distance(Vector3d) and it assumes infinite lengths. ARGGHHHH.

If you have line A, of which you care about the segment from A0 to A1, line B, of which you care about the segment from B0 to B1, and intersection point C, then if:

distance(C to A0) <= distance (A0 to A1) &&
distance(C to A1) <= distance (A0 to A1) &&
distance(C to B0) <= distance (B0 to B1) &&
distance(C to B1) <= distance (B0 to B1)

Then C should be on both line segments. If it were not then the distance to one of the points should be longer than the length of that line segment.
 
Sep 29, 2004
18,656
67
91
Ken,

Ya ... that would work. I'm mad that the API doesn't provide the checks. We let a developer do things unsupervised and he didn't know about libraries that do things like dot products. So we are trying to refactor it all out in favor of libraries. Part of this is determining whether two line segments intersect.

Or in detail since we are talking doubles:
distance(A0,A1) >= distance(C, A0) + distance (C, A1) - tolerance &&
distance(B0,B1) >= distance(C, B0) + distance (C, B1) - tolerance
 
Sep 29, 2004
18,656
67
91
Oh great.

Just instantiate SubLine and use the intersects method there.

SubLine actually remember start and end point (just don't look at the source code for it)