source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/vrippack-0.31/src/plytools/plybbox.c @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 4.9 KB
Line 
1/*
2
3  Compute the axis aligned bounding box that fits around the
4  vertices within a ply file.
5
6  Brian Curless
7  June 1995
8
9  Steve Marschner, December 2000
10  Generalized to multiple input files.
11
12*/
13
14
15#include <ply.h>
16#include <stdlib.h>
17#include <strings.h>
18#include <math.h>
19#include <limits.h>
20#include <stdio.h>
21#include <float.h>
22
23#define MAX(a, b) (a) > (b) ? (a) : (b)
24#define MIN(a, b) (a) < (b) ? (a) : (b)
25
26typedef struct Vertex {
27    float x, y, z;
28    float diff_r, diff_g, diff_b;
29} Vertex;
30
31static char *elem_names[] = { 
32  "vertex",
33};
34
35static PlyProperty vert_props[] = { 
36  {"x", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0},
37  {"y", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0},
38  {"z", PLY_FLOAT, PLY_FLOAT, 0, 0, PLY_START_TYPE, PLY_START_TYPE, 0},
39};
40
41typedef struct BBox {
42    float minx, miny, minz, maxx, maxy, maxz;
43} BBox;
44
45
46PlyFile *readPlyFile(FILE *inFile, Vertex **pVerts, int *pNumVerts);
47void initbbox(BBox *b);
48void updatebbox(BBox *b, Vertex *verts, int numVerts);
49void printbbox(BBox *b);
50void printUsage();
51
52
53void
54printusage(char *progname)
55{
56  fprintf (stderr, "usage: %s [in.ply ...]\n", progname);
57  fprintf (stderr, "   or: %s < in.ply\n", progname);
58  exit (-1);
59}
60
61
62int
63main(int argc, char**argv)
64{   
65   Vertex *verts = NULL;
66   int numVerts;
67   char *s;
68   char *progname;
69   FILE *inFile = NULL;
70   BBox box;
71   
72   progname = argv[0];
73   
74   /* parse -flags */
75   while (--argc > 0 && (*++argv)[0]=='-') {
76      for (s = argv[0]+1; *s; s++)
77         switch (*s) {
78         default:
79             printusage(progname);
80             break;
81         }
82   }   
83   
84   initbbox(&box);
85
86   /* optional input files (if not, read stdin ) */
87   if (argc > 0) {
88       while (argc > 0 && *argv[0] != '-') {
89           inFile = fopen(argv[0], "r");
90           if (inFile == NULL) {
91               fprintf(stderr, "Error: Couldn't open input file %s\n", argv[0]);
92               printusage(progname);
93           }
94           argc --;
95           argv ++;
96
97           readPlyFile(inFile, &verts, &numVerts);
98
99           if (verts == NULL) {
100               fprintf(stderr, "Obtained no vertices from %s.\n", argv[0]);
101               exit(1);
102           }
103
104           updatebbox(&box, verts, numVerts);
105       } 
106   } else {
107       readPlyFile(stdin, &verts, &numVerts);
108
109       if (verts == NULL) {
110           fprintf(stderr, "Obtained no vertices from %s.\n", argv[0]);
111           exit(1);
112           }
113       
114       updatebbox(&box, verts, numVerts);
115   }
116
117   if (argc > 0) {
118     fprintf(stderr, "Error: Unhandled arg: %s\n", argv[0]);
119     printusage(progname);
120     exit(-1);
121   }
122   
123   printbbox(&box);
124   
125   exit(0);
126}
127
128
129void
130initbbox(BBox *b)
131{
132    b->minx = FLT_MAX;
133    b->miny = FLT_MAX;
134    b->minz = FLT_MAX;
135
136    b->maxx = -FLT_MAX;
137    b->maxy = -FLT_MAX;
138    b->maxz = -FLT_MAX;
139}
140
141
142void
143updatebbox(BBox *b, Vertex *verts, int numVerts)
144{
145    int i;
146
147    for(i = 0; i < numVerts; i++) {
148        b->minx = MIN(b->minx, verts[i].x);
149        b->miny = MIN(b->miny, verts[i].y);
150        b->minz = MIN(b->minz, verts[i].z);
151       
152        b->maxx = MAX(b->maxx, verts[i].x);
153        b->maxy = MAX(b->maxy, verts[i].y);
154        b->maxz = MAX(b->maxz, verts[i].z);     
155    }
156}
157
158
159void
160printbbox(BBox *b)
161{
162    printf("\n");
163    printf("%f %f %f\n", b->minx, b->miny, b->minz);
164    printf("%f %f %f\n", b->maxx, b->maxy, b->maxz);
165    printf("\n");
166}
167
168
169
170PlyFile *
171readPlyFile(FILE *inFile, Vertex **pVerts, int *pNumVerts)
172{
173    int i, j;
174    int nelems, numVerts;
175    char **elist;
176    int file_type;
177    float version;
178    char *elem_name;
179    int nprops, num_vert_props;
180    int num_elems;
181    PlyProperty **plist;
182    Vertex *verts;
183    PlyFile *ply;
184
185    vert_props[0].offset = offsetof(Vertex, x);
186    vert_props[1].offset = offsetof(Vertex, y);
187    vert_props[2].offset = offsetof(Vertex, z);
188    num_vert_props = 3;
189
190    ply  = ply_read (inFile, &nelems, &elist);
191    ply_get_info (ply, &version, &file_type);
192
193    if (!ply)
194        exit(1);
195
196    verts = NULL;
197
198    for (i = 0; i < nelems; i++) {
199
200        /* get the description of the first element */
201        elem_name = elist[i];
202        plist = ply_get_element_description
203            (ply, elem_name, &num_elems, &nprops);
204       
205        /* if we're on vertex elements, read them in */
206        if (equal_strings ("vertex", elem_name)) {
207           
208            numVerts = *pNumVerts = num_elems;
209            verts = (Vertex *)malloc(sizeof(Vertex)*numVerts);
210           
211            /* set up for getting vertex elements */
212            ply_get_element_setup (ply, elem_name, num_vert_props, vert_props);
213           
214            /* grab all the vertex elements */
215            for (j = 0; j < numVerts; j++)
216                ply_get_element (ply, (void *) &verts[j]);
217        }
218    }
219
220    if (*pVerts) free(*pVerts);
221    *pVerts = verts;
222
223    return ply;
224}
225
226
227void
228printUsage()
229{
230    fprintf(stderr, "\n");
231    fprintf(stderr, "plybbox <ply-file>\n");
232    fprintf(stderr, "\n");
233    fprintf(stderr, "  Plybbox prints the min and max x,y,z values of the vertices\n");
234    fprintf(stderr, "  in the Ply file.  The output is relatively unformated:\n");
235    fprintf(stderr, "\n");
236    fprintf(stderr, "     <minx> <miny> <minz>\n");
237    fprintf(stderr, "     <maxx> <maxy> <maxz>\n");
238    fprintf(stderr, "\n");
239}
240
Note: See TracBrowser for help on using the repository browser.