-
Notifications
You must be signed in to change notification settings - Fork 21
/
PLYWriter.hpp
93 lines (74 loc) · 2.37 KB
/
PLYWriter.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Static class for writing PLY files.
Copyright (C) 2011 Tao Ju
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
(LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef PLYWRITER_H
#define PLYWRITER_H
class PLYWriter {
public:
/// Constructor
PLYWriter( ) { };
/// Write ply header
static void writeHeader ( FILE* fout, int numVert, int numFace ) {
// Ply
fprintf( fout, "ply\n" ) ;
// Always big endian
fprintf( fout, "format binary_big_endian 1.0\n" ) ;
// vertex properties
fprintf( fout, "element vertex %d\n", numVert ) ;
fprintf( fout, "property float x\n" ) ;
fprintf( fout, "property float y\n" ) ;
fprintf( fout, "property float z\n" ) ;
// face properties
fprintf( fout, "element face %d\n", numFace ) ;
fprintf( fout, "property list uchar int vertex_indices\n" ) ;
// End
fprintf( fout, "end_header\n" ) ;
};
// data written below is not ascii, but binary!
/// Write vertex
static void writeVertex ( FILE* fout, float vt[3] )
{
float nvt[3] ;
for ( int i = 0 ; i < 3 ; i ++ )
{
nvt[i] = vt[i] ;
flipBits32( &(nvt[i]) ) ;
}
fwrite( nvt, sizeof ( float ), 3, fout ) ;
};
/// Write face
static void writeFace ( FILE* fout, int num, int fc[] )
{
unsigned char cnum = num ;
fwrite( &cnum, sizeof( unsigned char ), 1, fout ) ;
for ( int i = 0 ; i < num ; i ++ )
{
flipBits32( &(fc[i]) ) ;
}
fwrite( fc, sizeof ( int ), num, fout ) ;
};
static void flipBits32 ( void *x )
{
unsigned char *temp = (unsigned char *)x;
unsigned char swap;
swap = temp [ 0 ];
temp [ 0 ] = temp [ 3 ];
temp [ 3 ] = swap;
swap = temp [ 1 ];
temp [ 1 ] = temp [ 2 ];
temp [ 2 ] = swap;
};
};
#endif