| 1 |
/* |
| 2 |
Copyright (c) 2023, 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 |
//////////////////// |
| 31 |
// GetBoundingBox // |
| 32 |
//////////////////// |
| 33 |
template< class Real , unsigned int Dim > |
| 34 |
XForm< Real , Dim+1 > GetXForm( Point< Real , Dim > min , Point< Real , Dim > max , Real scaleFactor , bool rotate ) |
| 35 |
{ |
| 36 |
Point< Real , Dim > center = ( max + min ) / 2; |
| 37 |
Real scale = max[0] - min[0]; |
| 38 |
for( int d=1 ; d<Dim ; d++ ) scale = std::max< Real >( scale , max[d]-min[d] ); |
| 39 |
scale *= scaleFactor; |
| 40 |
for( int i=0 ; i<Dim ; i++ ) center[i] -= scale/2; |
| 41 |
XForm< Real , Dim+1 > tXForm = XForm< Real , Dim+1 >::Identity() , sXForm = XForm< Real , Dim+1 >::Identity() , rXForm = XForm< Real , Dim+1 >::Identity(); |
| 42 |
for( int i=0 ; i<Dim ; i++ ) sXForm(i,i) = (Real)(1./scale ) , tXForm(Dim,i) = -center[i]; |
| 43 |
unsigned int maxDim = 0; |
| 44 |
for( int i=1 ; i<Dim ; i++ ) if( (max[i]-min[i])>(max[maxDim]-min[maxDim]) ) maxDim = i; |
| 45 |
if( rotate ) |
| 46 |
{ |
| 47 |
for( int i=0 ; i<Dim ; i++ ) rXForm(i,i) = 0; |
| 48 |
for( int i=0 ; i<Dim ; i++ ) rXForm((maxDim+i)%Dim,(Dim-1+i)%Dim) = 1; |
| 49 |
} |
| 50 |
return rXForm * sXForm * tXForm; |
| 51 |
} |
| 52 |
|
| 53 |
template< class Real , unsigned int Dim , bool ExtendedAxes > |
| 54 |
XForm< Real , Dim+1 > GetXForm( const Extent< Real , Dim , ExtendedAxes > &extent , Real scaleFactor , unsigned int dir ) |
| 55 |
{ |
| 56 |
using _Extent = Extent< Real , Dim , ExtendedAxes >; |
| 57 |
bool rotate = ( dir >= _Extent::DirectionN ); |
| 58 |
|
| 59 |
if( rotate ) // Find the direction of maximal extent |
| 60 |
{ |
| 61 |
dir = 0; |
| 62 |
for( unsigned int d=1 ; d<_Extent::DirectionN ; d++ ) if( extent[d].second - extent[d].first > extent[dir].second - extent[dir].first ) dir = d; |
| 63 |
} |
| 64 |
|
| 65 |
const unsigned int *frame = _Extent::Frame(dir); |
| 66 |
|
| 67 |
// Compute the rotation taking the direction of maximal extent to the last axis |
| 68 |
XForm< Real , Dim+1 > R = XForm< Real , Dim+1 >::Identity(); |
| 69 |
for( unsigned int c=0 ; c<Dim ; c++ ) for( unsigned int r=0 ; r<Dim ; r++ ) R(r,c) = _Extent::Direction( frame[c] )[r]; |
| 70 |
|
| 71 |
// Get the bounding box with respect to the maximal extent direction's frame |
| 72 |
Point< Real , Dim > bBox[2]; |
| 73 |
for( unsigned int d=0 ; d<Dim ; d++ ) |
| 74 |
{ |
| 75 |
bBox[0][d] = extent[ frame[d] ].first; |
| 76 |
bBox[1][d] = extent[ frame[d] ].second; |
| 77 |
} |
| 78 |
return GetXForm( bBox[0] , bBox[1] , scaleFactor , rotate ) * R; |
| 79 |
} |
| 80 |
|
| 81 |
/////////// |
| 82 |
// Frame // |
| 83 |
/////////// |
| 84 |
template< typename Real , unsigned int Dim , bool ExtendedAxes > |
| 85 |
Frame< Real , Dim , ExtendedAxes >::Frame( void ) |
| 86 |
{ |
| 87 |
for( unsigned int d=0 ; d<Dim ; d++ ) |
| 88 |
{ |
| 89 |
directions[d][d] = 1; |
| 90 |
for( unsigned int dd=0 ; dd<Dim ; dd++ ) frames[d][dd] = (d+1+dd) % Dim; |
| 91 |
} |
| 92 |
|
| 93 |
if constexpr( ExtendedAxes ) |
| 94 |
{ |
| 95 |
static_assert( Dim==2 || Dim==3 , "[ERROR] Non-axis extents only supported for dimensions 2 and 3" ); |
| 96 |
if constexpr( Dim==2 ) |
| 97 |
{ |
| 98 |
directions[2] = Point< Real , 2 >( 1 , 1 )/(Real)sqrt(2.); |
| 99 |
directions[3] = Point< Real , 2 >( 1 ,-1 )/(Real)sqrt(2.); |
| 100 |
frames[2][0] = 2 , frames[2][1] = 3; |
| 101 |
frames[3][0] = 3 , frames[3][1] = 2; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
directions[3] = Point< Real , 3 >( 1 , 1 , 0 )/(Real)sqrt(2.); |
| 106 |
directions[4] = Point< Real , 3 >( 1 , 0 , 1 )/(Real)sqrt(2.); |
| 107 |
directions[5] = Point< Real , 3 >( 0 , 1 , 1 )/(Real)sqrt(2.); |
| 108 |
directions[6] = Point< Real , 3 >( 1 ,-1 , 0 )/(Real)sqrt(2.); |
| 109 |
directions[7] = Point< Real , 3 >( 1 , 0 ,-1 )/(Real)sqrt(2.); |
| 110 |
directions[8] = Point< Real , 3 >( 0 , 1 ,-1 )/(Real)sqrt(2.); |
| 111 |
frames[3][0] = 2 , frames[3][1] = 6 , frames[3][2] = 3; |
| 112 |
frames[4][0] = 7 , frames[4][1] = 1 , frames[4][2] = 4; |
| 113 |
frames[5][0] = 0 , frames[5][1] = 8 , frames[5][2] = 5; |
| 114 |
frames[6][0] = 3 , frames[6][1] = 2 , frames[6][2] = 6; |
| 115 |
frames[7][0] = 1 , frames[7][1] = 4 , frames[7][2] = 7; |
| 116 |
frames[8][0] = 5 , frames[8][1] = 0 , frames[8][2] = 8; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
//////////// |
| 122 |
// Extent // |
| 123 |
//////////// |
| 124 |
template< typename Real , unsigned int Dim , bool ExtendedAxes > |
| 125 |
Extent< Real , Dim , ExtendedAxes >::Extent( void ) |
| 126 |
{ |
| 127 |
Real inf = std::numeric_limits< Real >::infinity(); |
| 128 |
for( unsigned int d=0 ; d<DirectionN ; d++ ) extents[d].first = inf , extents[d].second = -inf; |
| 129 |
} |
| 130 |
|
| 131 |
template< typename Real , unsigned int Dim , bool ExtendedAxes > |
| 132 |
void Extent< Real , Dim , ExtendedAxes >::add( Point< Real , Dim > p ) |
| 133 |
{ |
| 134 |
for( unsigned int d=0 ; d<DirectionN ; d++ ) |
| 135 |
{ |
| 136 |
extents[d].first = std::min< Real >( extents[d].first , Point< Real , Dim >::Dot( p , _Frame.directions[d] ) ); |
| 137 |
extents[d].second = std::max< Real >( extents[d].second , Point< Real , Dim >::Dot( p , _Frame.directions[d] ) ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
template< typename Real , unsigned int Dim , bool ExtendedAxes > |
| 142 |
Extent< Real , Dim , ExtendedAxes > Extent< Real , Dim , ExtendedAxes >::operator + ( const Extent &e ) const |
| 143 |
{ |
| 144 |
Extent _e; |
| 145 |
for( unsigned int d=0 ; d<DirectionN ; d++ ) |
| 146 |
{ |
| 147 |
_e.extents[d].first = std::min< Real >( extents[d].first , e.extents[d].first ); |
| 148 |
_e.extents[d].second = std::max< Real >( extents[d].second , e.extents[d].second ); |
| 149 |
} |
| 150 |
return _e; |
| 151 |
} |
| 152 |
|
| 153 |
template< typename Real , unsigned int Dim , bool ExtendedAxes > |
| 154 |
std::ostream &operator << ( std::ostream &os , const Extent< Real , Dim , ExtendedAxes > &e ) |
| 155 |
{ |
| 156 |
for( unsigned int d=0 ; d<Extent< Real , Dim , ExtendedAxes >::DirectionN ; d++ ) |
| 157 |
{ |
| 158 |
os << Extent< Real , Dim , ExtendedAxes >::_Frame.directions[d] << " : [ " << e.extents[d].first << " , " << e.extents[d].second << " ]"; |
| 159 |
os << "\t(" << e.extents[d].second - e.extents[d].first << " )" << std::endl; |
| 160 |
} |
| 161 |
return os; |
| 162 |
} |
| 163 |
|
| 164 |
template< class Real , unsigned int Dim , bool ExtendedAxes , typename ... Data > |
| 165 |
Extent< Real , Dim , ExtendedAxes > GetExtent( InputDataStream< Point< Real , Dim > , Data ... > &stream , Data ... data ) |
| 166 |
{ |
| 167 |
Point< Real, Dim > p; |
| 168 |
Extent< Real , Dim , ExtendedAxes > e; |
| 169 |
while( stream.read( p , data... ) ) e.add(p); |
| 170 |
return e; |
| 171 |
} |
| 172 |
|
| 173 |
template< class Real , unsigned int Dim , bool ExtendedAxes , typename ... Data > |
| 174 |
XForm< Real , Dim+1 > GetXForm( InputDataStream< Point< Real , Dim > , Data ... > &stream , Data ... data , Real scaleFactor , unsigned int dir ) |
| 175 |
{ |
| 176 |
return GetXForm( GetExtent< Real , Dim , ExtendedAxes >( stream , data... ) , scaleFactor , dir ); |
| 177 |
} |