Forum Message

 

 

We have moved the forum to https://forum.ngsolve.org . This is an archived version of the topics until 05/05/23. All the topics were moved to the new forum and conversations can be continued there. This forum is just kept as legacy to not invalidate old links. If you want to continue a conversation just look for the topic in the new forum.

Notice

The forum is in read only mode.

Interfacing 3D surface mesh data with Numpy

More
4 years 7 months ago #1898 by MA
Hello,
I'm a new user. So, sorry if my question is too basic.
I have this simple example

import netgen.csg as csg
geo = csg.CSGeometry()
sphere = csg.Sphere(csg.Pnt(0, 0, 0), 3)
geo.Add(sphere)
mesh = geo.GenerateMesh(maxh=1.5)

and I need to store in numpy arrays the information that I am able to store in an STL file without having to export and import again in STL format. Information consists of the coordinates of the 3 points of triangles and the normal vector.

Thank you for your help.

MA
More
4 years 7 months ago #1899 by hvwahl
Hi MA,

if it is specifically only a numpy array you need to store and reload, numpy has a save function . For more general objects (including ngsolve objects, such as GridFunctions, FESpaces...), you could use pythons pickle module:
Code:
import numpy as np import pickle x = np.array([1,2,3]) y = np.array([4,5,6]) pickle.dump([x,y], open("filename", "wb")) unpickler = pickle.Unpickler( open("filename", "rb") ) a,b = unpickler.load() for i in range(len(x)): assert a[i] == x[i] for i in range(len(y)): assert b[i] == y[i]

Best wishes,
Henry
More
4 years 7 months ago - 4 years 7 months ago #1902 by MA
Thank you Henry.
However, I want to avoid exporting my mesh into an STL file then read it again to get it stored into a numpy array.
Is there another way to get, for a 3D surface mesh, the coordinates of the 3 points of each elementary triangle (facet) and the local normal vector?
Thanks a lot for your time.
Regards,
MA
Last edit: 4 years 7 months ago by MA.
More
4 years 7 months ago #1904 by hvwahl
Hi MA,
sorry, I misunderstood your original question. You can access the coordinates of all triangle vertices
Code:
for el in mesh.Elements2D(): for i in range(3): print(mesh.Points()[el.vertices[i]])
but I guess this is not exactly what you are looking for.

Best wishes,
Henry
More
4 years 7 months ago #1906 by MA
Thanks Henry,
This helps. Any idea how to find the normal vectors?
Thanks
More
4 years 7 months ago #1909 by hvwahl
not from within netgen. You could always compute a normal as
[tex]\bf n = \frac{ e_1\times e_2}{\Vert e_1\times e_2 \Vert} [/tex]
where
[tex]\bf e_i = v_i - v_3 [/tex]
for i = 1,2 are two of the triangle edges. But these might not be outward pointing.

Best wishes,
Henry
Time to create page: 0.112 seconds