1 2//////////////////////////// 3// PLYOrientedPointStream // 4//////////////////////////// 5template< class Real > 6PLYOrientedPointStream< Real >::PLYOrientedPointStream( const char* fileName ) 7{ 8 _fileName = new char[ strlen( fileName )+1 ]; 9 strcpy( _fileName , fileName ); 10 _ply = NULL; 11 reset(); 12} 13template< class Real > 14void PLYOrientedPointStream< Real >::reset( void ) 15{ 16 int fileType; 17 float version; 18 PlyProperty** plist; 19 if( _ply ) _free(); 20 _ply = ply_open_for_reading( _fileName, &_nr_elems, &_elist, &fileType, &version ); 21 if( !_ply ) 22 { 23 fprintf( stderr, "[ERROR] Failed to open ply file for reading: %s\n" , _fileName ); 24 exit( 0 ); 25 } 26 bool foundVertices = false; 27 for( int i=0 ; i<_nr_elems ; i++ ) 28 { 29 int num_elems; 30 int nr_props; 31 char* elem_name = _elist[i]; 32 plist = ply_get_element_description( _ply , elem_name , &num_elems , &nr_props ); 33 if( !plist ) 34 { 35 fprintf( stderr , "[ERROR] Failed to get element description: %s\n" , elem_name ); 36 exit( 0 ); 37 } 38 39 if( equal_strings( "vertex" , elem_name ) ) 40 { 41 foundVertices = true; 42 _pCount = num_elems , _pIdx = 0; 43 for( int i=0 ; i<PlyOrientedVertex< Real >::ReadComponents ; i++ ) 44 if( !ply_get_property( _ply , elem_name , &(PlyOrientedVertex< Real >::ReadProperties[i]) ) ) 45 { 46 fprintf( stderr , "[ERROR] Failed to find property in ply file: %s\n" , PlyOrientedVertex< Real >::ReadProperties[i].name ); 47 exit( 0 ); 48 } 49 } 50 for( int j=0 ; j<nr_props ; j++ ) 51 { 52 free( plist[j] ); 53 } 54 free( plist ); 55 if( foundVertices ) break; 56 } 57 if( !foundVertices ) 58 { 59 fprintf( stderr , "[ERROR] Could not find vertices in ply file\n" ); 60 exit( 0 ); 61 } 62} 63template< class Real > 64void PLYOrientedPointStream< Real >::_free( void ) 65{ 66 if( _ply ) ply_close( _ply ) , _ply = NULL; 67 if( _elist ) 68 { 69 for( int i=0 ; i<_nr_elems ; i++ ) free( _elist[i] ); 70 free( _elist ); 71 } 72} 73template< class Real > 74PLYOrientedPointStream< Real >::~PLYOrientedPointStream( void ) 75{ 76 _free(); 77 if( _fileName ) delete[] _fileName , _fileName = NULL; 78} 79template< class Real > 80bool PLYOrientedPointStream< Real >::nextPoint( OrientedPoint3D< Real >& p ) 81{ 82 if( _pIdx<_pCount ) 83 { 84 PlyOrientedVertex< Real > op; 85 ply_get_element( _ply, (void *)&op ); 86 p.p = op.point; 87 p.n = op.normal; 88 _pIdx++; 89 return true; 90 } 91 else return false; 92} 93 94 95//////////////////////////////////// 96// PLYOrientedPointStreamWithData // 97//////////////////////////////////// 98 99template< class Real , class Data > 100PLYOrientedPointStreamWithData< Real , Data >::PLYOrientedPointStreamWithData( const char* fileName , const PlyProperty* dataProperties , int dataPropertiesCount , bool (*validationFunction)( const bool* ) ) : _dataPropertiesCount( dataPropertiesCount ) , _validationFunction( validationFunction ) 101{ 102 _dataProperties = new PlyProperty[ _dataPropertiesCount ]; 103 memcpy( _dataProperties , dataProperties , sizeof(PlyProperty) * _dataPropertiesCount ); 104 for( int i=0 ; i<_dataPropertiesCount ; i++ ) _dataProperties[i].offset += sizeof( PlyOrientedVertex< Real > ); 105 _fileName = new char[ strlen( fileName )+1 ]; 106 strcpy( _fileName , fileName ); 107 _ply = NULL; 108 reset(); 109} 110template< class Real , class Data > 111void PLYOrientedPointStreamWithData< Real , Data >::reset( void ) 112{ 113 int fileType; 114 float version; 115 PlyProperty** plist; 116 if( _ply ) _free(); 117 _ply = ply_open_for_reading( _fileName, &_nr_elems, &_elist, &fileType, &version ); 118 if( !_ply ) 119 { 120 fprintf( stderr, "[ERROR] Failed to open ply file for reading: %s\n" , _fileName ); 121 exit( 0 ); 122 } 123 bool foundVertices = false; 124 for( int i=0 ; i<_nr_elems ; i++ ) 125 { 126 int num_elems; 127 int nr_props; 128 char* elem_name = _elist[i]; 129 plist = ply_get_element_description( _ply , elem_name , &num_elems , &nr_props ); 130 if( !plist ) 131 { 132 fprintf( stderr , "[ERROR] Failed to get element description: %s\n" , elem_name ); 133 exit( 0 ); 134 } 135 136 if( equal_strings( "vertex" , elem_name ) ) 137 { 138 foundVertices = true; 139 _pCount = num_elems , _pIdx = 0; 140 for( int i=0 ; i<PlyOrientedVertex< Real >::ReadComponents ; i++ ) 141 if( !ply_get_property( _ply , elem_name , &(PlyOrientedVertex< Real >::ReadProperties[i]) ) ) 142 { 143 fprintf( stderr , "[ERROR] Failed to find property in ply file: %s\n" , PlyOrientedVertex< Real >::ReadProperties[i].name ); 144 exit( 0 ); 145 } 146 if( _validationFunction ) 147 { 148 bool* properties = new bool[_dataPropertiesCount]; 149 for( int i=0 ; i<_dataPropertiesCount ; i++ ) 150 if( !ply_get_property( _ply , elem_name , &(_dataProperties[i]) ) ) properties[i] = false; 151 else properties[i] = true; 152 bool valid = _validationFunction( properties ); 153 delete[] properties; 154 if( !valid ) fprintf( stderr , "[ERROR] Failed to validate properties in file\n" ) , exit( 0 ); 155 } 156 else 157 { 158 for( int i=0 ; i<_dataPropertiesCount ; i++ ) 159 if( !ply_get_property( _ply , elem_name , &(_dataProperties[i]) ) ) 160 fprintf( stderr , "[WARNING] Failed to find property in ply file: %s\n" , _dataProperties[i].name ); 161 } 162 } 163 for( int j=0 ; j<nr_props ; j++ ) 164 { 165 free( plist[j] ); 166 } 167 free( plist ); 168 if( foundVertices ) break; 169 } 170 if( !foundVertices ) 171 { 172 fprintf( stderr , "[ERROR] Could not find vertices in ply file\n" ); 173 exit( 0 ); 174 } 175} 176template< class Real , class Data > 177void PLYOrientedPointStreamWithData< Real , Data >::_free( void ) 178{ 179 if( _ply ) ply_close( _ply ) , _ply = NULL; 180 if( _elist ) 181 { 182 for( int i=0 ; i<_nr_elems ; i++ ) free( _elist[i] ); 183 free( _elist ); 184 } 185} 186template< class Real , class Data > 187PLYOrientedPointStreamWithData< Real , Data >::~PLYOrientedPointStreamWithData( void ) 188{ 189 _free(); 190 if( _fileName ) delete[] _fileName , _fileName = NULL; 191 if( _dataProperties ) delete[] _dataProperties , _dataProperties = NULL; 192} 193template< class Real , class Data > 194bool PLYOrientedPointStreamWithData< Real , Data >::nextPoint( OrientedPoint3D< Real >& p , Data& d ) 195{ 196 if( _pIdx<_pCount ) 197 { 198 _PlyOrientedVertexWithData op; 199 ply_get_element( _ply, (void *)&op ); 200 p.p = op.point; 201 p.n = op.normal; 202 d = op.data; 203 _pIdx++; 204 return true; 205 } 206 else return false; 207} 208