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