emlabcpp
modern opinionated embedded C++ library
vector.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "./vec_point_base.h"
27 
28 namespace emlabcpp
29 {
30 
31 template < std::size_t N >
32 class vector : public vec_point_base< vector< N >, N >
33 {
34 public:
36 
37  constexpr vector< N >& operator+=( vector< N > const& other )
38  {
39  for ( std::size_t const i : range( N ) )
40  ( *this )[i] += other[i];
41  return *this;
42  }
43 
44  constexpr vector< N >& operator-=( vector< N > const& other )
45  {
46  for ( std::size_t const i : range( N ) )
47  ( *this )[i] -= other[i];
48  return *this;
49  }
50 };
51 
54 constexpr vector< 3 > x_axis{ 1, 0, 0 };
55 constexpr vector< 3 > y_axis{ 0, 1, 0 };
56 constexpr vector< 3 > z_axis{ 0, 0, 1 };
57 
58 template < std::size_t N >
59 constexpr vector< N > operator+( vector< N > lh, vector< N > const& rh )
60 {
61  return lh += rh;
62 }
63 
66 constexpr vector< 3 > cross_product( vector< 3 > const& a, vector< 3 > const& b )
67 {
68  return vector< 3 >{
69  a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] };
70 }
71 
74 constexpr vector< 2 > normal_of( vector< 2 > const& a )
75 {
76  return vector< 2 >{ a[1], -a[0] };
77 }
78 
79 template < std::size_t N >
80 constexpr float vector_angle( vector< N > const& a, vector< N > const& b )
81 {
82  auto s = sqrt( length2_of( a ) * length2_of( b ) );
83  return acosf( float( dot( a, b ) ) / *s );
84 }
85 
86 } // namespace emlabcpp
Definition: vec_point_base.h:35
Definition: vector.h:33
constexpr vector< N > & operator+=(vector< N > const &other)
Definition: vector.h:37
constexpr vector< N > & operator-=(vector< N > const &other)
Definition: vector.h:44
MIT License.
Definition: impl.h:31
constexpr vector< 3 > normal_of(triangle< 3 > const &tri)
Definition: triangle.h:56
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 vector< 3 > x_axis
instances of constants in the code for X/Y/Z axis
Definition: vector.h:54
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 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 vector< 3 > cross_product(vector< 3 > const &a, vector< 3 > const &b)
Calculates cross product between points A and B.
Definition: vector.h:66
constexpr vector< 3 > z_axis
Definition: vector.h:56
constexpr vector< 3 > y_axis
Definition: vector.h:55
constexpr float vector_angle(vector< N > const &a, vector< N > const &b)
Definition: vector.h:80
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