2006-02-13 17:15:03 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// VCG headers for triangular mesh processing
|
2008-12-19 11:43:36 +01:00
|
|
|
#include<vcg/simplex/edge/base.h>
|
|
|
|
#include<vcg/simplex/edge/component.h>
|
|
|
|
#include<vcg/simplex/vertex/base.h>
|
|
|
|
#include<vcg/simplex/face/base.h>
|
2006-02-13 17:15:03 +01:00
|
|
|
#include <vcg/complex/trimesh/base.h>
|
|
|
|
#include <vcg/complex/trimesh/update/topology.h>
|
|
|
|
#include <vcg/complex/trimesh/update/edges.h>
|
|
|
|
#include <vcg/complex/trimesh/update/bounding.h>
|
|
|
|
#include <vcg/complex/trimesh/update/flag.h>
|
|
|
|
#include <vcg/complex/trimesh/clean.h>
|
2006-02-13 17:17:06 +01:00
|
|
|
#include <vcg/complex/intersection.h>
|
|
|
|
#include <vcg/space/index/grid_static_ptr.h>
|
2007-12-13 01:31:04 +01:00
|
|
|
//#include <vcg/simplex/edge/with/ae.h>
|
2006-02-13 17:17:06 +01:00
|
|
|
#include <vcg/complex/edgemesh/base.h>
|
|
|
|
#include <vcg/complex/edgemesh/allocate.h>
|
|
|
|
#include <vcg/complex/edgemesh/update/bounding.h>
|
2006-02-13 17:15:03 +01:00
|
|
|
|
2006-02-13 17:17:06 +01:00
|
|
|
// VCG File Format Importer/Exporter
|
2006-02-13 17:15:03 +01:00
|
|
|
#include <wrap/io_trimesh/import.h>
|
2006-02-13 17:17:06 +01:00
|
|
|
#include <wrap/io_edgemesh/export_svg.h>
|
2008-09-30 12:07:34 +02:00
|
|
|
#include <wrap/io_edgemesh/export_dxf.h>
|
2006-02-13 17:15:03 +01:00
|
|
|
|
|
|
|
// VCG Vertex
|
|
|
|
|
|
|
|
// VCG Faces
|
|
|
|
|
|
|
|
using namespace vcg;
|
|
|
|
|
2007-12-13 01:31:04 +01:00
|
|
|
class MyFace;
|
2008-09-30 12:07:34 +02:00
|
|
|
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>{};
|
|
|
|
|
|
|
|
|
|
|
|
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 <MyUsedTypes, face::VertexRef,face::FFAdj, face::BitFlags, face::Normal3f> {};
|
2007-12-13 01:31:04 +01:00
|
|
|
|
2008-09-30 12:07:34 +02:00
|
|
|
class MyEdgeMesh: public vcg::edg::EdgeMesh< vector<MyVertex>, vector<MyEdge> > {};
|
2007-12-13 01:31:04 +01:00
|
|
|
class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyFace > >{};
|
|
|
|
|
|
|
|
|
2006-02-13 17:17:06 +01:00
|
|
|
typedef vcg::GridStaticPtr<MyMesh::FaceType, MyMesh::ScalarType> TriMeshGrid;
|
2006-02-13 17:15:03 +01:00
|
|
|
|
|
|
|
int main(int argc,char ** argv)
|
|
|
|
{
|
|
|
|
if (argc<6)
|
|
|
|
{
|
2006-02-13 17:17:06 +01:00
|
|
|
printf("\n");
|
|
|
|
printf(" Usage: trimesh_intersection <filename> <a> <b> <c> <d>\n\n");
|
|
|
|
printf(" <filename> Mesh model to intersect (PLY format).\n");
|
|
|
|
printf(" <a> <b> <c> <d> The coefficients that specifying a plane in the form:\n\n");
|
|
|
|
printf(" a*x + b*y + c*z + d = 0\n\n\n");
|
2006-02-13 17:15:03 +01:00
|
|
|
|
2006-02-13 17:17:06 +01:00
|
|
|
printf(" Example: trimesh_intersection bunny.ply 0.0 1.0 0.0 0.0\n\n");
|
2006-02-13 17:15:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MyMesh m;
|
|
|
|
|
|
|
|
// open a mesh
|
|
|
|
int err = tri::io::Importer<MyMesh>::Open(m,argv[1]);
|
2006-02-13 17:17:06 +01:00
|
|
|
if(err)
|
2006-02-13 17:15:03 +01:00
|
|
|
{
|
2006-02-13 17:17:06 +01:00
|
|
|
printf("Error in reading %s: '%s'\n",argv[1],tri::io::Importer<MyMesh>::ErrorMsg(err));
|
|
|
|
exit(-1);
|
2006-02-13 17:15:03 +01:00
|
|
|
}
|
|
|
|
|
2006-02-13 17:17:06 +01:00
|
|
|
// some cleaning to get rid of bad file formats like stl that duplicate vertexes..
|
|
|
|
int dup = tri::Clean<MyMesh>::RemoveDuplicateVertex(m);
|
|
|
|
int unref = tri::Clean<MyMesh>::RemoveUnreferencedVertex(m);
|
2006-02-13 17:15:03 +01:00
|
|
|
|
|
|
|
if (dup > 0 || unref > 0)
|
|
|
|
printf("Removed %i duplicate and %i unreferenced vertices from mesh %s\n",dup,unref,argv[1]);
|
|
|
|
|
2006-02-13 17:17:06 +01:00
|
|
|
// Compute cross-intersection with the given plane
|
2006-02-13 17:15:03 +01:00
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
|
2006-02-13 17:17:06 +01:00
|
|
|
MyMesh::ScalarType a = static_cast<MyMesh::ScalarType>(atof(argv[2]));
|
|
|
|
MyMesh::ScalarType b = static_cast<MyMesh::ScalarType>(atof(argv[3]));
|
|
|
|
MyMesh::ScalarType c = static_cast<MyMesh::ScalarType>(atof(argv[4]));
|
|
|
|
MyMesh::ScalarType d = static_cast<MyMesh::ScalarType>(atof(argv[5]));
|
|
|
|
|
|
|
|
vcg::Point3<MyMesh::ScalarType> direction(a, b, c);
|
|
|
|
MyMesh::ScalarType distance = -d / direction.Norm();
|
|
|
|
direction.Normalize();
|
|
|
|
|
|
|
|
vcg::Plane3<MyMesh::ScalarType> plane(distance, direction);
|
|
|
|
|
|
|
|
double avg_length; // average length of the edges
|
|
|
|
MyEdgeMesh edge_mesh; // returned EdgeMesh (i.e. the cross-section)
|
|
|
|
|
|
|
|
// Create a static grid (for fast indexing) and fill it
|
|
|
|
TriMeshGrid static_grid;
|
|
|
|
static_grid.Set(m.face.begin(), m.face.end());
|
|
|
|
std::vector<TriMeshGrid::Cell *> intersected_cells;
|
|
|
|
|
|
|
|
vcg::Intersection<MyMesh, MyEdgeMesh, MyMesh::ScalarType, TriMeshGrid>(plane,
|
|
|
|
edge_mesh, avg_length, &static_grid, intersected_cells);
|
|
|
|
|
|
|
|
// Compute bounding box
|
2008-09-30 12:07:34 +02:00
|
|
|
vcg::edg::UpdateBounding<MyEdgeMesh>::Box(edge_mesh);
|
2006-02-13 17:15:03 +01:00
|
|
|
|
2006-02-13 17:17:06 +01:00
|
|
|
// export the cross-section
|
2009-03-30 22:58:18 +02:00
|
|
|
edg::io::SVGProperties pro;
|
|
|
|
if (edg::io::ExporterSVG<MyEdgeMesh>::Save(edge_mesh, "out.svg",pro))
|
2006-02-13 17:17:06 +01:00
|
|
|
printf(" The cross-intersection has been successfully saved (OUT.SVG).\n");
|
|
|
|
else
|
|
|
|
printf(" The cross-intersection cannot be saved.\n");
|
2006-02-13 17:15:03 +01:00
|
|
|
|
2008-09-30 12:07:34 +02:00
|
|
|
|
2006-02-13 17:17:06 +01:00
|
|
|
return 0;
|
2006-02-13 17:15:03 +01:00
|
|
|
}
|
|
|
|
|