19 #ifndef OST_SPATIAL_ORGANIZER_HI
20 #define OST_SPATIAL_ORGANIZER_HI
29 namespace ost {
namespace mol {
37 template <
class ITEM,
class VEC=geom::Vec3>
46 Index(
int uu,
int vv,
int ww):
49 bool operator<(
const Index& other)
const {
50 return w!=other.w ? w<other.w : (v!=other.v ? v<other.v : u<other.u);
57 Entry(
const ITEM& i,
const VEC& p): item(i), pos(p) {}
63 typedef std::vector<Entry> EntryList;
65 typedef std::map<Index,EntryList> ItemMap;
71 throw "delta cannot be zero";
75 void Add(
const ITEM& item,
const VEC& pos) {
76 Index indx=gen_index(pos);
77 map_[indx].push_back(Entry(item,pos));
81 typename ItemMap::iterator i=map_.begin();
82 for (; i!=map_.end(); ++i) {
83 for (
size_t j=0; j<i->second.size(); ++j) {
84 if (i->second[j].item==item) {
85 i->second.erase(i->second.begin()+j);
95 Real best_dist2=std::numeric_limits<Real>::max();
98 for(
typename ItemMap::const_iterator map_it = map_.begin();
99 map_it!=map_.end();++map_it) {
101 if(dist2<best_dist2 && !map_it->second.empty()) {
108 if(!found_i0)
return ITEM();
111 best_dist2=std::numeric_limits<Real>::max();
112 ITEM* best_item=NULL;
114 for(
int wc=i0.w-1;wc<=i0.w+1;++wc) {
115 for(
int vc=i0.v-1;wc<=i0.v+1;++vc) {
116 for(
int uc=i0.u-1;wc<=i0.u+1;++uc) {
117 typename ItemMap::const_iterator map_it = map_.find(Index(uc,vc,wc));
119 if(map_it!=map_.end()) {
120 for(
typename EntryList::iterator entry_it = map_it->second.begin();
121 entry_it != map_it->second.end(); ++entry_it) {
122 Real delta_x = entry_it->pos[0]-pos[0];
123 Real delta_y = entry_it->pos[1]-pos[1];
124 Real delta_z = entry_it->pos[2]-pos[2];
125 Real dist2=delta_x*delta_x+delta_y*delta_y+delta_z*delta_z;
126 if(dist2<best_dist2) {
143 Real dist2=dist*dist;
144 Index imin = gen_index(pos-VEC(dist,dist,dist));
145 Index imax = gen_index(pos+VEC(dist,dist,dist));
149 for(
int wc=imin.w;wc<=imax.w;++wc) {
150 for(
int vc=imin.v;vc<=imax.v;++vc) {
151 for(
int uc=imin.u;uc<=imax.u;++uc) {
152 typename ItemMap::const_iterator map_it = map_.find(Index(uc,vc,wc));
154 if(map_it!=map_.end()) {
155 for(
typename EntryList::const_iterator entry_it = map_it->second.begin();
156 entry_it != map_it->second.end(); ++entry_it) {
163 Real delta_x = entry_it->pos[0]-pos[0];
164 Real delta_y = entry_it->pos[1]-pos[1];
165 Real delta_z = entry_it->pos[2]-pos[2];
166 if(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z<=dist2) {
167 item_list.push_back(entry_it->item);
182 std::swap(delta_,o.delta_);
190 Index gen_index(
const VEC& pos)
const {
191 Index nrvo(static_cast<int>(
round(pos[0]/delta_)),
192 static_cast<int>(
round(pos[1]/delta_)),
193 static_cast<int>(
round(pos[2]/delta_)));
198 geom::Vec3 nrvo((static_cast<Real>(i.u)+0.5)*delta_,
199 (static_cast<Real>(i.v)+0.5)*delta_,
200 (static_cast<Real>(i.w)+0.5)*delta_);