ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/objutil.h
Revision: 2.1
Committed: Mon Mar 30 18:28:35 2020 UTC (4 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Created robjutil tool to manipulate Wavefront .OBJ files

File Contents

# User Rev Content
1 greg 2.1 /* RCSid $Id$ */
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 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     /* Add a descriptive comment */
92     void addComment(Scene *sc, const char *comment);
93    
94     /* Clear comments */
95     void clearComments(Scene *sc);
96    
97     /* Write a .OBJ file, return # faces written or -1 on error */
98     int toOBJ(Scene *sc, FILE *fp);
99     int writeOBJ(Scene *sc, const char *fspec);
100    
101     /* Convert indicated faces to Radiance, return # written or -1 on error */
102     int toRadiance(Scene *sc, FILE *fp, int flreq, int flexc);
103     int writeRadiance(Scene *sc, const char *fspec,
104     int flreq, int flexc);
105    
106     /* Compute face area (and normal) */
107     double faceArea(const Scene *sc, const Face *f, Normal nrm);
108    
109     /* Eliminate duplicate vertices, return # joined */
110     int coalesceVertices(Scene *sc, double eps);
111    
112     /* Identify duplicate faces */
113     int findDuplicateFaces(Scene *sc);
114    
115     /* Delete indicated faces, return # deleted */
116     int deleteFaces(Scene *sc, int flreq, int flexc);
117    
118     /* Clear face selection */
119     void clearSelection(Scene *sc, int set);
120    
121     /* Invert face selection */
122     void invertSelection(Scene *sc);
123    
124     /* Count number of faces selected */
125     int numberSelected(Scene *sc);
126    
127     /* Select faces by object name (modifies current) */
128     void selectGroup(Scene *sc, const char *gname, int invert);
129    
130     /* Select faces by material name (modifies current) */
131     void selectMaterial(Scene *sc, const char *mname, int invert);
132    
133     /* Execute callback on indicated faces */
134     int foreachFace(Scene *sc, int (*cb)(Scene *, Face *, void *),
135     int flreq, int flexc, void *c_data);
136    
137     /* Remove texture coordinates from the indicated faces */
138     int removeTexture(Scene *sc, int flreq, int flexc);
139    
140     /* Remove surface normals from the indicated faces */
141     int removeNormals(Scene *sc, int flreq, int flexc);
142    
143     /* Change group for the indicated faces */
144     int changeGroup(Scene *sc, const char *gname,
145     int flreq, int flexc);
146    
147     /* Change material for the indicated faces */
148     int changeMaterial(Scene *sc, const char *mname,
149     int flreq, int flexc);
150    
151     /* Grab texture coord's/normals from another object via ray tracing */
152     #define GET_TEXTURE 01
153     #define GET_NORMALS 02
154     int traceSurface(Scene *sc, int flreq, int flexc,
155     const char *oct, int what);
156    
157     /* Free a scene */
158     void freeScene(Scene *sc);
159    
160     /* Find an existing name in a list of names */
161     int findName(const char *nm, const char **nmlist, int n);
162    
163     /* Verbose mode global */
164     extern int verbose;
165    
166     extern char *emalloc(unsigned int n);
167     extern char *ecalloc(unsigned int ne, unsigned int n);
168     extern char *erealloc(char *cp, unsigned int n);
169     extern void efree(char *cp);
170    
171     #define CHUNKSIZ 128 /* object allocation chunk size */
172    
173     #define chunk_alloc(typ, arr, nold) \
174     ((nold)%CHUNKSIZ ? (arr) : \
175     (typ *)erealloc((char *)(arr), sizeof(typ)*((nold)+CHUNKSIZ)))
176    
177     #ifdef __cplusplus
178     }
179     #endif
180    
181     #endif /* ! _OBJUTIL_H_ */