VCG Library
trimesh_intersection_plane.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
32 #include <vcg/complex/complex.h>
33 #include <vcg/complex/algorithms/clean.h>
34 #include <vcg/complex/algorithms/intersection.h>
35 #include <vcg/complex/algorithms/refine.h>
36 #include <vcg/complex/algorithms/create/platonic.h>
37 
38 #include <wrap/io_edgemesh/export_svg.h>
39 #include <wrap/io_edgemesh/export_dxf.h>
40 #include <wrap/io_trimesh/export_off.h>
41 
42 using namespace std;
43 using namespace vcg;
44 
45 class MyFace;
46 class MyEdge;
47 class MyVertex;
48 
49 struct MyUsedTypes : public UsedTypes<
50  Use<MyVertex>::AsVertexType,
51  Use<MyEdge> ::AsEdgeType,
52  Use<MyFace> ::AsFaceType>{};
53 
54 
55 class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::BitFlags, vertex::Normal3f, vertex::Qualityf, vertex::Mark>{};
56 class MyEdge : public Edge < MyUsedTypes, edge::VertexRef, edge::EVAdj> {};
57 class MyFace : public Face < MyUsedTypes, face::VertexRef, face::FFAdj, face::BitFlags, face::Normal3f> {};
58 
59 class MyEdgeMesh: public tri::TriMesh< vector<MyVertex>, vector<MyEdge> > {};
60 class MyMesh: public tri::TriMesh< vector<MyVertex>, vector<MyFace > >{};
61 
62 int main(int ,char **)
63 {
64  MyMesh m, m_over, m_under;;
65  Sphere(m);
66  printf("Created a sphere mesh of %i vertices %i edges\n",m.VN(),m.FN());
67 
68  // Compute intersection with a random plane
69  math::MarsenneTwisterRNG rnd;
70  Point3f direction = vcg::math::GeneratePointOnUnitSphereUniform<float,math::MarsenneTwisterRNG>(rnd);
71  float distance = rnd.generate01();
72  vcg::Plane3<MyMesh::ScalarType> plane(distance, direction);
73 
74  printf("Intersecting a sphere with a plane %4.2f %4.2f %4.2f off: %4.2f\n",direction[0],direction[1],direction[2],distance);
75  MyEdgeMesh edge_mesh; // the cross-section
76  vcg::IntersectionPlaneMesh<MyMesh, MyEdgeMesh, MyMesh::ScalarType>(m, plane, edge_mesh);
77 
78  // Compute bounding box
80  printf("Created a edge mesh of %i vertices %i edges\n",edge_mesh.VN(),edge_mesh.EN());
81 
82  // export the cross-section (projected along the plane normal direction)
83  tri::io::SVGProperties pro;
84  pro.projDir = plane.Direction();
85  tri::io::ExporterSVG<MyEdgeMesh>::Save(edge_mesh, "trimesh_intersection.svg",pro);
86 
87  // Now actually cut the mesh using the refine framework
89  tri::QualityMidPointFunctor<MyMesh> slicingfunc(0.0);
90  tri::QualityEdgePredicate<MyMesh> slicingpred(0.0,0.0);
92  tri::RefineE<MyMesh, tri::QualityMidPointFunctor<MyMesh>, tri::QualityEdgePredicate<MyMesh> > (m, slicingfunc, slicingpred, false);
93 
94  tri::UpdateSelection<MyMesh>::VertexFromQualityRange(m,0,std::numeric_limits<float>::max());
96  tri::Append<MyMesh,MyMesh>::Mesh(m_over,m,true);
98  tri::Append<MyMesh,MyMesh>::Mesh(m_under,m,true);
99  printf("Created a sphere mesh of %i vertices %i edges\n",m_over.VN(),m_over.FN());
100  tri::io::ExporterOFF<MyMesh>::Save(m_over,"trimesh_intersection_over.off");
101  printf("Created a sphere mesh of %i vertices %i edges\n",m_under.VN(),m_under.FN());
102  tri::io::ExporterOFF<MyMesh>::Save(m_under,"trimesh_intersection_under.off");
103 
104  return 0;
105 }
106