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 const size_t tmp = (imax.u-imin.u+1)*(imax.v-imin.v+1)*(imax.w-imin.w+1);
121 if (tmp > map_.size()) {
122 return this->has_within_all_buckets(pos, dist2);
124 for(
int wc=imin.w;wc<=imax.w;++wc) {
125 for(
int vc=imin.v;vc<=imax.v;++vc) {
126 for(
int uc=imin.u;uc<=imax.u;++uc) {
127 typename ItemMap::const_iterator map_it = map_.find(Index(uc,vc,wc));
128 if(map_it!=map_.end()) {
129 for(
typename EntryList::const_iterator entry_it = map_it->second.begin();
130 entry_it != map_it->second.end(); ++entry_it) {
137 Real delta_x = entry_it->pos[0]-pos[0];
138 Real delta_y = entry_it->pos[1]-pos[1];
139 Real delta_z = entry_it->pos[2]-pos[2];
140 if(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z<=dist2) {
152 Real dist2=dist*dist;
153 Index imin = gen_index(pos-VEC(dist,dist,dist));
154 Index imax = gen_index(pos+VEC(dist,dist,dist));
158 for(
int wc=imin.w;wc<=imax.w;++wc) {
159 for(
int vc=imin.v;vc<=imax.v;++vc) {
160 for(
int uc=imin.u;uc<=imax.u;++uc) {
161 typename ItemMap::const_iterator map_it = map_.find(Index(uc,vc,wc));
163 if(map_it!=map_.end()) {
164 for(
typename EntryList::const_iterator entry_it = map_it->second.begin();
165 entry_it != map_it->second.end(); ++entry_it) {
172 Real delta_x = entry_it->pos[0]-pos[0];
173 Real delta_y = entry_it->pos[1]-pos[1];
174 Real delta_z = entry_it->pos[2]-pos[2];
175 if(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z<=dist2) {
176 item_list.push_back(entry_it->item);
191 std::swap(delta_,o.delta_);
192 std::swap(min_, o.min_);
193 std::swap(max_, o.max_);
197 bool has_within_all_buckets(
const VEC& pos,
Real dist2)
const {
198 for (
typename ItemMap::const_iterator
199 i = map_.begin(), e = map_.end(); i!=e; ++i) {
200 for(
typename EntryList::const_iterator j = i->second.begin();
201 j != i->second.end(); ++j) {
202 Real delta_x = j->pos[0]-pos[0];
203 Real delta_y = j->pos[1]-pos[1];
204 Real delta_z = j->pos[2]-pos[2];
205 if(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z<=dist2) {
217 Index gen_index(
const VEC& pos)
const {
218 Index nrvo(static_cast<int>(round(pos[0]/delta_)),
219 static_cast<int>(round(pos[1]/delta_)),
220 static_cast<int>(round(pos[2]/delta_)));
225 geom::Vec3 nrvo((static_cast<Real>(i.u)+0.5)*delta_,
226 (static_cast<Real>(i.v)+0.5)*delta_,
227 (static_cast<Real>(i.w)+0.5)*delta_);
std::vector< ITEM > ItemList
bool operator<(const ResidueHandle &lhs, const ResidueHandle &rhs)
void Add(const ITEM &item, const VEC &pos)
ItemList FindWithin(const VEC &pos, Real dist) const
void Swap(SpatialOrganizer &o)
Vec2 Max(const Vec2 &v1, const Vec2 &v2)
Three dimensional vector class, using Real precision.
SpatialOrganizer(Real delta)
#define DLLEXPORT_OST_MOL
bool HasWithin(const VEC &pos, Real dist) const
Vec2 Min(const Vec2 &v1, const Vec2 &v2)
void Remove(const ITEM &item)