My goal is to figure out if a point exists within a 3D polygon. To do this here is what I am doing:

  1. Load obj mesh
from openmesh import *

points = []
triangles = []

mesh = read_trimesh(filename)
for (x, y, z) in mesh.points():
    points.append((float(x), float(y), float(z)))
for (v0, v1, v2) in mesh.face_vertex_indices():
    triangles.append((int(v0), int(v1), int(v2)))
  1. Use Raycast library to figure out if point exists inside of mesh
for x in range(5):
    for y in range(5):
        for z in range(5):
            is_inside = mesh.is_inside(x, y, z)

I realized that majority of points are returning false from is_inside. I then started to take a look at the mesh file and realized that all the vertices were less than 1:

v  0.1234 0.3644 0.4345
v  0.1234 0.3644 0.4313
v  0.1186 0.3493 0.4313
v  0.1186 0.3493 0.4345

So, that means that points like (0, 0, 9) would not exist in the mesh. I started reading up and it seems like I need to convert the model coordinate system to the world coordinate system. However, how do I define the world coordinate system? Would I have to create a transformation matrix?

  • \$\begingroup\$ What led you to choose the magic number 5 as the extent of your range? \$\endgroup\$
    – DMGregory
    Commented Mar 10, 2022 at 19:11
  • \$\begingroup\$ Was quicker to run, also tried it with 30 and 50 \$\endgroup\$
    – joethemow
    Commented Mar 10, 2022 at 19:53

1 Answer 1


You need to choose a different step for your for loops - currently your for loops are checking the co-ordinates at integer increments: (0, 0, 0), (0, 0, 1), ..., (4, 4, 4)

See https://docs.python.org/3/library/stdtypes.html#range for the full documentation.

The key part you will find useful is the definition of range range(start, stop[, step])

So, to find points inside the mesh between -1 and 1 with 0.1 step increments, you would do

for x in range(-1, 1.1, 0.1):
    for y in range(-1, 1.1, 0.1):
        for z in range(-1, 1.1, 0.1):
            is_inside = mesh.is_inside(x, y, z)

Transforming to world co-ordinates would be useful if you had a world co-ordinate system that defines the "position" of the mesh in the world, and the vertex positions are offsets from that, which doesn't appear to be the case.

  • \$\begingroup\$ The loop makes sense, but not every obj vertex would have the same scale. Meaning this obj would be on a scale from 0-1 but another obj file would use 0-100 \$\endgroup\$
    – joethemow
    Commented Mar 10, 2022 at 20:05
  • 1
    \$\begingroup\$ Well, you could first iterate over all the vertices in the obj file, find the maximum co-ordinate for each dimension, and then scale your min/max/step accordingly \$\endgroup\$
    – Arth
    Commented Mar 10, 2022 at 20:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .