Files
PoissonRecon/Src/Array.inl
T

435 lines
14 KiB
C++
Raw Normal View History

2015-07-14 19:33:02 -04:00
/*
Copyright (c) 2011, Michael Kazhdan and Ming Chuang
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer. Redistributions in binary form must reproduce
the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
Neither the name of the Johns Hopkins University nor the names of its contributors
may be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
2018-04-14 23:00:29 -04:00
#include <string.h>
2015-07-14 19:33:02 -04:00
#include <stdio.h>
#include <emmintrin.h>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#endif // _WIN32
#include <stddef.h>
2019-03-19 19:47:43 -04:00
#include <type_traits>
#include <cstddef>
2015-07-14 19:33:02 -04:00
template< class C >
class Array
{
2018-04-14 23:00:29 -04:00
template< class D > friend class Array;
2019-03-19 19:47:43 -04:00
void _assertBounds( std::ptrdiff_t idx ) const
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
if( idx<min || idx>=max )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
StackTracer::Trace();
ERROR_OUT( "Array index out-of-bounds: " , min , " <= " , idx , " < " , max );
2015-07-14 19:33:02 -04:00
}
}
2019-03-19 19:47:43 -04:00
protected:
C *data , *_data;
std::ptrdiff_t min , max;
2015-07-14 19:33:02 -04:00
public:
2019-03-19 19:47:43 -04:00
std::ptrdiff_t minimum( void ) const { return min; }
std::ptrdiff_t maximum( void ) const { return max; }
2015-07-14 19:33:02 -04:00
static inline Array New( size_t size , const char* name=NULL )
{
Array a;
a._data = a.data = new C[size];
a.min = 0;
2018-04-14 23:00:29 -04:00
#ifdef SHOW_WARNINGS
2015-07-14 19:33:02 -04:00
#pragma message( "[WARNING] Casting unsigned to signed" )
2018-04-14 23:00:29 -04:00
#endif // SHOW_WARNINGS
2019-03-19 19:47:43 -04:00
a.max = (std::ptrdiff_t)size;
2015-07-14 19:33:02 -04:00
return a;
}
static inline Array Alloc( size_t size , bool clear , const char* name=NULL )
{
Array a;
a._data = a.data = ( C* ) malloc( size * sizeof( C ) );
if( clear ) memset( a.data , 0 , size * sizeof( C ) );
a.min = 0;
2018-04-14 23:00:29 -04:00
#ifdef SHOW_WARNINGS
2015-07-14 19:33:02 -04:00
#pragma message( "[WARNING] Casting unsigned to signed" )
2018-04-14 23:00:29 -04:00
#endif // SHOW_WARNINGS
2019-03-19 19:47:43 -04:00
a.max = (std::ptrdiff_t)size;
2015-07-14 19:33:02 -04:00
return a;
}
2019-03-19 19:47:43 -04:00
2015-07-14 19:33:02 -04:00
static inline Array AlignedAlloc( size_t size , size_t alignment , bool clear , const char* name=NULL )
{
Array a;
a.data = ( C* ) aligned_malloc( sizeof(C) * size , alignment );
a._data = ( C* )( ( ( void** )a.data )[-1] );
if( clear ) memset( a.data , 0 , size * sizeof( C ) );
a.min = 0;
2018-04-14 23:00:29 -04:00
#ifdef SHOW_WARNINGS
2015-07-14 19:33:02 -04:00
#pragma message( "[WARNING] Casting unsigned to signed" )
2018-04-14 23:00:29 -04:00
#endif // SHOW_WARNINGS
2019-03-19 19:47:43 -04:00
a.max = (std::ptrdiff_t)size;
2015-07-14 19:33:02 -04:00
return a;
}
2019-03-19 19:47:43 -04:00
2015-07-14 19:33:02 -04:00
static inline Array ReAlloc( Array& a , size_t size , bool clear , const char* name=NULL )
{
Array _a;
_a._data = _a.data = ( C* ) realloc( a.data , size * sizeof( C ) );
if( clear ) memset( _a.data , 0 , size * sizeof( C ) );
a._data = NULL;
_a.min = 0;
2018-04-14 23:00:29 -04:00
#ifdef SHOW_WARNINGS
2015-07-14 19:33:02 -04:00
#pragma message( "[WARNING] Casting unsigned to signed" )
2018-04-14 23:00:29 -04:00
#endif // SHOW_WARNINGS
2019-03-19 19:47:43 -04:00
_a.max = (std::ptrdiff_t)size;
2015-07-14 19:33:02 -04:00
return _a;
}
Array( void )
{
data = _data = NULL;
min = max = 0;
}
template< class D >
Array( Array< D >& a )
{
_data = NULL;
if( !a )
{
data = NULL;
min = max = 0;
}
else
{
2019-03-19 19:47:43 -04:00
// [WARNING] Changing szC and szD to size_t causes some really strange behavior.
std::ptrdiff_t szC = (std::ptrdiff_t)sizeof( C );
std::ptrdiff_t szD = (std::ptrdiff_t)sizeof( D );
2015-10-12 10:05:38 -04:00
data = (C*)a.data;
2015-07-14 19:33:02 -04:00
min = ( a.minimum() * szD ) / szC;
max = ( a.maximum() * szD ) / szC;
2019-03-19 19:47:43 -04:00
if( min*szC!=a.minimum()*szD || max*szC!=a.maximum()*szD ) ERROR_OUT( "Could not convert array [ " , a.minimum() , " , " , a.maximum() , " ] * " , szD , " => [ " , min , " , " , max , " ] * " , szC );
2015-07-14 19:33:02 -04:00
}
}
2019-03-19 19:47:43 -04:00
static Array FromPointer( C* data , std::ptrdiff_t max )
2015-07-14 19:33:02 -04:00
{
Array a;
a._data = NULL;
a.data = data;
a.min = 0;
a.max = max;
return a;
}
2019-03-19 19:47:43 -04:00
static Array FromPointer( C* data , std::ptrdiff_t min , std::ptrdiff_t max )
2015-07-14 19:33:02 -04:00
{
Array a;
a._data = NULL;
a.data = data;
a.min = min;
a.max = max;
return a;
}
inline bool operator == ( const Array< C >& a ) const { return data==a.data; }
inline bool operator != ( const Array< C >& a ) const { return data!=a.data; }
inline bool operator == ( const C* c ) const { return data==c; }
inline bool operator != ( const C* c ) const { return data!=c; }
inline C* operator -> ( void )
{
_assertBounds( 0 );
return data;
}
2019-03-19 19:47:43 -04:00
inline const C* operator -> ( void ) const
2015-07-14 19:33:02 -04:00
{
_assertBounds( 0 );
return data;
}
2019-03-19 19:47:43 -04:00
inline C &operator * ( void )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
_assertBounds( 0 );
return data[0];
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
inline const C &operator * ( void ) const
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
_assertBounds( 0 );
return data[0];
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
template< typename Int >
inline C& operator[]( Int idx )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
static_assert( std::is_integral< Int >::value , "Integral type expected" );
_assertBounds( idx );
return data[idx];
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
template< typename Int >
inline const C& operator[]( Int idx ) const
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
static_assert( std::is_integral< Int >::value , "Integral type expected" );
_assertBounds( idx );
return data[idx];
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
template< typename Int >
inline Array operator + ( Int idx ) const
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
static_assert( std::is_integral< Int >::value , "Integral type expected" );
2015-07-14 19:33:02 -04:00
Array a;
a._data = _data;
a.data = data+idx;
a.min = min-idx;
a.max = max-idx;
return a;
}
2019-03-19 19:47:43 -04:00
template< typename Int >
inline Array& operator += ( Int idx )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
static_assert( std::is_integral< Int >::value , "Integral type expected" );
2015-07-14 19:33:02 -04:00
min -= idx;
max -= idx;
data += idx;
return (*this);
}
2019-03-19 19:47:43 -04:00
template< typename Int > Array operator - ( Int idx ) const { return (*this) + (-idx); }
template< typename Int > Array& operator -= ( Int idx ) { return (*this) += (-idx); }
2015-07-14 19:33:02 -04:00
inline Array& operator ++ ( void ) { return (*this) += 1; }
2015-10-12 10:05:38 -04:00
inline Array operator++( int ){ Array< C > temp = (*this) ; (*this) +=1 ; return temp; }
inline Array operator--( int ){ Array< C > temp = (*this) ; (*this) -=1 ; return temp; }
2019-03-19 19:47:43 -04:00
std::ptrdiff_t operator - ( const Array& a ) const { return data - a.data; }
2015-07-14 19:33:02 -04:00
void Free( void )
{
if( _data )
{
free( _data );
}
(*this) = Array( );
}
void Delete( void )
{
if( _data )
{
delete[] _data;
}
(*this) = Array( );
}
C* pointer( void ){ return data; }
const C* pointer( void ) const { return data; }
bool operator !( void ) const { return data==NULL; }
operator bool( ) const { return data!=NULL; }
};
template< class C >
class ConstArray
{
2018-04-14 23:00:29 -04:00
template< class D > friend class ConstArray;
2019-03-19 19:47:43 -04:00
void _assertBounds( std::ptrdiff_t idx ) const
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
if( idx<min || idx>=max ) ERROR_OUT( "ConstArray index out-of-bounds: " , min , " <= " , idx , " < " , max );
2015-07-14 19:33:02 -04:00
}
protected:
const C *data;
2019-03-19 19:47:43 -04:00
std::ptrdiff_t min , max;
2015-07-14 19:33:02 -04:00
public:
2019-03-19 19:47:43 -04:00
std::ptrdiff_t minimum( void ) const { return min; }
std::ptrdiff_t maximum( void ) const { return max; }
2015-07-14 19:33:02 -04:00
inline ConstArray( void )
{
data = NULL;
min = max = 0;
}
inline ConstArray( const Array< C >& a )
{
// [WARNING] Changing szC and szD to size_t causes some really strange behavior.
data = ( const C* )a.pointer( );
min = a.minimum();
max = a.maximum();
}
template< class D >
inline ConstArray( const Array< D >& a )
{
// [WARNING] Changing szC and szD to size_t causes some really strange behavior.
2019-03-19 19:47:43 -04:00
std::ptrdiff_t szC = (std::ptrdiff_t)sizeof( C );
std::ptrdiff_t szD = (std::ptrdiff_t)sizeof( D );
2015-07-14 19:33:02 -04:00
data = ( const C* )a.pointer( );
min = ( a.minimum() * szD ) / szC;
max = ( a.maximum() * szD ) / szC;
2019-03-19 19:47:43 -04:00
if( min*szC!=a.minimum()*szD || max*szC!=a.maximum()*szD ) ERROR_OUT( "Could not convert const array [ " , a.minimum() , " , " , a.maximum() , " ] * " , szD , " => [ " , min , " , " , max , " ] * " , szC );
2015-07-14 19:33:02 -04:00
}
template< class D >
inline ConstArray( const ConstArray< D >& a )
{
// [WARNING] Chaning szC and szD to size_t causes some really strange behavior.
2019-03-19 19:47:43 -04:00
std::ptrdiff_t szC = (std::ptrdiff_t)sizeof( C );
std::ptrdiff_t szD = (std::ptrdiff_t)sizeof( D );
2015-07-14 19:33:02 -04:00
data = ( const C*)a.pointer( );
min = ( a.minimum() * szD ) / szC;
max = ( a.maximum() * szD ) / szC;
2019-03-19 19:47:43 -04:00
if( min*szC!=a.minimum()*szD || max*szC!=a.maximum()*szD ) ERROR_OUT( "Could not convert array [ " , a.minimum() , " , " , a.maximum() , " ] * " , szD , " => [ " , min , " , " , max , " ] * " , szC );
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
static ConstArray FromPointer( const C* data , std::ptrdiff_t max )
2015-07-14 19:33:02 -04:00
{
ConstArray a;
a.data = data;
a.min = 0;
a.max = max;
return a;
}
2019-03-19 19:47:43 -04:00
static ConstArray FromPointer( const C* data , std::ptrdiff_t min , std::ptrdiff_t max )
2015-07-14 19:33:02 -04:00
{
ConstArray a;
a.data = data;
a.min = min;
a.max = max;
return a;
}
inline bool operator == ( const ConstArray< C >& a ) const { return data==a.data; }
inline bool operator != ( const ConstArray< C >& a ) const { return data!=a.data; }
inline bool operator == ( const C* c ) const { return data==c; }
inline bool operator != ( const C* c ) const { return data!=c; }
inline const C* operator -> ( void )
{
_assertBounds( 0 );
return data;
}
2019-03-19 19:47:43 -04:00
inline const C &operator * ( void )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
_assertBounds( 0 );
return data[0];
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
template< typename Int >
inline const C& operator[]( Int idx ) const
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
static_assert( std::is_integral< Int >::value , "Integral type expected" );
_assertBounds( idx );
return data[idx];
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
template< typename Int >
inline ConstArray operator + ( Int idx ) const
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
static_assert( std::is_integral< Int >::value , "Integral type expected" );
2015-07-14 19:33:02 -04:00
ConstArray a;
a.data = data+idx;
a.min = min-idx;
a.max = max-idx;
return a;
}
2019-03-19 19:47:43 -04:00
template< typename Int >
inline ConstArray& operator += ( Int idx )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
static_assert( std::is_integral< Int >::value , "Integral type expected" );
2015-07-14 19:33:02 -04:00
min -= idx;
max -= idx;
data += idx;
return (*this);
}
2019-03-19 19:47:43 -04:00
template< typename Int > ConstArray operator - ( Int idx ) const { return (*this) + (-idx); }
template< typename Int > ConstArray& operator -= ( Int idx ) { return (*this) += (-idx); }
2015-07-14 19:33:02 -04:00
inline ConstArray& operator ++ ( void ) { return (*this) += 1; }
2015-10-12 10:05:38 -04:00
inline ConstArray operator++( int ){ ConstArray< C > temp = (*this) ; (*this) +=1 ; return temp; }
inline ConstArray operator--( int ){ ConstArray< C > temp = (*this) ; (*this) -=1 ; return temp; }
2019-03-19 19:47:43 -04:00
std::ptrdiff_t operator - ( const ConstArray& a ) const { return data - a.data; }
std::ptrdiff_t operator - ( const Array< C >& a ) const { return data - a.pointer(); }
2015-07-14 19:33:02 -04:00
const C* pointer( void ) const { return data; }
bool operator !( void ) { return data==NULL; }
operator bool( ) { return data!=NULL; }
};
template< class C >
Array< C > memcpy( Array< C > destination , const void* source , size_t size )
{
2019-03-19 19:47:43 -04:00
if( size>destination.maximum()*sizeof(C) ) ERROR_OUT( "Size of copy exceeds destination maximum: " , size , " > " , destination.maximum()*sizeof( C ) );
2015-07-14 19:33:02 -04:00
if( size ) memcpy( &destination[0] , source , size );
return destination;
}
template< class C , class D >
Array< C > memcpy( Array< C > destination , Array< D > source , size_t size )
{
2019-03-19 19:47:43 -04:00
if( size>destination.maximum()*sizeof( C ) ) ERROR_OUT( "Size of copy exceeds destination maximum: " , size , " > " , destination.maximum()*sizeof( C ) );
if( size>source.maximum()*sizeof( D ) ) ERROR_OUT( "Size of copy exceeds source maximum: " , size , " > " , source.maximum()*sizeof( D ) );
2015-07-14 19:33:02 -04:00
if( size ) memcpy( &destination[0] , &source[0] , size );
return destination;
}
template< class C , class D >
Array< C > memcpy( Array< C > destination , ConstArray< D > source , size_t size )
{
2019-03-19 19:47:43 -04:00
if( size>destination.maximum()*sizeof( C ) ) ERROR_OUT( "Size of copy exceeds destination maximum: " , size , " > " , destination.maximum()*sizeof( C ) );
if( size>source.maximum()*sizeof( D ) ) ERROR_OUT( "Size of copy exceeds source maximum: " , size , " > " , source.maximum()*sizeof( D ) );
2015-07-14 19:33:02 -04:00
if( size ) memcpy( &destination[0] , &source[0] , size );
return destination;
}
template< class D >
void* memcpy( void* destination , Array< D > source , size_t size )
{
2019-03-19 19:47:43 -04:00
if( size>source.maximum()*sizeof( D ) ) ERROR_OUT( "Size of copy exceeds source maximum: " , size , " > " , source.maximum()*sizeof( D ) );
2015-07-14 19:33:02 -04:00
if( size ) memcpy( destination , &source[0] , size );
return destination;
}
template< class D >
void* memcpy( void* destination , ConstArray< D > source , size_t size )
{
2019-03-19 19:47:43 -04:00
if( size>source.maximum()*sizeof( D ) ) ERROR_OUT( "Size of copy exceeds source maximum: " , size , " > " , source.maximum()*sizeof( D ) );
2015-07-14 19:33:02 -04:00
if( size ) memcpy( destination , &source[0] , size );
return destination;
}
template< class C >
Array< C > memset( Array< C > destination , int value , size_t size )
{
2019-03-19 19:47:43 -04:00
if( size>destination.maximum()*sizeof( C ) ) ERROR_OUT( "Size of set exceeds destination maximum: " , size , " > " , destination.maximum()*sizeof( C ) );
2015-07-14 19:33:02 -04:00
if( size ) memset( &destination[0] , value , size );
return destination;
}
template< class C >
size_t fread( Array< C > destination , size_t eSize , size_t count , FILE* fp )
{
2019-03-19 19:47:43 -04:00
if( count*eSize>destination.maximum()*sizeof( C ) ) ERROR_OUT( "Size of read exceeds source maximum: " , count*eSize , " > " , destination.maximum()*sizeof( C ) );
2015-07-14 19:33:02 -04:00
return fread( &destination[0] , eSize , count , fp );
}
template< class C >
size_t fwrite( Array< C > source , size_t eSize , size_t count , FILE* fp )
{
2019-03-19 19:47:43 -04:00
if( count*eSize>source.maximum()*sizeof( C ) ) ERROR_OUT( "Size of write exceeds source maximum: " , count*eSize , " > " , source.maximum()*sizeof( C ) );
2015-07-14 19:33:02 -04:00
return fwrite( &source[0] , eSize , count , fp );
}
template< class C >
size_t fwrite( ConstArray< C > source , size_t eSize , size_t count , FILE* fp )
{
2019-03-19 19:47:43 -04:00
if( count*eSize>source.maximum()*sizeof( C ) ) ERROR_OUT( "Size of write exceeds source maximum: " , count*eSize , " > " , source.maximum()*sizeof( C ) );
2015-07-14 19:33:02 -04:00
return fwrite( &source[0] , eSize , count , fp );
}
template< class C >
void qsort( Array< C > base , size_t numElements , size_t elementSize , int (*compareFunction)( const void* , const void* ) )
{
2019-03-19 19:47:43 -04:00
if( sizeof(C)!=elementSize ) ERROR_OUT( "Element sizes differ: " , sizeof(C) , " != " , elementSize );
if( base.minimum()>0 || base.maximum()<numElements ) ERROR_OUT( "Array access out of bounds: " , base.minimum() , " <= 0 <= " , base.maximum() , " <= " , numElements );
2015-07-14 19:33:02 -04:00
qsort( base.pointer() , numElements , elementSize , compareFunction );
}