diff --git a/apps/sample/trimesh_intersection_plane/trimesh_intersection_plane.cpp b/apps/sample/trimesh_intersection_plane/trimesh_intersection_plane.cpp index 68c8966b..7431770b 100644 --- a/apps/sample/trimesh_intersection_plane/trimesh_intersection_plane.cpp +++ b/apps/sample/trimesh_intersection_plane/trimesh_intersection_plane.cpp @@ -20,16 +20,24 @@ * for more details. * * * ****************************************************************************/ +/*! \file trimesh_intersection_plane.cpp +\ingroup code_sample + +\brief An example of computing the intersection of a mesh with a plane, +saving this polyline as a well projected 2D SVG +and splicing the mesh in the two components below and over the plane + +*/ #include #include #include -#include +#include +#include -// VCG File Format Importer/Exporter -#include #include #include +#include using namespace std; using namespace vcg; @@ -38,82 +46,61 @@ class MyFace; class MyEdge; class MyVertex; -struct MyUsedTypes : public UsedTypes< Use ::AsVertexType, - Use ::AsEdgeType, - Use ::AsFaceType>{}; +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 MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::BitFlags, vertex::Normal3f, vertex::Qualityf, vertex::Mark>{}; +class MyEdge : public Edge < MyUsedTypes, edge::VertexRef, edge::EVAdj> {}; +class MyFace : public Face < MyUsedTypes, face::VertexRef, face::FFAdj, face::BitFlags, face::Normal3f> {}; class MyEdgeMesh: public tri::TriMesh< vector, vector > {}; -class MyMesh : public tri::TriMesh< 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); - - MyEdgeMesh edge_mesh; // returned EdgeMesh (i.e. the cross-section) - - vcg::IntersectionPlaneMesh(m, plane, edge_mesh); - - // Compute bounding box - vcg::tri::UpdateBounding::Box(edge_mesh); - - // export the cross-section - tri::io::SVGProperties pro; - if (tri::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; +int main(int ,char **) +{ + MyMesh m, m_over, m_under;; + Sphere(m); + printf("Created a sphere mesh of %i vertices %i edges\n",m.VN(),m.FN()); + + // Compute intersection with a random plane + math::MarsenneTwisterRNG rnd; + Point3f direction = vcg::math::GeneratePointOnUnitSphereUniform(rnd); + float distance = rnd.generate01(); + vcg::Plane3 plane(distance, direction); + + printf("Intersecting a sphere with a plane %4.2f %4.2f %4.2f off: %4.2f\n",direction[0],direction[1],direction[2],distance); + MyEdgeMesh edge_mesh; // the cross-section + vcg::IntersectionPlaneMesh(m, plane, edge_mesh); + + // Compute bounding box + vcg::tri::UpdateBounding::Box(edge_mesh); + printf("Created a edge mesh of %i vertices %i edges\n",edge_mesh.VN(),edge_mesh.EN()); + + // export the cross-section (projected along the plane normal direction) + tri::io::SVGProperties pro; + pro.projDir = plane.Direction(); + tri::io::ExporterSVG::Save(edge_mesh, "trimesh_intersection.svg",pro); + + // Now actually cut the mesh using the refine framework + tri::UpdateQuality::VertexFromPlane(m, plane); + tri::QualityMidPointFunctor slicingfunc(0.0); + tri::QualityEdgePredicate slicingpred(0.0,0.0); + tri::UpdateTopology::FaceFace(m); + tri::RefineE, tri::QualityEdgePredicate > (m, slicingfunc, slicingpred, false); + + tri::UpdateSelection::VertexFromQualityRange(m,0,std::numeric_limits::max()); + tri::UpdateSelection::FaceFromVertexStrict(m); + tri::Append::Mesh(m_over,m,true); + tri::UpdateSelection::FaceInvert(m); + tri::Append::Mesh(m_under,m,true); + printf("Created a sphere mesh of %i vertices %i edges\n",m_over.VN(),m_over.FN()); + tri::io::ExporterOFF::Save(m_over,"trimesh_intersection_over.off"); + printf("Created a sphere mesh of %i vertices %i edges\n",m_under.VN(),m_under.FN()); + tri::io::ExporterOFF::Save(m_under,"trimesh_intersection_under.off"); + + return 0; } diff --git a/apps/sample/trimesh_intersection_plane/trimesh_intersection_plane.pro b/apps/sample/trimesh_intersection_plane/trimesh_intersection_plane.pro index e3b6b91e..901b8ad6 100644 --- a/apps/sample/trimesh_intersection_plane/trimesh_intersection_plane.pro +++ b/apps/sample/trimesh_intersection_plane/trimesh_intersection_plane.pro @@ -1,3 +1,3 @@ include(../common.pri) -TARGET = trimesh_intersection -SOURCES += trimesh_intersection.cpp ../../../wrap/ply/plylib.cpp +TARGET = trimesh_intersection_plane +SOURCES += trimesh_intersection_plane.cpp