1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004-2016 \/)\/ *
* 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));
}
void _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(size_t 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
|