marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
1.98k stars 257 forks source link

Normal vectors of meshes are wrong #1032

Closed Oguked closed 5 months ago

Oguked commented 5 months ago

When the mesh is not in the origin, then the normal vectors are not perpendicular w.r.t. to the mesh surface.

image

"""Draw color arrow glyphs"""
from vedo import *

# Create two spheres with different radii, wireframes, 
# and colors, and set the position of one of them
s1 = Sphere(pos=[100,0,0], r=10, res=8).wireframe().c('white')

# Get the coordinates of the vertices of each sphere
start = s1.points()  
normal = s1.compute_normals(points=True, cells=False).points() 
end = start + normal*0.1 

# --- color can be a colormap which maps arrow sizes
# Define a title for the first set of arrows,
#  and create an Arrows object with coordinates and a colormap for color
t1 = 'Color arrows by size\nusing a color map'
a1 = Arrows(start, end, c='red', alpha=1., s=1)
a1.add_scalarbar(c='w')

# Display two groups of objects on two renderers: the two spheres, 
# the Arrows object with a colormap for color and a scalar bar, 
# and the title for the first set of arrows on one renderer; 
# the two spheres, the Arrows object with random RGB colors, 
# and the title for the second set of arrows on another renderer
show([(s1, a1, t1)], N=1, bg='bb', bg2='lb').close()
marcomusy commented 5 months ago

Hi @Oguked I would write it like this:

pip install -U vedo

then

from vedo import *

s1 = Sphere(pos=[100,0,0], r=10, res=8).wireframe().c('white')
s1.compute_normals(points=True, cells=False)

# Get the coordinates of the vertices of each sphere
start = s1.vertices
end = start + s1.vertex_normals * (5 + np.random.randn(len(start), 3))

t1 = 'Color arrows by size\nusing a color map'
a1 = Arrows(start, end, c="viridis", s=2, res=24).lighting("plastic")
a1.add_scalarbar(c='w')

show(s1, a1, t1, bg='bb', bg2='lb').close()

Screenshot from 2024-01-23 14-22-35