Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Rocon/include/KNN_Grid.h
Go to the documentation of this file.
1 #ifndef SPACEGRID_INCLUDED
2 #define SPACEGRID_INCLUDED
3 
4 
5 #include <iostream>
6 #include <vector>
7 #include <algorithm>
8 #include <ext/hash_map>
9 #include <cstdlib>
10 #include <cmath>
11 #include <map>
12 #include <queue>
13 #include "NVec.h"
14 
15 //FIX -- use hash_map
16 //FIX -- hash_map already keeps <key,data> pairs. keeping data with key in them
17 //wastes space.
18 
19 //map<unsigned int, void *> mymap;
20 
21 using namespace std;
22 using namespace nvc;
23 
24 const int KNNDIM = 3;
25 
27 {
28 public:
29  typedef unsigned int index_t;
30  typedef float coord_t;
33 
35  Point3D bb_min, bb_max, cellsize;
36 
37  UniformGrid(index_t w, index_t h, index_t d) { size = Index(w,h,d); }
38 
39  void set_extent(const Point3D & m, const Point3D & M)
40  {
41  bb_min = m; bb_max = M;
42  cellsize = M - m;
43  for(int k=0; k<KNNDIM; k++)
44  {
45  cellsize[k] /= (float)(size[k]);
46  }
47  }
48 
49  void get_cellsize(Point3D & c){c = cellsize;};
50 
52  {
54  for(int k=0; k<KNNDIM; k++)
55  {
56  int x = (int)floor(((v[k] - bb_min[k]) / cellsize[k]));
57  if (x <0)
58  i[k] = 0;
59  else
60  i[k] = x;
61  if( i[k] >= size[k])
62  {
63  i[k] = size[k]-1;
64  }
65  }
66  return i;
67  }
68 };
69 
70 
71 template<class CellData>
72 class SparseGrid : public UniformGrid
73 {
74  protected:
75  typedef map<unsigned int, CellData *> Map;
76 
77  //
78  // ASSUME: Each component of the index must be AT MOST 10 bits (0..2047).
79  // We're assuming that this should be sufficient for most
80  // reasonable uses for which we'll be using this grid.
81  //
82  unsigned int pack_index(const UniformGrid::Index& i)
83  { return (i[0]<<20) | (i[1]<<10) | i[2]; }
84 
85 
86 
88 
89 public:
90  SparseGrid(int w, int h, int d) : UniformGrid(w,h,d) { }
92  {
93  // Delete all the entries that were allocated by locate()
94  for(typename Map::iterator i=cellmap.begin(); i!=cellmap.end(); ++i)
95  delete i->second;
96  }
97 
98  //
99  // Iteration interface just reflects definitions from Map
100  //
101  typedef typename Map::iterator iterator;
102  typedef typename Map::const_iterator const_iterator;
103 
104  iterator begin() { return cellmap.begin(); }
105  iterator end() { return cellmap.end(); }
106  CellData *iterator_data(iterator i) { return i->second; }
107 
108  int usage_count() const { return cellmap.size(); }
109 
110 
111  //
112  // Accessors become lookups in the internal Map
113  //
114 
116  {
117  iterator iter = cellmap.find(pack_index(i));
118  return iter==cellmap.end() ? NULL : iter->second;
119  }
120 
121  CellData *locate_packed_index_only(unsigned int i)
122  {
123  iterator iter = cellmap.find(i);
124  return iter==cellmap.end() ? NULL : iter->second;
125  }
126 
127  CellData *locate_only(const Point3D& v)
128  { return locate_index_only(compute_index(v)); }
129 
130  CellData *locate(const Point3D& v)
131  {
132  UniformGrid::Index i = compute_index(v);
133  CellData *d = locate_index(i);//locate_index_only(i);
134  if( !d )
135  {
136  cellmap[pack_index(i)] = d = new CellData;
137  }
138  return d;
139  }
140 
142  {
143  CellData *d = locate_index_only(i);
144  if( !d )
145  {
146  cellmap[pack_index(i)] = d = new CellData;
147  }
148  return d;
149  }
150 
151  CellData *locate_packed_index(const unsigned int i)
152  {
153  CellData *d = locate_packed_index_only(i);
154  if( !d )
155  {
156  cellmap[i] = d = new CellData;
157  }
158  return d;
159  }
160 };
161 
162 
163 template <typename T>
164 class KNN_Grid
165 {
166 
167  //-----------------------------------------------------
168  //Helper classes
169  class KNNnbr
170  {
171  public:
172  float distance;
173  T item;
174 
175  KNNnbr(){};
176  bool operator<(const KNNnbr & p)const
177  {
178  return (distance < p.distance);
179  }
180 
181  KNNnbr & operator = (const KNNnbr & p){
182  distance = p.distance; item = p.item;
183  return *this;
184  }
185 
186  };
187 
188  typedef vector< T > SpaceCell;
189  typedef unsigned int index_t;
191  //-------------------------------------------------------
192 
193 
194  //private data
197  int cell_dim[3];
198  float cellsz[3];
199 
201 
202 public:
203  KNN_Grid(Point3D m, Point3D M, int long_dim)
204  :bbmin(m),bbmax(M)
205  {
206  //Construct a grid with square elements
207  Point3D box_len = bbmax-bbmin;
208  int max_dim;
209  // Find max length dimension of the grid
210  if ( box_len[0] > box_len[1])
211  max_dim = 0;
212  else
213  max_dim = 1;
214  if (box_len[2]>box_len[max_dim])
215  max_dim = 2;
216 
217  cellsz[max_dim] = box_len[max_dim]/((float) long_dim);
218  for(int i=0; i<3;i++){
219  cellsz[i] = cellsz[max_dim];
220  cell_dim[i] = (int)ceil(box_len[0]/cellsz[max_dim]); // ** maybe box_len[i] **
221  bbmax[i] = bbmin[i] + ((float) cell_dim[i]) * cellsz[i];
222  }
223  grid = new SparseGrid< SpaceCell >(cell_dim[0],cell_dim[1],cell_dim[2]);
224  grid->set_extent(bbmin,bbmax);
225  //cout << "Grid Extent \n";
226 
227  }
228 
229  KNN_Grid(Point3D m, Point3D M, int x_dim, int y_dim, int z_dim)
230  :bbmin(m),bbmax(M)
231  {
232  cell_dim[0]=x_dim;cell_dim[1]=y_dim;cell_dim[2]=z_dim;
233  grid = new SparseGrid< SpaceCell >(cell_dim[0],cell_dim[1],cell_dim[2]);
234  grid->set_extent(bbmin,bbmax);
235  }
236 
237  int insert(T & item, Point3D &p)
238  {
239  SpaceCell * cell = grid->locate(p);
240  cell->push_back(item);
241  //cout << "Size: "<< cell->size() << "\n" ;
242  return cell->size();
243  }
244 
245  int insertRange(T & item, Point3D &p, Point3D &q, Point3D &r){
246  int count = 0;
247 
248  Index min,max;
249 
250  Index a = grid->compute_index(p);
251  Index b = grid->compute_index(q);
252  Index c = grid->compute_index(r);
253 
254  for(int i=0;i<3;++i)
255  {
256  min[i] = (a[i]>b[i])?b[i]:a[i];
257  max[i] = (a[i]>b[i])?a[i]:b[i];
258  }
259 
260  for(int i=0;i<3;++i)
261  {
262  min[i] = (min[i]>c[i])?c[i]:min[i];
263  max[i] = (max[i]>c[i])?max[i]:c[i];
264  }
265 
266  for(unsigned int i=min[0]; i<=max[0]; ++i)
267  {
268  for(unsigned int j=min[1]; j<=max[1]; ++j)
269  {
270  for(unsigned int k=min[2]; k<=max[2]; ++k)
271  {
272  Index x(i,j,k);
273  SpaceCell * cell = grid->locate_index(x);
274  typename vector<T>::iterator temp ;
275  //temp = find(cell->begin(), cell->end(), item);
276  //if(temp == cell->end() || cell->size() == 0)
277  //{
278  cell->push_back(item);
279  count ++;//= cell->size();
280  //}
281  }
282  }
283  }
284 
285  return count;
286  }
287 
288  void clear(){
289  delete grid;
290  grid = new SparseGrid< SpaceCell >(cell_dim[0],cell_dim[1],cell_dim[2]);
291  grid->set_extent(bbmin,bbmax);
292  }
293 
294  unsigned int k_nearest(Point3D &p, int k, vector<T> & nbrs){
295  KNNnbr pnew;
296  priority_queue<KNNnbr> pts;
297  vector< T > * cell_pts = NULL;
298 
299  UniformGrid::Index grid_coord = grid->compute_index(p);
300  for(int x = grid_coord[0] - 2; x<grid_coord[0]+2;x++)
301  for(int y = grid_coord[1] - 2; y<grid_coord[1]+2;y++)
302  for(int z = grid_coord[2] - 2; z<grid_coord[2]+2;z++)
303  {
304  //get cell points
306  //cerr << "Checking cell: [" << x << " , " << y << " , "
307  // << z << "]" << endl;
308  cell_pts = get_cell(i);
309  if (cell_pts != NULL)
310  {
311  for(int j=0;j<cell_pts->size();j++)
312  {
313  pnew.distance = euclid_distance((*cell_pts)[j].coord,p);
314  pnew.item = (*cell_pts)[j];
315  pts.push(pnew);
316  // insert candidates into queue
317  }
318  }
319  }
320  int j=0;
321  int ptnum = pts.size();
322  nbrs.clear();
323  while( (j<k) && (!pts.empty()))
324  {
325  for(int j=0; j<k;j++)
326  {
327  KNNnbr n = pts.top();
328  nbrs.push_back(n.item);
329  pts.pop();
330  }
331  }
332  return ptnum;
333  }
334 
335 vector<T> * get_cell_range(Point3D &p, Point3D &q){
336  vector<T> * ret = new vector<T>;
337  vector<T> * temp = NULL;
338 
339  Index a = grid->compute_index(p);
340  Index b = grid->compute_index(q);
341  // cout << "A: "<< a[0]<< ", "<< a[1]<< ", "<< a[2]<< "\n";
342  // cout << "B: "<< b[0]<< ", "<< b[1]<< ", "<< b[2]<< "\n";
343  Index min,max;
344 
345  for(int i=0;i<3;++i)
346  {
347  min[i] = (a[i]>b[i])?b[i]:a[i];
348  max[i] = (a[i]>b[i])?a[i]:b[i];
349  }
350 
351  for(unsigned int i=min[0]; i<=max[0];++i)
352  {
353  for(unsigned int j=min[1]; j<=max[1]; ++j)
354  {
355  for(unsigned int k=min[2]; k<=max[2]; ++k)
356  {
357  Index p(i,j,k);
358  //cout<<"Index x: "<< i <<" y:"<< j <<" z:"<< k <<"\n";
359  temp = get_cell(p);
360  if(temp!=NULL){
361  //cout<< "temp size: " << temp->size() <<"\n";
362  ret->insert(ret->begin(), temp->begin(), temp->end());
363  }
364  }
365  }
366  }
367  return ret;
368  }
369 
370  vector<T> * get_cell(Point3D &p){
371  return grid->locate_only(p);
372  }
373 
374  vector<T> * get_cell(UniformGrid::Index &p){
375  for(int i=0;i<3;i++)
376  if ((p[i] < 0) || (p[i] >= static_cast<unsigned int>(cell_dim[i])))
377  return NULL;
378  //cerr << "i get in here\n";
379  return grid->locate_index_only(p);
380  }
381 
382 };
383 
384 
385 #endif
vector< T > * get_cell(Point3D &p)
CellData * locate_packed_index_only(unsigned int i)
void get_cellsize(Point3D &c)
T euclid_distance(const NVec< DIM, T > &u, const NVec< DIM, T > &v)
vector< T > * get_cell(UniformGrid::Index &p)
const NT & d
j indices k indices k
Definition: Indexing.h:6
CellData * locate(const Point3D &v)
void int int REAL REAL * y
Definition: read.cpp:74
Map::iterator iterator
Vector_n max(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:354
CellData * iterator_data(iterator i)
CellData * locate_index_only(const UniformGrid::Index &i)
CellData * locate_index(const UniformGrid::Index &i)
Map::const_iterator const_iterator
NVec< 3, index_t > Index
int coord[NPANE][NROW *NCOL][3]
Definition: blastest.C:86
vector< T > SpaceCell
bool operator<(const KNNnbr &p) const
vector< T > * get_cell_range(Point3D &p, Point3D &q)
int insertRange(T &item, Point3D &p, Point3D &q, Point3D &r)
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed free of to any person **obtaining a copy of this software and associated documentation to deal with the Software without including without limitation the rights to and or **sell copies of the and to permit persons to whom the **Software is furnished to do subject to the following this list of conditions and the following disclaimers ****Redistributions in binary form must reproduce the above **copyright this list of conditions and the following **disclaimers in the documentation and or other materials **provided with the distribution ****Neither the names of the Center for Simulation of Advanced the University of nor the names of its **contributors may be used to endorse or promote products derived **from this Software without specific prior written permission ****THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE v
Definition: roccomf90.h:20
map< unsigned int, CellData * > Map
const int KNNDIM
void set_extent(const Point3D &m, const Point3D &M)
void int int int REAL REAL REAL * z
Definition: write.cpp:76
UniformGrid::Index compute_index(const Point3D &v)
CellData * locate_packed_index(const unsigned int i)
blockLoc i
Definition: read.cpp:79
CellData * locate_only(const Point3D &v)
void int int REAL * x
Definition: read.cpp:74
int insert(T &item, Point3D &p)
const NT & n
unsigned int k_nearest(Point3D &p, int k, vector< T > &nbrs)
KNN_Grid(Point3D m, Point3D M, int x_dim, int y_dim, int z_dim)
int usage_count() const
Vector_n min(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:346
j indices j
Definition: Indexing.h:6
NT q
SparseGrid(int w, int h, int d)
unsigned int pack_index(const UniformGrid::Index &i)
NVec< 3, index_t > Index
unsigned int index_t
UniformGrid(index_t w, index_t h, index_t d)
NVec< 3, coord_t > Vec
SparseGrid< SpaceCell > * grid
unsigned int index_t
KNN_Grid(Point3D m, Point3D M, int long_dim)