signed distance between plane and point
Asked Answered
I

2

16

I cannot find a consistent method for finding the signed distance between a point and a plane. How can I calculate this given a plane defined as a point and a normal?

struct Plane
{
    Vec3 point;
    Vec3 normal;
} 
Inoculum answered 5/10, 2010 at 0:56 Comment(6)
why is dist = dotProduct(dif,dif)? distance should be: d(P, *B); ??Fico
d(p,b) simply calls this = norm(sqrt(dot(p - b, p - b)));Inoculum
So my code does the same thing.Inoculum
How can it be negative with the sqrt in there?Inoculum
Square root of a number can be positive or negative...Sieracki
@ŽarkoTomičić In this case dot(a, a) >= 0 for all vectors in R^n. (Note that this question is 12 years old also).Inoculum
H
42

You're making things much too complicated. If your normal is normalized, you can just do this:

float dist = dotProduct(p.normal, (vectorSubtract(point, p.point)));
Horton answered 5/10, 2010 at 12:53 Comment(5)
Sorry, but I couldn't figure out how the dot product could produce the distance between p.normal and point - p.point. Can you please explain(maybe using picture) that? In my case, p.normal = (1, 0, 0) and point - p.point = (-200, 0, 0).Herodias
@user26409021: The idea is to find the signed distance between point and p. In your case, what do you think that is? I think it's -200.Horton
Yeah, I know it give me the distance which -200.. So is it correct if I say by using dotProductwe could find distance between 2 points?Herodias
@user26409021: Yes, the distance between p and q is sqrt((p-q).(p-q)).Horton
One explanation as to why this works is that you're computing a vector from an arbitrary point on the plane to the point; d = point - p.point. Then we're projecting d onto the normal. The projection formula is p=dot(d,n)/||n||^2*n={n is unit}=dot(d,n)*n. Since n is unit, the signed length of that vector is dot(d,n).Moderato
E
5

For those curious as to how the dot product formula posted by Beta is derived, here is a visual proof:

           N        Q
           ^        | 
           |        | d
           |        | 
           |        |
---------- P -------+------ > plane

d is the signed distance between Q and the plane

P is a known point that lies on the plane.

N is a normal unit vector perpendicular to the the plane at P.

To find d, we move d to where P and N are

           N
           ^
           |
           |
           +--------Q
           |      / 
         d |    /   
           |θ /     
           |/       
---------- P -------------- > plane

Use cosine to generate Equation 1

cos(θ) = adjacent / hypotenuse
cos(θ) = d / |PQ|                -- |PQ| is length of (Q-P)
     d = |PQ| x cos(θ)           -- Equation 1

Using the following dot product formula

a · b = |a| × |b| × cos(θ)

We derive Equation 2

    N . (Q-P) = |N| x |PQ| x cos(θ)
    N . (Q-P) = 1 x |PQ| x cos(θ)    -- |N| is 1 because unit vector
|PQ| x cos(θ) = N . (Q-P)            -- Equation 2

Substitute the left side of Equation 2 with Equation 1

d = N . (Q-P)
Enterovirus answered 8/3, 2023 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.