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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "poly.h"
#include "polytok.h"
#include "vds.h"
#include "vector.h"
extern char *yytext;
extern FILE *yyin;
extern int pLineNo;
static char *curFileName;
/* Prototypes. */
void read_poly_head(int *nv, int *nf);
void read_poly_body(object *o);
int read_poly_vertex(vertex *v);
int read_poly_face(face *f);
int yylex(void);
/* Routine prints an error message in the appropriate format. */
void poly_error(char *msg) {
fprintf(stderr,"[%s]:%d %s.\n",curFileName,pLineNo,msg);
exit(1);
}
/* Read the .poly file given by fname and return a single object. */
void read_poly(char *fname, object *o) {
int i;
/* Open the file... */
if (fname == NULL) {
fprintf(stderr,"Filename param was not initialized in read_poly!\n");
exit(1);
} else {
curFileName = fname;
pLineNo = 1;
}
if ( (yyin = fopen(fname,"r")) == NULL ) {
poly_error("Could not open file for reading");
}
/* Read the header. */
read_poly_head(&o->nv,&o->nf);
if ( (o->nv > 0) && (o->nf > 0) ) {
/* Allocate some memory. */
o->v = (vertex *)malloc(sizeof(vertex) * o->nv);
o->f = (face *)malloc(sizeof(face) * o->nf);
if ( (o->v == NULL) || (o->f == NULL) ) {
poly_error("Couldn't allocate enough memory for verts and faces");
}
/* Read the rest of the file. */
read_poly_body(o);
} else {
poly_error("Invalid vertex or face count");
}
}
void read_poly_head(int *nv, int *nf) {
if (yylex() == NUMVERT) {
if (yylex() == INT) {
*nv = atoi(yytext);
} else {
poly_error("Expected a positive integer after vertices:");
}
if (yylex() == NUMFACE) {
if (yylex() == INT) {
*nf = atoi(yytext);
} else {
poly_error("Expected a positive integer after faces:");
}
} else {
poly_error("Expected faces:\n");
}
} else {
poly_error("Expected vertices:\n");
}
}
void read_poly_body(object *o) {
int ttype, prevtok;
int iv, ifc, numtris, i, j, k;
float x, y, z;
iv = ifc = numtris = 0;
prevtok = -1;
/* Read lines until we get the EOF. */
while(1) {
if (prevtok == -1) {
ttype = yylex();
} else {
ttype = prevtok;
}
if (ttype == VERT) {
if (iv == o->nv) {
poly_error("There are more vertices than indicated by the vertices: directive");
}
prevtok = read_poly_vertex(&(o->v[iv]));
iv++;
} else if (ttype == FACE) {
if (ifc == o->nf) {
poly_error("There are more faces than indicated by the faces: directive");
}
prevtok = read_poly_face(&(o->f[ifc]));
numtris += o->f[ifc].n - 2;
ifc++;
} else if (ttype == PEOF) {
break;
} else {
poly_error("Invalid .poly file syntax");
}
}
if (iv != o->nv || ifc != o->nf) {
poly_error("Number of vertices or faces is less than specified");
}
/* Convert faces to triangles here. */
o->nt = numtris;
if ((o->t = (triangle *)malloc(sizeof(triangle)*numtris)) == NULL) {
poly_error("Could not allocate triangle array");
}
/* Iterate through faces, building triangles from CONVEX faces. */
k = 0;
for(i=0; i < o->nf; i++) {
for(j=0; j < (o->f[i].n - 2); j++) {
o->t[k].verts[0] = o->f[i].verts[0];
o->t[k].verts[1] = o->f[i].verts[j+1];
o->t[k].verts[2] = o->f[i].verts[j+2];
/* Compute normal for this triangle. */
kTriNormal(o,&o->t[k]);
k++;
}
}
/* Generate the vertex tree */
generateVertexTree(o);
}
/* Read a vertex. Returns type of last token read. */
int read_poly_vertex(vertex *v) {
int ttype;
int i;
v->hasNormal = 0;
for(i=0;i<3;i++) {
ttype = yylex();
if (ttype == INT || ttype == FLOAT) {
v->coord[i] = atof(yytext);
} else if (ttype == PEOF) {
poly_error("Premature end of file");
} else {
poly_error("Invalid vector specification");
}
}
ttype = yylex();
if (ttype == VERT || ttype == FACE) {
return ttype;
} else if (!(ttype == INT || ttype == FLOAT)) {
poly_error("I don't know what just happened, but your file is foobar");
}
v->hasNormal = 1;
for(i=0;i<3;i++) {
if (i > 0) ttype = yylex();
if (ttype == INT || ttype == FLOAT) {
v->normal[i] = atof(yytext);
} else if (ttype == PEOF) {
poly_error("Premature end of file");
} else {
poly_error("Invalid normal specification");
}
}
return yylex();
}
/* Read a face. Returns type of last token read. */
int read_poly_face(face *f) {
int ttype;
int queue[20];
int i;
i = 0;
while(1) {
ttype = yylex();
if (ttype == INT) {
if (i == 20) {
poly_error("Cannot handle faces with more than 20 sides");
} else {
queue[i++] = atoi(yytext);
}
} else if (ttype == PEOF || ttype == VERT || ttype == FACE) {
break;
} else {
poly_error("Invalid face syntax");
}
}
/* allocate memory for face vertex indices. */
if (i < 3) {
poly_error("Faces must have 3 or more vertices");
}
f->n = i;
if ((f->verts = (int *)malloc(sizeof(int)*f->n)) == NULL) {
poly_error("Could not allocate memory for vertices in face object");
}
for(i=0;i<f->n;i++) {
f->verts[i] = queue[i];
}
return ttype;
}
/***************************************************************************\
Copyright 1999 The University of Virginia.
All Rights Reserved.
Permission to use, copy, modify and distribute this software and its
documentation without fee, and without a written agreement, is
hereby granted, provided that the above copyright notice and the
complete text of this comment appear in all copies, and provided that
the University of Virginia and the original authors are credited in
any publications arising from the use of this software.
IN NO EVENT SHALL THE UNIVERSITY OF VIRGINIA
OR THE AUTHOR OF THIS SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
DOCUMENTATION, EVEN IF THE UNIVERSITY OF VIRGINIA AND/OR THE
AUTHOR OF THIS SOFTWARE HAVE BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
The author of the vdslib software library may be contacted at:
US Mail: Dr. David Patrick Luebke
Department of Computer Science
Thornton Hall, University of Virginia
Charlottesville, VA 22903
Phone: (804)924-1021
EMail: luebke@cs.virginia.edu
\*****************************************************************************/
|