2012-10-09 10:43:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
2016-06-13 07:29:25 +02:00
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
2012-10-09 10:43:35 +02:00
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2005-09-28 22:14:53 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2011-04-01 19:07:57 +02:00
|
|
|
#include<vcg/complex/complex.h>
|
2012-10-09 10:43:35 +02:00
|
|
|
#include<vcg/simplex/face/distance.h>
|
2012-10-09 09:12:31 +02:00
|
|
|
#include<vcg/simplex/face/component_ep.h>
|
2011-04-01 19:07:57 +02:00
|
|
|
#include <vcg/complex/algorithms/create/platonic.h>
|
|
|
|
#include <vcg/complex/algorithms/update/normal.h>
|
2012-10-09 09:12:31 +02:00
|
|
|
#include <vcg/complex/algorithms/update/component_ep.h>
|
2011-04-01 19:07:57 +02:00
|
|
|
#include <vcg/complex/algorithms/update/flag.h>
|
2005-09-29 13:52:22 +02:00
|
|
|
#include <vcg/space/intersection3.h>
|
2005-09-28 22:14:53 +02:00
|
|
|
#include <vcg/space/index/aabb_binary_tree/aabb_binary_tree.h>
|
|
|
|
|
|
|
|
typedef float AScalarType;
|
|
|
|
|
2006-10-13 15:17:23 +02:00
|
|
|
using namespace vcg;
|
|
|
|
|
[ 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 AVertex;
|
2005-09-28 22:14:53 +02:00
|
|
|
class AFace;
|
[ 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
|
|
|
|
|
|
|
struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<AVertex> ::AsVertexType,
|
|
|
|
vcg::Use<AFace> ::AsFaceType>{};
|
|
|
|
|
|
|
|
class AVertex : public Vertex< MyUsedTypes, vertex::Normal3f, vertex::Coord3f,vertex::BitFlags >{};
|
|
|
|
class AFace : public Face< MyUsedTypes, face::VertexRef, face::Normal3f, face::EdgePlane, face::BitFlags> {};
|
2005-09-28 22:14:53 +02:00
|
|
|
class AMesh : public vcg::tri::TriMesh< std::vector<AVertex>, std::vector<AFace> > { };
|
|
|
|
|
|
|
|
typedef vcg::AABBBinaryTreeIndex<AFace, AScalarType, vcg::EmptyClass> AIndex;
|
|
|
|
|
|
|
|
static AMesh gMesh;
|
|
|
|
static AIndex gIndex;
|
|
|
|
|
|
|
|
static void CreateMesh(void) {
|
|
|
|
vcg::tri::Dodecahedron<AMesh>(gMesh);
|
|
|
|
|
|
|
|
vcg::tri::UpdateFlags<AMesh>::Clear(gMesh);
|
2012-10-09 09:12:31 +02:00
|
|
|
vcg::tri::UpdateNormal<AMesh>::PerVertexNormalized(gMesh);
|
|
|
|
vcg::tri::UpdateComponentEP<AMesh>::Set(gMesh);
|
2005-09-28 22:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void SetIndex(void) {
|
|
|
|
gIndex.Set(gMesh.face.begin(), gMesh.face.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestClosest(void) {
|
2012-10-09 09:12:31 +02:00
|
|
|
vcg::face::PointDistanceEPFunctor<AIndex::ScalarType> getPtDist;
|
2005-09-28 22:14:53 +02:00
|
|
|
const AIndex::CoordType queryPoint((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
|
|
|
|
const AIndex::ScalarType maxDist = std::numeric_limits<AIndex::ScalarType>::max();
|
|
|
|
|
|
|
|
AIndex::ObjPtr closestFace;
|
|
|
|
AIndex::ScalarType closestDist;
|
|
|
|
AIndex::CoordType closestPoint;
|
|
|
|
|
2008-09-30 12:12:51 +02:00
|
|
|
vcg::EmptyClass a;
|
|
|
|
closestFace = gIndex.GetClosest(getPtDist, a, queryPoint, maxDist, closestDist, closestPoint);
|
2005-09-28 22:14:53 +02:00
|
|
|
|
|
|
|
printf("GetClosest Test:\n");
|
|
|
|
|
|
|
|
if (closestFace != 0) {
|
|
|
|
printf("\tface : 0x%p\n", closestFace);
|
|
|
|
printf("\tdistance : %f\n", closestDist);
|
|
|
|
printf("\tpoint : [%f, %f, %f]\n", closestPoint[0], closestPoint[1], closestPoint[2]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("\tno object found (index is probably empty).\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestKClosest(void) {
|
2012-10-09 09:12:31 +02:00
|
|
|
vcg::face::PointDistanceEPFunctor<AIndex::ScalarType> getPtDist;
|
2005-09-28 22:14:53 +02:00
|
|
|
const unsigned int k = 10;
|
|
|
|
const AIndex::CoordType queryPoint((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
|
|
|
|
const AIndex::ScalarType maxDist = std::numeric_limits<AIndex::ScalarType>::max();
|
|
|
|
|
|
|
|
std::vector<AIndex::ObjPtr> closestObjects;
|
|
|
|
std::vector<AIndex::ScalarType> closestDistances;
|
|
|
|
std::vector<AIndex::CoordType> closestPoints;
|
|
|
|
|
2008-09-30 12:12:51 +02:00
|
|
|
vcg::EmptyClass a;
|
|
|
|
unsigned int rk = gIndex.GetKClosest(getPtDist, a, k, queryPoint, maxDist, closestObjects, closestDistances, closestPoints);
|
2005-09-28 22:14:53 +02:00
|
|
|
|
|
|
|
printf("GetKClosest Test:\n");
|
|
|
|
printf("\tfound %d objects\n", rk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestRay(void) {
|
|
|
|
const bool TEST_BACK_FACES = true;
|
|
|
|
|
2005-09-29 13:52:22 +02:00
|
|
|
vcg::RayTriangleIntersectionFunctor<TEST_BACK_FACES> rayIntersector;
|
2005-09-28 22:14:53 +02:00
|
|
|
const AIndex::ScalarType maxDist = std::numeric_limits<AIndex::ScalarType>::max();
|
|
|
|
const AIndex::CoordType rayOrigin((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
|
|
|
|
const AIndex::CoordType rayDirection((AIndex::ScalarType)1, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
|
|
|
|
const vcg::Ray3<AIndex::ScalarType, false> ray(rayOrigin, rayDirection);
|
|
|
|
|
|
|
|
AIndex::ObjPtr isectFace;
|
|
|
|
AIndex::ScalarType rayT;
|
|
|
|
AIndex::CoordType isectPt;
|
|
|
|
|
2008-09-30 12:12:51 +02:00
|
|
|
vcg::EmptyClass a;
|
|
|
|
isectFace = gIndex.DoRay(rayIntersector, a , ray, maxDist, rayT);
|
2005-09-28 22:14:53 +02:00
|
|
|
|
|
|
|
printf("DoRay Test:\n");
|
|
|
|
if (isectFace != 0) {
|
|
|
|
printf("\tface : 0x%p\n", isectFace);
|
|
|
|
printf("\tray t : %f\n", rayT);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("\tno object found (index is probably empty).\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-14 10:29:32 +01:00
|
|
|
int main (int /*argc*/, char ** /*argv*/) {
|
2005-09-28 22:14:53 +02:00
|
|
|
CreateMesh();
|
|
|
|
|
|
|
|
SetIndex();
|
|
|
|
|
|
|
|
printf("Spatial Index Tests\n");
|
|
|
|
printf("---\n");
|
|
|
|
TestClosest();
|
|
|
|
printf("---\n");
|
|
|
|
TestKClosest();
|
|
|
|
printf("---\n");
|
|
|
|
TestRay();
|
|
|
|
printf("---\n");
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|