ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/objutil.h
Revision: 2.5
Committed: Fri May 1 18:55:34 2020 UTC (4 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +16 -1 lines
Log Message:
Made some useful routines public from readwfobj.c

File Contents

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