[37] | 1 | |
---|
| 2 | // |
---|
| 3 | // plycullmaxx.cc -- David Koller (dk@cs.stanford.edu), 8/16/98 |
---|
| 4 | // |
---|
| 5 | // This program takes 6 parameters (xmin,ymin,zmin,xmax,ymax,zmax) |
---|
| 6 | // defining an axis-aligned bounding box. A .ply file is read |
---|
| 7 | // from stdin, and is culled such that any face whose maximum-X- |
---|
| 8 | // coordinate vertex lies outside the bounding box is removed, along |
---|
| 9 | // with any unused vertices. The resulting culled model is written |
---|
| 10 | // to stdout as a .ply file. |
---|
| 11 | // |
---|
| 12 | // This program was written with the intention of being used with |
---|
| 13 | // the "vripsplit" program. |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include <stdio.h> |
---|
| 17 | #include <stdlib.h> |
---|
| 18 | #include <ply.h> |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | #define min(a,b) (((a) < (b)) ? (a) : (b)) |
---|
| 22 | #define max(a,b) (((a) > (b)) ? (a) : (b)) |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | typedef struct { |
---|
| 26 | float x,y,z; |
---|
| 27 | void *other_props; |
---|
| 28 | int num; // used first as reference count, then new id # |
---|
| 29 | } Vertex; |
---|
| 30 | |
---|
| 31 | typedef struct { |
---|
| 32 | unsigned char nverts; |
---|
| 33 | int *verts; |
---|
| 34 | void *other_props; |
---|
| 35 | int in; // boolean true if face is in bbox |
---|
| 36 | } Face; |
---|
| 37 | |
---|
| 38 | PlyProperty vert_props[] = { |
---|
| 39 | {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0}, |
---|
| 40 | {"y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0}, |
---|
| 41 | {"z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0}, |
---|
| 42 | }; |
---|
| 43 | |
---|
| 44 | PlyProperty face_props[] = { |
---|
| 45 | {"vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts), |
---|
| 46 | 1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)}, |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | // Variables for the PLY object |
---|
| 51 | int nelems; |
---|
| 52 | char **elist; |
---|
| 53 | int file_type; |
---|
| 54 | int numVerts; |
---|
| 55 | Vertex *verts; |
---|
| 56 | PlyOtherProp *vert_other; |
---|
| 57 | int numFaces; |
---|
| 58 | Face *faces; |
---|
| 59 | PlyOtherProp *face_other; |
---|
| 60 | PlyOtherElems *other_elements; |
---|
| 61 | int num_comments; |
---|
| 62 | char **comments; |
---|
| 63 | int num_obj_info; |
---|
| 64 | char **obj_info; |
---|
| 65 | |
---|
| 66 | int numOutputVerts; |
---|
| 67 | int numOutputFaces; |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | /////////////////////////////////////////////////////////////////////// |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | void ReadPlyFile() |
---|
| 74 | { |
---|
| 75 | PlyFile *ply; |
---|
| 76 | float version; |
---|
| 77 | int i,j; |
---|
| 78 | char *elem_name; |
---|
| 79 | int num_elems; |
---|
| 80 | int nprops; |
---|
| 81 | |
---|
| 82 | ply = ply_read (stdin, &nelems, &elist); |
---|
| 83 | ply_get_info (ply, &version, &file_type); |
---|
| 84 | |
---|
| 85 | for (i=0; i<nelems; ++i) { |
---|
| 86 | |
---|
| 87 | elem_name = elist[i]; |
---|
| 88 | ply_get_element_description (ply, elem_name, &num_elems, &nprops); |
---|
| 89 | |
---|
| 90 | if (equal_strings ("vertex", elem_name)) { |
---|
| 91 | |
---|
| 92 | numVerts = num_elems; |
---|
| 93 | verts = new Vertex[numVerts]; |
---|
| 94 | |
---|
| 95 | ply_get_property (ply, elem_name, &vert_props[0]); |
---|
| 96 | ply_get_property (ply, elem_name, &vert_props[1]); |
---|
| 97 | ply_get_property (ply, elem_name, &vert_props[2]); |
---|
| 98 | vert_other = ply_get_other_properties (ply, elem_name, |
---|
| 99 | offsetof(Vertex,other_props)); |
---|
| 100 | |
---|
| 101 | for (j=0; j<num_elems; ++j) |
---|
| 102 | ply_get_element (ply, (void *) &verts[j]); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | else if (equal_strings ("face", elem_name)) { |
---|
| 106 | |
---|
| 107 | numFaces = num_elems; |
---|
| 108 | faces = new Face[numFaces]; |
---|
| 109 | |
---|
| 110 | ply_get_property (ply, elem_name, &face_props[0]); |
---|
| 111 | face_other = ply_get_other_properties (ply, elem_name, |
---|
| 112 | offsetof(Face,other_props)); |
---|
| 113 | |
---|
| 114 | for (j=0; j<num_elems; ++j) |
---|
| 115 | ply_get_element (ply, (void *) &faces[j]); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | else |
---|
| 119 | other_elements=ply_get_other_element(ply, elem_name, num_elems); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | comments = ply_get_comments (ply, &num_comments); |
---|
| 123 | obj_info = ply_get_obj_info (ply, &num_obj_info); |
---|
| 124 | |
---|
| 125 | ply_close (ply); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | void WritePlyFile() |
---|
| 130 | { |
---|
| 131 | PlyFile *ply; |
---|
| 132 | int i; |
---|
| 133 | |
---|
| 134 | ply = ply_write (stdout, nelems, elist, file_type); |
---|
| 135 | |
---|
| 136 | ply_element_count (ply, "vertex", numOutputVerts); |
---|
| 137 | ply_describe_property (ply, "vertex", &vert_props[0]); |
---|
| 138 | ply_describe_property (ply, "vertex", &vert_props[1]); |
---|
| 139 | ply_describe_property (ply, "vertex", &vert_props[2]); |
---|
| 140 | ply_describe_other_properties(ply,vert_other,offsetof(Vertex,other_props)); |
---|
| 141 | |
---|
| 142 | ply_element_count (ply, "face", numOutputFaces); |
---|
| 143 | ply_describe_property (ply, "face", &face_props[0]); |
---|
| 144 | ply_describe_other_properties(ply,face_other,offsetof(Face,other_props)); |
---|
| 145 | |
---|
| 146 | ply_describe_other_elements (ply, other_elements); |
---|
| 147 | |
---|
| 148 | for (i = 0; i < num_comments; i++) |
---|
| 149 | ply_put_comment (ply, comments[i]); |
---|
| 150 | |
---|
| 151 | for (i = 0; i < num_obj_info; i++) |
---|
| 152 | ply_put_obj_info (ply, obj_info[i]); |
---|
| 153 | |
---|
| 154 | ply_header_complete (ply); |
---|
| 155 | |
---|
| 156 | ply_put_element_setup (ply, "vertex"); |
---|
| 157 | for (i = 0; i < numVerts; i++) |
---|
| 158 | if (verts[i].num >= 0) |
---|
| 159 | ply_put_element (ply, (void *) &verts[i]); |
---|
| 160 | |
---|
| 161 | ply_put_element_setup (ply, "face"); |
---|
| 162 | for (i = 0; i < numFaces; i++) |
---|
| 163 | if (faces[i].in) |
---|
| 164 | ply_put_element (ply, (void *) &faces[i]); |
---|
| 165 | |
---|
| 166 | ply_put_other_elements (ply); |
---|
| 167 | |
---|
| 168 | ply_close (ply); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | int Keep_Face(float xmin, float ymin, float zmin, |
---|
| 173 | float xmax, float ymax, float zmax, Face *face) |
---|
| 174 | { |
---|
| 175 | float maxx; // The maximum X-coord of the face |
---|
| 176 | Vertex *maxvert; // The vertex with maximum X-coord |
---|
| 177 | int i; |
---|
| 178 | |
---|
| 179 | if (face->nverts <= 0) |
---|
| 180 | return 0; |
---|
| 181 | |
---|
| 182 | maxvert = &verts[face->verts[0]]; |
---|
| 183 | maxx = maxvert->x; |
---|
| 184 | |
---|
| 185 | for (i=1; i<face->nverts; ++i) |
---|
| 186 | if (verts[face->verts[i]].x > maxx) { |
---|
| 187 | maxvert = &verts[face->verts[i]]; |
---|
| 188 | maxx = maxvert->x; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | if (maxvert->x > xmax) return 0; |
---|
| 192 | if (maxvert->x < xmin) return 0; |
---|
| 193 | if (maxvert->y > ymax) return 0; |
---|
| 194 | if (maxvert->y < ymin) return 0; |
---|
| 195 | if (maxvert->z > zmax) return 0; |
---|
| 196 | if (maxvert->z < zmin) return 0; |
---|
| 197 | |
---|
| 198 | return 1; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | int |
---|
| 203 | main(int argc, char *argv[]) |
---|
| 204 | { |
---|
| 205 | float x1, y1, z1, x2, y2, z2; |
---|
| 206 | float xmin, ymin, zmin; |
---|
| 207 | float xmax, ymax, zmax; |
---|
| 208 | int i, j; |
---|
| 209 | float epsilon = 0; |
---|
| 210 | |
---|
| 211 | if (argc != 7 && argc != 8) { |
---|
| 212 | fprintf(stderr, "USAGE: %s <xmin> <ymin> <zmin> <xmax> <ymax> <zmax> [epsilon] < in.ply > out.ply\n", argv[0]); |
---|
| 213 | fprintf(stderr, "Where it will clip all triangles beyond epsilon\n"); |
---|
| 214 | fprintf(stderr, "of the bounding box.\n"); |
---|
| 215 | exit(-1); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | x1 = atof(argv[1]); |
---|
| 219 | y1 = atof(argv[2]); |
---|
| 220 | z1 = atof(argv[3]); |
---|
| 221 | x2 = atof(argv[4]); |
---|
| 222 | y2 = atof(argv[5]); |
---|
| 223 | z2 = atof(argv[6]); |
---|
| 224 | |
---|
| 225 | if (argc == 8) { |
---|
| 226 | epsilon = atof(argv[7]); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | xmin = min(x1,x2) - epsilon; |
---|
| 230 | xmax = max(x1,x2) + epsilon; |
---|
| 231 | ymin = min(y1,y2) - epsilon; |
---|
| 232 | ymax = max(y1,y2) + epsilon; |
---|
| 233 | zmin = min(z1,z2) - epsilon; |
---|
| 234 | zmax = max(z1,z2) + epsilon; |
---|
| 235 | |
---|
| 236 | ReadPlyFile(); |
---|
| 237 | |
---|
| 238 | for (i=0; i<numVerts; ++i) |
---|
| 239 | verts[i].num = 0; |
---|
| 240 | |
---|
| 241 | for (i=0; i<numFaces; ++i) { |
---|
| 242 | |
---|
| 243 | if (Keep_Face(xmin,ymin,zmin,xmax,ymax,zmax,&faces[i])) { |
---|
| 244 | |
---|
| 245 | faces[i].in = 1; |
---|
| 246 | |
---|
| 247 | // Increment reference counts of relevant vertexes |
---|
| 248 | |
---|
| 249 | for (j=0; j<faces[i].nverts; ++j) |
---|
| 250 | ++(verts[faces[i].verts[j]].num); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | else |
---|
| 254 | faces[i].in = 0; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | // Count up the number of verts and faces to be output, |
---|
| 258 | // and update the marking data for valid verts and faces |
---|
| 259 | |
---|
| 260 | for (i=0, numOutputVerts=0; i<numVerts; ++i) |
---|
| 261 | if (verts[i].num > 0) |
---|
| 262 | verts[i].num = numOutputVerts++; |
---|
| 263 | else |
---|
| 264 | verts[i].num = -1; |
---|
| 265 | |
---|
| 266 | for (i=0, numOutputFaces=0; i<numFaces; ++i) |
---|
| 267 | if (faces[i].in) { |
---|
| 268 | |
---|
| 269 | ++numOutputFaces; |
---|
| 270 | |
---|
| 271 | for (j=0; j<faces[i].nverts; ++j) |
---|
| 272 | faces[i].verts[j] = verts[faces[i].verts[j]].num; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | WritePlyFile(); |
---|
| 276 | } |
---|
| 277 | |
---|