Skip to content

C++ implementation for computing occupancy grids and signed distance functions (SDFs) from watertight meshes.

Notifications You must be signed in to change notification settings

CRISZJ/mesh-voxelization

 
 

Repository files navigation

Mesh Voxelization

This is a parallel C++ implementation for efficiently voxelizing (watertight, triangular) meshes into signed distance functions (SDFs) and/or occupancy grids.

If you use this tool, please cite the following work:

@inproceedings{Stutz2018CVPR,
    title = {Learning 3D Shape Completion from Laser Scan Data with Weak Supervision },
    author = {Stutz, David and Geiger, Andreas},
    booktitle = {IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
    publisher = {IEEE Computer Society},
    year = {2018}
}
@misc{Stutz2017,
    author = {David Stutz},
    title = {Learning Shape Completion from Bounding Boxes with CAD Shape Priors},
    month = {September},
    year = {2017},
    institution = {RWTH Aachen University},
    address = {Aachen, Germany},
    howpublished = {http://davidstutz.de/},
}

Example of voxelizations.

Overview

SDFs: The implementation uses the triangle-point distance implementation from christopherbatty/SDFGen and triangle-ray intersection implementation by Tomas Akenine-Möller. To determine the distance of each voxel (its corner or center) to the nearest mesh face as well as its sign. Negative sign corresponds to interior voxels.

Publicly available marching cubes implementations, such as PyMCubes or skimage, can be used to derive triangular meshes at sub-voxel accuracy from the computed SDFs. Most implementations use the voxel corner as reference points; this tool supports both the voxel corner and its center. For a marching cubes implementation also using the center, see the voxel_centers branch of this PyMCubes fork.

Occupancy: Occupancy grids can either be derived from the computed SDFs (not included) or computed separately using triangle-box intersections; the code from Tomas Akenine-Möller is used. This has the advantage that voxels intersecting the mesh surface are definitely identified as occupied - this is not always the case when deriving occupancy from SDFs.

The computed SDFs and occupancy grids are saved using the HDF5 file format. C++ and Python utilities for reading this format are provided; as well as C++ and Python utilities for the used (triangular) mesh format: OFF.

Installation

Requirements for C++ tool:

  • CMake;
  • Boost;
  • HDF5;
  • Eigen;
  • OpenMP;
  • C++11.

Requirements for Python tool:

  • Numpy;
  • h5py;
  • PyMCubes or skimage.

On Ubuntu and related Linux distributions, these requirements can be installed as follows:

sudo apt-get install build-essential cmake libboost-all-dev libhdf5-dev libeigen3-dev

Make sure that the used compiler supports OpenMp and that the installed Eigen version include the unsupported Tensor module.

For using the Python tools, also make sure to install numpy, h5py and skimage (or PyMCubes):

pip install numpy
pip install h5py
pip install scikit-image

Follow the instructions here to install PyMCubes as alternative to skimage (however, only one of both is required).

To build, first adapt cmake/FindEigen3.cmake to include the correct path to Eigen3's include directory and remove NO_CMAKE_SYSTEM_PATH if necessary, and run:

mkdir build
cd build
cmake ..
make

To test the installation you can run (form within the build directory):

../bin/voxelize occ ../examples/input ../examples/output.h5

To obtain occupancy grids; or

../bin/voxelize sdf ../examples/input ../examples/output.h5

To obtain SDFs.

Also install MeshLab to visualize OFF files.

Usage

The general procedure can be summarized as follows:

  1. Scale the raw meshes to lie in [0, H] x [0, W] x [0, D] corresponding to the chosen resolution H x D x W.
  2. Voxelize the scaled meshes into SDFs or occupancy grids.
    1. Fill the occupancy grids if necessary.
  3. Convert the occupancy grids or SDFs to meshes for visualization.

The first step can be done using examples/scale_off.py which takes the meshes, for example from examples/raw, and first normalizes them to [-0.5,0.5]^3 to afterwards sale and translate them to [0, H] x [0, W] x [0, D]. This can be accomplished using:

python ../examples/scale_off.py ../examples/raw/ ../examples/input/

The meshes can then be voxelized; for details, check the --help option:

$ ../bin/voxelize --help
Allowed options:
  --help                produce help message
  --mode arg (=occ)     operation mode, 'occ' or 'sdf'
  --input arg           input, either single OFF file or directory containing 
                        OFF files where the names correspond to integers (zero 
                        padding allowed) and are consecutively numbered 
                        starting with zero
  --height arg (=32)    height of volume, corresponding to y-axis (=up)
  --width arg (=32)     width of volume, corresponding to x-axis (=right
  --depth arg (=32)     depth of volume, corresponding to z-axis (=forward)
  --center              by default, the top-left-front corner is used for SDF 
                        computation; if instead the voxel centers should be 
                        used, set this flag
  --output arg          output file, will be a HDF5 file containing either a N 
                        x C x height x width x depth tensor or a C x height x 
                        width x depth tensor, where N is the number of files 
                        and C=2 the number of channels, N is discarded if only 
                        a single file is processed; should have the .h5 
                        extension

The mode determines whether occupancy grids or SDFs are computed. For SDFs, --center indicates that the voxel's centers are to be used for SDF computation instead of the corners (by default); this has influence on the used marching cubes implementation. The output will be a N x H x W x D tensor as HDF5 file containing the occupancy grids or SDFs per mesh.

Note: The triangular meshes of the input OFF files should be watertight. This can, together with a simplification of the meshes, be acheived using Andreas Geiger's semi-convex hull algorithm which, however, imposes a rather crude simplification. The meshes should additionally be scaled to fit the volume used for voxelization. For the OFF files, the tool assumes the coordinate system x=right, y=up and z=forward; this means that the x and y axes are swapped for voxelization (in the output volume, the height is the first dimension).

Example

Using example meshes from ModelNet, two examples illustrate usage of the tools. In both cases, the meshes are first scaled:

python ../examples/scale_off.py ../examples/raw/ ../examples/input/

Here, we use the default resolution of 32^3.

SDFs

Given the scaled meshes, voxelization into SDFs is done as follows:

../bin/voxelize sdf ../examples/input ../examples/output.h5

In order to visualize the SDFs, marching cubes can be used to derive triangular meshes. As we used the default parameters, the voxels' corners were used for SDF computation, therefore, the standard PyMCubes can be used; alternatively, skimage can also be used:

python ../examples/marching_cubes.py ../examples/output.h5 ../examples/output/

Note that marching cubes might fail, e.g. with Surface level must be within volume data range., if the original mesh was not watertight (with significant holes) or structures within the outer surface prevents SDF computation. In this case, the SDF might not have negative values and marching cube fails.

Note that in low resolution, the reconstruction might look bad; try the same procedure with 64^3 to get significantly better results.

Occupancy Grids

Given the scaled meshes,

../bin/voxelize occ ../examples/input ../examples/output.h5

Is used to compute occupancy grids. Note that these are not "filled"; meaning that only the mesh surfaces are voxelized. Assuming the original shapes to be mostly watertight, the occupancy grids can be filled using a connected components algorithm as in examples/fill_occupancy.py:

python ../examples/fill_occupancy.py ../examples/output.h5 ../examples/filled.h5

The occupancy grids can be converted to meshes for visualization using

python ../examples/occ_to_off.py ../examples/filled.h5 ../examples/output/

License

Check:

The remaining code is licensed as follows:

Copyright (c) 2018 David Stutz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that this license does not include triangle_ray, see here for license information, triangle_point, see triangle_point/README.md for license information, and box_triangle, see here for license information. Also check Tronic/cmake-modules.

About

C++ implementation for computing occupancy grids and signed distance functions (SDFs) from watertight meshes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 61.7%
  • C 21.6%
  • CMake 16.7%