| 1 |
/* |
| 2 |
Copyright (c) 2019, Michael Kazhdan |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#ifndef RASTERIZER_INCLUDED |
| 30 |
#define RASTERIZER_INCLUDED |
| 31 |
|
| 32 |
#include <mutex> |
| 33 |
#include "Array.h" |
| 34 |
#include "Geometry.h" |
| 35 |
#include "RegularGrid.h" |
| 36 |
#include "MyMiscellany.h" |
| 37 |
|
| 38 |
namespace PoissonRecon |
| 39 |
{ |
| 40 |
|
| 41 |
template< typename Real , unsigned int Dim > |
| 42 |
struct Rasterizer |
| 43 |
{ |
| 44 |
struct ThreadSafety |
| 45 |
{ |
| 46 |
enum Type |
| 47 |
{ |
| 48 |
MUTEXES , |
| 49 |
MAP_REDUCE , |
| 50 |
SINGLE_THREADED |
| 51 |
}; |
| 52 |
Type type; |
| 53 |
unsigned int lockDepth; |
| 54 |
ThreadSafety( Type t=MUTEXES , unsigned int ld=0 ) : type(t) , lockDepth(ld) { } |
| 55 |
}; |
| 56 |
template< typename IndexType , unsigned int K > using SimplexRasterizationGrid = RegularGrid< std::vector< std::pair< IndexType , Simplex< Real , Dim , K > > > , Dim >; |
| 57 |
|
| 58 |
// This templated function rasterizes simplices. |
| 59 |
// It is assumed that the simplices are scaled to be contained in the cube [0,1]^3. |
| 60 |
// Template parameters: |
| 61 |
// IndexType: specifies the storage for vertex/simplex indices |
| 62 |
// Input: |
| 63 |
// vertices: the vertices of the mesh |
| 64 |
// simplices: the connectivity of the mesh |
| 65 |
// depth: the depth of the voxel grid, generating a grid of size (2^depth) x (2^depth) x (2^depth) |
| 66 |
// lockDepth: the depth of the voxel grid storing the locks |
| 67 |
// Output: |
| 68 |
// A RegularGrid object where each cell stores the list of pairs containing the index into the original simplex list and the (clipped) simplex |
| 69 |
template< typename IndexType , unsigned int K > |
| 70 |
static SimplexRasterizationGrid< IndexType , K > Rasterize( const SimplicialComplex< Real , Dim , K > &simplicialComplex , unsigned int depth , ThreadSafety threadSafety ); |
| 71 |
|
| 72 |
protected: |
| 73 |
struct _RegularGridIndex |
| 74 |
{ |
| 75 |
unsigned int depth , index[Dim]; |
| 76 |
_RegularGridIndex( void ); |
| 77 |
_RegularGridIndex( unsigned int d , Point< Real , Dim > p ); |
| 78 |
template< unsigned int K > _RegularGridIndex( unsigned int maxDepth , Simplex< Real , Dim , K > simplex ); |
| 79 |
|
| 80 |
bool operator != ( const _RegularGridIndex &idx ) const; |
| 81 |
bool operator == ( const _RegularGridIndex &idx ) const { return !( (*this)!=idx ); } |
| 82 |
|
| 83 |
_RegularGridIndex child( unsigned int c ) const; |
| 84 |
}; |
| 85 |
|
| 86 |
struct _RegularGridMutexes |
| 87 |
{ |
| 88 |
_RegularGridMutexes( unsigned int lockDepth , unsigned int maxDepth ) |
| 89 |
{ |
| 90 |
if( lockDepth>maxDepth ) |
| 91 |
{ |
| 92 |
MK_WARN( "Lock depth exceeds max depth: " , lockDepth , " > " , maxDepth ); |
| 93 |
lockDepth = maxDepth; |
| 94 |
} |
| 95 |
_bitShift = maxDepth - lockDepth; |
| 96 |
unsigned int _res = 1<<lockDepth; |
| 97 |
unsigned int res[Dim]; |
| 98 |
for( int d=0 ; d<Dim ; d++ ) res[d] = _res; |
| 99 |
_mutexes.resize( res ); |
| 100 |
} |
| 101 |
|
| 102 |
std::mutex &operator() ( const unsigned int idx[Dim] ) |
| 103 |
{ |
| 104 |
unsigned int _idx[Dim]; |
| 105 |
for( int d=0 ; d<Dim ; d++ ) _idx[d] = idx[d] >> _bitShift; |
| 106 |
return _metexes( _idx ); |
| 107 |
} |
| 108 |
std::mutex &operator() ( unsigned int idx[Dim] ) |
| 109 |
{ |
| 110 |
unsigned int _idx[Dim]; |
| 111 |
for( int d=0 ; d<Dim ; d++ ) _idx[d] = idx[d] >> _bitShift; |
| 112 |
return _mutexes( _idx ); |
| 113 |
} |
| 114 |
template< typename ... UnsignedInts > |
| 115 |
std::mutex &operator()( UnsignedInts ... idx ) |
| 116 |
{ |
| 117 |
unsigned int _idx[] = { idx ... }; |
| 118 |
for( int d=0 ; d<Dim ; d++ ) _idx[d] = _idx[d] >> _bitShift; |
| 119 |
return _mutexes( _idx ); |
| 120 |
} |
| 121 |
|
| 122 |
protected: |
| 123 |
RegularGrid< std::mutex , Dim > _mutexes; |
| 124 |
size_t _bitShift; |
| 125 |
}; |
| 126 |
|
| 127 |
template< typename IndexType , unsigned int K > |
| 128 |
static size_t _Rasterize( _RegularGridMutexes &mutexes , SimplexRasterizationGrid< IndexType , K > &raster , IndexType simplexIndex , Simplex< Real , Dim , K > simplex , unsigned int depth , _RegularGridIndex idx ); |
| 129 |
|
| 130 |
template< typename IndexType , unsigned int K > |
| 131 |
static size_t _Rasterize( SimplexRasterizationGrid< IndexType , K > &raster , IndexType simplexIndex , Simplex< Real , Dim , K > simplex , unsigned int depth , _RegularGridIndex idx ); |
| 132 |
}; |
| 133 |
#include "Rasterizer.inl" |
| 134 |
} |
| 135 |
#endif // RASTERIZER_INCLUDED |