2006-06-01 22:53:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
History
|
|
|
|
|
|
|
|
$Log: not supported by cvs2svn $
|
2007-03-26 10:12:30 +02:00
|
|
|
Revision 1.25 2007/03/08 17:05:50 pietroni
|
|
|
|
line 375, corrected 1 error concerning intersection with bounding of the grid
|
|
|
|
|
2007-03-08 18:05:50 +01:00
|
|
|
Revision 1.24 2007/02/20 16:22:50 ganovelli
|
|
|
|
modif in ClosestIterator to include the last shell Si.siz [X|Y|X]. Tested with minialign and point based animation
|
|
|
|
|
2007-02-20 17:22:50 +01:00
|
|
|
Revision 1.23 2006/12/06 12:53:14 pietroni
|
|
|
|
changed 1 wrong comment RayIterator---- Refresh .. was the opposite
|
|
|
|
|
2006-12-06 13:53:14 +01:00
|
|
|
Revision 1.22 2006/10/26 08:28:50 pietroni
|
|
|
|
corrected 1 bug in operator ++ of closest iterator
|
|
|
|
|
2006-10-26 10:28:50 +02:00
|
|
|
Revision 1.21 2006/10/25 15:59:29 pietroni
|
|
|
|
corrected bug in closest iterator.. if doesn't find any alement at first cells examinated continue until find some element
|
|
|
|
|
2006-10-25 17:59:29 +02:00
|
|
|
Revision 1.20 2006/10/25 09:47:53 pietroni
|
|
|
|
added max dist control and constructor
|
|
|
|
|
2006-10-25 11:47:53 +02:00
|
|
|
Revision 1.19 2006/10/02 07:47:57 cignoni
|
|
|
|
Reverted to version 1.14 to nullify dangerous marfr960's changes
|
|
|
|
|
2006-08-23 16:53:50 +02:00
|
|
|
Revision 1.14 2006/06/01 20:53:56 cignoni
|
|
|
|
added missing header
|
|
|
|
|
2006-06-01 22:53:56 +02:00
|
|
|
****************************************************************************/
|
2005-09-30 15:13:47 +02:00
|
|
|
#ifndef __VCGLIB_SPATIAL_ITERATORS
|
|
|
|
#define __VCGLIB_SPATIAL_ITERATORS
|
|
|
|
|
2005-09-19 15:33:58 +02:00
|
|
|
#include <vector>
|
2006-01-23 12:41:03 +01:00
|
|
|
#include <vcg/space/intersection3.h>
|
2005-09-30 15:13:47 +02:00
|
|
|
#include <vcg/space/point3.h>
|
2005-09-19 15:33:58 +02:00
|
|
|
#include <vcg/space/box3.h>
|
2005-09-30 15:13:47 +02:00
|
|
|
#include <vcg/space/ray3.h>
|
|
|
|
#include <vcg/math/base.h>
|
2005-09-19 15:33:58 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <float.h>
|
2005-12-14 18:03:13 +01:00
|
|
|
#include <limits>
|
2005-09-19 15:33:58 +02:00
|
|
|
|
|
|
|
namespace vcg{
|
2008-10-31 11:40:06 +01:00
|
|
|
template <class Spatial_Idexing,class INTFUNCTOR,class TMARKER>
|
2005-10-03 12:06:53 +02:00
|
|
|
class RayIterator
|
2006-02-08 18:04:10 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename Spatial_Idexing::ScalarType ScalarType;
|
|
|
|
typedef typename vcg::Ray3<ScalarType> RayType;
|
|
|
|
typedef typename Spatial_Idexing::Box3x IndexingBoxType;
|
2005-10-03 12:06:53 +02:00
|
|
|
protected:
|
|
|
|
typedef typename Spatial_Idexing::ObjType ObjType;
|
|
|
|
typedef typename vcg::Point3<ScalarType> CoordType;
|
|
|
|
typedef typename Spatial_Idexing::CellIterator CellIterator;
|
2006-10-25 11:47:53 +02:00
|
|
|
ScalarType max_dist;
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
///control right bonding current cell index (only on initialization)
|
|
|
|
void _ControlLimits()
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
for (int i=0;i<3;i++)
|
|
|
|
{
|
|
|
|
vcg::Point3i dim=Si.siz;
|
|
|
|
if (CurrentCell.V(i)<0)
|
|
|
|
CurrentCell.V(i) = 0;
|
2008-10-31 11:40:06 +01:00
|
|
|
else
|
2005-10-03 12:06:53 +02:00
|
|
|
if (CurrentCell.V(i)>=dim.V(i))
|
|
|
|
CurrentCell.V(i)=dim.V(i)-1;
|
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
}
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
///find initial line parameters
|
|
|
|
void _FindLinePar()
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
/* Punti goal */
|
|
|
|
|
|
|
|
///da verificare se vanno oltre ai limiti
|
|
|
|
vcg::Point3i ip;
|
|
|
|
Si.PToIP(start,ip);
|
2008-10-31 11:40:06 +01:00
|
|
|
Si.IPiToPf(ip,goal);
|
2005-10-03 12:06:53 +02:00
|
|
|
for (int i=0;i<3;i++)
|
2008-10-31 11:40:06 +01:00
|
|
|
if(r.Direction().V(i)>0.0)
|
2005-10-03 12:06:53 +02:00
|
|
|
goal.V(i)+=Si.voxel.V(i);
|
|
|
|
|
|
|
|
ScalarType gx=goal.X();
|
|
|
|
ScalarType gy=goal.Y();
|
|
|
|
ScalarType gz=goal.Z();
|
|
|
|
|
2008-10-31 11:40:06 +01:00
|
|
|
dist=(r.Origin()-goal).Norm();
|
2005-10-03 12:06:53 +02:00
|
|
|
|
2007-02-20 17:22:50 +01:00
|
|
|
const float LocalMaxScalar = (std::numeric_limits<float>::max)();
|
2010-02-11 21:07:27 +01:00
|
|
|
const float EPS = std::numeric_limits<float>::min();
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
/* Parametri della linea */
|
|
|
|
ScalarType tx,ty,tz;
|
|
|
|
|
2009-07-15 18:29:10 +02:00
|
|
|
if( fabs(r.Direction().X())>EPS )
|
2005-10-03 12:06:53 +02:00
|
|
|
tx = (gx-r.Origin().X())/r.Direction().X();
|
2008-10-31 11:40:06 +01:00
|
|
|
else
|
2005-12-02 01:20:32 +01:00
|
|
|
tx =LocalMaxScalar;
|
2005-10-03 12:06:53 +02:00
|
|
|
|
2009-07-15 18:29:10 +02:00
|
|
|
if( fabs(r.Direction().Y())>EPS)
|
2005-10-03 12:06:53 +02:00
|
|
|
ty = (gy-r.Origin().Y())/r.Direction().Y();
|
2008-10-31 11:40:06 +01:00
|
|
|
else
|
2005-12-02 01:20:32 +01:00
|
|
|
ty =LocalMaxScalar;
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2009-07-15 18:29:10 +02:00
|
|
|
if( fabs(r.Direction().Z())>EPS )
|
2005-10-03 12:06:53 +02:00
|
|
|
tz = (gz-r.Origin().Z())/r.Direction().Z();
|
2008-10-31 11:40:06 +01:00
|
|
|
else
|
2005-12-02 01:20:32 +01:00
|
|
|
tz =LocalMaxScalar;
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
t=CoordType(tx,ty,tz);
|
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
bool _controlEnd()
|
|
|
|
{
|
|
|
|
return (((CurrentCell.X()<0)||(CurrentCell.Y()<0)||(CurrentCell.Z()<0))||
|
|
|
|
((CurrentCell.X()>=Si.siz.X())||(CurrentCell.Y()>=Si.siz.Y())||(CurrentCell.Z()>=Si.siz.Z())));
|
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
void _NextCell()
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
assert(!end);
|
2007-03-26 10:12:30 +02:00
|
|
|
vcg::Box3<ScalarType> bb_current;
|
|
|
|
|
2008-10-31 11:40:06 +01:00
|
|
|
Si.IPiToPf(CurrentCell,bb_current.min);
|
|
|
|
Si.IPiToPf(CurrentCell+vcg::Point3i(1,1,1),bb_current.max);
|
2007-03-26 10:12:30 +02:00
|
|
|
|
|
|
|
CoordType inters;
|
2010-04-21 19:22:39 +02:00
|
|
|
IntersectionRayBox(bb_current,r,inters);
|
2007-03-26 10:12:30 +02:00
|
|
|
ScalarType testmax_dist=(inters-r.Origin()).Norm();
|
2006-10-25 11:47:53 +02:00
|
|
|
|
|
|
|
if (testmax_dist>max_dist)
|
|
|
|
end=true;
|
|
|
|
else
|
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
if( t.X()<t.Y() && t.X()<t.Z() )
|
|
|
|
{
|
2008-10-31 11:40:06 +01:00
|
|
|
if(r.Direction().X()<0.0)
|
2005-10-03 12:06:53 +02:00
|
|
|
{goal.X() -= Si.voxel.X(); --CurrentCell.X();}
|
2008-10-31 11:40:06 +01:00
|
|
|
else
|
2005-10-03 12:06:53 +02:00
|
|
|
{goal.X() += Si.voxel.X(); ++CurrentCell.X();}
|
|
|
|
t.X() = (goal.X()-r.Origin().X())/r.Direction().X();
|
2008-10-31 11:40:06 +01:00
|
|
|
}
|
2005-10-03 12:06:53 +02:00
|
|
|
else if( t.Y()<t.Z() ){
|
2008-10-31 11:40:06 +01:00
|
|
|
if(r.Direction().Y()<0.0)
|
2005-10-03 12:06:53 +02:00
|
|
|
{goal.Y() -= Si.voxel.Y(); --CurrentCell.Y();}
|
2008-10-31 11:40:06 +01:00
|
|
|
else
|
2005-10-03 12:06:53 +02:00
|
|
|
{goal.Y() += Si.voxel.Y(); ++CurrentCell.Y();}
|
|
|
|
t.Y() = (goal.Y()-r.Origin().Y())/r.Direction().Y();
|
|
|
|
} else {
|
2008-10-31 11:40:06 +01:00
|
|
|
if(r.Direction().Z()<0.0)
|
2005-10-03 12:06:53 +02:00
|
|
|
{ goal.Z() -= Si.voxel.Z(); --CurrentCell.Z();}
|
|
|
|
else
|
|
|
|
{ goal.Z() += Si.voxel.Z(); ++CurrentCell.Z();}
|
|
|
|
t.Z() = (goal.Z()-r.Origin().Z())/r.Direction().Z();
|
|
|
|
}
|
|
|
|
|
|
|
|
dist=(r.Origin()-goal).Norm();
|
|
|
|
end=_controlEnd();
|
2005-09-19 15:33:58 +02:00
|
|
|
}
|
2006-10-25 11:47:53 +02:00
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
public:
|
2005-09-19 15:33:58 +02:00
|
|
|
|
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
///contructor
|
2006-10-25 11:47:53 +02:00
|
|
|
RayIterator(Spatial_Idexing &_Si,
|
|
|
|
INTFUNCTOR _int_funct
|
|
|
|
,const ScalarType &_max_dist)
|
|
|
|
:Si(_Si),int_funct(_int_funct)
|
|
|
|
{
|
|
|
|
max_dist=_max_dist;
|
2005-10-03 12:06:53 +02:00
|
|
|
};
|
2008-10-31 11:40:06 +01:00
|
|
|
|
2008-11-03 17:21:52 +01:00
|
|
|
void SetMarker(TMARKER _tm)
|
2005-10-03 12:06:53 +02:00
|
|
|
{
|
|
|
|
tm=_tm;
|
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
void Init(const RayType _r)
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
r=_r;
|
|
|
|
end=false;
|
|
|
|
tm.UnMarkAll();
|
|
|
|
Elems.clear();
|
|
|
|
//CoordType ip;
|
|
|
|
//control if intersect the bounding box of the mesh
|
2006-06-01 10:41:26 +02:00
|
|
|
if (Si.bbox.IsIn(r.Origin()))
|
|
|
|
start=r.Origin();
|
|
|
|
else
|
2010-04-20 02:59:38 +02:00
|
|
|
if (!(vcg::IntersectionRayBox<ScalarType>(Si.bbox,r,start))){
|
2006-06-01 10:41:26 +02:00
|
|
|
end=true;
|
|
|
|
return;
|
|
|
|
}
|
2005-10-03 12:06:53 +02:00
|
|
|
Si.PToIP(start,CurrentCell);
|
|
|
|
_ControlLimits();
|
|
|
|
_FindLinePar();
|
|
|
|
//go to first intersection
|
|
|
|
while ((!End())&& Refresh())
|
|
|
|
_NextCell();
|
2008-10-31 11:40:06 +01:00
|
|
|
|
2005-09-19 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
bool End()
|
|
|
|
{return end;}
|
|
|
|
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2008-10-31 11:40:06 +01:00
|
|
|
///refresh current cell intersection , return false if there are
|
2005-10-03 12:06:53 +02:00
|
|
|
///at lest 1 intersection
|
|
|
|
bool Refresh()
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
//Elems.clear();
|
|
|
|
|
2005-12-02 01:20:32 +01:00
|
|
|
typename Spatial_Idexing::CellIterator first,last,l;
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
///take first, last iterators to elements in the cell
|
|
|
|
Si.Grid(CurrentCell.X(),CurrentCell.Y(),CurrentCell.Z(),first,last);
|
|
|
|
for(l=first;l!=last;++l)
|
|
|
|
{
|
|
|
|
ObjType* elem=&(*(*l));
|
|
|
|
ScalarType t;
|
|
|
|
CoordType Int;
|
2007-03-26 10:12:30 +02:00
|
|
|
if((!elem->IsD())&&(!tm.IsMarked(elem))&&(int_funct((**l),r,t))&&(t<=max_dist))
|
2005-10-03 12:06:53 +02:00
|
|
|
{
|
|
|
|
Int=r.Origin()+r.Direction()*t;
|
|
|
|
Elems.push_back(Entry_Type(elem,t,Int));
|
|
|
|
tm.Mark(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
////then control if there are more than 1 element
|
2008-07-29 18:05:29 +02:00
|
|
|
std::sort(Elems.begin(),Elems.end());
|
|
|
|
CurrentElem=Elems.rbegin();
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
return((Elems.size()==0)||(Dist()>dist));
|
2005-09-19 15:33:58 +02:00
|
|
|
}
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
void operator ++()
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2008-07-29 18:05:29 +02:00
|
|
|
if (!Elems.empty()) Elems.pop_back();
|
2008-10-31 11:40:06 +01:00
|
|
|
|
2008-07-29 18:05:29 +02:00
|
|
|
CurrentElem = Elems.rbegin();
|
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
if (Dist()>dist)
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
if (!End())
|
|
|
|
{
|
2005-09-19 15:33:58 +02:00
|
|
|
_NextCell();
|
2005-10-03 12:06:53 +02:00
|
|
|
while ((!End())&&Refresh())
|
|
|
|
_NextCell();
|
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
ObjType &operator *(){return *((*CurrentElem).elem);}
|
|
|
|
|
|
|
|
CoordType IntPoint()
|
|
|
|
{return ((*CurrentElem).intersection);}
|
|
|
|
|
|
|
|
ScalarType Dist()
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
if (Elems.size()>0)
|
|
|
|
return ((*CurrentElem).dist);
|
2008-10-31 11:40:06 +01:00
|
|
|
else
|
2005-10-03 12:06:53 +02:00
|
|
|
return ((ScalarType)FLT_MAX);
|
2005-09-19 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
///set the current spatial indexing structure used
|
|
|
|
void SetIndexStructure(Spatial_Idexing &_Si)
|
|
|
|
{Si=_Si;}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
protected:
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2008-10-31 11:40:06 +01:00
|
|
|
///structure that mantain for the current cell pre-calculated data
|
2008-10-24 14:45:02 +02:00
|
|
|
struct Entry_Type
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
Entry_Type(ObjType* _elem,ScalarType _dist,CoordType _intersection)
|
|
|
|
{
|
|
|
|
elem=_elem;
|
|
|
|
dist=_dist;
|
|
|
|
intersection=_intersection;
|
|
|
|
}
|
2008-10-31 11:40:06 +01:00
|
|
|
inline bool operator < ( const Entry_Type & l ) const{return (dist > l.dist); }
|
2005-10-03 12:06:53 +02:00
|
|
|
ObjType* elem;
|
|
|
|
ScalarType dist;
|
|
|
|
CoordType intersection;
|
|
|
|
};
|
|
|
|
|
2008-07-29 18:05:29 +02:00
|
|
|
RayType r; //ray to find intersections
|
2005-10-03 12:06:53 +02:00
|
|
|
Spatial_Idexing &Si; //reference to spatial index algorithm
|
2008-07-29 18:05:29 +02:00
|
|
|
bool end; //true if the scan is terminated
|
2005-10-03 12:06:53 +02:00
|
|
|
INTFUNCTOR &int_funct;
|
2008-11-03 17:21:52 +01:00
|
|
|
TMARKER tm;
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
std::vector<Entry_Type> Elems; //element loaded from curren cell
|
2008-07-29 18:05:29 +02:00
|
|
|
typedef typename std::vector<Entry_Type>::reverse_iterator ElemIterator;
|
2005-10-03 12:06:53 +02:00
|
|
|
ElemIterator CurrentElem; //iterator to current element
|
|
|
|
|
|
|
|
vcg::Point3i CurrentCell; //current cell
|
|
|
|
|
|
|
|
//used for raterization
|
|
|
|
CoordType start;
|
2008-10-31 11:40:06 +01:00
|
|
|
CoordType goal;
|
2005-10-03 12:06:53 +02:00
|
|
|
ScalarType dist;
|
|
|
|
CoordType t;
|
|
|
|
|
|
|
|
};
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2008-10-31 11:40:06 +01:00
|
|
|
|
|
|
|
template <class Spatial_Idexing,class DISTFUNCTOR,class TMARKER>
|
2005-10-03 12:06:53 +02:00
|
|
|
class ClosestIterator
|
2008-10-31 11:40:06 +01:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
typedef typename Spatial_Idexing::ObjType ObjType;
|
|
|
|
typedef typename Spatial_Idexing::ScalarType ScalarType;
|
|
|
|
typedef typename vcg::Point3<ScalarType> CoordType;
|
|
|
|
typedef typename Spatial_Idexing::CellIterator CellIterator;
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
///control the end of scanning
|
|
|
|
bool _EndGrid()
|
|
|
|
{
|
2007-02-20 17:22:50 +01:00
|
|
|
if ((explored.min==vcg::Point3i(0,0,0))&&(explored.max==Si.siz))
|
2005-09-19 15:33:58 +02:00
|
|
|
end =true;
|
2005-10-03 12:06:53 +02:00
|
|
|
return end;
|
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
void _UpdateRadius()
|
|
|
|
{
|
2005-09-21 11:21:20 +02:00
|
|
|
if (radius>=max_dist)
|
|
|
|
end=true;
|
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
radius+=step_size;
|
2005-09-19 15:33:58 +02:00
|
|
|
//control bounds
|
|
|
|
if (radius>max_dist)
|
|
|
|
radius=max_dist;
|
2005-10-03 12:06:53 +02:00
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
///add cell to the curren set of explored cells
|
|
|
|
bool _NextShell()
|
|
|
|
{
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
//then expand the box
|
|
|
|
explored=to_explore;
|
|
|
|
_UpdateRadius();
|
|
|
|
Box3<ScalarType> b3d(p,radius);
|
|
|
|
Si.BoxToIBox(b3d,to_explore);
|
2007-03-08 18:05:50 +01:00
|
|
|
Box3i ibox(Point3i(0,0,0),Si.siz-Point3i(1,1,1));
|
2005-10-03 12:06:53 +02:00
|
|
|
to_explore.Intersect(ibox);
|
|
|
|
if (!to_explore.IsNull())
|
|
|
|
{
|
2006-10-02 09:47:57 +02:00
|
|
|
assert(!( to_explore.min.X()<0 || to_explore.max.X()>=Si.siz[0] ||
|
2008-10-31 11:40:06 +01:00
|
|
|
to_explore.min.Y()<0 || to_explore.max.Y()>=Si.siz[1] || to_explore.min.Z()<0
|
2006-10-02 09:47:57 +02:00
|
|
|
|| to_explore.max.Z()>=Si.siz[2] ));
|
|
|
|
return true;
|
2005-10-03 12:06:53 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
public:
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
///contructor
|
|
|
|
ClosestIterator(Spatial_Idexing &_Si,DISTFUNCTOR _dist_funct):Si(_Si),dist_funct(_dist_funct){}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
///set the current spatial indexing structure used
|
|
|
|
void SetIndexStructure(Spatial_Idexing &_Si)
|
|
|
|
{Si=_Si;}
|
2005-09-30 15:13:47 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
void SetMarker(TMARKER _tm)
|
|
|
|
{
|
|
|
|
tm=_tm;
|
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2007-03-26 10:12:30 +02:00
|
|
|
///initialize the Iterator
|
2005-10-03 12:06:53 +02:00
|
|
|
void Init(CoordType _p,const ScalarType &_max_dist)
|
|
|
|
{
|
2006-10-02 09:47:57 +02:00
|
|
|
explored.SetNull();
|
2005-10-05 19:04:45 +02:00
|
|
|
to_explore.SetNull();
|
2006-10-02 09:47:57 +02:00
|
|
|
p=_p;
|
|
|
|
max_dist=_max_dist;
|
|
|
|
Elems.clear();
|
2005-10-03 12:06:53 +02:00
|
|
|
end=false;
|
|
|
|
tm.UnMarkAll();
|
2006-10-02 09:47:57 +02:00
|
|
|
//step_size=Si.voxel.X();
|
2005-10-03 12:06:53 +02:00
|
|
|
step_size=Si.voxel.Norm();
|
|
|
|
radius=0;
|
|
|
|
|
|
|
|
///inflate the bbox until find a valid bbox
|
2008-10-24 14:45:02 +02:00
|
|
|
while ((!_NextShell())&&(!End())) {}
|
2005-10-03 12:06:53 +02:00
|
|
|
|
2006-10-25 17:59:29 +02:00
|
|
|
while ((!End())&& Refresh()&&(!_EndGrid()))
|
|
|
|
_NextShell();
|
2005-10-03 12:06:53 +02:00
|
|
|
}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
//return true if the scan is complete
|
|
|
|
bool End()
|
|
|
|
{return end;}
|
2005-09-20 16:02:16 +02:00
|
|
|
|
2008-10-31 11:40:06 +01:00
|
|
|
///refresh Object found also considering current shere radius,
|
2005-10-03 12:06:53 +02:00
|
|
|
//and object comes from previos that are already in the stack
|
2006-10-25 17:59:29 +02:00
|
|
|
//return false if no elements find
|
|
|
|
bool Refresh()
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
int ix,iy,iz;
|
|
|
|
for( iz = to_explore.min.Z();iz <= to_explore.max.Z(); ++iz)
|
2005-10-05 19:04:45 +02:00
|
|
|
for(iy =to_explore.min.Y(); iy <=to_explore.max.Y(); ++iy)
|
|
|
|
for(ix =to_explore.min.X(); ix <= to_explore.max.X();++ix)
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-10-05 19:04:45 +02:00
|
|
|
// this test is to avoid to re-process already analyzed cells.
|
|
|
|
if((explored.IsNull())||
|
2008-10-31 11:40:06 +01:00
|
|
|
(ix<explored.min[0] || ix>explored.max[0] ||
|
2005-10-03 12:06:53 +02:00
|
|
|
iy<explored.min[1] || iy>explored.max[1] ||
|
2005-10-05 19:04:45 +02:00
|
|
|
iz<explored.min[2] || iz>explored.max[2] ))
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2005-12-02 01:20:32 +01:00
|
|
|
typename Spatial_Idexing::CellIterator first,last,l;
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
Si.Grid(ix,iy,iz,first,last);
|
2006-10-02 09:47:57 +02:00
|
|
|
for(l=first;l!=last;++l)
|
2005-09-20 16:02:16 +02:00
|
|
|
{
|
2005-10-03 12:06:53 +02:00
|
|
|
ObjType *elem=&(**l);
|
|
|
|
if (!tm.IsMarked(elem))
|
|
|
|
{
|
|
|
|
|
|
|
|
CoordType nearest;
|
|
|
|
ScalarType dist=max_dist;
|
2006-10-02 09:47:57 +02:00
|
|
|
if (dist_funct((**l),p,dist,nearest))
|
|
|
|
Elems.push_back(Entry_Type(elem,fabs(dist),nearest));
|
2005-10-03 12:06:53 +02:00
|
|
|
tm.Mark(elem);
|
|
|
|
}
|
2005-09-20 16:02:16 +02:00
|
|
|
}
|
2006-08-23 16:53:50 +02:00
|
|
|
}
|
2006-10-02 09:47:57 +02:00
|
|
|
|
2005-09-19 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2007-02-20 17:22:50 +01:00
|
|
|
////sort the elements in Elems and take a iterator to the last one
|
|
|
|
std::sort(Elems.begin(),Elems.end());
|
|
|
|
CurrentElem=Elems.rbegin();
|
2006-10-25 17:59:29 +02:00
|
|
|
|
2007-02-20 17:22:50 +01:00
|
|
|
return((Elems.size()==0)||(Dist()>radius));
|
2005-09-19 15:33:58 +02:00
|
|
|
}
|
2005-10-03 12:06:53 +02:00
|
|
|
|
2006-10-26 10:28:50 +02:00
|
|
|
bool ToUpdate()
|
|
|
|
{return ((Elems.size()==0)||(Dist()>radius));}
|
|
|
|
|
2006-10-25 17:59:29 +02:00
|
|
|
void operator ++()
|
|
|
|
{
|
2007-02-20 17:22:50 +01:00
|
|
|
if (!Elems.empty()) Elems.pop_back();
|
2008-10-31 11:40:06 +01:00
|
|
|
|
2007-02-20 17:22:50 +01:00
|
|
|
CurrentElem = Elems.rbegin();
|
|
|
|
|
2006-10-26 10:28:50 +02:00
|
|
|
if ((!End())&& ToUpdate())
|
2009-01-29 12:27:32 +01:00
|
|
|
do{_NextShell();}
|
|
|
|
while (Refresh()&&(!_EndGrid()));
|
2005-09-19 15:33:58 +02:00
|
|
|
}
|
2008-10-31 11:40:06 +01:00
|
|
|
|
2006-10-02 09:47:57 +02:00
|
|
|
ObjType &operator *(){return *((*CurrentElem).elem);}
|
2005-10-03 12:06:53 +02:00
|
|
|
|
2008-10-31 11:40:06 +01:00
|
|
|
//return distance of the element form the point if no element
|
2005-10-03 12:06:53 +02:00
|
|
|
//are in the vector then return max dinstance
|
|
|
|
ScalarType Dist()
|
2005-09-19 15:33:58 +02:00
|
|
|
{
|
2006-10-02 09:47:57 +02:00
|
|
|
if (Elems.size()>0)
|
|
|
|
return ((*CurrentElem).dist);
|
2008-10-31 11:40:06 +01:00
|
|
|
else
|
2005-10-03 12:06:53 +02:00
|
|
|
return ((ScalarType)FLT_MAX);
|
2005-09-19 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
CoordType NearestPoint()
|
2006-10-02 09:47:57 +02:00
|
|
|
{return ((*CurrentElem).intersection);}
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2005-10-03 12:06:53 +02:00
|
|
|
protected:
|
2005-09-19 15:33:58 +02:00
|
|
|
|
2008-10-31 11:40:06 +01:00
|
|
|
///structure that mantain for the current cell pre-calculated data
|
2008-10-24 14:45:02 +02:00
|
|
|
struct Entry_Type
|
2005-10-03 12:06:53 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Entry_Type(ObjType* _elem,ScalarType _dist,CoordType _intersection)
|
|
|
|
{
|
|
|
|
elem=_elem;
|
|
|
|
dist=_dist;
|
|
|
|
intersection=_intersection;
|
|
|
|
}
|
|
|
|
|
2008-10-31 11:40:06 +01:00
|
|
|
inline bool operator < ( const Entry_Type & l ) const{return (dist > l.dist); }
|
2005-10-03 12:06:53 +02:00
|
|
|
|
2008-10-31 11:40:06 +01:00
|
|
|
inline bool operator == ( const Entry_Type & l ) const{return (elem == l.elem); }
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
ObjType* elem;
|
|
|
|
ScalarType dist;
|
|
|
|
CoordType intersection;
|
|
|
|
};
|
|
|
|
|
2008-07-29 18:05:29 +02:00
|
|
|
CoordType p; //initial point
|
2006-10-02 09:47:57 +02:00
|
|
|
Spatial_Idexing &Si; //reference to spatial index algorithm
|
2008-07-29 18:05:29 +02:00
|
|
|
bool end; //true if the scan is terminated
|
2006-10-02 09:47:57 +02:00
|
|
|
ScalarType max_dist; //max distance when the scan terminate
|
|
|
|
vcg::Box3i explored; //current bounding box explored
|
2008-07-29 18:05:29 +02:00
|
|
|
vcg::Box3i to_explore; //current bounding box explored
|
2006-10-02 09:47:57 +02:00
|
|
|
ScalarType radius; //curret radius for sphere expansion
|
|
|
|
ScalarType step_size; //radius step
|
|
|
|
std::vector<Entry_Type> Elems; //element loaded from the current sphere
|
2005-10-03 12:06:53 +02:00
|
|
|
|
|
|
|
DISTFUNCTOR &dist_funct;
|
|
|
|
TMARKER tm;
|
2006-10-02 09:47:57 +02:00
|
|
|
|
2007-02-20 17:22:50 +01:00
|
|
|
typedef typename std::vector<Entry_Type>::reverse_iterator ElemIterator;
|
2006-10-02 09:47:57 +02:00
|
|
|
ElemIterator CurrentElem; //iterator to current element
|
|
|
|
|
2005-09-19 15:33:58 +02:00
|
|
|
};
|
|
|
|
}
|
2005-09-30 15:13:47 +02:00
|
|
|
|
2005-12-02 01:20:32 +01:00
|
|
|
#endif
|