1 #ifndef SPACEGRID_INCLUDED
2 #define SPACEGRID_INCLUDED
8 #include <ext/hash_map>
41 bb_min = m; bb_max = M;
45 cellsize[
k] /= (float)(size[
k]);
56 int x = (int)floor(((v[
k] - bb_min[
k]) / cellsize[k]));
71 template<
class CellData>
75 typedef map<unsigned int, CellData *>
Map;
83 {
return (i[0]<<20) | (i[1]<<10) | i[2]; }
94 for(
typename Map::iterator
i=cellmap.begin();
i!=cellmap.end(); ++
i)
117 iterator iter = cellmap.find(pack_index(i));
118 return iter==cellmap.end() ? NULL : iter->second;
124 return iter==cellmap.end() ? NULL : iter->second;
128 {
return locate_index_only(compute_index(v)); }
133 CellData *
d = locate_index(i);
136 cellmap[pack_index(i)] = d =
new CellData;
143 CellData *
d = locate_index_only(i);
146 cellmap[pack_index(i)] = d =
new CellData;
153 CellData *
d = locate_packed_index_only(i);
156 cellmap[
i] = d =
new CellData;
163 template <
typename T>
210 if ( box_len[0] > box_len[1])
214 if (box_len[2]>box_len[max_dim])
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]);
221 bbmax[
i] = bbmin[
i] + ((float) cell_dim[
i]) * cellsz[
i];
232 cell_dim[0]=x_dim;cell_dim[1]=y_dim;cell_dim[2]=z_dim;
240 cell->push_back(item);
250 Index a = grid->compute_index(p);
251 Index b = grid->compute_index(q);
252 Index c = grid->compute_index(r);
256 min[
i] = (a[
i]>b[
i])?b[
i]:a[
i];
257 max[
i] = (a[
i]>b[
i])?a[
i]:b[
i];
262 min[
i] = (min[
i]>c[
i])?c[
i]:min[
i];
263 max[
i] = (max[
i]>c[
i])?max[
i]:c[
i];
266 for(
unsigned int i=min[0];
i<=max[0]; ++
i)
268 for(
unsigned int j=min[1];
j<=max[1]; ++
j)
270 for(
unsigned int k=min[2];
k<=max[2]; ++
k)
273 SpaceCell * cell = grid->locate_index(x);
274 typename vector<T>::iterator temp ;
278 cell->push_back(item);
296 priority_queue<KNNnbr> pts;
297 vector< T > * cell_pts = NULL;
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++)
308 cell_pts = get_cell(i);
309 if (cell_pts != NULL)
311 for(
int j=0;
j<cell_pts->size();
j++)
314 pnew.item = (*cell_pts)[
j];
321 int ptnum = pts.size();
323 while( (j<k) && (!pts.empty()))
325 for(
int j=0; j<
k;j++)
327 KNNnbr
n = pts.top();
328 nbrs.push_back(n.item);
336 vector<T> * ret =
new vector<T>;
337 vector<T> * temp = NULL;
339 Index a = grid->compute_index(p);
340 Index b = grid->compute_index(q);
347 min[
i] = (a[
i]>b[
i])?b[
i]:a[
i];
348 max[
i] = (a[
i]>b[
i])?a[
i]:b[
i];
351 for(
unsigned int i=min[0];
i<=max[0];++
i)
353 for(
unsigned int j=min[1];
j<=max[1]; ++
j)
355 for(
unsigned int k=min[2];
k<=max[2]; ++
k)
362 ret->insert(ret->begin(), temp->begin(), temp->end());
371 return grid->locate_only(p);
376 if ((p[
i] < 0) || (p[
i] >= static_cast<unsigned int>(cell_dim[
i])))
379 return grid->locate_index_only(p);
vector< T > * get_cell(Point3D &p)
CellData * locate_packed_index_only(unsigned int i)
T euclid_distance(const NVec< DIM, T > &u, const NVec< DIM, T > &v)
vector< T > * get_cell(UniformGrid::Index &p)
CellData * locate(const Point3D &v)
void int int REAL REAL * y
Vector_n max(const Array_n_const &v1, const Array_n_const &v2)
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
int coord[NPANE][NROW *NCOL][3]
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
map< unsigned int, CellData * > Map
void int int int REAL REAL REAL * z
CellData * locate_packed_index(const unsigned int i)
CellData * locate_only(const Point3D &v)
int insert(T &item, Point3D &p)
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)
Vector_n min(const Array_n_const &v1, const Array_n_const &v2)
SparseGrid(int w, int h, int d)
unsigned int pack_index(const UniformGrid::Index &i)
SparseGrid< SpaceCell > * grid
KNN_Grid(Point3D m, Point3D M, int long_dim)