# Interfacing 3D surface mesh data with Numpy

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)
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.

MA

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”) )

for i in range(len(x)):
assert a[i] == x[i]

for i in range(len(y)):
assert b[i] == y[i][/code]

Best wishes,
Henry

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

Hi MA,
sorry, I misunderstood your original question. You can access the coordinates of all triangle vertices

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

Thanks Henry,
This helps. Any idea how to find the normal vectors?
Thanks

not from within netgen. You could always compute a normal as
$$\bf n = \frac{ e_1\times e_2}{\Vert e_1\times e_2 \Vert}$$
where
$$\bf e_i = v_i - v_3$$
for i = 1,2 are two of the triangle edges. But these might not be outward pointing.

Best wishes,
Henry