1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <strings.h> |
---|
4 | #include <iostream> |
---|
5 | #include <limits.h> |
---|
6 | #include <ply.h> |
---|
7 | |
---|
8 | #define MAX_VERT_PROPS 20 |
---|
9 | |
---|
10 | #include "plyio.h" |
---|
11 | |
---|
12 | static void reallocTris(Vertex *v); |
---|
13 | |
---|
14 | struct PlyVertex { |
---|
15 | float x, y, z; |
---|
16 | float nx, ny, nz; |
---|
17 | uchar diff_r, diff_g, diff_b; |
---|
18 | float intensity; |
---|
19 | float std_dev; |
---|
20 | float confidence; |
---|
21 | }; |
---|
22 | |
---|
23 | |
---|
24 | struct PlyFace { |
---|
25 | uchar nverts; |
---|
26 | int *verts; |
---|
27 | }; |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | static PlyProperty vert_prop_x = |
---|
32 | {"x", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
33 | static PlyProperty vert_prop_y = |
---|
34 | {"y", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
35 | static PlyProperty vert_prop_z = |
---|
36 | {"z", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
37 | static PlyProperty vert_prop_nx = |
---|
38 | {"nx", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
39 | static PlyProperty vert_prop_ny = |
---|
40 | {"ny", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
41 | static PlyProperty vert_prop_nz = |
---|
42 | {"nz", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
43 | static PlyProperty vert_prop_intens = |
---|
44 | {"intensity", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
45 | static PlyProperty vert_prop_std_dev = |
---|
46 | {"std_dev", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
47 | static PlyProperty vert_prop_confidence = |
---|
48 | {"confidence", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
49 | static PlyProperty vert_prop_diff_r = |
---|
50 | {"diffuse_red", PLY_UCHAR, PLY_UCHAR, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
51 | static PlyProperty vert_prop_diff_g = |
---|
52 | {"diffuse_green", PLY_UCHAR, PLY_UCHAR, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
53 | static PlyProperty vert_prop_diff_b = |
---|
54 | {"diffuse_blue", PLY_UCHAR, PLY_UCHAR, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0}; |
---|
55 | |
---|
56 | static PlyProperty vert_props[MAX_VERT_PROPS]; |
---|
57 | |
---|
58 | |
---|
59 | static PlyProperty face_props[] = { |
---|
60 | {"vertex_indices", PLY_INT, PLY_INT, 0, 1, PLY_UCHAR, PLY_UCHAR, 0}, |
---|
61 | }; |
---|
62 | |
---|
63 | |
---|
64 | /* dummy variables and associated macros for computing field offsets */ |
---|
65 | |
---|
66 | static PlyVertex *vert_dummy; |
---|
67 | #define voffset(field) ((char *) (&vert_dummy->field) - (char *) vert_dummy) |
---|
68 | static PlyFace *face_dummy; |
---|
69 | #define foffset(field) ((char *) (&face_dummy->field) - (char *) face_dummy) |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | // |
---|
75 | // Read in data from the ply file |
---|
76 | // |
---|
77 | |
---|
78 | Mesh * |
---|
79 | readPlyFile(FILE *inFile) |
---|
80 | { |
---|
81 | int j; |
---|
82 | int nelems; |
---|
83 | char **elist; |
---|
84 | char *elem_name; |
---|
85 | int file_type; |
---|
86 | float version; |
---|
87 | int nprops, num_vert_props; |
---|
88 | int num_elems; |
---|
89 | PlyProperty **plist; |
---|
90 | |
---|
91 | face_props[0].offset = foffset(verts); |
---|
92 | face_props[0].count_offset = foffset(nverts); /* count offset */ |
---|
93 | |
---|
94 | PlyFile *ply = ply_read (inFile, &nelems, &elist); |
---|
95 | ply_get_info (ply, &version, &file_type); |
---|
96 | |
---|
97 | if (!ply) { |
---|
98 | fprintf(stderr, "Error in ply_read, aborting...\n"); |
---|
99 | exit(-1); |
---|
100 | } |
---|
101 | |
---|
102 | int nvp = 0; |
---|
103 | |
---|
104 | if (ply_is_valid_property(ply, "vertex", vert_prop_x.name)) { |
---|
105 | vert_props[nvp] = vert_prop_x; |
---|
106 | vert_props[nvp].offset = voffset(x); nvp++; |
---|
107 | } |
---|
108 | |
---|
109 | if (ply_is_valid_property(ply, "vertex", vert_prop_y.name)) { |
---|
110 | vert_props[nvp] = vert_prop_y; |
---|
111 | vert_props[nvp].offset = voffset(y); nvp++; |
---|
112 | } |
---|
113 | |
---|
114 | if (ply_is_valid_property(ply, "vertex", vert_prop_z.name)) { |
---|
115 | vert_props[nvp] = vert_prop_z; |
---|
116 | vert_props[nvp].offset = voffset(z); nvp++; |
---|
117 | } |
---|
118 | |
---|
119 | if (ply_is_valid_property(ply, "vertex", vert_prop_nx.name)) { |
---|
120 | vert_props[nvp] = vert_prop_nx; |
---|
121 | vert_props[nvp].offset = voffset(nx); nvp++; |
---|
122 | } |
---|
123 | |
---|
124 | if (ply_is_valid_property(ply, "vertex", vert_prop_ny.name)) { |
---|
125 | vert_props[nvp] = vert_prop_ny; |
---|
126 | vert_props[nvp].offset = voffset(ny); nvp++; |
---|
127 | } |
---|
128 | |
---|
129 | if (ply_is_valid_property(ply, "vertex", vert_prop_nz.name)) { |
---|
130 | vert_props[nvp] = vert_prop_nz; |
---|
131 | vert_props[nvp].offset = voffset(nz); nvp++; |
---|
132 | } |
---|
133 | |
---|
134 | if (ply_is_valid_property(ply, "vertex", vert_prop_intens.name)) { |
---|
135 | vert_props[nvp] = vert_prop_intens; |
---|
136 | vert_props[nvp].offset = voffset(intensity); nvp++; |
---|
137 | } |
---|
138 | |
---|
139 | if (ply_is_valid_property(ply, "vertex", vert_prop_std_dev.name)) { |
---|
140 | vert_props[nvp] = vert_prop_std_dev; |
---|
141 | vert_props[nvp].offset = voffset(std_dev); nvp++; |
---|
142 | } |
---|
143 | |
---|
144 | int hasConfidence = 0; |
---|
145 | if (ply_is_valid_property(ply, "vertex", vert_prop_confidence.name)) { |
---|
146 | vert_props[nvp] = vert_prop_confidence; |
---|
147 | vert_props[nvp].offset = voffset(confidence); nvp++; |
---|
148 | hasConfidence = 1; |
---|
149 | } |
---|
150 | |
---|
151 | if (ply_is_valid_property(ply, "vertex", "diffuse_red") && |
---|
152 | ply_is_valid_property(ply, "vertex", "diffuse_green") && |
---|
153 | ply_is_valid_property(ply, "vertex", "diffuse_blue")) |
---|
154 | { |
---|
155 | vert_props[nvp] = vert_prop_diff_r; |
---|
156 | vert_props[nvp].offset = voffset(diff_r); nvp++; |
---|
157 | vert_props[nvp] = vert_prop_diff_g; |
---|
158 | vert_props[nvp].offset = voffset(diff_g); nvp++; |
---|
159 | vert_props[nvp] = vert_prop_diff_b; |
---|
160 | vert_props[nvp].offset = voffset(diff_b); nvp++; |
---|
161 | } |
---|
162 | |
---|
163 | num_vert_props = nvp; |
---|
164 | |
---|
165 | Mesh *mesh = new Mesh; |
---|
166 | Vertex *vert; |
---|
167 | Triangle *tri; |
---|
168 | PlyVertex plyVert; |
---|
169 | PlyFace plyFace; |
---|
170 | |
---|
171 | for (int i = 0; i < nelems; i++) { |
---|
172 | |
---|
173 | /* get the description of the first element */ |
---|
174 | elem_name = elist[i]; |
---|
175 | plist = ply_get_element_description |
---|
176 | (ply, elem_name, &num_elems, &nprops); |
---|
177 | |
---|
178 | /* if we're on vertex elements, read them in */ |
---|
179 | if (equal_strings ("vertex", elem_name)) { |
---|
180 | |
---|
181 | mesh->numVerts = num_elems; |
---|
182 | mesh->verts = new Vertex[mesh->numVerts]; |
---|
183 | |
---|
184 | /* set up for getting vertex elements */ |
---|
185 | ply_get_element_setup (ply, elem_name, num_vert_props, vert_props); |
---|
186 | |
---|
187 | /* grab all the vertex elements */ |
---|
188 | for (j = 0; j < mesh->numVerts; j++) { |
---|
189 | ply_get_element (ply, (void *) &plyVert); |
---|
190 | vert = &mesh->verts[j]; |
---|
191 | vert->coord.x = plyVert.x; |
---|
192 | vert->coord.y = plyVert.y; |
---|
193 | vert->coord.z = plyVert.z; |
---|
194 | vert->confidence = plyVert.confidence; |
---|
195 | vert->index = j; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | if (equal_strings ("face", elem_name)) { |
---|
200 | |
---|
201 | ply_get_element_setup (ply, elem_name, 1, face_props); |
---|
202 | |
---|
203 | mesh->numTris = num_elems; |
---|
204 | |
---|
205 | if (mesh->numTris == 0) |
---|
206 | continue; |
---|
207 | |
---|
208 | mesh->tris = new Triangle[mesh->numTris]; |
---|
209 | |
---|
210 | // Assumes vertices preceed faces in the file! |
---|
211 | |
---|
212 | for (j = 0; j < mesh->numTris; j++) { |
---|
213 | ply_get_element (ply, (void *) &plyFace); |
---|
214 | tri = &mesh->tris[j]; |
---|
215 | tri->vert1 = &mesh->verts[plyFace.verts[0]]; |
---|
216 | tri->vert2 = &mesh->verts[plyFace.verts[1]]; |
---|
217 | tri->vert3 = &mesh->verts[plyFace.verts[2]]; |
---|
218 | free(plyFace.verts); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | return mesh; |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | int |
---|
228 | writePlyFile(FILE *outFile, Mesh *mesh, int numVerts, int numTris) |
---|
229 | { |
---|
230 | int i, j; |
---|
231 | int nelems; |
---|
232 | char **elist; |
---|
233 | int file_type; |
---|
234 | float version; |
---|
235 | char *elem_name; |
---|
236 | int nprops, num_vert_props; |
---|
237 | int num_elems; |
---|
238 | PlyProperty **plist; |
---|
239 | int hasIntensity; |
---|
240 | int hasColor; |
---|
241 | int hasConfidence; |
---|
242 | int nvp; |
---|
243 | char *elem_names[] = {"vertex", "face"}; |
---|
244 | |
---|
245 | PlyFile *ply = ply_write(outFile, 2, elem_names, PLY_BINARY_BE); |
---|
246 | |
---|
247 | if (ply == NULL) |
---|
248 | return 0; |
---|
249 | |
---|
250 | |
---|
251 | nvp = 0; |
---|
252 | |
---|
253 | vert_props[nvp] = vert_prop_x; |
---|
254 | vert_props[nvp].offset = offsetof(PlyVertex,x); nvp++; |
---|
255 | vert_props[nvp] = vert_prop_y; |
---|
256 | vert_props[nvp].offset = offsetof(PlyVertex,y); nvp++; |
---|
257 | vert_props[nvp] = vert_prop_z; |
---|
258 | vert_props[nvp].offset = offsetof(PlyVertex,z); nvp++; |
---|
259 | |
---|
260 | vert_props[nvp] = vert_prop_confidence; |
---|
261 | vert_props[nvp].offset = offsetof(PlyVertex, confidence); nvp++; |
---|
262 | |
---|
263 | num_vert_props = nvp; |
---|
264 | |
---|
265 | face_props[0].offset = offsetof(PlyFace, verts); |
---|
266 | face_props[0].count_offset = offsetof(PlyFace, nverts); /* count offset */ |
---|
267 | |
---|
268 | ply_describe_element (ply, "vertex", numVerts, num_vert_props, vert_props); |
---|
269 | |
---|
270 | ply_describe_element (ply, "face", numTris, 1, face_props); |
---|
271 | |
---|
272 | ply_header_complete (ply); |
---|
273 | |
---|
274 | /* set up and write the vertex elements */ |
---|
275 | PlyVertex plyVert; |
---|
276 | Vertex *vert; |
---|
277 | |
---|
278 | ply_put_element_setup (ply, "vertex"); |
---|
279 | |
---|
280 | for (i = 0; i < mesh->numVerts; i++) { |
---|
281 | vert = &mesh->verts[i]; |
---|
282 | if (vert->index < 0) |
---|
283 | continue; |
---|
284 | |
---|
285 | plyVert.x = vert->coord.x; |
---|
286 | plyVert.y = vert->coord.y; |
---|
287 | plyVert.z = vert->coord.z; |
---|
288 | plyVert.confidence = vert->confidence; |
---|
289 | |
---|
290 | ply_put_element (ply, (void *) &plyVert); |
---|
291 | } |
---|
292 | |
---|
293 | PlyFace plyFace; |
---|
294 | Triangle *tri; |
---|
295 | int vertIndices[3]; |
---|
296 | |
---|
297 | ply_put_element_setup (ply, "face"); |
---|
298 | |
---|
299 | for (i = 0; i < mesh->numTris; i++) { |
---|
300 | tri = &mesh->tris[i]; |
---|
301 | // If the triangle has 3 unique points, and they |
---|
302 | // haven't been deleted by triedgecol, write it out... |
---|
303 | if (tri->vert1 != tri->vert2 && |
---|
304 | tri->vert1 != tri->vert3 && |
---|
305 | tri->vert2 != tri->vert3 && |
---|
306 | tri->vert1->index > -1 && |
---|
307 | tri->vert2->index > -1 && |
---|
308 | tri->vert3->index > -1) { |
---|
309 | |
---|
310 | plyFace.nverts = 3; |
---|
311 | vertIndices[0] = tri->vert1->index; |
---|
312 | vertIndices[1] = tri->vert2->index; |
---|
313 | vertIndices[2] = tri->vert3->index; |
---|
314 | plyFace.verts = (int *)vertIndices; |
---|
315 | |
---|
316 | ply_put_element (ply, (void *) &plyFace); |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | /* close the PLY file */ |
---|
321 | ply_close (ply); |
---|
322 | |
---|
323 | return 1; |
---|
324 | } |
---|
325 | |
---|
326 | |
---|