Debian Codegraph @

src/thirdparty/PoissonRecon/Array.inl

raw
1 /*
2 Copyright (c) 2011, Michael Kazhdan and Ming Chuang
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 C > class ConstArray;
30
31 template< class C >
32 class Array
33 {
34 template< class D > friend class Array;
35 friend class ConstArray< C >;
36 void _assertBounds( std::ptrdiff_t idx ) const
37 {
38 if( idx<min || idx>=max ) PoissonRecon::MK_THROW( "Array index out-of-bounds: " , min , " <= " , idx , " < " , max );
39 }
40 protected:
41 C *data , *_data;
42 std::ptrdiff_t min , max;
43
44 public:
45 std::ptrdiff_t minimum( void ) const { return min; }
46 std::ptrdiff_t maximum( void ) const { return max; }
47
48 static inline Array New( size_t size , const char* name=NULL )
49 {
50 Array a;
51 a._data = a.data = new C[size];
52 a.min = 0;
53 #ifdef SHOW_WARNINGS
54 #pragma message( "[WARNING] Casting unsigned to signed" )
55 #endif // SHOW_WARNINGS
56 a.max = (std::ptrdiff_t)size;
57 return a;
58 }
59 static inline Array Alloc( size_t size , bool clear , const char* name=NULL )
60 {
61 Array a;
62 a._data = a.data = ( C* ) malloc( size * sizeof( C ) );
63 if( clear ) memset( a.data , 0 , size * sizeof( C ) );
64 a.min = 0;
65 #ifdef SHOW_WARNINGS
66 #pragma message( "[WARNING] Casting unsigned to signed" )
67 #endif // SHOW_WARNINGS
68 a.max = (std::ptrdiff_t)size;
69 return a;
70 }
71
72 static inline Array AlignedAlloc( size_t size , size_t alignment , bool clear , const char* name=NULL )
73 {
74 Array a;
75 a.data = ( C* ) aligned_malloc( sizeof(C) * size , alignment );
76 a._data = ( C* )( ( ( void** )a.data )[-1] );
77 if( clear ) memset( a.data , 0 , size * sizeof( C ) );
78 a.min = 0;
79 #ifdef SHOW_WARNINGS
80 #pragma message( "[WARNING] Casting unsigned to signed" )
81 #endif // SHOW_WARNINGS
82 a.max = (std::ptrdiff_t)size;
83 return a;
84 }
85
86 static inline Array ReAlloc( Array& a , size_t size , bool clear , const char* name=NULL )
87 {
88 Array _a;
89 _a._data = _a.data = ( C* ) realloc( a.data , size * sizeof( C ) );
90 if( clear ) memset( _a.data , 0 , size * sizeof( C ) );
91 a._data = NULL;
92 _a.min = 0;
93 #ifdef SHOW_WARNINGS
94 #pragma message( "[WARNING] Casting unsigned to signed" )
95 #endif // SHOW_WARNINGS
96 _a.max = (std::ptrdiff_t)size;
97 return _a;
98 }
99
100 Array( void )
101 {
102 data = _data = NULL;
103 min = max = 0;
104 }
105 template< class D >
106 Array( Array< D > a )
107 {
108 _data = NULL;
109 if( !a )
110 {
111 data = NULL;
112 min = max = 0;
113 }
114 else
115 {
116 // [WARNING] Changing szC and szD to size_t causes some really strange behavior.
117 std::ptrdiff_t szC = (std::ptrdiff_t)sizeof( C );
118 std::ptrdiff_t szD = (std::ptrdiff_t)sizeof( D );
119 data = (C*)a.data;
120 min = ( a.minimum() * szD ) / szC;
121 max = ( a.maximum() * szD ) / szC;
122 if( min*szC!=a.minimum()*szD || max*szC!=a.maximum()*szD ) PoissonRecon::MK_THROW( "Could not convert array [ " , a.minimum() , " , " , a.maximum() , " ] * " , szD , " => [ " , min , " , " , max , " ] * " , szC );
123 }
124 }
125 static Array FromPointer( C* data , std::ptrdiff_t max )
126 {
127 Array a;
128 a._data = NULL;
129 a.data = data;
130 a.min = 0;
131 a.max = max;
132 return a;
133 }
134 static Array FromPointer( C* data , std::ptrdiff_t min , std::ptrdiff_t max )
135 {
136 Array a;
137 a._data = NULL;
138 a.data = data;
139 a.min = min;
140 a.max = max;
141 return a;
142 }
143 inline bool operator == ( const Array< C >& a ) const { return data==a.data; }
144 inline bool operator != ( const Array< C >& a ) const { return data!=a.data; }
145 inline bool operator == ( const C* c ) const { return data==c; }
146 inline bool operator != ( const C* c ) const { return data!=c; }
147 inline C* operator -> ( void )
148 {
149 _assertBounds( 0 );
150 return data;
151 }
152 inline const C* operator -> ( void ) const
153 {
154 _assertBounds( 0 );
155 return data;
156 }
157 inline C &operator * ( void )
158 {
159 _assertBounds( 0 );
160 return data[0];
161 }
162 inline const C &operator * ( void ) const
163 {
164 _assertBounds( 0 );
165 return data[0];
166 }
167 template< typename Int >
168 inline C& operator[]( Int idx )
169 {
170 static_assert( std::is_integral< Int >::value , "Integral type expected" );
171 _assertBounds( idx );
172 return data[idx];
173 }
174
175 template< typename Int >
176 inline const C& operator[]( Int idx ) const
177 {
178 static_assert( std::is_integral< Int >::value , "Integral type expected" );
179 _assertBounds( idx );
180 return data[idx];
181 }
182
183 template< typename Int >
184 inline Array operator + ( Int idx ) const
185 {
186 static_assert( std::is_integral< Int >::value , "Integral type expected" );
187 Array a;
188 a._data = _data;
189 a.data = data+idx;
190 a.min = min-idx;
191 a.max = max-idx;
192 return a;
193 }
194 template< typename Int >
195 inline Array& operator += ( Int idx )
196 {
197 static_assert( std::is_integral< Int >::value , "Integral type expected" );
198 min -= idx;
199 max -= idx;
200 data += idx;
201 return (*this);
202 }
203 template< typename Int > Array operator - ( Int idx ) const { return (*this) + (-idx); }
204 template< typename Int > Array& operator -= ( Int idx ) { return (*this) += (-idx); }
205
206 inline Array& operator ++ ( void ) { return (*this) += 1; }
207 inline Array operator++( int ){ Array< C > temp = (*this) ; (*this) +=1 ; return temp; }
208 inline Array operator--( int ){ Array< C > temp = (*this) ; (*this) -=1 ; return temp; }
209 std::ptrdiff_t operator - ( const Array& a ) const { return data - a.data; }
210
211 void Free( void )
212 {
213 if( _data )
214 {
215 free( _data );
216 }
217 (*this) = Array( );
218 }
219 void Delete( void )
220 {
221 if( _data )
222 {
223 delete[] _data;
224 }
225 (*this) = Array( );
226 }
227 C* pointer( void ){ return data; }
228 const C* pointer( void ) const { return data; }
229 bool operator !( void ) const { return data==NULL; }
230 operator bool( ) const { return data!=NULL; }
231 };
232
233 template< class C >
234 class ConstArray
235 {
236 template< class D > friend class ConstArray;
237 void _assertBounds( std::ptrdiff_t idx ) const
238 {
239 if( idx<min || idx>=max ) PoissonRecon::MK_THROW( "ConstArray index out-of-bounds: " , min , " <= " , idx , " < " , max );
240 }
241 protected:
242 const C *data;
243 std::ptrdiff_t min , max;
244 public:
245 std::ptrdiff_t minimum( void ) const { return min; }
246 std::ptrdiff_t maximum( void ) const { return max; }
247
248 inline ConstArray( void )
249 {
250 data = NULL;
251 min = max = 0;
252 }
253 inline ConstArray( const Array< C >& a )
254 {
255 // [WARNING] Changing szC and szD to size_t causes some really strange behavior.
256 data = ( const C* )a.pointer( );
257 min = a.minimum();
258 max = a.maximum();
259 }
260 template< class D >
261 inline ConstArray( Array< D > a )
262 {
263 // [WARNING] Changing szC and szD to size_t causes some really strange behavior.
264 std::ptrdiff_t szC = (std::ptrdiff_t)sizeof( C );
265 std::ptrdiff_t szD = (std::ptrdiff_t)sizeof( D );
266 data = ( const C* )a.pointer( );
267 min = ( a.minimum() * szD ) / szC;
268 max = ( a.maximum() * szD ) / szC;
269 if( min*szC!=a.minimum()*szD || max*szC!=a.maximum()*szD ) PoissonRecon::MK_THROW( "Could not convert const array [ " , a.minimum() , " , " , a.maximum() , " ] * " , szD , " => [ " , min , " , " , max , " ] * " , szC );
270 }
271 template< class D >
272 inline ConstArray( ConstArray< D > a )
273 {
274 // [WARNING] Chaning szC and szD to size_t causes some really strange behavior.
275 std::ptrdiff_t szC = (std::ptrdiff_t)sizeof( C );
276 std::ptrdiff_t szD = (std::ptrdiff_t)sizeof( D );
277 data = ( const C*)a.pointer( );
278 min = ( a.minimum() * szD ) / szC;
279 max = ( a.maximum() * szD ) / szC;
280 if( min*szC!=a.minimum()*szD || max*szC!=a.maximum()*szD ) PoissonRecon::MK_THROW( "Could not convert array [ " , a.minimum() , " , " , a.maximum() , " ] * " , szD , " => [ " , min , " , " , max , " ] * " , szC );
281 }
282 explicit operator Array< C >() const
283 {
284 Array< C > a;
285 a.data = const_cast< C * >( data );
286 a.min = minimum();
287 a.max = maximum();
288 return a;
289 }
290 static ConstArray FromPointer( const C* data , std::ptrdiff_t max )
291 {
292 ConstArray a;
293 a.data = data;
294 a.min = 0;
295 a.max = max;
296 return a;
297 }
298
299 static ConstArray FromPointer( const C* data , std::ptrdiff_t min , std::ptrdiff_t max )
300 {
301 ConstArray a;
302 a.data = data;
303 a.min = min;
304 a.max = max;
305 return a;
306 }
307
308 inline bool operator == ( const ConstArray< C >& a ) const { return data==a.data; }
309 inline bool operator != ( const ConstArray< C >& a ) const { return data!=a.data; }
310 inline bool operator == ( const C* c ) const { return data==c; }
311 inline bool operator != ( const C* c ) const { return data!=c; }
312 inline const C* operator -> ( void )
313 {
314 _assertBounds( 0 );
315 return data;
316 }
317 inline const C &operator * ( void )
318 {
319 _assertBounds( 0 );
320 return data[0];
321 }
322 template< typename Int >
323 inline const C& operator[]( Int idx ) const
324 {
325 static_assert( std::is_integral< Int >::value , "Integral type expected" );
326 _assertBounds( idx );
327 return data[idx];
328 }
329 template< typename Int >
330 inline ConstArray operator + ( Int idx ) const
331 {
332 static_assert( std::is_integral< Int >::value , "Integral type expected" );
333 ConstArray a;
334 a.data = data+idx;
335 a.min = min-idx;
336 a.max = max-idx;
337 return a;
338 }
339 template< typename Int >
340 inline ConstArray& operator += ( Int idx )
341 {
342 static_assert( std::is_integral< Int >::value , "Integral type expected" );
343 min -= idx;
344 max -= idx;
345 data += idx;
346 return (*this);
347 }
348 template< typename Int > ConstArray operator - ( Int idx ) const { return (*this) + (-idx); }
349 template< typename Int > ConstArray& operator -= ( Int idx ) { return (*this) += (-idx); }
350 inline ConstArray& operator ++ ( void ) { return (*this) += 1; }
351 inline ConstArray operator++( int ){ ConstArray< C > temp = (*this) ; (*this) +=1 ; return temp; }
352 inline ConstArray operator--( int ){ ConstArray< C > temp = (*this) ; (*this) -=1 ; return temp; }
353 std::ptrdiff_t operator - ( const ConstArray& a ) const { return data - a.data; }
354 std::ptrdiff_t operator - ( const Array< C >& a ) const { return data - a.pointer(); }
355
356 const C* pointer( void ) const { return data; }
357 bool operator !( void ) const { return data==NULL; }
358 operator bool( ) const { return data!=NULL; }
359 };
360
361 template< class C >
362 Array< C > memcpy( Array< C > destination , const void* source , size_t size )
363 {
364 if( size>destination.maximum()*sizeof(C) ) PoissonRecon::MK_THROW( "Size of copy exceeds destination maximum: " , size , " > " , destination.maximum()*sizeof( C ) );
365 if( size ) memcpy( &destination[0] , source , size );
366 return destination;
367 }
368 template< class C , class D >
369 Array< C > memcpy( Array< C > destination , Array< D > source , size_t size )
370 {
371 if( size>destination.maximum()*sizeof( C ) ) PoissonRecon::MK_THROW( "Size of copy exceeds destination maximum: " , size , " > " , destination.maximum()*sizeof( C ) );
372 if( size>source.maximum()*sizeof( D ) ) PoissonRecon::MK_THROW( "Size of copy exceeds source maximum: " , size , " > " , source.maximum()*sizeof( D ) );
373 if( size ) memcpy( &destination[0] , &source[0] , size );
374 return destination;
375 }
376 template< class C , class D >
377 Array< C > memcpy( Array< C > destination , ConstArray< D > source , size_t size )
378 {
379 if( size>destination.maximum()*sizeof( C ) ) PoissonRecon::MK_THROW( "Size of copy exceeds destination maximum: " , size , " > " , destination.maximum()*sizeof( C ) );
380 if( size>source.maximum()*sizeof( D ) ) PoissonRecon::MK_THROW( "Size of copy exceeds source maximum: " , size , " > " , source.maximum()*sizeof( D ) );
381 if( size ) memcpy( &destination[0] , &source[0] , size );
382 return destination;
383 }
384 template< class D >
385 void* memcpy( void* destination , Array< D > source , size_t size )
386 {
387 if( size>source.maximum()*sizeof( D ) ) PoissonRecon::MK_THROW( "Size of copy exceeds source maximum: " , size , " > " , source.maximum()*sizeof( D ) );
388 if( size ) memcpy( destination , &source[0] , size );
389 return destination;
390 }
391 template< class D >
392 void* memcpy( void* destination , ConstArray< D > source , size_t size )
393 {
394 if( size>source.maximum()*sizeof( D ) ) PoissonRecon::MK_THROW( "Size of copy exceeds source maximum: " , size , " > " , source.maximum()*sizeof( D ) );
395 if( size ) memcpy( destination , &source[0] , size );
396 return destination;
397 }
398 template< class C >
399 Array< C > memset( Array< C > destination , int value , size_t size )
400 {
401 if( size>destination.maximum()*sizeof( C ) ) PoissonRecon::MK_THROW( "Size of set exceeds destination maximum: " , size , " > " , destination.maximum()*sizeof( C ) );
402 if( size ) memset( &destination[0] , value , size );
403 return destination;
404 }
405
406 template< class C >
407 size_t fread( Array< C > destination , size_t eSize , size_t count , FILE* fp )
408 {
409 if( count*eSize>destination.maximum()*sizeof( C ) ) PoissonRecon::MK_THROW( "Size of read exceeds source maximum: " , count*eSize , " > " , destination.maximum()*sizeof( C ) );
410 if( count ) return fread( &destination[0] , eSize , count , fp );
411 else return 0;
412 }
413 template< class C >
414 size_t fwrite( Array< C > source , size_t eSize , size_t count , FILE* fp )
415 {
416 if( count*eSize>source.maximum()*sizeof( C ) ) PoissonRecon::MK_THROW( "Size of write exceeds source maximum: " , count*eSize , " > " , source.maximum()*sizeof( C ) );
417 if( count ) return fwrite( &source[0] , eSize , count , fp );
418 else return 0;
419 }
420 template< class C >
421 size_t fwrite( ConstArray< C > source , size_t eSize , size_t count , FILE* fp )
422 {
423 if( count*eSize>source.maximum()*sizeof( C ) ) PoissonRecon::MK_THROW( "Size of write exceeds source maximum: " , count*eSize , " > " , source.maximum()*sizeof( C ) );
424 if( count ) return fwrite( &source[0] , eSize , count , fp );
425 else return 0;
426 }
427 template< class C >
428 void qsort( Array< C > base , size_t numElements , size_t elementSize , int (*compareFunction)( const void* , const void* ) )
429 {
430 if( sizeof(C)!=elementSize ) PoissonRecon::MK_THROW( "Element sizes differ: " , sizeof(C) , " != " , elementSize );
431 if( base.minimum()>0 || base.maximum()<numElements ) MK_THROW( "Array access out of bounds: " , base.minimum() , " <= 0 <= " , base.maximum() , " <= " , numElements );
432 qsort( base.pointer() , numElements , elementSize , compareFunction );
433 }