emlabcpp
modern opinionated embedded C++ library
pose.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "./point.h"
27 #include "./quaternion.h"
28 
29 #include <utility>
30 
31 namespace emlabcpp
32 {
33 
36 {
37  float dist;
38  float angle_dist;
39 };
40 
45 
46 constexpr std::size_t steps( pose_distance dist, float dist_step, float angle_step )
47 {
48  auto d_steps = std::size_t( 1.f + dist.dist / dist_step );
49  auto a_steps = std::size_t( 1.f + dist.angle_dist / angle_step );
50  return std::max( d_steps, a_steps );
51 }
52 
54 struct pose
55 {
56 
59 
60  constexpr pose()
61  : position( 0, 0, 0 )
63  {
64  }
65 
66  explicit constexpr pose( point< 3 > const& position )
67  : position( position )
69  {
70  }
71 
72  explicit constexpr pose( quaternion const& orientation )
73  : position( 0, 0, 0 )
75  {
76  }
77 
78  constexpr pose( point< 3 > const& position, quaternion const& orientation )
79  : position( position )
81  {
82  }
83 };
84 
85 constexpr bool operator<( pose const& x, pose const& y )
86 {
87  if ( x.position == y.position )
88  return x.orientation < y.orientation;
89  return x.position < y.position;
90 }
91 
93 constexpr bool operator==( pose const& x, pose const& y )
94 {
95  return x.position == y.position && x.orientation == y.orientation;
96 }
97 
99 constexpr bool operator!=( pose const& x, pose const& y )
100 {
101  return !( x == y );
102 }
103 
105 constexpr pose_distance distance_of( pose const& x, pose const& y )
106 {
107  return {
108  distance_of( x.position, y.position ),
110 }
111 
114 constexpr pose lin_interp( pose const& from, pose const& goal, float factor )
115 {
116  return pose{
117  lin_interp( from.position, goal.position, factor ),
118  slerp( from.orientation, goal.orientation, factor ) };
119 }
120 
121 inline std::vector< pose >
122 lineary_interpolate_path( std::vector< pose > const& ipath, float d_step, float a_step )
123 {
124  std::vector< pose > res;
125  if ( ipath.empty() )
126  return res;
127  for ( std::size_t const i : range( ipath.size() - 1 ) ) {
128  pose const& from = ipath[i];
129  pose const& to = ipath[i + 1];
130  std::size_t const seg_steps = steps( distance_of( to, from ), d_step, a_step );
131  for ( std::size_t const j : range( seg_steps ) )
132  res.push_back( lin_interp( from, to, float( j ) / float( seg_steps ) ) );
133  }
134  res.push_back( ipath.back() );
135  return res;
136 }
137 
140 constexpr point< 3 > transform( point< 3 > const& a, pose const& transformation )
141 {
142  return rotate( a, transformation.orientation ) + vector_cast( transformation.position );
143 }
144 
145 constexpr vector< 3 > transform( vector< 3 > const& v, pose const& transformation )
146 {
147  return rotate( v, transformation.orientation ) + vector_cast( transformation.position );
148 }
149 
152 constexpr pose transform( pose const& x, pose const& transformation )
153 {
154  return pose{
155  transform( x.position, transformation ), transformation.orientation * x.orientation };
156 }
157 
158 template < typename T >
159 constexpr auto transform( min_max< T > const& mm, pose const& transformation )
160 {
161  return min_max{ transform( mm.min, transformation ), transform( mm.max, transformation ) };
162 }
163 
164 constexpr pose inverse( pose const& x )
165 {
166  return pose{ -x.position, inverse( x.orientation ) };
167 }
168 
169 template < typename T >
170 constexpr auto inverse_transform( T const& item, pose const& transformation )
171 {
172  return transform( item, inverse( transformation ) );
173 }
174 
176 constexpr pose rotate( pose const& x, quaternion const& quat )
177 {
178  return pose{ rotate( x.position, quat ), quat * x.orientation };
179 }
180 
181 } // namespace emlabcpp
API and behavior of this is inspired by tf::Quaternion.
Definition: quaternion.h:33
Definition: vector.h:33
MIT License.
Definition: impl.h:31
constexpr float angle_shortest_path(quaternion const &m, quaternion const &n)
Definition: quaternion.h:111
constexpr auto inverse_transform(T const &item, pose const &transformation)
Definition: pose.h:170
constexpr quaternion neutral_quat
Definition: quaternion.h:89
constexpr quaternion slerp(quaternion const &q, quaternion const &s, float f)
Definition: quaternion.h:120
constexpr point< 3 > transform(point< 3 > const &a, pose const &transformation)
Point A is rotated based on 'transformation' orientation and than moved based on 'transformation' pos...
Definition: pose.h:140
float dist
Definition: pose.h:37
float angle_dist
Definition: pose.h:38
constexpr pose lin_interp(pose const &from, pose const &goal, float factor)
linear interpolation between base se and goal pose, with factor 0 'base' is returned,...
Definition: pose.h:114
constexpr std::size_t steps(pose_distance dist, float dist_step, float angle_step)
returns steps necessary for linear interpolation of distance between poses 'dis', such that:
Definition: pose.h:46
T res
Definition: algorithm.h:505
constexpr pose inverse(pose const &x)
Definition: pose.h:164
std::vector< point< N > > lineary_interpolate_path(std::vector< point< N > > const &ipath, float d_step)
Definition: point.h:127
constexpr bool operator<(pose const &x, pose const &y)
Definition: pose.h:85
constexpr Derived max(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Definition: vec_point_base.h:229
constexpr vector< N > vector_cast(point< N > const &p)
Definition: point.h:67
constexpr pose rotate(pose const &x, quaternion const &quat)
Pose X is rotated based on quaternion 'quad'.
Definition: pose.h:176
constexpr float distance_of(line< N > const &l, point< N > const &p)
Definition: line.h:41
constexpr view< iterators::numeric_iterator< Numeric > > range(Numeric from, Numeric to)
Builds numeric view over interval [from, to)
Definition: range.h:34
constexpr bool operator!=(pose const &x, pose const &y)
negation of operator== between poses
Definition: pose.h:99
constexpr bool operator==(pose const &x, pose const &y)
compares poses on their position and orientation
Definition: pose.h:93
distance between two poses in space, represented as 'space distance' and 'angular distance'
Definition: pose.h:36
A structure representing a range defined by a minimum and a maximum value.
Definition: min_max.h:37
constexpr T & min()
Definition: min_max.h:40
constexpr T & max()
Definition: min_max.h:50
represents orientation and position in 3D space
Definition: pose.h:55
constexpr pose(quaternion const &orientation)
Definition: pose.h:72
point< 3 > position
Definition: pose.h:57
quaternion orientation
Definition: pose.h:58
constexpr pose(point< 3 > const &position)
Definition: pose.h:66
constexpr pose(point< 3 > const &position, quaternion const &orientation)
Definition: pose.h:78
constexpr pose()
Definition: pose.h:60