vcglib/apps/sample/trimesh_intersection_plane/trimesh_intersection_plane.cpp

107 lines
5.1 KiB
C++
Raw Normal View History

/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004-2016 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* 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
*/
2006-02-13 17:15:03 +01:00
#include <vcg/complex/complex.h>
#include <vcg/complex/algorithms/clean.h>
#include <vcg/complex/algorithms/intersection.h>
#include <vcg/complex/algorithms/refine.h>
#include <vcg/complex/algorithms/create/platonic.h>
2006-02-13 17:15:03 +01:00
2006-02-13 17:17:06 +01:00
#include <wrap/io_edgemesh/export_svg.h>
#include <wrap/io_edgemesh/export_dxf.h>
#include <wrap/io_trimesh/export_off.h>
2006-02-13 17:15:03 +01:00
using namespace std;
2006-02-13 17:15:03 +01:00
using namespace vcg;
2007-12-13 01:31:04 +01:00
class MyFace;
class MyEdge;
[ Changes in definition of TriMesh: PART I ] Note for the developers: the change to make to existing projects is very little but strictly necessary to compile. This change IS NOT backward compliant. ==== OLD ==== way to define a TriMesh: // forward declarations class MyVertex; class MyEdge; class MyFace; class MyVertex: public VertexSimp2 < MyVertex, MyEdge, MyFace, vertex::Coord3f,...other components>{}; class MyFace: public FaceSimp2 < MyVertex, MyEdge, MyFace, face::VertexRef,...other components>{}; class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{}; ==== NEW ==== way to define a TriMesh: // forward declarations class MyVertex; class MyEdge; class MyFace; // declaration of which types is used as VertexType, which type is used as FaceType and so on... class MyUsedTypes: public vcg::UsedType < vcg::Use<MyVertex>::AsVertexType, vcg::Use<MyFace>::AsFaceType>{}; class MyVertex: public Vertex < MyUsedTypes, vertex::Coord3f,...other components>{}; class MyFace: public Face < MyUsedTypes, face::VertexRef,...other components>{}; class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{}; ===== classes introduced [vcg::UsedType] : it is a class containing all the types that must be passed to the definition of Vertex, Face, Edge... This class replaces the list of typenames to pass as first templates and the need to specify the maximal simplicial. So <MyVertex, MyEdge, MyFace becomes <MyUsedTypes< and VertexSimp2 becomes Vertex [vcg::Use] : an auxiliary class to give a simple way to specify the role of a type Note 2: the order of templates parameters to vcg::UsedTypes is unimportant, e.g: class MyUsedTypes: public vcg::UsedType <vcg::Use<MyVertex>::AsVertexType, vcg::Use<MyEdge>::AsEdgeType, vcg::Use<MyFace>::AsFaceType>{}; is the same as: class MyUsedTypes: public vcg::UsedType <vcg::Use<MyFace>::AsFaceType, vcg::Use<MyEdge>::AsEdgeType, vcg::Use<MyVertex>::AsVertexType>{}; Note 3: you only need to specify the type you use. If you do not have edges you do not need to include vcg::Use<MyEdge>::AsEdgeType in the template list of UsedTypes. ==== the Part II will be a tiny change to the class TriMesh it self.
2010-03-15 11:44:40 +01:00
class MyVertex;
struct MyUsedTypes : public UsedTypes<
Use<MyVertex>::AsVertexType,
Use<MyEdge> ::AsEdgeType,
Use<MyFace> ::AsFaceType>{};
[ Changes in definition of TriMesh: PART I ] Note for the developers: the change to make to existing projects is very little but strictly necessary to compile. This change IS NOT backward compliant. ==== OLD ==== way to define a TriMesh: // forward declarations class MyVertex; class MyEdge; class MyFace; class MyVertex: public VertexSimp2 < MyVertex, MyEdge, MyFace, vertex::Coord3f,...other components>{}; class MyFace: public FaceSimp2 < MyVertex, MyEdge, MyFace, face::VertexRef,...other components>{}; class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{}; ==== NEW ==== way to define a TriMesh: // forward declarations class MyVertex; class MyEdge; class MyFace; // declaration of which types is used as VertexType, which type is used as FaceType and so on... class MyUsedTypes: public vcg::UsedType < vcg::Use<MyVertex>::AsVertexType, vcg::Use<MyFace>::AsFaceType>{}; class MyVertex: public Vertex < MyUsedTypes, vertex::Coord3f,...other components>{}; class MyFace: public Face < MyUsedTypes, face::VertexRef,...other components>{}; class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{}; ===== classes introduced [vcg::UsedType] : it is a class containing all the types that must be passed to the definition of Vertex, Face, Edge... This class replaces the list of typenames to pass as first templates and the need to specify the maximal simplicial. So <MyVertex, MyEdge, MyFace becomes <MyUsedTypes< and VertexSimp2 becomes Vertex [vcg::Use] : an auxiliary class to give a simple way to specify the role of a type Note 2: the order of templates parameters to vcg::UsedTypes is unimportant, e.g: class MyUsedTypes: public vcg::UsedType <vcg::Use<MyVertex>::AsVertexType, vcg::Use<MyEdge>::AsEdgeType, vcg::Use<MyFace>::AsFaceType>{}; is the same as: class MyUsedTypes: public vcg::UsedType <vcg::Use<MyFace>::AsFaceType, vcg::Use<MyEdge>::AsEdgeType, vcg::Use<MyVertex>::AsVertexType>{}; Note 3: you only need to specify the type you use. If you do not have edges you do not need to include vcg::Use<MyEdge>::AsEdgeType in the template list of UsedTypes. ==== the Part II will be a tiny change to the class TriMesh it self.
2010-03-15 11:44:40 +01:00
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> {};
2007-12-13 01:31:04 +01:00
class MyEdgeMesh: public tri::TriMesh< vector<MyVertex>, vector<MyEdge> > {};
class MyMesh: public tri::TriMesh< vector<MyVertex>, vector<MyFace > >{};
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<float,math::MarsenneTwisterRNG>(rnd);
float distance = rnd.generate01();
vcg::Plane3<MyMesh::ScalarType> 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<MyMesh, MyEdgeMesh, MyMesh::ScalarType>(m, plane, edge_mesh);
// Compute bounding box
vcg::tri::UpdateBounding<MyEdgeMesh>::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<MyEdgeMesh>::Save(edge_mesh, "trimesh_intersection.svg",pro);
// Now actually cut the mesh using the refine framework
tri::UpdateQuality<MyMesh>::VertexFromPlane(m, plane);
tri::QualityMidPointFunctor<MyMesh> slicingfunc(0.0);
tri::QualityEdgePredicate<MyMesh> slicingpred(0.0,0.0);
tri::UpdateTopology<MyMesh>::FaceFace(m);
tri::RefineE<MyMesh, tri::QualityMidPointFunctor<MyMesh>, tri::QualityEdgePredicate<MyMesh> > (m, slicingfunc, slicingpred, false);
tri::UpdateSelection<MyMesh>::VertexFromQualityRange(m,0,std::numeric_limits<float>::max());
tri::UpdateSelection<MyMesh>::FaceFromVertexStrict(m);
tri::Append<MyMesh,MyMesh>::Mesh(m_over,m,true);
tri::UpdateSelection<MyMesh>::FaceInvert(m);
tri::Append<MyMesh,MyMesh>::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<MyMesh>::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<MyMesh>::Save(m_under,"trimesh_intersection_under.off");
return 0;
2006-02-13 17:15:03 +01:00
}