Python bindings for a C++ library to convert mesh models into Voxel images with OpenMp and CUDA
Project description
Details
Cad2Vox is a Python package to efficiently perform mesh voxelisation on GPU (using CUDA) or CPU (using OpenMP) for surface and volume cad meshes based on triangles and tetrahedrons respectively.
The code itself consists of two Python packages, cad2vox and Cudavox. CudaVox is a python package that provides bindings to c++ code using pybind11 and xtensor python (using CUDA to perform calculations on a GPU and OpenMP to perfrom them in parallel on a multi-core CPU). Cad2vox meanwhile is a pure python package that reads in and wrangles the mesh/greyscale data using meshio and acts as a user interface to Cudavox.
This project is a fork of cuda_voxelizer (https://github.com/Forceflow/cuda_voxelizer) the original plan was to simply add bindings to allow us to call it from python. However as my research project progressed the code has since ballooned into its own thing (adding support for volume meshes and using meshio and xtensor instead of trimesh). Thus it feels more appropriate to release it as a standalone project.
For Surface meshes (based on triangles) CudaVox implements an optimised version of the method described in M. Schwarz and HP Seidel's 2010 paper Fast Parallel Surface and Solid Voxelization on GPU's.
For volume meshes (based on Tetrahedrons) it uses a simple algorithm to check if a point P (taken as the centre of the voxel) is inside a tetrahedron defined by 4 vertices (A,B,C,D). This is achieved by calculating the normal of the four triangles that make up the surface of the tetrahedron. Since these vectors will all point away from the centre of the tetrahedron we can simply check to see if the point P is on the opposite side of the plane for each of the four triangles. if this is true for all 4 planes then the point must be inside the tetrahedron (see https://stackoverflow.com/questions/25179693/how-to-check-whether-the-point-is-in-the-tetrahedron-or-not/51733522#51733522 for examples of this algorithm implemented in python).
Installing
cad2vox and CudaVox are both avalible as pip packages through pypi and as such can be installed with:
pip install cad2vox
Note: CudaVox is listed a requirement for cad2vox so will be installed automatically by pip.
Building from source
Dependencies
The project has the following build dependencies:
It also has the following optional dependency (see building without CUDA for details):
- Nvidia Cuda 8.0 (or higher) for CUDA + Thrust libraries.
You will also need the following python packages:
- cmake
- numpy
- pybind11
- tifffile
- xtensor
- xtl
- xtensor-python
- meshio
- pytest
- pandas
- pillow
We recommend using anaconda as the python dependencies are all available through the conda package manager (as well as CUDA through cudatookit). These can be installed with the following two commands.
conda install cmake numpy pybind11 tifffile pillow cudatoolkit
conda install -c conda-forge xtensor xtl meshio xtensor-python
If however, you wish to use pure python many of the packages are available through pip and you can obtain them using:
pip install -r requirements.txt
You will however, need to build xtl, xtensor and xtensor-python from source using cmake. Instructions for which can be found here:
Once you have the dependencies installed you can use the setup.py scripts to build and install the two packages as:
python3 Cudavox/setup_cudavox.py install
python3 setup_cad2vox.py install
Building without CUDA
It is possible to build cad2vox without CUDA or a GPU. To do this simply install all the requirments except CUDA (or cudatoolkit if using ananconda) and build as above i.e.
python3 Cudavox/setup_cudavox.py install
python3 setup_cad2vox.py install
When building CudaVox Cmake will detect if CUDA is installed and configured corectly. If Cmake finds a sutible instalation of CUDA It will then automatically include all the aditional sorce and headerfiles nessacry to perfrom caculations on ether the GPU or CPU. If CUDA is not installed it will compile CudaVox to do caculations on the CPU only, using OpenMP. This is intened to provide options to the end user as CUDA is a rather large (not to mention proprietary) depenedecy that some users may not want/need.
The OpenMP version is by comparison considerably slower, however, OpenMP is much more compatible and not tied to Nvidia hardware (pretty much any modern CPU and compiler should suport OpenMP out of the box). OpenMP is also useful for calculationbs that wont fit in VRAM. Given that the memory requirements are in our experiance the main bottleneck and the required memory also scales cubically with Gridsize. Memory can very quickly become a limiting factor.
Automated testing
It is good practice once Cad2vox is built and installed to test the functionallity. To this end we have included an automated test suite using pytest that can be run as follows in the root directory of cad2vox:
pytest
This will test the code under a variety of differnt senarios and if your setup is working correctly should all pass (for those curious souls who wish to see what we are testing the test functions are stored in the tests sub directory).
Note: if you are NOT USING CUDA some of the tests may fail. However, you can skip any tests related to CUDA with:
pytest -k "not CUDA"
Usage
The main user facing python function from Cad2Vox is voxelise.
The information about how to call/use the voxelise function can be viewed at anytime through python by calling:
import cad2vox
help(cad2vox.voxelise)
voxelise(input_file, output_file, greyscale_file=None, gridsize=0, unit_length=-1.0, use_tetra=False, cpu=False, solid=False, im_format=None)
Wrapper Function to setup the CudaVox python bindings for the C++ code and provide the main user
interface.
This function will first try to perform the voxelisation using a CUDA capible GPU. If that fails
or CUDA is unavalible it will fallback to running on CPU with the maximum number of avalible
threads.
Parameters:
input_file (string): Hopefully self explanitory, Our recomended (i.e. tested) format is Salome
med. However, theortically any of the aprrox. 30 file formats suported by meshio will
work. Provided they are using either tetrahedrons or triangles as there element type
(see https://github.com/nschloe/meshio for the full list).
output_file (string): Filename for output as 8 bit greyscale images. Note do not include the
extension as it will automatically be appended based on the requested image format.
The default is a virtual tiff stack other formats can be selected with the im_format option.
greyscale_file (string/None): csv file for defining custom Greyscale values. If not given the
code evenly distributes greyscale values from 0 to 255 across all materials defined in the
input file. It also auto-generates a file 'greyscale.csv' with the correct formatting which
you can then tweak to your liking.
gridsize (+ve int): Number of voxels in each axis. That is you get a grid of grisize^3 voxels
and the resulting output will be a series of gridsize by gridside images. Note: if you set
this to any postive interger except 0 it will calculate unit length for you based on the max
and min of the mesh so in that case you don't set unit_length. i.e. leave unit_length at
it's default value. (see unit_length for details).
unit_length (+ve non-zero float): size of each voxel in mesh co-ordinate space. You can define
this instead of Gridsize to caculate the number of voxels in each dimension, again based on max
and min of the mesh grid. Again if using Gridsize leave this a default value (i.e. -1.0).
use_tetra (bool): flag to specifically use Tetrahedrons instead of Triangles. This only applies
in the event that you have multiple element types defined in the same file. Normally the code
defaults to triangles however this flag overides that.
cpu (bool): Flag to ignore any CUDA capible GPUS and instead use the OpenMp implementation.
By default the code will first check for GPUS and only use OpenMP as a fallback. This flag
overrides that and forces the use of OpenMP. Note: if you wish to use CPU permenantly,
as noted in the build docs, you can safely compile CudaVox without CUDA in which case the code
simply skips the CUDA check altogether and permenantly runs on CPU.
Solid (bool): This Flag can be set if you want to auto-fill the interior when using a Surface
Mesh (only applies to Triangles). If you intend to use this functionality there are three
Caveats to briefly note here:
1) This flag will be ignored if you only supply Tetrahedron data or set use_tetra since in
both cases that is by definition not a surface mesh.
2) The algorithm currently used is considerably slower and not robust (can lead to artifacts and
holes in complex meshes).
3) Setting this flag turns off greyscale values (background becomes 0 and the mesh becomes 255).
This is because we dont have any data as to what materials are inside the mesh so this seems a
sensible default.
The only reason 2 and 3 exist is because this functionaly is not activley being used by our
team so there has been no pressing need to fix them. However, if any of these become an
issue either message b.j.thorpe@swansea.ac.uk or raise an issue on git repo as they can easily
be fixed and incorporated into a future release.
im_format (string): The default output is a Tiff virtual stack written using tiffle. This option
however, when set allows you to output each slice in z as a seperate image in any format supported
by Pillow (see https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html for the full
list). Simply specify the format you require as a sting e.g. "png" Note: this has only been fully tested
with png and jpeg so your millage may vary.
Citation
If you use Cad2Vox in your published paper or other software, please reference it, for example as follows:
@Misc{CAD2VOX, author = "Dr Benjamin Thorpe", title = "Cad2Vox", howpublished = "\url{https://github.com/bjthorpe/Cad2vox}", year = "2022"}
If you end up using Cad2Vox in something cool I'd be interested to hear about it so feel free to drop me an e-mail: b,.j.thorpe@swansea.ac.uk
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file CudaVox-1.7.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: CudaVox-1.7.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 439.4 kB
- Tags: PyPy, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0193cf8242bc8c97a149f31cbf134ab3eee9827735b21a8216bd0482ab8562fe |
|
MD5 | e31dc7c36f9ecd6bb3bfc0fce8870726 |
|
BLAKE2b-256 | c9e29d90522c041d7be8ea64a5922faf56b5a19e2b3c3f2b2b3da1c41211345b |
File details
Details for the file CudaVox-1.7.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: CudaVox-1.7.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 439.4 kB
- Tags: PyPy, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76206e8eb4529d6ee646448cf08f22ad6733f6db64569d907ac8c554f272450b |
|
MD5 | b559f7636062953dfefbc07cac755227 |
|
BLAKE2b-256 | b4049b0c87341948d8dfb8c54a50dbddf6f6e463de7498123aad7eeba707b244 |
File details
Details for the file CudaVox-1.7.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: CudaVox-1.7.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 438.8 kB
- Tags: PyPy, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0c0cf0259fcf9187a7c54c48db41a69a178d97bac4684245878581e9c7ae140 |
|
MD5 | dbbf554ccae54769de212c2726e42854 |
|
BLAKE2b-256 | 6237d603fed7eb5cda5d250db456025e2646fa4ca25ac082c5ab46724724667c |
File details
Details for the file CudaVox-1.7.0-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: CudaVox-1.7.0-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 663.7 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18331f79f6a247ada69ed1b015b17a22b889a8e2f6aad39d96836c22c9004736 |
|
MD5 | 55178699da3f26481d867242b2bde042 |
|
BLAKE2b-256 | 2085ff8ce50e019df72e1f41bd592ff37485a8772d5363095ac428a30a3d7838 |
File details
Details for the file CudaVox-1.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: CudaVox-1.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 438.0 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef275de4b67d4dd55cf79c9c46840a56ce1569e8b7e601ba434e49947729f71a |
|
MD5 | e72d6040f3b25177a85ae4054aec54c4 |
|
BLAKE2b-256 | c1760e8c71ee3973913056a549e0e78f807c4bfa6352cb4456b6869eef664860 |
File details
Details for the file CudaVox-1.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: CudaVox-1.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 438.1 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74be6ce32cb18acebfc2d6366cfc37d0c274178c1b7261a5d6aebb94d2218550 |
|
MD5 | 1b9ab42b8822d35bd60732128d96e5ae |
|
BLAKE2b-256 | 86bda9145dd8ce48328dc9daf179b165975fa29a4894ed19fff4a68fc80cb32b |
File details
Details for the file CudaVox-1.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: CudaVox-1.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 438.3 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f83195e3a5751e80b87c305d2698a9da37f9840b614d536e7546fa4cc0e9a4c2 |
|
MD5 | dc35b6a9652231e4bd89185e1cd82503 |
|
BLAKE2b-256 | eb9c49165795b112e8257db2064cdffc817ea3945ff810c20e79fcd1bc04bf23 |
File details
Details for the file CudaVox-1.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: CudaVox-1.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 437.8 kB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d35fe2de45f1b98196632d6526c277a550675153c31bffed56e413542fbb4c8 |
|
MD5 | 100527c1c7066c067fafffcd1046e272 |
|
BLAKE2b-256 | 0fde112e0b7a9d4e8ddf5d3d8bd9f804bdd0c5a25effaab54c9d5ba27519c88e |
File details
Details for the file CudaVox-1.7.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: CudaVox-1.7.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 438.4 kB
- Tags: CPython 3.7m, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6560bb138db41cd1ebfe330745e62977778aac7cc927b72e3cfd84f78886b8ba |
|
MD5 | d843fefea260ace69a21fce50ca7113c |
|
BLAKE2b-256 | a950cdc8a59479f012212df178b74d547d5046828544a04d3550c9ccf593284b |
File details
Details for the file CudaVox-1.7.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: CudaVox-1.7.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 438.4 kB
- Tags: CPython 3.6m, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20770dd980c8ee89905d8bfa3c1206ba5b82dacc656f02ffb04a649c6f418635 |
|
MD5 | 9c0bd8f261ae637735e27d2e3108413d |
|
BLAKE2b-256 | 79afc8fe0ad558ff98e4847992767525db4e2c76b0bde07cb740c78bf9d22a6c |