/** * Computes intesections of coplanars faces from object A with faces from object B. * @param mesh mesh that contains the faces, edges and vertices * @param facesA set of faces from object A * @param facesB set of faces from object B */ void BOP_sew(BOP_Mesh *mesh, BOP_Faces *facesA, BOP_Faces *facesB) { for(unsigned int idxFaceB = 0; idxFaceB < facesB->size(); idxFaceB++) { BOP_Face *faceB = (*facesB)[idxFaceB]; MT_Plane3 planeB = faceB->getPlane(); MT_Point3 p1 = mesh->getVertex(faceB->getVertex(0))->getPoint(); MT_Point3 p2 = mesh->getVertex(faceB->getVertex(1))->getPoint(); MT_Point3 p3 = mesh->getVertex(faceB->getVertex(2))->getPoint(); for(unsigned int idxFaceA = 0; idxFaceA < facesA->size() && faceB->getTAG() != BROKEN && faceB->getTAG() != PHANTOM; idxFaceA++) { BOP_Face *faceA = (*facesA)[idxFaceA]; if ((faceA->getTAG() != BROKEN)&&(faceA->getTAG() != PHANTOM)) { MT_Plane3 planeA = faceA->getPlane(); if (BOP_containsPoint(planeA,p1) && BOP_containsPoint(planeA,p2) && BOP_containsPoint(planeA,p3)) { if (BOP_orientation(planeA,planeB) > 0) { BOP_intersectCoplanarFaces(mesh,facesA,faceB,faceA,true); } } } } } }
/** * Removes faces from facesB that are overlapped with anyone from facesA. * @param mesh mesh that contains the faces, edges and vertices * @param facesA set of faces from object A * @param facesB set of faces from object B */ void BOP_removeOverlappedFaces(BOP_Mesh *mesh, BOP_Faces *facesA, BOP_Faces *facesB) { for(unsigned int i=0;i<facesA->size();i++) { BOP_Face *faceI = (*facesA)[i]; if (faceI->getTAG()==BROKEN) continue; bool overlapped = false; MT_Point3 p1 = mesh->getVertex(faceI->getVertex(0))->getPoint(); MT_Point3 p2 = mesh->getVertex(faceI->getVertex(1))->getPoint(); MT_Point3 p3 = mesh->getVertex(faceI->getVertex(2))->getPoint(); for(unsigned int j=0;j<facesB->size();) { BOP_Face *faceJ = (*facesB)[j]; if (faceJ->getTAG()!=BROKEN) { MT_Plane3 planeJ = faceJ->getPlane(); if (BOP_containsPoint(planeJ,p1) && BOP_containsPoint(planeJ,p2) && BOP_containsPoint(planeJ,p3)) { MT_Point3 q1 = mesh->getVertex(faceJ->getVertex(0))->getPoint(); MT_Point3 q2 = mesh->getVertex(faceJ->getVertex(1))->getPoint(); MT_Point3 q3 = mesh->getVertex(faceJ->getVertex(2))->getPoint(); if (BOP_overlap(MT_Vector3(planeJ.x(),planeJ.y(),planeJ.z()), p1,p2,p3,q1,q2,q3)) { facesB->erase(facesB->begin()+j,facesB->begin()+(j+1)); faceJ->setTAG(BROKEN); overlapped = true; } else j++; } else j++; }else j++; } if (overlapped) faceI->setTAG(OVERLAPPED); } }
/** * 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); } } } } } }
/** * testMesh */ void BOP_Mesh::testMesh() { BOP_Face* cares[10]; unsigned int nedges=0,i; for(i=0;i<m_edges.size();i++) { BOP_Edge *edge = m_edges[i]; BOP_Indexs faces = edge->getFaces(); unsigned int count = 0; const BOP_IT_Indexs facesEnd = faces.end(); for(BOP_IT_Indexs it = faces.begin();it!=facesEnd;it++) { if (m_faces[*it]->getTAG()!=BROKEN) { cares[count] = m_faces[*it]; count++; } } if ((count%2)!=0) nedges++; } if (nedges) cout << nedges << " wrong edges." << endl; else cout << "well edges." << endl; unsigned int duplFaces = 0; unsigned int wrongFaces = 0; for(i=0;i<m_faces.size();i++){ BOP_Face *faceI = m_faces[i]; if (faceI->getTAG()==BROKEN) continue; if (testFace(faceI)){ wrongFaces++; cout << "Wrong Face: " << faceI << endl; } for(unsigned int j=i+1;j<m_faces.size();j++){ BOP_Face *faceJ = m_faces[j]; if (faceJ->getTAG()==BROKEN) continue; if (testFaces(faceI,faceJ)){ duplFaces++; cout << "Duplicate FaceI: " << faceI << endl; cout << "Duplicate FaceJ: " << faceJ << endl; } } } cout << duplFaces << " duplicate faces." << endl; cout << wrongFaces << " wrong faces." << endl; }
/** * Tests if faces since firstFace have all vertexs non-coincident of colinear, otherwise repairs the mesh. * @param mesh mesh that contains the faces, edges and vertices * @param firstFace first face index to be tested */ void BOP_mergeVertexs(BOP_Mesh *mesh, unsigned int firstFace) { unsigned int numFaces = mesh->getNumFaces(); for(unsigned int idxFace = firstFace; idxFace < numFaces; idxFace++) { BOP_Face *face = mesh->getFace(idxFace); if ((face->getTAG() != BROKEN) && (face->getTAG() != PHANTOM)) { MT_Point3 vertex1 = mesh->getVertex(face->getVertex(0))->getPoint(); MT_Point3 vertex2 = mesh->getVertex(face->getVertex(1))->getPoint(); MT_Point3 vertex3 = mesh->getVertex(face->getVertex(2))->getPoint(); if (BOP_collinear(vertex1,vertex2,vertex3)) // collinear triangle face->setTAG(PHANTOM); } } }
/** * Returns the first face of faces that shares the input edge of face. * @param mesh mesh that contains the faces, edges and vertices * @param faces set of faces * @param face input face * @param edge face's edge * @return first face that shares the edge of input face */ BOP_Face *BOP_getOppositeFace(BOP_Mesh* mesh, BOP_Faces* faces, BOP_Face* face, BOP_Edge* edge) { if (edge == NULL) return NULL; BOP_Indexs auxfaces = edge->getFaces(); const BOP_IT_Indexs auxfacesEnd = auxfaces.end(); for(BOP_IT_Indexs it = auxfaces.begin(); it != auxfacesEnd; it++) { BOP_Face *auxface = mesh->getFace(*it); if ((auxface != face) && (auxface->getTAG()!=BROKEN) && BOP_containsFace(faces,auxface)) { return auxface; } } return NULL; }
void BOP_Merge2::cleanup( void ) { BOP_Edges edges = m_mesh->getEdges(); for (BOP_IT_Edges edge = edges.begin(); edge != edges.end(); ++edge) { BOP_Indexs faces = (*edge)->getFaces(); for (BOP_IT_Indexs face = faces.begin(); face != faces.end(); ++face) { BOP_Face *f = m_mesh->getFace(*face); if(f->getTAG()== UNCLASSIFIED) ; else (*edge)->removeFace(*face); } if( (*edge)->getFaces().size() == 0) (*edge)->setUsed(false); } BOP_Vertexs v = m_mesh->getVertexs(); for( BOP_IT_Vertexs it = v.begin(); it != v.end(); ++it ) { if( (*it)->getTAG() != BROKEN) { BOP_Indexs iedges = (*it)->getEdges(); for(BOP_IT_Indexs i = iedges.begin();i!=iedges.end();i++) if( m_mesh->getEdge((*i))->getUsed( ) == false) (*it)->removeEdge( *i ); if( (*it)->getEdges().size() == 0 ) (*it)->setTAG(BROKEN); } } // clean_nonmanifold( m_mesh ); }
void dumpmesh ( BOP_Mesh *m, bool force ) { unsigned int nonmanifold = 0; { BOP_Edges edges = m->getEdges(); int count = 0; for (BOP_IT_Edges edge = edges.begin(); edge != edges.end(); ++count, ++edge) { if (!(*edge)->getUsed() && (*edge)->getFaces().size() == 0 ) continue; BOP_Vertex * v1 = m->getVertex((*edge)->getVertex1()); BOP_Vertex * v2 = m->getVertex((*edge)->getVertex2()); if(v1->getTAG()!= BROKEN || v2->getTAG()!= BROKEN ) { int fcount = 0; BOP_Indexs faces = (*edge)->getFaces(); for (BOP_IT_Indexs face = faces.begin(); face != faces.end(); face++) { BOP_Face *f = m->getFace(*face); if(f->getTAG()== UNCLASSIFIED) ++fcount; } if(fcount !=0 && fcount !=2 ) { ++nonmanifold; } } } if (!force && nonmanifold == 0) return; } if( nonmanifold ) cout << nonmanifold << " edges detected" << endl; #ifdef BOP_DEBUG cout << "---------------------------" << endl; BOP_Edges edges = m->getEdges(); int count = 0; for (BOP_IT_Edges edge = edges.begin(); edge != edges.end(); ++count, ++edge) { BOP_Vertex * v1 = m->getVertex((*edge)->getVertex1()); BOP_Vertex * v2 = m->getVertex((*edge)->getVertex2()); if(v1->getTAG()!= BROKEN || v2->getTAG()!= BROKEN ) { int fcount = 0; BOP_Indexs faces = (*edge)->getFaces(); cout << count << ", " << (*edge) << ", " << faces.size() << endl; for (BOP_IT_Indexs face = faces.begin(); face != faces.end(); face++) { BOP_Face *f = m->getFace(*face); if(f->getTAG()== UNCLASSIFIED) ++fcount; cout << " face " << f << endl; } if(fcount !=0 && fcount !=2 ) cout << " NON-MANIFOLD" << endl; } } BOP_Faces faces = m->getFaces(); count = 0; for (BOP_IT_Faces face = faces.begin(); face != faces.end(); face++) { if( count < 12*2 || (*face)->getTAG() != BROKEN ) { cout << count << ", " << *face << endl; } ++count; } BOP_Vertexs verts = m->getVertexs(); count = 0; for (BOP_IT_Vertexs vert = verts.begin(); vert != verts.end(); vert++) { cout << count++ << ", " << *vert << " " << (*vert)->getNumEdges() << endl; BOP_Indexs edges = (*vert)->getEdges(); for( BOP_IT_Indexs it = edges.begin(); it != edges.end(); ++it) { BOP_Edge *edge = m->getEdge(*it); cout << " " << edge << endl; } } cout << "===========================" << endl; #endif }
/** * Triangulates faceB using edges of faceA that both are complanars. * @param mesh mesh that contains the faces, edges and vertices * @param facesB set of faces from object B * @param faceA face from object A * @param faceB face from object B * @param invert indicates if faceA has priority over faceB */ void BOP_intersectCoplanarFaces(BOP_Mesh* mesh, BOP_Faces* facesB, BOP_Face* faceA, BOP_Face* faceB, bool invert) { unsigned int oldSize = facesB->size(); unsigned int originalFaceB = faceB->getOriginalFace(); MT_Point3 p1 = mesh->getVertex(faceA->getVertex(0))->getPoint(); MT_Point3 p2 = mesh->getVertex(faceA->getVertex(1))->getPoint(); MT_Point3 p3 = mesh->getVertex(faceA->getVertex(2))->getPoint(); MT_Vector3 normal(faceA->getPlane().x(),faceA->getPlane().y(),faceA->getPlane().z()); MT_Vector3 p1p2 = p2-p1; MT_Plane3 plane1((p1p2.cross(normal).normalized()),p1); BOP_Segment sA; sA.m_cfg1 = BOP_Segment::createVertexCfg(1); sA.m_v1 = faceA->getVertex(0); sA.m_cfg2 = BOP_Segment::createVertexCfg(2); sA.m_v2 = faceA->getVertex(1); BOP_intersectCoplanarFaces(mesh,facesB,faceB,sA,plane1,invert); MT_Vector3 p2p3 = p3-p2; MT_Plane3 plane2((p2p3.cross(normal).normalized()),p2); sA.m_cfg1 = BOP_Segment::createVertexCfg(2); sA.m_v1 = faceA->getVertex(1); sA.m_cfg2 = BOP_Segment::createVertexCfg(3); sA.m_v2 = faceA->getVertex(2); if (faceB->getTAG() == BROKEN) { for(unsigned int idxFace = oldSize; idxFace < facesB->size(); idxFace++) { BOP_Face *face = (*facesB)[idxFace]; if (face->getTAG() != BROKEN && originalFaceB == face->getOriginalFace()) BOP_intersectCoplanarFaces(mesh,facesB,face,sA,plane2,invert); } } else { BOP_intersectCoplanarFaces(mesh,facesB,faceB,sA,plane2,invert); } MT_Vector3 p3p1 = p1-p3; MT_Plane3 plane3((p3p1.cross(normal).safe_normalized()),p3); sA.m_cfg1 = BOP_Segment::createVertexCfg(3); sA.m_v1 = faceA->getVertex(2); sA.m_cfg2 = BOP_Segment::createVertexCfg(1); sA.m_v2 = faceA->getVertex(0); if (faceB->getTAG() == BROKEN) { for(unsigned int idxFace = oldSize; idxFace < facesB->size(); idxFace++) { BOP_Face *face = (*facesB)[idxFace]; if (face->getTAG() != BROKEN && originalFaceB == face->getOriginalFace()) BOP_intersectCoplanarFaces(mesh,facesB,face,sA,plane3,invert); } } else { BOP_intersectCoplanarFaces(mesh,facesB,faceB,sA,plane3,invert); } }
void BOP_Face2Face(BOP_Mesh *mesh, BOP_Faces *facesA, BOP_Faces *facesB) { for(unsigned int idxFaceA=0;idxFaceA<facesA->size();idxFaceA++) { BOP_Face *faceA = (*facesA)[idxFaceA]; MT_Plane3 planeA = faceA->getPlane(); MT_Point3 p1 = mesh->getVertex(faceA->getVertex(0))->getPoint(); MT_Point3 p2 = mesh->getVertex(faceA->getVertex(1))->getPoint(); MT_Point3 p3 = mesh->getVertex(faceA->getVertex(2))->getPoint(); /* get (or create) bounding box for face A */ if( faceA->getBBox() == NULL ) faceA->setBBox(p1,p2,p3); BOP_BBox *boxA = faceA->getBBox(); /* start checking B faces with the previously stored split index */ for(unsigned int idxFaceB=faceA->getSplit(); idxFaceB<facesB->size() && (faceA->getTAG() != BROKEN) && (faceA->getTAG() != PHANTOM);) { BOP_Face *faceB = (*facesB)[idxFaceB]; faceA->setSplit(idxFaceB); if ((faceB->getTAG() != BROKEN) && (faceB->getTAG() != PHANTOM)) { /* get (or create) bounding box for face B */ if( faceB->getBBox() == NULL ) faceB->setBBox(mesh->getVertex(faceB->getVertex(0))->getPoint(), mesh->getVertex(faceB->getVertex(1))->getPoint(), mesh->getVertex(faceB->getVertex(2))->getPoint()); BOP_BBox *boxB = faceB->getBBox(); if (boxA->intersect(*boxB)) { MT_Plane3 planeB = faceB->getPlane(); if (BOP_containsPoint(planeB,p1) && BOP_containsPoint(planeB,p2) && BOP_containsPoint(planeB,p3)) { if (BOP_orientation(planeB,planeA)>0) { BOP_intersectCoplanarFaces(mesh,facesB,faceA,faceB,false); } } else { BOP_intersectNonCoplanarFaces(mesh,facesA,facesB,faceA,faceB); } } } idxFaceB++; } } // Clean broken faces from facesA BOP_IT_Faces it; it = facesA->begin(); while (it != facesA->end()) { BOP_Face *face = *it; if (face->getTAG() == BROKEN) it = facesA->erase(it); else it++; } /* it = facesB->begin(); while (it != facesB->end()) { BOP_Face *face = *it; if (face->getTAG() == BROKEN) it = facesB->erase(it); else it++; } */ }