void GW_Mesh::CheckIntegrity() { for( GW_U32 i=0; i<this->GetNbrVertex(); ++i ) { GW_Vertex* pVert = this->GetVertex(i); GW_ASSERT( pVert!=NULL ); GW_Face* pFace = pVert->GetFace(); GW_ASSERT( pFace!=NULL ); if( pFace!=NULL && pFace->GetVertex(0)!=pVert && pFace->GetVertex(1)!=pVert && pFace->GetVertex(2)!=pVert ) GW_ASSERT( GW_False ); } for( GW_U32 i=0; i<this->GetNbrFace(); ++i ) { GW_Face* pFace = this->GetFace(i); GW_ASSERT( pFace!=NULL ); for( GW_U32 k=0; k<3; ++k ) { GW_U32 k1 = (k+1)%3; GW_U32 k2 = (k+2)%3; GW_Face* pNeighFace = pFace->GetFaceNeighbor(k); GW_Vertex* pV1 = pFace->GetVertex(k1); GW_ASSERT( pV1!=NULL ); GW_Vertex* pV2 = pFace->GetVertex(k2); GW_ASSERT( pV2!=NULL ); if( pNeighFace!=NULL ) { GW_ASSERT( pNeighFace->GetFaceNeighbor(*pV1, *pV2)==pFace ); GW_ASSERT( pFace->GetFaceNeighbor(*pV1, *pV2)==pNeighFace); } } } }
/*------------------------------------------------------------------------------*/ void GW_Mesh::FlipOrientation() { for( GW_U32 i=0; i<this->GetNbrFace(); ++i ) { GW_Face* pFace = this->GetFace(i); GW_ASSERT( pFace!=NULL ); pFace->SetVertex( *pFace->GetVertex(1), *pFace->GetVertex(0), *pFace->GetVertex(2) ); pFace->SetFaceNeighbor( pFace->GetFaceNeighbor(1), pFace->GetFaceNeighbor(0), pFace->GetFaceNeighbor(2) ); } }
/*------------------------------------------------------------------------------*/ void GW_Mesh::ReOrientMesh( GW_Face& start_face ) { /* march on the voronoi diagram */ T_FaceList FaceToProceed; FaceToProceed.push_back( &start_face ); T_FaceMap FaceDone; FaceDone[ start_face.GetID() ] = &start_face; while( !FaceToProceed.empty() ) { GW_Face* pFace = FaceToProceed.front(); GW_ASSERT( pFace!=NULL ); FaceToProceed.pop_front(); /* add neighbors */ for( GW_U32 i=0; i<3; ++i ) { GW_Vertex* pVertDir = pFace->GetVertex(i); GW_ASSERT( pVertDir!=NULL ); GW_Face* pNewFace = pFace->GetFaceNeighbor(*pVertDir); if( pNewFace!=NULL && FaceDone.find(pNewFace->GetID())==FaceDone.end() ) { /* find the two other vertices */ GW_U32 i1 = (i+1)%3; GW_U32 i2 = (i+2)%3; GW_Vertex* pNewVert[3]; pNewVert[0] = pFace->GetVertex(i2); GW_ASSERT( pNewVert[0]!=NULL ); pNewVert[1] = pFace->GetVertex(i1); GW_ASSERT( pNewVert[1]!=NULL ); pNewVert[2] = pNewFace->GetVertex(*pNewVert[0], *pNewVert[1]); GW_ASSERT( pNewVert[2]!=NULL ); GW_Face* pNeigh[3]; pNeigh[0] = pNewFace->GetFaceNeighbor( *pNewVert[0] ); pNeigh[1] = pNewFace->GetFaceNeighbor( *pNewVert[1] ); pNeigh[2] = pNewFace->GetFaceNeighbor( *pNewVert[2] ); /* reorient the face */ pNewFace->SetVertex( *pNewVert[0], *pNewVert[1], *pNewVert[2] ); pNewFace->SetFaceNeighbor( pNeigh[0], pNeigh[1], pNeigh[2] ); FaceToProceed.push_back( pNewFace ); FaceDone[ pNewFace->GetID() ] = pNewFace; // so that it won't be added anymore } } } /* check for global orientation (just an heuristic) */ GW_Face* pFace = this->GetFace(0); GW_ASSERT( pFace!=NULL ); GW_Vector3D v = pFace->GetVertex(0)->GetPosition() + pFace->GetVertex(1)->GetPosition() + pFace->GetVertex(2)->GetPosition(); GW_Vector3D n = pFace->ComputeNormal(); if( n*v<0 ) this->FlipOrientation(); }
/*------------------------------------------------------------------------------*/ void GW_Vertex::BuildRawNormal() { GW_Vector3D FaceNormal; Normal_.SetZero(); GW_U32 nIter = 0; for( GW_FaceIterator it = this->BeginFaceIterator(); it!=this->EndFaceIterator(); ++it ) { GW_Face* pFace = *it; GW_ASSERT( pFace!=NULL ); FaceNormal = (pFace->GetVertex(0)->GetPosition()-pFace->GetVertex(1)->GetPosition()) ^ (pFace->GetVertex(0)->GetPosition()-pFace->GetVertex(2)->GetPosition()); FaceNormal.Normalize(); Normal_ += FaceNormal; nIter++; if( nIter>20 ) break; } Normal_.Normalize(); }
/*------------------------------------------------------------------------------*/ GW_Float GW_Mesh::GetArea() { GW_Float rArea = 0; for( IT_FaceVector it=FaceVector_.begin(); it!=FaceVector_.end(); ++it) { GW_Face* pFace = *it; GW_ASSERT( pFace!=NULL ); GW_Vertex* v0 = pFace->GetVertex(0); GW_Vertex* v1 = pFace->GetVertex(1); GW_Vertex* v2 = pFace->GetVertex(2); if( v0!=NULL && v1!=NULL && v2!=NULL ) { GW_Vector3D e1 = v1->GetPosition() - v0->GetPosition(); GW_Vector3D e2 = v2->GetPosition() - v0->GetPosition(); rArea += ~(e1 ^ e2); } } return (GW_Float) 0.5*rArea; }
/*------------------------------------------------------------------------------*/ void GW_Mesh::ReOrientNormals() { for( GW_U32 i=0; i<this->GetNbrFace(); ++i ) { GW_Face* pFace = this->GetFace(i); GW_ASSERT( pFace!=NULL ); GW_Vector3D n = pFace->ComputeNormal(); for( GW_U32 k=0; k<3; ++k ) { GW_Vector3D& nv = pFace->GetVertex(k)->GetNormal(); if( nv*n < 0 ) nv = -nv; } } }
/*------------------------------------------------------------------------------*/ void GW_Mesh::BuildConnectivity() { T_FaceList* VertexToFaceMap = new T_FaceList[this->GetNbrVertex()]; /* build the inverse map vertex->face */ for( IT_FaceVector it = FaceVector_.begin(); it!=FaceVector_.end(); ++it ) { GW_Face* pFace = *it; GW_ASSERT( pFace!=NULL ); for( GW_U32 i=0; i<3; ++i ) { GW_Vertex* pVert = pFace->GetVertex(i); GW_ASSERT(pVert!=NULL); GW_ASSERT( pVert->GetID() <= this->GetNbrVertex() ); VertexToFaceMap[pVert->GetID()].push_back( pFace ); } } /* now we can set up connectivity */ for( IT_FaceVector it=FaceVector_.begin(); it!=FaceVector_.end(); ++it ) { GW_Face* pFace = *it; GW_ASSERT( pFace!=NULL ); /* set up the neigbooring faces of the 3 vertices */ T_FaceList* pFaceLists[3]; for( GW_U32 i=0; i<3; ++i ) { GW_Vertex* pVert = pFace->GetVertex(i); pFaceLists[i] = &VertexToFaceMap[pVert->GetID()]; } /* compute neighbor in the 3 directions */ for( GW_U32 i=0; i<3; ++i ) { GW_Face* pNeighbor = NULL; GW_U32 i1 = (i+1)%3; GW_U32 i2 = (i+2)%3; /* we must find the intersection of the surrounding faces of these 2 vertex */ GW_Bool bFind = GW_False; for( IT_FaceList it1 = pFaceLists[i1]->begin(); it1!=pFaceLists[i1]->end() && bFind!=GW_True; ++it1 ) { GW_Face* pFace1 = *it1; for( IT_FaceList it2 = pFaceLists[i2]->begin(); it2!=pFaceLists[i2]->end() && bFind!=GW_True; ++it2 ) { GW_Face* pFace2 = *it2; if( pFace1==pFace2 && pFace1!=pFace ) { pNeighbor = pFace1; bFind=GW_True; } } } // GW_ASSERT( pNeighbor!=NULL ); /* assign the face */ /* if( pFace->GetFaceNeighbor(i)!=NULL ) GW_ASSERT( pFace->GetFaceNeighbor(i)==pNeighbor ); */ pFace->SetFaceNeighbor( pNeighbor, i ); /* make some test on the neighbor to assure symetry in the connectivity relationship */ if( pNeighbor!=NULL ) { GW_I32 nEdgeNumber = pNeighbor->GetEdgeNumber( *pFace->GetVertex(i1),*pFace->GetVertex(i2) ); GW_ASSERT( nEdgeNumber>=0 ); #if 0 if( pNeighbor->GetFaceNeighbor( nEdgeNumber )!=NULL ) GW_ASSERT(pNeighbor->GetFaceNeighbor(nEdgeNumber)==pFace); #endif pNeighbor->SetFaceNeighbor( pFace, nEdgeNumber ); } } } GW_DELETEARRAY( VertexToFaceMap ); }
/*------------------------------------------------------------------------------*/ void GW_VoronoiMesh::FixHole() { typedef std::pair<GW_VoronoiVertex*, GW_VoronoiVertex*> T_VertexPair; typedef std::list<T_VertexPair> T_VertexPairList; typedef T_VertexPairList::iterator IT_VertexPairList; T_VertexPairList VertexPairList; for( GW_U32 i=0; i<this->GetNbrFace(); ++i ) { GW_Face* pFace = this->GetFace(i); GW_ASSERT( pFace!=NULL ); for( GW_U32 nV = 0; nV<3; ++nV ) { if( pFace->GetFaceNeighbor(nV)==NULL ) { GW_VoronoiVertex* pVert1 = (GW_VoronoiVertex*) pFace->GetVertex( (nV+1)%3 ); GW_VoronoiVertex* pVert2 = (GW_VoronoiVertex*) pFace->GetVertex( (nV+2)%3 ); VertexPairList.push_back( T_VertexPair(pVert1,pVert2) ); } } } char str[50]; sprintf( str, "%d boundary edges detected.", VertexPairList.size() ); GW_OutputComment( str ); while( !VertexPairList.empty() ) { T_VertexPairList HoleBorder; T_VertexPair StartEdge = VertexPairList.front(); T_VertexPair CurEdge = StartEdge; VertexPairList.pop_front(); HoleBorder.push_back( CurEdge ); /* try to find the hole border */ GW_Bool bNextEdgeFound = GW_False; while( true ) { bNextEdgeFound = GW_False; for( IT_VertexPairList it = VertexPairList.begin(); it!=VertexPairList.end(); ++it ) { T_VertexPair NewEdge = *it; if( (NewEdge.first==CurEdge.second) && (NewEdge.second!=CurEdge.first) ) { CurEdge = NewEdge; HoleBorder.push_back( CurEdge ); bNextEdgeFound = GW_True; VertexPairList.erase( it ); break; } if( (NewEdge.second==CurEdge.second) && (NewEdge.first!=CurEdge.first) ) { CurEdge = T_VertexPair( NewEdge.second, NewEdge.first); HoleBorder.push_back( CurEdge ); bNextEdgeFound = GW_True; VertexPairList.erase( it ); break; } } if( !bNextEdgeFound ) break; // the hole cannot be completed if( StartEdge.first == CurEdge.second ) break; // the hole is completed } if( bNextEdgeFound && HoleBorder.size()>2 ) // that means we have a full hole { char str[50]; sprintf( str, "Filing a hole of %d vertex.", HoleBorder.size() ); GW_OutputComment( str ); IT_VertexPairList it = HoleBorder.begin(); GW_VoronoiVertex* pVert0 = it->first; GW_ASSERT( pVert0!=NULL ); it++; GW_VoronoiVertex* pVert1 = it->first; GW_ASSERT( pVert1!=NULL ); it++; for( ; it!=HoleBorder.end(); ++it ) { GW_VoronoiVertex* pVert2 = it->first; GW_ASSERT( pVert2!=NULL ); /* test for manifold structure before creating a new edge [v0,v2] */ GW_Bool bManifold = GW_True; GW_Face* pFace1, *pFace2; pVert0->GetFaces( *pVert1, pFace1, pFace2 ); if( pFace1!=NULL && pFace2!=NULL ) bManifold = GW_False; pVert1->GetFaces( *pVert2, pFace1, pFace2 ); if( pFace1!=NULL && pFace2!=NULL ) bManifold = GW_False; pVert0->GetFaces( *pVert2, pFace1, pFace2 ); if( pFace1!=NULL && pFace2!=NULL ) bManifold = GW_False; if( bManifold ) { GW_Face& Face = this->CreateNewFace(); Face.SetVertex( *pVert0, *pVert1, *pVert2 ); this->AddFace( Face ); } pVert1 = pVert2; } } } }
/*------------------------------------------------------------------------------*/ void GW_VoronoiMesh::BuildMesh( GW_GeodesicMesh& Mesh, GW_Bool bFixHole ) { /* Create Vornoi vertex and make the inverse map GeodesicVertex->VoronoiVertex */ this->CreateVoronoiVertex(); #if 1 // simple method GW_OutputComment("Recomputing the whole Voronoi diagram."); GW_VoronoiMesh::PerformFastMarching( Mesh, BaseVertexList_ ); /* find the faces */ GW_OutputComment("Building the faces."); T_FaceMap FaceMap; // to store the faces already built. for( GW_U32 i=0; i<Mesh.GetNbrFace(); ++i ) { GW_Face* pFace = Mesh.GetFace(i); GW_ASSERT( pFace!=NULL ); GW_GeodesicVertex* pGeo[3]; GW_GeodesicVertex* pFront[3]; for( GW_U32 j=0; j<3; ++j ) { pGeo[j] = (GW_GeodesicVertex*) pFace->GetVertex(j); GW_ASSERT( pGeo[j]!=NULL ); pFront[j] = pGeo[j]->GetFront(); } if( pFront[0]!=pFront[1] && pFront[1]!=pFront[2] && pFront[2]!=pFront[0] ) { GW_U32 nID = GW_Vertex::ComputeUniqueId( *pFront[0], *pFront[1], *pFront[2] ); if( FaceMap.find(nID)==FaceMap.end() ) { /* create the face */ GW_Face& Face = this->CreateNewFace(); FaceMap[nID] = &Face; for( GW_U32 j=0; j<3; ++j ) // assign the vertices { GW_U32 nID = pFront[j]->GetID(); GW_ASSERT( VoronoiVertexMap_.find(nID)!=VoronoiVertexMap_.end() ); GW_VoronoiVertex* pVorVert = VoronoiVertexMap_[nID]; GW_ASSERT( pVorVert!=NULL ); Face.SetVertex( *pVorVert, j ); } } } } #else GW_OutputComment("Computing voronoi diagrams."); /* perform once more a firestart to set up connectivity */ Mesh.RegisterNewDeadVertexCallbackFunction( GW_VoronoiMesh::FastMarchingCallbackFunction_MeshBuilding ); Mesh.ResetGeodesicMesh(); GW_VoronoiMesh::PerformFastMarching( Mesh, BaseVertexList_ ); Mesh.RegisterNewDeadVertexCallbackFunction( NULL ); /* build the faces */ T_FaceMap FaceMap; // to store the faces already built. GW_OutputComment("Building voronoi mesh faces."); for( IT_GeodesicVertexList it = BaseVertexList_.begin(); it!=BaseVertexList_.end(); ++it ) { GW_GeodesicVertex* pVert0 = *it; GW_ASSERT( pVert0!=NULL ); /* retrive the corresponding voronoi vertex */ GW_VoronoiVertex* pVoronoiVert0 = GW_VoronoiMesh::GetVoronoiFromGeodesic( *pVert0 ); GW_ASSERT( pVoronoiVert0!=NULL ); for( IT_VoronoiVertexList itVoronoi1=pVoronoiVert0->BeginNeighborIterator(); itVoronoi1!=pVoronoiVert0->EndNeighborIterator(); ++itVoronoi1 ) { GW_VoronoiVertex* pVoronoiVert1 = *itVoronoi1; GW_ASSERT( pVoronoiVert1!=NULL ); GW_U32 nNumTriangle = 0; for( IT_VoronoiVertexList itVoronoi2=pVoronoiVert1->BeginNeighborIterator(); itVoronoi2!=pVoronoiVert1->EndNeighborIterator(); ++itVoronoi2 ) { GW_VoronoiVertex* pVoronoiVert2 = *itVoronoi2; GW_ASSERT( pVoronoiVert2!=NULL ); if( pVoronoiVert2!=pVoronoiVert0 && pVoronoiVert0->IsNeighbor(*pVoronoiVert2) ) { /* yes, we find a triangle ! Test if it wasn't already constructed */ GW_U32 nUniqueId = GW_Vertex::ComputeUniqueId( *pVoronoiVert0, *pVoronoiVert1, *pVoronoiVert2 ); if( FaceMap.find(nUniqueId)==FaceMap.end() ) { nNumTriangle++; GW_ASSERT( nNumTriangle<=2 ); // assert manifold structure /* this is the 1st time we encounter this face. */ GW_Face* pFace = &Mesh.CreateNewFace(); /* set up the face */ pFace->SetVertex( *pVoronoiVert0, *pVoronoiVert1, *pVoronoiVert2 ); FaceMap[nUniqueId] = pFace; } } } } } #endif /* assign the faces */ this->SetNbrFace( (GW_U32) FaceMap.size() ); GW_U32 nNum = 0; for( IT_FaceMap it = FaceMap.begin(); it!=FaceMap.end(); ++it ) { this->SetFace( nNum, it->second ); nNum++; } /* rebuild connectivity */ GW_OutputComment("Building connectivity."); this->BuildConnectivity(); /* try to fill the holes */ if( bFixHole ) { GW_OutputComment("Fixing holes."); this->FixHole(); /* re-rebuild connectivity */ this->BuildConnectivity(); } }