First Commit.

This commit is contained in:
Marco Di Benedetto 2005-09-28 20:14:53 +00:00
parent 7f70a0b847
commit 21d8967063
3 changed files with 263 additions and 0 deletions

View File

@ -0,0 +1,125 @@
// standard headers
#include <stdio.h>
// stl headers
#include <vector>
// vcg headers
#include<vcg/simplex/vertex/vertex.h>
#include<vcg/simplex/face/with/rtfmfn.h>
#include<vcg/simplex/face/distance.h>
#include<vcg/complex/trimesh/base.h>
#include <vcg/complex/trimesh/create/platonic.h>
#include <vcg/complex/trimesh/update/normal.h>
#include <vcg/complex/trimesh/update/edges.h>
#include <vcg/complex/trimesh/update/flag.h>
#include <vcg/space/index/aabb_binary_tree/aabb_binary_tree.h>
#include <vcg/space/index/aabb_binary_tree/face_functors.h>
typedef float AScalarType;
class AEdge;
class AFace;
class AVertex : public vcg::Vertex< AScalarType, AEdge, AFace > { };
class AFace : public vcg::FaceRTFMFN< AVertex, AEdge, AFace > { };
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);
vcg::tri::UpdateNormals<AMesh>::PerVertexNormalized(gMesh);
vcg::tri::UpdateEdges<AMesh>::Set(gMesh);
}
static void SetIndex(void) {
gIndex.Set(gMesh.face.begin(), gMesh.face.end());
}
static void TestClosest(void) {
vcg::face::PointDistanceFunctor getPtDist;
const AIndex::ScalarType x = 0;
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;
closestFace = gIndex.GetClosest(getPtDist, vcg::EmptyClass(), queryPoint, maxDist, closestDist, closestPoint);
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) {
vcg::face::PointDistanceFunctor getPtDist;
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;
unsigned int rk = gIndex.GetKClosest(getPtDist, vcg::EmptyClass(), k, queryPoint, maxDist, closestObjects, closestDistances, closestPoints);
printf("GetKClosest Test:\n");
printf("\tfound %d objects\n", rk);
}
static void TestRay(void) {
const bool TEST_BACK_FACES = true;
vcg::FaceRayIntersectFunctor<TEST_BACK_FACES> rayIntersector;
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;
isectFace = gIndex.DoRay(rayIntersector, vcg::EmptyClass(), ray, maxDist, rayT);
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");
}
}
int main (int argc, char ** argv) {
CreateMesh();
SetIndex();
printf("Spatial Index Tests\n");
printf("---\n");
TestClosest();
printf("---\n");
TestKClosest();
printf("---\n");
TestRay();
printf("---\n");
return (0);
}

View File

@ -0,0 +1,11 @@
######################################################################
# Automatically generated by qmake (2.00a) ven 24. giu 14:14:20 2005
######################################################################
TARGET = aabb_binary_tree
LIBPATH +=
DEPENDPATH += .
INCLUDEPATH += . ../../..
CONFIG += console
TEMPLATE = app
SOURCES += aabb_binary_tree.cpp

View File

@ -0,0 +1,127 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* 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. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
****************************************************************************/
#ifndef __VCGLIB_AABBBINARYTREEINDEX_H
#define __VCGLIB_AABBBINARYTREEINDEX_H
// vcg headers
#include <vcg/space/index/base.h>
#include <vcg/space/index/aabb_binary_tree/base.h>
#include <vcg/space/index/aabb_binary_tree/closest.h>
#include <vcg/space/index/aabb_binary_tree/kclosest.h>
#include <vcg/space/index/aabb_binary_tree/ray.h>
#include <wrap/utils.h>
/***************************************************************************/
namespace vcg {
template <class OBJTYPE, class SCALARTYPE, class NODEAUXDATA = EmptyClass>
class AABBBinaryTreeIndex : public SpatialIndex<OBJTYPE, SCALARTYPE> {
public:
typedef AABBBinaryTreeIndex<OBJTYPE, SCALARTYPE, NODEAUXDATA> ClassType;
typedef OBJTYPE ObjType;
typedef SCALARTYPE ScalarType;
typedef NODEAUXDATA NodeAuxData;
typedef ObjType * ObjPtr;
typedef Point3<ScalarType> CoordType;
typedef AABBBinaryTree<ObjType, ScalarType, NodeAuxData> TreeType;
inline TreeType & Tree(void) {
return (this->tree);
}
inline const TreeType & Tree(void) const {
return (this->tree);
}
template <class OBJITER>
inline void Set(const OBJITER & _oBegin, const OBJITER & _oEnd) {
class GetBoxFunctor {
public:
void operator () (const ObjType & obj, Box3<ScalarType> & box) {
obj.GetBBox(box);
}
};
class GetBarycenterFunctor {
public:
void operator () (const ObjType & obj, CoordType & bar) {
bar = obj.Barycenter();
}
};
GetPointerFunctor getPtr;
GetBoxFunctor getBox;
GetBarycenterFunctor getBarycenter;
const unsigned int divs = 100;
const unsigned int size = (unsigned int)(std::distance(_oBegin, _oEnd));
const unsigned int maxObjectsPerLeaf = (size < divs) ? (size) : ((unsigned int)((float)(std::distance(_oBegin, _oEnd)) / (float)divs));
const ScalarType leafBoxMaxVolume = ((ScalarType)0);
const bool useVariance = true;
(void)(this->tree.Set(_oBegin, _oEnd, getPtr, getBox, getBarycenter, maxObjectsPerLeaf, leafBoxMaxVolume, useVariance));
}
template <class OBJITERATOR, class OBJITERATORPTRFUNCT, class OBJBOXFUNCT, class OBJBARYCENTERFUNCT>
inline bool Set(const OBJITERATOR & oBegin, const OBJITERATOR & oEnd, OBJITERATORPTRFUNCT & objPtr, OBJBOXFUNCT & objBox, OBJBARYCENTERFUNCT & objBarycenter, const unsigned int maxElemsPerLeaf = 1, const ScalarType & leafBoxMaxVolume = ((ScalarType)0), const bool useVariance = true) {
return (this->tree.Set(oBegin, oEnd, objPtr, objBox, objBarycenter, maxElemsPerLeaf, leafBoxMaxVolume, useVariance));
}
template <class OBJPOINTDISTFUNCTOR, class OBJMARKER>
inline ObjPtr GetClosest(
OBJPOINTDISTFUNCTOR & _getPointDistance, OBJMARKER & _marker, const CoordType & _p, const ScalarType & _maxDist,
ScalarType & _minDist, CoordType & _closestPt) {
(void)_marker;
return (AABBBinaryTreeClosest<TreeType>::Closest(this->tree, _getPointDistance, _p, _maxDist, _minDist, _closestPt));
}
template <class OBJPOINTDISTFUNCTOR, class OBJMARKER, class OBJPTRCONTAINER, class DISTCONTAINER, class POINTCONTAINER>
inline unsigned int GetKClosest(
OBJPOINTDISTFUNCTOR & _getPointDistance, OBJMARKER & _marker, const unsigned int _k, const CoordType & _p, const ScalarType & _maxDist,
OBJPTRCONTAINER & _objectPtrs, DISTCONTAINER & _distances, POINTCONTAINER & _points) {
(void)_marker;
return (AABBBinaryTreeKClosest<TreeType>::KClosest(this->tree, _getPointDistance, _k, _p, _maxDist, _objectPtrs, _distances, _points));
}
template <class OBJRAYISECTFUNCTOR, class OBJMARKER>
inline ObjPtr DoRay(OBJRAYISECTFUNCTOR & _rayIntersector, OBJMARKER & _marker, const Ray3<ScalarType> & _ray, const ScalarType & _maxDist, ScalarType & _t) {
(void)_marker;
return (AABBBinaryTreeRay<TreeType>::Ray(this->tree, _rayIntersector, _ray, _maxDist, _t));
}
protected:
TreeType tree;
};
} // end namespace vcg
#endif // #ifndef __VCGLIB_AABBBINARYTREEINDEX_H