Inside or Outside?? Mesh problem

Hi guys!
Thanks again for everything that you had done in this time.
Today my question is: how can i recognize if a point (it doesn’t matter if it’s really an empty or not) is inside or outside a mesh using a python script??
the mesh that i want to use is not a simple cube or cone but it could be a sweep nurbs or a loft nurbs or something else that have not a regular geometry.
Thanks again !

bpy.ops.mesh.normals_make_consistent, so you get all normals of the faces showing to the outside.
Now finding a face which is closest to that point (i have no idea how to determine close in this context).
Take the normal of this face, the direction from an arbitrary point of the face to your point and make a “mathutil.dotVecs” of those two vectors.
If the result is negativ, it means, the normal of the face is directing more than 90° away from the vector to your point, than your point is inside.

Hi flashnfantasy !!!
Thank you for the answer but there is a little problem in your reasoning:
http://www.kino3d.com/forum/viewtopic.php?f=7&t=8721
if i have a cube like that i can’t apply your method i suppose…! :confused:
Thanks again

the problem was the way you asked the question albiz1985. you have no ‘mesh’ , so "is inside or outside a mesh " is not what you are asking, if this is your image

the answer will depend on the geometry, if it’s simple geometry like cubes, you will probably find an answer fast (at least a nice approximation). fun to figure out, less fun to explain :slight_smile:

Hi zeffii !!
Maybe there is a misunderstood, i know that my explanations are very bad… :frowning:
My focus is to know if 2 MESH (with faces and edges) are intersected each other;
the post of flashnfantasy is good and wonderful so my question is: “is there another method that can be used also when 2 of that cubes are intersected??”

the answer will depend on the geometry, if it’s simple geometry like cubes, you will probably find an answer fast (at least a nice approximation)

Thank you very much :cool:

Hi flashnfantasy !!!
Are you sure about the code???
When i write it an error message appear: “Operator bpy.ops.mesh.normals_make_consistent.poll() failed, context is incorrect” :frowning:
I know that it’s well writed but it doesn’t work and i don’t know why…

I came up with this a while ago (with help).



def pointInsideMesh(point,ob):
    # provided by aothms.
    # axes = [ mathutils.Vector((1,0,0)), mathutils.Vector((0,1,0)), mathutils.Vector((0,0,1))  ]
    # @Abel, ok just one then
    axes = [ mathutils.Vector((1,0,0)) ]
    outside = False
    for axis in axes:
        # @Atom you're right, ray_cast is in object_space
        # http://www.blender.org/documentation/250PythonDoc/bpy.types.Object.html#bpy.types.Object.ray_cast
        mat = mathutils.Matrix(ob.matrix_world).invert()
        orig = mat*point
        count = 0
        while True:
            location,normal,index = ob.ray_cast(orig,orig+axis*10000.0)
            if index == -1: break
            count += 1
            orig = location + axis*0.00001
        if count%2 == 0:
            outside = True
            break
    return not outside

Hi Atom !!!
Thanks for the answer !!!
I just write this code:

import bpy
import mathutils
from mathutils import Vector

def Dist(V):
    P=Vector((5,4,2))
    (V - P).length

def Choice(A):
    B=[]
    for i in range(len(A)):
        B.append(Dist(A[i]))
    M=min(B)
    I=B.index(M)
    return I

def run():
    D=[]
    cu=bpy.context.object
    me=bpy.data.meshes[cu.name]
    N=me.faces
    for i in range(len(N)):
        D.append(N[i].center)
    V=Choice(D)
    #N[V] is the nearest face to P
    a=(N[V].normal).dot((N[V].center-P))
    if (a<0):
        print("inside")
    else:
        if(a==0):
            print("face")
        else:
            print("outside")

def Start():
    run()

Now i’m not sure about the value of “a” in run function.
Thanks !!!
Edit: Hi Atom! your script seems not working in orig=mat*point… Matrix and vector are imported but …