bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
geom_point.h
Go to the documentation of this file.
1#ifndef GEOM_POINT_H
2#define GEOM_POINT_H
3
4#include "bitrl/bitrl_types.h"
5
6#include <algorithm>
7#include <array>
8#include <cmath>
9#include <ostream>
10#include <string>
11#include <vector>
12
13namespace bitrl
14{
15namespace utils::geom
16{
17
21template <int spacedim, typename T = real_t> class GeomPoint
22{
23
24public:
28 typedef T value_type;
29
33 static const int dimension = spacedim;
34
38 explicit GeomPoint(T val = T());
39
43 template <typename Container> explicit GeomPoint(const Container &data);
44
48 GeomPoint(const std::initializer_list<T> &list);
49
52
55
57 virtual ~GeomPoint() {}
58
63
66
69
72
76 GeomPoint &operator=(const std::initializer_list<T> &list);
77
81 void scale(T factor);
82
87 void scale(const std::vector<T> &factors);
88
92 void zero();
93
95 void add_scaled(const GeomPoint &p, T factor);
96
99
102
104 T entry(uint_t i) const { return (*this)[i]; }
105
107 auto coordinates() const { return data_; }
108
112 T max() const;
113
117 T min() const;
118
120 T distance(const GeomPoint &) const;
121
125 T L2_norm() const { return distance(GeomPoint(static_cast<T>(0))); }
126
131 T dot(const GeomPoint &other) const;
132
136 T square_sum() const;
137
141 std::ostream &print_point_info(std::ostream &out) const;
142
146 const std::string to_string() const;
147
148private:
150 std::array<T, spacedim> data_;
151};
152
153template <int spacedim, typename T> inline GeomPoint<spacedim, T>::GeomPoint(T val) : data_()
154{
155 std::for_each(data_.begin(), data_.end(), [=](T &item) { item = val; });
156}
157
158template <int spacedim, typename T>
159template <typename Container>
160inline GeomPoint<spacedim, T>::GeomPoint(const Container &data) : data_()
161{
162 for (uint_t i = 0; i < data_.size(); ++i)
163 {
164 data_[i] = data[i];
165 }
166}
167
168template <int spacedim, typename T>
169GeomPoint<spacedim, T>::GeomPoint(const std::initializer_list<T> &list) : data_()
170{
171 if (list.size() != spacedim)
172 {
173 std::string msg;
174 msg += "Invalid initialization ";
175 msg += " list size for point construction. " + std::to_string(list.size());
176 msg += " not equal to " + std::to_string(spacedim);
177 throw std::logic_error(msg);
178 }
179
180 auto start = list.begin();
181 auto end = list.end();
182
183 uint_t i = 0;
184 for (; start != end; ++start)
185 {
186 data_[i++] = *start;
187 }
188}
189
190template <int spacedim, typename T>
192{
193}
194
195template <int spacedim, typename T>
197{
198
199 if (this == &t)
200 {
201 return *this;
202 }
203 data_ = t.data_;
204 return *this;
205}
206
207template <int spacedim, typename T> inline T &GeomPoint<spacedim, T>::operator[](uint_t i)
208{
209 return data_[i];
210}
211
212template <int spacedim, typename T> inline T GeomPoint<spacedim, T>::operator[](uint_t i) const
213{
214 return data_[i];
215}
216
217template <int spacedim, typename T>
219{
220
221 for (int i = 0; i < spacedim; ++i)
222 {
223 data_[i] += t.data_[i];
224 }
225 return *this;
226}
227
228template <int spacedim, typename T>
230{
231
232 for (int i = 0; i < spacedim; ++i)
233 {
234 data_[i] -= t.data_[i];
235 }
236 return *this;
237}
238
239template <int spacedim, typename T>
241{
242
243 for (int i = 0; i < spacedim; ++i)
244 {
245 data_[i] *= factor;
246 }
247
248 return *this;
249}
250
251template <int spacedim, typename T>
253{
254
255 (*this) *= (static_cast<T>(1) / factor);
256 return *this;
257}
258
259template <int spacedim, typename T>
261GeomPoint<spacedim, T>::operator=(const std::initializer_list<T> &list)
262{
263
264 if (list.size() != spacedim)
265 {
266 std::string msg;
267 msg += "Invalid initialization ";
268 msg += " list size for point construction. " + std::to_string(list.size());
269 msg += " not equal to " + std::to_string(spacedim);
270 throw std::logic_error(msg);
271 }
272
273 auto start = list.begin();
274 auto end = list.end();
275
276 uint_t i = 0;
277 for (; start != end; ++start)
278 {
279 data_[i++] = *start;
280 }
281
282 return *this;
283}
284
285template <int spacedim, typename T>
287{
288
289 for (int i = 0; i < spacedim; ++i)
290 {
291 data_[i] += p.data_[i] * factor;
292 }
293}
294
295template <int spacedim, typename T> T GeomPoint<spacedim, T>::max() const
296{
297
298 T max_val = data_[0];
299 for (int i = 1; i < spacedim; ++i)
300 {
301 max_val = std::max(max_val, data_[i]);
302 }
303
304 return max_val;
305}
306
307template <int spacedim, typename T> T GeomPoint<spacedim, T>::min() const
308{
309
310 T min_val = data_[0];
311
312 for (int i = 1; i < spacedim; ++i)
313 {
314 min_val = std::min(min_val, data_[i]);
315 }
316
317 return min_val;
318}
319
320template <int spacedim, typename T>
322{
323
324 T sum = T();
325
326 auto data = p.coordinates();
327
328 for (int i = 0; i < spacedim; ++i)
329 sum += (data_[i] - data[i]) * (data_[i] - data[i]);
330
331 return std::sqrt(sum);
332}
333
334template <int spacedim, typename T>
335void GeomPoint<spacedim, T>::scale(const std::vector<T> &factors)
336{
337 for (int i = 0; i < spacedim; ++i)
338 {
339 data_[i] *= factors[i];
340 }
341}
342
343template <int spacedim, typename T> T GeomPoint<spacedim, T>::square_sum() const
344{
345 T result = T(0);
346
347 std::for_each(data_.begin(), data_.end(), [&](const T &value) { result += value * value; });
348
349 return result;
350}
351
352template <int spacedim, typename T>
354{
355
356 T result = (*this)[0] * other[0];
357
358 for (uint_t i = 1; i < spacedim; ++i)
359 {
360 result += (*this)[i] * other[i];
361 }
362
363 return result;
364}
365
366template <int spacedim, typename T> inline void GeomPoint<spacedim, T>::zero()
367{
368 for (int i = 0; i < spacedim; ++i)
369 data_[i] = T();
370}
371
372template <int spacedim, typename T>
373inline std::ostream &GeomPoint<spacedim, T>::print_point_info(std::ostream &out) const
374{
375
376 out << "( ";
377
378 for (int i = 0; i < spacedim; ++i)
379 {
380 out << data_[i];
381
382 if (i < spacedim - 1)
383 {
384 out << ",";
385 }
386 }
387
388 out << " )";
389 return out;
390}
391
392template <int spacedim, typename T> const std::string GeomPoint<spacedim, T>::to_string() const
393{
394
395 std::string rslt("");
396 auto end = data_.begin();
397 std::advance(end, spacedim - 1);
398 std::for_each(data_.begin(), end,
399 [&](const T &value)
400 {
401 rslt += std::to_string(value);
402 rslt += ",";
403 });
404
405 rslt += std::to_string(data_[spacedim - 1]);
406 return rslt;
407}
408
410
412template <int spacedim, typename T>
413inline std::ostream &operator<<(std::ostream &out, const GeomPoint<spacedim, T> &p)
414{
415 return p.print_point_info(out);
416}
417
420template <int spacedim, typename T>
422{
423 return (GeomPoint<spacedim, T>(t) *= factor);
424}
425
427template <int spacedim, typename T>
428const GeomPoint<spacedim, T> scale(const GeomPoint<spacedim, T> &t, const std::vector<T> &factors)
429{
430
431 auto data = t.data();
432
433 for (int i = 0; i < spacedim; ++i)
434 data[i] *= factors[i];
435
436 return GeomPoint<spacedim, T>(data);
437}
438
441template <int spacedim, typename T>
443{
444 auto data = t.coordinates();
445
446 for (int i = 0; i < spacedim; ++i)
447 data[i] = std::abs(data[i]);
448
449 return GeomPoint<spacedim, T>(data);
450}
451
453template <int spacedim, typename T>
455{
456
457 return (GeomPoint<spacedim, T>(t) *= factor);
458}
459
460template <int spacedim, typename T>
462{
463
464 return (GeomPoint<spacedim, T>(t) *= factor);
465}
466
467template <int spacedim, typename T>
469{
470
471 return (GeomPoint<spacedim, T>(t) /= factor);
472}
473
474template <int spacedim, typename T>
481
482template <int spacedim, typename T>
488
489template <int spacedim, typename T>
491{
492 bool result = true;
493
494 for (int i = 0; i < spacedim; ++i)
495 {
496 if (t1[i] != t2[i])
497 {
498
499 result = false;
500 }
501 }
502
503 return result;
504}
505
506template <int spacedim, typename T>
508{
509 return !(t1 == t2);
510}
511
512}
513} // namespace bitrl
514
515#endif // GEOM_POINT_H
A class that describes a point with spacedim spatial dimension space.
Definition geom_point.h:22
T min() const
Get the min element in the point.
Definition geom_point.h:307
static const int dimension
dimension. Spatial dimension of the point
Definition geom_point.h:33
T distance(const GeomPoint &) const
Get the distance from the given point.
Definition geom_point.h:321
T entry(uint_t i) const
access the i-th coordinate of the point read-only
Definition geom_point.h:104
GeomPoint & operator-=(const GeomPoint &)
\detailed Subtract another tensor.
Definition geom_point.h:229
void scale(T factor)
scale with a given factor
T value_type
value_type. The type of the coordinates
Definition geom_point.h:28
T & operator[](uint_t i)
Access the i-th coordinate of the point.
Definition geom_point.h:207
virtual ~GeomPoint()
dtor
Definition geom_point.h:57
GeomPoint & operator+=(const GeomPoint &)
Add another vector, i.e. move this point by the given offset.
Definition geom_point.h:218
auto coordinates() const
Get a copy of the data of this object.
Definition geom_point.h:107
GeomPoint & operator/=(T factor)
Scale the vector by factor.
Definition geom_point.h:252
T dot(const GeomPoint &other) const
Returns the dot product of this point and the given point.
Definition geom_point.h:353
GeomPoint & operator=(const GeomPoint &t)
copy assignement operator
Definition geom_point.h:196
GeomPoint(T val=T())
ctor all dim data are assigned the given value
Definition geom_point.h:153
T L2_norm() const
Return the distance from the origin.
Definition geom_point.h:125
T operator[](uint_t i) const
Access the i-th coordinate of the point read-only.
Definition geom_point.h:212
T max() const
Get the max element in the point.
Definition geom_point.h:295
GeomPoint(const GeomPoint &t)
copy ctor
Definition geom_point.h:191
const std::string to_string() const
Returns string representation of the point.
Definition geom_point.h:392
void add_scaled(const GeomPoint &p, T factor)
Add the coordinates of the given point to this scaled by factor.
Definition geom_point.h:286
GeomPoint & operator*=(T factor)
Scale the point by factor.
Definition geom_point.h:240
GeomPoint(const std::initializer_list< T > &list)
Construct given an initializer_list.
Definition geom_point.h:169
GeomPoint & operator=(const std::initializer_list< T > &list)
operator = Assign from the initializer list
Definition geom_point.h:261
T square_sum() const
Returns the square sum of the compontents.
Definition geom_point.h:343
void scale(const std::vector< T > &factors)
scale this object by the given factors p factors should have size at least spacedim
Definition geom_point.h:335
std::ostream & print_point_info(std::ostream &out) const
print the point
Definition geom_point.h:373
GeomPoint(const Container &data)
Create by passing a vector of data.
Definition geom_point.h:160
void zero()
Zero the entries of the tensor.
Definition geom_point.h:366
std::ostream & operator<<(std::ostream &out, const GeomPoint< spacedim, T > &p)
free functions that work on the GeomPoint<spacedim,T> class
Definition geom_point.h:413
bool operator==(const GeomPoint< spacedim, T > &t1, const GeomPoint< spacedim, T > &t2)
Definition geom_point.h:490
const GeomPoint< spacedim, T > operator*(T factor, const GeomPoint< spacedim, T > &t)
Allow multiplication from left with a factor.
Definition geom_point.h:454
const GeomPoint< spacedim, T > abs(const GeomPoint< spacedim, T > &t)
Return a point having the absolute values of the given point object.
Definition geom_point.h:442
const GeomPoint< spacedim, T > operator/(const GeomPoint< spacedim, T > &t, T factor)
Definition geom_point.h:468
bool operator!=(const GeomPoint< spacedim, T > &t1, const GeomPoint< spacedim, T > &t2)
Definition geom_point.h:507
const GeomPoint< spacedim, T > operator+(const GeomPoint< spacedim, T > &t1, const GeomPoint< spacedim, T > &t2)
Definition geom_point.h:475
const GeomPoint< spacedim, T > operator-(const GeomPoint< spacedim, T > &t1, const GeomPoint< spacedim, T > &t2)
Definition geom_point.h:483
const GeomPoint< spacedim, T > scale(const GeomPoint< spacedim, T > &t, T factor)
scale the given point by facto and returns a copyr. This function does not change the entries of t.
Definition geom_point.h:421
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:25
Definition bitrl_consts.h:14
std::size_t uint_t
uint_t
Definition bitrl_types.h:43