RepastHPC  2.3.1
VN2DGridQuery.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * VN2DGridQuery.h
36  *
37  * Created on: Aug 12, 2010
38  * Author: nick
39  */
40 
41 #ifndef VN2DGRIDQUERY_H_
42 #define VN2DGRIDQUERY_H_
43 
44 #include "Grid2DQuery.h"
45 
46 namespace repast {
47 
55 template<typename T>
56 class VN2DGridQuery: public Grid2DQuery<T> {
57 
58 public:
59 
60  VN2DGridQuery(const Grid<T, int>* grid);
61  virtual ~VN2DGridQuery() {
62  }
63 
72  virtual void query(const Point<int>& center, int range, bool includeCenter, std::vector<T*>& out) const;
73 };
74 
75 template<typename T>
77  Grid2DQuery<T> (grid) {
78 }
79 
80 template<typename T>
81 void VN2DGridQuery<T>::query(const Point<int>& center, int range, bool includeCenter, std::vector<T*>& out) const {
82  if (includeCenter) {
83  Grid2DQuery<T>::_grid->getObjectsAt(center, out);
84  }
85 
86  int yMin = center[1] - range;
87  if (!Grid2DQuery<T>::_grid->isPeriodic() && yMin < Grid2DQuery<T>::minMax[1][0])
88  yMin = Grid2DQuery<T>::minMax[1][0];
89 
90  int yMax = center[1];
91  for (int y = yMin; y < yMax; y++) {
92  Grid2DQuery<T>::_grid->getObjectsAt(Point<int> (center[0], y), out);
93  }
94 
95  yMax = center[1] + range + 1;
96  if (!Grid2DQuery<T>::_grid->isPeriodic() && yMax >= Grid2DQuery<T>::minMax[1][1])
97  yMax = Grid2DQuery<T>::minMax[1][1];
98  // skip the center
99  yMin = center[1] + 1;
100  for (int y = yMin; y < yMax; y++) {
101  Grid2DQuery<T>::_grid->getObjectsAt(Point<int> (center[0], y), out);
102  }
103 
104  int xMin = center[0] - range;
105  if (!Grid2DQuery<T>::_grid->isPeriodic() && xMin < Grid2DQuery<T>::minMax[0][0])
106  xMin = Grid2DQuery<T>::minMax[0][0];
107  for (int x = xMin; x < center[0]; x++) {
108  Grid2DQuery<T>::_grid->getObjectsAt(Point<int> (x, center[1]), out);
109  }
110 
111  int xMax = center[0] + range + 1;
112  if (!Grid2DQuery<T>::_grid->isPeriodic() && xMax >= Grid2DQuery<T>::minMax[0][1])
113  xMax = Grid2DQuery<T>::minMax[0][1];
114  xMin = center[0] + 1;
115  for (int x = xMin; x < xMax; x++) {
116  Grid2DQuery<T>::_grid->getObjectsAt(Point<int> (x, center[1]), out);
117  }
118 }
119 
120 }
121 
122 #endif /* VN2DGRIDQUERY_H_ */
repast::Grid2DQuery
Base class for neighborhood queries on discrete Grids.
Definition: Grid2DQuery.h:54
repast::Grid< T, int >
repast::VN2DGridQuery::query
virtual void query(const Point< int > &center, int range, bool includeCenter, std::vector< T * > &out) const
Queries the Grid for the Von Neumann neighbors surrounding the center point within a specified range.
Definition: VN2DGridQuery.h:81
repast::VN2DGridQuery
Neighborhood query that gathers neighbors in a Von Neumann (N, S, E, W) neighborhood.
Definition: VN2DGridQuery.h:56
repast::Point< int >