This commit is contained in:
Nico Pietroni 2014-08-01 00:18:43 +00:00
parent 46cdf95e0d
commit 3dfce75a37
3 changed files with 489 additions and 0 deletions

View File

@ -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 <math.h>
#include <vcg/space/point2.h>
#include <vcg/space/segment2.h>
#include <vcg/space/box2.h>
#include <vcg/simplex/face/distance.h>
namespace vcg {
namespace tri {
//**MARKER CLASSES**//
template <class MESH_TYPE,class OBJ_TYPE>
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 <class MESH, class GRID>
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<ScalarType> Point3x;
typedef FaceTmark<MESH> MarkerFace;
MarkerFace mf;
mf.SetMesh(&mesh);
vcg::PointSegment2DEPFunctor<ScalarType> PDistFunct;
_minDist=_maxDist;
return (gr.GetClosest(PDistFunct,mf,_p,_maxDist,_minDist,_closestPt));
}
} // end namespace tri
} // end namespace vcg
#endif

View File

@ -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 <vector>
#include <algorithm>
#include <stdio.h>
#include <vcg/space/box2.h>
#include <vcg/space/line2.h>
#include <vcg/space/index/index2D/grid_util_2D.h>
#include <vcg/space/index/index2D/grid_closest_2D.h>
namespace vcg {
template < class OBJTYPE, class FLT=float >
class GridStaticPtr2D: public BasicGrid2D<FLT>, SpatialIndex2D<OBJTYPE,FLT>
{
public:
typedef OBJTYPE ObjType;
typedef ObjType* ObjPtr;
typedef typename ObjType::ScalarType ScalarType;
typedef Point2<ScalarType> CoordType;
typedef Box2<ScalarType> Box2x;
typedef GridStaticPtr2D<OBJTYPE,FLT> MyGridType;
typedef typename std::vector<OBJTYPE*>::iterator CellIterator;
std::vector<std::vector<std::vector<OBJTYPE*> > > data;
private:
void Add( const int x,
const int y,
ObjType *elem)
{
assert((x>=0)&&(x<data.size()));
assert((y>=0)&&(y<data[x].size()));
data[x][y].push_back(elem);
}
template <class OBJITER>
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;x<data.size();x++)
data[x].resize(this->siz.Y());
OBJITER IteObj;
for (IteObj=_oBegin;IteObj!=_oEnd;IteObj++)
{
Box2<ScalarType> 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 <class OBJITER>
inline void Set(const OBJITER & _oBegin,
const OBJITER & _oEnd,
const Box2x &_bbox)
{
int _size=(int)std::distance<OBJITER>(_oBegin,_oEnd);
Point2<FLT> _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 <class OBJITER>
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<OBJITER>(_oBegin,_oEnd,bbox);
}
template <class OBJPOINTDISTFUNCTOR, class OBJMARKER>
ObjPtr GetClosest(OBJPOINTDISTFUNCTOR & _getPointDistance,
OBJMARKER & _marker,
const typename OBJPOINTDISTFUNCTOR::QueryType & _p,
const ScalarType & _maxDist,
ScalarType & _minDist,
CoordType & _closestPt)
{
return (vcg::GridClosest2D<MyGridType,OBJPOINTDISTFUNCTOR,OBJMARKER>(*this,_getPointDistance,_marker, _p,_maxDist,_minDist,_closestPt));
}
template <class OBJMARKER, class OBJPTRCONTAINER>
unsigned int GetInBox(OBJMARKER & _marker,
const vcg::Box2<ScalarType> _bbox,
OBJPTRCONTAINER & _objectPtrs)
{
return(vcg::GridGetInBox2D<MyGridType,OBJMARKER,OBJPTRCONTAINER>
(*this,_marker,_bbox,_objectPtrs));
}
template <class OBJRAYISECTFUNCTOR, class OBJMARKER>
ObjPtr DoRay(OBJRAYISECTFUNCTOR & _rayIntersector, OBJMARKER & _marker,
const Ray2<ScalarType> & _ray, const ScalarType & _maxDist,
ScalarType & _t)
{
return(vcg::GridDoRay<MyGridType,OBJRAYISECTFUNCTOR,OBJMARKER>(*this,_rayIntersector,_marker,_ray,_maxDist,_t));
}
}; //end class GridStaticPtr_2D
} // end namespace
#endif

View File

@ -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 <vector>
#include <vcg/space/intersection2.h>
#include <vcg/space/point2.h>
#include <vcg/space/box2.h>
#include <vcg/space/ray2.h>
#include <vcg/math/base.h>
#include <algorithm>
#include <float.h>
#include <limits>
namespace vcg{
template <class Spatial_Idexing,class INTFUNCTOR,class TMARKER>
class RayIterator2D
{
public:
typedef typename Spatial_Idexing::ScalarType ScalarType;
typedef typename vcg::Ray2<ScalarType> RayType;
typedef typename Spatial_Idexing::Box2x IndexingBoxType;
protected:
typedef typename Spatial_Idexing::ObjType ObjType;
typedef typename vcg::Point2<ScalarType> 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<ObjType*> objectPtrs;
GridGetInBox2D(Si,tm,currBox,objectPtrs,false);
//printf(" size %d \n",objectPtrs.size());
for(int i=0;i<objectPtrs.size();i++)
{
ObjType* elem=objectPtrs[i];
if (elem->IsD())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()<dist);
}
public:
//contructor
RayIterator2D(Spatial_Idexing &_Si,
INTFUNCTOR _int_funct,
const ScalarType &_max_dist,
TMARKER _tm)
:Si(_Si),int_funct(_int_funct),tm(_tm)
{
max_dist=_max_dist;
};
void Init(const RayType _r)
{
r=_r;
r.Normalize();
//initialization
end=false;
tm.UnMarkAll();
Elems.clear();
CoordType start;
//control if intersect the bounding box of the grid
if (Si.bbox.IsIn(r.Origin()))
start=r.Origin();
else
if (!(vcg::RayBoxIntersection<ScalarType>(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<Entry_Type> Elems; //element loaded from curren cell
typedef typename std::vector<Entry_Type>::reverse_iterator ElemIterator;
ElemIterator CurrentElem; //iterator to current element
vcg::Box2<ScalarType> currBox;
CoordType step;
ScalarType stepsize;
ScalarType dist;
};
}
#endif