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);
116 void Remove(
const ITEM& item,
const VEC& pos) {
120 Index indx=gen_index(pos);
121 typename ItemMap::iterator i = map_.find(indx);
122 if(i != map_.end()) {
123 for (
size_t j=0; j<i->second.size(); ++j) {
124 if (i->second[j].item==item) {
125 i->second.erase(i->second.begin()+j);
134 Real dist2=dist*dist;
135 Index imin =
Index::Max(min_, gen_index(pos-VEC(dist,dist,dist)));
136 Index imax =
Index::Min(max_, gen_index(pos+VEC(dist,dist,dist)));
137 const size_t tmp = (imax.u-imin.u+1)*(imax.v-imin.v+1)*(imax.w-imin.w+1);
138 if (tmp > map_.size()) {
139 return this->has_within_all_buckets(pos, dist2);
141 for(
int wc=imin.w;wc<=imax.w;++wc) {
142 for(
int vc=imin.v;vc<=imax.v;++vc) {
143 for(
int uc=imin.u;uc<=imax.u;++uc) {
144 typename ItemMap::const_iterator map_it = map_.find(Index(uc,vc,wc));
145 if(map_it!=map_.end()) {
146 for(
typename EntryList::const_iterator entry_it = map_it->second.begin();
147 entry_it != map_it->second.end(); ++entry_it) {
154 Real delta_x = entry_it->pos[0]-pos[0];
155 Real delta_y = entry_it->pos[1]-pos[1];
156 Real delta_z = entry_it->pos[2]-pos[2];
157 if(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z<=dist2) {
169 Real dist2=dist*dist;
170 Index imin = gen_index(pos-VEC(dist,dist,dist));
171 Index imax = gen_index(pos+VEC(dist,dist,dist));
175 for(
int wc=imin.w;wc<=imax.w;++wc) {
176 for(
int vc=imin.v;vc<=imax.v;++vc) {
177 for(
int uc=imin.u;uc<=imax.u;++uc) {
178 typename ItemMap::const_iterator map_it = map_.find(Index(uc,vc,wc));
180 if(map_it!=map_.end()) {
181 for(
typename EntryList::const_iterator entry_it = map_it->second.begin();
182 entry_it != map_it->second.end(); ++entry_it) {
189 Real delta_x = entry_it->pos[0]-pos[0];
190 Real delta_y = entry_it->pos[1]-pos[1];
191 Real delta_z = entry_it->pos[2]-pos[2];
192 if(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z<=dist2) {
193 item_list.push_back(entry_it->item);
208 std::swap(delta_,o.delta_);
209 std::swap(min_, o.min_);
210 std::swap(max_, o.max_);
214 bool has_within_all_buckets(
const VEC& pos,
Real dist2)
const {
215 for (
typename ItemMap::const_iterator
216 i = map_.begin(), e = map_.end(); i!=e; ++i) {
217 for(
typename EntryList::const_iterator j = i->second.begin();
218 j != i->second.end(); ++j) {
219 Real delta_x = j->pos[0]-pos[0];
220 Real delta_y = j->pos[1]-pos[1];
221 Real delta_z = j->pos[2]-pos[2];
222 if(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z<=dist2) {
234 Index gen_index(
const VEC& pos)
const {
235 Index nrvo(
static_cast<int>(round(pos[0]/delta_)),
236 static_cast<int>(round(pos[1]/delta_)),
237 static_cast<int>(round(pos[2]/delta_)));
243 (
static_cast<Real>(i.v)+0.5)*delta_,
244 (
static_cast<Real>(i.w)+0.5)*delta_);
Three dimensional vector class, using Real precision.
ItemList FindWithin(const VEC &pos, Real dist) const
void Remove(const ITEM &item, const VEC &pos)
std::vector< ITEM > ItemList
SpatialOrganizer(Real delta)
void Add(const ITEM &item, const VEC &pos)
void Remove(const ITEM &item)
void Swap(SpatialOrganizer &o)
bool HasWithin(const VEC &pos, Real dist) const
#define DLLEXPORT_OST_MOL
Vec2 Min(const Vec2 &v1, const Vec2 &v2)
Vec2 Max(const Vec2 &v1, const Vec2 &v2)
bool operator<(const ResidueHandle &lhs, const ResidueHandle &rhs)