Resolved bug that would cause a bad memory access when the octree had empty slices.

This commit is contained in:
mkazhdan
2019-02-06 21:38:31 -05:00
parent 3107d150fb
commit 7a3db196e6
7 changed files with 179 additions and 135 deletions
+41 -43
View File
@@ -91,61 +91,59 @@ void ShowUsage( char* ex )
}
template< typename Real , unsigned int Dim >
bool WriteImage( const Real *values , int res , const char *fileName , bool verbose )
void WriteGrid( ConstPointer( Real ) values , int res , const char *fileName )
{
if( Dim!=2 ) return false;
int resolution = 1;
for( int d=0 ; d<Dim ; d++ ) resolution *= res;
Real avg = 0;
#pragma omp parallel for reduction( + : avg )
for( int i=0 ; i<resolution ; i++ ) avg += values[i];
avg /= (Real)resolution;
char *ext = GetFileExtension( fileName );
Real std = 0;
#pragma omp parallel for reduction( + : std )
for( int i=0 ; i<resolution ; i++ ) std += ( values[i] - avg ) * ( values[i] - avg );
std = (Real)sqrt( std / resolution );
if( verbose ) printf( "Grid to image: [%.2f,%.2f] -> [0,255]\n" , avg - 2*std , avg + 2*std );
unsigned char *pixels = new unsigned char[ resolution*3 ];
#pragma omp parallel for
for( int i=0 ; i<resolution ; i++ )
if( Dim==2 && ImageWriter::ValidExtension( ext ) )
{
Real v = (Real)std::min< Real >( (Real)1. , std::max< Real >( (Real)-1. , ( values[i] - avg ) / (2*std ) ) );
v = (Real)( ( v + 1. ) / 2. * 256. );
unsigned char color = (unsigned char )std::min< Real >( (Real)255. , std::max< Real >( (Real)0. , v ) );
for( int c=0 ; c<3 ; c++ ) pixels[i*3+c ] = color;
Real avg = 0;
#pragma omp parallel for reduction( + : avg )
for( int i=0 ; i<resolution ; i++ ) avg += values[i];
avg /= (Real)resolution;
Real std = 0;
#pragma omp parallel for reduction( + : std )
for( int i=0 ; i<resolution ; i++ ) std += ( values[i] - avg ) * ( values[i] - avg );
std = (Real)sqrt( std / resolution );
if( Verbose.set ) printf( "Grid to image: [%.2f,%.2f] -> [0,255]\n" , avg - 2*std , avg + 2*std );
unsigned char *pixels = new unsigned char[ resolution*3 ];
#pragma omp parallel for
for( int i=0 ; i<resolution ; i++ )
{
Real v = (Real)std::min< Real >( (Real)1. , std::max< Real >( (Real)-1. , ( values[i] - avg ) / (2*std ) ) );
v = (Real)( ( v + 1. ) / 2. * 256. );
unsigned char color = (unsigned char )std::min< Real >( (Real)255. , std::max< Real >( (Real)0. , v ) );
for( int c=0 ; c<3 ; c++ ) pixels[i*3+c ] = color;
}
ImageWriter::Write( fileName , pixels , res , res , 3 );
delete[] pixels;
}
bool success = true;
try{ ImageWriter::Write( fileName , pixels , res , res , 3 ); }
catch( MKExceptions::Exception & ){ success = false; }
delete[] pixels;
return success;
}
template< typename Real , unsigned int Dim >
void WriteGrid( const Real *values , int res , const char *fileName )
{
int resolution = 1;
for( int d=0 ; d<Dim ; d++ ) resolution *= res;
FILE *fp = fopen( fileName , "wb" );
if( !fp ) ERROR_OUT( "Failed to open grid file for writing: %s" , fileName );
else
{
fwrite( &res , sizeof(int) , 1 , fp );
if( typeid(Real)==typeid(float) ) fwrite( values , sizeof(float) , resolution , fp );
FILE *fp = fopen( fileName , "wb" );
if( !fp ) ERROR_OUT( "Failed to open grid file for writing: %s" , fileName );
else
{
float *fValues = new float[resolution];
for( int i=0 ; i<resolution ; i++ ) fValues[i] = float( values[i] );
fwrite( fValues , sizeof(float) , resolution , fp );
delete[] fValues;
fwrite( &res , sizeof(int) , 1 , fp );
if( typeid(Real)==typeid(float) ) fwrite( values , sizeof(float) , resolution , fp );
else
{
float *fValues = new float[resolution];
for( int i=0 ; i<resolution ; i++ ) fValues[i] = float( values[i] );
fwrite( fValues , sizeof(float) , resolution , fp );
delete[] fValues;
}
fclose( fp );
}
fclose( fp );
}
delete[] ext;
}
template< unsigned int Dim , class Real , unsigned int FEMSig >
@@ -163,7 +161,7 @@ void _Execute( const FEMTree< Dim , Real >* tree , FILE* fp )
double t = Time();
Pointer( Real ) values = tree->template regularGridEvaluate< true >( coefficients , res , -1 , PrimalGrid.set );
if( Verbose.set ) printf( "Got grid: %.2f(s)\n" , Time()-t );
if( !WriteImage< Real , Dim >( values , res , OutGrid.value , Verbose.set ) ) WriteGrid< Real , Dim >( values , res , OutGrid.value );
WriteGrid< Real , Dim >( values , res , OutGrid.value );
DeletePointer( values );
}
+1 -1
View File
@@ -42,7 +42,7 @@ DAMAGE.
#ifndef FEM_TREE_INCLUDED
#define FEM_TREE_INCLUDED
#define VERSION "10.06"
#define VERSION "10.07"
#define MEMORY_ALLOCATOR_BLOCK_SIZE 1<<12
#define NEW_CODE
+36
View File
@@ -37,6 +37,7 @@ struct ImageReader
return pixels;
}
static bool ValidExtension( const char *ext );
static ImageReader* Get( const char* fileName );
static void GetInfo( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels );
virtual ~ImageReader( void ){ }
@@ -70,6 +71,8 @@ struct ImageWriter
for( unsigned int j=0 ; j<height ; j++ ) writer->nextRow( pixels + j*width*channels );
delete writer;
}
static bool ValidExtension( const char *ext );
static ImageWriter* Get( const char* fileName , unsigned int width , unsigned int height , unsigned int channels , ImageWriterParams params=ImageWriterParams() );
virtual ~ImageWriter( void ){ }
unsigned int width( void ) const { return _width; }
@@ -160,6 +163,20 @@ protected:
}
};
inline bool ImageReader::ValidExtension( const char *ext )
{
#ifdef WIN32
if ( !_stricmp( ext , "jpeg" ) || !_stricmp( ext , "jpg" ) ) return true;
else if( !_stricmp( ext , "png" ) ) return true;
else if( !_stricmp( ext , "iGrid" ) ) return true;
#else // !WIN32
if( !strcasecmp( ext , "jpeg" ) || !strcasecmp( ext , "jpg" ) ) return true;
else if( !strcasecmp( ext , "png" ) ) return true;
else if( !strcasecmp( ext , "iGrid" ) ) return true;
#endif // WIN32
return false;
}
inline ImageReader* ImageReader::Get( const char* fileName )
{
unsigned int width , height , channels;
@@ -200,6 +217,25 @@ inline void ImageReader::GetInfo( const char* fileName , unsigned int& width , u
#endif // WIN32
delete[] ext;
}
inline bool ImageWriter::ValidExtension( const char *ext )
{
#ifdef WIN32
if( !_stricmp( ext , "jpeg" ) || !_stricmp( ext , "jpg" ) ) return true;
else if( !_stricmp( ext , "png" ) ) return true;
#ifdef SUPPORT_TILES
else if( !_stricmp( ext , "iGrid" ) ) return true;
#endif // SUPPORT_TILES
#else // !WIN32
if( !strcasecmp( ext , "jpeg" ) || !strcasecmp( ext , "jpg" ) ) return true;
else if( !strcasecmp( ext , "png" ) ) return true;
#ifdef SUPPORT_TILES
else if( !strcasecmp( ext , "iGrid" ) ) return true;
#endif // SUPPORT_TILES
#endif // WIN32
return false;
}
inline ImageWriter* ImageWriter::Get( const char* fileName , unsigned int width , unsigned int height , unsigned int channels , ImageWriterParams params )
{
ImageWriter* writer = NULL;
+42 -43
View File
@@ -353,63 +353,62 @@ void ExtractMesh( UIntPack< FEMSigs ... > , std::tuple< SampleData ... > , FEMTr
}
template< typename Real , unsigned int Dim >
bool WriteImage( const Real *values , int res , const char *fileName , bool verbose )
void WriteGrid( ConstPointer( Real ) values , int res , const char *fileName )
{
if( Dim!=2 ) return false;
int resolution = 1;
for( int d=0 ; d<Dim ; d++ ) resolution *= res;
Real avg = 0;
#pragma omp parallel for reduction( + : avg )
for( int i=0 ; i<resolution ; i++ ) avg += values[i];
avg /= (Real)resolution;
char *ext = GetFileExtension( fileName );
Real std = 0;
#pragma omp parallel for reduction( + : std )
for( int i=0 ; i<resolution ; i++ ) std += ( values[i] - avg ) * ( values[i] - avg );
std = (Real)sqrt( std / resolution );
if( verbose ) printf( "Grid to image: [%.2f,%.2f] -> [0,255]\n" , avg - 2*std , avg + 2*std );
unsigned char *pixels = new unsigned char[ resolution*3 ];
#pragma omp parallel for
for( int i=0 ; i<resolution ; i++ )
if( Dim==2 && ImageWriter::ValidExtension( ext ) )
{
Real v = (Real)std::min< Real >( (Real)1. , std::max< Real >( (Real)-1. , ( values[i] - avg ) / (2*std ) ) );
v = (Real)( ( v + 1. ) / 2. * 256. );
unsigned char color = (unsigned char )std::min< Real >( (Real)255. , std::max< Real >( (Real)0. , v ) );
for( int c=0 ; c<3 ; c++ ) pixels[i*3+c ] = color;
Real avg = 0;
#pragma omp parallel for reduction( + : avg )
for( int i=0 ; i<resolution ; i++ ) avg += values[i];
avg /= (Real)resolution;
Real std = 0;
#pragma omp parallel for reduction( + : std )
for( int i=0 ; i<resolution ; i++ ) std += ( values[i] - avg ) * ( values[i] - avg );
std = (Real)sqrt( std / resolution );
if( Verbose.set ) printf( "Grid to image: [%.2f,%.2f] -> [0,255]\n" , avg - 2*std , avg + 2*std );
unsigned char *pixels = new unsigned char[ resolution*3 ];
#pragma omp parallel for
for( int i=0 ; i<resolution ; i++ )
{
Real v = (Real)std::min< Real >( (Real)1. , std::max< Real >( (Real)-1. , ( values[i] - avg ) / (2*std ) ) );
v = (Real)( ( v + 1. ) / 2. * 256. );
unsigned char color = (unsigned char )std::min< Real >( (Real)255. , std::max< Real >( (Real)0. , v ) );
for( int c=0 ; c<3 ; c++ ) pixels[i*3+c ] = color;
}
ImageWriter::Write( fileName , pixels , res , res , 3 );
delete[] pixels;
}
bool success = true;
try{ ImageWriter::Write( fileName , pixels , res , res , 3 ); }
catch( MKExceptions::Exception & ){ success = false; }
delete[] pixels;
return success;
}
template< typename Real , unsigned int Dim >
void WriteGrid( const Real *values , int res , const char *fileName )
{
int resolution = 1;
for( int d=0 ; d<Dim ; d++ ) resolution *= res;
FILE *fp = fopen( fileName , "wb" );
if( !fp ) ERROR_OUT( "Failed to open grid file for writing: %s" , fileName );
else
{
fwrite( &res , sizeof(int) , 1 , fp );
if( typeid(Real)==typeid(float) ) fwrite( values , sizeof(float) , resolution , fp );
FILE *fp = fopen( fileName , "wb" );
if( !fp ) ERROR_OUT( "Failed to open grid file for writing: %s" , fileName );
else
{
float *fValues = new float[resolution];
for( int i=0 ; i<resolution ; i++ ) fValues[i] = float( values[i] );
fwrite( fValues , sizeof(float) , resolution , fp );
delete[] fValues;
fwrite( &res , sizeof(int) , 1 , fp );
if( typeid(Real)==typeid(float) ) fwrite( values , sizeof(float) , resolution , fp );
else
{
float *fValues = new float[resolution];
for( int i=0 ; i<resolution ; i++ ) fValues[i] = float( values[i] );
fwrite( fValues , sizeof(float) , resolution , fp );
delete[] fValues;
}
fclose( fp );
}
fclose( fp );
}
delete[] ext;
}
template< class Real , typename ... SampleData , unsigned int ... FEMSigs >
void Execute( int argc , char* argv[] , UIntPack< FEMSigs ... > )
{
@@ -669,7 +668,7 @@ void Execute( int argc , char* argv[] , UIntPack< FEMSigs ... > )
#pragma omp parallel for
for( int i=0 ; i<resolution ; i++ ) values[i] -= isoValue;
profiler.dumpOutput( "Got grid:" );
if( !WriteImage< Real , DIMENSION >( values , res , Grid.value , Verbose.set ) ) WriteGrid< Real , DIMENSION >( values , res , Grid.value );
WriteGrid< Real , DIMENSION >( values , res , Grid.value );
DeletePointer( values );
if( Verbose.set )
{
+42 -43
View File
@@ -377,63 +377,62 @@ void ExtractMesh( UIntPack< FEMSigs ... > , std::tuple< SampleData ... > , FEMTr
}
template< typename Real , unsigned int Dim >
bool WriteImage( const Real *values , int res , const char *fileName , bool verbose )
void WriteGrid( ConstPointer( Real ) values , int res , const char *fileName )
{
if( Dim!=2 ) return false;
int resolution = 1;
for( int d=0 ; d<Dim ; d++ ) resolution *= res;
Real avg = 0;
#pragma omp parallel for reduction( + : avg )
for( int i=0 ; i<resolution ; i++ ) avg += values[i];
avg /= (Real)resolution;
char *ext = GetFileExtension( fileName );
Real std = 0;
#pragma omp parallel for reduction( + : std )
for( int i=0 ; i<resolution ; i++ ) std += ( values[i] - avg ) * ( values[i] - avg );
std = (Real)sqrt( std / resolution );
if( verbose ) printf( "Grid to image: [%.2f,%.2f] -> [0,255]\n" , avg - 2*std , avg + 2*std );
unsigned char *pixels = new unsigned char[ resolution*3 ];
#pragma omp parallel for
for( int i=0 ; i<resolution ; i++ )
if( Dim==2 && ImageWriter::ValidExtension( ext ) )
{
Real v = (Real)std::min< Real >( (Real)1. , std::max< Real >( (Real)-1. , ( values[i] - avg ) / (2*std ) ) );
v = (Real)( ( v + 1. ) / 2. * 256. );
unsigned char color = (unsigned char )std::min< Real >( (Real)255. , std::max< Real >( (Real)0. , v ) );
for( int c=0 ; c<3 ; c++ ) pixels[i*3+c ] = color;
Real avg = 0;
#pragma omp parallel for reduction( + : avg )
for( int i=0 ; i<resolution ; i++ ) avg += values[i];
avg /= (Real)resolution;
Real std = 0;
#pragma omp parallel for reduction( + : std )
for( int i=0 ; i<resolution ; i++ ) std += ( values[i] - avg ) * ( values[i] - avg );
std = (Real)sqrt( std / resolution );
if( Verbose.set ) printf( "Grid to image: [%.2f,%.2f] -> [0,255]\n" , avg - 2*std , avg + 2*std );
unsigned char *pixels = new unsigned char[ resolution*3 ];
#pragma omp parallel for
for( int i=0 ; i<resolution ; i++ )
{
Real v = (Real)std::min< Real >( (Real)1. , std::max< Real >( (Real)-1. , ( values[i] - avg ) / (2*std ) ) );
v = (Real)( ( v + 1. ) / 2. * 256. );
unsigned char color = (unsigned char )std::min< Real >( (Real)255. , std::max< Real >( (Real)0. , v ) );
for( int c=0 ; c<3 ; c++ ) pixels[i*3+c ] = color;
}
ImageWriter::Write( fileName , pixels , res , res , 3 );
delete[] pixels;
}
bool success = true;
try{ ImageWriter::Write( fileName , pixels , res , res , 3 ); }
catch( MKExceptions::Exception & ){ success = false; }
delete[] pixels;
return success;
}
template< typename Real , unsigned int Dim >
void WriteGrid( const Real *values , int res , const char *fileName )
{
int resolution = 1;
for( int d=0 ; d<Dim ; d++ ) resolution *= res;
FILE *fp = fopen( fileName , "wb" );
if( !fp ) ERROR_OUT( "Failed to open grid file for writing: %s" , fileName );
else
{
fwrite( &res , sizeof(int) , 1 , fp );
if( typeid(Real)==typeid(float) ) fwrite( values , sizeof(float) , resolution , fp );
FILE *fp = fopen( fileName , "wb" );
if( !fp ) ERROR_OUT( "Failed to open grid file for writing: %s" , fileName );
else
{
float *fValues = new float[resolution];
for( int i=0 ; i<resolution ; i++ ) fValues[i] = float( values[i] );
fwrite( fValues , sizeof(float) , resolution , fp );
delete[] fValues;
fwrite( &res , sizeof(int) , 1 , fp );
if( typeid(Real)==typeid(float) ) fwrite( values , sizeof(float) , resolution , fp );
else
{
float *fValues = new float[resolution];
for( int i=0 ; i<resolution ; i++ ) fValues[i] = float( values[i] );
fwrite( fValues , sizeof(float) , resolution , fp );
delete[] fValues;
}
fclose( fp );
}
fclose( fp );
}
delete[] ext;
}
template< class Real , typename ... SampleData , unsigned int ... FEMSigs >
void Execute( int argc , char* argv[] , UIntPack< FEMSigs ... > )
{
@@ -674,7 +673,7 @@ void Execute( int argc , char* argv[] , UIntPack< FEMSigs ... > )
#pragma omp parallel for
for( int i=0 ; i<resolution ; i++ ) values[i] -= isoValue;
profiler.dumpOutput( "Got grid:" );
if( !WriteImage< Real , DIMENSION >( values , res , Grid.value , Verbose.set ) ) WriteGrid< Real , DIMENSION >( values , res , Grid.value );
WriteGrid< Real , DIMENSION >( values , res , Grid.value );
DeletePointer( values );
if( Verbose.set )
{
+7 -2
View File
@@ -598,13 +598,18 @@ template< class T , class IndexType , size_t MaxRowSize >
template< class T2 >
void SparseMatrix< T , IndexType , MaxRowSize >::operator() ( const T2* in , T2* out ) const { Interface::multiply( in , out ); }
template< class T , class IndexType , size_t MaxRowSize > SparseMatrix< T , IndexType , MaxRowSize >::~SparseMatrix( void ) { resize( 0 ); }
template< class T , class IndexType , size_t MaxRowSize >
SparseMatrix< T , IndexType , MaxRowSize >::~SparseMatrix( void )
{
FreePointer( _rowSizes );
FreePointer( _entries );
}
template< class T , class IndexType , size_t MaxRowSize >
void SparseMatrix< T , IndexType , MaxRowSize >::resize( size_t rowNum )
{
_rowNum = rowNum;
if( !rowNum || rowNum>_maxRows )
if( rowNum>_maxRows )
{
FreePointer( _rowSizes );
FreePointer( _entries );