Point.h
Go to the documentation of this file.
1 /*
2  * Point.h
3  *
4  * Created on: Apr 11, 2013
5  * Author: Henning
6  */
7
8 #ifndef POINT_H_
9 #define POINT_H_
10
11 #include <vector>
12 #include <cinttypes>
13 #include <cassert>
14 #include <cmath>
15 #include <cstdint>
16 #include <iostream>
17 #include <sstream>
18
19 namespace NetworKit {
20
21 typedef uint64_t index; // more expressive name for an index into an array
22 typedef uint64_t count; // more expressive name for an integer quantity
23
24
25
38 template<class T>
39 class Point {
40 protected:
41  std::vector<T> data;
42
43 public:
44  Point() { data = {0.0, 0.0}; }
45  Point(T x, T y) { data = {x, y}; }
46  Point(count dimension) : data(std::vector<T>(dimension, 0.0)) {}
47  Point(std::vector<T>& values): data(values) {}
48  virtual ~Point() {}
49
50  count getDimensions() const { return data.size(); }
51
52  T distance(const Point<T>& p) const;
53  T squaredDistance(const Point<T>& p) const;
54
55  Point& operator+=(const Point<T>& p);
56  Point& operator-=(const Point<T>& p);
57  Point& scale(const T factor);
58
59  Point operator-(const Point<T>& other);
60  Point operator+(const Point<T>& other);
61
62  Point operator*(const double scalar) const;
63
64  bool operator==(const Point<T>& other) const;
65  bool operator!=(const Point<T>& other) const;
66
67  void operator=(const Point<T>& other);
68
69  T length() const;
70  T squaredLength() const;
71
72  T& operator[](const index i);
73  T& at(const index i);
74
75  T operator[](const index i) const;
76  T at(const index i) const;
77
81  std::string toString();
82
86  std::string toCsvString();
87
91  std::string toSsvString();
92
93  std::string genericToString(const std::string& start, const std::string& sep, const std::string& end);
94
95 // friend std::ostream& operator<< <>(std::ostream &out, Point<T>& point);
96 };
97
98 template<class T>
99 T Point<T>::length() const {
100  T length = (T) 0;
101  for (index i = 0; i < data.size(); ++i) {
102  T diff = this->data[i];
103  length += diff * diff;
104  }
105  return sqrt(length);
106 }
107
108 template<class T>
110  T length = (T) 0;
111  for (index i = 0; i < data.size(); ++i) {
112  T diff = this->data[i];
113  length += diff * diff;
114  }
115  return length;
116 }
117
118 template<class T>
120  assert(this->data.size() == p.data.size());
121  T dist = (T) 0;
122  for (index i = 0; i < data.size(); ++i) {
123  T diff = this->data[i] - p.data[i];
124  dist += diff * diff;
125  }
126  return dist;
127 }
128
129 template<class T>
130 T Point<T>::distance(const Point<T>& p) const {
131  return sqrt(squaredDistance(p));
132 }
133
134 template<class T>
136  assert(this->data.size() == p.data.size());
137  for (index i = 0; i < data.size(); ++i) {
138  this->data[i] += p.data[i];
139  }
140  return *this;
141 }
142
143 template<class T>
145  assert(this->data.size() == p.data.size());
146  for (index i = 0; i < data.size(); ++i) {
147  this->data[i] -= p.data[i];
148  }
149  return *this;
150 }
151
152 template<class T>
154  Point<T> result(*this);
155  assert(result.data.size() == other.data.size());
156  for (index i = 0; i < result.data.size(); ++i) {
157  result.data[i] -= other.data[i];
158  }
159  return result;
160 }
161
162 template<class T>
164  Point<T> result(*this);
165  assert(result.data.size() == other.data.size());
166  for (index i = 0; i < result.data.size(); ++i) {
167  result.data[i] += other.data[i];
168  }
169  return result;
170 }
171
172 template<typename T>
173 Point<T> Point<T>::operator*(const double scalar) const {
174  Point<T> result(*this);
175  for (index i = 0; i < getDimensions(); ++i) {
176  result.data[i] = data[i] * scalar;
177  }
178
179  return result;
180 }
181
182 template<typename T>
183 bool Point<T>::operator==(const Point<T>& other) const {
184  if (getDimensions() != other.getDimensions()) return false;
185  for (index i = 0; i < data.size(); ++i) {
186  if (data[i] != other.data[i]) return false;
187  }
188  return true;
189 }
190
191 template<typename T>
192 bool Point<T>::operator!=(const Point<T>& other) const {
193  return !(*this == other);
194 }
195
196 template<typename T>
197 void Point<T>::operator=(const Point& other) {
198  this->data = other.data;
199 }
200
201
202 template<class T>
203 Point<T>& Point<T>::scale(const T factor) {
204  for (index i = 0; i < data.size(); ++i) {
205  this->data[i] *= factor;
206  }
207  return *this;
208 }
209
210 template<class T>
212  assert(i >= 0 && i < data.size());
213  return data[i];
214 }
215
216 template<class T>
217 inline T& Point<T>::at(index i) {
218  assert(i >= 0 && i < data.size());
219  return data[i];
220 }
221
222 template<class T>
223 inline T Point<T>::operator [](index i) const {
224  assert(i >= 0 && i < data.size());
225  return data[i];
226 }
227
228 template<class T>
229 inline T Point<T>::at(index i) const {
230  assert(i >= 0 && i < data.size());
231  return data[i];
232 }
233
234 template<class T>
235 std::ostream& operator <<(std::ostream& out, Point<T>& point)
236 {
237  assert(point.data.size() > 0);
238  out << "(" << point[0];
239  for (index i = 1; i < point.data.size(); ++i) {
240  out << ", " << point.data[i];
241  }
242  out << ")";
243  return out;
244 }
245
246 template<class T>
247 std::string Point<T>::toString() {
248  return genericToString("", ", ", "");
249 }
250
251 template<class T>
252 inline std::string NetworKit::Point<T>::toCsvString() {
253  return genericToString("(", ", ", ")");
254 }
255
256 template<class T>
257 inline std::string NetworKit::Point<T>::toSsvString() {
258  return genericToString("", " ", "");
259 }
260
261 template<class T>
263  const std::string& start, const std::string& sep,
264  const std::string& end)
265 {
266  assert(this->data.size() > 0);
267  std::stringstream out;
268  out << start << (*this)[0];
269  for (index i = 1; i < this->data.size(); ++i) {
270  out << sep << this->data[i];
271  }
272  out << end;
273  return out.str();
274 }
275
276 } /* namespace NetworKit */
277
278 #endif /* POINT_H_ */
Point & scale(const T factor)
Definition: Point.h:203
Point & operator-=(const Point< T > &p)
Definition: Point.h:144
std::vector< T > data
Definition: Point.h:41
std::string toString()
Default point to string conversion.
Definition: Point.h:247
std::string genericToString(const std::string &start, const std::string &sep, const std::string &end)
Definition: Point.h:262
T length() const
Definition: Point.h:99
uint64_t index
Typedefs.
Definition: Globals.h:20
void operator=(const Point< T > &other)
Definition: Point.h:197
std::string toSsvString()
Point to space separated string.
Definition: Point.h:257
T & operator[](const index i)
Definition: Point.h:211
bool operator!=(const Point< T > &other) const
Definition: Point.h:192
Point()
Definition: Point.h:44
Formerly marked as deprecated: To take advantage of automatic mapping between C++ and Python data str...
Definition: Point.h:39
Point(T x, T y)
Definition: Point.h:45
T & at(const index i)
Definition: Point.h:217
T squaredDistance(const Point< T > &p) const
Definition: Point.h:119
T distance(const Point< T > &p) const
Definition: Point.h:130
Point(std::vector< T > &values)
Definition: Point.h:47
uint64_t count
Definition: Globals.h:21
Point(count dimension)
Definition: Point.h:46
std::string toCsvString()
Point to comma separated string.
Definition: Point.h:252
bool operator==(const Point< T > &other) const
Definition: Point.h:183
T squaredLength() const
Definition: Point.h:109
Point operator-(const Point< T > &other)
Definition: Point.h:153
Point & operator+=(const Point< T > &p)
Definition: Point.h:135
count getDimensions() const
Definition: Point.h:50
virtual ~Point()
Definition: Point.h:48
Point operator+(const Point< T > &other)
Definition: Point.h:163
Point operator*(const double scalar) const
Definition: Point.h:173