Fast algorithms to compute an approximation of the minimal volume oriented bounding box of a point cloud in 3D.

Overview

ApproxMVBB

C++ Deps System

Status

Build UnitTests
Build Status Build Status

Homepage


Fast algorithms to compute an approximation of the minimal volume oriented bounding box of a point cloud in 3D.

Computing the minimal volume oriented bounding box for a given point cloud in 3D is a hard problem in computer science. Exact algorithms are known and of cubic order in the number of points in 3D. A faster exact algorithm is currently not know. However, for lots of applications an approximation of the minimum volume oriented bounding box is acceptable and already accurate enough. This project was developed for research in Granular Rigidbody Dynamics. This small standard compliant C++11 library can either be built into a shared object library or directly be included in an existing C++ project.

I am not especially proud of the underlying code as it was written years ago, nevertheless consider PR for refactoring and clean ups are very welcome!

This library includes code for :

  • computing an approximation of an oriented minimal volume box (multithreading support: OpenMP),
  • computing the convex hull of a point cloud in 2d,
  • computing the minimal area rectangle of a 2d point cloud,
  • 2d projections of point clouds,
  • fast building a kD-Tree (n-dimensional, templated) with sophisticated splitting techniques which optimizes a quality criteria during the splitting process,
  • computing the k-nearest neighbors to a given point (kNN search) via kd-Tree.
  • fast statistical outlier filtering of point clouds via (nearest neighbor search, kD-Tree).


Installation & Dependencies

To build the library, the tests and the example you need the build tool cmake. This library has these light-weight required dependencies:

  • Eigen at least version 3.
    • With homebrew or linuxbrew: brew install eigen3
  • meta
    • Install optional: Gets downloaded and used during build.

and theses optional dependecies:

  • pugixml
    • With homebrew or linuxbrew: brew install pugixml
    • install with #define PUGIXML_HAS_LONG_LONG enabled in pugiconfig.hpp.
    • only needed if cmake variable ApproxMVBB_XML_SUPPORT=ON (default=OFF).
  • python3 only needed for visualization purposes.

Download these and install it on your system.

Download the latest ApproxMVBB code:

    git clone https://github.com/gabyx/ApproxMVBB.git ApproxMVBB

Make a build directory and navigate to it:

    mkdir Build
    cd Build

Invoke cmake in the Build directory:

    cmake ../ApproxMVBB

The cmake script tries to find Eigen,meta and pugixml If you installed these in a system wide folder (e.g /usr/local/) this should succeed without any problems. In the CMakeCache.txt file (or over the console by -D<variable>=ON) you can specify what you want to build, the following options are availabe:

  • ApproxMVBB_BUILD_LIBRARY,
  • ApproxMVBB_BUILD_TESTS
  • ApproxMVBB_BUILD_EXAMPLE
  • ApproxMVBB_BUILD_BENCHMARKS
  • etc. See the marked red options after configuring in cmake-gui.

To install the library and the header files at a specific location /usr/local/ run cmake with:

    cmake -DCMAKE_INSTALL_PREFIX="/usr/local/" ../ApproxMVBB

Finally, build and install the project:

    make all
    make install

By default the multithreading support is enabled if OpenMP is found! (see Multithreading Support) To build in parallel use the -jN flag in the make command, where Ndenotes the number of parallel threads to use, or use the Ninja Generator which already uses maximum threads your system offers.

CMake FindScripts The installation installs also scripts approxmvbb-config.cmake and approxmvbb-config-version.cmake into the lib/cmake folder. To include the library in another project the only thing you need to add in your cmake script is

    find_package(ApproxMVBB [version] [COMPONENTS [SUPPORT_KDTREE] [SUPPORT_XML] ] [Required] )

which defines the following targets if ApproxMVBB has been found successfully:

    ApproxMVBB::Core            # Main target to link with!
    ApproxMVBB::KdTreeSupport   # Optional target for KdTree support to link with (only available if installed with this supported!)
    ApproxMVBB::XMLSupport      # Optional target for XML support to link with (only available if installed with this supported!)

The components SUPPORT_KDTREE additionally loads the dependency meta for the KdTree.hpp header and SUPPORT_XML loads pugixml for the KdTreeXml.hpp header.

If you installed the library into non-system generic location you can set the cmake variable $ApproxMVBB_DIR before invoking the find_library command:

    set(ApproxMVBB_DIR "path/to/installation/lib/cmake")
    find_package(ApproxMVBB [version] [Required] )

See the example example/libraryUsage which should be configured as a separate build, and the example example/kdTreeFiltering for more information on how to set up the dependencies!


Supported Platforms

The code has been tested on Linux and OS X with compilers clang and gcc. It should work for Windows as well, but has not been tested properly. Under Visual Studio 15 it seems to build.


Example Usage: Approximation MVBB

Please see the example/approxMVBB/main.cpp in the source directory. Given a point cloud with n=10000 points sampled in the unit cube in 3D we compute an approximation of the minimum volume bounding volume by the following calls:

    #include <iostream>
    #include "ApproxMVBB/ComputeApproxMVBB.hpp"

    int  main(int argc, char** argv)
    {
          ApproxMVBB::Matrix3Dyn points(3,10000);
          points.setRandom();
          ApproxMVBB::OOBB oobb = ApproxMVBB::approximateMVBB(points,0.001,500,5,0,5);
          oobb.expandToMinExtentRelative(0.1);
          return 0;
    }

The returned object oriented bounding box oobb contains the lower oobb.m_minPoint and upper point oobb.m_maxPoint expressed in the coordinate frame K of the bounding box. The bounding box also stores the rotation matrix from the world frame to the object frame K as a quaternion oobb.m_q_KI . The rotation matrix R_KI from frame I to frame K can be obtained by oobb.m_q_KI.matrix() (see Eigen::Quaternion). This rotation matrix R_KI corresponds to a coordinate transformation A_IK which transforms coordinates from frame K to coordinates in frame I. Thereforce, to get the lower point expressed in the coordinate frame I this yields:

    ApproxMVBB::Vector3 p = oobb.m_q_KI * oobb.m_minPoint  // A_IK * oobb.m_minPoint

Degenerate OOBB: The returned bounding box might have a degenerated extent in some axis directions depending on the input points (e.g. 3 points defines a plane which is the minimal oriented bounding box with zero volume). The function oobb.expandToMinExtentRelative(0.1); is a post processing function to enlarge the bounding box by a certain percentage of the largest extent (if existing, otherwise a default value is used).

Points Outside of the final OOBB: Because the algorithm works internally with a sample of the point cloud, the resulting OOBB might not contain all points of the original point cloud! To compensate for this an additional loop is required:

    ApproxMVBB::Matrix33 A_KI = oobb.m_q_KI.matrix().transpose();
    auto size = points.cols();
    for( unsigned int i=0;  i<size; ++i ) {
        oobb.unite(A_KI*points.col(i));
    }

Function Parameters & How It Works: The most important function:

    ApproxMVBB::approximateMVBB(pts,
                                epsilon,
                                pointSamples,
                                gridSize,
                                mvbbDiamOptLoops,
                                mvbbGridSearchOptLoops)

computes an approximation of the minimal volume bounding box in the following steps:

  1. An approximation of the diameter (direction which realizes the diameter: z ) of the points pts is computed. The value epsilon is the absolute tolerance for the approximation of the diameter and has the same units as the points pts (in the example 0.001 meter)
  2. The points are projected into the plane perpendicular to the direction z
  3. An approximation of the diameter of the projected points in 2D is computed (direction x )
  4. The initial approximate bounding box A is computed in the orthogonal frame [x,y,z]
  5. A first optional optimization loop is performed (parameter mvbbDiamOptLoops specifies how many loops) by computing the minimal volume bounding box over a direction t where the direction t is choosen sequentially from the current optimal bounding box solution. The algorithm starts with the directions of the box A. This optimization works with all points in pts and might use a lot of time
  6. The initial bounding box A is used as a tight fit around the points pts to compute a representative sample RS of the point cloud. The value pointSamples defines how many points are used for the exhaustive grid search procedure in the next step
  7. An exhaustive grid search (value gridSize specifies the x,y,z dimension of the grid defined by the bounding box A) is performed. This search is a simple loop over all grid directions (see Gill Barequet, and Sariel Har-Peled [1]) to find a even smaller bounding box. For each grid direction g the minimal bounding box of the projected points in direction g is computed. This consists of finding the minimal rectangle (axis u and v in world frame) of the projected point cloud in the plane perpendicular to direction g. The minimal bounding box G in direction g can be computed from the basis (u,v,g) and is a candidate for the overall minimization problem. Each found bounding box candidate G and its directions (u,v,g) can be used as a starting point for a second optional optimization loop (parameter mvbbGridSearchOptLoops, same algorithm as in step 5 but with less points, namely RS ).
  8. The final approximation for the minimal volume bounding box (minimal volume over all computed candidates) is returned. 💩

Example Usage: Generating a KdTree and Outlier Filtering

The library includes a fast KdTree implementation (which is not claimed to be ultimativly fast and absolutely memory efficient, but was written to fulfill this aspects to a certain level, real benchmarks still need to be done, the implementation can really well compete with famous implementations such as PCL(FLANN),ANN, and CGAL ) The KdTree splitting heuristic implements an extendable sophisticated splitting optimization which in the most elaborate, performance worst case consists of searching for the best split between the splitting heuristics MIDPOINT , MEDIAN and GEOMETRIC_MEAN by evaluating a user-provided quality evaluator. The simple standard quality evaluator is the LinearQualityEvaluator which computes the split quality by a weighted linear combination of the quantities splitRatio , pointRatio, minMaxExtentRatio.

Outlier filtering is done with the k-nearest neighbor search algorithm (similar to the PCL library but faster, and with user defined precision) and works roughly as the following: The algorithm finds for each point p in the point cloud k nearest neighbors and averages their distance (distance functor) to the point p to obtain a mean distance distance for this particular point. All nearest mean distances for all points give a histogram with a sample mean mean and sample standard deviation stdDev. All points which have a mean nearest neighbor distance greater or equal to mean + stdDevMult * stdDev are classified as outlier points.

Look at the examples in examples/kdTreeFiltering which produced the following pictures with the provided visualization notebook examples/kdTreeFiltering/python/VisualizeKdTree.ipynb.

Function Parameters & How It Works To come


Building and Visualizing the Tests

Building and installing the basic tests is done by:

    cd ApproxMVBB
    git submodule init
    git submodule update
    cd ../Build
    make build_and_test

**Note that if the tests fail, submit a new issue and report which test failed. The results can still be visualized and should be correct. **

Note: To run the test in high-performance mode (needs lots of ram), which tests also points clouds of 140 million points and some polygonal statue lucy.txt successfully you need to set the cmake variable ApproxMVBB_TESTS_HIGH_PERFORMANCE to ON and additionally initialize the submodule additional and unzip the files:

     cd ApproxMVBB
     git submodule init
     git submodule update
     cd additional/tests/files; cat Lucy* | tar xz

and rebuild the tests. (this will copy the additional files next to the executable)

Executing the test application cd tests; ./ApproxMVBBTests will then run the following tests:

  1. Testing the ConvexHull2D for several point clouds in 2D
  2. Minimal area rectangle tests for several point clouds in 2D
  3. Testing the diameter computation and calculation of the initial bounding box A (see [section](Function Parameters & How It Works)) for point clouds in 3D
  4. Testing the full optimization pipeline to generate an approximation of the minimal volume bounding box

The output can be visualized with the ipython notebook /tests/python/PlotTestResults.ipynb:

    cd Build/tests
    ipython noteboook


Benchmark

Here are some short benchmarks (single core) from the tests folder:

Point Cloud # Points ~ CPU Time approximateMVBB
Standford Bunny 35'945 0.91 s
Standford Lucy 14'027'872 1.19 s
Unit Cube 140'000'000 7.0 s

approximateMVBB runs approximateMVBBDiam and performs a grid search afterwards (here 5x5x5=25 directions with 5 optimization runs for each) It seems to take a long time for 140 million points. The most inefficient task is to get a good initial bounding box. This takes the most time as diameter computations are performed in 3d and then all points are projected in the found diameter direction in 3d and another diameter in the projected plane in 2d is computed. Afterwards the point cloud is sampled (not just random points, its done with a grid) and convex hull, minimal rectangle computations are performed over the grid directions. These algorithms could be made faster by exploiting the following things:

  • Use an axis aligned bounding box as the initial bounding box for the grid search (not implemented yet)
  • Parallelism for the projection -> (CUDA, threads)

Multithreading Support

You can build the library with OpenMP (by default enabled) You can set the cmake cache variables ApproxMVBB_OPENMP_USE_OPENMP=On which will further enable ApproxMVBB_OPENMP_USE_NTHREADS=On/Off. The variable ApproxMVBB_OPENMP_USE_NTHREADS toogles the number of threads to use. If Off, the number of threads is determined at runtime (default).

If you use clang, make sure you have the OpenMP enabled clang! GCC already supports OpenMP.


References

The main articles this code is based on:

@Article{malandain2002,
Author = {Gr'egoire Malandain and Jean-Daniel Boissonnat},
Journal = {International Journal of Computational Geometry & Applications},
Month = {December},
Number = {6},
Pages = {489 - 510},
Timestamp = {2015.09.02},
Title = {Computing the Diameter of a Point Set},
Volume = {12},
Year = {2002}}

and

@inproceedings{barequet2001,
Author = {Gill Barequet and Sariel Har-peled},
Booktitle = {In Proc. 10th ACM-SIAM Sympos. Discrete Algorithms},
Pages = {38--91},
Timestamp = {2015.09.02},
Title = {Efficiently Approximating the Minimum-Volume Bounding Box of a Point Set in Three Dimensions},
Year = {2001}}

Optimizations for future work:

@Article{chang2011,
Acmid = {2019641},
Address = {New York, NY, USA},
Articleno = {122},
Author = {Chang, Chia-Tche and Gorissen, Bastien and Melchior, Samuel},
Doi = {10.1145/2019627.2019641},
Issn = {0730-0301},
Issue_Date = {October 2011},
Journal = {ACM Trans. Graph.},
Keywords = {Computational geometry, bounding box, manifolds, optimization},
Month = oct,
Number = {5},
Numpages = {16},
Pages = {122:1--122:16},
Publisher = {ACM},
Timestamp = {2015.09.03},
Title = {Fast Oriented Bounding Box Optimization on the Rotation Group {$SO(3,\mathbb{R})$}},
Url = {http://doi.acm.org/10.1145/2019627.2019641},
Volume = {30},
Year = {2011},
Bdsk-Url-1 = {http://doi.acm.org/10.1145/2019627.2019641},
Bdsk-Url-2 = {http://dx.doi.org/10.1145/2019627.2019641}}

Licensing

This source code is released under MPL 2.0.


Author and Acknowledgements

ApproxMVBB was written by Gabriel Nützi, with source code from Grégoire Malandain & Jean-Daniel Boissonnat for the approximation of the diameter of a point cloud. I was inspired by the work and algorithms of Gill Barequet & Sariel Har-Peled for computing a minimal volume bounding box. Additionally, the geometric predicates (orient2d) used in the convex hull algorithm (graham scan) have been taken from the fine work of Jonathan Richard Shewchuk. Special thanks go to my significant other which always had an ear during breakfast for this little project 😘

Owner
Gabriel Nützi
with a degree in permanent-head-damage (phd)
Gabriel Nützi
Experiments with differentiable stacks and queues in PyTorch

Please use stacknn-core instead! StackNN This project implements differentiable stacks and queues in PyTorch. The data structures are implemented in s

Will Merrill 141 Oct 06, 2022
上海交通大学全自动抢课脚本,支持准点开抢与抢课后持续捡漏两种模式。2021/06/08更新。

Welcome to Course-Bullying-in-SJTU-v3.1! 2021/6/8 紧急更新v3.1 更新说明 为了更好地保护用户隐私,将原来用户名+密码的登录方式改为微信扫二维码+cookie登录方式,不再需要配置使用pytesseract。在使用扫码登录模式时,请稍等,二维码将马

87 Sep 13, 2022
A Pytorch Implementation of ClariNet

ClariNet A Pytorch Implementation of ClariNet (Mel Spectrogram -- Waveform) Requirements PyTorch 0.4.1 & python 3.6 & Librosa Examples Step 1. Downlo

Sungwon Kim 286 Sep 15, 2022
Official repository of my book: "Deep Learning with PyTorch Step-by-Step: A Beginner's Guide"

This is the official repository of my book "Deep Learning with PyTorch Step-by-Step". Here you will find one Jupyter notebook for every chapter in the book.

Daniel Voigt Godoy 340 Jan 01, 2023
A light-weight image labelling tool for Python designed for creating segmentation data sets.

An image labelling tool for creating segmentation data sets, for Django and Flask.

117 Nov 21, 2022
[ICCV'21] UNISURF: Unifying Neural Implicit Surfaces and Radiance Fields for Multi-View Reconstruction

UNISURF: Unifying Neural Implicit Surfaces and Radiance Fields for Multi-View Reconstruction Project Page | Paper | Supplementary | Video This reposit

331 Dec 28, 2022
JAX bindings to the Flatiron Institute Non-uniform Fast Fourier Transform (FINUFFT) library

JAX bindings to FINUFFT This package provides a JAX interface to (a subset of) the Flatiron Institute Non-uniform Fast Fourier Transform (FINUFFT) lib

Dan Foreman-Mackey 32 Oct 15, 2022
Deal or No Deal? End-to-End Learning for Negotiation Dialogues

Introduction This is a PyTorch implementation of the following research papers: (1) Hierarchical Text Generation and Planning for Strategic Dialogue (

Facebook Research 1.4k Dec 29, 2022
Storchastic is a PyTorch library for stochastic gradient estimation in Deep Learning

Storchastic is a PyTorch library for stochastic gradient estimation in Deep Learning

Emile van Krieken 140 Dec 30, 2022
Code for DeepXML: A Deep Extreme Multi-Label Learning Framework Applied to Short Text Documents

DeepXML Code for DeepXML: A Deep Extreme Multi-Label Learning Framework Applied to Short Text Documents Architectures and algorithms DeepXML supports

Extreme Classification 49 Nov 06, 2022
Learning Confidence for Out-of-Distribution Detection in Neural Networks

Learning Confidence Estimates for Neural Networks This repository contains the code for the paper Learning Confidence for Out-of-Distribution Detectio

235 Jan 05, 2023
QMagFace: Simple and Accurate Quality-Aware Face Recognition

Quality-Aware Face Recognition 26.11.2021 start readme QMagFace: Simple and Accurate Quality-Aware Face Recognition Research Paper Implementation - To

Philipp Terhörst 59 Jan 04, 2023
Implementation of Kronecker Attention in Pytorch

Kronecker Attention Pytorch Implementation of Kronecker Attention in Pytorch. Results look less than stellar, but if someone found some context where

Phil Wang 16 May 06, 2022
An end-to-end image translation model with weight-map for color constancy

CCUnet An end-to-end image translation model with weight-map for color constancy 1. Download the dataset (take Colorchecker_recommended dataset as an

Jianhui Qiu 1 Dec 21, 2021
A simple Python library for stochastic graphical ecological models

What is Viridicle? Viridicle is a library for simulating stochastic graphical ecological models. It implements the continuous time models described in

Theorem Engine 0 Dec 04, 2021
A large-scale video dataset for the training and evaluation of 3D human pose estimation models

ASPset-510 (Australian Sports Pose Dataset) is a large-scale video dataset for the training and evaluation of 3D human pose estimation models. It contains 17 different amateur subjects performing 30

Aiden Nibali 25 Jun 20, 2021
SemiNAS: Semi-Supervised Neural Architecture Search

SemiNAS: Semi-Supervised Neural Architecture Search This repository contains the code used for Semi-Supervised Neural Architecture Search, by Renqian

Renqian Luo 21 Aug 31, 2022
Official PyTorch implementation of "RMGN: A Regional Mask Guided Network for Parser-free Virtual Try-on" (IJCAI-ECAI 2022)

RMGN-VITON RMGN: A Regional Mask Guided Network for Parser-free Virtual Try-on In IJCAI-ECAI 2022(short oral). [Paper] [Supplementary Material] Abstra

27 Dec 01, 2022
Controlling a game using mediapipe hand tracking

These scripts use the Google mediapipe hand tracking solution in combination with a webcam in order to send game instructions to a racing game. It features 2 methods of control

3 May 17, 2022
A curated list of the latest breakthroughs in AI (in 2021) by release date with a clear video explanation, link to a more in-depth article, and code.

2021: A Year Full of Amazing AI papers- A Review 📌 A curated list of the latest breakthroughs in AI by release date with a clear video explanation, l

Louis-François Bouchard 2.9k Dec 31, 2022