2012-06-22 16:27:33 +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-06-22 16:27:33 +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. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-08 23:32:50 +02:00
|
|
|
#ifndef __VCGLIB_GRID_CLOSEST_2D
|
|
|
|
#define __VCGLIB_GRID_CLOSEST_2D
|
2012-06-22 16:27:33 +02:00
|
|
|
|
2014-10-09 16:10:27 +02:00
|
|
|
#include <vcg/space/index/space_iterators2d.h>
|
2014-08-01 02:17:06 +02:00
|
|
|
#include <vcg/space/intersection2.h>
|
2012-06-22 16:27:33 +02:00
|
|
|
|
|
|
|
namespace vcg{
|
|
|
|
|
|
|
|
|
2014-08-01 02:17:06 +02:00
|
|
|
template <class SPATIALINDEXING,class OBJMARKER, class OBJPTRCONTAINER>
|
|
|
|
unsigned int GridGetInBox2D(SPATIALINDEXING &_Si,
|
|
|
|
OBJMARKER & _marker,
|
|
|
|
const vcg::Box2<typename SPATIALINDEXING::ScalarType> &_bbox,
|
|
|
|
OBJPTRCONTAINER & _objectPtrs,
|
|
|
|
bool update_global_mark=true)
|
|
|
|
{
|
|
|
|
typename SPATIALINDEXING::CellIterator first,last,l;
|
|
|
|
//_objectPtrs.clear();
|
|
|
|
vcg::Box2i ibbox;
|
|
|
|
Box2i Si_ibox(Point2i(0,0),_Si.siz-Point2i(1,1));
|
|
|
|
_Si.BoxToIBox(_bbox, ibbox);
|
|
|
|
ibbox.Intersect(Si_ibox);
|
|
|
|
|
|
|
|
if (update_global_mark)
|
|
|
|
_marker.UnMarkAll();
|
2012-08-27 14:47:12 +02:00
|
|
|
|
2014-08-01 02:17:06 +02:00
|
|
|
if (ibbox.IsNull())
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int ix,iy;
|
|
|
|
for (ix=ibbox.min[0]; ix<=ibbox.max[0]; ix++)
|
|
|
|
for (iy=ibbox.min[1]; iy<=ibbox.max[1]; iy++)
|
2012-08-27 14:47:12 +02:00
|
|
|
{
|
2014-08-01 02:17:06 +02:00
|
|
|
_Si.Grid( ix, iy, first, last );
|
|
|
|
for(l=first;l!=last;++l)
|
|
|
|
if (!(**l).IsD())
|
|
|
|
{
|
|
|
|
typename SPATIALINDEXING::ObjPtr elem=&(**l);
|
|
|
|
vcg::Box2<typename SPATIALINDEXING::ScalarType> box_elem;
|
2014-10-13 10:36:01 +02:00
|
|
|
elem->GetBBox(box_elem);
|
2014-08-01 02:17:06 +02:00
|
|
|
if (update_global_mark)
|
|
|
|
{
|
|
|
|
if(( ! _marker.IsMarked(elem))&&(box_elem.Collide(_bbox)))
|
|
|
|
{
|
|
|
|
_objectPtrs.push_back(elem);
|
|
|
|
_marker.Mark(elem);
|
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
if(box_elem.Collide(_bbox))
|
|
|
|
_objectPtrs.push_back(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (static_cast<unsigned int>(_objectPtrs.size()));
|
|
|
|
}
|
|
|
|
}
|
2012-08-27 14:47:12 +02:00
|
|
|
|
2014-08-01 02:17:06 +02:00
|
|
|
template <class SPATIAL_INDEX, class OBJPOINTDISTFUNCTOR, class OBJMARKER>
|
|
|
|
typename SPATIAL_INDEX::ObjPtr GridClosest2D(SPATIAL_INDEX &Si,
|
|
|
|
OBJPOINTDISTFUNCTOR _getPointDistance,
|
|
|
|
OBJMARKER & _marker,
|
|
|
|
const typename OBJPOINTDISTFUNCTOR::QueryType & _p_obj,
|
|
|
|
const typename SPATIAL_INDEX::ScalarType & _maxDist,
|
|
|
|
typename SPATIAL_INDEX::ScalarType & _minDist,
|
|
|
|
typename SPATIAL_INDEX:: CoordType &_closestPt)
|
|
|
|
{
|
|
|
|
typedef typename SPATIAL_INDEX::ObjPtr ObjPtr;
|
|
|
|
typedef SPATIAL_INDEX SpatialIndex;
|
|
|
|
typedef typename SPATIAL_INDEX::CoordType CoordType;
|
|
|
|
typedef typename SPATIAL_INDEX::ScalarType ScalarType;
|
|
|
|
typedef typename SPATIAL_INDEX::Box2x Box2x;
|
2012-08-27 14:47:12 +02:00
|
|
|
|
2014-08-01 02:17:06 +02:00
|
|
|
Point2<ScalarType> _p = OBJPOINTDISTFUNCTOR::Pos(_p_obj);
|
|
|
|
|
|
|
|
// Initialize min_dist with _maxDist to exploit early rejection test.
|
|
|
|
_minDist = _maxDist;
|
2012-08-27 14:47:12 +02:00
|
|
|
|
2014-08-01 02:17:06 +02:00
|
|
|
ObjPtr winner=NULL;
|
|
|
|
_marker.UnMarkAll();
|
|
|
|
ScalarType newradius = Si.voxel.Norm();
|
|
|
|
ScalarType radius;
|
|
|
|
Box2i iboxdone,iboxtodo;
|
|
|
|
CoordType t_res;
|
|
|
|
typename SPATIAL_INDEX::CellIterator first,last,l;
|
|
|
|
if(Si.bbox.IsInEx(_p))
|
|
|
|
{
|
|
|
|
Point2i _ip;
|
|
|
|
Si.PToIP(_p,_ip);
|
|
|
|
Si.Grid( _ip[0],_ip[1], first, last );
|
|
|
|
for(l=first;l!=last;++l)
|
|
|
|
{
|
|
|
|
ObjPtr elem=&(**l);
|
|
|
|
if (!elem->IsD())
|
2012-08-27 14:47:12 +02:00
|
|
|
{
|
2014-08-01 02:17:06 +02:00
|
|
|
if (_getPointDistance((**l), _p_obj,_minDist, t_res))
|
2012-08-27 14:47:12 +02:00
|
|
|
{
|
2014-08-01 02:17:06 +02:00
|
|
|
winner=elem;
|
|
|
|
_closestPt=t_res;
|
|
|
|
newradius=_minDist; //
|
2012-08-27 14:47:12 +02:00
|
|
|
}
|
2014-08-01 02:17:06 +02:00
|
|
|
_marker.Mark(elem);
|
2012-08-27 14:47:12 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-01 02:17:06 +02:00
|
|
|
iboxdone=Box2i(_ip,_ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ix,iy;
|
|
|
|
Box2i ibox(Point2i(0,0),Si.siz-Point2i(1,1));
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
radius=newradius;
|
|
|
|
Box2x boxtodo=Box2x(_p,radius);
|
|
|
|
//boxtodo.Intersect(Si.bbox);
|
|
|
|
Si.BoxToIBox(boxtodo, iboxtodo);
|
|
|
|
iboxtodo.Intersect(ibox);
|
|
|
|
if(!boxtodo.IsNull())
|
|
|
|
{
|
|
|
|
for (ix=iboxtodo.min[0]; ix<=iboxtodo.max[0]; ix++)
|
|
|
|
for (iy=iboxtodo.min[1]; iy<=iboxtodo.max[1]; iy++)
|
|
|
|
if(ix<iboxdone.min[0] || ix>iboxdone.max[0] || // this test is to avoid to re-process already analyzed cells.
|
|
|
|
iy<iboxdone.min[1] || iy>iboxdone.max[1] )
|
|
|
|
{
|
|
|
|
Si.Grid( ix, iy, first, last );
|
|
|
|
for(l=first;l!=last;++l) if (!(**l).IsD())
|
|
|
|
{
|
|
|
|
ObjPtr elem=&(**l);
|
|
|
|
if (!elem->IsD())
|
|
|
|
{
|
|
|
|
if( ! _marker.IsMarked(elem))
|
|
|
|
{
|
|
|
|
if (_getPointDistance((**l), _p_obj, _minDist, t_res))
|
|
|
|
{
|
|
|
|
winner=elem;
|
|
|
|
_closestPt=t_res;
|
|
|
|
};
|
|
|
|
_marker.Mark(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!winner) newradius=radius+Si.voxel.Norm();
|
|
|
|
else newradius = _minDist;
|
|
|
|
iboxdone=iboxtodo;
|
|
|
|
}
|
|
|
|
while (_minDist>radius);
|
|
|
|
|
|
|
|
return winner;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class SPATIALINDEXING,class OBJRAYISECTFUNCTOR, class OBJMARKER>
|
2014-08-08 23:32:50 +02:00
|
|
|
typename SPATIALINDEXING::ObjPtr GridDoRay2D(SPATIALINDEXING &_Si,
|
2014-08-01 02:17:06 +02:00
|
|
|
OBJRAYISECTFUNCTOR &_rayIntersector,
|
|
|
|
OBJMARKER &_marker,
|
|
|
|
const Ray2<typename SPATIALINDEXING::ScalarType> & _ray,
|
|
|
|
const typename SPATIALINDEXING::ScalarType & _maxDist,
|
|
|
|
typename SPATIALINDEXING::ScalarType & _t)
|
|
|
|
{
|
|
|
|
typedef vcg::RayIterator2D<SPATIALINDEXING,OBJRAYISECTFUNCTOR,OBJMARKER> RayIteratorType;
|
|
|
|
RayIteratorType RayIte=RayIteratorType(_Si,_rayIntersector,_maxDist,_marker);
|
|
|
|
RayIte.Init(_ray);
|
|
|
|
|
|
|
|
if (!RayIte.End())
|
|
|
|
{
|
|
|
|
_t=RayIte.Dist();
|
|
|
|
return(&(*RayIte));
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-27 14:47:12 +02:00
|
|
|
|
2014-08-01 02:17:06 +02:00
|
|
|
}//end namespace vcg
|
2012-06-22 16:27:33 +02:00
|
|
|
#endif
|
|
|
|
|