ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/ot/obj2mesh.c
Revision: 2.1
Committed: Tue Mar 11 17:08:55 2003 UTC (21 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
First working version of new "mesh" primitive, obj2mesh converter

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2     static const char RCSid[] = "$Id$";
3     #endif
4     /*
5     * Main program to compile a Wavefront .OBJ file into a Radiance mesh
6     */
7    
8     #include "copyright.h"
9     #include "standard.h"
10     #include "cvmesh.h"
11     #include "otypes.h"
12    
13     extern int o_face();
14    
15     int o_default() { return(O_MISS); }
16    
17     FUN ofun[NUMOTYPE] = INIT_OTYPE; /* needed for link resolution */
18    
19     char *progname; /* argv[0] */
20    
21     int nowarn = 0; /* supress warnings? */
22    
23     int objlim = 15; /* # of objects before split */
24    
25     int resolu = 16384; /* octree resolution limit */
26    
27     double mincusize; /* minimum cube size from resolu */
28    
29    
30     main(argc, argv) /* compile a .OBJ file into a mesh */
31     int argc;
32     char *argv[];
33     {
34     int i;
35    
36     progname = argv[0];
37     ofun[OBJ_FACE].funp = o_face;
38    
39     for (i = 1; i < argc && argv[i][0] == '-'; i++)
40     switch (argv[i][1]) {
41     case 'n': /* set limit */
42     objlim = atoi(argv[++i]);
43     break;
44     case 'r': /* resolution limit */
45     resolu = atoi(argv[++i]);
46     break;
47     case 'w': /* supress warnings */
48     nowarn = 1;
49     break;
50     default:
51     sprintf(errmsg, "unknown option: '%s'", argv[i]);
52     error(USER, errmsg);
53     break;
54     }
55     /* initialize mesh */
56     cvinit(i==argc-2 ? argv[i+1] : "<stdout>");
57    
58     if (i == argc) /* read .OBJ file into triangles */
59     wfreadobj(NULL);
60     else
61     wfreadobj(argv[i]);
62    
63     cvmeshbounds(); /* set octree boundaries */
64    
65     if (i == argc-2) /* open output file */
66     if (freopen(argv[i+1], "w", stdout) == NULL)
67     error(SYSTEM, "cannot open output file");
68     #ifdef MSDOS
69     setmode(fileno(stdout), O_BINARY);
70     #endif
71     newheader("RADIANCE", stdout); /* new binary file header */
72     printargs(i<argc ? i+1 : argc, argv, stdout);
73     fputformat(MESHFMT, stdout);
74     fputc('\n', stdout);
75    
76     mincusize = ourmesh->mcube.cusize / resolu - FTINY;
77    
78     for (i = 0; i < nobjects; i++) /* add triangles to octree */
79     addface(&ourmesh->mcube, i);
80    
81     /* optimize octree */
82     ourmesh->mcube.cutree = combine(ourmesh->mcube.cutree);
83    
84     if (ourmesh->mcube.cutree == EMPTY)
85     error(WARNING, "mesh is empty");
86    
87     cvmesh(); /* convert mesh and leaf nodes */
88    
89     writemesh(ourmesh, stdout); /* write mesh to output */
90    
91     printmeshstats(ourmesh, stderr);
92    
93     quit(0);
94     }
95    
96    
97     void
98     quit(code) /* exit program */
99     int code;
100     {
101     exit(code);
102     }
103    
104    
105     void
106     cputs() /* interactive error */
107     {
108     /* referenced, but not used */
109     }
110    
111    
112     void
113     wputs(s) /* warning message */
114     char *s;
115     {
116     if (!nowarn)
117     eputs(s);
118     }
119    
120    
121     void
122     eputs(s) /* put string to stderr */
123     register char *s;
124     {
125     static int inln = 0;
126    
127     if (!inln++) {
128     fputs(progname, stderr);
129     fputs(": ", stderr);
130     }
131     fputs(s, stderr);
132     if (*s && s[strlen(s)-1] == '\n')
133     inln = 0;
134     }
135    
136    
137     addface(cu, obj) /* add a face to a cube */
138     register CUBE *cu;
139     OBJECT obj;
140     {
141     CUBE cukid;
142     OCTREE ot;
143     OBJECT oset[MAXSET+1];
144     register int i, j;
145    
146     if (o_face(objptr(obj), cu) == O_MISS)
147     return;
148    
149     if (istree(cu->cutree)) {
150     /* do children */
151     cukid.cusize = cu->cusize * 0.5;
152     for (i = 0; i < 8; i++) {
153     cukid.cutree = octkid(cu->cutree, i);
154     for (j = 0; j < 3; j++) {
155     cukid.cuorg[j] = cu->cuorg[j];
156     if ((1<<j) & i)
157     cukid.cuorg[j] += cukid.cusize;
158     }
159     addface(&cukid, obj);
160     octkid(cu->cutree, i) = cukid.cutree;
161     }
162     return;
163     }
164     if (isempty(cu->cutree)) {
165     /* singular set */
166     oset[0] = 1; oset[1] = obj;
167     cu->cutree = fullnode(oset);
168     return;
169     }
170     /* add to full node */
171     objset(oset, cu->cutree);
172     cukid.cusize = cu->cusize * 0.5;
173    
174     if (oset[0] < objlim || cukid.cusize < mincusize) {
175     /* add to set */
176     if (oset[0] >= MAXSET) {
177     sprintf(errmsg, "set overflow in addface (%s)",
178     objptr(obj)->oname);
179     error(INTERNAL, errmsg);
180     }
181     insertelem(oset, obj);
182     cu->cutree = fullnode(oset);
183     return;
184     }
185     /* subdivide cube */
186     if ((ot = octalloc()) == EMPTY)
187     error(SYSTEM, "out of octree space");
188     /* assign subcubes */
189     for (i = 0; i < 8; i++) {
190     cukid.cutree = EMPTY;
191     for (j = 0; j < 3; j++) {
192     cukid.cuorg[j] = cu->cuorg[j];
193     if ((1<<j) & i)
194     cukid.cuorg[j] += cukid.cusize;
195     }
196     for (j = 1; j <= oset[0]; j++)
197     addface(&cukid, oset[j]);
198     addface(&cukid, obj);
199     /* returned node */
200     octkid(ot, i) = cukid.cutree;
201     }
202     cu->cutree = ot;
203     }