How to know if a vertex is back or front to a plane?

Hello,
I’d like to know if a vertex is back or front to a plane.
I have search in mathutils.geometry but with no success.
There is a simple way to do this?

Just to make sure I didn’t misunderstand your question… you want to know if a vertex is in front of or behind a plane?

If so… I think… perhaps… just maybe… you could try the following steps:

  • Calculate the plane’s normal
  • Calculate the vector between your vertex and a point on the plane (via vector subtraction).
  • Calculate the cosine of the angle between the two (via dot product)

If the result is greater than zero, then the vertex is in front of the plane
If the result is less than zero, then the vertex is behind the plane.
If the result is zero, then the vertex lies on the plane.

Below is some actual Blender Python code to perform this:


<i>#find the plane's normal. p0, p1, and p2 are simply points on the plane (in world space)</i>
p0 = bpy.data.objects["plane"].data.vertices[0].co * bpy.data.objects["plane"].matrix_world
p1 = bpy.data.objects["plane"].data.vertices[1].co * bpy.data.objects["plane"].matrix_world
p2 = bpy.data.objects["plane"].data.vertices[2].co * bpy.data.objects["plane"].matrix_world
v1 = p0 - p1
v2 = p2 - p1
normal = v1.cross(v2)
normal.normalize()

<i>#vertex you want to test (in world space)</i>
vertex = Vector((123.0, 324.32, 12.43))

<i>#actual test performed here</i>
t = vertex - p0
t.normalize()
result = normal.dot(t)

EPSILON = 0.0000001

if(abs(result) &lt; EPSILON):
  #ON_PLANE
elif(result &lt; 0):
  #BEHIND PLANE
elif(result &gt; 0):
  #IN FRONT OF PLANE

As for a pre-existing Blender/Python function… I haven’t bothered to look :o… if one does exist maybe someone else can point yah to it.

Problems! @notyourbody you are using an outdated blender, get a newer one ;-). Seen by you used vector * matrix … has changed into matrix * vector.
Next there is an orientation problem! Depending WHICH vertices you use of a plane you either are on the same side of the normal or just not!

Meaning Stun has to think about it … if you he checks several vertices he can find out if they are at the same side!
And if he says the origin is (or whatever suitable point in 3D) is at the front side of the plane he has fixed the orientation he wants to use.

Here the little bit adjusted code


import bpy
from mathutils import Vector
#find the plane's normal. p0, p1, and p2 are simply points on the plane (in world space)
p0 = bpy.data.objects["plane"].matrix_world * bpy.data.objects["plane"].data.vertices[1].co   #interchange 0 and 1 ... to see the effect!
p1 =  bpy.data.objects["plane"].matrix_world * bpy.data.objects["plane"].data.vertices[0].co  # and here
p2 =  bpy.data.objects["plane"].matrix_world * bpy.data.objects["plane"].data.vertices[2].co 
v1 = p0 - p1
v2 = p2 - p1
normal = v1.cross(v2)
normal.normalize()

#vertex you want to test (in world space)
vertex = Vector((123.0, 324.32, 12.43))

#actual test performed here
t = vertex - p0
t.normalize()
result = normal.dot(t)

EPSILON = 0.0000001

if(abs(result) &lt; EPSILON):
  #ON_PLANE
  print("on plane")
elif(result &lt; 0):
  print("behind")
elif(result &gt; 0):
  print("in front")

Many thanks,

the dot product is my friend!!
(I was lost in the depths of the geometry)

you want to know if a vertex is in front of or behind a plane?

Right behind not back :slight_smile:

Other good idea!
In the Python console:
C.active_object.data.faces[0].normal
gives (my example, default cube)
Vector((0.0, 0.0, -1.0))

Meaning: use the normal of your objects faces!