emlabcpp
modern opinionated embedded C++ library
simplex.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../../algorithm.h"
27 #include "../matrix.h"
28 #include "./point.h"
29 
30 #include <algorithm>
31 #include <array>
32 #include <vector>
33 
34 namespace emlabcpp
35 {
36 
37 template < typename Item, std::size_t N >
38 class simplex
39 {
40 public:
41  using container = std::array< Item, N + 1 >;
42 
43 private:
44  container points_;
45 
46 public:
47  using value_type = Item;
48  using iterator = typename container::iterator;
49  using const_iterator = typename container::const_iterator;
50 
51  constexpr explicit simplex() = default;
52 
53  constexpr explicit simplex( Item item )
54  : points_( { item } )
55  {
56  }
57 
58  constexpr simplex( simplex< Item, N - 1 > const& sub, Item item )
59  : points_()
60  {
61  std::copy( sub.begin(), sub.end(), points_.begin() );
62  *points_.rbegin() = item;
63 
64  std::sort( points_.begin(), points_.end() );
65  }
66 
67  constexpr simplex( std::array< Item, N + 1 > data )
68  : points_( std::move( data ) )
69  {
70  std::sort( points_.begin(), points_.end() );
71  }
72 
73  template < typename... Ts >
74  constexpr explicit simplex( Ts... ts )
75  : points_( { ts... } )
76  {
77  static_assert(
78  sizeof...( Ts ) == N + 1, "Number of parameters for simplex has to be N+1" );
79  }
80 
81  [[nodiscard]] constexpr const_iterator begin() const
82  {
83  return points_.begin();
84  }
85 
86  [[nodiscard]] constexpr const_iterator end() const
87  {
88  return points_.end();
89  }
90 
91  [[nodiscard]] constexpr Item const& operator[]( std::size_t index ) const
92  {
93  return points_[index];
94  }
95 
96  [[nodiscard]] constexpr std::size_t size() const
97  {
98  return points_.size();
99  }
100 };
101 
102 template < std::size_t N, std::size_t U >
103 constexpr point< N > center_of( simplex< point< N >, U > const& s )
104 {
105  vector< N > avg = sum( s, [&]( point< N > const& p ) -> vector< N > {
106  return vector_cast( p ) / s.size();
107  } );
108 
109  return point_cast( avg );
110 }
111 
112 template < std::size_t N >
113 constexpr float volume_of( simplex< point< N >, N > const& simplex )
114 {
115  matrix< N, N > m;
116  for ( std::size_t const i : range( simplex.size() - 1 ) ) {
117  auto diff = simplex[i + 1] - simplex[0];
118  std::copy( diff.begin(), diff.end(), &m[i][0] );
119  }
120 
121  return std::abs( ( 1 / std::tgammaf( N + 1 ) ) * determinant( m ) );
122 }
123 
124 template < typename Item, std::size_t N >
125 constexpr bool operator<( simplex< Item, N > const& lh, simplex< Item, N > const& rh )
126 {
127  // there are actually N+1 items in N simplex;
128  std::size_t i = *find_if( range( N ), [&]( std::size_t j ) {
129  return lh[j] != rh[j];
130  } );
131  return lh[i] < rh[i];
132 }
133 
134 template < typename Item, std::size_t N >
135 constexpr bool operator>( simplex< Item, N > const& lh, simplex< Item, N > const& rh )
136 {
137  return rh < lh;
138 }
139 
140 template < typename Item, std::size_t N >
141 constexpr bool operator<=( simplex< Item, N > const& lh, simplex< Item, N > const& rh )
142 {
143  return !( lh > rh );
144 }
145 
146 template < typename Item, std::size_t N >
147 constexpr bool operator>=( simplex< Item, N > const& lh, simplex< Item, N > const& rh )
148 {
149  return !( lh < rh );
150 }
151 
152 template < typename Item, std::size_t N >
153 constexpr bool operator==( simplex< Item, N > const& lh, simplex< Item, N > const& rh )
154 {
155  return equal( lh, rh );
156 }
157 
158 template < typename Item, std::size_t N >
159 constexpr bool operator!=( simplex< Item, N > const& lh, simplex< Item, N > const& rh )
160 {
161  return !( lh == rh );
162 }
163 
164 } // namespace emlabcpp
Definition: matrix.h:34
Class implementing multidimensional point in coordinate system of dimension N.
Definition: point.h:36
Definition: simplex.h:39
constexpr simplex(Item item)
Definition: simplex.h:53
constexpr simplex()=default
std::array< Item, N+1 > container
Definition: simplex.h:41
constexpr const_iterator end() const
Definition: simplex.h:86
constexpr simplex(std::array< Item, N+1 > data)
Definition: simplex.h:67
typename container::const_iterator const_iterator
Definition: simplex.h:49
constexpr std::size_t size() const
Definition: simplex.h:96
constexpr Item const & operator[](std::size_t index) const
Definition: simplex.h:91
constexpr simplex(simplex< Item, N - 1 > const &sub, Item item)
Definition: simplex.h:58
constexpr simplex(Ts... ts)
Definition: simplex.h:74
typename container::iterator iterator
Definition: simplex.h:48
constexpr const_iterator begin() const
Definition: simplex.h:81
Item value_type
Definition: simplex.h:47
Definition: vector.h:33
MIT License.
Definition: impl.h:31
constexpr point< N > point_cast(vector< N > const &v)
Definition: point.h:61
constexpr bool operator>(simplex< Item, N > const &lh, simplex< Item, N > const &rh)
Definition: simplex.h:135
constexpr pointer data() noexcept
Returns pointer to first item of the storage.
Definition: static_storage.h:108
constexpr float volume_of(simplex< point< N >, N > const &simplex)
Definition: simplex.h:113
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 bool equal(LhContainer &&lh, RhContainer &&rh, BinaryPredicateCallable &&f=std::equal_to< void >{})
Returns true if containers 'lh' and 'rh' has same size and calls to predicate f - f(lh[i],...
Definition: algorithm.h:356
constexpr T avg(Container &&cont, UnaryCallable &&f=std::identity())
Applies callable 'f(x)' to each element of container 'cont' and returns the average value of each cal...
Definition: algorithm.h:275
void copy(Container &&cont, Iterator iter)
Definition: algorithm.h:455
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 bool operator<=(simplex< Item, N > const &lh, simplex< Item, N > const &rh)
Definition: simplex.h:141
constexpr bool operator<(pose const &x, pose const &y)
Definition: pose.h:85
constexpr vector< N > vector_cast(point< N > const &p)
Definition: point.h:67
constexpr bool operator>=(simplex< Item, N > const &lh, simplex< Item, N > const &rh)
Definition: simplex.h:147
constexpr point< N > center_of(simplex< point< N >, U > const &s)
Definition: simplex.h:103
constexpr view< iterators::numeric_iterator< Numeric > > range(Numeric from, Numeric to)
Builds numeric view over interval [from, to)
Definition: range.h:34
constexpr auto find_if(Container &&cont, PredicateCallable &&f=std::identity())
Returns iterator for first item, for which call to predicate f(*iter) holds true.
Definition: algorithm.h:112
constexpr bool operator!=(pose const &x, pose const &y)
negation of operator== between poses
Definition: pose.h:99
N
Definition: static_storage.h:97
constexpr bool operator==(pose const &x, pose const &y)
compares poses on their position and orientation
Definition: pose.h:93