ALL 0.9.3
A Loadbalacing Library
Loading...
Searching...
No Matches
ALL_Point.hpp
Go to the documentation of this file.
1/*
2Copyright 2018-2020 Rene Halver, Forschungszentrum Juelich GmbH, Germany
3Copyright 2018-2020 Godehard Sutmann, Forschungszentrum Juelich GmbH, Germany
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17 specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30
31#ifndef ALL_POINT_HEADER_INC
32#define ALL_POINT_HEADER_INC
33
35#include <algorithm>
36#include <cmath>
37#include <iostream>
38#include <stdexcept>
39#include <vector>
40
41namespace ALL {
42
43template <class T> class Point;
44template <typename T>
45std::ostream &operator<<(std::ostream &, const Point<T> &);
46
49template <class T> class Point {
50public:
52 Point() : dimension(0) {}
55 Point(const int d) : dimension(d) {
56 coordinates.resize(d);
57 weight = (T)1;
58 }
59
62 Point(const int d, const T *data) : Point<T>(d) {
63 // copy each element of the array into the vector (insecure, no boundary
64 // checks for data!)
65 for (auto i = 0; i < d; ++i)
66 coordinates.at(i) = data[i];
67 }
68
72 Point(const std::vector<T> &data) {
73 // initialize the coordinates vector with the data vector
74 coordinates.insert(coordinates.begin(), data.begin(), data.end());
75 // update the dimension with the size of the data vector
76 dimension = data.size();
77 }
78
83 Point(const int d, const T *data, const T w) : Point<T>(d, data) {
84 weight = w;
85 }
86
92 Point(const int d, const T *data, const T w, const long i)
93 : Point<T>(d, data, w) {
94 id = i;
95 }
96
101 Point(const std::vector<T> &data, const T w) : Point<T>(data) {
102 weight = w;
103 }
104
110 Point(const std::vector<T> &data, const T w, const long i)
111 : Point<T>(data, w) {
112 id = i;
113 }
114
117
118 // TODO: return values to check if operation was successful?
121 void setDimension(const int d) {
122 dimension = d;
123 coordinates.resize(d);
124 }
125
128 void set_weight(const T w) { weight = w; }
129
132 void set_id(const long i) { id = i; }
133
137 T &operator[](const std::size_t idx) { return coordinates.at(idx); }
138
142 const T &operator[](const std::size_t idx) const {
143 return coordinates.at(idx);
144 }
145
148 T getWeight() const { return weight; }
149
152 long get_id() const { return id; }
153
156 int getDimension() const { return dimension; }
157
162 T norm(T nd = 2) {
163 T res = 0;
164 for (int d = 0; d < dimension; ++d)
165 res += std::pow(coordinates.at(d), nd);
166 return std::pow(res, 1.0 / nd);
167 }
168
174 T d(Point<T> p) {
175 int d_p = p.getDimension();
176 if (d_p != dimension)
177 throw PointDimensionMissmatchException(__FILE__, __func__, __LINE__);
178 return dist(p).norm();
179 }
180
187 int d_p = p.getDimension();
188 if (d_p != dimension)
189 throw PointDimensionMissmatchException(__FILE__, __func__, __LINE__);
190 return dist(p, 1).norm();
191 }
192
199 int d_p = p.getDimension();
200 if (d_p != dimension)
201 throw PointDimensionMissmatchException(__FILE__, __func__, __LINE__);
202 std::vector<T> d_v;
203 for (int d = 0; d < dimension; ++d)
204 d_v.push_back(p[d] - coordinates.at(d));
205
206 return Point<T>(d_v);
207 }
208
215 T dist_plane(const Point<T> &A, const Point<T> &B,
216 const Point<T> &C) {
217
218 if (A.getDimension() != dimension || B.getDimension() != dimension ||
219 C.getDimension() != dimension)
220 throw PointDimensionMissmatchException(__FILE__, __func__, __LINE__);
221
222 // vectors spanning the plane from vertex 'a'
223 Point<T> vb = B - A;
224 Point<T> vc = C - A;
225
226 // normal vector of plane
227 Point<T> n = vb.cross(vc);
228 n = n/n.norm();
229
230 // return r.n - A.n = (r-A).n as distance from plane to r
231 return std::abs( (coordinates.at(0) - A[0]) * n[0] +
232 (coordinates.at(1) - A[1]) * n[1] +
233 (coordinates.at(2) - A[2]) * n[2] );
234 }
235
239 Point<T> operator+(const Point<T> &rhs) const {
240 if (rhs.getDimension() != dimension) {
241 throw PointDimensionMissmatchException(__FILE__, __func__, __LINE__);
242 }
243 Point<T> result(dimension);
244 for (int d = 0; d < dimension; ++d) {
245 result[d] = coordinates.at(d) + rhs[d];
246 }
247 return result;
248 }
249
254 Point<T> operator-(const Point<T> &rhs) const {
255 if (rhs.getDimension() != dimension) {
256 throw PointDimensionMissmatchException(__FILE__, __func__, __LINE__);
257 }
258 Point<T> result(dimension);
259 for (int d = 0; d < dimension; ++d) {
260 result[d] = coordinates.at(d) - rhs[d];
261 }
262 return result;
263 }
264
268 T operator*(const Point<T> &rhs) const {
269 if (rhs.getDimension() != dimension) {
270 throw PointDimensionMissmatchException(__FILE__, __func__, __LINE__);
271 }
272 T result = (T)0.0;
273 for (int d = 0; d < dimension; ++d) {
274 result += coordinates.at(d) * rhs[d];
275 }
276 return result;
277 }
278
282 Point<T> operator*(const T &rhs) const {
283 Point<T> result(dimension);
284 for (int d = 0; d < dimension; ++d) {
285 result[d] = coordinates.at(d) * rhs;
286 }
287 return result;
288 }
289
293 Point<T> operator/(const T &rhs) const {
294 Point<T> result(dimension);
295 for (int d = 0; d < dimension; ++d) {
296 result[d] = coordinates.at(d) / rhs;
297 }
298 return result;
299 }
300
305 Point<T> cross(const Point<T> &rhs) const {
306 if (rhs.getDimension() != dimension && dimension != 3) {
307 throw PointDimensionMissmatchException(__FILE__, __func__, __LINE__);
308 }
309 Point<T> result(dimension);
310 for (int d = 0; d < dimension; ++d) {
311 result[d] = coordinates.at((d + 1) % 3) * rhs[(d + 2) % 3] -
312 coordinates.at((d + 2) % 3) * rhs[(d + 1) % 3];
313 }
314 return result;
315 }
316
326 bool same_side_plane(const Point<T> &A, const Point<T> &B,
327 const Point<T> &C, const Point<T> &P) {
328 // compute difference vectors:
329 Point<T> BA = B - A;
330 Point<T> CA = C - A;
331 Point<T> PA = P - A;
332 Point<T> tA = *(this) - A;
333
334 // compute normal vector of plane
335 Point<T> n = CA.cross(BA);
336
337 // compute scalar product of distance
338 // vectors with normal vector
339 T PAn = PA * n;
340 T tAn = tA * n;
341
342 return ((PAn * tAn) > 0);
343 }
344
354 bool inTetrahedron(const Point<T> &A, const Point<T> &B,
355 const Point<T> &C, const Point<T> &D) {
359 return (same_side_plane(A, B, C, D) && same_side_plane(B, C, D, A) &&
360 same_side_plane(C, D, A, B) && same_side_plane(A, D, B, C));
361 };
362
363private:
365 int dimension;
367 long id;
368 // array containg the coordinates
369 std::vector<T> coordinates;
372 T weight;
373};
374
379template <class T>
380std::ostream &operator<<(std::ostream &os, const Point<T> &p) {
381 for (int i = 0; i < p.getDimension(); ++i)
382 os << p[i] << " ";
383 os << p.getWeight();
384 return os;
385}
386
392template <class T>
393Point<T> operator*(const T &lhs, const Point<T> &rhs) {
394 return rhs * lhs;
395}
396
397// template <class T>
398// Point<T> operator/(const T &lhs, const Point<T> &rhs) {
399// return rhs * ((T)1 / lhs);
400// }
401
402}//namespace ALL
403
404#endif
T operator*(const Point< T > &rhs) const
~Point()
destructor
T d(Point< T > p)
Point(const int d, const T *data, const T w)
Definition ALL_Point.hpp:83
Point(const std::vector< T > &data, const T w)
const T & operator[](const std::size_t idx) const
Point< T > operator+(const Point< T > &rhs) const
int getDimension() const
void set_id(const long i)
Point(const int d)
Definition ALL_Point.hpp:55
T getWeight() const
bool same_side_plane(const Point< T > &A, const Point< T > &B, const Point< T > &C, const Point< T > &P)
Point< T > dist(Point< T > &p)
T norm(T nd=2)
Point< T > operator/(const T &rhs) const
T d_1(Point< T > p)
Point< T > cross(const Point< T > &rhs) const
bool inTetrahedron(const Point< T > &A, const Point< T > &B, const Point< T > &C, const Point< T > &D)
T & operator[](const std::size_t idx)
Point< T > operator-(const Point< T > &rhs) const
Point(const int d, const T *data, const T w, const long i)
Definition ALL_Point.hpp:92
long get_id() const
Point(const std::vector< T > &data, const T w, const long i)
Point(const int d, const T *data)
Definition ALL_Point.hpp:62
Point()
default constructor
Definition ALL_Point.hpp:52
void setDimension(const int d)
Point(const std::vector< T > &data)
Definition ALL_Point.hpp:72
void set_weight(const T w)
Point< T > operator*(const T &rhs) const
T dist_plane(const Point< T > &A, const Point< T > &B, const Point< T > &C)
Definition ALL.hpp:75
std::ostream & operator<<(std::ostream &, const Point< T > &)
Point< T > operator*(const T &lhs, const Point< T > &rhs)
Exception to be used for missmatches in dimension for ALL::Point class.