tags:

views:

66

answers:

2

Hey guys, I have been looking all over google and cannot find a consistent method for finding the signed distance between a point and a plane. My planes are defined by a point and a normal. Here is what I have.

JGCameraPlane p;
// Calculate D part of plane equation
float d = -dotProduct(p.normal, p.point);

What do I do from here? Did I calculate the "D" right? As I said I have googled this all day and I cannot find a straight forward answer.

EDIT

Here is my adaptation. Does everything look good?

float    sb, sn, sd;
point3D b;

sn = -dotProduct( p.normal, (vectorSubtract(point,p.point)));
sd = dotProduct(p.normal, p.normal);
sb = sn / sd;

b =  vectorAdd(point, vectorScale(p.normal, sb));

point3D dif = vectorSubtract(point, b);

float dist = sqrtf(dotProduct(dif,dif));

if (dist < 0)
    return NO;
+1  A: 

dont worry i understand exactly how you feel. I am assuming you want some code snippets. so you can implement it in your own. you need to do a lot more work than just finding out the dot product.

It is up to you to understand this algorithm and to implement it into your own program what i will do is give you an implementation of this algorithm

signed distance between point and plane

Here are some sample "C++" implementations of these algorithms.

// Assume that classes are already given for the objects:
//    Point and Vector with
//        coordinates {float x, y, z;}
//        operators for:
//            Point  = Point ± Vector
//            Vector = Point - Point
//            Vector = Scalar * Vector    (scalar product)
//    Plane with a point and a normal {Point V0; Vector n;}
//===================================================================

// dot product (3D) which allows vector operations in arguments
#define dot(u,v)   ((u).x * (v).x + (u).y * (v).y + (u).z * (v).z)
#define norm(v)    sqrt(dot(v,v))  // norm = length of vector
#define d(u,v)     norm(u-v)       // distance = norm of difference

// pbase_Plane(): get base of perpendicular from point to a plane
//    Input:  P = a 3D point
//            PL = a plane with point V0 and normal n
//    Output: *B = base point on PL of perpendicular from P
//    Return: the distance from P to the plane PL
float
pbase_Plane( Point P, Plane PL, Point* B)
{
    float    sb, sn, sd;

    sn = -dot( PL.n, (P - PL.V0));
    sd = dot(PL.n, PL.n);
    sb = sn / sd;

    *B = P + sb * PL.n;
    return d(P, *B);
}

Taken from here: http://www.softsurfer.com/Archive/algorithm_0104/algorithm_0104.htm

PK

Pavan
Hmm that distance seems accurate but it is never negative. Are you sure this is signed?
Justin Meiners
Yes. These formulas give a signed distance which is positive on one side of the plane and negative on the other. And yep it is accurate indeed.
Pavan
Ok I edited to show my adaptation of that code. Does it all look good?
Justin Meiners
On that website they never said that function was the signed distance. My normal is also normalized so I dont need any of the sqrt stuff.
Justin Meiners
dude this algorithm follows the exact mathematical equation. so as long as you have the right implementation then it should be fine. What i suggest is that you do a simple test with a value you know the answer of. feed the values into your implementation, and if its right then its right if its wrong then either the implementation is wrong or your expected answer is wrong either way you make a change until the implementation is right.
Pavan
control + f type signed and youll see a paragraph on signed formula? no?
Pavan
the example i gave you was taken from that site as they had already written an implementation.
Pavan
I know they have a paragraph on it! but there code is the relative distance! tell me how it is supposed to return a negative with a sqrt finishing it off? If you would have actually read about this before copying the first code you saw you would have seen this part of it. "For a unit normal, when |n| = 1, this formula simplifies to:"
Justin Meiners
OH WOW ok so i completely missed that. which means the formula simplifies right? ok so did you solve it then in the end?
Pavan
@Pavan I have to go I will work on it tommorow.
Justin Meiners
+2  A: 

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)));
Beta
Thanks so much!
Justin Meiners