Debian Codegraph @

src/thirdparty/PoissonRecon/Geometry.h

raw
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 #ifndef GEOMETRY_INCLUDED
30 #define GEOMETRY_INCLUDED
31
32 #include <stdio.h>
33 #include <math.h>
34 #include <vector>
35 #include <stdlib.h>
36 #include <unordered_map>
37 #include <string.h>
38 #ifdef _WIN32
39 #include <io.h>
40 #endif // _WIN32
41 #include "MyMiscellany.h"
42 #include "Array.h"
43
44 namespace PoissonRecon
45 {
46
47 // An empty type
48 template< typename Real >
49 struct EmptyVectorType
50 {
51 EmptyVectorType& operator += ( const EmptyVectorType& p ){ return *this; }
52 EmptyVectorType& operator -= ( const EmptyVectorType& p ){ return *this; }
53 EmptyVectorType& operator *= ( Real s ) { return *this; }
54 EmptyVectorType& operator /= ( Real s ) { return *this; }
55 EmptyVectorType operator + ( const EmptyVectorType& p ) const { EmptyVectorType _p = *this ; _p += p ; return _p; }
56 EmptyVectorType operator - ( const EmptyVectorType& p ) const { EmptyVectorType _p = *this ; _p -= p ; return _p; }
57 EmptyVectorType operator * ( Real s ) const { EmptyVectorType _p = *this ; _p *= s ; return _p; }
58 EmptyVectorType operator / ( Real s ) const { EmptyVectorType _p = *this ; _p /= s ; return _p; }
59
60 friend std::ostream &operator << ( std::ostream &os , const EmptyVectorType &v ){ return os; }
61 };
62 template< typename Real > EmptyVectorType< Real > operator * ( Real s , EmptyVectorType< Real > v ){ return v*s; }
63
64 template< typename _Real , typename ... VectorTypes > struct DirectSum;
65
66 template< typename _Real , typename FirstType , typename ... RestTypes >
67 struct DirectSum< _Real , FirstType , RestTypes... >
68 {
69 friend Atomic< DirectSum< _Real , FirstType , RestTypes... > >;
70
71 using Real = _Real;
72
73 template< unsigned int I > using VectorType = std::tuple_element_t< I , std::tuple< FirstType , RestTypes ... > >;
74
75 template< unsigned int I > VectorType< I >& get( void )
76 {
77 if constexpr( I==0 ) return _first;
78 else return _rest.template get< I-1 >();
79 }
80 template< unsigned int I > const VectorType< I >& get( void ) const
81 {
82 if constexpr( I==0 ) return _first;
83 else return _rest.template get< I-1 >();
84 }
85
86 DirectSum& operator += ( const DirectSum& p ){ _first += p._first ; _rest += p._rest ; return *this; }
87 DirectSum& operator -= ( const DirectSum& p ){ _first -= p._first ; _rest -= p._rest ; return *this; }
88 DirectSum& operator *= ( Real s ) { _first *= s ; _rest *= s ; return *this; }
89 DirectSum& operator /= ( Real s ) { _first /= s ; _rest /= s ; return *this; }
90 DirectSum operator + ( const DirectSum& p ) const { DirectSum _p = *this ; _p += p ; return _p; }
91 DirectSum operator - ( const DirectSum& p ) const { DirectSum _p = *this ; _p -= p ; return _p; }
92 DirectSum operator * ( Real s ) const { DirectSum _p = *this ; _p *= s ; return _p; }
93 DirectSum operator / ( Real s ) const { DirectSum _p = *this ; _p /= s ; return _p; }
94
95 DirectSum( void ){}
96
97 template< typename _FirstType , typename ... _RestTypes >
98 DirectSum( const _FirstType &first , const _RestTypes & ... rest ) : _first(first) , _rest(rest...){}
99
100 template< typename ComponentFunctor /*=std::function< void (VectorTypes&...)>*/ >
101 void process( ComponentFunctor f ){ _process( f ); }
102
103 template< typename ComponentFunctor /*=std::function< void (const VectorTypes&...)>*/ >
104 void process( ComponentFunctor f ) const { _process( f ); }
105
106 friend std::ostream &operator << ( std::ostream &os , const DirectSum &v )
107 {
108 os << "{ ";
109 v._write( os );
110 os << " }";
111 return os;
112 }
113
114 protected:
115 FirstType _first;
116 DirectSum< Real , RestTypes... > _rest;
117
118 void _write( std::ostream &os ) const
119 {
120 os << _first;
121 if constexpr( sizeof...(RestTypes) )
122 {
123 os << " , ";
124 _rest._write( os );
125 }
126 }
127
128 template< typename ComponentFunctor /*=std::function< void ( FirstType& , RestTypes&... )>*/ , typename ... Components >
129 void _process( ComponentFunctor &f , Components ... c )
130 {
131 if constexpr( sizeof...(Components)==sizeof...(RestTypes)+1 ) f( c... );
132 else _process( f , c... , this->template get< sizeof...(Components) >() );
133 }
134 template< typename ComponentFunctor /*=std::function< void ( const FirstType& , const RestTypes&... )>*/ , typename ... Components >
135 void _process( ComponentFunctor &f , Components ... c ) const
136 {
137 if constexpr( sizeof...(Components)==sizeof...(RestTypes)+1 ) f( c... );
138 else _process( f , c... , this->template get< sizeof...(Components) >() );
139 }
140 };
141
142 template< typename _Real >
143 struct DirectSum< _Real >
144 {
145 using Real = _Real;
146
147 DirectSum& operator += ( const DirectSum& p ){ return *this; }
148 DirectSum& operator -= ( const DirectSum& p ){ return *this; }
149 DirectSum& operator *= ( Real s ) { return *this; }
150 DirectSum& operator /= ( Real s ) { return *this; }
151 DirectSum operator + ( const DirectSum& p ) const { return DirectSum(); }
152 DirectSum operator - ( const DirectSum& p ) const { return DirectSum(); }
153 DirectSum operator * ( Real s ) const { return DirectSum(); }
154 DirectSum operator / ( Real s ) const { return DirectSum(); }
155
156 DirectSum( void ){}
157
158 template< typename ComponentFunctor /*=std::function< void (VectorTypes&...)>*/ >
159 void process( ComponentFunctor f ){ f(); }
160
161 template< typename ComponentFunctor /*=std::function< void (const VectorTypes&...)>*/ >
162 void process( ComponentFunctor f ) const { f(); }
163
164 friend std::ostream &operator << ( std::ostream &os , const DirectSum &v ){ return os << "{ }"; }
165 };
166
167 template< typename Real , typename ... Vectors >
168 DirectSum< Real , Vectors ... > operator * ( Real s , DirectSum< Real , Vectors ... > vu ){ return vu * s; }
169
170 template< class Real > Real Random( void );
171
172 template< class Real , unsigned int Dim > struct XForm;
173
174
175 template< typename Real , unsigned int ... Dims > struct Point;
176
177 template< class Real , unsigned int Dim >
178 struct Point< Real , Dim >
179 {
180 void _init( unsigned int d )
181 {
182 if( !d ) memset( coords , 0 , sizeof(Real)*Dim );
183 else MK_THROW( "Should never be called" );
184 }
185 template< class _Real , class ... _Reals > void _init( unsigned int d , _Real v , _Reals ... values )
186 {
187 coords[d] = (Real)v;
188 if( d+1<Dim ) _init( d+1 , values... );
189 }
190 template< class ... Points >
191 static void _AddColumnVector( XForm< Real , Dim >& x , unsigned int c , Point point , Points ... points )
192 {
193 for( unsigned int r=0 ; r<Dim ; r++ ) x( c , r ) = point[r];
194 _AddColumnVector( x , c+1 , points ... );
195 }
196 static void _AddColumnVector( XForm< Real , Dim >& x , unsigned int c ){ ; }
197 public:
198 Real coords[Dim];
199 Point( void ) { memset( coords , 0 , sizeof(Real)*Dim ); }
200 Point( const Point& p ){ memcpy( coords , p.coords , sizeof(Real)*Dim ); }
201 template< class ... _Reals > Point( _Reals ... values ){ static_assert( sizeof...(values)==Dim || sizeof...(values)==0 , "[ERROR] Point::Point: Invalid number of coefficients" ) ; _init( 0 , values... ); }
202 template< class _Real > Point( const Point< _Real , Dim >& p ){ for( unsigned int d=0 ; d<Dim ; d++ ) coords[d] = (Real) p.coords[d]; }
203 Point( Real *values ){ for( unsigned int d=0 ; d<Dim ; d++ ) coords[d] = values[d]; }
204 Point( const Real *values ){ for( unsigned int d=0 ; d<Dim ; d++ ) coords[d] = values[d]; }
205 inline Real& operator[] ( unsigned int i ) { return coords[i]; }
206 inline const Real& operator[] ( unsigned int i ) const { return coords[i]; }
207 inline Point operator - ( void ) const { Point q ; for( unsigned int d=0 ; d<Dim ; d++ ) q.coords[d] = - coords[d] ; return q; }
208
209 template< class _Real > inline Point& operator += ( Point< _Real , Dim > p ) { for( unsigned int d=0 ; d<Dim ; d++ ) coords[d] += (Real)p.coords[d] ; return *this; }
210 template< class _Real > inline Point operator + ( Point< _Real , Dim > p ) const { Point q ; for( unsigned int d=0 ; d<Dim ; d++ ) q.coords[d] = coords[d] + (Real)p.coords[d] ; return q; }
211 template< class _Real > inline Point& operator -= ( Point< _Real , Dim > p ) { return (*this)+=(-p); }
212 template< class _Real > inline Point operator - ( Point< _Real , Dim > p ) const { return (*this)+ (-p); }
213 template< class Scalar > inline Point& operator *= ( Scalar r ) { for( unsigned int d=0 ; d<Dim ; d++ ) coords[d] *= r ; return *this; }
214 template< class Scalar > inline Point operator * ( Scalar r ) const { Point q ; for( unsigned int d=0 ; d<Dim ; d++ ) q.coords[d] = coords[d] * r ; return q; }
215 template< class Scalar > inline Point& operator /= ( Scalar r ) { for( unsigned int d=0 ; d<Dim ; d++ ) coords[d] /= r ; return *this; }
216 template< class Scalar > inline Point operator / ( Scalar r ) const { Point q ; for( unsigned int d=0 ; d<Dim ; d++ ) q.coords[d] = coords[d] / r ; return q; }
217 template< class _Real > inline Point& operator *= ( Point< _Real , Dim > p ) { for( unsigned int d=0 ; d<Dim ; d++ ) coords[d] *= p.coords[d] ; return *this; }
218 template< class _Real > inline Point operator * ( Point< _Real , Dim > p ) const { Point q ; for( unsigned int d=0 ; d<Dim ; d++ ) q.coords[d] = coords[d] * p.coords[d] ; return q; }
219 template< class _Real > inline Point& operator /= ( Point< _Real , Dim > p ) { for( unsigned int d=0 ; d<Dim ; d++ ) coords[d] /= p.coords[d] ; return *this; }
220 template< class _Real > inline Point operator / ( Point< _Real , Dim > p ) const { Point q ; for( unsigned int d=0 ; d<Dim ; d++ ) q.coords[d] = coords[d] / p.coords[d] ; return q; }
221
222 static Real Dot( const Point& p1 , const Point& p2 ){ Real dot = {} ; for( unsigned int d=0 ; d<Dim ; d++ ) dot += p1.coords[d] * p2.coords[d] ; return dot; }
223 static Real SquareNorm( const Point& p ){ return Dot( p , p ); }
224 template< class ... Points > static Point CrossProduct( Points ... points )
225 {
226 static_assert( sizeof ... ( points )==Dim-1 , "Number of points in cross-product must be one less than the dimension" );
227 XForm< Real , Dim > x;
228 _AddColumnVector( x , 0 , points ... );
229 Point p;
230 for( unsigned int d=0 ; d<Dim ; d++ ) p[d] = ( d&1 ) ? -x.subDeterminant( Dim-1 , d ) : x.subDeterminant( Dim-1 , d );
231 return p;
232 }
233 static Point CrossProduct( const Point* points )
234 {
235 XForm< Real , Dim > x;
236 for( unsigned int d=0 ; d<Dim-1 ; d++ ) for( unsigned int c=0 ; c<Dim ; c++ ) x(d,c) = points[d][c];
237 Point p;
238 for( unsigned int d=0 ; d<Dim ; d++ ) p[d] = ( d&1 ) ? -x.subDeterminant( Dim-1 , d ) : x.subDeterminant( Dim-1 , d );
239 return p;
240 }
241 static Point CrossProduct( Point* points ){ return CrossProduct( ( const Point* )points ); }
242
243 template< class _Real , unsigned int _Dim >
244 friend std::ostream &operator << ( std::ostream &os , const Point< _Real , _Dim > &p );
245 };
246 template< class Real , unsigned int Dim > Point< Real , Dim > operator * ( Real r , Point< Real , Dim > p ){ return p*r; }
247 template< class Real , unsigned int Dim > Point< Real , Dim > operator / ( Real r , Point< Real , Dim > p ){ return p/r; }
248 template< class Real , unsigned int Dim >
249 std::ostream &operator << ( std::ostream &os , const Point< Real , Dim > &p )
250 {
251 os << "( ";
252 for( int d=0 ; d<Dim ; d++ )
253 {
254 if( d ) os << " , ";
255 os << p[d];
256 }
257 return os << " )";
258 }
259
260 // This class represents a point whose is size is allocated dynamically.
261 // The size can be set by:
262 // 1. Construction
263 // 2. Copying
264 // 3. Adding / subtracting (if the size has not been set yet)
265 template< class Real >
266 struct Point< Real >
267 {
268 friend struct Atomic< Point< Real > >;
269
270 Point( void ) : _coords( NullPointer(Real) ) , _dim(0){}
271 Point( size_t dim ) : _coords( NullPointer(Real) ) , _dim(0) { if( dim ){ _resize( (unsigned int)dim ) ; memset( _coords , 0 , sizeof(Real)*_dim ); } }
272 Point( const Point &p ) : _coords( NullPointer(Real) ) , _dim(0) { if( p._dim ){ _resize( p._dim ) ; memcpy( _coords , p._coords , sizeof(Real)*_dim ); } }
273 ~Point( void ){ DeletePointer( _coords ); }
274
275 Point &operator = ( const Point &p )
276 {
277 if( !_dim || !p._dim ){ _resize( p._dim ) ; memcpy( _coords , p._coords , sizeof(Real)*_dim ); }
278 else if( _dim==p._dim ) memcpy( _coords , p._coords , sizeof(Real)*_dim );
279 else MK_THROW( "Dimensions don't match: " , _dim , " != " , p._dim );
280 return *this;
281 }
282
283 unsigned int dim( void ) const { return _dim; }
284 Real &operator[]( size_t idx ){ return _coords[idx]; }
285 const Real &operator[]( size_t idx ) const { return _coords[idx]; }
286
287
288 Point& operator += ( const Point& p )
289 {
290 if( !_dim ){ _resize( p._dim ) ; for( unsigned int i=0 ; i<_dim ; i++ ) _coords[i] = p._coords[i]; }
291 else if( _dim==p._dim ) for( unsigned int i=0 ; i<_dim ; i++ ) _coords[i] += p._coords[i];
292 else MK_THROW( "Dimensions don't match: " , _dim , " != " , p._dim );
293 return *this;
294 }
295 Point& operator -= ( const Point& p )
296 {
297 if( !_dim ){ _resize( p._dim ) ; for( unsigned int i=0 ; i<_dim ; i++ ) _coords[i] = -p._coords[i]; }
298 else if( _dim==p._dim ) for( unsigned int i=0 ; i<_dim ; i++ ) _coords[i] -= p._coords[i];
299 else MK_THROW( "Dimensions don't match: " , _dim , " != " , p._dim );
300 return *this;
301 }
302 Point& operator *= ( Real s )
303 {
304 for( unsigned int i=0 ; i<_dim ; i++ ) (*this)[i] *= s;
305 return *this;
306 }
307 Point& operator /= ( Real s )
308 {
309 for( unsigned int i=0 ; i<_dim ; i++ ) (*this)[i] /= s;
310 return *this;
311 }
312 Point operator + ( const Point& p ) const { Point _p = *this ; _p += p ; return _p; }
313 Point operator - ( const Point& p ) const { Point _p = *this ; _p -= p ; return _p; }
314 Point operator * ( Real s ) const { Point _p = *this ; _p *= s ; return _p; }
315 Point operator / ( Real s ) const { Point _p = *this ; _p /= s ; return _p; }
316
317 static Real Dot( const Point &p1 , const Point &p2 )
318 {
319 Real dot;
320 if( p1._dim!=p2._dim ) MK_THROW( "Dimensions differ: " , p1._dim , " != " , p2._dim );
321 for( size_t d=0 ; d<p1._dim ; d++ ) dot += p1[d] * p2[d];
322 return dot;
323 }
324 static Real SquareNorm( const Point& p ){ return Dot( p , p ); }
325
326 friend std::ostream &operator << ( std::ostream &os , const Point &p )
327 {
328 os << "( ";
329 for( size_t i=0 ; i<p._dim ; i++ )
330 {
331 if( i ) os << " , ";
332 os << p[i];
333 }
334 return os << " )";
335 }
336 protected:
337 Pointer( Real ) _coords;
338 unsigned int _dim;
339 void _resize( unsigned int dim )
340 {
341 DeletePointer( _coords );
342 if( dim ) _coords = NewPointer< Real >( dim );
343 _dim = dim;
344 }
345 };
346 template< class Real > Point< Real > operator * ( Real s , Point< Real > p ){ return p*s; }
347
348 /** This templated class represents a Ray.*/
349 template< class Real , unsigned int Dim >
350 class Ray
351 {
352 public:
353 /** The starting point of the ray */
354 Point< Real , Dim > position;
355
356 /** The direction of the ray */
357 Point< Real , Dim > direction;
358
359 /** The default constructor */
360 Ray( void ){}
361
362 /** The constructor settign the the position and direction of the ray */
363 Ray( const Point< Real , Dim > &p , const Point< Real , Dim > &d ) : position(p) , direction(d){}
364
365 /** This method computes the translation of the ray by p and returns the translated ray.*/
366 Ray operator + ( const Point< Real , Dim > &p ) const { return Ray( position+p , direction ); }
367
368 /** This method translates the current ray by p.*/
369 Ray &operator += ( const Point< Real , Dim > &p ){ position +=p ; return *this; }
370
371 /** This method computes the translation of the ray by -p and returns the translated ray.*/
372 Ray operator - ( const Point< Real , Dim > &p ) const { return Ray( position-p , direction ); }
373
374 /** This method translates the current ray by -p.*/
375 Ray &operator -= ( const Point< Real , Dim > &p ){ position -= p ; return *this; }
376
377 /** This method returns the point at a distance of t along the ray. */
378 Point< Real , Dim > operator() ( double t ) const { return position + direction * (Real)t; }
379 };
380
381 /** This function prints out the ray.*/
382 template< class Real , unsigned int Dim >
383 std::ostream &operator << ( std::ostream &stream , const Ray< Real , Dim > &ray )
384 {
385 stream << "[ " << ray.position << " ] [ " << ray.direction << " ]";
386 return stream;
387 }
388
389 template< class Real , unsigned int _Columns , unsigned int _Rows >
390 struct Matrix
391 {
392 static const unsigned int Columns = _Columns;
393 static const unsigned int Rows = _Rows;
394 Real coords[Columns][Rows];
395 Matrix( void ) { memset( coords , 0 , sizeof(coords) ); }
396 inline Real& operator() ( unsigned int c , unsigned int r ) { return coords[c][r]; }
397 inline const Real& operator() ( unsigned int c , unsigned int r ) const { return coords[c][r]; }
398 inline Real* operator[] ( unsigned int c ) { return coords[c] ; }
399 inline const Real* operator[] ( unsigned int c ) const { return coords[c] ; }
400
401 inline Matrix operator - ( void ) const { Matrix m ; for( unsigned int c=0 ; c<Columns ; c++ ) for( unsigned int r=0 ; r<Rows ; r++ ) m.coords[c][r] = - coords[c][r] ; return m; }
402
403 inline Matrix& operator += ( const Matrix& m ){ for( unsigned int c=0 ; c<Columns ; c++ ) for( unsigned int r=0 ; r<Rows ; r++ ) coords[c][r] += m.coords[c][r] ; return *this; }
404 inline Matrix operator + ( const Matrix& m ) const { Matrix n ; for( unsigned int c=0 ; c<Columns ; c++ ) for( unsigned int r=0 ; r<Rows ; r++ ) n.coords[c][r] = coords[c][r] + m.coords[c][r] ; return n; }
405 inline Matrix& operator *= ( Real s ) { for( unsigned int c=0 ; c<Columns ; c++ ) for( unsigned int r=0 ; r<Rows ; r++ ) coords[c][r] *= s ; return *this; }
406 inline Matrix operator * ( Real s ) const { Matrix n ; for( unsigned int c=0 ; c<Columns ; c++ ) for( unsigned int r=0 ; r<Rows ; r++ ) n.coords[c][r] = coords[c][r] * s ; return n; }
407
408 inline Matrix& operator -= ( const Matrix& m ){ return ( (*this)+=(-m) ); }
409 inline Matrix operator - ( const Matrix& m ) const { return (*this)+(-m); }
410 inline Matrix& operator /= ( Real s ){ return ( (*this)*=(Real)(1./s) ); }
411 inline Matrix operator / ( Real s ) const { return (*this) * ( (Real)(1./s) ); }
412
413 static Real Dot( const Matrix& m1 , const Matrix& m2 ){ Real dot = (Real)0 ; for( unsigned int c=0 ; c<Columns ; c++ ) for( unsigned int r=0 ; r<Rows ; r++ ) dot += m1.coords[c][r] * m2.coords[c][r] ; return dot; }
414
415 template< typename T >
416 inline Point< T , Rows > operator* ( const Point< T , Columns >& p ) const { Point< T , Rows > q ; for( unsigned int c=0 ; c<Columns ; c++ ) for( unsigned int r=0 ; r<Rows ; r++ ) q[r] += (T)( p[c] * coords[c][r] ) ; return q; }
417
418 template< unsigned int Cols >
419 inline Matrix< Real , Cols , Rows > operator * ( const Matrix< Real , Cols , Columns > &M ) const
420 {
421 Matrix< Real , Cols , Rows > prod;
422 for( unsigned int c=0 ; c<Cols ; c++ ) for( unsigned int r=0 ; r<Rows ; r++ ) for( unsigned int i=0 ; i<Columns ; i++ ) prod(c,r) += coords[i][r] * M(c,i);
423 return prod;
424 }
425
426 inline Matrix< Real , Rows , Columns > transpose( void ) const
427 {
428 Matrix< Real , Rows , Columns > t;
429 for( unsigned int c=0 ; c<Columns ; c++ ) for( unsigned int r=0 ; r<Rows ; r++ ) t(r,c) = coords[c][r];
430 return t;
431 }
432 friend std::ostream &operator << ( std::ostream &os , const Matrix &m )
433 {
434 os << "{ ";
435 for( int r=0 ; r<Rows ; r++ )
436 {
437 if( r ) os << " , ";
438 os << "{ ";
439 for( int c=0 ; c<Columns ; c++ )
440 {
441 if( c ) os << " , ";
442 os << m.coords[c][r];
443 }
444 os << " }";
445 }
446 return os << " }";
447 }
448 };
449
450 template< class Real , unsigned int Dim >
451 struct XForm
452 {
453 Real coords[Dim][Dim];
454 XForm( void ) { memset( coords , 0 , sizeof(Real) * Dim * Dim ); }
455 XForm( const Matrix< Real , Dim , Dim > &M ){ memcpy( coords , M.coords , sizeof(Real)*Dim*Dim ); }
456 XForm( const XForm &M ){ memcpy( coords , M.coords , sizeof(Real)*Dim*Dim ); }
457 XForm( const Matrix< Real , Dim+1 , Dim+1 > &M ){ for( unsigned int i=0 ; i<Dim ; i++ ) for( unsigned int j=0 ; j<Dim ; j++ ) this->operator()(i,j) = M(i,j); }
458 XForm( const XForm< Real , Dim+1 > &M ){ for( unsigned int i=0 ; i<Dim ; i++ ) for( unsigned int j=0 ; j<Dim ; j++ ) this->operator()(i,j) = M(i,j); }
459 XForm &operator = ( const Matrix< Real , Dim , Dim > &M ){ memcpy( coords , M.coords , sizeof(Real)*Dim*Dim ) ; return *this; }
460 XForm &operator = ( const XForm< Real , Dim > &M ){ memcpy( coords , M.coords , sizeof(Real)*Dim*Dim ) ; return *this; }
461 XForm &operator = ( const Matrix< Real , Dim+1 , Dim+1> &M ){ for( unsigned int i=0 ; i<Dim ; i++ ) for( unsigned int j=0 ; j<Dim ; j++ ) this->operator()(i,j) = M(i,j) ; return *this; }
462 XForm &operator = ( const XForm< Real , Dim+1 > &M ){ for( unsigned int i=0 ; i<Dim ; i++ ) for( unsigned int j=0 ; j<Dim ; j++ ) this->operator()(i,j) = M(i,j) ; return *this; }
463
464 static XForm Identity( void )
465 {
466 XForm xForm;
467 for( unsigned int d=0 ; d<Dim ; d++ ) xForm(d,d) = (Real)1.;
468 return xForm;
469 }
470 Real& operator() ( unsigned int i , unsigned int j ){ return coords[i][j]; }
471 const Real& operator() ( unsigned int i , unsigned int j ) const { return coords[i][j]; }
472 template< class _Real > Point< _Real , Dim-1 > operator * ( const Point< _Real , Dim-1 >& p ) const
473 {
474 Point< _Real , Dim-1 > q;
475 for( unsigned int i=0 ; i<Dim-1 ; i++ )
476 {
477 for( unsigned int j=0 ; j<Dim-1 ; j++ ) q[i] += (_Real)( coords[j][i] * p[j] );
478 q[i] += (_Real)coords[Dim-1][i];
479 }
480 return q;
481 }
482 template< class _Real > Point< _Real , Dim > operator * ( const Point< _Real , Dim >& p ) const
483 {
484 Point< _Real , Dim > q;
485 for( unsigned int i=0 ; i<Dim ; i++ ) for( unsigned int j=0 ; j<Dim ; j++ ) q[i] += (_Real)( coords[j][i] * p[j] );
486 return q;
487 }
488 XForm operator * ( const XForm& m ) const
489 {
490 XForm n;
491 for( unsigned int i=0 ; i<Dim ; i++ ) for( unsigned int j=0 ; j<Dim ; j++ ) for( unsigned int k=0 ; k<Dim ; k++ ) n.coords[i][j] += m.coords[i][k]*coords[k][j];
492 return n;
493 }
494 XForm transpose( void ) const
495 {
496 XForm xForm;
497 for( unsigned int i=0 ; i<Dim ; i++ ) for( unsigned int j=0 ; j<Dim ; j++ ) xForm( i , j ) = coords[j][i];
498 return xForm;
499 }
500 Real determinant( void ) const
501 {
502 Real det = (Real)0.;
503 for( unsigned int d=0 ; d<Dim ; d++ )
504 if( d&1 ) det -= coords[d][0] * subDeterminant( d , 0 );
505 else det += coords[d][0] * subDeterminant( d , 0 );
506 return det;
507 }
508 XForm inverse( void ) const
509 {
510 XForm xForm;
511 Real d = determinant();
512 for( unsigned int i=0 ; i<Dim ; i++ ) for( unsigned int j=0 ; j<Dim ; j++ )
513 if( (i+j)%2==0 ) xForm.coords[j][i] = subDeterminant( i , j ) / d;
514 else xForm.coords[j][i] = -subDeterminant( i , j ) / d;
515 return xForm;
516 }
517 Real subDeterminant( unsigned int i , unsigned int j ) const
518 {
519 XForm< Real , Dim-1 > xForm;
520 unsigned int ii[Dim-1] , jj[Dim-1];
521 for( unsigned int a=0 , _i=0 , _j=0 ; a<Dim ; a++ )
522 {
523 if( a!=i ) ii[_i++] = a;
524 if( a!=j ) jj[_j++] = a;
525 }
526 for( unsigned int _i=0 ; _i<Dim-1 ; _i++ ) for( unsigned int _j=0 ; _j<Dim-1 ; _j++ ) xForm( _i , _j ) = coords[ ii[_i] ][ jj[_j] ];
527 return xForm.determinant();
528 }
529
530 inline XForm operator - ( void ) const { XForm m ; for( unsigned int c=0 ; c<Dim ; c++ ) for( unsigned int r=0 ; r<Dim ; r++ ) m.coords[c][r] = - coords[c][r] ; return m; }
531
532 inline XForm& operator += ( const XForm& m ){ for( unsigned int c=0 ; c<Dim ; c++ ) for( unsigned int r=0 ; r<Dim ; r++ ) coords[c][r] += m.coords[c][r] ; return *this; }
533 inline XForm operator + ( const XForm& m ) const { XForm n ; for( unsigned int c=0 ; c<Dim ; c++ ) for( unsigned int r=0 ; r<Dim ; r++ ) n.coords[c][r] = coords[c][r] + m.coords[c][r] ; return n; }
534 inline XForm& operator *= ( Real s ) { for( unsigned int c=0 ; c<Dim ; c++ ) for( unsigned int r=0 ; r<Dim ; r++ ) coords[c][r] *= s ; return *this; }
535 inline XForm operator * ( Real s ) const { XForm n ; for( unsigned int c=0 ; c<Dim ; c++ ) for( unsigned int r=0 ; r<Dim ; r++ ) n.coords[c][r] = coords[c][r] * s ; return n; }
536
537 inline XForm& operator -= ( const XForm& m ){ return ( (*this)+=(-m) ); }
538 inline XForm operator - ( const XForm& m ) const { return (*this)+(-m); }
539 inline XForm& operator /= ( Real s ){ return ( (*this)*=(Real)(1./s) ); }
540 inline XForm operator / ( Real s ) const { return (*this) * ( (Real)(1./s) ); }
541
542 friend std::ostream &operator << ( std::ostream &os , const XForm &x )
543 {
544 os << "{ ";
545 for( int r=0 ; r<Dim ; r++ )
546 {
547 if( r ) os << " , ";
548 os << "{ ";
549 for( int c=0 ; c<Dim ; c++ )
550 {
551 if( c ) os << " , ";
552 os << x.coords[c][r];
553 }
554 os << " }";
555 }
556 return os << " }";
557 }
558 };
559 template<>
560 inline XForm< float , 1 > XForm< float , 1 >::inverse( void ) const
561 {
562 XForm< float , 1 > x;
563 x.coords[0][0] = (float)(1./coords[0][0] );
564 return x;
565 }
566 template<>
567 inline XForm< double , 1 > XForm< double , 1 >::inverse( void ) const
568 {
569 XForm< double , 1 > x;
570 x.coords[0][0] = (double)(1./coords[0][0] );
571 return x;
572 }
573 template<> inline float XForm< float , 1 >::determinant( void ) const { return coords[0][0]; }
574 template<> inline double XForm< double , 1 >::determinant( void ) const { return coords[0][0]; }
575
576 template< class Real , unsigned int Dim >
577 struct OrientedPoint
578 {
579 Point< Real , Dim > p , n;
580 OrientedPoint( Point< Real , Dim > pp = Point< Real , Dim >() , Point< Real , Dim > nn=Point< Real , Dim >() ) : p(pp) , n(nn) { ; }
581 template< class _Real > OrientedPoint( const OrientedPoint< _Real , Dim>& p ) : OrientedPoint( Point< Real , Dim >( p.p ) , Point< Real , Dim >( p.n ) ){ ; }
582
583 template< class _Real > inline OrientedPoint& operator += ( OrientedPoint< _Real , Dim > _p ){ p += _p.p , n += _p.n ; return *this; }
584 template< class _Real > inline OrientedPoint operator + ( OrientedPoint< _Real , Dim > _p ) const { return OrientedPoint< Real , Dim >( p+_p.p , n+_p.n ); }
585 template< class _Real > inline OrientedPoint& operator *= ( _Real r ) { p *= r , n *= r ; return *this; }
586 template< class _Real > inline OrientedPoint operator * ( _Real r ) const { return OrientedPoint< Real , Dim >( p*r , n*r ); }
587
588 template< class _Real > inline OrientedPoint& operator -= ( OrientedPoint< _Real , Dim > p ){ return ( (*this)+=(-p) ); }
589 template< class _Real > inline OrientedPoint operator - ( OrientedPoint< _Real , Dim > p ) const { return (*this)+(-p); }
590 template< class _Real > inline OrientedPoint& operator /= ( _Real r ){ return ( (*this)*=Real(1./r) ); }
591 template< class _Real > inline OrientedPoint operator / ( _Real r ) const { return (*this) * ( Real(1.)/r ); }
592 };
593
594
595 template< class Data , class Real >
596 struct ProjectiveData
597 {
598 Data data;
599 Real weight;
600 ProjectiveData( Data d=Data() , Real w=(Real)0 ) : data(d) , weight(w) { ; }
601 operator Data (){ return weight!=0 ? data/weight : data*weight; }
602 Data value( void ) const { return weight!=0 ? data/weight : data*weight; }
603 ProjectiveData& operator += ( const ProjectiveData& p ){ data += p.data , weight += p.weight ; return *this; }
604 ProjectiveData& operator -= ( const ProjectiveData& p ){ data -= p.data , weight -= p.weight ; return *this; }
605 ProjectiveData& operator *= ( Real s ){ data *= s , weight *= s ; return *this; }
606 ProjectiveData& operator /= ( Real s ){ data /= s , weight /= s ; return *this; }
607 ProjectiveData operator + ( const ProjectiveData& p ) const { return ProjectiveData( data+p.data , weight+p.weight ); }
608 ProjectiveData operator - ( const ProjectiveData& p ) const { return ProjectiveData( data-p.data , weight-p.weight ); }
609 ProjectiveData operator * ( Real s ) const { return ProjectiveData( data*s , weight*s ); }
610 ProjectiveData operator / ( Real s ) const { return ProjectiveData( data/s , weight/s ); }
611 };
612
613 template< class Real , unsigned int Dim > Point< Real , Dim > RandomBallPoint( void );
614 template< class Real , unsigned int Dim > Point< Real , Dim > RandomSpherePoint( void );
615 template< class Real , unsigned int Dim > Real Length( Point< Real , Dim > p ){ return (Real)sqrt( Point< Real , Dim >::SquareNorm( p ) ); }
616 template< class Real , unsigned int Dim > Real SquareLength( Point< Real , Dim > p ){ return Point< Real , Dim >::SquareNorm( p ); }
617 template< class Real , unsigned int Dim > Real Distance( Point< Real , Dim > p1 , Point< Real , Dim > p2 ){ return Length(p1-p2); }
618 template< class Real , unsigned int Dim > Real SquareDistance( Point< Real , Dim > p1 , Point< Real , Dim > p2 ){ return SquareLength( p1-p2 ); }
619 template< class Real > Point< Real , 3 > CrossProduct( Point< Real , 3 > p1 , Point< Real , 3 > p2 ){ return Point< Real , 3 >::CrossProduct( p1 , p2 ); }
620
621 template< class Real , unsigned int Dim > Real SquareArea( Point< Real , Dim > p1 , Point< Real , Dim > p2 , Point< Real , Dim > p3 )
622 {
623 Point< Real , Dim > v1 = p2-p1 , v2 = p3-p1;
624 // Area^2 = ( |v1|^2 * |v2|^2 * sin^2( < v1 ,v2 ) ) / 4
625 // = ( |v1|^2 * |v2|^2 * ( 1 - cos^2( < v1 ,v2 ) ) ) / 4
626 // = ( |v1|^2 * |v2|^2 * ( 1 - < v1 , v2 >^2 / ( |v1|^2 * |v2|^2 ) ) ) / 4
627 // = ( |v1|^2 * |v2|^2 - < v1 , v2 >^2 ) / 4
628 Real dot = Point< Real , Dim >::Dot( v1 , v2 );
629 Real l1 = Point< Real , Dim >::SquareNorm( v1 ) , l2 = Point< Real , Dim >::SquareNorm( v2 );
630 return ( l1 * l2 - dot * dot ) / 4;
631 }
632 template< class Real , unsigned int Dim > Real Area( Point< Real , Dim > p1 , Point< Real , Dim > p2 , Point< Real , Dim > p3 ){ return (Real)sqrt( SquareArea( p1 , p2 , p3 ) ); }
633
634 template< unsigned int K > struct Factorial{ static const unsigned long long Value = Factorial< K-1 >::Value * K; };
635 template<> struct Factorial< 0 >{ static const unsigned long long Value = 1; };
636
637 template< class Real , unsigned int Dim , unsigned int K >
638 struct Simplex
639 {
640 Point< Real , Dim > p[K+1];
641 Simplex( void ){ static_assert( K<=Dim , "[ERROR] Bad simplex dimension" ); }
642 Point< Real , Dim >& operator[]( unsigned int k ){ return p[k]; }
643 const Point< Real , Dim >& operator[]( unsigned int k ) const { return p[k]; }
644 Real measure( void ) const { return (Real)sqrt( squareMeasure() ); }
645 Real squareMeasure( void ) const
646 {
647 XForm< Real , K > mass;
648 for( unsigned int i=1 ; i<=K ; i++ ) for( unsigned int j=1 ; j<=K ; j++ ) mass(i-1,j-1) = Point< Real , Dim >::Dot( p[i]-p[0] , p[j]-p[0] );
649 return mass.determinant() / ( Factorial< K >::Value * Factorial< K >::Value );
650 }
651 Point< Real , Dim > center( void ) const
652 {
653 Point< Real , Dim > c;
654 for( unsigned int k=0 ; k<=K ; k++ ) c += p[k];
655 return c / (K+1);
656 }
657 void split( Point< Real , Dim > pNormal , Real pOffset , std::vector< Simplex >& back , std::vector< Simplex >& front ) const;
658
659 template< unsigned int _K=K >
660 typename std::enable_if< _K==Dim-1 , Point< Real , Dim > >::type normal( void ) const
661 {
662 static_assert( K==Dim-1 , "[ERROR] Co-dimension is not one" );
663 Point< Real , Dim > d[Dim-1];
664 for( int k=1 ; k<Dim ; k++ ) d[k-1] = p[k] - p[0];
665 return Point< Real , Dim >::CrossProduct( d );
666 }
667 template< unsigned int _K=K >
668 typename std::enable_if< _K==Dim-1 , bool >::type intersect( Ray< Real , Dim > ray , Real &t , Real barycentricCoordinates[Dim] ) const
669 {
670 static_assert( K==Dim-1 , "[ERROR] Co-dimension is not one" );
671 Point< Real , Dim > n = normal();
672 Real denominator = Point< Real , Dim >::Dot( n , ray.direction );
673 if( denominator==0 ) return false;
674 // Solve for t s.t. < ray(t) , n > = < p[0] , n >
675 t = Point< Real , Dim >::Dot( n , p[0] - ray.position ) / denominator;
676 Point< Real, Dim > q = ray(t);
677
678 // Let C be the matrix whose columns are the simplex vertices.
679 // Solve for the barycentric coordinates, b, minimizing:
680 // E(b) = || C * b - q ||^2
681 // = b^t * C^t * C * b - 2 * b^t * C^t * q + || q ||^2
682 // Taking the gradient with respect to b gives:
683 // b = ( C^t * C )^{-1} * C^t * q
684 Matrix< Real , Dim-1 , Dim > C;
685 for( int c=0 ; c<Dim-1 ; c++ ) for( int r=0 ; r<Dim ; r++ ) C(c,r) = p[c+1][r] - p[0][r];
686 Matrix< Real , Dim , Dim-1 > C_t = C.transpose();
687 XForm< Real , Dim-1 > M = C_t * C;
688 Point< Real , Dim-1 > bc = M.inverse() * ( C_t * ( q - p[0] ) );
689 barycentricCoordinates[0] = (Real)1.;
690 for( unsigned int d=0 ; d<Dim-1 ; d++ ) barycentricCoordinates[d+1] = bc[d] , barycentricCoordinates[0] -= bc[d];
691 for( unsigned int d=0 ; d<Dim ; d++ ) if( barycentricCoordinates[d]<0 ) return false;
692 return true;
693 }
694
695 template< unsigned int _K=K >
696 static typename std::enable_if< _K==Dim-1 , bool >::type IsInterior( Point< Real , Dim > p , const std::vector< Simplex< Real , Dim , Dim-1 > > &simplices )
697 {
698 if( !simplices.size() ) MK_THROW( "No simplices provided" );
699
700 // Create a ray that intersecting the largest simplex
701 int idx;
702 {
703 Real maxMeasure = 0;
704 for( int i=0 ; i<simplices.size() ; i++ )
705 {
706 Real measure = simplices[i].squareMeasure();
707 if( measure>maxMeasure ) maxMeasure = measure , idx = i;
708 }
709 }
710 Ray< Real , Dim > ray( p , simplices[idx].center() - p );
711 Real l = (Real)Point< Real , Dim >::SquareNorm( ray.direction );
712 if( !l ) MK_THROW( "point is on simplex" );
713 l = (Real)sqrt(l);
714 ray.direction /= l;
715
716 // Make the assessment based on which side of the simplex the point p is
717 Real min_t = l;
718 bool isInside = Point< Real , Dim >::Dot( simplices[idx].normal() , ray.direction )*min_t>0;
719
720 // Look for intersections with closer simplices
721 for( size_t i=0 ; i<simplices.size() ; i++ )
722 {
723 Real t , barycentricCoordinates[ Dim ];
724 if( simplices[i].intersect( ray , t , barycentricCoordinates ) && t<min_t ) min_t = t , isInside = Point< Real , Dim >::Dot( simplices[i].normal() , ray.direction )*t>0;
725 }
726 return isInside;
727 }
728
729 template< unsigned int _K=K >
730 static typename std::enable_if< _K==Dim-1 , bool >::type IsInterior( Point< Real , Dim > p , const std::vector< Simplex< Real , Dim , Dim-1 > > &simplices , const std::vector< Point< Real , Dim > > &normals )
731 {
732 if( !simplices.size() ) MK_THROW( "No simplices provided" );
733
734 #if 0
735 // A more conservative approach for ray-tracing, sending a ray for each simplex and using the consensus solution
736 Real insideMeasure = 0;
737 for( int idx=0 ; idx<simplices.size() ; idx++ )
738 {
739 Ray< Real , Dim > ray( p , simplices[idx].center() - p );
740 Real l = (Real)Point< Real , Dim >::SquareNorm( ray.direction );
741 if( !l ){ MK_WARN( "point is on simplex" ) ; continue; }
742 l = (Real)sqrt(l);
743 ray.direction /= l;
744
745 // Make the assessment based on which side of the simplex the point p is
746 Real min_t = l;
747 bool isInside = Point< Real , Dim >::Dot( normals[idx] , ray.direction )*min_t>0;
748
749 // Look for intersections with closer simplices
750 for( size_t i=0 ; i<simplices.size() ; i++ )
751 {
752 Real t , barycentricCoordinates[ Dim ];
753 if( simplices[i].intersect( ray , t , barycentricCoordinates ) && t<min_t ) min_t = t , isInside = Point< Real , Dim >::Dot( normals[i] , ray.direction )*t>0;
754 }
755 Real measure = simplices[idx].measure();
756 if( isInside ) insideMeasure += measure;
757 else insideMeasure -= measure;
758 }
759 return insideMeasure>0;
760 #else
761 // Create a ray that intersecting the largest simplex
762 int idx;
763 {
764 Real maxMeasure = 0;
765 for( int i=0 ; i<simplices.size() ; i++ )
766 {
767 Real measure = simplices[i].squareMeasure();
768 if( measure>maxMeasure ) maxMeasure = measure , idx = i;
769 }
770 }
771 Ray< Real , Dim > ray( p , simplices[idx].center() - p );
772 Real l = (Real)Point< Real , Dim >::SquareNorm( ray.direction );
773 if( !l ) MK_THROW( "point is on simplex" );
774 l = (Real)sqrt(l);
775 ray.direction /= l;
776
777 // Make the assessment based on which side of the simplex the point p is
778 Real min_t = l;
779 bool isInside = Point< Real , Dim >::Dot( normals[idx] , ray.direction )*min_t>0;
780
781 // Look for intersections with closer simplices
782 for( size_t i=0 ; i<simplices.size() ; i++ )
783 {
784 Real t , barycentricCoordinates[ Dim ];
785 if( simplices[i].intersect( ray , t , barycentricCoordinates ) && t<min_t ) min_t = t , isInside = Point< Real , Dim >::Dot( normals[i] , ray.direction )*t>0;
786 }
787 return isInside;
788 #endif
789 }
790 };
791
792
793 template< class Real , unsigned int Dim >
794 struct Simplex< Real , Dim , 0 >
795 {
796 static const unsigned int K=0;
797 Point< Real , Dim > p[1];
798 Point< Real , Dim >& operator[]( unsigned int k ){ return p[k]; }
799 const Point< Real , Dim >& operator[]( unsigned int k ) const { return p[k]; }
800 Real squareMeasure( void ) const { return (Real)1.; }
801 Real measure( void ) const { return (Real)1.; }
802 Point< Real , Dim > center( void ) const { return p[0]; }
803 void split( Point< Real , Dim > pNormal , Real pOffset , std::vector< Simplex >& back , std::vector< Simplex >& front ) const
804 {
805 if( Point< Real , Dim >::Dot( p[0] , pNormal ) < pOffset ) back.push_back( *this );
806 else front.push_back( *this );
807 }
808 template< unsigned int _K=K >
809 typename std::enable_if< _K==Dim-1 , bool >::type intersect( Ray< Real , Dim > ray , Real &t , Real barycentricCoordinates[Dim] ) const
810 {
811 static_assert( K==Dim-1 , "[ERROR] Co-dimension is not one" );
812 if( !ray.direction[0] ) return false;
813 // Solve for t s.t. ray(t) = p[0]
814 t = ( p[0][0] - ray.position[0] ) / ray.direction[0];
815 barycentricCoordinates[0] = (Real)1.;
816 return true;
817 }
818
819 template< unsigned int _K=0 >
820 static typename std::enable_if< _K==Dim-1 , bool >::type IsInterior( Point< Real , Dim > p , const std::vector< Simplex< Real , Dim , Dim-1 > > &simplices , const std::vector< Point< Real , Dim > > &normals )
821 {
822 if( !simplices.size() ) MK_THROW( "No simplices provided" );
823
824 Ray< Real , Dim > ray( p , simplices[0].center() - p );
825 Real l = (Real)Point< Real , Dim >::SquareNorm( ray.direction );
826 if( !l ) MK_THROW( "point is on simplex" );
827 l = (Real)sqrt(l);
828 ray.direction /= l;
829
830 // Make the assessment based on which side of the simplex the point p is
831 Real min_t = l;
832 bool isInside = Point< Real , Dim >::Dot( normals[0] , ray.direction )*min_t>0;
833
834 // Look for intersections with closer simplices
835 for( size_t i=0 ; i<simplices.size() ; i++ )
836 {
837 Real t , barycentricCoordinates[ Dim ];
838 if( simplices[i].intersect( ray , t , barycentricCoordinates ) && t<min_t ) min_t = t , isInside = Point< Real , Dim >::Dot( normals[i] , ray.direction )*t>0;
839 }
840 return isInside;
841 }
842 };
843 template< typename Real , unsigned int Dim , unsigned int K >
844 std::ostream &operator << ( std::ostream &os , const Simplex< Real , Dim , K > &s )
845 {
846 for( unsigned int k=0 ; k<K ; k++ ) os << s.p[k] << " , ";
847 return os << s.p[K];
848 }
849
850
851 template< class Real , unsigned int Dim > using Edge = Simplex< Real , Dim , 1 >;
852 template< class Real , unsigned int Dim > using Triangle = Simplex< Real , Dim , 2 >;
853
854 template< unsigned int K , typename Index >
855 struct SimplexIndex
856 {
857 Index idx[K+1];
858 template< class ... Ints >
859 SimplexIndex( Ints ... values ){ static_assert( sizeof...(values)==K+1 || sizeof...(values)==0 , "[ERROR] Invalid number of coefficients" ) ; _init( 0 , values ... ); }
860 Index &operator[] ( unsigned int i ) { return idx[i] ;}
861 const Index &operator[] ( unsigned int i ) const { return idx[i]; }
862 protected:
863 void _init( unsigned int k )
864 {
865 if( !k ) memset( idx , 0 , sizeof(idx) );
866 else MK_THROW( "Should never be called" );
867 }
868 template< class ... Ints > void _init( unsigned int k , Index v , Ints ... values )
869 {
870 idx[k] = v;
871 if( k<K ) _init( k+1 , values ... );
872 }
873 };
874 template< typename Index > using EdgeIndex = SimplexIndex< 1 , Index >;
875 template< typename Index > using TriangleIndex = SimplexIndex< 2 , Index >;
876
877 template< typename Real , unsigned int Dim , unsigned int K >
878 struct SimplicialComplex
879 {
880 SimplicialComplex( const std::vector< Simplex< Real , Dim , K > > &simplices ) : _simplices( simplices ){}
881 virtual size_t size( void ) const { return _simplices.size(); }
882 virtual Simplex< Real , Dim , K > operator[]( size_t idx ) const { return _simplices[idx]; }
883 protected:
884 SimplicialComplex( void ) :_simplices(__simplices) {}
885 const std::vector< Simplex< Real , Dim , K > > &_simplices;
886 const std::vector< Simplex< Real , Dim , K > > __simplices;
887 };
888
889 template< typename Real , unsigned int Dim , unsigned int K , typename IndexType >
890 struct IndexedSimplicialComplex : public SimplicialComplex< Real , Dim , K >
891 {
892 IndexedSimplicialComplex( const std::vector< Point< Real , Dim > > &vertices , const std::vector< SimplexIndex< K , IndexType > > &simplices ) : _vertices(vertices) , _simplices(simplices){}
893 IndexedSimplicialComplex( IndexedSimplicialComplex && isc )
894 {
895 std::swap( _vertices , isc._vertices );
896 std::swap( _simplices , isc._simplices );
897 }
898
899 size_t size( void ) const { return _simplices.size(); }
900 Simplex< Real , Dim , K > operator[]( size_t idx ) const
901 {
902 Simplex< Real , Dim , K > s;
903 for( unsigned int k=0 ; k<=K ; k++ ) s[k] = _vertices[ _simplices[idx][k] ];
904 return s;
905 }
906 protected:
907 const std::vector< Point< Real , Dim > > &_vertices;
908 const std::vector< SimplexIndex< K , IndexType > > &_simplices;
909 };
910
911
912 template< typename Index >
913 class TriangulationEdge
914 {
915 public:
916 TriangulationEdge( void ){ pIndex[0] = pIndex[1] = tIndex[0] = tIndex[1] = -1; }
917 Index pIndex[2] , tIndex[2];
918 };
919
920 template< typename Index >
921 class TriangulationTriangle
922 {
923 public:
924 TriangulationTriangle( void ){ eIndex[0] = eIndex[1] = eIndex[2] = -1; }
925 Index eIndex[3];
926 };
927
928 #include "Geometry.inl"
929 }
930
931 #endif // GEOMETRY_INCLUDED