marv
marv

Reputation: 21

Create surface points with normals on a mesh in python

I basically would like to generate random surface points on a mesh of a 3D object including surface normals in python. I dont have a lot of experience in that field. So can anyone recommend me some packages, approaches, methods to solve the task?

Looked into open3d and trimesh, but still have some troubles.

Thank you!

Upvotes: 2

Views: 2806

Answers (2)

Mike
Mike

Reputation: 21

that's pretty straightforward with trimesh:

In [1]: import trimesh

In [2]: m = trimesh.creation.icosphere()

In [3]: m
Out[3]: <trimesh.base.Trimesh at 0x7f99bec7d550>

In [4]: m.sample?
Signature: m.sample(count, return_index=False)
Docstring:
Return random samples distributed normally across the
surface of the mesh

Parameters
---------
count : int
  Number of points to sample
return_index : bool
  If True will also return the index of which face each
  sample was taken from.

Returns
---------
samples : (count, 3) float
  Points on surface of mesh
face_index : (count, ) int
  Index of self.faces
File:      /media/psf/Dropbox/robotics/trimesh/trimesh/base.py
Type:      method

In [5]: points, index = m.sample(1000, return_index=True)

In [6]: points
Out[6]: 
array([[ 0.79934465,  0.58103816,  0.1308479 ],
       [-0.07373243,  0.08338232,  0.99055759],
       [ 0.71660325,  0.21369974,  0.65889903],
       ...,
       [-0.08330094,  0.98915598,  0.10205582],
       [ 0.2558548 , -0.68523377, -0.6770221 ],
       [-0.11483521,  0.97023335,  0.19696663]])


In [8]: normals = m.face_normals[index]

In [9]: normals
Out[9]: 
array([[ 0.79167915,  0.58957859,  0.16012871],
       [-0.04950537,  0.06905681,  0.99638365],
       [ 0.73810358,  0.21732806,  0.63872656],
       ...,
       [-0.06905681,  0.99638365,  0.04950537],
       [ 0.23921922, -0.7022584 , -0.67052763],
       [-0.08142553,  0.97123096,  0.22378629]])

You could get nicer normals by finding the barycentric coordinates of each point and then interpolating vertex normals, but just using the face normals is super easy.

Upvotes: 2

Frederik Bode
Frederik Bode

Reputation: 2744

I would use matplotlib. Maybe 'Tri-Surface plots' is what you need on this page here? You can CTRL+F Tri-Surface plots literally to quickly find it and look at the image it generates to see if it's what you are looking for.

Upvotes: 0

Related Questions