View Full Version : Distance between 3d Planes
david.larkin
2004-07-27, 02:18 AM
Does anyone have a routine that measures the shortest distance between 3D planes ?
There doesn't seems to be any AutoCAD 2002 command for it.
If you set either plane to be the xy-plane of a UCS, then the z-coordinate of any point on the other plane is the distance between the planes...
It isn't really the "shortest" distance... It's the "distance". Your question is only valid if the planes are parallel. Otherwise, they intersect, and the shortest distance between them is 0.
I don't know if this helps you or not. What exactly are you trying to do?
peter
2004-07-27, 01:24 PM
I assume that you are not considering infinite planes but finite plane surfaces like 3dfaces.
Although I don't have the solution as code, I could make some suggestions as to how to solve this puzzle.
First each surface has a normal that is perpendicular to its face.
Well you have to figure out what that is.
Check your matrix mathmatics for formulas to figure those out.
I would then create a series of conditionals that would use the normal as a vector and place them at each vertice of each 3dface and then figure out how to find the distance to the intersection of that vector to the other surface. Again I would use matrix math to do that.
Whichever is shorter is the closest distance.
Peter Jamtgaard
AutoCAD has routines that return the normal of an object and return the intersection of a plane and a line (the later is in the cal routine), but that will only work if one of the 3D faces is oriented so that the normal even hits the other 3D face... The shortest distance might be a non-normal vector from a corner of one 3D face to a corner of the other, or corner to a point on the other 3D face, or even something like a point on the edge of one 3D face to a point on the edge of the other 3D face.
I'm not sure what the easiest way of approaching it would be.
I'm thinking maybe of drawing a polyline around the edge of each 3D face. Then from each vertex of the first 3D face, find the distance to the closest point on the polyline around the second 3D face using vlax-curve-getClosestPointTo. Repeat, using the vertices of the second 3D face and the polyline around the first 3D face. The shortest distance you find will be the shortest distance between in all cases except when the shortest distance is from the corner of one 3D face to a point on the surface of the other, or the case when the closest distance is from a point on one edge to a point on the other edge...
Now, how best to detect those cases?
How about the following pseudo-code:
<draw polylines (p1, p2) around each face (f1, f2)>
<get shortest distance from each vertex of p1 to p2 using vlax-curve-getClosestPointTo>
<get shortest distance from midpoint of each segment of p1 to p2 similarly>
<repeat last two steps for p2 to p1>
<if>
<shortest distance found above is from a corner, you found it>
<else if>
<shortest distance found above is from a midpoint, then:>
<if>
<two and only two opposite sides have shorter distances from the midpoint than from
either adjacent vertex, then shortest distance is from edge to edge; in this case the
shortest distance is between [the point you found on p2 that's closest to p1] and [the
point you found on p1 that's closest to p2])>
<else>
<shortest distance vector is normal to the other plane and hits somewhere on its
surface; figure it using cal to locate the intersection of a line and a plane>
<endif>
<endif>
david.larkin
2004-07-28, 06:15 AM
Peter is correct that I am working with finite plane surfaces using solids in most cases.
His suggestion is probably the best way of solving this puzzle.
What I have been trying to do is to find the minimum clearances between existing and new structures. Finding out where it clashes is easy. Finding out where the minimum clearance are is more tricky and very time consuming.
Looks like this is a big job to create this routine. Have to give this a miss as I have a short time frame to complete this 3D project I am working on.
Thanks for the hints guys.
Can't that be done with simple elevation comparisons?
stig.madsen
2004-07-28, 09:09 PM
Looks like this is a big job to create this routine.
Could be. Then again, might be easier than it sounds. All depends on circumstances.
Simply finding the distance between a point and a plane is not a problem. If you're working with finite, planar surfaces then it would suffice to check the vertices because - if the surface planes are not parallel - one of the vertices will always describe the shortest distance between the surfaces. Of course, if it's required that projected vertices of one surface are located within the other surface - and project edges onto the surface - then it might be a little more work.
As Richard points out, it's already part of the CAL program. But here's a purely mathematical approach to find the distance between a point and a plane. If you're working with planar 3Dfaces then simply extract all 3 or 4 vertices and get the minimum distance from those of all points to the plane of the other surface (if 4 vertices then have in mind that a 3DFace can describe two planes). The function to use or modify is getDistToPlane. The others are utility functions.
(defun getDistToPlane (/ p0 p1 p2 p3 dist norm)
;; getting user input. This could easily be changed in order
;; to receive a plane and a point, e.g. a vertice to check
(cond ((and (setq p2 (getpoint "\nOrigin: "))
(setq p1 (getpoint p2 "\nFirst axis direction: "))
(setq p3 (getpoint p2 "\nSecond axis direction: "))
(setq p0 (getpoint "\nPick point: "))
)
;; transform all points to WCS
(mapcar (function (lambda (p) (set p (trans (eval p) 1 0))))
'(p1 p2 p3 p0)
)
;; find normal vector to plane p1-p2-p3
;; and distance from point p0 to plane
(setq norm (V3Normalize (V3Cross (V3Sub p2 p1) (V3Sub p3 p1)))
dist (V3Dot norm (V3Sub p0 p1))
)
(abs dist)
)
)
)
;;; Return the cross product, vec1 cross vec2
(defun V3Cross (vec1 vec2)
(list (- (* (cadr vec1) (caddr vec2)) (* (caddr vec1) (cadr vec2)))
(- (* (caddr vec1) (car vec2)) (* (car vec1) (caddr vec2)))
(- (* (car vec1) (cadr vec2)) (* (cadr vec1) (car vec2))))
)
;;; Return the dot product of vectors vec1 and vec2
(defun V3Dot (v1 v2)
(apply '+ (mapcar '* v1 v2))
)
;;; Return vector difference vec1-vec2 (notice sequence)
(defun V3Sub (vec1 vec2)
(mapcar '- vec1 vec2)
)
;;; Returns squared length of input vector
(defun V3SquaredLength (vec / expt2)
(defun expt2 (a)
(* a a)
)
(apply '+ (mapcar 'expt2 vec))
)
;;; Returns length of input vector
(defun V3Length (vec)
(sqrt (V3SquaredLength vec))
)
;;; Normalizes the input vector and returns it
(defun V3Normalize (vec / len)
(if (/= (setq len (V3Length vec)) 0.0)
(mapcar (function (lambda (a) (/ a len))) vec)
vec
)
)
If you're working with finite, planar surfaces then it would suffice to check the vertices because - if the surface planes are not parallel - one of the vertices will always describe the shortest distance between the surfaces.
Well, no. It is very possible that the shortest distance between the two 3D faces is along a vector that hits somewhere on the edge of either one or both planes, and the faces don't have to be parallel. It might help if you pull out a couple of business cards (or wait - aren't you on vacation, Stig? - make that "playing cards" :-P ) or similar item and hold them next too each other in various ways; I'm sure you'll run into several such orientations quite quickly. And if a corner of one is pointed toward the other, the shortest-distance-vector may even hit in the somewhere on the interior of the other plane, nowhere near either an edge or a vertex (but in this case the problem degenerates into the simple distance-to-a-plane problem).
That's why I have all the "check midpoints" rigamarole in the pseudocode above.
All this may be moot, though; if the problem is really to find the CLEARANCE between objects, that's a different problem that might have a simpler solution...
stig.madsen
2004-07-29, 12:53 AM
Richard, it's probably over my head but I see three possible scenarios (regardless if the shortest vector hits outside one of the surfaces or not):
1. Surfaces (read: planes) are parallel. Distance is constant.
2. Surface 1 has one or two edges that are parallel with one or two edges of surface 2 (but planes are not parallel). Distance along edge(s) is constant.
3. Surfaces are not parallel in any way. Shortest distance must be a vector from a vertex (seeing that they decribe finite planes) that is perpendicular to the other plane.
In all cases, the shortest distance(s) are decribed by normal vectors, right? Like the green (or the grey) lines in this image (http://www.smadsen.com/planes.jpg). Whether or not such a vector hits outside the finite planes may be another matter.
david.larkin
2004-07-29, 01:35 AM
....problem is really to find the CLEARANCE between objects, ...
That is the key to the problem I am having - find the minimum clearance. This can't be done by simple elevation comparisons.
Do you guys want me to send you a sample drawing (a very simple one) ?
Sure. That would probably be clearest.
In all cases, the shortest distance(s) are decribed by normal vectors, right? Like the green (or the grey) lines in this image (http://www.smadsen.com/planes.jpg). Whether or not such a vector hits outside the finite planes may be another matter.
Hey, good idea. Yeah, I was trying to think of all cases, including cases like this (http://www.ejsurveying.com/quux/Planes.jpg). The shortest-distance vector is normal to both sides that it hits, but may not be normal to either plane.
Unfortunately, it occurs to me that the pseudocode I posted earlier will fail in some instances similar to this...
stig.madsen
2004-07-29, 04:31 PM
Hey, good idea. Yeah, I was trying to think of all cases, including cases like this (http://www.ejsurveying.com/quux/Planes.jpg). The shortest-distance vector is normal to both sides that it hits, but may not be normal to either plane.
Doh! You're right. Thinking too much in planes than in objects, I was. Thanks for the heads up.
david.larkin
2004-07-30, 01:21 AM
Here is the attached file. This is a very simple one compared to the others I had to check out. I have removed all the xrefs, as you don't need them.
Sometimes the sidewalls nibs (dark blue on the drawing) are more critical than the soffit of the arch or deck slab. As you can see on the drawing, the nibs (architectural stone finishing on the walls) are all over the place. Sometimes the soffits of the deck slabs are all over the place !!!!
See if you can quickly find minimum clearance between the nibs and KE+200 profiles.
Have a nice weekend everyone.
I can't quite figure out what you're trying to do. I think the problem is I work in a different industry, and I don't know what nibs, soffits, and KE+200 profiles are. I take it the nibs are the blue solids... Do the cyan dimensions in the drawing have anything to do with the distance you're trying to find?
I assume this is a bridge or tunnel? I notice you're using a lot of 3D-solids. Are you looking for something that will let you select two faces on two seperate 3D-solids, and calculate a clearance, i.e., the shortest distance between the two faces along a line parallel with the z-axis? Or a minimum separation, i.e., the shortest distance between the two faces perpendicular to one of the faces?
david.larkin
2004-08-02, 02:24 AM
The minimum separation, i.e., the shortest distance between the two faces perpendicular to one of the faces.
The sample drawing shows the outlines of a new high speed train (160km/h) going through an old stone arch bridge. The nibs are the blue solids and they are blocks sticking out a bit to create an architectural finish. I have attached a photo of a bridge which is very similar to the drawing.
"Soffit" means the bottom surface of the deck slab or the inside face of the arch.
KE+200 is a name describing the new train profile that will be built on either normal track or slab track. The slab track doesn't move as much as the normal track and that is the main reason why the slab profile is smaller than the normal profile. The Rail Authority want us to show them why it is better to use the slab track option.
Hoping that this answers all your questions.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.