| 1 |
/* |
| 2 |
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho |
| 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 |
template< class Real , unsigned int DataDegree , unsigned int ... DataDegrees > typename std::enable_if< sizeof ... ( DataDegrees )==0 >::type __SetBSplineComponentValues( const Real* position , const Real* start , Real width , double* values , unsigned int stride ) |
| 30 |
{ |
| 31 |
Polynomial< DataDegree >::BSplineComponentValues( ( position[0] - start[0] ) / width , values ); |
| 32 |
} |
| 33 |
template< class Real , unsigned int DataDegree , unsigned int ... DataDegrees > typename std::enable_if< sizeof ... ( DataDegrees )!=0 >::type __SetBSplineComponentValues( const Real* position , const Real* start , Real width , double* values , unsigned int stride ) |
| 34 |
{ |
| 35 |
Polynomial< DataDegree >::BSplineComponentValues( ( position[0] - start[0] ) / width , values ); |
| 36 |
__SetBSplineComponentValues< Real , DataDegrees ... >( position+1 , start+1 , width , values + stride , stride ); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
// evaluate the result of splatting along a plane and then evaluating at a point on the plane. |
| 41 |
template< unsigned int Dim , class Real > |
| 42 |
template< unsigned int CoDim , unsigned int Degree > |
| 43 |
Real FEMTree< Dim , Real >::_GetScaleValue( Point< Real , Dim > p ) const |
| 44 |
{ |
| 45 |
static_assert( ( Dim>=CoDim) , "[ERROR] Co-dimension exceeds dimension" ); |
| 46 |
static const int PointSupportStart = -BSplineSupportSizes< Degree >::SupportEnd , PointSupportEnd = -BSplineSupportSizes< Degree >::SupportStart; |
| 47 |
static const int PointSupportSize = PointSupportEnd - PointSupportStart + 1; |
| 48 |
static const int BSplineSupportStart = BSplineSupportSizes< Degree >::SupportStart , BSplineSupportEnd = BSplineSupportSizes< Degree >::SupportEnd; |
| 49 |
static const int BSplineSupportSize = BSplineSupportEnd - BSplineSupportStart + 1; |
| 50 |
double splineValues[Dim][Degree+1]; |
| 51 |
|
| 52 |
// Evaluate the B-spline component functions at the position |
| 53 |
for( int d=0 ; d<Dim ; d++ ) Polynomial< Degree >::BSplineComponentValues( p[d] , splineValues[d] ); |
| 54 |
|
| 55 |
StaticWindow< double , IsotropicUIntPack< Dim , PointSupportSize > > splatValues , densityValues; |
| 56 |
|
| 57 |
// Get the values with which the center point splats into its neighbors |
| 58 |
{ |
| 59 |
double scratch[Dim+1]; |
| 60 |
scratch[0] = 1.; |
| 61 |
int idx[Dim]; |
| 62 |
WindowLoop< Dim >::Run |
| 63 |
( |
| 64 |
PointSupportStart , PointSupportEnd+1 , |
| 65 |
[&]( int d , int i ){ scratch[d+1] = scratch[d] * splineValues[d][i-PointSupportStart] , idx[d] = i; } , |
| 66 |
[&]( void ) |
| 67 |
{ |
| 68 |
int _idx[Dim]; |
| 69 |
for( int d=0 ; d<Dim ; d++ ) _idx[d] = idx[d] - PointSupportStart; |
| 70 |
splatValues( _idx ) = scratch[ Dim ] , densityValues( _idx ) = 0; |
| 71 |
} |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
// Splat from points along the hyperplane |
| 76 |
// A point at node i will contribute to the evaluation of a point at node 0 if: |
| 77 |
// 0 <= i + PointSupportEnd + BSplineSupportEnd |
| 78 |
// and |
| 79 |
// 0 >= i + PointSupportStart + BSplineSupportStart |
| 80 |
// Or, equivalently: |
| 81 |
// - PointSupportEnd - BSplineSupportEnd <= i <= - PointSupportStart - BSplineSupportStart |
| 82 |
{ |
| 83 |
int neighborPointIndex[Dim]; |
| 84 |
// Iterate over all points that can contribute |
| 85 |
WindowLoop< Dim >::Run |
| 86 |
( |
| 87 |
- PointSupportEnd - BSplineSupportEnd , - PointSupportStart - BSplineSupportStart + 1 , |
| 88 |
[&]( int d , int i ){ neighborPointIndex[d] = i; } , |
| 89 |
[&]( void ) |
| 90 |
{ |
| 91 |
// Check that the neighboring point's node lies on the hyperplane |
| 92 |
bool validNeighbor = true; |
| 93 |
for( int d=0 ; d<CoDim ; d++ ) if( neighborPointIndex[d]!=0 ) validNeighbor = false; |
| 94 |
|
| 95 |
if( validNeighbor ) |
| 96 |
{ |
| 97 |
int splineIndex[Dim] , _splineIndex[Dim]; |
| 98 |
// Iterate over all B-Splines supported on the neighboring point |
| 99 |
WindowLoop< Dim >::Run |
| 100 |
( |
| 101 |
PointSupportStart , PointSupportEnd+1 , |
| 102 |
[&]( int d , int i ){ splineIndex[d] = neighborPointIndex[d] + i , _splineIndex[d] = i; } , |
| 103 |
[&]( void ) |
| 104 |
{ |
| 105 |
int idx[Dim] , _idx[Dim]; |
| 106 |
bool inRange = true; |
| 107 |
for( int d=0 ; d<Dim ; d++ ) |
| 108 |
{ |
| 109 |
idx[d] = splineIndex[d] - PointSupportStart; |
| 110 |
_idx[d] = _splineIndex[d] - PointSupportStart; |
| 111 |
if( idx[d]<0 || idx[d]>=PointSupportSize ) inRange = false; |
| 112 |
} |
| 113 |
if( inRange ) densityValues( idx ) += splatValues( _idx ); |
| 114 |
} |
| 115 |
); |
| 116 |
} |
| 117 |
} |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
double scaleValue = 0; |
| 122 |
{ |
| 123 |
int idx[Dim]; |
| 124 |
WindowLoop< Dim >::Run |
| 125 |
( |
| 126 |
PointSupportStart , PointSupportEnd+1 , |
| 127 |
[&]( int d , int i ){ idx[d] = i - PointSupportStart; } , |
| 128 |
[&]( void ){ scaleValue += splatValues(idx) * densityValues(idx); } |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
return (Real)( 1./scaleValue ); |
| 133 |
} |
| 134 |
|
| 135 |
// Evaluate the result of splatting along a hyper-plane of co-dimension CoDim through points in the interior of the node and then evaluating at those points. |
| 136 |
template< unsigned int Dim , class Real > |
| 137 |
template< unsigned int CoDim , unsigned int Degree > Real FEMTree< Dim , Real >::_GetScaleValue( unsigned int res ) const |
| 138 |
{ |
| 139 |
Point< Real , Dim > p; |
| 140 |
Real dx = (Real)(1./res); |
| 141 |
unsigned int count = 0; |
| 142 |
Real scaleValueSum = 0; |
| 143 |
|
| 144 |
WindowLoop< Dim >::Run |
| 145 |
( |
| 146 |
0 , res , |
| 147 |
[&]( int d , int i ){ p[d] = dx/2 + dx*i; } , |
| 148 |
[&]( void ){ count++ ; scaleValueSum += _GetScaleValue< CoDim , Degree >(p); } |
| 149 |
); |
| 150 |
return scaleValueSum / count; |
| 151 |
} |
| 152 |
|
| 153 |
template< unsigned int Dim , class Real > |
| 154 |
template< bool ThreadSafe , unsigned int CoDim , unsigned int WeightDegree > |
| 155 |
void FEMTree< Dim , Real >::_addWeightContribution( Allocator< FEMTreeNode > *nodeAllocator , DensityEstimator< WeightDegree >& densityWeights , FEMTreeNode* node , Point< Real , Dim > position , PointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey , Real weight ) |
| 156 |
{ |
| 157 |
static const Real ScaleValue = _GetScaleValue< CoDim , WeightDegree >( 10 ); |
| 158 |
double values[ Dim ][ BSplineSupportSizes< WeightDegree >::SupportSize ]; |
| 159 |
typename FEMTreeNode::template Neighbors< IsotropicUIntPack< Dim , BSplineSupportSizes< WeightDegree >::SupportSize > >& neighbors = weightKey.template getNeighbors< true , ThreadSafe >( node , nodeAllocator , _nodeInitializer ); |
| 160 |
|
| 161 |
densityWeights.reserve( nodeCount() ); |
| 162 |
|
| 163 |
// Evaluate the B-spline components at the position |
| 164 |
{ |
| 165 |
Point< Real , Dim > start; |
| 166 |
Real w; |
| 167 |
node->startAndWidth( start , w ); |
| 168 |
for( int dim=0 ; dim<Dim ; dim++ ) Polynomial< WeightDegree >::BSplineComponentValues( ( position[dim]-start[dim] ) / w , values[dim] ); |
| 169 |
} |
| 170 |
|
| 171 |
weight *= (Real)ScaleValue; |
| 172 |
double scratch[Dim+1]; |
| 173 |
scratch[0] = weight; |
| 174 |
WindowLoop< Dim >::Run |
| 175 |
( |
| 176 |
IsotropicUIntPack< Dim , 0 >() , IsotropicUIntPack< Dim , BSplineSupportSizes< WeightDegree >::SupportSize >() , |
| 177 |
[&]( int d , int i ){ scratch[d+1] = scratch[d] * values[d][i]; } , |
| 178 |
[&]( FEMTreeNode* node ) |
| 179 |
{ |
| 180 |
if( node ) |
| 181 |
{ |
| 182 |
AddAtomic( densityWeights[ node ] , (Real)scratch[Dim] ); |
| 183 |
} |
| 184 |
} , |
| 185 |
neighbors.neighbors() |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
template< unsigned int Dim , class Real > |
| 190 |
template< unsigned int WeightDegree , class PointSupportKey > |
| 191 |
Real FEMTree< Dim , Real >::_getSamplesPerNode( const DensityEstimator< WeightDegree >& densityWeights , const FEMTreeNode* node , Point< Real , Dim > position , PointSupportKey& weightKey ) const |
| 192 |
{ |
| 193 |
Real weight = 0; |
| 194 |
typedef typename PointSupportKey::NeighborType Neighbors; |
| 195 |
double values[ Dim ][ BSplineSupportSizes< WeightDegree >::SupportSize ]; |
| 196 |
Neighbors neighbors = weightKey.getNeighbors( node ); |
| 197 |
Point< Real , Dim > start; |
| 198 |
Real w; |
| 199 |
_startAndWidth( node , start , w ); |
| 200 |
|
| 201 |
for( int dim=0 ; dim<Dim ; dim++ ) Polynomial< WeightDegree >::BSplineComponentValues( ( position[dim]-start[dim] ) / w , values[dim] ); |
| 202 |
double scratch[Dim+1]; |
| 203 |
scratch[0] = 1; |
| 204 |
WindowLoop< Dim >::Run |
| 205 |
( |
| 206 |
IsotropicUIntPack< Dim , 0 >() , IsotropicUIntPack< Dim , BSplineSupportSizes< WeightDegree >::SupportSize >() , |
| 207 |
[&]( int d , int i ){ scratch[d+1] = scratch[d] * values[d][i]; } , |
| 208 |
[&]( typename Neighbors::StaticWindow::data_type node ){ if( node ){ const Real *w = densityWeights( node ) ; if( w ) weight += (Real)( scratch[Dim] * (*w) ); } } , |
| 209 |
neighbors.neighbors() |
| 210 |
); |
| 211 |
return weight; |
| 212 |
} |
| 213 |
template< unsigned int Dim , class Real > |
| 214 |
template< unsigned int WeightDegree , class PointSupportKey > |
| 215 |
void FEMTree< Dim , Real >::_getSampleDepthAndWeight( const DensityEstimator< WeightDegree >& densityWeights , const FEMTreeNode* node , Point< Real , Dim > position , PointSupportKey& weightKey , Real& depth , Real& weight ) const |
| 216 |
{ |
| 217 |
const FEMTreeNode* temp = node; |
| 218 |
while( _localDepth( temp )>densityWeights.kernelDepth() ) temp = temp->parent; |
| 219 |
// Goal: |
| 220 |
// Find the depth d at which the number of samples per node is equal to densityWeights.samplesPerNode. |
| 221 |
// Assume that the number of samples per node grows by a factor of 2^( Dim-CoDim ) as the depth is decreased by 1. |
| 222 |
// That is: |
| 223 |
// SamplesPerNode( d ) = C / 2^( d * ( Dim - CoDim ) ) |
| 224 |
// So, given a target spd, we have: |
| 225 |
// spd = C / 2^( d * ( Dim - CoDim ) ) |
| 226 |
// log( spd ) = log( C ) / log( 2^( d * ( Dim - CoDim ) ) ) |
| 227 |
// log( spd ) = log( C ) - log( 2 ) * ( d * ( Dim - CoDim ) ) ) |
| 228 |
// d = [ log( C ) - log( spd ) ] / [ log(2) * ( Dim-CoDim ) ] |
| 229 |
// To get C, we note that if we know that we have spd_0 at depth d_0, this gives: |
| 230 |
// spd_0 = C / 2^( d_0 * ( Dim - CoDim ) ) |
| 231 |
// C = spd_0 * 2^( d_0 * ( Dim - CoDim ) ) |
| 232 |
// Putting these together, we get: |
| 233 |
// d = [ log( spd_0 * 2^( d_0 * ( Dim - CoDim ) ) ) - log( spd ) ] / [ log(2) * ( Dim-CoDim ) ] |
| 234 |
// d = [ log( spd_0 ) - log( spd ) + log(2) * ( d_0 * ( Dim - CoDim ) ) ) ] / [ log(2) * ( Dim-CoDim ) ] |
| 235 |
// d = [ log( spd_0 / spd ) ] / [ log(2) * ( Dim-CoDim ) ] + d_0 |
| 236 |
|
| 237 |
Real samplesPerNode = _getSamplesPerNode( densityWeights , temp , position , weightKey ); |
| 238 |
if( samplesPerNode>=densityWeights.samplesPerNode() ) depth = Real( _localDepth( temp ) + log( samplesPerNode / densityWeights.samplesPerNode() ) / ( log(2.) * ( Dim-densityWeights.coDimension() ) ) ); |
| 239 |
else |
| 240 |
{ |
| 241 |
Real fineSamplesPerNode , coarseSamplesPerNode; |
| 242 |
fineSamplesPerNode = coarseSamplesPerNode = samplesPerNode; |
| 243 |
while( coarseSamplesPerNode<densityWeights.samplesPerNode() && _localDepth(temp) ) |
| 244 |
{ |
| 245 |
temp = temp->parent; |
| 246 |
fineSamplesPerNode = coarseSamplesPerNode; |
| 247 |
coarseSamplesPerNode = _getSamplesPerNode( densityWeights , temp , position , weightKey ); |
| 248 |
} |
| 249 |
// Rather than assuming that the number of samples per node scales by a factor of 2^(Dim-CoDim), |
| 250 |
// use the fact that between the coarse and fine levels the samples per node scaled by coarseSamplesPerNode / fineSamplesPerNode |
| 251 |
depth = Real( _localDepth( temp ) + log( coarseSamplesPerNode / densityWeights.samplesPerNode() ) / log( coarseSamplesPerNode / fineSamplesPerNode ) ); |
| 252 |
samplesPerNode = coarseSamplesPerNode; |
| 253 |
} |
| 254 |
Real nodeWidth = (Real)( 1. / (1<<_localDepth(temp) ) ); |
| 255 |
weight = (Real)pow( nodeWidth , Dim-densityWeights.coDimension() ) / samplesPerNode; |
| 256 |
} |
| 257 |
template< unsigned int Dim , class Real > |
| 258 |
template< unsigned int WeightDegree , class PointSupportKey > |
| 259 |
void FEMTree< Dim , Real >::_getSampleDepthAndWeight( const DensityEstimator< WeightDegree >& densityWeights , Point< Real , Dim > position , PointSupportKey& weightKey , Real& depth , Real& weight ) const |
| 260 |
{ |
| 261 |
FEMTreeNode* temp; |
| 262 |
Point< Real, Dim > myCenter; |
| 263 |
for( int d=0 ; d<Dim ; d++ ) myCenter[d] = (Real)0.5; |
| 264 |
Real myWidth = Real( 1. ); |
| 265 |
|
| 266 |
// Get the finest node with depth less than or equal to the splat depth that contains the point |
| 267 |
temp = _spaceRoot; |
| 268 |
while( _localDepth( temp )<densityWeights.kernelDepth() ) |
| 269 |
{ |
| 270 |
if( !IsActiveNode< Dim >( temp->children ) ) break; // MK_THROW( "" ); |
| 271 |
int cIndex = FEMTreeNode::ChildIndex( myCenter , position ); |
| 272 |
temp = temp->children + cIndex; |
| 273 |
myWidth /= 2; |
| 274 |
for( int d=0 ; d<Dim ; d++ ) |
| 275 |
if( (cIndex>>d) & 1 ) myCenter[d] += myWidth/2; |
| 276 |
else myCenter[d] -= myWidth/2; |
| 277 |
} |
| 278 |
return _getSampleDepthAndWeight( densityWeights , temp , position , weightKey , depth , weight ); |
| 279 |
} |
| 280 |
|
| 281 |
template< unsigned int Dim , class Real > |
| 282 |
template< bool CreateNodes , bool ThreadSafe , class V , unsigned int ... DataSigs > |
| 283 |
void FEMTree< Dim , Real >::_splatPointData( V zero , Allocator< FEMTreeNode > *nodeAllocator , FEMTreeNode* node , Point< Real , Dim > position , V v , SparseNodeData< V , UIntPack< DataSigs ... > >& dataInfo , PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey ) |
| 284 |
{ |
| 285 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > SupportSizes; |
| 286 |
double values[ Dim ][ SupportSizes::Max() ]; |
| 287 |
typename FEMTreeNode::template Neighbors< UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > >& neighbors = dataKey.template getNeighbors< CreateNodes , ThreadSafe >( node , nodeAllocator , _nodeInitializer ); |
| 288 |
Point< Real , Dim > start; |
| 289 |
Real w; |
| 290 |
_startAndWidth( node , start , w ); |
| 291 |
__SetBSplineComponentValues< Real , FEMSignature< DataSigs >::Degree ... >( &position[0] , &start[0] , w , &values[0][0] , SupportSizes::Max() ); |
| 292 |
double scratch[Dim+1]; |
| 293 |
scratch[0] = 1; |
| 294 |
WindowLoop< Dim >::Run |
| 295 |
( |
| 296 |
ZeroUIntPack< Dim >() , UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... >() , |
| 297 |
[&]( int d , int i ){ scratch[d+1] = scratch[d] * values[d][i]; } , |
| 298 |
[&]( FEMTreeNode* node ){ if( IsActiveNode< Dim >( node ) ) Atomic< V >::Add( dataInfo.at( node , zero ) , v * (Real)scratch[Dim] ); } , |
| 299 |
neighbors.neighbors() |
| 300 |
); |
| 301 |
} |
| 302 |
template< unsigned int Dim , class Real > |
| 303 |
template< bool CreateNodes , bool ThreadSafe , unsigned int WeightDegree , class V , unsigned int ... DataSigs > |
| 304 |
Point< Real , 2 > FEMTree< Dim , Real >::_splatPointData( V zero , Allocator< FEMTreeNode > *nodeAllocator , const DensityEstimator< WeightDegree >& densityWeights , Real minDepthCutoff , Point< Real , Dim > position , V v , SparseNodeData< V , UIntPack< DataSigs ... > >& dataInfo , PointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey , PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , LocalDepth minDepth , LocalDepth maxDepth , int dim , Real depthBias ) |
| 305 |
{ |
| 306 |
// Get the depth and weight at position |
| 307 |
Real weight , depth; |
| 308 |
FEMTreeNode *temp = _spaceRoot; |
| 309 |
Point< Real , Dim > myCenter; |
| 310 |
Real myWidth; |
| 311 |
{ |
| 312 |
for( int d=0 ; d<Dim ; d++ ) myCenter[d] = (Real)0.5; |
| 313 |
myWidth = (Real)1.; |
| 314 |
while( _localDepth( temp )<densityWeights.kernelDepth() ) |
| 315 |
{ |
| 316 |
if( !IsActiveNode< Dim >( temp->children ) ) break; |
| 317 |
int cIndex = FEMTreeNode::ChildIndex( myCenter , position ); |
| 318 |
temp = temp->children + cIndex; |
| 319 |
myWidth /= 2; |
| 320 |
for( int d=0 ; d<Dim ; d++ ) |
| 321 |
if( (cIndex>>d) & 1 ) myCenter[d] += myWidth/2; |
| 322 |
else myCenter[d] -= myWidth/2; |
| 323 |
} |
| 324 |
_getSampleDepthAndWeight( densityWeights , temp , position , weightKey , depth , weight ); |
| 325 |
depth += depthBias; |
| 326 |
} |
| 327 |
|
| 328 |
if( depth<minDepthCutoff ) return Point< Real , 2 >( (Real)-1. , (Real)0. ); |
| 329 |
Real rawDepth = depth; |
| 330 |
|
| 331 |
if( depth<minDepth ) depth = Real(minDepth); |
| 332 |
if( depth>maxDepth ) depth = Real(maxDepth); |
| 333 |
int topDepth = (int)ceil(depth); |
| 334 |
|
| 335 |
double dx = 1.0-(topDepth-depth); |
| 336 |
if ( topDepth<=minDepth ) topDepth = minDepth , dx = 1; |
| 337 |
else if( topDepth> maxDepth ) topDepth = maxDepth , dx = 1; |
| 338 |
|
| 339 |
while( _localDepth( temp )>topDepth ) temp=temp->parent; |
| 340 |
while( _localDepth( temp )<topDepth ) |
| 341 |
{ |
| 342 |
if( !temp->children ) temp->template initChildren< ThreadSafe >( nodeAllocator , _nodeInitializer ); |
| 343 |
int cIndex = FEMTreeNode::ChildIndex( myCenter , position ); |
| 344 |
temp = &temp->children[cIndex]; |
| 345 |
myWidth/=2; |
| 346 |
for( int d=0 ; d<Dim ; d++ ) |
| 347 |
if( (cIndex>>d) & 1 ) myCenter[d] += myWidth/2; |
| 348 |
else myCenter[d] -= myWidth/2; |
| 349 |
} |
| 350 |
|
| 351 |
auto Splat = [&]( FEMTreeNode *node , Real dx ) |
| 352 |
{ |
| 353 |
double width = 1.0 / ( 1<<_localDepth( temp ) ); |
| 354 |
// Scale by: |
| 355 |
// weight: the area/volume associated with the sample |
| 356 |
// dx: the fraction of the sample splatted into the current depth |
| 357 |
// pow( width , -dim ): So that each sample is splatted with a unit volume |
| 358 |
V _v = v * weight / Real( pow( width , dim ) ) * dx; |
| 359 |
// V _v = v / Length(v) * dx; |
| 360 |
#if defined( __GNUC__ ) && __GNUC__ < 5 |
| 361 |
#ifdef SHOW_WARNINGS |
| 362 |
#warning "you've got me gcc version<5" |
| 363 |
#endif // SHOW_WARNINGS |
| 364 |
_splatPointData< CreateNodes , ThreadSafe , V >( zero , nodeAllocator , temp , position , _v , dataInfo , dataKey ); |
| 365 |
#else // !__GNUC__ || __GNUC__ >=5 |
| 366 |
_splatPointData< CreateNodes , ThreadSafe , V , DataSigs ... >( zero , nodeAllocator , temp , position , _v , dataInfo , dataKey ); |
| 367 |
#endif // __GNUC__ || __GNUC__ < 4 |
| 368 |
}; |
| 369 |
Splat( temp , (Real)dx ); |
| 370 |
if( fabs(1.-dx)>1e-6 ) Splat( temp->parent , (Real)(1.-dx) ); |
| 371 |
return Point< Real , 2 >( rawDepth , weight ); |
| 372 |
} |
| 373 |
|
| 374 |
template< unsigned int Dim , class Real > |
| 375 |
template< bool CreateNodes , bool ThreadSafe , unsigned int WeightDegree , class V , unsigned int ... DataSigs > |
| 376 |
Point< Real , 2 > FEMTree< Dim , Real >::_splatPointData( V zero , Allocator< FEMTreeNode > *nodeAllocator , const DensityEstimator< WeightDegree >& densityWeights , Real minDepthCutoff , Point< Real , Dim > position , V v , SparseNodeData< V , UIntPack< DataSigs ... > >& dataInfo , PointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey , PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , LocalDepth minDepth , std::function< int ( Point< Real , Dim > ) > &pointDepthFunctor , int dim , Real depthBias ) |
| 377 |
{ |
| 378 |
// Get the depth and weight at position |
| 379 |
Real sampleWeight , sampleDepth; |
| 380 |
FEMTreeNode *temp = _spaceRoot; |
| 381 |
Point< Real , Dim > myCenter; |
| 382 |
Real myWidth; |
| 383 |
|
| 384 |
int maxDepth = pointDepthFunctor( position ); |
| 385 |
{ |
| 386 |
int depth = 0; |
| 387 |
for( int d=0 ; d<Dim ; d++ ) myCenter[d] = (Real)0.5; |
| 388 |
myWidth = (Real)1.; |
| 389 |
while( depth<maxDepth && depth<densityWeights.kernelDepth() ) |
| 390 |
{ |
| 391 |
if( !IsActiveNode< Dim >( temp->children ) ) break; |
| 392 |
int cIndex = FEMTreeNode::ChildIndex( myCenter , position ); |
| 393 |
temp = temp->children + cIndex; |
| 394 |
myWidth /= 2; |
| 395 |
for( int d=0 ; d<Dim ; d++ ) |
| 396 |
if( (cIndex>>d) & 1 ) myCenter[d] += myWidth/2; |
| 397 |
else myCenter[d] -= myWidth/2; |
| 398 |
depth++; |
| 399 |
} |
| 400 |
_getSampleDepthAndWeight( densityWeights , temp , position , weightKey , sampleDepth , sampleWeight ); |
| 401 |
sampleDepth += depthBias; |
| 402 |
} |
| 403 |
|
| 404 |
if( sampleDepth<minDepthCutoff ) return Point< Real , 2 >( (Real)-1. , (Real)0. ); |
| 405 |
Real rawSampleDepth = sampleDepth; |
| 406 |
|
| 407 |
if( sampleDepth<minDepth ) sampleDepth = (Real)minDepth; |
| 408 |
if( sampleDepth>maxDepth ) sampleDepth = (Real)maxDepth; |
| 409 |
int topDepth = (int)ceil(sampleDepth); |
| 410 |
|
| 411 |
double dx = 1.0-(topDepth-sampleDepth); |
| 412 |
if ( topDepth<=minDepth ) topDepth = minDepth , dx = 1; |
| 413 |
else if( topDepth> maxDepth ) topDepth = maxDepth , dx = 1; |
| 414 |
|
| 415 |
while( _localDepth( temp )>topDepth ) temp=temp->parent; |
| 416 |
while( _localDepth( temp )<topDepth ) |
| 417 |
{ |
| 418 |
if( !temp->children ) temp->template initChildren< ThreadSafe >( nodeAllocator , _nodeInitializer ); |
| 419 |
int cIndex = FEMTreeNode::ChildIndex( myCenter , position ); |
| 420 |
temp = &temp->children[cIndex]; |
| 421 |
myWidth/=2; |
| 422 |
for( int d=0 ; d<Dim ; d++ ) |
| 423 |
if( (cIndex>>d) & 1 ) myCenter[d] += myWidth/2; |
| 424 |
else myCenter[d] -= myWidth/2; |
| 425 |
} |
| 426 |
|
| 427 |
auto Splat = [&]( FEMTreeNode *node , Real dx ) |
| 428 |
{ |
| 429 |
double width = 1.0 / ( 1<<_localDepth( temp ) ); |
| 430 |
// Scale by: |
| 431 |
// weight: the area/volume associated with the sample |
| 432 |
// dx: the fraction of the sample splatted into the current depth |
| 433 |
// pow( width , -dim ): So that each sample is splatted with a unit volume |
| 434 |
V _v = v * sampleWeight / Real( pow( width , dim ) ) * dx; |
| 435 |
// V _v = v / Length(v) * dx; |
| 436 |
#if defined( __GNUC__ ) && __GNUC__ < 5 |
| 437 |
#ifdef SHOW_WARNINGS |
| 438 |
#warning "you've got me gcc version<5" |
| 439 |
#endif // SHOW_WARNINGS |
| 440 |
_splatPointData< CreateNodes , ThreadSafe , V >( zero , nodeAllocator , temp , position , _v , dataInfo , dataKey ); |
| 441 |
#else // !__GNUC__ || __GNUC__ >=5 |
| 442 |
_splatPointData< CreateNodes , ThreadSafe , V , DataSigs ... >( zero , nodeAllocator , temp , position , _v , dataInfo , dataKey ); |
| 443 |
#endif // __GNUC__ || __GNUC__ < 4 |
| 444 |
}; |
| 445 |
Splat( temp , (Real)dx ); |
| 446 |
if( fabs(1.-dx)>1e-6 ) Splat( temp->parent , (Real)(1.-dx) ); |
| 447 |
return Point< Real , 2 >( rawSampleDepth , sampleWeight ); |
| 448 |
} |
| 449 |
|
| 450 |
template< unsigned int Dim , class Real > |
| 451 |
template< bool CreateNodes , bool ThreadSafe , unsigned int WeightDegree , class V , unsigned int ... DataSigs > |
| 452 |
Point< Real , 2 > FEMTree< Dim , Real >::_multiSplatPointData( V zero , Allocator< FEMTreeNode > *nodeAllocator , const DensityEstimator< WeightDegree >* densityWeights , FEMTreeNode* node , Point< Real , Dim > position , V v , SparseNodeData< V , UIntPack< DataSigs ... > >& dataInfo , PointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey , PointSupportKey< UIntPack< FEMSignature< DataSigs >::Degree ... > >& dataKey , int dim ) |
| 453 |
{ |
| 454 |
typedef UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > SupportSizes; |
| 455 |
Real _depth , weight; |
| 456 |
if( densityWeights ) _getSampleDepthAndWeight( *densityWeights , position , weightKey , _depth , weight ); |
| 457 |
else _depth=(Real)-1. , weight = (Real)1.; |
| 458 |
V _v = v * weight; |
| 459 |
|
| 460 |
double values[ Dim ][ SupportSizes::Max() ]; |
| 461 |
dataKey.template getNeighbors< CreateNodes , ThreadSafe >( node , nodeAllocator , _nodeInitializer ); |
| 462 |
|
| 463 |
for( FEMTreeNode* _node=node ; _localDepth( _node )>=0 ; _node=_node->parent ) |
| 464 |
{ |
| 465 |
V __v = _v * (Real)pow( 1<<_localDepth( _node ) , dim ); |
| 466 |
Point< Real , Dim > start; |
| 467 |
Real w; |
| 468 |
_startAndWidth( _node , start , w ); |
| 469 |
__SetBSplineComponentValues< Real , FEMSignature< DataSigs >::Degree ... >( &position[0] , &start[0] , w , &values[0][0] , SupportSizes::Max() ); |
| 470 |
typename FEMTreeNode::template Neighbors< UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... > >& neighbors = dataKey.neighbors[ _localToGlobal( _localDepth( _node ) ) ]; |
| 471 |
double scratch[Dim+1]; |
| 472 |
scratch[0] = 1.; |
| 473 |
WindowLoop< Dim >::Run |
| 474 |
( |
| 475 |
ZeroUIntPack< Dim >() , UIntPack< BSplineSupportSizes< FEMSignature< DataSigs >::Degree >::SupportSize ... >() , |
| 476 |
[&]( int d , int i ){ scratch[d+1] = scratch[d] * values[d][i]; } , |
| 477 |
[&]( FEMTreeNode* node ){ if( IsActiveNode< Dim >( node ) ) Atomic< V >::Add( dataInfo.at( node , zero ) , __v * (Real)scratch[Dim] ) ; } , |
| 478 |
neighbors.neighbors() |
| 479 |
); |
| 480 |
} |
| 481 |
return Point< Real , 2 >( _depth , weight ); |
| 482 |
} |
| 483 |
|
| 484 |
template< unsigned int Dim , class Real > |
| 485 |
template< unsigned int WeightDegree , class V , unsigned int ... DataSigs > |
| 486 |
Real FEMTree< Dim , Real >::_nearestMultiSplatPointData( V zero , const DensityEstimator< WeightDegree >* densityWeights , FEMTreeNode* node , Point< Real , Dim > position , V v , SparseNodeData< V , UIntPack< DataSigs ... > >& dataInfo , PointSupportKey< IsotropicUIntPack< Dim , WeightDegree > >& weightKey , int dim ) |
| 487 |
{ |
| 488 |
Real _depth , weight; |
| 489 |
if( densityWeights ) _getSampleDepthAndWeight( *densityWeights , position , weightKey , _depth , weight ); |
| 490 |
else weight = (Real)1.; |
| 491 |
V _v = v * weight; |
| 492 |
|
| 493 |
for( FEMTreeNode* _node=node ; _localDepth( _node )>=0 ; _node=_node->parent ) if( IsActiveNode< Dim >( _node ) ) Atomic< V >::Add( dataInfo.at( _node , zero ) , _v * (Real)pow( 1<<_localDepth( _node ) , dim ) ); |
| 494 |
return weight; |
| 495 |
} |
| 496 |
////////////////////////////////// |
| 497 |
// MultiThreadedWeightEvaluator // |
| 498 |
////////////////////////////////// |
| 499 |
template< unsigned int Dim , class Real > |
| 500 |
template< unsigned int DensityDegree > |
| 501 |
FEMTree< Dim , Real >::MultiThreadedWeightEvaluator< DensityDegree >::MultiThreadedWeightEvaluator( const FEMTree< Dim , Real >* tree , const DensityEstimator< DensityDegree >& density , int threads ) : _density( density ) , _tree( tree ) |
| 502 |
{ |
| 503 |
_threads = std::max< int >( 1 , threads ); |
| 504 |
_neighborKeys.resize( _threads ); |
| 505 |
for( int t=0 ; t<_neighborKeys.size() ; t++ ) _neighborKeys[t].set( tree->_localToGlobal( density.kernelDepth() ) ); |
| 506 |
} |
| 507 |
template< unsigned int Dim , class Real > |
| 508 |
template< unsigned int DensityDegree > |
| 509 |
Real FEMTree< Dim , Real >::MultiThreadedWeightEvaluator< DensityDegree >::weight( Point< Real , Dim > p , int thread ) |
| 510 |
{ |
| 511 |
ConstPointSupportKey< IsotropicUIntPack< Dim , DensityDegree > >& nKey = _neighborKeys[thread]; |
| 512 |
Real depth , weight; |
| 513 |
_tree->_getSampleDepthAndWeight( _density , p , nKey , depth , weight ); |
| 514 |
return weight; |
| 515 |
} |