bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
waypoint_trajectory.h
Go to the documentation of this file.
1#ifndef BITSIM_TRAJECTORY_WAYPOINT_TRAJECTORY_H
2#define BITSIM_TRAJECTORY_WAYPOINT_TRAJECTORY_H
4
5#include <limits>
6#include <utility>
7#include <vector>
8
9namespace bitsim
10{
11namespace trajectory
12{
13
20template <typename LinkType> class WaypointTrajectory
21{
22 public:
23 static_assert(std::is_default_constructible<LinkType>::value,
24 "LinkType must have default constructor");
25
26 using link_type = LinkType;
27
28 using w_point_type = typename link_type::w_point_type;
29
33 using link_iterator = typename std::vector<link_type>::iterator;
34 using const_link_iterator = typename std::vector<link_type>::const_iterator;
35
40
44 explicit WaypointTrajectory(uint_t n);
45
50 std::pair<real_t, link_type> distance(const w_point_type &p) const;
51
55 uint_t size() const { return links_.size(); }
56
60 void reserve(uint_t n) { links_.reserve(n); }
61
65 void clear() { links_.clear(); }
66
70 bool empty() const { return links_.empty(); }
71
75 void push(const link_type &link) { links_.push_back(link); }
76
80 link_type &operator[](uint_t i) { return links_[i]; }
81
85 const link_type &operator[](uint_t i) const { return links_[i]; }
86
90 void resize(uint_t n) { links_.resize(n); }
91
95 link_iterator begin() { return links_.begin(); }
96 link_iterator end() { return links_.end(); }
97
101 const_link_iterator begin() const { return links_.begin(); }
102 const_link_iterator end() const { return links_.end(); }
103
104 private:
108 std::vector<link_type> links_;
109};
110
111template <typename LinkType> WaypointTrajectory<LinkType>::WaypointTrajectory() : links_() {}
112
113template <typename LinkType> WaypointTrajectory<LinkType>::WaypointTrajectory(uint_t n) : links_()
114{
115 links_.resize(n);
116}
117
118template <typename LinkType>
119std::pair<real_t, typename WaypointTrajectory<LinkType>::link_type>
121 const typename WaypointTrajectory<LinkType>::w_point_type &p) const
122{
123
124 real_t dist_ = std::numeric_limits<real_t>::max();
125 link_type link_;
126
127 for (const auto &link : links_)
128 {
129
130 auto dist = link.distance(p);
131
132 if (dist < dist_)
133 {
134 dist_ = dist;
135 link_ = link;
136 }
137 }
138
139 return std::make_pair(dist_, link_);
140}
141
142} // namespace trajectory
143} // namespace bitsim
144
145#endif // BITSIM_TRAJECTORY_WAYPOINT_TRAJECTORY_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
link_type & operator[](uint_t i)
Returns a read/write reference of the i-th link.
Definition waypoint_trajectory.h:80
bool empty() const
Returns true if the trajectory is empty.
Definition waypoint_trajectory.h:70
const_link_iterator end() const
Definition waypoint_trajectory.h:102
typename link_type::w_point_type w_point_type
Definition waypoint_trajectory.h:28
uint_t size() const
How many waypoints the pah has.
Definition waypoint_trajectory.h:55
LinkType link_type
Definition waypoint_trajectory.h:26
typename std::vector< link_type >::iterator link_iterator
point iteration
Definition waypoint_trajectory.h:33
std::pair< real_t, link_type > distance(const w_point_type &p) const
Computes the minimum distance of the given point from the trajectory.
Definition waypoint_trajectory.h:120
typename std::vector< link_type >::const_iterator const_link_iterator
Definition waypoint_trajectory.h:34
void reserve(uint_t n)
Reserve space for waypoints.
Definition waypoint_trajectory.h:60
void clear()
clear the memory allocated for points and edges
Definition waypoint_trajectory.h:65
void resize(uint_t n)
Resize the underlying links.
Definition waypoint_trajectory.h:90
const link_type & operator[](uint_t i) const
Returns a read reference of the i-th link.
Definition waypoint_trajectory.h:85
link_iterator begin()
Raw node iteration.
Definition waypoint_trajectory.h:95
WaypointTrajectory()
Constructor.
Definition waypoint_trajectory.h:111
const_link_iterator begin() const
Raw node iteration.
Definition waypoint_trajectory.h:101
void push(const link_type &link)
Push a new link.
Definition waypoint_trajectory.h:75
link_iterator end()
Definition waypoint_trajectory.h:96
Definition line_segment_link.h:11
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