diff --git a/vcg/space/index/index2D/closest_2D.h b/vcg/space/index/index2D/closest_2D.h new file mode 100644 index 00000000..1209e033 --- /dev/null +++ b/vcg/space/index/index2D/closest_2D.h @@ -0,0 +1,72 @@ +/**************************************************************************** +* 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. * +* * +****************************************************************************/ + + +#ifndef __VCG_TRIMESH_CLOSEST_2D +#define __VCG_TRIMESH_CLOSEST_2D + +#include +#include +#include +#include +#include + +namespace vcg { + namespace tri { + + //**MARKER CLASSES**// + template + class Tmark + { + MESH_TYPE *m; + public: + Tmark(){} + Tmark( MESH_TYPE *m) {SetMesh(m);} + void UnMarkAll(){ vcg::tri::UnMarkAll(*m);} + bool IsMarked(OBJ_TYPE* obj){return (vcg::tri::IsMarked(*m,obj));} + void Mark(OBJ_TYPE* obj){ vcg::tri::Mark(*m,obj);} + void SetMesh(MESH_TYPE *_m) + { + m=_m; + } + }; + + template + typename MESH::FaceType * GetClosestEdgeBase( MESH & mesh,GRID & gr,const typename GRID::CoordType & _p, + const typename GRID::ScalarType _maxDist,typename GRID::ScalarType & _minDist, + typename GRID::CoordType &_closestPt) + { + typedef typename GRID::ScalarType ScalarType; + typedef Point3 Point3x; + typedef FaceTmark MarkerFace; + MarkerFace mf; + mf.SetMesh(&mesh); + vcg::PointSegment2DEPFunctor PDistFunct; + _minDist=_maxDist; + return (gr.GetClosest(PDistFunct,mf,_p,_maxDist,_minDist,_closestPt)); + } + + } // end namespace tri +} // end namespace vcg + +#endif diff --git a/vcg/space/index/index2D/grid_static_ptr_2D.h b/vcg/space/index/index2D/grid_static_ptr_2D.h new file mode 100644 index 00000000..12d2bfdd --- /dev/null +++ b/vcg/space/index/index2D/grid_static_ptr_2D.h @@ -0,0 +1,182 @@ +/**************************************************************************** +* 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. * +* * +****************************************************************************/ + +#ifndef __VCGLIB_UGRID_2D +#define __VCGLIB_UGRID_2D + +#include +#include +#include + +#include +#include +#include +#include + +namespace vcg { + +template < class OBJTYPE, class FLT=float > +class GridStaticPtr2D: public BasicGrid2D, SpatialIndex2D +{ +public: + typedef OBJTYPE ObjType; + typedef ObjType* ObjPtr; + typedef typename ObjType::ScalarType ScalarType; + typedef Point2 CoordType; + typedef Box2 Box2x; + typedef GridStaticPtr2D MyGridType; + typedef typename std::vector::iterator CellIterator; + std::vector > > data; + +private: + + + + void Add( const int x, + const int y, + ObjType *elem) + { + assert((x>=0)&&(x=0)&&(y + inline void Set(const OBJITER & _oBegin, + const OBJITER & _oEnd, + const Box2x &_bbox, + Point2i _siz) + { + this->bbox=_bbox; + this->siz=_siz; + + // find voxel size starting from the provided bbox and grid size. + + this->dim = this->bbox.max - this->bbox.min; + this->voxel.X() = (this->dim.X()/(ScalarType)this->siz.X()); + this->voxel.Y() = (this->dim.Y()/(ScalarType)this->siz.Y()); + + ///allocate space + data.resize(this->siz.X()); + for (int x=0;xsiz.Y()); + + + OBJITER IteObj; + for (IteObj=_oBegin;IteObj!=_oEnd;IteObj++) + { + Box2 Box2D=(*IteObj).BBox(); + + //get index of intersected cells + Point2i minIndex=this->GridP(Box2D.min); + Point2i maxIndex=this->GridP(Box2D.max); + + for (int x=minIndex.X();x<=maxIndex.X();x++) + for (int y=minIndex.Y();y<=maxIndex.Y();y++) + Add(x,y,&(*IteObj)); + } + } + + template + inline void Set(const OBJITER & _oBegin, + const OBJITER & _oEnd, + const Box2x &_bbox) + { + int _size=(int)std::distance(_oBegin,_oEnd); + + Point2 _dim = _bbox.max - _bbox.min; + + Point2i _siz; + + BestDim2D( _size, _dim, _siz ); + + Set(_oBegin,_oEnd,_bbox,_siz); + } + +public: + + void Grid( const int x, const int y, + CellIterator & first, + CellIterator & last ) + { + first = data[x][y].begin(); + last = data[x][y].end(); + } + + void Grid( const Point2i pi, + CellIterator & first, + CellIterator & last ) + { + return Grid(pi.X(),pi.Y(),first,last); + } + + template + inline void Set(const OBJITER & _oBegin, + const OBJITER & _oEnd) + { + Box2x bbox; + + OBJITER IteObj; + for (IteObj=_oBegin;IteObj!=_oEnd;IteObj++) + bbox.Add((*IteObj).BBox()); + + ScalarType diag=bbox.Diag(); + bbox.Offset(diag*0.01); + Set(_oBegin,_oEnd,bbox); + } + + template + ObjPtr GetClosest(OBJPOINTDISTFUNCTOR & _getPointDistance, + OBJMARKER & _marker, + const typename OBJPOINTDISTFUNCTOR::QueryType & _p, + const ScalarType & _maxDist, + ScalarType & _minDist, + CoordType & _closestPt) + { + return (vcg::GridClosest2D(*this,_getPointDistance,_marker, _p,_maxDist,_minDist,_closestPt)); + } + + template + unsigned int GetInBox(OBJMARKER & _marker, + const vcg::Box2 _bbox, + OBJPTRCONTAINER & _objectPtrs) + { + return(vcg::GridGetInBox2D + (*this,_marker,_bbox,_objectPtrs)); + } + + + template + ObjPtr DoRay(OBJRAYISECTFUNCTOR & _rayIntersector, OBJMARKER & _marker, + const Ray2 & _ray, const ScalarType & _maxDist, + ScalarType & _t) + { + return(vcg::GridDoRay(*this,_rayIntersector,_marker,_ray,_maxDist,_t)); + } + + +}; //end class GridStaticPtr_2D + +} // end namespace + +#endif diff --git a/vcg/space/index/index2D/space_iterators_2D.h b/vcg/space/index/index2D/space_iterators_2D.h new file mode 100644 index 00000000..536dd96f --- /dev/null +++ b/vcg/space/index/index2D/space_iterators_2D.h @@ -0,0 +1,235 @@ +/**************************************************************************** +* VCGLib o o * +* Visual and Computer Graphics Library o o * +* _ O _ * +* Copyright(C) 2006 \/)\/ * +* 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. * +* * +****************************************************************************/ + +#ifndef __VCGLIB_SPATIAL_ITERATORS_2D +#define __VCGLIB_SPATIAL_ITERATORS_2D + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +namespace vcg{ +template +class RayIterator2D +{ +public: + typedef typename Spatial_Idexing::ScalarType ScalarType; + typedef typename vcg::Ray2 RayType; + typedef typename Spatial_Idexing::Box2x IndexingBoxType; +protected: + typedef typename Spatial_Idexing::ObjType ObjType; + typedef typename vcg::Point2 CoordType; + typedef typename Spatial_Idexing::CellIterator CellIterator; + ScalarType max_dist; + + + bool _controlEnd() + { + return (currBox.Collide(Si.bbox)); + } + + + bool _NextCell() + { + currBox.min+=step; + currBox.max+=step; + dist+=step.Norm(); + end=!_controlEnd(); + } + + //refresh current cell intersection , + // return true if there is at lest 1 intersection + bool Refresh() + { + std::vector objectPtrs; + GridGetInBox2D(Si,tm,currBox,objectPtrs,false); + //printf(" size %d \n",objectPtrs.size()); + for(int i=0;iIsD())continue; + if (tm.IsMarked(elem))continue; + //tm.Mark(elem); + + ScalarType t; + CoordType Int; + if((int_funct((*elem),r,t))&& + (t<=max_dist)) + { + Int=r.Origin()+r.Direction()*t; + Elems.push_back(Entry_Type(elem,t,Int)); + } + } + if (Elems.size()==0) return false; + //then control if there are more than 1 element + std::sort(Elems.begin(),Elems.end()); + CurrentElem=Elems.rbegin(); + + return(Dist()(r,Si.bbox,start))) + { + end=true; + return; + } + + + stepsize=Si.voxel.Norm()*2; + step=r.Direction()*stepsize; + + //create initial BB, inflate in case the direction is orthogonal to one axis + currBox.SetNull(); + currBox.Add(start); + currBox.Add(start+step); + ScalarType diag=currBox.Diag(); + currBox.Offset(diag*0.01); + dist=currBox.Diag(); + end=!_controlEnd(); + + while ((!End()) && (!Refresh())) + _NextCell(); + + fflush(stdout); + } + + bool End() + {return end;} + + + ObjType &operator *(){return *((*CurrentElem).elem);} + + CoordType IntPoint() + {return ((*CurrentElem).intersection);} + + ScalarType Dist() + { + if (Elems.size()>0) + return ((*CurrentElem).dist); + else + return ((ScalarType)FLT_MAX); + } + + void operator ++() + { + if (!Elems.empty()) Elems.pop_back(); + + CurrentElem = Elems.rbegin(); + + if (Dist()>dist) + { + if (!End()) + { + _NextCell(); + while ((!End()) && (!Refresh())) + _NextCell(); + } + } + } + +protected: + + ///structure that mantain for the current cell pre-calculated data + struct Entry_Type + { + public: + + Entry_Type(ObjType* _elem,ScalarType _dist,CoordType _intersection) + { + elem=_elem; + dist=_dist; + intersection=_intersection; + } + + Entry_Type(const Entry_Type &e) + { + elem=e.elem; + dist=e.dist; + intersection=e.intersection; + } + + inline bool operator < ( const Entry_Type & l ) const{return (dist > l.dist); } + ObjType* elem; + ScalarType dist; + CoordType intersection; + }; + + RayType r; //ray to find intersections + Spatial_Idexing &Si; //reference to spatial index algorithm + bool end; //true if the scan is terminated + INTFUNCTOR &int_funct; + TMARKER &tm; + + std::vector Elems; //element loaded from curren cell + typedef typename std::vector::reverse_iterator ElemIterator; + ElemIterator CurrentElem; //iterator to current element + + vcg::Box2 currBox; + CoordType step; + ScalarType stepsize; + ScalarType dist; + +}; + +} + +#endif