OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
spatial_organizer.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_SPATIAL_ORGANIZER_HI
20 #define OST_SPATIAL_ORGANIZER_HI
21 
22 #include <vector>
23 #include <map>
24 #include <cmath>
25 #include <limits>
26 
27 #include <ost/geom/geom.hh>
28 
29 namespace ost { namespace mol {
30 
32 /*
33  organizes ITEMs defined by positions
34  in a spatial hash map, into bins of
35  a defined size (as given in the ctor)
36 */
37 template <class ITEM, class VEC=geom::Vec3>
39 public:
40  typedef std::vector<ITEM> ItemList;
41 
42 private:
43  struct Index {
44  Index():
45  u(0),v(0),w(0) {}
46  Index(int uu, int vv, int ww):
47  u(uu),v(vv),w(ww) {}
48 
49  bool operator<(const Index& other) const {
50  return w!=other.w ? w<other.w : (v!=other.v ? v<other.v : u<other.u);
51  }
52 
53  int u,v,w;
54 
55  static Index Max(const Index& a, const Index& b) {
56  Index m;
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;
60  return m;
61  }
62 
63  static Index Min(const Index& a, const Index& b) {
64  Index m;
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;
68  return m;
69  }
70 
71  };
72 
73  struct Entry {
74  Entry(const ITEM& i, const VEC& p): item(i), pos(p) {}
75 
76  ITEM item;
77  VEC pos;
78  };
79 
80  typedef std::vector<Entry> EntryList;
81 
82  typedef std::map<Index,EntryList> ItemMap;
83 
84 public:
85 
86  SpatialOrganizer(Real delta): delta_(delta) {
87  if(delta==0.0) {
88  throw "delta cannot be zero";
89  }
90  }
91 
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));
96  if (!first) {
97  min_ = Index::Min(min_, indx);
98  max_ = Index::Max(max_, indx);
99  } else {
100  min_ = indx;
101  max_ = indx;
102  }
103  }
104  void Remove(const ITEM& item) {
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);
110  return;
111  }
112  }
113  }
114  }
115 
116  bool HasWithin(const VEC& pos, Real dist) const {
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);
122  }
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) {
130  /*
131  speed tests indicate that pre-calculating dx2 or dy2
132  and pre-checking them with an additional if gives little
133  speed improvement for very specific circumstances only,
134  but most of the time the performance is worse.
135  */
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) {
140  return true;
141  }
142  }
143  }
144  }
145  }
146  }
147  return false;
148  }
149 
150  ItemList FindWithin(const VEC& pos, Real dist) const {
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));
154 
155  ItemList item_list;
156 
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));
161 
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) {
165  /*
166  speed tests indicate that pre-calculating dx2 or dy2
167  and pre-checking them with an additional if gives little
168  speed improvement for very specific circumstances only,
169  but most of the time the performance is worse.
170  */
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);
176  }
177  }
178  }
179  }
180  }
181  }
182  return item_list;
183  }
184  void Clear()
185  {
186  map_.clear();
187  }
189  map_.swap(o.map_);
190  std::swap(delta_,o.delta_);
191  std::swap(min_, o.min_);
192  std::swap(max_, o.max_);
193  }
194 
195 private:
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) {
205  return true;
206  }
207  }
208  }
209  return false;
210  }
211  ItemMap map_;
212  Real delta_;
213  Index min_;
214  Index max_;
215 
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_)));
220  return nrvo;
221  }
222 
223  geom::Vec3 gen_middle(const Index& i) const {
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_);
227  return nrvo;
228  }
229 
230 };
231 
232 }} // ns
233 
234 #endif