RepastHPC  2.3.1
Moore2DGridQuery.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  * Moore2DGridQuery.h
36  *
37  * Created on: Aug 12, 2010
38  * Author: nick
39  */
40 
41 #ifndef MOORE2DGRIDQUERY_H_
42 #define MOORE2DGRIDQUERY_H_
43 
44 #include "Grid2DQuery.h"
45 #include "Point.h"
46 #include "Grid.h"
47 
48 namespace repast {
49 
57 template<typename T>
58 class Moore2DGridQuery: public Grid2DQuery<T> {
59 
60 public:
61 
65  Moore2DGridQuery(const Grid<T, int>* grid);
66  virtual ~Moore2DGridQuery() {
67  }
68 
77  virtual void query(const Point<int>& center, int range, bool includeCenter, std::vector<T*>& out) const;
78 };
79 
80 template<typename T>
82  Grid2DQuery<T> (grid) {
83 }
84 
85 template<typename T>
86 void Moore2DGridQuery<T>::query(const Point<int>& center, int range, bool includeCenter, std::vector<T*>& out) const {
87 
88  int xMin = center[0] - range;
89  int yMin = center[1] - range;
90  int xMax = center[0] + range + 1;
91  int yMax = center[1] + range + 1;
92 
93  if (!Grid2DQuery<T>::_grid->isPeriodic()) {
94  if (xMin < Grid2DQuery<T>::minMax[0][0])
95  xMin = Grid2DQuery<T>::minMax[0][0];
96 
97  if (yMin < Grid2DQuery<T>::minMax[1][0])
98  yMin = Grid2DQuery<T>::minMax[1][0];
99 
100  if (xMax >= Grid2DQuery<T>::minMax[0][1])
101  xMax = Grid2DQuery<T>::minMax[0][1];
102 
103  if (yMax >= Grid2DQuery<T>::minMax[1][1])
104  yMax = Grid2DQuery<T>::minMax[1][1];
105  }
106 
107  if (includeCenter) {
108  for (int x = xMin; x < xMax; x++) {
109  for (int y = yMin; y < yMax; y++) {
110  Grid2DQuery<T>::_grid->getObjectsAt(Point<int> (x, y), out);
111  }
112  }
113  } else {
114  for (int x = xMin; x < xMax; x++) {
115  for (int y = yMin; y < yMax; y++) {
116  if (!(x == center[0] && y == center[1])) {
117  Grid2DQuery<T>::_grid->getObjectsAt(Point<int> (x, y), out);
118  }
119  }
120  }
121  }
122 
123 }
124 
125 }
126 
127 #endif /* MOORE2DGRIDQUERY_H_ */
repast::Moore2DGridQuery
Neighborhood query that gathers neighbors in a Moore (N, S, E, W, NE, etc.) neighborhood.
Definition: Moore2DGridQuery.h:58
repast::Grid2DQuery
Base class for neighborhood queries on discrete Grids.
Definition: Grid2DQuery.h:54
repast::Moore2DGridQuery::Moore2DGridQuery
Moore2DGridQuery(const Grid< T, int > *grid)
Creates Moore2DGridQuery that will query the specified Grid.
Definition: Moore2DGridQuery.h:81
repast::Grid< T, int >
repast::Moore2DGridQuery::query
virtual void query(const Point< int > &center, int range, bool includeCenter, std::vector< T * > &out) const
Queries the Grid for the Moore neighbors surrounding the center point within a specified range.
Definition: Moore2DGridQuery.h:86
repast::Point< int >