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