Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Geometry.cpp
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 #include "Geometry.h"
29 #include <stdio.h>
30 #include <string.h>
31 
33 // CoredMeshData //
35 
36 TriangulationEdge::TriangulationEdge(void){pIndex[0]=pIndex[1]=tIndex[0]=tIndex[1]=-1;}
37 TriangulationTriangle::TriangulationTriangle(void){eIndex[0]=eIndex[1]=eIndex[2]=-1;}
38 
40 // CoredVectorMeshData //
42 CoredVectorMeshData::CoredVectorMeshData( void ) { oocPointIndex = polygonIndex = 0; }
43 void CoredVectorMeshData::resetIterator ( void ) { oocPointIndex = polygonIndex = 0; }
44 int CoredVectorMeshData::addOutOfCorePoint(const Point3D<float>& p){
45  oocPoints.push_back(p);
46  return int(oocPoints.size())-1;
47 }
48 int CoredVectorMeshData::addPolygon( const std::vector< CoredVertexIndex >& vertices )
49 {
50  std::vector< int > polygon( vertices.size() );
51  for( int i=0 ; i<int(vertices.size()) ; i++ )
52  if( vertices[i].inCore ) polygon[i] = vertices[i].idx;
53  else polygon[i] = -vertices[i].idx-1;
54  polygons.push_back( polygon );
55  return int( polygons.size() )-1;
56 }
57 int CoredVectorMeshData::nextOutOfCorePoint(Point3D<float>& p){
58  if(oocPointIndex<int(oocPoints.size())){
59  p=oocPoints[oocPointIndex++];
60  return 1;
61  }
62  else{return 0;}
63 }
64 int CoredVectorMeshData::nextPolygon( std::vector< CoredVertexIndex >& vertices )
65 {
66  if( polygonIndex<int( polygons.size() ) )
67  {
68  std::vector< int >& polygon = polygons[ polygonIndex++ ];
69  vertices.resize( polygon.size() );
70  for( int i=0 ; i<int(polygon.size()) ; i++ )
71  if( polygon[i]<0 ) vertices[i].idx = -polygon[i]-1 , vertices[i].inCore = false;
72  else vertices[i].idx = polygon[i] , vertices[i].inCore = true;
73  return 1;
74  }
75  else return 0;
76 }
77 int CoredVectorMeshData::outOfCorePointCount(void){return int(oocPoints.size());}
78 int CoredVectorMeshData::polygonCount( void ) { return int( polygons.size() ); }
79 
81 // BufferedReadWriteFile //
83 BufferedReadWriteFile::BufferedReadWriteFile( char* fileName , int bufferSize )
84 {
85  _bufferIndex = 0;
86  _bufferSize = bufferSize;
87  if( fileName ) strcpy( _fileName , fileName ) , tempFile = false;
88 #ifdef _WIN32
89  else strcpy( _fileName , _tempnam( "." , "foo" ) ) , tempFile = true;
90 #else // !_WIN32
91  else strcpy( _fileName , tempnam( "." , "foo" ) ) , tempFile = true;
92 #endif // _WIN32
93  _fp = fopen( _fileName , "w+b" );
94  if( !_fp ) fprintf( stderr , "[ERROR] Failed to open file: %s\n" , _fileName ) , exit( 0 );
95  _buffer = (char*) malloc( _bufferSize );
96 }
97 BufferedReadWriteFile::~BufferedReadWriteFile( void )
98 {
99  free( _buffer );
100  fclose( _fp );
101  if( tempFile ) remove( _fileName );
102 }
103 void BufferedReadWriteFile::reset( void )
104 {
105  if( _bufferIndex ) fwrite( _buffer , 1 , _bufferIndex , _fp );
106  _bufferIndex = 0;
107  fseek( _fp , 0 , SEEK_SET );
108  _bufferIndex = 0;
109  _bufferSize = fread( _buffer , 1 , _bufferSize , _fp );
110 }
111 bool BufferedReadWriteFile::write( const void* data , size_t size )
112 {
113  if( !size ) return true;
114  char* _data = (char*) data;
115  size_t sz = _bufferSize - _bufferIndex;
116  while( sz<=size )
117  {
118  memcpy( _buffer+_bufferIndex , _data , sz );
119  fwrite( _buffer , 1 , _bufferSize , _fp );
120  _data += sz;
121  size -= sz;
122  _bufferIndex = 0;
123  sz = _bufferSize;
124  }
125  if( size )
126  {
127  memcpy( _buffer+_bufferIndex , _data , size );
128  _bufferIndex += size;
129  }
130  return true;
131 }
132 bool BufferedReadWriteFile::read( void* data , size_t size )
133 {
134  if( !size ) return true;
135  char *_data = (char*) data;
136  size_t sz = _bufferSize - _bufferIndex;
137  while( sz<=size )
138  {
139  if( size && !_bufferSize ) return false;
140  memcpy( _data , _buffer+_bufferIndex , sz );
141  _bufferSize = fread( _buffer , 1 , _bufferSize , _fp );
142  _data += sz;
143  size -= sz;
144  _bufferIndex = 0;
145  if( !size ) return true;
146  sz = _bufferSize;
147  }
148  if( size )
149  {
150  if( !_bufferSize ) return false;
151  memcpy( _data , _buffer+_bufferIndex , size );
152  _bufferIndex += size;
153  }
154  return true;
155 }
156 
157 
159 // CoredFileMeshData //
161 CoredFileMeshData::CoredFileMeshData( void )
162 {
163  oocPoints = polygons = 0;
164 
165  oocPointFile = new BufferedReadWriteFile();
166  polygonFile = new BufferedReadWriteFile();
167 
168  memset(pointFileName,0,sizeof(char)*1024);
169  memset(polygonFileName,0,sizeof(char)*1024);
170 }
171 CoredFileMeshData::~CoredFileMeshData( void )
172 {
173  delete oocPointFile;
174  delete polygonFile;
175 }
176 void CoredFileMeshData::resetIterator ( void )
177 {
178  oocPointFile->reset();
179  polygonFile->reset();
180 }
181 int CoredFileMeshData::addOutOfCorePoint( const Point3D< float >& p )
182 {
183  oocPointFile->write( &p , sizeof( Point3D< float > ) );
184  oocPoints++;
185  return oocPoints-1;
186 }
187 int CoredFileMeshData::addPolygon( const std::vector< CoredVertexIndex >& vertices )
188 {
189  int pSize = int( vertices.size() );
190  std::vector< int > polygon( pSize );
191  for( int i=0 ; i<pSize ; i++ )
192  if( vertices[i].inCore ) polygon[i] = vertices[i].idx;
193  else polygon[i] = -vertices[i].idx-1;
194 
195  polygonFile->write( &pSize , sizeof(int) );
196  polygonFile->write( &polygon[0] , sizeof(int)*pSize );
197  polygons++;
198  return polygons-1;
199 }
200 int CoredFileMeshData::nextOutOfCorePoint( Point3D< float >& p )
201 {
202  if( oocPointFile->read( &p , sizeof( Point3D< float > ) ) ) return 1;
203  else return 0;
204 }
205 int CoredFileMeshData::nextPolygon( std::vector< CoredVertexIndex >& vertices )
206 {
207  int pSize;
208  if( polygonFile->read( &pSize , sizeof(int) ) )
209  {
210  std::vector< int > polygon( pSize );
211  if( polygonFile->read( &polygon[0] , sizeof(int)*pSize ) )
212  {
213  vertices.resize( pSize );
214  for( int i=0 ; i<int(polygon.size()) ; i++ )
215  if( polygon[i]<0 ) vertices[i].idx = -polygon[i]-1 , vertices[i].inCore = false;
216  else vertices[i].idx = polygon[i] , vertices[i].inCore = true;
217  return 1;
218  }
219  return 0;
220  }
221  else return 0;
222 }
223 int CoredFileMeshData::outOfCorePointCount( void ){ return oocPoints; }
224 int CoredFileMeshData::polygonCount( void ) { return polygons; }
225 
227 // CoredVectorMeshData2 //
229 CoredVectorMeshData2::CoredVectorMeshData2( void ) { oocPointIndex = polygonIndex = 0; }
230 void CoredVectorMeshData2::resetIterator ( void ) { oocPointIndex = polygonIndex = 0; }
231 int CoredVectorMeshData2::addOutOfCorePoint( const CoredMeshData2::Vertex& v )
232 {
233  oocPoints.push_back( v );
234  return int(oocPoints.size())-1;
235 }
236 int CoredVectorMeshData2::addPolygon( const std::vector< CoredVertexIndex >& vertices )
237 {
238  std::vector< int > polygon( vertices.size() );
239  for( int i=0 ; i<int(vertices.size()) ; i++ )
240  if( vertices[i].inCore ) polygon[i] = vertices[i].idx;
241  else polygon[i] = -vertices[i].idx-1;
242  polygons.push_back( polygon );
243  return int( polygons.size() )-1;
244 }
245 int CoredVectorMeshData2::nextOutOfCorePoint( CoredMeshData2::Vertex& v )
246 {
247  if(oocPointIndex<int(oocPoints.size()))
248  {
249  v = oocPoints[oocPointIndex++];
250  return 1;
251  }
252  else{return 0;}
253 }
254 int CoredVectorMeshData2::nextPolygon( std::vector< CoredVertexIndex >& vertices )
255 {
256  if( polygonIndex<int( polygons.size() ) )
257  {
258  std::vector< int >& polygon = polygons[ polygonIndex++ ];
259  vertices.resize( polygon.size() );
260  for( int i=0 ; i<int(polygon.size()) ; i++ )
261  if( polygon[i]<0 ) vertices[i].idx = -polygon[i]-1 , vertices[i].inCore = false;
262  else vertices[i].idx = polygon[i] , vertices[i].inCore = true;
263  return 1;
264  }
265  else return 0;
266 }
267 int CoredVectorMeshData2::outOfCorePointCount(void){return int(oocPoints.size());}
268 int CoredVectorMeshData2::polygonCount( void ) { return int( polygons.size() ); }