| 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 |
/////////////////////////////////// |
| 30 |
// Rasterizer::_RegularGridIndex // |
| 31 |
/////////////////////////////////// |
| 32 |
template< typename Real , unsigned int Dim > |
| 33 |
Rasterizer< Real , Dim >::_RegularGridIndex::_RegularGridIndex( void ) |
| 34 |
{ |
| 35 |
depth = 0; |
| 36 |
for( int d=0 ; d<Dim ; d++ ) index[d] = 0; |
| 37 |
} |
| 38 |
|
| 39 |
template< typename Real , unsigned int Dim > |
| 40 |
Rasterizer< Real , Dim >::_RegularGridIndex::_RegularGridIndex( unsigned int depth , Point< Real , Dim > point ) |
| 41 |
{ |
| 42 |
this->depth = depth; |
| 43 |
// After clipping the coordinate could be equal to exactly one, in which case we want to round down. |
| 44 |
for( unsigned int d=0 ; d<Dim ; d++ ) index[d] = std::min< unsigned int >( (unsigned int)( point[d] * (1<<depth) ) , (1<<depth)-1 ); |
| 45 |
} |
| 46 |
|
| 47 |
template< typename Real , unsigned int Dim > |
| 48 |
template< unsigned int K > |
| 49 |
Rasterizer< Real , Dim >::_RegularGridIndex::_RegularGridIndex( unsigned int maxDepth , Simplex< Real , Dim , K > simplex ) |
| 50 |
{ |
| 51 |
for( depth=0 ; depth<maxDepth ; depth++ ) |
| 52 |
{ |
| 53 |
_RegularGridIndex idx( depth , simplex[0] ); |
| 54 |
|
| 55 |
bool done = false; |
| 56 |
for( int k=1 ; k<=K && !done ; k++ ) if( _RegularGridIndex( depth , simplex[k] )!=idx ) done = true; |
| 57 |
if( done ) break; |
| 58 |
} |
| 59 |
if( depth==0 ) MK_THROW( "Simplex is not in unit cube: " , simplex ); |
| 60 |
else *this = _RegularGridIndex( depth-1 , simplex[0] ); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
template< typename Real , unsigned int Dim > |
| 65 |
bool Rasterizer< Real , Dim >::_RegularGridIndex::operator != ( const _RegularGridIndex &idx ) const |
| 66 |
{ |
| 67 |
if( depth!=idx.depth ) return false; |
| 68 |
for( int d=0 ; d<Dim ; d++ ) if( index[d]!=idx.index[d] ) return true; |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
template< typename Real , unsigned int Dim > |
| 73 |
typename Rasterizer< Real , Dim >::_RegularGridIndex Rasterizer< Real , Dim >::_RegularGridIndex::child( unsigned int c ) const |
| 74 |
{ |
| 75 |
_RegularGridIndex idx; |
| 76 |
idx.depth = depth+1; |
| 77 |
for( int d=0 ; d<Dim ; d++ ) idx.index[d] = index[d]*2 + ( c&(1<<d) ? 1 : 0 ); |
| 78 |
return idx; |
| 79 |
} |
| 80 |
|
| 81 |
//////////////// |
| 82 |
// Rasterizer // |
| 83 |
//////////////// |
| 84 |
|
| 85 |
template< typename Real , unsigned int Dim > |
| 86 |
template< typename IndexType , unsigned int K > |
| 87 |
size_t Rasterizer< Real , Dim >::_Rasterize( _RegularGridMutexes &mutexes , SimplexRasterizationGrid< IndexType , K > &raster , IndexType simplexIndex , Simplex< Real , Dim , K > simplex , unsigned int maxDepth , _RegularGridIndex idx ) |
| 88 |
{ |
| 89 |
if( idx.depth==maxDepth ) |
| 90 |
{ |
| 91 |
// If the simplex has non-zero size, add it to the list |
| 92 |
Real weight = simplex.measure(); |
| 93 |
if( weight && weight==weight ) |
| 94 |
{ |
| 95 |
std::lock_guard< std::mutex > lock( mutexes( idx.index ) ); |
| 96 |
raster( idx.index ).push_back( std::pair< IndexType , Simplex< Real , Dim , K > >( simplexIndex , simplex ) ); |
| 97 |
} |
| 98 |
return 1; |
| 99 |
} |
| 100 |
else |
| 101 |
{ |
| 102 |
size_t sCount = 0; |
| 103 |
|
| 104 |
// Split up the simplex and pass the parts on to the children |
| 105 |
Point< Real , Dim > center; |
| 106 |
for( unsigned int d=0 ; d<Dim ; d++ ) center[d] = (Real)( idx.index[d] + 0.5 ) / (1<<idx.depth); |
| 107 |
|
| 108 |
std::vector< std::vector< Simplex< Real , Dim , K > > > childSimplices( 1 ); |
| 109 |
childSimplices[0].push_back( simplex ); |
| 110 |
for( int d=0 ; d<Dim ; d++ ) |
| 111 |
{ |
| 112 |
Point< Real , Dim > n ; n[Dim-d-1] = 1; |
| 113 |
std::vector< std::vector< Simplex< Real , Dim , K > > > temp( (int)( 1<<(d+1) ) ); |
| 114 |
for( int c=0 ; c<(1<<d) ; c++ ) for( int i=0 ; i<childSimplices[c].size() ; i++ ) childSimplices[c][i].split( n , center[Dim-d-1] , temp[2*c] , temp[2*c+1] ); |
| 115 |
childSimplices = temp; |
| 116 |
} |
| 117 |
for( int c=0 ; c<(1<<Dim) ; c++ ) for( int i=0 ; i<childSimplices[c].size() ; i++ ) sCount += _Rasterize( mutexes , raster , simplexIndex , childSimplices[c][i] , maxDepth , idx.child(c) ); |
| 118 |
return sCount; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
template< typename Real , unsigned int Dim > |
| 123 |
template< typename IndexType , unsigned int K > |
| 124 |
size_t Rasterizer< Real , Dim >::_Rasterize( SimplexRasterizationGrid< IndexType , K > &raster , IndexType simplexIndex , Simplex< Real , Dim , K > simplex , unsigned int maxDepth , _RegularGridIndex idx ) |
| 125 |
{ |
| 126 |
if( idx.depth==maxDepth ) |
| 127 |
{ |
| 128 |
// If the simplex has non-zero size, add it to the list |
| 129 |
Real weight = simplex.measure(); |
| 130 |
if( weight && weight==weight ) raster( idx.index ).push_back( std::pair< IndexType , Simplex< Real , Dim , K > >( simplexIndex , simplex ) ); |
| 131 |
return 1; |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
size_t sCount = 0; |
| 136 |
|
| 137 |
// Split up the simplex and pass the parts on to the children |
| 138 |
Point< Real , Dim > center; |
| 139 |
for( unsigned int d=0 ; d<Dim ; d++ ) center[d] = (Real)( idx.index[d] + 0.5 ) / (1<<idx.depth); |
| 140 |
|
| 141 |
std::vector< std::vector< Simplex< Real , Dim , K > > > childSimplices( 1 ); |
| 142 |
childSimplices[0].push_back( simplex ); |
| 143 |
for( int d=0 ; d<Dim ; d++ ) |
| 144 |
{ |
| 145 |
Point< Real , Dim > n ; n[Dim-d-1] = 1; |
| 146 |
std::vector< std::vector< Simplex< Real , Dim , K > > > temp( (int)( 1<<(d+1) ) ); |
| 147 |
for( int c=0 ; c<(1<<d) ; c++ ) for( int i=0 ; i<childSimplices[c].size() ; i++ ) childSimplices[c][i].split( n , center[Dim-d-1] , temp[2*c] , temp[2*c+1] ); |
| 148 |
childSimplices = temp; |
| 149 |
} |
| 150 |
for( int c=0 ; c<(1<<Dim) ; c++ ) for( int i=0 ; i<childSimplices[c].size() ; i++ ) sCount += _Rasterize( raster , simplexIndex , childSimplices[c][i] , maxDepth , idx.child(c) ); |
| 151 |
return sCount; |
| 152 |
} |
| 153 |
} |
| 154 |
template< typename Real , unsigned int Dim > |
| 155 |
template< typename IndexType , unsigned int K > |
| 156 |
typename Rasterizer< Real , Dim >::template SimplexRasterizationGrid< IndexType , K > Rasterizer< Real , Dim >::Rasterize( const SimplicialComplex< Real , Dim , K > &simplicialComplex , unsigned int depth , ThreadSafety threadSafety ) |
| 157 |
{ |
| 158 |
unsigned int res = 1<<depth; |
| 159 |
|
| 160 |
SimplexRasterizationGrid< IndexType , K > raster; |
| 161 |
{ |
| 162 |
unsigned int _res[Dim]; |
| 163 |
for( int d=0 ; d<Dim ; d++ ) _res[d] = res; |
| 164 |
raster.resize( _res ); |
| 165 |
} |
| 166 |
|
| 167 |
if( threadSafety.type==ThreadSafety::MUTEXES ) |
| 168 |
{ |
| 169 |
if( threadSafety.lockDepth>depth ) MK_THROW( "Lock depth cannot excceed depth: " , threadSafety.lockDepth , " <= " , depth ); |
| 170 |
_RegularGridMutexes mutexes( threadSafety.lockDepth , depth ); |
| 171 |
|
| 172 |
ThreadPool::ParallelFor( 0 , simplicialComplex.size() , [&]( unsigned int t , size_t i ) |
| 173 |
{ |
| 174 |
std::vector< Simplex< Real , Dim , K > > subSimplices; |
| 175 |
subSimplices.push_back( simplicialComplex[i] ); |
| 176 |
|
| 177 |
// Clip the simplex to the unit cube |
| 178 |
{ |
| 179 |
for( int d=0 ; d<Dim ; d++ ) |
| 180 |
{ |
| 181 |
Point< Real , Dim > n; |
| 182 |
n[d] = 1; |
| 183 |
{ |
| 184 |
std::vector< Simplex< Real , Dim , K > > back , front; |
| 185 |
for( int i=0 ; i<subSimplices.size() ; i++ ) subSimplices[i].split( n , 0 , back , front ); |
| 186 |
subSimplices = front; |
| 187 |
} |
| 188 |
{ |
| 189 |
std::vector< Simplex< Real , Dim , K > > back , front; |
| 190 |
for( int i=0 ; i<subSimplices.size() ; i++ ) subSimplices[i].split( n , 1 , back , front ); |
| 191 |
subSimplices = back; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
for( int j=0 ; j<subSimplices.size() ; j++ ) _Rasterize< IndexType , K >( mutexes , raster , (IndexType)i , subSimplices[j] , depth , _RegularGridIndex( depth , subSimplices[j] ) ); |
| 196 |
} ); |
| 197 |
} |
| 198 |
else if( threadSafety.type==ThreadSafety::SINGLE_THREADED ) |
| 199 |
{ |
| 200 |
for( size_t i=0 ; i<simplicialComplex.size() ; i++ ) |
| 201 |
{ |
| 202 |
std::vector< Simplex< Real , Dim , K > > subSimplices; |
| 203 |
subSimplices.push_back( simplicialComplex[i] ); |
| 204 |
|
| 205 |
// Clip the simplex to the unit cube |
| 206 |
{ |
| 207 |
for( int d=0 ; d<Dim ; d++ ) |
| 208 |
{ |
| 209 |
Point< Real , Dim > n; |
| 210 |
n[d] = 1; |
| 211 |
{ |
| 212 |
std::vector< Simplex< Real , Dim , K > > back , front; |
| 213 |
for( int i=0 ; i<subSimplices.size() ; i++ ) subSimplices[i].split( n , 0 , back , front ); |
| 214 |
subSimplices = front; |
| 215 |
} |
| 216 |
{ |
| 217 |
std::vector< Simplex< Real , Dim , K > > back , front; |
| 218 |
for( int i=0 ; i<subSimplices.size() ; i++ ) subSimplices[i].split( n , 1 , back , front ); |
| 219 |
subSimplices = back; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
for( int j=0 ; j<subSimplices.size() ; j++ ) _Rasterize< IndexType , K >( raster , (IndexType)i , subSimplices[j] , depth , _RegularGridIndex( depth , subSimplices[j] ) ); |
| 224 |
} |
| 225 |
} |
| 226 |
else if( threadSafety.type==ThreadSafety::MAP_REDUCE ) |
| 227 |
{ |
| 228 |
std::vector< SimplexRasterizationGrid< IndexType , K > > rasters( ThreadPool::NumThreads() ); |
| 229 |
for( int t=0 ; t<rasters.size() ; t++ ) |
| 230 |
{ |
| 231 |
unsigned int _res[Dim]; |
| 232 |
for( int d=0 ; d<Dim ; d++ ) _res[d] = res; |
| 233 |
rasters[t].resize( _res ); |
| 234 |
} |
| 235 |
|
| 236 |
// Map |
| 237 |
ThreadPool::ParallelFor( 0 , simplicialComplex.size() , [&]( unsigned int t , size_t i ) |
| 238 |
{ |
| 239 |
std::vector< Simplex< Real , Dim , K > > subSimplices; |
| 240 |
subSimplices.push_back( simplicialComplex[i] ); |
| 241 |
|
| 242 |
// Clip the simplex to the unit cube |
| 243 |
{ |
| 244 |
for( int d=0 ; d<Dim ; d++ ) |
| 245 |
{ |
| 246 |
Point< Real , Dim > n; |
| 247 |
n[d] = 1; |
| 248 |
{ |
| 249 |
std::vector< Simplex< Real , Dim , K > > back , front; |
| 250 |
for( int i=0 ; i<subSimplices.size() ; i++ ) subSimplices[i].split( n , 0 , back , front ); |
| 251 |
subSimplices = front; |
| 252 |
} |
| 253 |
{ |
| 254 |
std::vector< Simplex< Real , Dim , K > > back , front; |
| 255 |
for( int i=0 ; i<subSimplices.size() ; i++ ) subSimplices[i].split( n , 1 , back , front ); |
| 256 |
subSimplices = back; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
for( int j=0 ; j<subSimplices.size() ; j++ ) _Rasterize< IndexType , K >( rasters[t] , (IndexType)i , subSimplices[j] , depth , _RegularGridIndex( depth , subSimplices[j] ) ); |
| 261 |
} ); |
| 262 |
|
| 263 |
// Reduce |
| 264 |
ThreadPool::ParallelFor( 0 , raster.resolution() , [&]( unsigned int , size_t i ) |
| 265 |
{ |
| 266 |
size_t count = 0; |
| 267 |
for( int t=0 ; t<rasters.size() ; t++ ) count += rasters[t][i].size(); |
| 268 |
raster[i].reserve( count ); |
| 269 |
for( int t=0 ; t<rasters.size() ; t++ ) for( int j=0 ; j<rasters[t][i].size() ; j++ ) raster[i].push_back( rasters[t][i][j] ); |
| 270 |
} ); |
| 271 |
} |
| 272 |
else MK_THROW( "Unrecognized thread safety type: " , threadSafety.type ); |
| 273 |
return raster; |
| 274 |
} |