emlabcpp
modern opinionated embedded C++ library
vec_point_base.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../../algorithm.h"
27 #include "../../physical_quantity.h"
28 #include "../../range.h"
29 
30 namespace emlabcpp
31 {
32 
33 template < typename Derived, std::size_t N >
35 {
36 public:
37  using container = std::array< float, N >;
38 
39 private:
40  container data_ = { 0 };
41 
42  [[nodiscard]] Derived& impl()
43  {
44  return static_cast< Derived& >( *this );
45  }
46 
47  [[nodiscard]] Derived const& impl() const
48  {
49  return static_cast< Derived const& >( *this );
50  }
51 
52 public:
53  static constexpr std::size_t dimensions = N;
54  using value_type = float;
55  using const_iterator = typename container::const_iterator;
56  using iterator = typename container::iterator;
57 
58  static Derived make_filled_with( value_type val )
59  {
60  std::array< float, N > res;
61  for ( std::size_t const i : range( N ) )
62  res[i] = val;
63  return Derived{ res };
64  }
65 
66  constexpr vec_point_base() noexcept = default;
67 
68  explicit constexpr vec_point_base( container cont ) noexcept
69  : data_( std::move( cont ) )
70  {
71  }
72 
73  template < typename... T >
74  explicit constexpr vec_point_base( T... t )
75  : data_( { float( t )... } )
76  {
77  static_assert( sizeof...( T ) == N, "Number of parameters has to be N" );
78  }
79 
80  [[nodiscard]] constexpr const_iterator begin() const
81  {
82  return data_.begin();
83  }
84 
85  [[nodiscard]] constexpr const_iterator end() const
86  {
87  return data_.end();
88  }
89 
90  [[nodiscard]] constexpr iterator begin()
91  {
92  return data_.begin();
93  }
94 
95  [[nodiscard]] constexpr iterator end()
96  {
97  return data_.end();
98  }
99 
100  [[nodiscard]] constexpr float operator[]( std::size_t i ) const
101  {
102  return data_[i];
103  }
104 
105  [[nodiscard]] constexpr float& operator[]( std::size_t i )
106  {
107  return data_[i];
108  }
109 
110  [[nodiscard]] constexpr Derived operator-() const
111  {
112  return { impl() * -1.f };
113  }
114 
115  [[nodiscard]] constexpr std::size_t size() const
116  {
117  return N;
118  }
119 
120  [[nodiscard]] constexpr container const& operator*() const
121  {
122  return data_;
123  }
124 
125  friend constexpr auto operator<=>( vec_point_base const&, vec_point_base const& ) = default;
126 };
127 
128 namespace detail
129 {
130  template < typename Derived, std::size_t N >
132  {
133  return true;
134  }
135 } // namespace detail
136 
137 template < typename T >
139 
142 template <
143  typename Derived,
144  std::size_t N,
145  typename T,
146  typename = typename std::enable_if_t< std::is_arithmetic_v< T > > >
147 constexpr Derived operator*( vec_point_base< Derived, N > const& a, T s )
148 {
149  Derived res{ *a };
150  for ( std::size_t const i : range( N ) )
151  res[i] *= s;
152  return res;
153 }
154 
157 template < typename Derived, std::size_t N, typename T >
158 constexpr Derived operator*( T s, vec_point_base< Derived, N > const& a )
159 {
160  return a * s;
161 }
162 
165 template <
166  typename Derived,
167  std::size_t N,
168  typename T,
169  typename = typename std::enable_if_t< std::is_arithmetic_v< T > > >
170 constexpr Derived operator/( vec_point_base< Derived, N > const& a, T s )
171 {
172  Derived res{ *a };
173  for ( std::size_t const i : range( N ) )
174  res[i] /= float( s );
175  return res;
176 }
177 
180 template < typename Derived, std::size_t N >
181 constexpr float dot( vec_point_base< Derived, N > const& a, vec_point_base< Derived, N > const& b )
182 {
183  return sum( range( N ), [&]( std::size_t i ) {
184  return a[i] * b[i];
185  } );
186 }
187 
191 template < typename Derived, std::size_t N >
192 constexpr auto length2_of( vec_point_base< Derived, N > const& a )
193 {
194  auto res = sum( range( N ), [a]( std::size_t i ) {
195  return std::pow( a[i], 2 );
196  } );
197  return float( res );
198 }
199 
202 template < typename Derived, std::size_t N >
203 constexpr float length_of( vec_point_base< Derived, N > const& a )
204 {
205  return std::sqrt( length2_of( a ) );
206 }
207 
210 template < typename Derived, std::size_t N >
211 constexpr Derived normalized( vec_point_base< Derived, N > const& a )
212 {
213  return a / length_of( a );
214 }
215 
218 template < typename Derived, std::size_t N >
219 constexpr Derived abs( vec_point_base< Derived, N > const& a )
220 {
221  Derived res;
222  for ( std::size_t const i : range( N ) )
223  res[i] = std::abs( a[i] );
224  return res;
225 }
226 
227 template < typename Derived, std::size_t N >
228 constexpr Derived
230 {
231  return Derived{ a > b ? *a : *b };
232 }
233 
234 template < typename Derived, std::size_t N >
235 constexpr Derived const&
237 {
238  return Derived{ a < b ? *a : *b };
239 }
240 
243 template < typename Derived, std::size_t N >
244 constexpr Derived
246 {
247  Derived res;
248  for ( std::size_t const i : range( N ) )
249  res[i] = std::max( a[i], b[i] );
250  return res;
251 }
252 
255 template < typename Derived, std::size_t N >
256 constexpr Derived
258 {
259  Derived res;
260  for ( std::size_t const i : range( N ) )
261  res[i] = std::min( a[i], b[i] );
262  return res;
263 }
264 
265 template <
266  typename Container,
267  typename UnaryFunction = std::identity,
268  typename Derived = std::decay_t< mapped_t< Container, UnaryFunction > > >
269 constexpr min_max< Derived >
270 dimensional_min_max_elem( Container const& cont, UnaryFunction&& f = std::identity{} )
271 {
272  min_max< Derived > bound_cube;
273  bound_cube.min = std::numeric_limits< Derived >::max();
274  bound_cube.max = std::numeric_limits< Derived >::lowest();
275  for ( auto const& item : cont ) {
276  auto const& p = f( item );
277  bound_cube.min = dimensional_min( bound_cube.min, p );
278  bound_cube.max = dimensional_max( bound_cube.max, p );
279  }
280  return bound_cube;
281 }
282 
283 template < typename Derived, std::size_t N >
284 constexpr Derived lin_interp(
285  vec_point_base< Derived, N > const& from,
286  vec_point_base< Derived, N > const& goal,
287  float factor )
288 {
289  Derived res;
290  for ( std::size_t const i : range( N ) )
291  res[i] = from[i] + ( goal[i] - from[i] ) * factor;
292  return res;
293 }
294 
295 } // namespace emlabcpp
Definition: vec_point_base.h:35
constexpr const_iterator begin() const
Definition: vec_point_base.h:80
constexpr vec_point_base() noexcept=default
static Derived make_filled_with(value_type val)
Definition: vec_point_base.h:58
constexpr iterator end()
Definition: vec_point_base.h:95
constexpr float & operator[](std::size_t i)
Definition: vec_point_base.h:105
static constexpr std::size_t dimensions
Definition: vec_point_base.h:53
float value_type
Definition: vec_point_base.h:54
typename container::iterator iterator
Definition: vec_point_base.h:56
constexpr Derived operator-() const
Definition: vec_point_base.h:110
constexpr const_iterator end() const
Definition: vec_point_base.h:85
constexpr friend auto operator<=>(vec_point_base const &, vec_point_base const &)=default
constexpr float operator[](std::size_t i) const
Definition: vec_point_base.h:100
constexpr container const & operator*() const
Definition: vec_point_base.h:120
constexpr iterator begin()
Definition: vec_point_base.h:90
std::array< float, N > container
Definition: vec_point_base.h:37
typename container::const_iterator const_iterator
Definition: vec_point_base.h:55
constexpr vec_point_base(T... t)
Definition: vec_point_base.h:74
constexpr std::size_t size() const
Definition: vec_point_base.h:115
constexpr bool vec_point_derived_test(vec_point_base< Derived, N > const &)
Definition: vec_point_base.h:131
MIT License.
Definition: impl.h:31
concept vec_point_derived
Definition: vec_point_base.h:138
constexpr float dot(quaternion const &q, quaternion const &s)
Definition: quaternion.h:101
constexpr Derived dimensional_max(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Calculates a C, where C[i] = max(A[i], B[i]) holds for 0 <= i < N.
Definition: vec_point_base.h:245
constexpr min_max< Derived > dimensional_min_max_elem(Container const &cont, UnaryFunction &&f=std::identity{})
Definition: vec_point_base.h:270
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 length2_of(vec_point_base< Derived, N > const &a)
Returns squared distance of A from [0,0,0], this is a squared length of vector represented by A.
Definition: vec_point_base.h:192
constexpr Derived dimensional_min(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Calculates a C, where C[i] = min(A[i], B[i]) holds for 0 <= i < N.
Definition: vec_point_base.h:257
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 length_of(vec_point_base< Derived, N > const &a)
Returns distance of A from [0,0,0], this is a length of vector represented by A.
Definition: vec_point_base.h:203
constexpr Derived abs(vec_point_base< Derived, N > const &a)
Creates absolute version of A - removing signs on all dimensions.
Definition: vec_point_base.h:219
constexpr Derived normalized(vec_point_base< Derived, N > const &a)
Calculates normalized version of A, this means that length(A) equals to 1.
Definition: vec_point_base.h:211
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
requires(!range_container< Container >) const expr std
Returns index of an element in tuple 't', for which call to predicate f(x) holds true,...
Definition: algorithm.h:127
constexpr Derived max(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Definition: vec_point_base.h:229
constexpr Derived const & min(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Definition: vec_point_base.h:236
constexpr view< iterators::numeric_iterator< Numeric > > range(Numeric from, Numeric to)
Builds numeric view over interval [from, to)
Definition: range.h:34
constexpr Derived operator/(vec_point_base< Derived, N > const &a, T s)
Divides each coordinate of A by item 's' of type T, if T satifies std::is_arithmetic.
Definition: vec_point_base.h:170
UnaryCallable && f
Definition: algorithm.h:161
N
Definition: static_storage.h:97