Skip to content

Commit

Permalink
feat: Added getVertexNeighbors() call to find neighboring edges/vertices
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregungory committed Apr 23, 2021
1 parent c6dd277 commit 624057e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
52 changes: 51 additions & 1 deletion src/common/objutil.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef lint
static const char RCSid[] = "$Id: objutil.c,v 2.18 2021/04/09 15:26:41 greg Exp $";
static const char RCSid[] = "$Id: objutil.c,v 2.19 2021/04/23 18:31:45 greg Exp $";
#endif
/*
* Basic .OBJ scene handling routines.
Expand Down Expand Up @@ -700,6 +700,56 @@ addFace(Scene *sc, VNDX vid[], int nv)
return(f);
}

/* Get neighbor vertices: malloc array with valence in index[0] */
int *
getVertexNeighbors(Scene *sc, int vid)
{
int *varr;
Face *f;
int j=0;

if (sc == NULL || (vid < 0) | (vid >= sc->nverts) ||
(f = sc->vert[vid].vflist) == NULL)
return(NULL);

varr = (int *)emalloc(sizeof(int)*CHUNKSIZ);
varr[0] = 0; /* add unique neighbors */
for ( ; f != NULL; f = f->v[j].fnext) {
int i, cvi;
for (j = f->nv; j--; ) /* find ourself in poly */
if (f->v[j].vid == vid)
break;
if (j < 0) /* this is an error */
break;
if (f->nv < 3) /* also never happens */
continue;
/* check previous neighbor */
cvi = f->v[ (j > 0) ? j-1 : f->nv-1 ].vid;
for (i = varr[0]+1; --i; ) /* making sure not in list */
if (varr[i] == cvi)
break;
if (!i) { /* new neighbor? */
varr = chunk_alloc(int, varr, varr[0]+1);
varr[++varr[0]] = cvi;
}
/* check next neighbor */
cvi = f->v[ (j < f->nv-1) ? j+1 : 0 ].vid;
for (i = varr[0]+1; --i; )
if (varr[i] == cvi)
break;
if (!i) {
varr = chunk_alloc(int, varr, varr[0]+1);
varr[++varr[0]] = cvi;
}
}
if (!varr[0]) {
efree((char *)varr); /* something went awry */
return(NULL);
}
/* tighten array & return */
return((int *)erealloc((char *)varr, sizeof(int)*(varr[0]+1)));
}

/* Callback for growBoundingBox() */
static int
addBBox(Scene *sc, Face *f, void *p)
Expand Down
10 changes: 7 additions & 3 deletions src/common/objutil.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* RCSid $Id: objutil.h,v 2.15 2021/04/09 15:26:41 greg Exp $ */
/* RCSid $Id: objutil.h,v 2.16 2021/04/23 18:31:45 greg Exp $ */
/*
* Declarations for .OBJ file utility
*
Expand Down Expand Up @@ -181,6 +181,9 @@ 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);
Expand Down Expand Up @@ -208,10 +211,11 @@ extern void efree(char *cp);
#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 CHUNKSIZ 128 /* object allocation chunk size */
#define CHUNKBITS 7 /* object allocation chunk bits */
#define CHUNKSIZ (1<<CHUNKBITS) /* object allocation chunk size */

#define chunk_alloc(typ, arr, nold) \
((nold)%CHUNKSIZ ? (arr) : \
((nold)&(CHUNKSIZ-1) ? (arr) : \
(typ *)erealloc((char *)(arr), sizeof(typ)*((nold)+CHUNKSIZ)))

#ifdef __cplusplus
Expand Down

0 comments on commit 624057e

Please sign in to comment.