bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
generic_line.h
Go to the documentation of this file.
1#ifndef BITSIM_GEOMETRY_GENERIC_LINE_H
2#define BITSIM_GEOMETRY_GENERIC_LINE_H
3
8
9#include <algorithm>
10#include <cmath>
11#include <stdexcept>
12
13namespace bitsim
14{
15namespace geometry
16{
17
23template <int dim> class GenericLine
24{
25 public:
26 static const int dimension = dim;
27
29
34
38 GenericLine(const vertex_type &p1, const vertex_type &p2);
39
43 GenericLine(const vertex_type &p1, const vertex_type &p2, uint_t id);
44
48 const vertex_type &get_vertex(uint_t v) const;
49
54
58 bool has_valid_id() const noexcept { return id_ != bitsim::consts::INVALID_ID; }
59
63 uint_t get_id() const noexcept { return id_; }
64
68 void set_id(uint_t id) noexcept { id_ = id; }
69
73 real_t slope() const;
74
78 real_t factor() const;
79
84 real_t distance(const vertex_type &n) const;
85
90
94 real_t length() const { return end_.distance(start_); }
95
96 private:
97 uint_t id_;
98 vertex_type start_;
99 vertex_type end_;
100};
101
102template <int dim>
103GenericLine<dim>::GenericLine() : id_(bitsim::consts::INVALID_ID), start_(), end_()
104{
105}
106
107template <int dim>
109 : id_(id), start_(p1), end_(p2)
110{
111}
112
113template <int dim>
115 : GenericLine<dim>(p1, p2, bitsim::consts::INVALID_ID)
116{
117}
118
119template <int dim>
121{
122
123 if (v == 0)
124 {
125 return start_;
126 }
127 else if (v == 1)
128 {
129 return end_;
130 }
131
132 throw std::logic_error("Invalid vertex index. Index not in [0,1]");
133}
134
136{
137
138 if (v == 0)
139 {
140 return start_;
141 }
142 else if (v == 1)
143 {
144 return end_;
145 }
146
147 throw std::logic_error("Invalid vertex index. Index not in [0,1]");
148}
149
150template <int dim> real_t GenericLine<dim>::slope() const
151{
152 return (end_[1] - start_[1]) / (end_[0] - start_[0]);
153}
154
155template <int dim> real_t GenericLine<dim>::factor() const
156{
157 auto slope_ = slope();
158 return start_[1] - slope_ * start_[0];
159}
160
161// The specialisations are inline: this header can be included in more than one source file
163{
164 // |d x (n - start)| / |d| with d the direction of the line, which, unlike the slope, works for
165 // a vertical line too
166 const real_t dx = end_[0] - start_[0];
167 const real_t dy = end_[1] - start_[1];
168 const real_t length = std::hypot(dx, dy);
169 if (length == 0.0)
170 {
171 return std::hypot(n[0] - start_[0], n[1] - start_[1]);
172 }
173 return std::fabs(dx * (n[1] - start_[1]) - dy * (n[0] - start_[0])) / length;
174}
175
176template <>
178{
179 // The nearest point of the segment is start + t d, with t the projection clamped to [0, 1]
180 const real_t dx = end_[0] - start_[0];
181 const real_t dy = end_[1] - start_[1];
182 const real_t length2 = dx * dx + dy * dy;
183 const real_t t =
184 length2 == 0.0
185 ? 0.0
186 : std::clamp(((n[0] - start_[0]) * dx + (n[1] - start_[1]) * dy) / length2, 0.0, 1.0);
187 return std::hypot(n[0] - (start_[0] + t * dx), n[1] - (start_[1] + t * dy));
188}
189
190} // namespace geometry
191} // namespace bitsim
192#endif // BITSIM_GEOMETRY_GENERIC_LINE_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
bool has_valid_id() const noexcept
Returns true iff id_ != rlenvscpp::consts::INVALID_ID.
Definition generic_line.h:58
real_t slope() const
The slope of the line; infinite for a vertical line.
Definition generic_line.h:150
GenericLine()
Constructor.
Definition generic_line.h:103
uint_t get_id() const noexcept
Returns the id of the line.
Definition generic_line.h:63
real_t segment_distance(const vertex_type &n) const
The distance of the point from the segment between the two vertices.
void set_id(uint_t id) noexcept
Set the id of the line.
Definition generic_line.h:68
const vertex_type & get_vertex(uint_t v) const
Returns the v-th vertex of the segment.
Definition generic_line.h:120
real_t factor() const
Returns the constant factor.
Definition generic_line.h:155
real_t distance(const vertex_type &n) const
The perpendicular distance of the point from the infinite line through the two vertices; the distance...
static const int dimension
Definition generic_line.h:26
real_t length() const
Calculate the length of the line.
Definition generic_line.h:94
GeomPoint< dim > vertex_type
Definition generic_line.h:28
A class that describes a point with spacedim spatial dimension space.
Definition geom_point.h:22
Definition bitsim_consts.cpp:10
const uint_t INVALID_ID
Invalid id.
Definition bitsim_consts.h:16
Definition generic_line.h:16
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20