From 21d89670631178e6684bccee857066271459d811 Mon Sep 17 00:00:00 2001 From: dibenedetto Date: Wed, 28 Sep 2005 20:14:53 +0000 Subject: [PATCH] First Commit. --- .../aabb_binary_tree/aabb_binary_tree.cpp | 125 +++++++++++++++++ .../aabb_binary_tree/aabb_binary_tree.pro | 11 ++ .../index/aabb_binary_tree/aabb_binary_tree.h | 127 ++++++++++++++++++ 3 files changed, 263 insertions(+) create mode 100644 apps/sample/aabb_binary_tree/aabb_binary_tree.cpp create mode 100644 apps/sample/aabb_binary_tree/aabb_binary_tree.pro create mode 100644 vcg/space/index/aabb_binary_tree/aabb_binary_tree.h diff --git a/apps/sample/aabb_binary_tree/aabb_binary_tree.cpp b/apps/sample/aabb_binary_tree/aabb_binary_tree.cpp new file mode 100644 index 00000000..0da26e1a --- /dev/null +++ b/apps/sample/aabb_binary_tree/aabb_binary_tree.cpp @@ -0,0 +1,125 @@ +// standard headers +#include + +// stl headers +#include + +// vcg headers +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +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, std::vector > { }; + +typedef vcg::AABBBinaryTreeIndex AIndex; + +static AMesh gMesh; +static AIndex gIndex; + +static void CreateMesh(void) { + vcg::tri::Dodecahedron(gMesh); + + vcg::tri::UpdateFlags::Clear(gMesh); + vcg::tri::UpdateNormals::PerVertexNormalized(gMesh); + vcg::tri::UpdateEdges::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::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::max(); + + std::vector closestObjects; + std::vector closestDistances; + std::vector 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 rayIntersector; + const AIndex::ScalarType maxDist = std::numeric_limits::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 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); +} diff --git a/apps/sample/aabb_binary_tree/aabb_binary_tree.pro b/apps/sample/aabb_binary_tree/aabb_binary_tree.pro new file mode 100644 index 00000000..9c82efb8 --- /dev/null +++ b/apps/sample/aabb_binary_tree/aabb_binary_tree.pro @@ -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 diff --git a/vcg/space/index/aabb_binary_tree/aabb_binary_tree.h b/vcg/space/index/aabb_binary_tree/aabb_binary_tree.h new file mode 100644 index 00000000..746a0f6a --- /dev/null +++ b/vcg/space/index/aabb_binary_tree/aabb_binary_tree.h @@ -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 +#include +#include +#include +#include +#include + +/***************************************************************************/ + +namespace vcg { + +template +class AABBBinaryTreeIndex : public SpatialIndex { +public: + typedef AABBBinaryTreeIndex ClassType; + typedef OBJTYPE ObjType; + typedef SCALARTYPE ScalarType; + typedef NODEAUXDATA NodeAuxData; + typedef ObjType * ObjPtr; + typedef Point3 CoordType; + typedef AABBBinaryTree TreeType; + + inline TreeType & Tree(void) { + return (this->tree); + } + + inline const TreeType & Tree(void) const { + return (this->tree); + } + + template + inline void Set(const OBJITER & _oBegin, const OBJITER & _oEnd) { + class GetBoxFunctor { + public: + void operator () (const ObjType & obj, Box3 & 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 + 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 + inline ObjPtr GetClosest( + OBJPOINTDISTFUNCTOR & _getPointDistance, OBJMARKER & _marker, const CoordType & _p, const ScalarType & _maxDist, + ScalarType & _minDist, CoordType & _closestPt) { + (void)_marker; + return (AABBBinaryTreeClosest::Closest(this->tree, _getPointDistance, _p, _maxDist, _minDist, _closestPt)); + } + + template + 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::KClosest(this->tree, _getPointDistance, _k, _p, _maxDist, _objectPtrs, _distances, _points)); + } + + template + inline ObjPtr DoRay(OBJRAYISECTFUNCTOR & _rayIntersector, OBJMARKER & _marker, const Ray3 & _ray, const ScalarType & _maxDist, ScalarType & _t) { + (void)_marker; + return (AABBBinaryTreeRay::Ray(this->tree, _rayIntersector, _ray, _maxDist, _t)); + } + +protected: + TreeType tree; + +}; + +} // end namespace vcg + +#endif // #ifndef __VCGLIB_AABBBINARYTREEINDEX_H