STL (Mesh) voxelization to Array

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
clel
Posts: 39
Joined: Mon Jun 13, 2016 10:30 am

STL (Mesh) voxelization to Array

Post by clel »

I am looking into ways to process CAD data with neural networks.

One way would be to voxelize the data and save it into a (numpy) array that can then be used as an input for a Keras 3D convolutional layer (for example).

I thought about writing a little script that takes the input data (STL) and iterates over a 3D grid checking for each point whether it is inside or outside the mesh. However, I did not find a function in FreeCAD for that. What I found was https://forum.freecadweb.org/viewtopic.php?t=30172 but I don't know whether it is the right thing for my needs, especially performance-wise. So hopefully there is a better way.

Also other hints how to do this outside FreeCAD will be appreciated.
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: STL (Mesh) voxelization to Array

Post by wmayer »

In the Mesh module we have a C++ class for a grid but at the moment this is not yet exposed to Python.
https://github.com/FreeCAD/FreeCAD/blob ... rid.h#L181
clel
Posts: 39
Joined: Mon Jun 13, 2016 10:30 am

Re: STL (Mesh) voxelization to Array

Post by clel »

Thanks for mentioning it. Unfortunately that seems to be too far away for me to achieve, since it is not exposed to Python and also I don't have an idea how to use it for grid generation from Mesh files and afterwards storing its values to an Array.

Probably a method like used in the thread I linked will be easier to adapt to achieve the goal?
fandaL
Posts: 440
Joined: Thu Jul 24, 2014 8:29 am

Re: STL (Mesh) voxelization to Array

Post by fandaL »

Seems that on Github there is many codes doing something related with voxeliyation https://github.com/search?q=voxelize By limiting to python you could find something easy to use.
clel
Posts: 39
Joined: Mon Jun 13, 2016 10:30 am

Re: STL (Mesh) voxelization to Array

Post by clel »

Thanks, that hint for GitHub was actually useful, although there do not seem to be much useful implementations in Python. There are pretty performant ones in C++, though. Unfortunately some require manual building which I am not capable of on my current machine, some only convert the shell of an object and not the inner parts and some require the output to be converted into the correct format. I did not find one that fit my needs perfectly. So if you think this is still possible in FreeCAD (performantly) than that might actually be the best option currently.
jbi
Posts: 117
Joined: Sun Apr 24, 2016 3:28 pm

Re: STL (Mesh) voxelization to Array

Post by jbi »

Maybe a way would be a regular 3d grid of points an then doing a "point cloud merge" (calcuate the signed distance field of the points to the stl mesh, and afterwards filter the negative or positive pts away) with the stl mesh in cloudcompare which should result in a point cloud which lies within the boundarys of the mesh. You can automate this process with python and the command line interface of cloudcompare. But as far as I understand you will soon hit a boundary with evenly sampled grids. Having 3 floats for a vector yielding 12 bytes in total for a point on a regular grid of 512^3 yielding 134 million points =1.61 GB (I do not know if so many points are possible in cloudcompare). But depending which resolution you need it may be possible.
fandaL
Posts: 440
Joined: Thu Jul 24, 2014 8:29 am

Re: STL (Mesh) voxelization to Array

Post by fandaL »

jbi wrote: Fri Feb 28, 2020 6:30 pm Maybe a way would be a regular 3d grid of points an then doing a "point cloud merge" (calcuate the signed distance field of the points to the stl mesh, and afterwards filter the negative or positive pts away) with the stl mesh in cloudcompare which should result in a point cloud which lies within the boundarys of the mesh. You can automate this process with python and the command line interface of cloudcompare. But as far as I understand you will soon hit a boundary with evenly sampled grids. Having 3 floats for a vector yielding 12 bytes in total for a point on a regular grid of 512^3 yielding 134 million points =1.61 GB (I do not know if so many points are possible in cloudcompare). But depending which resolution you need it may be possible.
I have tried it as an exercise:
https://github.com/fandaL/FreeCAD_macro ... oxelize.py
Example from mesh wb: Meshes -> Regular Solid -> Cone with Radius1=0
Just select the mesh and run the code from github
output for voxel_size=1.0 is

Code: Select all

array([[[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 1., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 1., 1., 0., 0., 0.],
        [0., 0., 0., 1., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 1., 1., 0., 0., 0.],
        [0., 0., 0., 1., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 1., 1., 0., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 0., 1., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 1., 1., 1., 1., 1., 0., 0.],
        [0., 1., 1., 1., 1., 1., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 0., 1., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 1., 1., 0., 0., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 0., 0., 1., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 1., 1., 1., 1., 1., 1., 0.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [0., 1., 1., 1., 1., 1., 1., 0.],
        [0., 0., 0., 1., 0., 0., 0., 0.]]])
Post Reply