RepastHPC  2.3.1
ValueLayer.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  * ValueLayer.h
36  *
37  * Created on: Apr 1, 2010
38  * Author: nick
39  */
40 
41 #ifndef VALUELAYER_H_
42 #define VALUELAYER_H_
43 
44 #include <boost/noncopyable.hpp>
45 
46 #include "Point.h"
47 #include "GridDimensions.h"
48 #include "matrix.h"
49 
50 #include <functional>
51 
52 namespace repast {
53 
58 class BaseValueLayer: public boost::noncopyable {
59 
60 protected:
61  std::string _name;
62 
63 public:
67  BaseValueLayer(const std::string& name);
68  virtual ~BaseValueLayer() {
69  }
70 
76  std::string name() const {
77  return _name;
78  }
79 };
80 
87 template<typename ValueType, typename PointType>
88 class ValueLayer: public BaseValueLayer {
89 
90 protected:
91  GridDimensions _dimensions;
92 
96  void translate(std::vector<PointType>& pt);
97 
98 public:
99  ValueLayer(const std::string& name, const GridDimensions& dimensions);
100  virtual ~ValueLayer() {
101  }
102 
113  virtual ValueType& get(const Point<PointType>& pt) = 0;
114 
122  virtual void set(const ValueType& value, const Point<PointType>& pt) = 0;
123 
134  ValueType& operator[](const Point<PointType>& pt);
135 
146  const ValueType& operator[](const Point<PointType>& pt) const;
147 
153  const GridDimensions dimensions() const {
154  return _dimensions;
155  }
156 
162  const Point<double> shape() const {
163  return _dimensions.extents();
164  }
165 };
166 
167 template<typename ValueType, typename PointType>
168 ValueLayer<ValueType, PointType>::ValueLayer(const std::string& name, const GridDimensions& dimensions) :
169  BaseValueLayer(name), _dimensions(dimensions) {
170 }
171 
172 template<typename ValueType, typename PointType>
173 void ValueLayer<ValueType, PointType>::translate(std::vector<PointType>& pt) {
174  for (size_t i = 0; i < _dimensions.dimensionCount(); i++) {
175  pt[i] -= _dimensions.origin(i);
176  }
177 }
178 
179 template<typename ValueType, typename PointType>
181  return get(pt);
182 }
183 
184 template<typename ValueType, typename PointType>
186  return get(pt);
187 }
188 
196 template<typename ValueType, typename Borders>
197 class DiscreteValueLayer: public ValueLayer<ValueType, int> {
198 
199 private:
200  Matrix<ValueType>* matrix;
201  bool _dense;
202  Borders borders;
203 
204  void create(bool dense, Matrix<ValueType>* other);
205 
206 public:
209 
220  DiscreteValueLayer(const std::string& name, const GridDimensions& dimensions, bool dense,
221  const ValueType& defaultValue = ValueType());
223 
234  ValueType& get(const Point<int>& pt);
235 
243  void set(const ValueType& value, const Point<int>& pt);
244 };
245 
246 template<typename ValueType, typename Borders>
248  if (dense) {
249  matrix = new DenseMatrix<ValueType> (*(dynamic_cast<DenseMatrix<ValueType>*> (other)));
250  } else {
251  matrix = new SparseMatrix<ValueType> (*(dynamic_cast<SparseMatrix<ValueType>*> (other)));
252  }
253 
254 }
255 
256 template<typename ValueType, typename Borders>
257 DiscreteValueLayer<ValueType, Borders>::DiscreteValueLayer(const DiscreteValueLayer<ValueType, Borders>& other) :
258  ValueLayer<ValueType, int> (other.name(), other.dimensions()), _dense(other._dense), borders(other.dimensions()) {
259  create(_dense, other.matrix);
260 }
261 
262 template<typename ValueType, typename Borders>
263 DiscreteValueLayer<ValueType, Borders>& DiscreteValueLayer<ValueType, Borders>::operator=(const DiscreteValueLayer<
264  ValueType, Borders>& rhs) {
265  if (&rhs != this) {
266  delete matrix;
267  ValueLayer<ValueType, int>::_name = rhs.name();
268  ValueLayer<ValueType, int>::_dimensions = rhs.dimensions();
269  _dense = rhs._dense;
270  create(_dense, rhs.matrix);
271  borders = Borders(ValueLayer<ValueType, int>::_dimensions);
272  }
273  return *this;
274 }
275 
276 template<typename ValueType, typename Borders>
277 DiscreteValueLayer<ValueType, Borders>::~DiscreteValueLayer() {
278  delete matrix;
279 }
280 
281 template<typename ValueType, typename Borders>
283  bool dense, const ValueType& defaultValue) :
284  ValueLayer<ValueType, int> (name, dimensions), _dense(dense) , borders(Borders(ValueLayer<ValueType, int>::_dimensions)) {
285 
286  // this is dangerous but at some point dimensions has been converted to take
287  // double extents and we don't have time to convert everything
288  const std::vector<double>& coords = dimensions.extents().coords();
289  std::vector<int> converted_coords;
290  for (double d : coords) {
291  converted_coords.push_back(static_cast<int>(d));
292  }
293  if (dense) {
294  matrix = new DenseMatrix<ValueType> (Point<int>(converted_coords), defaultValue);
295  } else {
296  matrix = new SparseMatrix<ValueType> (Point<int>(converted_coords), defaultValue);
297  }
298 }
299 
300 template<typename ValueType, typename Borders>
302  std::vector<int> out(pt.dimensionCount());
303  borders.transform(pt.coords(), out);
304  if (_dense)
306  return matrix->get(Point<int> (out));
307 }
308 
309 template<typename ValueType, typename Borders>
310 void DiscreteValueLayer<ValueType, Borders>::set(const ValueType& value, const Point<int>& pt) {
311  std::vector<int> out(pt.dimensionCount());
312  borders.transform(pt.coords(), out);
313  if (_dense)
315  return matrix->set(value, Point<int> (out));
316 }
317 
326 template<typename ValueType, typename Borders>
327 class ContinuousValueLayer: public ValueLayer<ValueType, double> {
328 
329 private:
330 
331  Borders borders;
332  std::map<std::vector<double>, ValueType> values;
333  ValueType _defaultValue;
334 
335  typedef typename std::map<std::vector<double>, ValueType>::iterator values_iter;
336 
337 public:
340 
351  ContinuousValueLayer(const std::string& name, const GridDimensions& dimensions, const ValueType& defaultValue =
352  ValueType());
354  }
355 
366  ValueType& get(const Point<double>& pt);
367 
375  void set(const ValueType& value, const Point<double>& pt);
376 };
377 
378 
379 template<typename ValueType, typename Borders>
381  ValueLayer<ValueType, double> (other._name, other._dimensions), values(other.values), _defaultValue(
382  other._defaultValue), borders(other._dimensions) {
383 }
384 
385 template<typename ValueType, typename Borders>
386 ContinuousValueLayer<ValueType, Borders>& ContinuousValueLayer<ValueType, Borders>::operator=(
387  const ContinuousValueLayer<ValueType, Borders>& rhs) {
388 
389  if (&rhs != this) {
390  values.clear();
391  values.insert(rhs.values.begin(), rhs.values.end());
392  ValueLayer<ValueType, double>::_name = rhs.name();
393  ValueLayer<ValueType, double>::_dimensions = rhs.dimensions();
394  _defaultValue = rhs._defaultValue;
395  borders = Borders(ValueLayer<ValueType, int>::_dimensions);
396  }
397  return *this;
398 }
399 
400 template<typename ValueType, typename Borders>
402  const GridDimensions& dimensions, const ValueType& defaultValue) :
403  ValueLayer<ValueType, double> (name, dimensions), _defaultValue(defaultValue), borders(ValueLayer<ValueType, double>::_dimensions) {
404 }
405 
406 template<typename ValueType, typename Borders>
408  std::vector<double> out(pt.dimensionCount());
409  borders.transform(pt.coords(), out);
410  // need to insert do an insert and return reference to
411  // result so that assignment via [] uses a reference in
412  // the map. insert inserts the entry if it doesn't exist
413  // and returns the entry or the existing entry
414  std::pair<values_iter, bool> res = values.insert(std::make_pair(out, _defaultValue));
415  return res.first->second;
416 }
417 
418 template<typename ValueType, typename Borders>
419 void ContinuousValueLayer<ValueType, Borders>::set(const ValueType& value, const Point<double>& pt) {
420  std::vector<double> out(pt.dimensionCount());
421  borders.transform(pt.coords(), out);
422  values[out] = value;
423 }
424 
425 }
426 
427 #endif /* VALUELAYER_H_ */
repast::DiscreteValueLayer::set
void set(const ValueType &value, const Point< int > &pt)
Sets the value at the specified point.
Definition: ValueLayer.h:310
repast::GridDimensions::extents
const Point< double > & extents() const
Gets the extents along each dimension.
Definition: GridDimensions.h:90
repast::BaseValueLayer::name
std::string name() const
Gets the value layer's name.
Definition: ValueLayer.h:76
repast::ValueLayer
A collection that stores values at point locations.
Definition: ValueLayer.h:88
repast::ValueLayer::operator[]
ValueType & operator[](const Point< PointType > &pt)
Gets the value at the specified point.
Definition: ValueLayer.h:180
repast::ValueLayer::dimensions
const GridDimensions dimensions() const
Gets the dimensions of this ValueLayer.
Definition: ValueLayer.h:153
repast::Borders
Base class for representations of border semantics (e.g.
Definition: GridComponents.h:57
repast::BaseValueLayer::BaseValueLayer
BaseValueLayer(const std::string &name)
Creates a BaseValueLayer with the specified name.
Definition: ValueLayer.cpp:48
repast::DiscreteValueLayer::get
ValueType & get(const Point< int > &pt)
Gets the value at the specified point.
Definition: ValueLayer.h:301
repast::Matrix< ValueType >
repast::DiscreteValueLayer
Creates ValueLayer whose location coordinates are ints.
Definition: ValueLayer.h:197
repast::DenseMatrix
A dense matrix implementation that stores each cell individually.
Definition: matrix.h:162
repast::ValueLayer::get
virtual ValueType & get(const Point< PointType > &pt)=0
Gets the value at the specified point.
repast::ContinuousValueLayer
Continous value layer whose location coordinates are double.
Definition: ValueLayer.h:327
repast::GridDimensions
Basic structure for specifying grid dimenions.
Definition: GridDimensions.h:58
repast::SparseMatrix
A sparse matrix implementation that stores values in a map.
Definition: matrix.h:243
repast::ValueLayer::set
virtual void set(const ValueType &value, const Point< PointType > &pt)=0
Sets the value at the specified point.
repast::Point::dimensionCount
size_t dimensionCount() const
Gets the number of dimensions of this point.
Definition: Point.h:196
repast::ValueLayer::translate
void translate(std::vector< PointType > &pt)
Translates pt by dimensions origin.
Definition: ValueLayer.h:173
repast::Point::coords
const std::vector< T > & coords() const
Gets the coordinates of this point as a vector.
Definition: Point.h:229
repast::Point
A N-dimensional Point representation.
Definition: Point.h:61
repast::BaseValueLayer
Base implementation of a ValueLayer.
Definition: ValueLayer.h:58
repast::ContinuousValueLayer::set
void set(const ValueType &value, const Point< double > &pt)
Sets the value at the specified point.
Definition: ValueLayer.h:419
repast::ContinuousValueLayer::get
ValueType & get(const Point< double > &pt)
Gets the value at the specified point.
Definition: ValueLayer.h:407
repast::ValueLayer::shape
const Point< double > shape() const
Gets the extents of this ValueLayer.
Definition: ValueLayer.h:162