Math: is a surface planar?

Discussions about Coding and Scripting
Post Reply
User avatar
Barbie
Godlike
Posts: 2792
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Math: is a surface planar?

Post by Barbie »

I have a little Windows program that reads T3D files and does some checks on it. Concerning brushes I'm interested how to check if a surface with more than 3 vertices is planar, but the time I did vector analysis is long ago and I've rarely used it since then...^^ One approach is to build all possible planes with exact 3 vertices and verify that the normal vectors are linearly dependent.

But perhaps anybody has vector calculations in shorter memory access and can provide a simpler solution. :D
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
Higor
Godlike
Posts: 1866
Joined: Sun Mar 04, 2012 6:47 pm

Re: Math: is a surface planar?

Post by Higor »

If you don't mind inconsistancies on very small surfaces.

OPEN UnMath.h (from ut public sources) to see how to use the following operations.

Create the FPlane struct using 3 points of the face.
Then perform 'PlaneDot' for any other points (from point 4 and onwards) using that FPlane.
If the absolute value of that 'PlaneDot' result is greater than... let's say 1, then surface isn't planar.

PlaneDot tells you how far out (or in) a point is towards a plane, in unreal units, can have negative values.
Values close to zero mean that the point is in that plane.
User avatar
Wormbo
Adept
Posts: 258
Joined: Sat Aug 24, 2013 6:04 pm
Contact:

Re: Math: is a surface planar?

Post by Wormbo »

From top of my head:
  1. Pick three different vertices A, B and C.
    (Make sure they are distinct locations. If not, pick a different combination of three vertices from the surface.)
  2. Define two vectors a = C - A and b = C - B to establish the surface orientation.
    (Make sure the two vectors are not parallel, otherwise start over with a different combination of vertices.)
  3. Define a vector that is perpendicular to the surface: n = a cross b. You might want to normalize it.
    (The cross product can act as both your distinct-edges check and your parallelity check, because it will yield a vector of length zero if either the input vectors are parallel or at least one of them is a zero vector itself.)
  4. Pick one vertex R of the surface as reference. (Could also be A, B or C.)
  5. For each other vertex V of the surface, define a vector v = R - V. This vector will be perpendicular to the surface normal vector if the surface is planar, i.e. their dot product will be zero: n dot v == 0
Note that there will be rounding errors and hardly any surface that isn't aligned to the coordinate axes will be perfectly planar. Instead of comparing (length or dot product) to zero, check if its absolute value is below a certain small threshold.
Post Reply