emlabcpp
modern opinionated embedded C++ library
point.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../../range.h"
27 #include "./vec_point_base.h"
28 #include "./vector.h"
29 
30 namespace emlabcpp
31 {
32 
34 template < std::size_t N >
35 class point : public vec_point_base< point< N >, N >
36 {
37 public:
39 
41  constexpr point< N >& operator+=( vector< N > const& other )
42  {
43  for ( std::size_t const i : range( N ) )
44  ( *this )[i] += other[i];
45  return *this;
46  }
47 
50  constexpr point< N >& operator-=( vector< N > const& other )
51  {
52  for ( std::size_t const i : range( N ) )
53  ( *this )[i] -= other[i];
54  return *this;
55  }
56 
57  ~point() = default;
58 };
59 
60 template < std::size_t N >
61 constexpr point< N > point_cast( vector< N > const& v )
62 {
63  return point< N >{ *v };
64 }
65 
66 template < std::size_t N >
67 constexpr vector< N > vector_cast( point< N > const& p )
68 {
69  return vector< N >{ *p };
70 }
71 
73 template < std::size_t N >
74 [[deprecated]] constexpr point< N > operator*( point< N > a, point< N > const& b )
75 {
76  for ( std::size_t const i : range( N ) )
77  a[i] *= b[i];
78  return a;
79 }
80 
83 template < std::size_t N >
84 constexpr vector< N > operator-( point< N > a, point< N > const& b )
85 {
86  a -= vector_cast( b );
87  return vector< N >{ *a };
88 }
89 
92 template < std::size_t N >
93 constexpr point< N > operator+( point< N > a, vector< N > const& b )
94 {
95  a += b;
96  return a;
97 }
98 
101 template < std::size_t N >
102 constexpr point< N > operator-( point< N > a, vector< N > const& b )
103 {
104  a -= b;
105  return a;
106 }
107 
110 template < std::size_t N >
111 constexpr float distance_of( point< N > const& a, point< N > const& b )
112 {
113  auto tmp = sum( range( N ), [&]( std::size_t i ) {
114  return std::pow( a[i] - b[i], 2 );
115  } );
116  return std::sqrt( float( tmp ) );
117 }
118 
119 template < std::size_t N >
120 constexpr float point_angle( point< N > const& a, point< N > const& b )
121 {
122  return vector_angle( vector_cast( a ), vector_cast( b ) );
123 }
124 
125 template < std::size_t N >
126 inline std::vector< point< N > >
127 lineary_interpolate_path( std::vector< point< N > > const& ipath, float d_step )
128 {
129  std::vector< point< N > > res;
130  if ( ipath.empty() )
131  return res;
132  for ( std::size_t i : range( ipath.size() - 1 ) ) {
133  point< N > const& from = ipath[i];
134  point< N > const& to = ipath[i + 1];
135 
136  std::size_t const seg_steps = std::size_t{ distance_of( from, to ) / d_step };
137  for ( std::size_t const j : range( seg_steps ) )
138  res.push_back( lin_interp( from, to, float( j ) / float( seg_steps ) ) );
139  }
140  res.push_back( ipath.back() );
141  return res;
142 }
143 
148 template < std::size_t N >
149 constexpr float axis_projection_distance( point< N > const& a, vector< N > const& axis_direction )
150 {
151  return float( dot( vector_cast( a ), axis_direction ) );
152 }
153 } // namespace emlabcpp
Class implementing multidimensional point in coordinate system of dimension N.
Definition: point.h:36
constexpr point< N > & operator-=(vector< N > const &other)
-= operator subtracts value of 'i'th coordinate of 'other' from 'this', for all 0 <= i < N
Definition: point.h:50
constexpr point< N > & operator+=(vector< N > const &other)
+= operator adds value of 'i'th coordinate from 'other' to 'this', for all 0 <= i < N
Definition: point.h:41
~point()=default
Definition: vec_point_base.h:35
Definition: vector.h:33
std::vector< T, allocator< T > > vector
Definition: aliases.h:54
MIT License.
Definition: impl.h:31
constexpr point< N > point_cast(vector< N > const &v)
Definition: point.h:61
constexpr float dot(quaternion const &q, quaternion const &s)
Definition: quaternion.h:101
constexpr point< N > operator+(point< N > a, vector< N > const &b)
Returns a result of addition a to b, viz += operator.
Definition: point.h:93
constexpr T sum(Container &&cont, UnaryCallable &&f=std::identity(), T init={})
Applies f(x) to each item of container 'cont', returns the sum of all the return values of each call ...
Definition: algorithm.h:250
constexpr auto sqrt(physical_quantity< Len, Mass, Time, Current, Temp, Mol, Li, Angle, Byte > val)
Square root of physical quantity is square root of it's value and the exponents are divided in half.
Definition: physical_quantity.h:215
constexpr point< N > operator*(point< N > a, point< N > const &b)
Multiplication of points multiplies each coordinate of A by coordinate of B on same dimension.
Definition: point.h:74
constexpr float point_angle(point< N > const &a, point< N > const &b)
Definition: point.h:120
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 auto pow(physical_quantity< Len, Mass, Time, Current, Temp, Mol, Li, Angle, Byte > val)
Power of physical quantity is power of root of it's value and the exponents are multiplied by the val...
Definition: physical_quantity.h:247
T res
Definition: algorithm.h:505
std::vector< point< N > > lineary_interpolate_path(std::vector< point< N > > const &ipath, float d_step)
Definition: point.h:127
constexpr float vector_angle(vector< N > const &a, vector< N > const &b)
Definition: vector.h:80
constexpr vector< N > vector_cast(point< N > const &p)
Definition: point.h:67
constexpr vector< N > operator-(point< N > a, point< N > const &b)
Returns a result of subtraction of A from B, viz -= operator.
Definition: point.h:84
constexpr float axis_projection_distance(point< N > const &a, vector< N > const &axis_direction)
Function to calculate distance of projection of point A.
Definition: point.h:149
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
N
Definition: static_storage.h:97