bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
generic_line.h
Go to the documentation of this file.
1#ifndef GENERIC_LINE_H
2#define GENERIC_LINE_H
3
5#include "bitrl/bitrl_types.h"
8
9#include <cmath>
10#include <stdexcept>
11
12namespace bitrl
13{
14namespace utils
15{
16namespace geom
17{
18
24template <int dim> class GenericLine
25{
26 public:
27 static const int dimension = dim;
28
30
35
39 GenericLine(const vertex_type &p1, const vertex_type &p2);
40
44 GenericLine(const vertex_type &p1, const vertex_type &p2, uint_t id);
45
49 const vertex_type &get_vertex(uint_t v) const;
50
55
60
64 uint_t get_id() const noexcept { return id_; }
65
69 void set_id(uint_t id) noexcept { id_ = id; }
70
74 real_t slope() const;
75
79 real_t factor() const;
80
86
90 real_t length() const { return end_.distance(start_); }
91
92 private:
93 uint_t id_;
94 vertex_type start_;
95 vertex_type end_;
96};
97
98template <int dim>
99GenericLine<dim>::GenericLine() : id_(bitrl::consts::INVALID_ID), start_(), end_()
100{
101}
102
103template <int dim>
105 : id_(id), start_(p1), end_(p2)
106{
107}
108
109template <int dim>
111 : GenericLine<dim>(p1, p2, bitrl::consts::INVALID_ID)
112{
113}
114
115template <int dim>
117{
118
119 if (v == 0)
120 {
121 return start_;
122 }
123 else if (v == 1)
124 {
125 return end_;
126 }
127
128 throw std::logic_error("Invalid vertex index. Index not in [0,1]");
129}
130
132{
133
134 if (v == 0)
135 {
136 return start_;
137 }
138 else if (v == 1)
139 {
140 return end_;
141 }
142
143 throw std::logic_error("Invalid vertex index. Index not in [0,1]");
144}
145
146template <int dim> real_t GenericLine<dim>::slope() const
147{
148 return (end_[1] - start_[1]) / (end_[0] - start_[0]);
149}
150
151template <int dim> real_t GenericLine<dim>::factor() const
152{
153 auto slope_ = slope();
154 return start_[1] - slope_ * start_[0];
155}
156
158{
159 // we use the formula from
160 // https://brilliant.org/wiki/dot-product-distance-between-point-and-a-line/
161 // to calculate the distance
162
163 real_t A = -slope();
164 real_t C = -factor();
165 real_t B = 1.0;
166
167 return std::fabs(A * n[0] + B * n[1] + C) /
169}
170
171} // namespace geom
172} // namespace utils
173} // namespace bitrl
174#endif // GENERIC_LINE_H
class GenericLine. Represents a generic line with vertex VertexType The equation of a linein l=Ax + B...
Definition generic_line.h:25
uint_t get_id() const noexcept
Returns the id of the line.
Definition generic_line.h:64
const vertex_type & get_vertex(uint_t v) const
Returns the v-th vertex of the segment.
Definition generic_line.h:116
real_t slope() const
The slope of the line.
Definition generic_line.h:146
void set_id(uint_t id) noexcept
Set the id of the line.
Definition generic_line.h:69
real_t factor() const
Returns the constant factor.
Definition generic_line.h:151
bool has_valid_id() const noexcept
Returns true iff id_ != rlenvscpp::consts::INVALID_ID.
Definition generic_line.h:59
GeomPoint< dim > vertex_type
Definition generic_line.h:29
real_t length() const
Calculate the length of the line.
Definition generic_line.h:90
static const int dimension
Definition generic_line.h:27
real_t distance(const vertex_type &n) const
Returns the perpendicular distance from the line to the node.
GenericLine()
Constructor.
Definition generic_line.h:99
A class that describes a point with spacedim spatial dimension space.
Definition geom_point.h:22
T distance(const GeomPoint &) const
Get the distance from the given point.
Definition geom_point.h:321
const uint_t INVALID_ID
Invalid id.
Definition bitrl_consts.h:21
T sqr(const T &v)
Definition math_utils.h:61
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
double real_t
real_t
Definition bitrl_types.h:23
std::size_t uint_t
uint_t
Definition bitrl_types.h:43