ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/objutil.h
Revision: 2.13
Committed: Fri Mar 12 03:59:25 2021 UTC (3 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.12: +9 -5 lines
Log Message:
feat: added routine to triangulate .OBJ scene in memory

File Contents

# Content
1 /* RCSid $Id: objutil.h,v 2.12 2021/03/03 18:53:08 greg Exp $ */
2 /*
3 * Declarations for .OBJ file utility
4 *
5 * Include after <stdio.h>
6 *
7 * Created by Greg Ward on Wed Feb 11 2004.
8 */
9
10 #ifndef _OBJUTIL_H_
11 #define _OBJUTIL_H_
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 #ifndef DUP_CHECK_REVERSE
18 #define DUP_CHECK_REVERSE 1 /* eliminate flipped duplicates */
19 #endif
20 #ifndef POPEN_SUPPORT
21 #define POPEN_SUPPORT 1 /* support "!command" i/o */
22 #endif
23 /* face flags */
24 #define FACE_DEGENERATE 01
25 #define FACE_DUPLICATE 02
26 #define FACE_SELECTED 04
27 #define FACE_CUSTOM(n) (FACE_SELECTED<<(n))
28 #define FACE_RESERVED (1<<15)
29
30 struct Face; /* forward declaration */
31
32 typedef int VNDX[3]; /* vertex indices (point,map,normal) */
33
34 /* Structure to hold vertex indices and link back to face list */
35 typedef struct {
36 int vid; /* vertex id */
37 int tid; /* texture id */
38 int nid; /* normal id */
39 struct Face *fnext; /* next in vertex face list */
40 } VertEnt;
41
42 /* Structure to hold face and its vertex references */
43 typedef struct Face {
44 struct Face *next; /* next face in main list */
45 short flags; /* face selected, etc */
46 short nv; /* number of vertices */
47 short grp; /* group/object index */
48 short mat; /* material index */
49 /* in cases where the same vertex appears twice, first has link */
50 VertEnt v[3]; /* vertex list (extends struct) */
51 } Face;
52
53 /* Structure to hold vertex */
54 typedef struct {
55 double p[3]; /* 3-D position */
56 Face *vflist; /* linked face list (no repeats) */
57 } Vertex;
58
59 /* Structure to hold texture coordinate */
60 typedef struct {
61 float u, v; /* 2-D local texture coordinate */
62 } TexCoord;
63
64 /* Array to hold surface normal */
65 typedef float Normal[3];
66
67 /* Structure to hold a loaded .OBJ file */
68 typedef struct {
69 char **descr; /* descriptive comments */
70 int ndescr; /* number of comments */
71 char **grpname; /* object/group name list */
72 int ngrps; /* number of group names */
73 int lastgrp; /* last group seen */
74 char **matname; /* material name list */
75 int nmats; /* number of materials */
76 int lastmat; /* last material seen */
77 Vertex *vert; /* vertex array */
78 int nverts; /* number of vertices */
79 TexCoord *tex; /* texture coordinate array */
80 int ntex; /* number of texture coord's */
81 Normal *norm; /* surface normal array */
82 int nnorms; /* number of surface normals */
83 Face *flist; /* linked face list */
84 int nfaces; /* count of faces */
85 } Scene;
86
87 /* Allocate a new scene holder */
88 Scene * newScene(void);
89
90 /* Add a .OBJ file to a scene */
91 Scene * loadOBJ(Scene *sc, const char *fspec);
92
93 /* Duplicate a scene, optionally selecting faces */
94 Scene * dupScene(const Scene *sc, int flreq, int flexc);
95
96 /* Transform entire scene */
97 int xfScene(Scene *sc, int xac, char *xav[]);
98 int xfmScene(Scene *sc, const char *xfm);
99
100 /* Add a descriptive comment */
101 void addComment(Scene *sc, const char *comment);
102
103 /* Find index for comment containing the given string (starting from n) */
104 int findComment(Scene *sc, const char *match, int n);
105
106 /* Clear comments */
107 void clearComments(Scene *sc);
108
109 /* Write a .OBJ file, return # faces written or -1 on error */
110 int toOBJ(Scene *sc, FILE *fp);
111 int writeOBJ(Scene *sc, const char *fspec);
112
113 /* Convert indicated faces to Radiance, return # written or -1 on error */
114 int toRadiance(Scene *sc, FILE *fp, int flreq, int flexc);
115 int writeRadiance(Scene *sc, const char *fspec,
116 int flreq, int flexc);
117
118 /* Compute face area (and normal) */
119 double faceArea(const Scene *sc, const Face *f, Normal nrm);
120
121 /* Eliminate duplicate vertices, return # joined */
122 int coalesceVertices(Scene *sc, double eps);
123
124 /* Identify duplicate faces */
125 int findDuplicateFaces(Scene *sc);
126
127 /* Delete indicated faces, return # deleted */
128 int deleteFaces(Scene *sc, int flreq, int flexc);
129
130 /* Clear face selection */
131 void clearSelection(Scene *sc, int set);
132
133 /* Invert face selection */
134 void invertSelection(Scene *sc);
135
136 /* Count number of faces selected */
137 int numberSelected(Scene *sc);
138
139 /* Select faces by object name (modifies current) */
140 void selectGroup(Scene *sc, const char *gname, int invert);
141
142 /* Select faces by material name (modifies current) */
143 void selectMaterial(Scene *sc, const char *mname, int invert);
144
145 /* Execute callback on indicated faces */
146 int foreachFace(Scene *sc, int (*cb)(Scene *, Face *, void *),
147 int flreq, int flexc, void *c_data);
148
149 /* Remove texture coordinates from the indicated faces */
150 int removeTexture(Scene *sc, int flreq, int flexc);
151
152 /* Remove surface normals from the indicated faces */
153 int removeNormals(Scene *sc, int flreq, int flexc);
154
155 /* Change group for the indicated faces */
156 int changeGroup(Scene *sc, const char *gname,
157 int flreq, int flexc);
158
159 /* Change material for the indicated faces */
160 int changeMaterial(Scene *sc, const char *mname,
161 int flreq, int flexc);
162
163 /* Add a vertex to our scene, returning index */
164 int addVertex(Scene *sc, double x, double y, double z);
165
166 /* Add a texture coordinate to our scene, returning index */
167 int addTexture(Scene *sc, double u, double v);
168
169 /* Add a surface normal to our scene, returning index */
170 int addNormal(Scene *sc, double xn, double yn, double zn);
171
172 /* Set current group (sc->lastgrp) to given ID */
173 void setGroup(Scene *sc, const char *nm);
174
175 /* Set current material (sc->lastmat) to given ID */
176 void setMaterial(Scene *sc, const char *nm);
177
178 /* Add a new face to our scene, using current group and material */
179 Face * addFace(Scene *sc, VNDX vid[], int nv);
180
181 /* Convert all faces with > 3 vertices to triangles */
182 int triangulateScene(Scene *sc);
183
184 /* Delete unreferenced vertices, normals, texture coords */
185 void deleteUnreferenced(Scene *sc);
186
187 /* Free a scene */
188 void freeScene(Scene *sc);
189
190 /* Find an existing name in a list of names */
191 int findName(const char *nm, const char **nmlist, int n);
192
193 /* Verbose mode global */
194 extern int verbose;
195
196 extern char *emalloc(unsigned int n);
197 extern char *ecalloc(unsigned int ne, unsigned int n);
198 extern char *erealloc(char *cp, unsigned int n);
199 extern void efree(char *cp);
200
201 #define getGroupID(sc,nm) findName(nm, (const char **)(sc)->grpname, (sc)->ngrps)
202 #define getMaterialID(sc,nm) findName(nm, (const char **)(sc)->matname, (sc)->nmats)
203
204 #define CHUNKSIZ 128 /* object allocation chunk size */
205
206 #define chunk_alloc(typ, arr, nold) \
207 ((nold)%CHUNKSIZ ? (arr) : \
208 (typ *)erealloc((char *)(arr), sizeof(typ)*((nold)+CHUNKSIZ)))
209
210 #ifdef __cplusplus
211 }
212 #endif
213
214 #endif /* ! _OBJUTIL_H_ */