mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
2.93k stars 573 forks source link

Incorrect output of the triangle_id #1131

Open Herzdame opened 3 years ago

Herzdame commented 3 years ago

`import pywavefront import trimesh import numpy as np from trimesh import proximity from trimesh import util from trimesh import triangles

model = pywavefront.Wavefront('C:/Users/Darija/Documents/Modell.obj', collect_faces=True)

print(model.vertices) # gibt dir alle Punkte aus

for mesh in model.mesh_list: # Die Flächen "faces" stecken in den "meshes" print(mesh.faces)

mesh = trimesh.Trimesh(model.vertices, mesh.faces)

ray_origins = np.tile(np.array([2.0,6.5,1.5]), (1000,1))

ray_directions = np.random.uniform(-1, 1, (1000,3))

print(mesh.ray.intersects_location.doc) print(ray_directions) locations, index_ray, index_tri = mesh.ray.intersects_location( ray_origins = ray_origins, ray_directions = ray_directions, multiple_hits=False)

print('Strahl trifft Mesh bei folgender Koordinate:\n', locations)

ray_visualize = trimesh.load_path(np.hstack((ray_origins, ray_origins + ray_directions*5.0)).reshape(-1, 2, 3))

unmerge so viewer doesn't smooth

mesh.unmerge_vertices()

make mesh white- ish

mesh.visual.face_colors = [255,255,255,255] mesh.visual.face_colors[index_tri] = [255, 0, 0, 255]

create a visualization scene with rays, hits, and mesh

scene = trimesh.Scene([mesh,

                   #ray_visualize])

show the visualization

scene.show()

p = np.loadtxt('C:/Users/Darija/PycharmProjects/Punktwolke/Punktwolke_Raum1_asbuilt_5000_S.txt')

faces = mesh.triangles[index_tri]

print('Triangles:', mesh.triangles)

def closest_point_naive2(mesh, points): """ Given a mesh and a list of points find the closest point on any triangle.

Does this by constructing a very large intermediate array and
comparing every point to every triangle.

Parameters
----------
mesh : Trimesh
  Takes mesh to have same interfaces as `closest_point`
points : (m, 3) float
  Points in space

Returns
----------
closest : (m, 3) float
  Closest point on triangles for each point
distance : (m,) float
  Distances between point and triangle
triangle_id : (m,) int
  Index of triangle containing closest point
"""
# get triangles from mesh
triangles = mesh.triangles[index_tri]
# establish that input points are sane
points = np.asanyarray(points, dtype=np.float64)
if not util.is_shape(triangles, (-1, 3, 3)):
    raise ValueError('triangles shape incorrect')
if not util.is_shape(points, (-1, 3)):
    raise ValueError('points must be (n,3)')

# create a giant tiled array of each point tiled len(triangles) times
points_tiled = np.tile(points, (1, len(triangles)))
on_triangle = np.array([proximity.closest_point_corresponding(
    triangles, i.reshape((-1, 3))) for i in points_tiled])

# distance squared
distance_2 = [((i - q)**2).sum(axis=1)
              for i, q in zip(on_triangle, points)]

triangle_id = np.array([i.argmin() for i in distance_2])

# closest cartesian point
closest = np.array([g[i] for i, g in zip(triangle_id, on_triangle)])
distance = np.array([g[i] for i, g in zip(triangle_id, distance_2)]) ** .5

return closest, distance, triangle_id

dist = closest_point_naive2(mesh, p)

print(index_tri)

print(mesh.triangles[index_tri])`

When I run the code it gives me triangle_ids that are not in mesh.triangles [index_tri]. I don't know where the problem is. Can someone help me?

kickd0wn commented 1 year ago

Hi,

I hope that this answer may help you.

According to my understanding, Triangle ID outputs the names or the numbers of the triangles in the overall 3d model that are closest to the points in question. If you have a mesh with 5000 triangles, you will get a column with the "name/number" of each triangle between #0 and #4999 that is closest to a point.

The function mesh.triangles returns the coordinates of the vertices that span the respective triangle in the 3D model. So in this example, you should get an array with 5000, 3, 3 values. Each triangle is described by 3 points with X, Y and Z values.

What helped me was to analyze the 3d model in Meshlab. Import it, turn the wireframe view on and press the Get Info button. There you will see, that each triangle has an ID and 3 vertices.

I hope that I could help you further :)