Debian Codegraph @

src/thirdparty/PoissonRecon/Ply.inl

raw
1 /*
2 Copyright (c) 2020, Michael Kazhdan
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 namespace PLY
30 {
31 inline int DefaultFileType( void ){ return PLY_ASCII; }
32
33 template<> inline int Type< int >( void ){ return PLY_INT ; }
34 template<> inline int Type< unsigned int >( void ){ return PLY_UINT ; }
35 template<> inline int Type< char >( void ){ return PLY_CHAR ; }
36 template<> inline int Type< unsigned char >( void ){ return PLY_UCHAR ; }
37 template<> inline int Type< float >( void ){ return PLY_FLOAT ; }
38 template<> inline int Type< double >( void ){ return PLY_DOUBLE; }
39 template< class Scalar > inline int Type( void )
40 {
41 MK_THROW( "Unrecognized scalar type: " , typeid(Scalar).name() );
42 return -1;
43 }
44
45 template<> const inline std::string Traits< int >::name="int";
46 template<> const inline std::string Traits< unsigned int >::name="unsigned int";
47 template<> const inline std::string Traits< long >::name="long";
48 template<> const inline std::string Traits< unsigned long >::name="unsigned long";
49 template<> const inline std::string Traits< long long >::name="long long";
50 template<> const inline std::string Traits< unsigned long long >::name="unsigned long long";
51
52 template<> const inline PlyProperty Edge< int >::Properties[] = { PlyProperty( "v1" , PLY_INT , PLY_INT , offsetof( Edge , v1 ) ) , PlyProperty( "v2" , PLY_INT , PLY_INT , offsetof( Edge , v2 ) ) };
53 template<> const inline PlyProperty Edge< unsigned int >::Properties[] = { PlyProperty( "v1" , PLY_UINT , PLY_UINT , offsetof( Edge , v1 ) ) , PlyProperty( "v2" , PLY_UINT , PLY_UINT , offsetof( Edge , v2 ) ) };
54 template<> const inline PlyProperty Edge< long long >::Properties[] = { PlyProperty( "v1" , PLY_LONGLONG , PLY_LONGLONG , offsetof( Edge , v1 ) ) , PlyProperty( "v2" , PLY_LONGLONG , PLY_LONGLONG , offsetof( Edge , v2 ) ) };
55 template<> const inline PlyProperty Edge< unsigned long long >::Properties[] = { PlyProperty( "v1" , PLY_ULONGLONG , PLY_ULONGLONG , offsetof( Edge , v1 ) ) , PlyProperty( "v2" , PLY_ULONGLONG , PLY_ULONGLONG , offsetof( Edge , v2 ) ) };
56
57 template<> const inline PlyProperty Face< int , false >::Properties[] = { PlyProperty( "vertex_indices" , PLY_INT , PLY_INT , offsetof( Face , vertices ) , 1 , PLY_INT , PLY_INT , offsetof( Face , nr_vertices ) ) };
58 template<> const inline PlyProperty Face< unsigned int , false >::Properties[] = { PlyProperty( "vertex_indices" , PLY_UINT , PLY_UINT , offsetof( Face , vertices ) , 1 , PLY_INT , PLY_INT , offsetof( Face , nr_vertices ) ) };
59 template<> const inline PlyProperty Face< long long , false >::Properties[] = { PlyProperty( "vertex_indices" , PLY_LONGLONG , PLY_LONGLONG , offsetof( Face , vertices ) , 1 , PLY_INT , PLY_INT , offsetof( Face , nr_vertices ) ) };
60 template<> const inline PlyProperty Face< unsigned long long , false >::Properties[] = { PlyProperty( "vertex_indices" , PLY_ULONGLONG , PLY_ULONGLONG , offsetof( Face , vertices ) , 1 , PLY_INT , PLY_INT , offsetof( Face , nr_vertices ) ) };
61 template<> const inline PlyProperty Face< int , true >::Properties[] = { PlyProperty( "vertex_indices" , PLY_INT , PLY_INT , offsetof( Face , vertices ) , 1 , PLY_CHAR , PLY_CHAR , offsetof( Face , nr_vertices ) ) };
62 template<> const inline PlyProperty Face< unsigned int , true >::Properties[] = { PlyProperty( "vertex_indices" , PLY_UINT , PLY_UINT , offsetof( Face , vertices ) , 1 , PLY_CHAR , PLY_CHAR , offsetof( Face , nr_vertices ) ) };
63 template<> const inline PlyProperty Face< long long , true >::Properties[] = { PlyProperty( "vertex_indices" , PLY_LONGLONG , PLY_LONGLONG , offsetof( Face , vertices ) , 1 , PLY_CHAR , PLY_CHAR , offsetof( Face , nr_vertices ) ) };
64 template<> const inline PlyProperty Face< unsigned long long , true >::Properties[] = { PlyProperty( "vertex_indices" , PLY_ULONGLONG , PLY_ULONGLONG , offsetof( Face , vertices ) , 1 , PLY_CHAR , PLY_CHAR , offsetof( Face , nr_vertices ) ) };
65
66 // Read
67 inline PlyFile *ReadHeader( std::string fileName , int &fileType , std::vector< std::tuple< std::string , size_t , std::vector< PlyProperty > > > &elems , std::vector< std::string > &comments )
68 {
69 std::vector< std::string > elist;
70 float version;
71
72 PlyFile *ply = PlyFile::Read( fileName , elist , fileType , version );
73 if( !ply ) MK_THROW( "Could not open ply file for reading: " , fileName );
74
75 elems.resize( elist.size() );
76 for( unsigned int i=0 ; i<elist.size() ; i++ )
77 {
78 std::get<0>( elems[i] ) = elist[i];
79 std::get<2>( elems[i] ) = ply->get_element_description( std::get<0>( elems[i] ) , std::get<1>( elems[i] ) );
80 }
81
82 comments.resize( ply->comments.size() );
83 for( int i=0 ; i<ply->comments.size() ; i++ ) comments[i] = ply->comments[i];
84
85 return ply;
86 }
87
88 inline PlyFile *WriteHeader( std::string fileName , int fileType , const std::vector< std::tuple< std::string , size_t , std::vector< PlyProperty > > > &elems , const std::vector< std::string > &comments )
89 {
90 PlyFile *ply = NULL;
91 {
92 float version;
93 std::vector< std::string > elist( elems.size() );
94 for( unsigned int i=0 ; i<elems.size() ; i++ ) elist[i] = std::get<0>( elems[i] );
95 ply = PlyFile::Write( fileName , elist , fileType , version );
96 }
97 if( !ply ) MK_THROW( "Could not open ply for writing: " , fileName );
98 for( unsigned int i=0 ; i<elems.size() ; i++ )
99 {
100 ply->element_count( std::get<0>( elems[i] ) , std::get<1>( elems[i] ) );
101 const std::vector< PlyProperty > &props = std::get<2>( elems[i] );
102 for( unsigned int j=0 ; j<props.size() ; j++ ) ply->describe_property( std::get<0>( elems[i] ) , &props[j] );
103 }
104
105 for( int i=0 ; i<comments.size() ; i++ ) ply->put_comment( comments[i] );
106 ply->header_complete();
107
108 return ply;
109 }
110
111 inline PlyFile *ReadHeader( std::string fileName , int &fileType , std::vector< std::tuple< std::string , size_t , std::vector< PlyProperty > > > &elems )
112 {
113 std::vector< std::string > comments;
114 return ReadHeader( fileName , fileType , elems , comments );
115 }
116
117 inline PlyFile *WriteHeader( std::string fileName , int fileType , const std::vector< std::tuple< std::string , size_t , std::vector< PlyProperty > > > &elems )
118 {
119 std::vector< std::string > comments;
120 return WriteHeader( fileName , fileType , elems , comments );
121 }
122
123 template< typename VertexFactory >
124 inline int ReadVertexHeader( std::string fileName , const VertexFactory &vFactory , bool *readFlags )
125 {
126 int fileType;
127 std::vector< std::string > elist;
128 float version;
129
130 PlyFile *ply = PlyFile::Read( fileName , elist , fileType , version );
131 if( !ply ) MK_THROW( "could not create read ply file: " , fileName );
132
133 for( int i=0 ; i<(int)elist.size() ; i++ ) if( elist[i]=="vertex" ) for( unsigned int j=0 ; j<vFactory.plyReadNum() ; j++ )
134 {
135 PlyProperty prop;
136 if constexpr( VertexFactory::IsStaticallyAllocated() ) prop = vFactory.plyStaticReadProperty(j);
137 else prop = vFactory.plyReadProperty(j);
138 readFlags[j] = ( ply->get_property( elist[i] , &prop )!=0 );
139 }
140
141 delete ply;
142 return fileType;
143 }
144
145 template< typename VertexFactory >
146 inline int ReadVertexHeader( std::string fileName , const VertexFactory &vFactory , bool *readFlags , std::vector< PlyProperty > &unprocessedProperties , size_t &vNum )
147 {
148 int fileType;
149 std::vector< std::string > elist;
150 float version;
151
152 PlyFile *ply = PlyFile::Read( fileName, elist, fileType, version );
153 if( !ply ) MK_THROW( "Failed to open ply file for reading: " , fileName );
154
155 std::vector< PlyProperty > plist = ply->get_element_description( "vertex" , vNum );
156 if( !plist.size() ) MK_THROW( "Failed to get element description: vertex" );
157 for( unsigned int i=0 ; i<vFactory.plyReadNum() ; i++ ) readFlags[i] = false;
158
159 for( int i=0 ; i<plist.size() ; i++ )
160 {
161 bool found = false;
162 for( unsigned int j=0 ; j<vFactory.plyReadNum() ; j++ )
163 {
164 PlyProperty prop;
165 if constexpr( VertexFactory::IsStaticallyAllocated() ) prop = vFactory.plyStaticReadProperty(j);
166 else prop = vFactory.plyReadProperty(j);
167 if( prop.name==plist[i].name ) found = readFlags[j] = true;
168 }
169 if( !found ) unprocessedProperties.push_back( plist[i] );
170 }
171 delete ply;
172 return fileType;
173 }
174 template< typename VertexFactory >
175 inline int ReadVertexHeader( std::string fileName , const VertexFactory &vFactory , bool *readFlags , std::vector< PlyProperty > &unprocessedProperties )
176 {
177 size_t vNum;
178 return ReadVertexHeader( fileName , vFactory , readFlags , unprocessedProperties , vNum );
179 }
180
181 inline int ReadVertexHeader( std::string fileName , std::vector< PlyProperty > &properties , size_t &vNum )
182 {
183 int fileType;
184 std::vector< std::string > elist;
185 float version;
186
187 PlyFile *ply = PlyFile::Read( fileName, elist, fileType, version );
188 if( !ply ) MK_THROW( "Failed to open ply file for reading: " , fileName );
189
190 std::vector< PlyProperty > plist = ply->get_element_description( "vertex" , vNum );
191 for( int i=0 ; i<plist.size() ; i++ ) properties.push_back( plist[i] );
192 delete ply;
193 return fileType;
194 }
195
196 inline int ReadVertexHeader( std::string fileName , std::vector< PlyProperty > &properties )
197 {
198 size_t vNum;
199 return ReadVertexHeader( fileName , properties , vNum );
200 }
201
202 template< typename VertexFactory , typename Index >
203 void ReadEdges( std::string fileName , const VertexFactory &vFactory , std::vector< typename VertexFactory::VertexType > &vertices , std::vector< std::pair< Index , Index > > &edges , int &file_type , std::vector< std::string > &comments , bool *readFlags )
204 {
205 std::vector< std::string > elist;
206 float version;
207
208 PlyFile *ply = PlyFile::Read( fileName , elist , file_type , version );
209 if( !ply ) MK_THROW( "Could not create ply file for reading: " , fileName );
210
211 comments.reserve( comments.size() + ply->comments.size() );
212 for( int i=0 ; i<ply->comments.size() ; i++ ) comments.push_back( ply->comments[i] );
213
214 for( int i=0 ; i<elist.size() ; i++ )
215 {
216 std::string &elem_name = elist[i];
217 size_t num_elems;
218 std::vector< PlyProperty > plist = ply->get_element_description( elem_name , num_elems );
219 if( !plist.size() ) MK_THROW( "Could not read element properties: " , elem_name );
220 if( elem_name=="vertex" )
221 {
222 for( unsigned int i=0 ; i<vFactory.plyReadNum() ; i++ )
223 {
224 PlyProperty prop;
225 if constexpr( VertexFactory::IsStaticallyAllocated() ) prop = vFactory.plyStaticReadProperty(i);
226 else prop = vFactory.plyReadProperty(i);
227 int hasProperty = ply->get_property( elem_name , &prop );
228 if( readFlags ) readFlags[i] = (hasProperty!=0);
229 }
230 vertices.resize( num_elems , vFactory() );
231 Pointer( char ) buffer = NewPointer< char >( vFactory.bufferSize() );
232 for( size_t j=0 ; j<num_elems ; j++ )
233 {
234 if constexpr( VertexFactory::IsStaticallyAllocated() ) ply->get_element( (void*)&vertices[j] );
235 else
236 {
237 ply->get_element( PointerAddress( buffer ) );
238 vFactory.fromBuffer( buffer , vertices[j] );
239 }
240 }
241 DeletePointer( buffer );
242 }
243 else if( elem_name=="edge" )
244 {
245 ply->get_property( elem_name , &Edge< Index >::Properties[0] );
246 ply->get_property( elem_name , &Edge< Index >::Properties[1] );
247 edges.resize( num_elems );
248 for( size_t j=0 ; j<num_elems ; j++ )
249 {
250 Edge< Index > ply_edge;
251 ply->get_element( (void *)&ply_edge );
252 edges[j].first = ply_edge.v1;
253 edges[j].second = ply_edge.v2;
254 } // for, read edges
255 } // if face
256 else ply->get_other_element( elem_name , num_elems );
257 } // for each type of element
258
259 delete ply;
260 }
261
262 template< typename VertexFactory , typename Index >
263 void ReadPolygons( std::string fileName , const VertexFactory &vFactory , std::vector< typename VertexFactory::VertexType > &vertices , std::vector< std::vector< Index > > &polygons , int &file_type , std::vector< std::string > &comments , bool *readFlags )
264 {
265 std::vector< std::string > elist;
266 float version;
267
268 PlyFile *ply = PlyFile::Read( fileName , elist , file_type , version );
269 if( !ply ) MK_THROW( "Could not create ply file for reading: " , fileName );
270
271 comments.reserve( comments.size() + ply->comments.size() );
272 for( int i=0 ; i<ply->comments.size() ; i++ ) comments.push_back( ply->comments[i] );
273
274 for( int i=0 ; i<elist.size() ; i++ )
275 {
276 std::string &elem_name = elist[i];
277 size_t num_elems;
278 std::vector< PlyProperty > plist = ply->get_element_description( elem_name , num_elems );
279 if( !plist.size() ) MK_THROW( "Could not read element properties: " , elem_name );
280 if( elem_name=="vertex" )
281 {
282 for( unsigned int i=0 ; i<vFactory.plyReadNum() ; i++)
283 {
284 PlyProperty prop;
285 if constexpr( VertexFactory::IsStaticallyAllocated() ) prop = vFactory.plyStaticReadProperty(i);
286 else prop = vFactory.plyReadProperty(i);
287 int hasProperty = ply->get_property( elem_name , &prop );
288 if( readFlags ) readFlags[i] = (hasProperty!=0);
289 }
290 vertices.resize( num_elems , vFactory() );
291 Pointer( char ) buffer = NewPointer< char >( vFactory.bufferSize() );
292 for( size_t j=0 ; j<num_elems ; j++ )
293 {
294 if constexpr( VertexFactory::IsStaticallyAllocated() ) ply->get_element( (void*)&vertices[j] );
295 else
296 {
297 ply->get_element( PointerAddress( buffer ) );
298 vFactory.fromBuffer( buffer , vertices[j] );
299 }
300 }
301 DeletePointer( buffer );
302 }
303 else if( elem_name=="face" )
304 {
305 ply->get_property( elem_name , Face< Index >::Properties );
306 polygons.resize( num_elems );
307 for( size_t j=0 ; j<num_elems ; j++ )
308 {
309 Face< Index > ply_face;
310 ply->get_element( (void *)&ply_face );
311 polygons[j].resize( ply_face.nr_vertices );
312 for( unsigned int k=0 ; k<ply_face.nr_vertices ; k++ ) polygons[j][k] = ply_face.vertices[k];
313 free( ply_face.vertices );
314 } // for, read faces
315 } // if face
316 else ply->get_other_element( elem_name , num_elems );
317 } // for each type of element
318
319 delete ply;
320 }
321
322 template< typename VertexFactory , typename Index , bool UseCharIndex >
323 void WritePolygons( std::string fileName , const VertexFactory &vFactory , const std::vector< typename VertexFactory::VertexType > &vertices , const std::vector< std::vector< Index > > &polygons , int file_type , const std::vector< std::string > &comments )
324 {
325 size_t nr_vertices = vertices.size();
326 size_t nr_faces = polygons.size();
327 float version;
328 std::vector< std::string > elem_names = { std::string( "vertex" ) , std::string( "face" ) };
329 PlyFile *ply = PlyFile::Write( fileName , elem_names , file_type , version );
330 if( !ply ) MK_THROW( "Could not create ply file for writing: " , fileName );
331
332 //
333 // describe vertex and face properties
334 //
335 ply->element_count( "vertex", nr_vertices );
336 for( unsigned int i=0 ; i<vFactory.plyWriteNum() ; i++ )
337 {
338 PlyProperty prop;
339 if constexpr( VertexFactory::IsStaticallyAllocated() ) prop = vFactory.plyStaticWriteProperty(i);
340 else prop = vFactory.plyWriteProperty(i);
341 ply->describe_property( "vertex" , &prop );
342 }
343 ply->element_count( "face" , nr_faces );
344 ply->describe_property( "face" , Face< Index , UseCharIndex >::Properties );
345
346 // Write in the comments
347 for( int i=0 ; i<comments.size() ; i++ ) ply->put_comment( comments[i] );
348 ply->header_complete();
349
350 // write vertices
351 ply->put_element_setup( elem_names[0] );
352
353 Pointer( char ) buffer = NewPointer< char >( vFactory.bufferSize() );
354 for( size_t j=0 ; j<vertices.size() ; j++ )
355 {
356 if constexpr( VertexFactory::IsStaticallyAllocated() ) ply->put_element( (void *)&vertices[j] );
357 else
358 {
359 vFactory.toBuffer( vertices[j] , buffer );
360 ply->put_element( PointerAddress( buffer ) );
361 }
362 }
363 DeletePointer( buffer );
364
365 // write faces
366 Face< Index > ply_face;
367 int maxFaceVerts=3;
368 ply_face.nr_vertices = 3;
369 ply_face.vertices = new Index[3];
370
371 ply->put_element_setup( elem_names[1] );
372 for( size_t i=0 ; i<nr_faces ; i++ )
373 {
374 if( (int)polygons[i].size()>maxFaceVerts )
375 {
376 delete[] ply_face.vertices;
377 maxFaceVerts = (int)polygons[i].size();
378 ply_face.vertices=new Index[ maxFaceVerts ];
379 }
380 ply_face.nr_vertices = (int)polygons[i].size();
381 for( size_t j=0 ; j<ply_face.nr_vertices ; j++ ) ply_face.vertices[j] = polygons[i][j];
382 ply->put_element( (void *)&ply_face );
383 }
384
385 delete[] ply_face.vertices;
386 delete ply;
387 }
388
389 template< typename VertexFactory , typename Index , class Real , int Dim , typename OutputIndex , bool UseCharIndex >
390 void Write( std::string fileName , const VertexFactory &vFactory , size_t vertexNum , size_t polygonNum , InputDataStream< typename VertexFactory::VertexType > &vertexStream , InputDataStream< std::vector< Index > > &polygonStream , int file_type , const std::vector< std::string > &comments )
391 {
392 if( vertexNum>(size_t)std::numeric_limits< OutputIndex >::max() )
393 {
394 if( std::is_same< Index , OutputIndex >::value ) MK_THROW( "more vertices than can be represented using " , Traits< Index >::name );
395 MK_WARN( "more vertices than can be represented using " , Traits< OutputIndex >::name , " using " , Traits< Index >::name , " instead" );
396 return Write< VertexFactory , Index , Real , Dim , Index >( fileName , vFactory , vertexNum , polygonNum , vertexStream , polygonStream , file_type , comments );
397 }
398 float version;
399 std::vector< std::string > elem_names = { std::string( "vertex" ) , std::string( "face" ) };
400 PlyFile *ply = PlyFile::Write( fileName , elem_names , file_type , version );
401 if( !ply ) MK_THROW( "Could not create ply file for writing: " , fileName );
402
403 vertexStream.reset();
404 polygonStream.reset();
405
406 //
407 // describe vertex and face properties
408 //
409 ply->element_count( "vertex" , vertexNum );
410 for( unsigned int i=0 ; i<vFactory.plyWriteNum() ; i++ )
411 {
412 PlyProperty prop;
413 if constexpr( VertexFactory::IsStaticallyAllocated() ) prop = vFactory.plyStaticWriteProperty(i);
414 else prop = vFactory.plyWriteProperty(i);
415 ply->describe_property( "vertex" , &prop );
416 }
417 ply->element_count( "face" , polygonNum );
418 ply->describe_property( "face" , Face< OutputIndex , UseCharIndex >::Properties );
419
420 // Write in the comments
421 for( int i=0 ; i<comments.size() ; i++ ) ply->put_comment( comments[i] );
422 ply->header_complete();
423
424 // write vertices
425 ply->put_element_setup( "vertex" );
426 if constexpr( VertexFactory::IsStaticallyAllocated() )
427 {
428 for( size_t i=0; i<vertexNum ; i++ )
429 {
430 typename VertexFactory::VertexType vertex = vFactory();
431 if( !vertexStream.read( vertex ) ) MK_THROW( "Failed to read vertex " , i , " / " , vertexNum );
432 ply->put_element( (void *)&vertex );
433 }
434 }
435 else
436 {
437 Pointer( char ) buffer = NewPointer< char >( vFactory.bufferSize() );
438 for( size_t i=0; i<vertexNum ; i++ )
439 {
440 typename VertexFactory::VertexType vertex = vFactory();
441 if( !vertexStream.read( vertex ) ) MK_THROW( "Failed to read vertex " , i , " / " , vertexNum );
442 vFactory.toBuffer( vertex , buffer );
443 ply->put_element( PointerAddress( buffer ) );
444 }
445 DeletePointer( buffer );
446 }
447
448 // write faces
449 std::vector< Index > polygon;
450 ply->put_element_setup( "face" );
451 for( size_t i=0 ; i<polygonNum ; i++ )
452 {
453 //
454 // create and fill a struct that the ply code can handle
455 //
456 Face< OutputIndex > ply_face;
457 if( !polygonStream.read( polygon ) ) MK_THROW( "Failed to read polygon " , i , " / " , polygonNum );
458 ply_face.nr_vertices = int( polygon.size() );
459 ply_face.vertices = new OutputIndex[ polygon.size() ];
460 for( int j=0 ; j<int(polygon.size()) ; j++ ) ply_face.vertices[j] = (OutputIndex)polygon[j];
461 ply->put_element( (void *)&ply_face );
462 delete[] ply_face.vertices;
463 } // for, write faces
464
465
466 delete ply;
467 }
468
469 template< typename VertexFactory , typename Index , class Real , int Dim , typename OutputIndex >
470 void Write( std::string fileName , const VertexFactory &vFactory , size_t vertexNum , size_t edgeNum , InputDataStream< typename VertexFactory::VertexType > &vertexStream , InputDataStream< std::pair< Index , Index > > &edgeStream , int file_type , const std::vector< std::string > &comments )
471 {
472 if( vertexNum>(size_t)std::numeric_limits< OutputIndex >::max() )
473 {
474 if( std::is_same< Index , OutputIndex >::value ) MK_THROW( "more vertices than can be represented using " , Traits< Index >::name );
475 MK_WARN( "more vertices than can be represented using " , Traits< OutputIndex >::name , " using " , Traits< Index >::name , " instead" );
476 return Write< VertexFactory , Index , Real , Dim , Index >( fileName , vFactory , vertexNum , edgeNum , vertexStream , edgeStream , file_type , comments );
477 }
478 float version;
479 std::vector< std::string > elem_names = { std::string( "vertex" ) , std::string( "edge" ) };
480 PlyFile *ply = PlyFile::Write( fileName , elem_names , file_type , version );
481 if( !ply ) MK_THROW( "Could not create ply file for writing: " , fileName );
482
483 vertexStream.reset();
484 edgeStream.reset();
485
486 //
487 // describe vertex and face properties
488 //
489 ply->element_count( "vertex" , vertexNum );
490 for( unsigned int i=0 ; i<vFactory.plyWriteNum() ; i++ )
491 {
492 PlyProperty prop;
493 if constexpr( VertexFactory::IsStaticallyAllocated() ) prop = vFactory.plyStaticWriteProperty(i);
494 else prop = vFactory.plyWriteProperty(i);
495 ply->describe_property( "vertex" , &prop );
496 }
497 ply->element_count( "edge" , edgeNum );
498 ply->describe_property( "edge" , &Edge< OutputIndex >::Properties[0] );
499 ply->describe_property( "edge" , &Edge< OutputIndex >::Properties[1] );
500
501 // Write in the comments
502 for( int i=0 ; i<comments.size() ; i++ ) ply->put_comment( comments[i] );
503 ply->header_complete();
504
505 // write vertices
506 ply->put_element_setup( "vertex" );
507 if constexpr( VertexFactory::IsStaticallyAllocated() )
508 {
509 for( size_t i=0; i<vertexNum ; i++ )
510 {
511 typename VertexFactory::VertexType vertex = vFactory();
512 if( !vertexStream.read( vertex ) ) MK_THROW( "Failed to read vertex " , i , " / " , vertexNum );
513 ply->put_element( (void *)&vertex );
514 }
515 }
516 else
517 {
518 Pointer( char ) buffer = NewPointer< char >( vFactory.bufferSize() );
519 for( size_t i=0; i<vertexNum ; i++ )
520 {
521 typename VertexFactory::VertexType vertex = vFactory();
522 if( !vertexStream.read( vertex ) ) MK_THROW( "Failed to read vertex " , i , " / " , vertexNum );
523 vFactory.toBuffer( vertex , buffer );
524 ply->put_element( PointerAddress( buffer ) );
525 }
526 DeletePointer( buffer );
527 }
528
529 // write edges
530 std::pair< Index , Index > edge;
531 ply->put_element_setup( "edge" );
532 for( size_t i=0 ; i<edgeNum ; i++ )
533 {
534 //
535 // create and fill a struct that the ply code can handle
536 //
537 Edge< OutputIndex > ply_edge;
538 if( !edgeStream.read( edge ) ) MK_THROW( "Failed to read edge " , i , " / " , edgeNum );
539 ply_edge.v1 = (OutputIndex)edge.first;
540 ply_edge.v2 = (OutputIndex)edge.second;
541 ply->put_element( (void *)&ply_edge );
542 } // for, write edges
543
544 delete ply;
545 }
546 }