comparison ply2bin.cpp @ 30:1a0e823283e4

Add simple converter for converting ASCII ply files into binary format.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 22 Nov 2019 09:49:06 +0200
parents
children d0cd281934a3
comparison
equal deleted inserted replaced
29:5c7f63fe5c19 30:1a0e823283e4
1 //
2 // ply2bin - Convert ASCII PLY file to binary PLY
3 // Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org>
4 // (C) Copyright 2019 Tecnic Software productions (TNSP)
5 //
6 // See file "COPYING" for license information.
7 //
8 #include <SDL.h>
9 #include "dmutil.h"
10 #include "dmmodel.h"
11
12
13 bool dmFWriteFloatLE(FILE *fh, const float val)
14 {
15 float tmp = SDL_SwapFloatLE(val);
16 return fwrite(&tmp, sizeof(tmp), 1, fh) == 1;
17 }
18
19
20 bool dmFWriteU32LE(FILE *fh, const Uint32 val)
21 {
22 Uint32 tmp = SDL_SwapLE32(val);
23 return fwrite(&tmp, sizeof(tmp), 1, fh) == 1;
24 }
25
26
27 bool dmFWriteU8(FILE *fh, const Uint8 val)
28 {
29 return fwrite(&val, sizeof(val), 1, fh) == 1;
30 }
31
32
33 bool dmFWriteBINPLY(FILE *fh, const DMModel &model)
34 {
35 bool writeNormals = model.normals.size() == (unsigned int) model.nvertices;
36
37 fprintf(fh,
38 "ply\n"
39 "format binary_little_endian 1.0\n"
40 "element vertex %d\n"
41 "property float x\n"
42 "property float y\n"
43 "property float z\n",
44 model.nvertices);
45
46 if (writeNormals)
47 {
48 fprintf(fh,
49 "property float nx\n"
50 "property float ny\n"
51 "property float nz\n"
52 );
53 }
54
55 fprintf(fh,
56 "element face %d\n"
57 "property list uchar uint vertex_indices\n"
58 "end_header\n",
59 model.nfaces);
60
61 for (int nvert = 0; nvert < model.nvertices; nvert++)
62 {
63 const DMVector3 &vert = model.vertices[nvert];
64
65 if (!dmFWriteFloatLE(fh, vert.x) ||
66 !dmFWriteFloatLE(fh, vert.y) ||
67 !dmFWriteFloatLE(fh, vert.z))
68 return false;
69
70 if (writeNormals)
71 {
72 const DMVector3 &vert = model.normals[nvert];
73 if (!dmFWriteFloatLE(fh, vert.x) ||
74 !dmFWriteFloatLE(fh, vert.y) ||
75 !dmFWriteFloatLE(fh, vert.z))
76 return false;
77 }
78 }
79
80 for (int nface = 0, offs = 0; nface < model.nfaces; nface++)
81 {
82 if (!dmFWriteU8(fh, 3))
83 return false;
84
85 for (int nvert = 0; nvert < 3; nvert++)
86 {
87 if (!dmFWriteU32LE(fh, model.faces[offs++]))
88 return false;
89 }
90 }
91
92 return true;
93 }
94
95
96 int main(int argc, char *argv[])
97 {
98 bool
99 optShowHelp = false;
100 std::string optInputFilename, optOutputFilename;
101 FILE *outFile = NULL;
102 DMModel model;
103
104 // Check commandline argument for enabling shaders
105 for (int narg = 1; narg < argc; narg++)
106 {
107 char *arg = argv[narg];
108 if (arg[0] == '-')
109 {
110 char *opt = arg + 1;
111
112 if ((opt[0] == '-' && opt[1] == 'h' && opt[2] == 'e') ||
113 opt[0] == '?' || (opt[0] == '-' && opt[1] == '?'))
114 {
115 optShowHelp = true;
116 break;
117 }
118 else
119 if (opt[0] == '-')
120 opt++;
121
122 switch (opt[0])
123 {
124 default:
125 printf("Unknown option '%s'.\n", opt);
126 goto exit;
127 }
128 }
129 else
130 {
131 std::string tmp = std::string(arg);
132 if (tmp.empty())
133 {
134 printf("ERROR: Invalid empty filename.\n");
135 goto exit;
136 }
137
138 if (optInputFilename.empty())
139 optInputFilename = tmp;
140 else
141 if (optOutputFilename.empty())
142 optOutputFilename = tmp;
143 else
144 {
145 printf("ERROR: Too many filenames specified.\n");
146 goto exit;
147 }
148 }
149 }
150
151 if (optInputFilename.empty() || optOutputFilename.empty() || optShowHelp)
152 {
153 printf(
154 "ply2bin - Convert ASCII PLY file to binary format PLY\n"
155 "Usage: %s [options] <input.ply> <output.ply>\n"
156 "-? Show this help\n"
157 "\n",
158 argv[0]
159 );
160
161 goto exit;
162 }
163
164 if (!model.loadFromPLY(optInputFilename))
165 goto exit;
166
167 if ((outFile = fopen(optOutputFilename.c_str(), "wb")) == NULL)
168 {
169 printf("ERROR: Could not create output file.\n");
170 goto exit;
171 }
172
173 printf("Writing output PLY ..\n");
174 if (!dmFWriteBINPLY(outFile, model))
175 {
176 printf("ERROR: Error writing output PLY file.\n");
177 }
178
179 exit:
180 if (outFile != NULL)
181 fclose(outFile);
182
183 return 0;
184 }