Today I was looking at the Project() method in Revit API. The method signature offers a distance parameter which outputs the distance of the projected point to the input point.

The value of distance parameter is always positive which means we have no clue if the point is above or below the plane. Apart from this issue , the method is implemented in Revit API 2018.1 and it is not available in earlier versions. In this post I discuss two different methods for finding the signed distance of a point from a plane, first using change of basis and trasformations and in the second method we use dot product which in my opinion it is the essence of linear algebra!

In order find the signed distance of point * p* from the given plane

*we can simply evaluate the point with respect to the local coordinate system of the plane. This operation is called*

**Q****change of basis**. Let us say the plane

**is at origin**

*Q**with basis*

**O****,**

*X***and**

*Y**, We can write the components of the plane*

**Z****in vector format :**

*Q*

The transformation of any point from global coordinate system to the plane coordinate system can be define by the 4x4 matrix * A *which is composed of the basis vectors of the plane and the origin point (translation part).

The point ** p’** is described in the plane local coordinate system, therefore by transforming

*to using plane transformation we obtain point*

**p’****in global coordinate system. This operation can be reversed to find the point**

*p**which is the coordinates of point*

**p’***with resepct to the plane coordinate system.*

**p**Below snipped code demonstrates the implementation of change of basis and extracts the * Z* coordinate of point p’ as the signed distance of the point

*with respect to the plane. Also note that we can output the projection point just by transforming the local projection of*

**p****to the global coordsys.**

*p’*/// <summary>

/// return signed distance of point from the plane.

/// If the point is above the plane (in positive side of the plane)

/// the result is positive and if the point

/// is below the plane the result is negetive

/// </summary>

/// <param name="plane">Input plane</param>

/// <param name="point">Point to measure the distance from</param>

/// <param name="projection">Projection of input point on this plane</param>

/// <returns></returns>

public static double DistanceTo (this Plane plane,XYZ point,out XYZ projection)

{

//build a transformation matrix based on the plane coordinate system

Transform tr = Transform.CreateTranslation(plane.Origin);

tr.BasisX = plane.XVec.Normalize();

tr.BasisY = plane.YVec.Normalize() ;

tr.BasisZ = plane.Normal.Normalize();

//Find the local coordinates of the given point with resepct to plabe coordinate system

XYZ local = tr.Inverse.OfPoint(point);

//project the local point to the XY plane (Z=0)

projection = new XYZ(local.X, local.Y, 0);

// transform the projection back to the global coordSYS

projection = tr.OfPoint(projection);

//The Z coordinate of the local point is the signed distance from the plane

return local.Z;

}

Well, above is not really an elegant method!, my aim was to discuss a bit of change of basis which is very important topic. The distance of a point from a plane is simply calculated from the equation * (5)* in below. Let us assume plane

*with normal*

**Q***and a point on plane*

**n***and a point*

**p0***outside the plane with its normal projection on plane*

**p***.*

**p’**Starting from equation ** (1)** which is simply sum of vectors and taking dot product of both sides we obtain equation

*.*

**(2)**Equation ** (3)** is obtained by expanding

*and rewriting*

**(2)***as*

**(p-p’).n***where*

**d*n***is the distance between the*

**d***and*

**p***(Note if*

**p’***is below the plane then*

**p***must be negative therefore*

**d***is signed distance). Then we can write*

**d***by replacing*

**(4)***with zero since two vectors are perpendicular and their dot product is zero.*

**(p₀-p’).n**Finally in we replace * n.n *by 1 since n is normal vector. Equation

**gives us the signed distance from the plane and by moving the point**

*(5)**in opposite direction of plane normal*

**p***by the magnitude of d we obtain*

**(n)***in equation*

**p’**

**(6)**Below is a simple and more elegant implementation of the same method.

public static double DistanceTo2(this Plane plane, XYZ point, out XYZ projection)

{

// signed distance from equation (5)

double d = (point - plane.Origin).DotProduct(plane.Normal);

// the projection point from equation (6)

projection = point - d * plane.Normal;

return d;

}

## Recent Comments