trimesh_intersection.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004-2012                                           \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 #include <vcg/complex/complex.h>
00025 #include <vcg/complex/algorithms/update/topology.h>
00026 #include <vcg/complex/algorithms/update/bounding.h>
00027 #include <vcg/complex/algorithms/update/flag.h>
00028 #include <vcg/complex/algorithms/clean.h>
00029 #include <vcg/complex/algorithms/intersection.h>
00030 #include <vcg/space/index/grid_static_ptr.h>
00031 
00032 // VCG File Format Importer/Exporter
00033 #include <wrap/io_trimesh/import.h>
00034 #include <wrap/io_edgemesh/export_svg.h>
00035 #include <wrap/io_edgemesh/export_dxf.h>
00036 
00037 using namespace std;
00038 using namespace vcg;
00039 
00040 class MyFace;
00041 class MyEdge;
00042 class MyVertex;
00043 
00044 struct MyUsedTypes : public UsedTypes<  Use<MyVertex>           ::AsVertexType,
00045                                                                                                                                                                 Use<MyEdge>                     ::AsEdgeType,
00046                                                                                                                                                                 Use<MyFace>                     ::AsFaceType>{};
00047 
00048 
00049 class MyVertex  : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::BitFlags, vertex::Normal3f, vertex::Mark>{};
00050 class MyEdge    : public Edge< MyUsedTypes, edge::VertexRef, edge::EVAdj> {};
00051 class MyFace    : public Face  <MyUsedTypes, face::VertexRef,face::FFAdj, face::BitFlags, face::Normal3f> {};
00052 
00053 class MyEdgeMesh: public tri::TriMesh< vector<MyVertex>, vector<MyEdge> > {};
00054 class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyFace > >{};
00055 
00056 
00057 typedef vcg::GridStaticPtr<MyMesh::FaceType, MyMesh::ScalarType> TriMeshGrid;
00058 
00059 int main(int argc,char ** argv)
00060 {
00061         if (argc<6) 
00062         {
00063                 printf("\n");
00064                 printf("    Usage: trimesh_intersection <filename> <a> <b> <c> <d>\n\n");
00065                 printf("       <filename>        Mesh model to intersect (PLY format).\n");
00066                 printf("       <a> <b> <c> <d>   The coefficients that specifying a plane in the form:\n\n");
00067                 printf("                             a*x + b*y + c*z + d = 0\n\n\n");
00068 
00069                 printf("       Example: trimesh_intersection bunny.ply 0.0 1.0 0.0 0.0\n\n");
00070 
00071                 return 0;
00072         }
00073 
00074         MyMesh m;
00075 
00076         // open a mesh
00077         int err = tri::io::Importer<MyMesh>::Open(m,argv[1]);
00078         if(err) 
00079         {
00080                 printf("Error in reading %s: '%s'\n",argv[1],tri::io::Importer<MyMesh>::ErrorMsg(err));
00081                 exit(-1);  
00082         }
00083 
00084         // some cleaning to get rid of bad file formats like stl that duplicate vertexes..
00085         int dup = tri::Clean<MyMesh>::RemoveDuplicateVertex(m);
00086         int unref =  tri::Clean<MyMesh>::RemoveUnreferencedVertex(m);
00087 
00088         if (dup > 0 || unref > 0)
00089                 printf("Removed %i duplicate and %i unreferenced vertices from mesh %s\n",dup,unref,argv[1]);
00090 
00091         // Compute cross-intersection with the given plane
00093 
00094         MyMesh::ScalarType a = static_cast<MyMesh::ScalarType>(atof(argv[2]));
00095         MyMesh::ScalarType b = static_cast<MyMesh::ScalarType>(atof(argv[3]));
00096         MyMesh::ScalarType c = static_cast<MyMesh::ScalarType>(atof(argv[4]));
00097         MyMesh::ScalarType d = static_cast<MyMesh::ScalarType>(atof(argv[5]));
00098 
00099         vcg::Point3<MyMesh::ScalarType> direction(a, b, c);
00100         MyMesh::ScalarType distance = -d / direction.Norm();
00101         direction.Normalize();
00102 
00103         vcg::Plane3<MyMesh::ScalarType> plane(distance, direction);
00104 
00105         MyEdgeMesh edge_mesh;  // returned EdgeMesh (i.e. the cross-section)
00106 
00107         vcg::IntersectionPlaneMesh<MyMesh, MyEdgeMesh, MyMesh::ScalarType>(m, plane, edge_mesh);
00108 
00109         // Compute bounding box
00110         vcg::tri::UpdateBounding<MyEdgeMesh>::Box(edge_mesh);
00111 
00112         // export the cross-section
00113         tri::io::SVGProperties pro;
00114         if (tri::io::ExporterSVG<MyEdgeMesh>::Save(edge_mesh, "out.svg",pro))
00115                 printf("    The cross-intersection has been successfully saved (OUT.SVG).\n");
00116         else
00117                 printf("    The cross-intersection cannot be saved.\n");
00118 
00119 
00120         return 0;
00121 }
00122 


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:38:20