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);
55 static Index
Max(
const Index& a,
const Index& b) {
57 m.u = a.u > b.u ? a.u : b.u;
58 m.v = a.v > b.v ? a.v : b.v;
59 m.w = a.w > b.w ? a.w : b.w;
63 static Index
Min(
const Index& a,
const Index& b) {
65 m.u = a.u < b.u ? a.u : b.u;
66 m.v = a.v < b.v ? a.v : b.v;
67 m.w = a.w < b.w ? a.w : b.w;
74 Entry(
const ITEM& i,
const VEC& p): item(i), pos(p) {}
80 typedef std::vector<Entry> EntryList;
82 typedef std::map<Index,EntryList> ItemMap;
88 throw "delta cannot be zero";
92 void Add(
const ITEM& item,
const VEC& pos) {
93 bool first = map_.empty();
94 Index indx=gen_index(pos);
95 map_[indx].push_back(Entry(item,pos));
105 typename ItemMap::iterator i=map_.begin();
106 for (; i!=map_.end(); ++i) {
107 for (
size_t j=0; j<i->second.size(); ++j) {
108 if (i->second[j].item==item) {
109 i->second.erase(i->second.begin()+j);
117 Real dist2=dist*dist;
118 Index imin =
Index::Max(min_, gen_index(pos-VEC(dist,dist,dist)));
119 Index imax =
Index::Min(max_, gen_index(pos+VEC(dist,dist,dist)));
120 if ((imax.u-imin.u+1)*(imax.v-imin.v+1)*(imax.w-imin.w+1)>map_.size()) {
121 return this->has_within_all_buckets(pos, dist2);
123 for(
int wc=imin.w;wc<=imax.w;++wc) {
124 for(
int vc=imin.v;vc<=imax.v;++vc) {
125 for(
int uc=imin.u;uc<=imax.u;++uc) {
126 typename ItemMap::const_iterator map_it = map_.find(Index(uc,vc,wc));
127 if(map_it!=map_.end()) {
128 for(
typename EntryList::const_iterator entry_it = map_it->second.begin();
129 entry_it != map_it->second.end(); ++entry_it) {
136 Real delta_x = entry_it->pos[0]-pos[0];
137 Real delta_y = entry_it->pos[1]-pos[1];
138 Real delta_z = entry_it->pos[2]-pos[2];
139 if(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z<=dist2) {
151 Real dist2=dist*dist;
152 Index imin = gen_index(pos-VEC(dist,dist,dist));
153 Index imax = gen_index(pos+VEC(dist,dist,dist));
157 for(
int wc=imin.w;wc<=imax.w;++wc) {
158 for(
int vc=imin.v;vc<=imax.v;++vc) {
159 for(
int uc=imin.u;uc<=imax.u;++uc) {
160 typename ItemMap::const_iterator map_it = map_.find(Index(uc,vc,wc));
162 if(map_it!=map_.end()) {
163 for(
typename EntryList::const_iterator entry_it = map_it->second.begin();
164 entry_it != map_it->second.end(); ++entry_it) {
171 Real delta_x = entry_it->pos[0]-pos[0];
172 Real delta_y = entry_it->pos[1]-pos[1];
173 Real delta_z = entry_it->pos[2]-pos[2];
174 if(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z<=dist2) {
175 item_list.push_back(entry_it->item);
190 std::swap(delta_,o.delta_);
191 std::swap(min_, o.min_);
192 std::swap(max_, o.max_);
196 bool has_within_all_buckets(
const VEC& pos,
Real dist2)
const {
197 for (
typename ItemMap::const_iterator
198 i = map_.begin(), e = map_.end(); i!=e; ++i) {
199 for(
typename EntryList::const_iterator j = i->second.begin();
200 j != i->second.end(); ++j) {
201 Real delta_x = j->pos[0]-pos[0];
202 Real delta_y = j->pos[1]-pos[1];
203 Real delta_z = j->pos[2]-pos[2];
204 if(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z<=dist2) {
216 Index gen_index(
const VEC& pos)
const {
217 Index nrvo(static_cast<int>(
round(pos[0]/delta_)),
218 static_cast<int>(
round(pos[1]/delta_)),
219 static_cast<int>(
round(pos[2]/delta_)));
224 geom::Vec3 nrvo((static_cast<Real>(i.u)+0.5)*delta_,
225 (static_cast<Real>(i.v)+0.5)*delta_,
226 (static_cast<Real>(i.w)+0.5)*delta_);