/** * Creates a list of lists L1, L2, ... LN where * LX = mesh faces with vertex v that come from the same original face * and without any of the vertices that appear before v in vertices * @param facesByOriginalFace list of faces lists * @param vertices vector with vertices indexs that contains v * @param v vertex index */ void BOP_Merge2::getFaces(BOP_LFaces &facesByOriginalFace, BOP_Indexs vertices, BOP_Index v) { // Get edges with vertex v BOP_Indexs edgeIndexs = m_mesh->getVertex(v)->getEdges(); const BOP_IT_Indexs edgeEnd = edgeIndexs.end(); for(BOP_IT_Indexs edgeIndex = edgeIndexs.begin();edgeIndex != edgeEnd;edgeIndex++) { // Foreach edge, add its no broken faces to the output list BOP_Edge* edge = m_mesh->getEdge(*edgeIndex); BOP_Indexs faceIndexs = edge->getFaces(); const BOP_IT_Indexs faceEnd = faceIndexs.end(); for(BOP_IT_Indexs faceIndex=faceIndexs.begin();faceIndex!=faceEnd;faceIndex++) { BOP_Face* face = m_mesh->getFace(*faceIndex); if (face->getTAG() != BROKEN) { // Search if the face contains any of the forbidden vertices bool found = false; for(BOP_IT_Indexs vertex = vertices.begin();*vertex!= v;vertex++) { if (face->containsVertex(*vertex)) { // face contains a forbidden vertex! found = true; break; } } if (!found) { // Search if we already have created a list with the // faces that come from the same original face const BOP_IT_LFaces lfEnd = facesByOriginalFace.end(); for(BOP_IT_LFaces facesByOriginalFaceX=facesByOriginalFace.begin(); facesByOriginalFaceX!=lfEnd; facesByOriginalFaceX++) { if (((*facesByOriginalFaceX)[0])->getOriginalFace() == face->getOriginalFace()) { // Search that the face has not been added to the list before for(unsigned int i = 0;i<(*facesByOriginalFaceX).size();i++) { if ((*facesByOriginalFaceX)[i] == face) { found = true; break; } } if (!found) { // Add face to the list if (face->getTAG()==OVERLAPPED) facesByOriginalFaceX->insert(facesByOriginalFaceX->begin(),face); else facesByOriginalFaceX->push_back(face); found = true; } break; } } if (!found) { // Create a new list and add the current face BOP_Faces facesByOriginalFaceX; facesByOriginalFaceX.push_back(face); facesByOriginalFace.push_back(facesByOriginalFaceX); } } } } } }
/** * Simplifies a mesh, merging the faces with vertex v that come from the same face. * @param oldFaces sequence of old mesh faces obtained from the merge * @param newFaces sequence of new mesh faces obtained from the merge * @param vertices sequence of indexs (v1 ... vi = v ... vn) where : * v is the current vertex to test, * vj (j < i) are tested vertices, * vk (k >= i) are vertices required to test to merge vj * (so if a vertex vk can't be merged, the merge is not possible). * @return true if the vertex v was 'merged' (obviously it could require to test * some new vertices that will be added to the vertices list) */ bool BOP_Merge::mergeFaces(BOP_Faces &oldFaces, BOP_Faces &newFaces, BOP_Indexs &vertices, BOP_Index v) { bool merged = true; // Get faces with v that come from the same original face, (without the already 'merged' from vertices) BOP_LFaces facesByOriginalFace; getFaces(facesByOriginalFace,vertices,v); if (facesByOriginalFace.size()==0) { // All the faces with this vertex were already merged!!! return true; } else { // Merge faces const BOP_IT_LFaces facesEnd = facesByOriginalFace.end(); for(BOP_IT_LFaces facesByOriginalFaceX = facesByOriginalFace.begin(); (facesByOriginalFaceX != facesEnd)&&merged; facesByOriginalFaceX++) { merged = mergeFaces((*facesByOriginalFaceX),oldFaces,newFaces,vertices,v); } } return merged; }
/** * Simplifies a mesh, merging the faces with the specified vertices. * @param mergeVertices vertices to test * @return true if a face merge was performed */ bool BOP_Merge2::mergeFaces(BOP_Indexs &mergeVertices) { // Check size > 0! if (mergeVertices.size() == 0) return false; bool didMerge = false; for( BOP_Index i = 0; i < mergeVertices.size(); ++i ) { BOP_LFaces facesByOriginalFace; BOP_Index v = mergeVertices[i]; BOP_Vertex *vert = m_mesh->getVertex(v); #ifdef BOP_DEBUG cout << "i = " << i << ", v = " << v << ", vert = " << vert << endl; if (v==48) cout << "found vert 48" << endl; #endif if ( vert->getTAG() != BROKEN ) { getFaces(facesByOriginalFace,v); switch (facesByOriginalFace.size()) { case 0: // v has no unbroken faces (so it's a new BROKEN vertex) freeVerts( v, vert ); vert->setTAG(BROKEN); break; case 2: { #ifdef BOP_DEBUG cout << "size of fBOF = " << facesByOriginalFace.size() << endl; #endif BOP_Faces ff = facesByOriginalFace.front(); BOP_Faces fb = facesByOriginalFace.back(); BOP_Index eindexs[2]; int ecount = 0; // look for two edges adjacent to v which contain both ofaces BOP_Indexs edges = vert->getEdges(); #ifdef BOP_DEBUG cout << " ff has " << ff.size() << " faces" << endl; cout << " fb has " << fb.size() << " faces" << endl; cout << " v has " << edges.size() << " edges" << endl; #endif for(BOP_IT_Indexs it = edges.begin(); it != edges.end(); ++it ) { BOP_Edge *edge = m_mesh->getEdge(*it); BOP_Indexs faces = edge->getFaces(); #ifdef BOP_DEBUG cout << " " << edge << " has " << edge->getFaces().size() << " faces" << endl; #endif if( faces.size() == 2 ) { BOP_Face *f0 = m_mesh->getFace(faces[0]); BOP_Face *f1 = m_mesh->getFace(faces[1]); if( f0->getOriginalFace() != f1->getOriginalFace() ) { #ifdef BOP_DEBUG cout << " " << f0 << endl; cout << " " << f1 << endl; #endif eindexs[ecount++] = (*it); } } } if(ecount == 2) { #ifdef BOP_DEBUG cout << " edge indexes are " << eindexs[0]; cout << " and " << eindexs[1] << endl; #endif BOP_Edge *edge = m_mesh->getEdge(eindexs[0]); BOP_Index N = edge->getVertex1(); if(N == v) N = edge->getVertex2(); #ifdef BOP_DEBUG cout << " ## OK, replace "<<v<<" with "<<N << endl; #endif mergeVertex(ff , v, N ); mergeVertex(fb , v, N ); // now remove v and its edges vert->setTAG(BROKEN); for(BOP_IT_Indexs it = edges.begin(); it != edges.end(); ++it ) { BOP_Edge *tedge = m_mesh->getEdge(*it); tedge->setUsed(false); } didMerge = true; } #ifdef BOP_DEBUG else { cout << " HUH: ecount was " << ecount << endl; } #endif } break; default: break; } } } return didMerge; }
/** * Simplifies a mesh, merging the faces with the specified vertices. * @param mergeVertices vertices to test * @return true if a face merge was performed */ bool BOP_Merge::mergeFaces(BOP_Indexs &mergeVertices) { // Check size > 0! if (mergeVertices.size() == 0) return false; // New faces added by merge BOP_Faces newFaces; // Old faces removed by merge BOP_Faces oldFaces; // Get the first vertex index and add it to // the current pending vertices to merge BOP_Index v = mergeVertices[0]; BOP_Indexs pendingVertices; pendingVertices.push_back(v); // Get faces with index v that come from the same original face BOP_LFaces facesByOriginalFace; getFaces(facesByOriginalFace,v); bool merged = true; // Check it has any unbroken face if (facesByOriginalFace.size()==0) { // v has not any unbroken face (so it's a new BROKEN vertex) (m_mesh->getVertex(v))->setTAG(BROKEN); merged = false; } // Merge vertex faces const BOP_IT_LFaces facesEnd = facesByOriginalFace.end(); for(BOP_IT_LFaces facesByOriginalFaceX = facesByOriginalFace.begin(); (facesByOriginalFaceX != facesEnd)&&merged; facesByOriginalFaceX++) { merged = mergeFaces((*facesByOriginalFaceX),oldFaces,newFaces,pendingVertices,v); } // Check if the are some pendingVertices to merge if (pendingVertices.size() > 1 && merged) { // There are pending vertices that we need to merge in order to merge v ... for(unsigned int i=1;i<pendingVertices.size() && merged;i++) merged = mergeFaces(oldFaces,newFaces,pendingVertices,pendingVertices[i]); } // If merge was succesful ... if (merged) { // Set old faces to BROKEN... const BOP_IT_Faces oldFacesEnd = oldFaces.end(); for(BOP_IT_Faces face=oldFaces.begin();face!=oldFacesEnd;face++) (*face)->setTAG(BROKEN); // ... and add merged faces (that are the new merged faces without pending vertices) const BOP_IT_Faces newFacesEnd = newFaces.end(); for(BOP_IT_Faces newFace=newFaces.begin();newFace!=newFacesEnd;newFace++) { m_mesh->addFace(*newFace); // Also, add new face vertices to the queue of vertices to merge if they weren't for(BOP_Index i = 0;i<(*newFace)->size();i++) { BOP_Index vertexIndex = (*newFace)->getVertex(i); if (vertexIndex >= m_firstVertex && !containsIndex(mergeVertices,vertexIndex)) mergeVertices.push_back(vertexIndex); } } // Set the merged vertices to BROKEN ... const BOP_IT_Indexs pendingEnd = pendingVertices.end(); for(BOP_IT_Indexs pendingVertex = pendingVertices.begin(); pendingVertex != pendingEnd;pendingVertex++) { BOP_Index pV = *pendingVertex; m_mesh->getVertex(pV)->setTAG(BROKEN); // ... and remove them from mergeVertices queue const BOP_IT_Indexs mergeEnd = mergeVertices.end(); for(BOP_IT_Indexs mergeVertex = mergeVertices.begin(); mergeVertex != mergeEnd;mergeVertex++) { BOP_Index mV = *mergeVertex; if (mV == pV) { mergeVertices.erase(mergeVertex); break; } } } } else { // The merge was not succesful, remove the vertex frome merge vertices queue mergeVertices.erase(mergeVertices.begin()); // free the not used newfaces const BOP_IT_Faces newFacesEnd = newFaces.end(); for(BOP_IT_Faces newFace=newFaces.begin();newFace!=newFacesEnd;newFace++) { delete (*newFace); } } // Invoke mergeFaces and return the merge result return (mergeFaces(mergeVertices) || merged); }