ALL
0.9.4
A Loadbalacing Library
Toggle main menu visibility
Loading...
Searching...
No Matches
ALL_Point.hpp
Go to the documentation of this file.
1
/*
2
Copyright 2018-2020 Rene Halver, Forschungszentrum Juelich GmbH, Germany
3
Copyright 2018-2020 Godehard Sutmann, Forschungszentrum Juelich GmbH, Germany
4
5
Redistribution and use in source and binary forms, with or without modification,
6
are permitted provided that the following conditions are met:
7
8
1. Redistributions of source code must retain the above copyright notice, this
9
list of conditions and the following disclaimer.
10
11
2. Redistributions in binary form must reproduce the above copyright notice,
12
this list of conditions and the following disclaimer in the documentation and/or
13
other materials provided with the distribution.
14
15
3. Neither the name of the copyright holder nor the names of its contributors
16
may be used to endorse or promote products derived from this software without
17
specific prior written permission.
18
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26
ANY 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
28
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
*/
30
31
#ifndef ALL_POINT_HEADER_INC
32
#define ALL_POINT_HEADER_INC
33
34
#include "
ALL_CustomExceptions.hpp
"
35
#include <algorithm>
36
#include <cmath>
37
#include <iostream>
38
#include <stdexcept>
39
#include <vector>
40
41
namespace
ALL
{
42
43
template
<
class
T>
class
Point
;
44
template
<
typename
T>
45
std::ostream &
operator<<
(std::ostream &,
const
Point<T>
&);
46
49
template
<
class
T>
class
Point
{
50
public
:
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
116
~Point
(){};
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
186
T
d_1
(
Point<T>
p) {
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
198
Point<T>
dist
(
Point<T>
&p) {
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
363
private
:
365
int
dimension;
367
long
id;
368
// array containg the coordinates
369
std::vector<T> coordinates;
372
T weight;
373
};
374
379
template
<
class
T>
380
std::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
392
template
<
class
T>
393
Point<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
ALL_CustomExceptions.hpp
ALL::Point
Definition
ALL_Point.hpp:49
ALL::Point::operator*
T operator*(const Point< T > &rhs) const
Definition
ALL_Point.hpp:268
ALL::Point::~Point
~Point()
destructor
Definition
ALL_Point.hpp:116
ALL::Point::d
T d(Point< T > p)
Definition
ALL_Point.hpp:174
ALL::Point::Point
Point(const int d, const T *data, const T w)
Definition
ALL_Point.hpp:83
ALL::Point::Point
Point(const std::vector< T > &data, const T w)
Definition
ALL_Point.hpp:101
ALL::Point::operator[]
const T & operator[](const std::size_t idx) const
Definition
ALL_Point.hpp:142
ALL::Point::operator+
Point< T > operator+(const Point< T > &rhs) const
Definition
ALL_Point.hpp:239
ALL::Point::getDimension
int getDimension() const
Definition
ALL_Point.hpp:156
ALL::Point::set_id
void set_id(const long i)
Definition
ALL_Point.hpp:132
ALL::Point::Point
Point(const int d)
Definition
ALL_Point.hpp:55
ALL::Point::getWeight
T getWeight() const
Definition
ALL_Point.hpp:148
ALL::Point::same_side_plane
bool same_side_plane(const Point< T > &A, const Point< T > &B, const Point< T > &C, const Point< T > &P)
Definition
ALL_Point.hpp:326
ALL::Point::dist
Point< T > dist(Point< T > &p)
Definition
ALL_Point.hpp:198
ALL::Point::norm
T norm(T nd=2)
Definition
ALL_Point.hpp:162
ALL::Point::operator/
Point< T > operator/(const T &rhs) const
Definition
ALL_Point.hpp:293
ALL::Point::d_1
T d_1(Point< T > p)
Definition
ALL_Point.hpp:186
ALL::Point::cross
Point< T > cross(const Point< T > &rhs) const
Definition
ALL_Point.hpp:305
ALL::Point::inTetrahedron
bool inTetrahedron(const Point< T > &A, const Point< T > &B, const Point< T > &C, const Point< T > &D)
Definition
ALL_Point.hpp:354
ALL::Point::operator[]
T & operator[](const std::size_t idx)
Definition
ALL_Point.hpp:137
ALL::Point::operator-
Point< T > operator-(const Point< T > &rhs) const
Definition
ALL_Point.hpp:254
ALL::Point::Point
Point(const int d, const T *data, const T w, const long i)
Definition
ALL_Point.hpp:92
ALL::Point::get_id
long get_id() const
Definition
ALL_Point.hpp:152
ALL::Point::Point
Point(const std::vector< T > &data, const T w, const long i)
Definition
ALL_Point.hpp:110
ALL::Point::Point
Point(const int d, const T *data)
Definition
ALL_Point.hpp:62
ALL::Point::Point
Point()
default constructor
Definition
ALL_Point.hpp:52
ALL::Point::setDimension
void setDimension(const int d)
Definition
ALL_Point.hpp:121
ALL::Point::Point
Point(const std::vector< T > &data)
Definition
ALL_Point.hpp:72
ALL::Point::set_weight
void set_weight(const T w)
Definition
ALL_Point.hpp:128
ALL::Point::operator*
Point< T > operator*(const T &rhs) const
Definition
ALL_Point.hpp:282
ALL::Point::dist_plane
T dist_plane(const Point< T > &A, const Point< T > &B, const Point< T > &C)
Definition
ALL_Point.hpp:215
ALL
Definition
ALL.hpp:78
ALL::operator<<
std::ostream & operator<<(std::ostream &, const Point< T > &)
Definition
ALL_Point.hpp:380
ALL::operator*
Point< T > operator*(const T &lhs, const Point< T > &rhs)
Definition
ALL_Point.hpp:393
ALL::PointDimensionMissmatchException
Exception to be used for missmatches in dimension for ALL::Point class.
Definition
ALL_CustomExceptions.hpp:111
include
ALL_Point.hpp
Generated by
1.17.0