| 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 > Real Random( void ){ return Real( rand() )/Real( RAND_MAX ); } |
| 30 |
|
| 31 |
template< class Real , int Dim > |
| 32 |
Point< Real , Dim > RandomBallPoint( void ) |
| 33 |
{ |
| 34 |
Point< Real , Dim > p; |
| 35 |
while(1) |
| 36 |
{ |
| 37 |
for( int d=0 ; d<Dim ; d++ ) p[d] = Real( 1.0-2.0*Random< Real >() ); |
| 38 |
double l=SquareLength(p); |
| 39 |
if( SquareLength( p )<=1 ) return p; |
| 40 |
} |
| 41 |
} |
| 42 |
template< class Real , int Dim > |
| 43 |
Point< Real , Dim > RandomSpherePoint( void ) |
| 44 |
{ |
| 45 |
Point< Real , Dim > p = RandomBallPoint< Real , Dim >(); |
| 46 |
return p / (Real)Length( p ); |
| 47 |
} |
| 48 |
|
| 49 |
///////////// |
| 50 |
// Simplex // |
| 51 |
///////////// |
| 52 |
template< class Real , unsigned int Dim , unsigned int K > |
| 53 |
void Simplex< Real , Dim , K >::split( Point< Real , Dim > pNormal , Real pOffset , std::vector< Simplex >& back , std::vector< Simplex >& front ) const |
| 54 |
{ |
| 55 |
Real values[K+1]; |
| 56 |
bool frontSet = false , backSet = false; |
| 57 |
|
| 58 |
// Evaluate the hyper-plane's function at the vertices and mark if strictly front/back vertices have been found |
| 59 |
for( unsigned int k=0 ; k<=K ; k++ ) |
| 60 |
{ |
| 61 |
values[k] = Point< Real , Dim >::Dot( p[k] , pNormal ) - pOffset; |
| 62 |
backSet |= ( values[k]<0 ) , frontSet |= ( values[k]>0 ); |
| 63 |
} |
| 64 |
|
| 65 |
// If all the vertices are behind or on, or all the vertices are in front or on, we are done. |
| 66 |
if( !frontSet ){ back.push_back( *this ) ; return; } |
| 67 |
if( !backSet ){ front.push_back( *this ) ; return; } |
| 68 |
|
| 69 |
// Pick some intersection of the hyper-plane with a simplex edge |
| 70 |
unsigned int v1 , v2; |
| 71 |
Point< Real , Dim > midPoint; |
| 72 |
{ |
| 73 |
for( unsigned int i=0 ; i<K ; i++ ) for( unsigned int j=i+1 ; j<=K ; j++ ) if( values[i]*values[j]<0 ) |
| 74 |
{ |
| 75 |
v1 = i , v2 = j; |
| 76 |
Real t1 = values[i] / ( values[i] - values[j] ) , t2 = (Real)( 1. - t1 ); |
| 77 |
midPoint = p[j]*t1 + p[i]*t2; |
| 78 |
} |
| 79 |
} |
| 80 |
// Iterate over each face of the simplex, split it with the hyper-plane and connect the sub-simplices to the mid-point |
| 81 |
for( unsigned int i=0 ; i<=K ; i++ ) |
| 82 |
{ |
| 83 |
if( i!=v1 && i!=v2 ) continue; |
| 84 |
Simplex< Real , Dim , K-1 > f; // The face |
| 85 |
Simplex< Real , Dim , K > s; // The sub-simplex |
| 86 |
for( unsigned int j=0 , idx=0 ; j<=K ; j++ ) if( j!=i ) f[idx++] = p[j]; |
| 87 |
std::vector< Simplex< Real , Dim , K-1 > > _back , _front; |
| 88 |
f.split( pNormal , pOffset , _back , _front ); |
| 89 |
s[i] = midPoint; |
| 90 |
|
| 91 |
for( unsigned int j=0 ; j<_back.size() ; j++ ) |
| 92 |
{ |
| 93 |
for( unsigned int k=0 ; k<K ; k++ ) s[ k<i ? k : k+1 ] = _back[j][k]; |
| 94 |
back.push_back( s ); |
| 95 |
} |
| 96 |
|
| 97 |
for( unsigned int j=0 ; j<_front.size() ; j++ ) |
| 98 |
{ |
| 99 |
for( unsigned int k=0 ; k<K ; k++ ) s[ k<i ? k : k+1 ] = _front[j][k]; |
| 100 |
front.push_back( s ); |
| 101 |
} |
| 102 |
} |
| 103 |
} |