00001 #include <vector>
00002
00003 using namespace std;
00004
00005
00006 #include<vcg/simplex/edge/base.h>
00007 #include<vcg/simplex/edge/component.h>
00008 #include<vcg/simplex/vertex/base.h>
00009 #include<vcg/simplex/face/base.h>
00010 #include <vcg/complex/trimesh/base.h>
00011 #include <vcg/complex/trimesh/update/topology.h>
00012 #include <vcg/complex/trimesh/update/edges.h>
00013 #include <vcg/complex/trimesh/update/bounding.h>
00014 #include <vcg/complex/trimesh/update/flag.h>
00015 #include <vcg/complex/trimesh/clean.h>
00016 #include <vcg/complex/intersection.h>
00017 #include <vcg/space/index/grid_static_ptr.h>
00018
00019 #include <vcg/complex/edgemesh/base.h>
00020 #include <vcg/complex/edgemesh/allocate.h>
00021 #include <vcg/complex/edgemesh/update/bounding.h>
00022
00023
00024 #include <wrap/io_trimesh/import.h>
00025 #include <wrap/io_edgemesh/export_svg.h>
00026 #include <wrap/io_edgemesh/export_dxf.h>
00027
00028
00029
00030
00031
00032 using namespace vcg;
00033
00034 class MyFace;
00035 class MyEdge;
00036 class MyVertex;
00037
00038 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
00039 Use<MyEdge> ::AsEdgeType,
00040 Use<MyFace> ::AsFaceType>{};
00041
00042
00043 class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::BitFlags, vertex::Normal3f, vertex::Mark>{};
00044 class MyEdge : public Edge< MyUsedTypes, edge::VertexRef, edge::EVAdj> {};
00045 class MyFace : public Face <MyUsedTypes, face::VertexRef,face::FFAdj, face::BitFlags, face::Normal3f> {};
00046
00047 class MyEdgeMesh: public vcg::edg::EdgeMesh< vector<MyVertex>, vector<MyEdge> > {};
00048 class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyFace > >{};
00049
00050
00051 typedef vcg::GridStaticPtr<MyMesh::FaceType, MyMesh::ScalarType> TriMeshGrid;
00052
00053 int main(int argc,char ** argv)
00054 {
00055 if (argc<6)
00056 {
00057 printf("\n");
00058 printf(" Usage: trimesh_intersection <filename> <a> <b> <c> <d>\n\n");
00059 printf(" <filename> Mesh model to intersect (PLY format).\n");
00060 printf(" <a> <b> <c> <d> The coefficients that specifying a plane in the form:\n\n");
00061 printf(" a*x + b*y + c*z + d = 0\n\n\n");
00062
00063 printf(" Example: trimesh_intersection bunny.ply 0.0 1.0 0.0 0.0\n\n");
00064
00065 return 0;
00066 }
00067
00068 MyMesh m;
00069
00070
00071 int err = tri::io::Importer<MyMesh>::Open(m,argv[1]);
00072 if(err)
00073 {
00074 printf("Error in reading %s: '%s'\n",argv[1],tri::io::Importer<MyMesh>::ErrorMsg(err));
00075 exit(-1);
00076 }
00077
00078
00079 int dup = tri::Clean<MyMesh>::RemoveDuplicateVertex(m);
00080 int unref = tri::Clean<MyMesh>::RemoveUnreferencedVertex(m);
00081
00082 if (dup > 0 || unref > 0)
00083 printf("Removed %i duplicate and %i unreferenced vertices from mesh %s\n",dup,unref,argv[1]);
00084
00085
00087
00088 MyMesh::ScalarType a = static_cast<MyMesh::ScalarType>(atof(argv[2]));
00089 MyMesh::ScalarType b = static_cast<MyMesh::ScalarType>(atof(argv[3]));
00090 MyMesh::ScalarType c = static_cast<MyMesh::ScalarType>(atof(argv[4]));
00091 MyMesh::ScalarType d = static_cast<MyMesh::ScalarType>(atof(argv[5]));
00092
00093 vcg::Point3<MyMesh::ScalarType> direction(a, b, c);
00094 MyMesh::ScalarType distance = -d / direction.Norm();
00095 direction.Normalize();
00096
00097 vcg::Plane3<MyMesh::ScalarType> plane(distance, direction);
00098
00099 double avg_length;
00100 MyEdgeMesh edge_mesh;
00101
00102
00103 TriMeshGrid static_grid;
00104 static_grid.Set(m.face.begin(), m.face.end());
00105 std::vector<TriMeshGrid::Cell *> intersected_cells;
00106
00107 vcg::Intersection<MyMesh, MyEdgeMesh, MyMesh::ScalarType, TriMeshGrid>(plane,
00108 edge_mesh, avg_length, &static_grid, intersected_cells);
00109
00110
00111 vcg::edg::UpdateBounding<MyEdgeMesh>::Box(edge_mesh);
00112
00113
00114 edg::io::SVGProperties pro;
00115 if (edg::io::ExporterSVG<MyEdgeMesh>::Save(edge_mesh, "out.svg",pro))
00116 printf(" The cross-intersection has been successfully saved (OUT.SVG).\n");
00117 else
00118 printf(" The cross-intersection cannot be saved.\n");
00119
00120
00121 return 0;
00122 }
00123