RepastHPC  2.3.1
GridComponents.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  * GridComponents.h
36  *
37  * Created on: Jun 23, 2009
38  * Author: nick
39  */
40 
41 #ifndef GRIDCOMPONENTS_H_
42 #define GRIDCOMPONENTS_H_
43 
44 #include <boost/unordered_map.hpp>
45 #include <boost/shared_ptr.hpp>
46 
47 #include "GridDimensions.h"
48 #include "Grid.h"
49 #include "Random.h"
50 
51 namespace repast {
52 
57 class Borders {
58 
59 protected:
60  const GridDimensions _dimensions;
61 
62  void boundsCheck(const std::vector<int>& pt) const;
63  void boundsCheck(const std::vector<double>& pt) const;
64 
65 public:
67 
68  void transform(const std::vector<int>& in, std::vector<int>& out) const;
69  void transform(const std::vector<double>& in, std::vector<double>& out) const;
70 
71  bool isPeriodic() const {
72  return false;
73  }
74 };
75 
80 class StrictBorders : public Borders {
81 
82 public:
84 
85  void translate(const std::vector<double>& oldPos, std::vector<double>& newPos, const std::vector<double>& displacement) const;
86  void translate(const std::vector<int>& oldPos, std::vector<int>& newPos, const std::vector<int>& displacement) const;
87 
88 };
89 
95 class StickyBorders : public Borders {
96 
97 private:
98  std::vector<int> mins, maxs;
99 
100  template<typename T>
101  T calcCoord(T coord, int dimension) const;
102 
103 public:
104 
106  void translate(const std::vector<double>& oldPos, std::vector<double>& newPos, const std::vector<double>& displacement) const;
107  void translate(const std::vector<int>& oldPos, std::vector<int>& newPos, const std::vector<int>& displacement) const;
108 };
109 
110 template<typename T>
111 T StickyBorders::calcCoord(T coord, int dimension) const {
112  if (coord < mins[dimension]) return mins[dimension];
113  else if (coord > maxs[dimension]) return maxs[dimension];
114  else return coord;
115 }
116 
124 
125 private:
126  GridDimensions _dimensions;
127  std::vector<int> mins, maxs;
128 
129 public:
130 
131  WrapAroundBorders(GridDimensions dimensions);
132 
133  void transform(const std::vector<int>& in, std::vector<int>& out) const;
134  void transform(const std::vector<double>& in, std::vector<double>& out) const;
135  void translate(const std::vector<double>& oldPos, std::vector<double>& newPos, const std::vector<double>& displacement) const;
136  void translate(const std::vector<int>& oldPos, std::vector<int>& newPos, const std::vector<int>& displacement) const;
137 
138  void init(const GridDimensions& dimensions);
139 
140  bool isPeriodic() const {
141  return true;
142  }
143 
144 };
145 
153 template<typename T>
154 class SimpleAdder {
155 public:
156 
157  template <typename GridType>
158  void init(GridDimensions dimensions, GridType* grid) {}
159  bool add(boost::shared_ptr<T> agent) {
160  return true;
161  }
162 };
163 
164 /*
165 template<typename T>
166 class RandomAdder {
167 private:
168  std::vector<IntUniformGenerator> gens;
169 
170 public:
171 
172  void init(GridDimensions dimensions);
173  bool add(boost::shared_ptr<T> agent);
174 };
175 */
176 
177 
178 
179 
180 }
181 
182 #endif /* GRIDCOMPONENTS_H_ */
repast::WrapAroundBorders
Implements periodic wrap around style border semantics.
Definition: GridComponents.h:123
repast::Borders
Base class for representations of border semantics (e.g.
Definition: GridComponents.h:57
repast::StickyBorders
Implements sticky border semantics: translates out side of the border are clamped to the border coord...
Definition: GridComponents.h:95
repast::SimpleAdder
Basic class for adding elements to grids.
Definition: GridComponents.h:154
repast::GridDimensions
Basic structure for specifying grid dimenions.
Definition: GridDimensions.h:58
repast::StrictBorders
Implements strict grid border semantics: anything outside the dimensions is out of bounds.
Definition: GridComponents.h:80