RepastHPC  2.3.1
Point.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  * Point.h
36  *
37  * Created on: Apr 1, 2010
38  * Author: nick
39  */
40 
41 #ifndef POINT_H_
42 #define POINT_H_
43 
44 #include <vector>
45 #include <stdexcept>
46 #include <iostream>
47 #include <algorithm>
48 #include <math.h>
49 
50 #include <boost/serialization/access.hpp>
51 #include <boost/functional/hash.hpp>
52 
53 #include "RepastErrors.h"
54 
55 namespace repast {
56 
60 template<typename T>
61 class Point;
62 
66 template<typename T>
67 struct HashGridPoint {
68  std::size_t operator()(const Point<T>& pt) const {
69  return pt.hash;
70  }
71 };
72 
73 template<typename T>
74 bool operator==(const Point<T> &one, const Point<T> &two);
75 
76 template<typename T>
77 std::ostream& operator<<(std::ostream& os, const Point<T>& pt);
78 
85 template<typename T>
86 class Point {
87 
88 private:
89  friend bool operator==<> (const Point<T> &one, const Point<T> &two);
90  friend std::ostream& operator<<<> (std::ostream& os, const Point<T>& pt);
91  friend struct HashGridPoint<T> ;
92 
93  void calcHash();
94 
95  std::vector<T> point;
96  size_t hash;
97 
98  friend class boost::serialization::access;
99 
100  template<class Archive>
101  void serialize(Archive& ar, const unsigned int version) {
102  ar & point;
103  ar & hash;
104  }
105 
106 public:
107 
108  typedef typename std::vector<T>::const_iterator const_iterator;
109 
115  explicit Point(T x);
116 
123  Point(T x, T y);
124 
132  Point(T x, T y, T z);
133 
140  Point(std::vector<T> coordinates);
141 
147  T getX() const;
148 
157  T getY() const;
158 
167  T getZ() const;
168 
180  T getCoordinate(int coordIndex) const;
181 
189  void add(const Point<T> &pt);
190 
196  size_t dimensionCount() const {
197  return point.size();
198  }
199 
208  const T& operator[](size_t index) const {
209  return point[index];
210  }
211 
220  T& operator[](size_t index) {
221  return point[index];
222  }
223 
229  const std::vector<T>& coords() const {
230  return point;
231  }
232 
238  const_iterator begin() const {
239  return point.begin();
240  }
241 
247  const_iterator end() const {
248  return point.end();
249  }
250 
257  void copy(std::vector<T>& out) const;
258 };
259 
260 template<typename T>
261 bool operator==(const Point<T> &one, const Point<T> &two);
262 template<typename T>
263 bool operator!=(const Point<T> &one, const Point<T> &two);
264 template<typename T>
265 std::ostream& operator<<(std::ostream& os, const Point<T>& pt);
266 
267 template<typename T>
269  point.push_back(x);
270  calcHash();
271 }
272 
273 template<typename T>
274 Point<T>::Point(T x, T y) {
275  point.push_back(x);
276  point.push_back(y);
277  calcHash();
278 }
279 
280 template<typename T>
281 Point<T>::Point(T x, T y, T z) {
282  point.push_back(x);
283  point.push_back(y);
284  point.push_back(z);
285  calcHash();
286 }
287 
288 template<typename T>
289 Point<T>::Point(std::vector<T> coordinates) :
290  point(coordinates.size(), 0) {
291  std::copy(coordinates.begin(), coordinates.end(), point.begin());
292  calcHash();
293 }
294 
295 template<typename T>
296 void Point<T>::calcHash() {
297  hash = 17;
298  boost::hash<T> hasher;
299  for (size_t i = 0; i < point.size(); i++) {
300  hash = 37 * hash + hasher(point[i]);
301  }
302 }
303 
304 template<typename T>
305 T Point<T>::getX() const {
306  return point.at(0);
307 }
308 
309 template<typename T>
310 T Point<T>::getY() const {
311  return point.at(1);
312 }
313 
314 template<typename T>
315 T Point<T>::getZ() const {
316  return point.at(2);
317 }
318 
319 template<typename T>
320 T Point<T>::getCoordinate(int coordIndex) const {
321  return point.at(coordIndex);
322 }
323 
324 template<typename T>
325 void Point<T>::copy(std::vector<T>& out) const {
326  std::copy(point.begin(), point.end(), out.begin());
327 }
328 
329 template<typename T>
330 bool operator==(const Point<T> &one, const Point<T> &two) {
331  return one.point == two.point;
332 }
333 
334 template<typename T>
335 bool operator!=(const Point<T> &one, const Point<T> &two) {
336  return !(one == two);
337 }
338 
339 template<typename T>
340 void Point<T>::add(const Point<T> &pt) {
341  if (pt.dimensionCount() != dimensionCount()) throw Repast_Error_35<Point<T> >(*this, pt); // Points do not have same number of dimensions
342 
343  for (size_t i = 0; i < point.size(); i++) {
344  point[i] += pt.getCoordinate(i);
345  }
346 }
347 
348 template<typename T>
349 std::ostream& operator<<(std::ostream& os, const Point<T>& pt) {
350  os << "Point[";
351  for (size_t i = 0; i < pt.point.size(); i++) {
352  if (i > 0)
353  os << ", ";
354  os << pt.point[i];
355  }
356  os << "]";
357  return os;
358 }
359 
360 template<typename T>
361 bool operator<(const Point<T>& one, const Point<T>& two) {
362  return std::lexicographical_compare(one.begin(), one.end(), two.begin(), two.end());
363 }
364 
365 }
366 
367 #endif /* POINT_H_ */
368 
repast::Point::begin
const_iterator begin() const
Gets the start of an iterator over the coordinates of this point.
Definition: Point.h:238
repast::Point::getY
T getY() const
Gets the y coordinate of the point.
Definition: Point.h:310
repast::Point::operator[]
T & operator[](size_t index)
Gets the coordinate value at the specified index.
Definition: Point.h:220
repast::Point::Point
Point(T x)
Creates a one dimensional point with the specified value.
Definition: Point.h:268
repast::Point::getCoordinate
T getCoordinate(int coordIndex) const
Gets the coodinate of the point in the specified dimension.
Definition: Point.h:320
repast::HashGridPoint
Class that allows retrieval of hash value for Point objects.
Definition: Point.h:67
repast::Point::end
const_iterator end() const
Gets the end of an iterator over the coordinates of this point.
Definition: Point.h:247
repast::Point::copy
void copy(std::vector< T > &out) const
Copies the point into the specified vector.
Definition: Point.h:325
repast::Point::add
void add(const Point< T > &pt)
Adds the specified GridPoint to this GridPoint.
Definition: Point.h:340
repast::Point::dimensionCount
size_t dimensionCount() const
Gets the number of dimensions of this point.
Definition: Point.h:196
repast::Point::operator[]
const T & operator[](size_t index) const
Gets the coordinate value at the specified index.
Definition: Point.h:208
repast::Point::getX
T getX() const
Gets the x coordinate of the point.
Definition: Point.h:305
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::Point::getZ
T getZ() const
Gets the z coordinate of the point.
Definition: Point.h:315