ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/objutil.h
Revision: 2.17
Committed: Sat Jan 15 16:57:46 2022 UTC (2 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +2 -2 lines
Log Message:
refactor: eliminated use of MEM_PTR and (char *) for untyped memory

File Contents

# User Rev Content
1 greg 2.17 /* RCSid $Id: objutil.h,v 2.16 2021/04/23 18:31:45 greg Exp $ */
2 greg 2.1 /*
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 greg 2.13 #ifdef __cplusplus
14     extern "C" {
15     #endif
16    
17 greg 2.1 #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 greg 2.10 #define FACE_DEGENERATE 01
25     #define FACE_DUPLICATE 02
26     #define FACE_SELECTED 04
27 greg 2.11 #define FACE_CUSTOM(n) (FACE_SELECTED<<(n))
28 greg 2.13 #define FACE_RESERVED (1<<15)
29 greg 2.1
30     struct Face; /* forward declaration */
31    
32 greg 2.6 typedef int VNDX[3]; /* vertex indices (point,map,normal) */
33    
34     /* Structure to hold vertex indices and link back to face list */
35 greg 2.1 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 greg 2.15 extern Scene * newScene(void);
89 greg 2.1
90     /* Add a .OBJ file to a scene */
91 greg 2.15 extern Scene * loadOBJ(Scene *sc, const char *fspec);
92 greg 2.1
93 greg 2.9 /* Duplicate a scene, optionally selecting faces */
94 greg 2.15 extern Scene * dupScene(const Scene *sc, int flreq, int flexc);
95 greg 2.1
96 greg 2.14 /* Add one scene to another, not checking for redundancies */
97 greg 2.15 extern int addScene(Scene *scdst, const Scene *scsrc);
98 greg 2.14
99 greg 2.2 /* Transform entire scene */
100 greg 2.15 extern int xfScene(Scene *sc, int xac, char *xav[]);
101     extern int xfmScene(Scene *sc, const char *xfm);
102 greg 2.2
103 greg 2.1 /* Add a descriptive comment */
104 greg 2.15 extern void addComment(Scene *sc, const char *comment);
105 greg 2.1
106 greg 2.8 /* Find index for comment containing the given string (starting from n) */
107 greg 2.15 extern int findComment(Scene *sc, const char *match, int n);
108 greg 2.8
109 greg 2.1 /* Clear comments */
110 greg 2.15 extern void clearComments(Scene *sc);
111 greg 2.1
112     /* Write a .OBJ file, return # faces written or -1 on error */
113 greg 2.15 extern int toOBJ(Scene *sc, FILE *fp);
114     extern int writeOBJ(Scene *sc, const char *fspec);
115 greg 2.1
116     /* Convert indicated faces to Radiance, return # written or -1 on error */
117 greg 2.15 extern int toRadiance(Scene *sc, FILE *fp, int flreq, int flexc);
118     extern int writeRadiance(Scene *sc, const char *fspec,
119 greg 2.1 int flreq, int flexc);
120    
121     /* Compute face area (and normal) */
122 greg 2.15 extern double faceArea(const Scene *sc, const Face *f, Normal nrm);
123 greg 2.1
124     /* Eliminate duplicate vertices, return # joined */
125 greg 2.15 extern int coalesceVertices(Scene *sc, double eps);
126 greg 2.1
127     /* Identify duplicate faces */
128 greg 2.15 extern int findDuplicateFaces(Scene *sc);
129 greg 2.1
130     /* Delete indicated faces, return # deleted */
131 greg 2.15 extern int deleteFaces(Scene *sc, int flreq, int flexc);
132 greg 2.1
133     /* Clear face selection */
134 greg 2.15 extern void clearSelection(Scene *sc, int set);
135 greg 2.1
136     /* Invert face selection */
137 greg 2.15 extern void invertSelection(Scene *sc);
138 greg 2.1
139     /* Count number of faces selected */
140 greg 2.15 extern int numberSelected(Scene *sc);
141 greg 2.1
142     /* Select faces by object name (modifies current) */
143 greg 2.15 extern void selectGroup(Scene *sc, const char *gname, int invert);
144 greg 2.1
145     /* Select faces by material name (modifies current) */
146 greg 2.15 extern void selectMaterial(Scene *sc, const char *mname, int invert);
147 greg 2.1
148     /* Execute callback on indicated faces */
149 greg 2.15 extern int foreachFace(Scene *sc, int (*cb)(Scene *, Face *, void *),
150 greg 2.1 int flreq, int flexc, void *c_data);
151    
152     /* Remove texture coordinates from the indicated faces */
153 greg 2.15 extern int removeTexture(Scene *sc, int flreq, int flexc);
154 greg 2.1
155     /* Remove surface normals from the indicated faces */
156 greg 2.15 extern int removeNormals(Scene *sc, int flreq, int flexc);
157 greg 2.1
158     /* Change group for the indicated faces */
159 greg 2.15 extern int changeGroup(Scene *sc, const char *gname,
160 greg 2.1 int flreq, int flexc);
161    
162     /* Change material for the indicated faces */
163 greg 2.15 extern int changeMaterial(Scene *sc, const char *mname,
164 greg 2.1 int flreq, int flexc);
165    
166 greg 2.7 /* Add a vertex to our scene, returning index */
167 greg 2.15 extern int addVertex(Scene *sc, double x, double y, double z);
168 greg 2.5
169 greg 2.7 /* Add a texture coordinate to our scene, returning index */
170 greg 2.15 extern int addTexture(Scene *sc, double u, double v);
171 greg 2.5
172 greg 2.7 /* Add a surface normal to our scene, returning index */
173 greg 2.15 extern int addNormal(Scene *sc, double xn, double yn, double zn);
174 greg 2.5
175 greg 2.7 /* Set current group (sc->lastgrp) to given ID */
176 greg 2.15 extern void setGroup(Scene *sc, const char *nm);
177 greg 2.5
178 greg 2.7 /* Set current material (sc->lastmat) to given ID */
179 greg 2.15 extern void setMaterial(Scene *sc, const char *nm);
180 greg 2.5
181 greg 2.7 /* Add a new face to our scene, using current group and material */
182 greg 2.15 extern Face * addFace(Scene *sc, VNDX vid[], int nv);
183    
184 greg 2.16 /* Get neighbor vertices: malloc array with valence in index[0] */
185     extern int * getVertexNeighbors(Scene *sc, int vid);
186    
187 greg 2.15 /* Expand bounding box min & max (initialize bbox to all zeroes) */
188     extern int growBoundingBox(Scene *sc, double bbox[2][3],
189     int flreq, int flexc);
190 greg 2.6
191 greg 2.13 /* Convert all faces with > 3 vertices to triangles */
192 greg 2.15 extern int triangulateScene(Scene *sc);
193 greg 2.13
194 greg 2.9 /* Delete unreferenced vertices, normals, texture coords */
195 greg 2.15 extern void deleteUnreferenced(Scene *sc);
196 greg 2.9
197 greg 2.1 /* Free a scene */
198 greg 2.15 extern void freeScene(Scene *sc);
199 greg 2.1
200     /* Find an existing name in a list of names */
201 greg 2.15 extern int findName(const char *nm, const char **nmlist, int n);
202 greg 2.1
203     /* Verbose mode global */
204     extern int verbose;
205    
206     extern char *emalloc(unsigned int n);
207     extern char *ecalloc(unsigned int ne, unsigned int n);
208     extern char *erealloc(char *cp, unsigned int n);
209 greg 2.12 extern void efree(char *cp);
210    
211     #define getGroupID(sc,nm) findName(nm, (const char **)(sc)->grpname, (sc)->ngrps)
212     #define getMaterialID(sc,nm) findName(nm, (const char **)(sc)->matname, (sc)->nmats)
213 greg 2.1
214 greg 2.16 #define CHUNKBITS 7 /* object allocation chunk bits */
215     #define CHUNKSIZ (1<<CHUNKBITS) /* object allocation chunk size */
216 greg 2.1
217     #define chunk_alloc(typ, arr, nold) \
218 greg 2.16 ((nold)&(CHUNKSIZ-1) ? (arr) : \
219 greg 2.17 (typ *)erealloc(arr, sizeof(typ)*((nold)+CHUNKSIZ)))
220 greg 2.1
221     #ifdef __cplusplus
222     }
223     #endif
224    
225     #endif /* ! _OBJUTIL_H_ */