diff --git a/vcg/complex/algorithms/polygon_support.h b/vcg/complex/algorithms/polygon_support.h index 801a35bb..84429259 100644 --- a/vcg/complex/algorithms/polygon_support.h +++ b/vcg/complex/algorithms/polygon_support.h @@ -74,46 +74,63 @@ namespace tri { } } - /** - Import a trianglemesh from a polygon mesh - **/ - static void ImportFromPolyMesh(TriMeshType & tm, PolyMeshType & pm) - { - tri::RequirePolygonalMesh(pm); - std::vector points; + /** + * @brief Import a trianglemesh from a polygon mesh + * @param tm: output triangle mesh + * @param pm: input polygonal mesh + * @param birthFaces: a mapping that tells, for each face of the triangle mesh, + * which one is its birth face in the polygonal mesh. + */ + static void ImportFromPolyMesh(TriMeshType& tm, PolyMeshType& pm, std::vector& birthFaces) + { + birthFaces.clear(); + birthFaces.reserve(pm.FN()); //at least the same face number of the polymesh + tri::RequirePolygonalMesh(pm); + std::vector points; - // the vertices are the same, simply import them - PolyVertexIterator vi; - TriVertexIterator tvi = Allocator::AddVertices(tm,pm.vert.size()); - int cnt = 0; - for(tvi = tm.vert.begin(),vi = pm.vert.begin(); tvi != tm.vert.end(); ++tvi,++vi,++cnt) - if(!(*vi).IsD()) (*tvi).ImportData(*vi); else tri::Allocator::DeleteVertex(tm,(*tvi)); + // the vertices are the same, simply import them + PolyVertexIterator vi; + TriVertexIterator tvi = Allocator::AddVertices(tm,pm.vert.size()); + int cnt = 0; + for(tvi = tm.vert.begin(),vi = pm.vert.begin(); tvi != tm.vert.end(); ++tvi,++vi,++cnt) + if(!(*vi).IsD()) (*tvi).ImportData(*vi); else tri::Allocator::DeleteVertex(tm,(*tvi)); - for(PolyFaceIterator fi = pm.face.begin(); fi != pm.face.end(); ++fi) - { - if(!((*fi).IsD())){ - points.clear(); - for(int i = 0; i < (*fi).VN(); ++i) { - typename PolyMeshType::VertexType * v = (*fi).V(i); - points.push_back(v->P()); - } - std::vector faces; - TessellatePlanarPolygon3(points,faces); - for(size_t i = 0; i::AddFace(tm, - tri::Index(pm,(*fi).V( faces[i+0] )), - tri::Index(pm,(*fi).V( faces[i+1] )), - tri::Index(pm,(*fi).V( faces[i+2] )) ); + for(PolyFaceIterator fi = pm.face.begin(); fi != pm.face.end(); ++fi) + { + if(!((*fi).IsD())){ + points.clear(); + for(int i = 0; i < (*fi).VN(); ++i) { + typename PolyMeshType::VertexType * v = (*fi).V(i); + points.push_back(v->P()); + } + std::vector faces; + TessellatePlanarPolygon3(points,faces); - tfi->ImportData(*fi); - // set the F flags - if( (faces[i ]+1)%points.size() != size_t(faces[i+1])) (*tfi).SetF(0); - if( (faces[i+1]+1)%points.size() != size_t(faces[i+2])) (*tfi).SetF(1); - if( (faces[i+2]+1)%points.size() != size_t(faces[i ])) (*tfi).SetF(2); - } - } - } - } + //all the faces we add in tm have as a birth face fi + birthFaces.insert(birthFaces.end(), faces.size()/3, tri::Index(pm, *fi)); + + for(size_t i = 0; i::AddFace( + tm, + tri::Index(pm,(*fi).V( faces[i+0] )), + tri::Index(pm,(*fi).V( faces[i+1] )), + tri::Index(pm,(*fi).V( faces[i+2] )) ); + + tfi->ImportData(*fi); + // set the F flags + if( (faces[i ]+1)%points.size() != size_t(faces[i+1])) (*tfi).SetF(0); + if( (faces[i+1]+1)%points.size() != size_t(faces[i+2])) (*tfi).SetF(1); + if( (faces[i+2]+1)%points.size() != size_t(faces[i ])) (*tfi).SetF(2); + } + } + } + } + + static void ImportFromPolyMesh(TriMeshType & tm, PolyMeshType & pm) + { + std::vector dummyVector; + ImportFromPolyMesh(tm, pm, dummyVector); + } /**