📄 chull.c
字号:
/*---------------------------------------------------------------------ConstructHull adds the vertices to the hull one at a time. The hullvertices are those in the list marked as onhull.---------------------------------------------------------------------*/void ConstructHull( void ){ tVertex v, vnext; int vol; bool changed; /* T if addition changes hull; not used. */ v = vertices; do { vnext = v->next; if ( !v->mark ) { v->mark = PROCESSED; changed = AddOne( v ); CleanUp( &vnext ); /* Pass down vnext in case it gets deleted. */ if ( check ) { fprintf(stderr,"ConstructHull: After Add of %d & Cleanup:\n", v->vnum); Checks(); } if ( debug ) PrintOut( v ); } v = vnext; } while ( v != vertices );}/*---------------------------------------------------------------------AddOne is passed a vertex. It first determines all faces visible from that point. If none are visible then the point is marked as not onhull. Next is a loop over edges. If both faces adjacent to an edgeare visible, then the edge is marked for deletion. If just one of theadjacent faces is visible then a new face is constructed.---------------------------------------------------------------------*/bool AddOne( tVertex p ){ tFace f; tEdge e, temp; int vol; bool vis = FALSE; if ( debug ) { fprintf(stderr, "AddOne: starting to add v%d.\n", p->vnum); PrintOut( vertices ); } /* Mark faces visible from p. */ f = faces; do { vol = VolumeSign( f, p ); if (debug) fprintf(stderr, "faddr: %6x paddr: %6x Vol = %d\n", f,p,vol); if ( vol < 0 ) { f->visible = VISIBLE; vis = TRUE; } f = f->next; } while ( f != faces ); /* If no faces are visible from p, then p is inside the hull. */ if ( !vis ) { p->onhull = !ONHULL; return FALSE; } /* Mark edges in interior of visible region for deletion. Erect a newface based on each border edge. */ e = edges; do { temp = e->next; if ( e->adjface[0]->visible && e->adjface[1]->visible ) /* e interior: mark for deletion. */ e->delete = REMOVED; else if ( e->adjface[0]->visible || e->adjface[1]->visible ) /* e border: make a new face. */ e->newface = MakeConeFace( e, p ); e = temp; } while ( e != edges ); return TRUE;}/*---------------------------------------------------------------------VolumeSign returns the sign of the volume of the tetrahedron determined by fand p. VolumeSign is +1 iff p is on the negative side of f,where the positive side is determined by the rh-rule. So the volume is positive if the ccw normal to f points outside the tetrahedron.The final fewer-multiplications form is due to Bob Williamson.---------------------------------------------------------------------*/int VolumeSign( tFace f, tVertex p ){ double vol; int voli; double ax, ay, az, bx, by, bz, cx, cy, cz; ax = f->vertex[0]->v[X] - p->v[X]; ay = f->vertex[0]->v[Y] - p->v[Y]; az = f->vertex[0]->v[Z] - p->v[Z]; bx = f->vertex[1]->v[X] - p->v[X]; by = f->vertex[1]->v[Y] - p->v[Y]; bz = f->vertex[1]->v[Z] - p->v[Z]; cx = f->vertex[2]->v[X] - p->v[X]; cy = f->vertex[2]->v[Y] - p->v[Y]; cz = f->vertex[2]->v[Z] - p->v[Z]; vol = ax * (by*cz - bz*cy) + ay * (bz*cx - bx*cz) + az * (bx*cy - by*cx); if ( debug ) { /* Compute the volume using integers for comparison. */ voli = Volumei( f, p ); fprintf(stderr,"Face=%6x; Vertex=%d: vol(int) = %d, vol(double) = %lf\n", f,p->vnum,voli,vol); } /* The volume should be an integer. */ if ( vol > 0.5 ) return 1; else if ( vol < -0.5 ) return -1; else return 0;}/*---------------------------------------------------------------------Same computation, but computes using ints, and returns the actual volume.---------------------------------------------------------------------*/int Volumei( tFace f, tVertex p ){ int vol; int ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz; ax = f->vertex[0]->v[X] - p->v[X]; ay = f->vertex[0]->v[Y] - p->v[Y]; az = f->vertex[0]->v[Z] - p->v[Z]; bx = f->vertex[1]->v[X] - p->v[X]; by = f->vertex[1]->v[Y] - p->v[Y]; bz = f->vertex[1]->v[Z] - p->v[Z]; cx = f->vertex[2]->v[X] - p->v[X]; cy = f->vertex[2]->v[Y] - p->v[Y]; cz = f->vertex[2]->v[Z] - p->v[Z]; vol = (ax * (by*cz - bz*cy) + ay * (bz*cx - bx*cz) + az * (bx*cy - by*cx)); return vol;}/*-------------------------------------------------------------------*/void PrintPoint( tVertex p ){ int i; for ( i = 0; i < 3; i++ ) printf("\t%d", p->v[i]); putchar('\n');}/*---------------------------------------------------------------------MakeConeFace makes a new face and two new edges between the edge and the point that are passed to it. It returns a pointer tothe new face.---------------------------------------------------------------------*/tFace MakeConeFace( tEdge e, tVertex p ){ tEdge new_edge[2]; tFace new_face; int i, j; /* Make two new edges (if don't already exist). */ for ( i=0; i < 2; ++i ) /* If the edge exists, copy it into new_edge. */ if ( !( new_edge[i] = e->endpts[i]->duplicate) ) { /* Otherwise (duplicate is NULL), MakeNullEdge. */ new_edge[i] = MakeNullEdge(); new_edge[i]->endpts[0] = e->endpts[i]; new_edge[i]->endpts[1] = p; e->endpts[i]->duplicate = new_edge[i]; } /* Make the new face. */ new_face = MakeNullFace(); new_face->edge[0] = e; new_face->edge[1] = new_edge[0]; new_face->edge[2] = new_edge[1]; MakeCcw( new_face, e, p ); /* Set the adjacent face pointers. */ for ( i=0; i < 2; ++i ) for ( j=0; j < 2; ++j ) /* Only one NULL link should be set to new_face. */ if ( !new_edge[i]->adjface[j] ) { new_edge[i]->adjface[j] = new_face; break; } return new_face;}/*---------------------------------------------------------------------MakeCcw puts the vertices in the face structure in counterclock wise order. We want to store the vertices in the same order as in the visible face. The third vertex is always p.Although no specific ordering of the edges of a face are usedby the code, the following condition is maintained for each face f:one of the two endpoints of f->edge[i] matches f->vertex[i]. But note that this does not imply that f->edge[i] is betweenf->vertex[i] and f->vertex[(i+1)%3]. (Thanks to Bob Williamson.)---------------------------------------------------------------------*/void MakeCcw( tFace f, tEdge e, tVertex p ){ tFace fv; /* The visible face adjacent to e */ int i; /* Index of e->endpoint[0] in fv. */ tEdge s; /* Temporary, for swapping */ if ( e->adjface[0]->visible ) fv = e->adjface[0]; else fv = e->adjface[1]; /* Set vertex[0] & [1] of f to have the same orientation as do the corresponding vertices of fv. */ for ( i=0; fv->vertex[i] != e->endpts[0]; ++i ) ; /* Orient f the same as fv. */ if ( fv->vertex[ (i+1) % 3 ] != e->endpts[1] ) { f->vertex[0] = e->endpts[1]; f->vertex[1] = e->endpts[0]; } else { f->vertex[0] = e->endpts[0]; f->vertex[1] = e->endpts[1]; SWAP( s, f->edge[1], f->edge[2] ); } /* This swap is tricky. e is edge[0]. edge[1] is based on endpt[0], edge[2] on endpt[1]. So if e is oriented "forwards," we need to move edge[1] to follow [0], because it precedes. */ f->vertex[2] = p;} /*---------------------------------------------------------------------MakeNullEdge creates a new cell and initializes all pointers to NULLand sets all flags to off. It returns a pointer to the empty cell.---------------------------------------------------------------------*/tEdge MakeNullEdge( void ){ tEdge e; NEW( e, tsEdge ); e->adjface[0] = e->adjface[1] = e->newface = NULL; e->endpts[0] = e->endpts[1] = NULL; e->delete = !REMOVED; ADD( edges, e ); return e;}/*--------------------------------------------------------------------MakeNullFace creates a new face structure and initializes all of itsflags to NULL and sets all the flags to off. It returns a pointerto the empty cell.---------------------------------------------------------------------*/tFace MakeNullFace( void ){ tFace f; int i; NEW( f, tsFace); for ( i=0; i < 3; ++i ) { f->edge[i] = NULL; f->vertex[i] = NULL; } f->visible = !VISIBLE; ADD( faces, f ); return f;}/*---------------------------------------------------------------------MakeFace creates a new face structure from three vertices (in ccworder). It returns a pointer to the face.---------------------------------------------------------------------*/tFace MakeFace( tVertex v0, tVertex v1, tVertex v2, tFace fold ){ tFace f; tEdge e0, e1, e2; /* Create edges of the initial triangle. */ if( !fold ) { e0 = MakeNullEdge(); e1 = MakeNullEdge(); e2 = MakeNullEdge(); } else { /* Copy from fold, in reverse order. */ e0 = fold->edge[2]; e1 = fold->edge[1]; e2 = fold->edge[0]; } e0->endpts[0] = v0; e0->endpts[1] = v1; e1->endpts[0] = v1; e1->endpts[1] = v2; e2->endpts[0] = v2; e2->endpts[1] = v0; /* Create face for triangle. */ f = MakeNullFace(); f->edge[0] = e0; f->edge[1] = e1; f->edge[2] = e2; f->vertex[0] = v0; f->vertex[1] = v1; f->vertex[2] = v2; /* Link edges to face. */ e0->adjface[0] = e1->adjface[0] = e2->adjface[0] = f; return f;}/*---------------------------------------------------------------------CleanUp goes through each data structure list and clears allflags and NULLs out some pointers. The order of processing(edges, faces, vertices) is important.---------------------------------------------------------------------*/void CleanUp( tVertex *pvnext ){ CleanEdges(); CleanFaces(); CleanVertices( pvnext );}/*---------------------------------------------------------------------CleanEdges runs through the edge list and cleans up the structure.If there is a newface then it will put that face in place of the visible face and NULL out newface. It also deletes so marked edges.---------------------------------------------------------------------*/void CleanEdges( void ){ tEdge e; /* Primary index into edge list. */ tEdge t; /* Temporary edge pointer. */ /* Integrate the newface's into the data structure. */ /* Check every edge. */ e = edges; do { if ( e->newface ) { if ( e->adjface[0]->visible ) e->adjface[0] = e->newface; else e->adjface[1] = e->newface; e->newface = NULL; } e = e->next; } while ( e != edges ); /* Delete any edges marked for deletion. */ while ( edges && edges->delete ) { e = edges; DELETE( edges, e ); } e = edges->next; do { if ( e->delete ) { t = e; e = e->next; DELETE( edges, t ); } else e = e->next; } while ( e != edges );}/*---------------------------------------------------------------------CleanFaces runs through the face list and deletes any face marked visible.---------------------------------------------------------------------*/void CleanFaces( void ){ tFace f; /* Primary pointer into face list. */ tFace t; /* Temporary pointer, for deleting. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -