--- ray/src/common/objutil.h 2020/03/30 18:28:35 2.1 +++ ray/src/common/objutil.h 2022/03/14 19:51:19 2.18 @@ -1,4 +1,4 @@ -/* RCSid $Id: objutil.h,v 2.1 2020/03/30 18:28:35 greg Exp $ */ +/* RCSid $Id: objutil.h,v 2.18 2022/03/14 19:51:19 greg Exp $ */ /* * Declarations for .OBJ file utility * @@ -10,6 +10,10 @@ #ifndef _OBJUTIL_H_ #define _OBJUTIL_H_ +#ifdef __cplusplus +extern "C" { +#endif + #ifndef DUP_CHECK_REVERSE #define DUP_CHECK_REVERSE 1 /* eliminate flipped duplicates */ #endif @@ -17,12 +21,17 @@ #define POPEN_SUPPORT 1 /* support "!command" i/o */ #endif /* face flags */ -#define FACE_SELECTED 01 -#define FACE_DEGENERATE 02 -#define FACE_DUPLICATE 04 +#define FACE_DEGENERATE 01 +#define FACE_DUPLICATE 02 +#define FACE_SELECTED 04 +#define FACE_CUSTOM(n) (FACE_SELECTED<<(n)) +#define FACE_RESERVED (1<<15) struct Face; /* forward declaration */ +typedef int VNDX[3]; /* vertex indices (point,map,normal) */ + +/* Structure to hold vertex indices and link back to face list */ typedef struct { int vid; /* vertex id */ int tid; /* texture id */ @@ -75,104 +84,139 @@ typedef struct { int nfaces; /* count of faces */ } Scene; -#ifdef __cplusplus -extern "C" { -#endif - /* Allocate a new scene holder */ -Scene * newScene(void); +extern Scene * newScene(void); /* Add a .OBJ file to a scene */ -Scene * loadOBJ(Scene *sc, const char *fspec); +extern Scene * loadOBJ(Scene *sc, const char *fspec); -/* Duplicate a scene */ -Scene * dupScene(const Scene *sc); +/* Duplicate a scene, optionally selecting faces */ +extern Scene * dupScene(const Scene *sc, int flreq, int flexc); +/* Add one scene to another, not checking for redundancies */ +extern int addScene(Scene *scdst, const Scene *scsrc); + +/* Transform entire scene */ +extern int xfScene(Scene *sc, int xac, char *xav[]); +extern int xfmScene(Scene *sc, const char *xfm); + /* Add a descriptive comment */ -void addComment(Scene *sc, const char *comment); +extern void addComment(Scene *sc, const char *comment); +/* Find index for comment containing the given string (starting from n) */ +extern int findComment(Scene *sc, const char *match, int n); + /* Clear comments */ -void clearComments(Scene *sc); +extern void clearComments(Scene *sc); /* Write a .OBJ file, return # faces written or -1 on error */ -int toOBJ(Scene *sc, FILE *fp); -int writeOBJ(Scene *sc, const char *fspec); +extern int toOBJ(Scene *sc, FILE *fp); +extern int writeOBJ(Scene *sc, const char *fspec); /* Convert indicated faces to Radiance, return # written or -1 on error */ -int toRadiance(Scene *sc, FILE *fp, int flreq, int flexc); -int writeRadiance(Scene *sc, const char *fspec, +extern int toRadiance(Scene *sc, FILE *fp, int flreq, int flexc); +extern int writeRadiance(Scene *sc, const char *fspec, int flreq, int flexc); /* Compute face area (and normal) */ -double faceArea(const Scene *sc, const Face *f, Normal nrm); +extern double faceArea(const Scene *sc, const Face *f, Normal nrm); /* Eliminate duplicate vertices, return # joined */ -int coalesceVertices(Scene *sc, double eps); +extern int coalesceVertices(Scene *sc, double eps); /* Identify duplicate faces */ -int findDuplicateFaces(Scene *sc); +extern int findDuplicateFaces(Scene *sc); /* Delete indicated faces, return # deleted */ -int deleteFaces(Scene *sc, int flreq, int flexc); +extern int deleteFaces(Scene *sc, int flreq, int flexc); /* Clear face selection */ -void clearSelection(Scene *sc, int set); +extern void clearSelection(Scene *sc, int set); /* Invert face selection */ -void invertSelection(Scene *sc); +extern void invertSelection(Scene *sc); /* Count number of faces selected */ -int numberSelected(Scene *sc); +extern int numberSelected(Scene *sc); /* Select faces by object name (modifies current) */ -void selectGroup(Scene *sc, const char *gname, int invert); +extern void selectGroup(Scene *sc, const char *gname, int invert); /* Select faces by material name (modifies current) */ -void selectMaterial(Scene *sc, const char *mname, int invert); +extern void selectMaterial(Scene *sc, const char *mname, int invert); /* Execute callback on indicated faces */ -int foreachFace(Scene *sc, int (*cb)(Scene *, Face *, void *), +extern int foreachFace(Scene *sc, int (*cb)(Scene *, Face *, void *), int flreq, int flexc, void *c_data); /* Remove texture coordinates from the indicated faces */ -int removeTexture(Scene *sc, int flreq, int flexc); +extern int removeTexture(Scene *sc, int flreq, int flexc); /* Remove surface normals from the indicated faces */ -int removeNormals(Scene *sc, int flreq, int flexc); +extern int removeNormals(Scene *sc, int flreq, int flexc); /* Change group for the indicated faces */ -int changeGroup(Scene *sc, const char *gname, +extern int changeGroup(Scene *sc, const char *gname, int flreq, int flexc); /* Change material for the indicated faces */ -int changeMaterial(Scene *sc, const char *mname, +extern int changeMaterial(Scene *sc, const char *mname, int flreq, int flexc); -/* Grab texture coord's/normals from another object via ray tracing */ -#define GET_TEXTURE 01 -#define GET_NORMALS 02 -int traceSurface(Scene *sc, int flreq, int flexc, - const char *oct, int what); +/* Add a vertex to our scene, returning index */ +extern int addVertex(Scene *sc, double x, double y, double z); +/* Add a texture coordinate to our scene, returning index */ +extern int addTexture(Scene *sc, double u, double v); + +/* Add a surface normal to our scene, returning index */ +extern int addNormal(Scene *sc, double xn, double yn, double zn); + +/* Set current group (sc->lastgrp) to given ID */ +extern void setGroup(Scene *sc, const char *nm); + +/* Set current material (sc->lastmat) to given ID */ +extern void setMaterial(Scene *sc, const char *nm); + +/* Add a new face to our scene, using current group and material */ +extern Face * addFace(Scene *sc, VNDX vid[], int nv); + +/* Get neighbor vertices: malloc array with valence in index[0] */ +extern int * getVertexNeighbors(Scene *sc, int vid); + +/* Expand bounding box min & max (initialize bbox to all zeroes) */ +extern int growBoundingBox(Scene *sc, double bbox[2][3], + int flreq, int flexc); + +/* Convert all faces with > 3 vertices to triangles */ +extern int triangulateScene(Scene *sc); + +/* Delete unreferenced vertices, normals, texture coords */ +extern void deleteUnreferenced(Scene *sc); + /* Free a scene */ -void freeScene(Scene *sc); +extern void freeScene(Scene *sc); /* Find an existing name in a list of names */ -int findName(const char *nm, const char **nmlist, int n); +extern int findName(const char *nm, const char **nmlist, int n); /* Verbose mode global */ extern int verbose; -extern char *emalloc(unsigned int n); -extern char *ecalloc(unsigned int ne, unsigned int n); -extern char *erealloc(char *cp, unsigned int n); -extern void efree(char *cp); +extern void *emalloc(unsigned int n); +extern void *ecalloc(unsigned int ne, unsigned int n); +extern void *erealloc(void *ptr, unsigned int n); +extern void efree(void *ptr); -#define CHUNKSIZ 128 /* object allocation chunk size */ +#define getGroupID(sc,nm) findName(nm, (const char **)(sc)->grpname, (sc)->ngrps) +#define getMaterialID(sc,nm) findName(nm, (const char **)(sc)->matname, (sc)->nmats) +#define CHUNKBITS 7 /* object allocation chunk bits */ +#define CHUNKSIZ (1<