#include using namespace std; // VCG headers for triangular mesh processing #include #include #include #include #include #include #include #include #include #include #include #include //#include #include #include #include // VCG File Format Importer/Exporter #include #include #include // VCG Vertex // VCG Faces using namespace vcg; class MyFace; class MyEdge; class MyVertex; struct MyUsedTypes : public UsedTypes< Use ::AsVertexType, Use ::AsEdgeType, Use ::AsFaceType>{}; class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::BitFlags, vertex::Normal3f, vertex::Mark>{}; class MyEdge : public Edge< MyUsedTypes, edge::VertexRef, edge::EVAdj> {}; class MyFace : public Face {}; class MyEdgeMesh: public vcg::edg::EdgeMesh< vector, vector > {}; class MyMesh : public tri::TriMesh< vector, vector >{}; typedef vcg::GridStaticPtr TriMeshGrid; int main(int argc,char ** argv) { if (argc<6) { printf("\n"); printf(" Usage: trimesh_intersection \n\n"); printf(" Mesh model to intersect (PLY format).\n"); printf(" The coefficients that specifying a plane in the form:\n\n"); printf(" a*x + b*y + c*z + d = 0\n\n\n"); printf(" Example: trimesh_intersection bunny.ply 0.0 1.0 0.0 0.0\n\n"); return 0; } MyMesh m; // open a mesh int err = tri::io::Importer::Open(m,argv[1]); if(err) { printf("Error in reading %s: '%s'\n",argv[1],tri::io::Importer::ErrorMsg(err)); exit(-1); } // some cleaning to get rid of bad file formats like stl that duplicate vertexes.. int dup = tri::Clean::RemoveDuplicateVertex(m); int unref = tri::Clean::RemoveUnreferencedVertex(m); if (dup > 0 || unref > 0) printf("Removed %i duplicate and %i unreferenced vertices from mesh %s\n",dup,unref,argv[1]); // Compute cross-intersection with the given plane ///////////////////////////////////////////////////////// MyMesh::ScalarType a = static_cast(atof(argv[2])); MyMesh::ScalarType b = static_cast(atof(argv[3])); MyMesh::ScalarType c = static_cast(atof(argv[4])); MyMesh::ScalarType d = static_cast(atof(argv[5])); vcg::Point3 direction(a, b, c); MyMesh::ScalarType distance = -d / direction.Norm(); direction.Normalize(); vcg::Plane3 plane(distance, direction); double avg_length; // average length of the edges MyEdgeMesh edge_mesh; // returned EdgeMesh (i.e. the cross-section) // Create a static grid (for fast indexing) and fill it TriMeshGrid static_grid; static_grid.Set(m.face.begin(), m.face.end()); std::vector intersected_cells; vcg::Intersection(plane, edge_mesh, avg_length, &static_grid, intersected_cells); // Compute bounding box vcg::edg::UpdateBounding::Box(edge_mesh); // export the cross-section edg::io::SVGProperties pro; if (edg::io::ExporterSVG::Save(edge_mesh, "out.svg",pro)) printf(" The cross-intersection has been successfully saved (OUT.SVG).\n"); else printf(" The cross-intersection cannot be saved.\n"); return 0; }