Lodestar
An integrated real-time control package in C++
Singleton.hpp
1 //
2 // Created by Hamza El-Kebir on 6/21/21.
3 //
4 
5 #ifndef LODESTAR_SINGLETON_HPP
6 #define LODESTAR_SINGLETON_HPP
7 
8 #include <type_traits>
9 #include <Eigen/Dense>
10 #include "Lodestar/aux/CompileTimeQualifiers.hpp"
11 
12 #include "SetExpression.hpp"
13 #include "SetUnion.hpp"
14 #include "EmptySet.hpp"
15 #include "SetComplement.hpp"
16 
17 namespace ls {
18  namespace primitives {
19  namespace sets {
28  template<int TDimension = -1, typename TScalarType = double>
29  class Singleton : public SetExpression<Singleton<TDimension, TScalarType>> {
30  public:
31  template<typename, typename>
32  friend
33  class SetUnion;
34 
37 
38  typedef Eigen::Matrix<TScalarType, LS_STATIC_UNLESS_DYNAMIC(TDimension), 1> TDValue;
39 
43  Singleton() : value_{TDValue::Zero()}
44  {
45  this->sEnum_ = SetEnum::Singleton;
46  }
47 
55  template<typename TDerived>
56  Singleton(const Eigen::EigenBase<TDerived> &value) : value_(value)
57  {
58  this->sEnum_ = SetEnum::Singleton;
59  }
60 
70  template<int T_TDimension = TDimension>
71  int
72  dimension(typename std::enable_if<(T_TDimension < 0)>::type * = nullptr) const
73  {
74  return T_TDimension;
75  }
76 
86  template<int T_TDimension = TDimension>
87  int
88  dimension(typename std::enable_if<(T_TDimension >= 0)>::type * = nullptr) const
89  {
90  return value_.rows();
91  }
92 
103  template<int T_TDimension = TDimension>
104  void
105  setDimension(size_t dim, typename std::enable_if<(T_TDimension < 0)>::type * = nullptr)
106  {
107  static_assert(TDimension > -1, "Cannot set dimension of statically sized singleton.");
108  }
109 
119  template<int T_TDimension = TDimension>
120  void
121  setDimension(size_t dim, typename std::enable_if<(T_TDimension >= 0)>::type * = nullptr)
122  {
123  value_.conservativeResize(dim);
124  }
125 
138  template<typename TExpression>
139  typename std::enable_if<std::is_same<TExpression, type>::value, bool>::type
140  contains(const SetExpression<TExpression> &expr, double tol = 1e-6) const
141  {
142  return (value_ - static_cast<const TExpression *>(&expr)->value_).isMuchSmallerThan(tol);
143  }
144 
157  template<typename TExpression>
158  typename std::enable_if<!std::is_same<TExpression, type>::value, bool>::type
159  contains(const SetExpression<TExpression> &expr, double tol = 1e-6) const
160  {
161  return false;
162  }
163 
175  template<typename TExpression>
176  typename std::enable_if<std::is_same<TExpression, type>::value, bool>::type
177  operator==(const SetExpression<TExpression> &expr) const
178  {
179  return (value_ - static_cast<const TExpression *>(&expr)->value_).isMuchSmallerThan(1e-6);
180  }
181 
182  // TODO: Add dynamic case.
183 
195  template<typename TExpression>
196  typename std::enable_if<!std::is_same<TExpression, type>::value, bool>::type
197  operator==(const SetExpression<TExpression> &expr) const
198  {
199  return false;
200  }
201 
213  template<typename TExpression>
214  typename std::enable_if<std::is_same<TExpression, type>::value, bool>::type
215  operator!=(const SetExpression<TExpression> &expr) const
216  {
217  return !((value_ - static_cast<const TExpression *>(&expr)->value_).isMuchSmallerThan(1e-6));
218  }
219 
231  template<typename TExpression>
232  typename std::enable_if<!std::is_same<TExpression, type>::value, bool>::type
233  operator!=(const SetExpression<TExpression> &expr) const
234  {
235  return true;
236  }
237 
247  template<typename TExpression>
249  {
250  return SetUnion<type, TExpression>(*this, *static_cast<const TExpression *>(&expr));
251  }
252 
262  template<typename TExpression>
263  bool isSubset(const SetExpression<TExpression> &expr)
264  {
265  return static_cast<const TExpression *>(&expr)->contains(*this);
266  }
267 
277  template<typename TExpression>
278  bool isSuperset(const SetExpression<TExpression> &expr)
279  {
280  return contains(expr);
281  }
282 
290  bool isEmpty() const
291  {
292  return false;
293  }
294 
310  template<typename TExpression>
312  {
313  return SetComplement<type, TExpression>(*this, *static_cast<const TExpression *>(&expr),
314  isSubset(expr));
315  }
316 
326  template<typename TDerived>
327  double sdf(Eigen::MatrixBase<TDerived> &p) const
328  {
329  p.resize(dimension());
330  return (value_ - p).norm();
331  }
332 
333  protected:
334  TDValue value_;
335  };
336  }
337  }
338 }
339 
340 #endif //LODESTAR_SINGLETON_HPP
ls::primitives::sets::Singleton::operator==
std::enable_if< std::is_same< TExpression, type >::value, bool >::type operator==(const SetExpression< TExpression > &expr) const
Checks if this expression is equal to \expr.
Definition: Singleton.hpp:176
ls::primitives::sets::Singleton::operator!=
std::enable_if< std::is_same< TExpression, type >::value, bool >::type operator!=(const SetExpression< TExpression > &expr) const
Checks if this expression is not equal to \expr.
Definition: Singleton.hpp:214
ls::primitives::sets::Singleton::isSubset
bool isSubset(const SetExpression< TExpression > &expr)
Checks if this expression is a subset of expr.
Definition: Singleton.hpp:262
ls::primitives::sets::Singleton::TDValue
Eigen::Matrix< TScalarType, LS_STATIC_UNLESS_DYNAMIC(TDimension), 1 > TDValue
Expression type.
Definition: Singleton.hpp:37
ls::primitives::sets::SetExpression
Definition: SetExpression.hpp:15
ls::primitives::sets::Singleton::sdf
double sdf(Eigen::MatrixBase< TDerived > &p) const
Returns signed distance to p.
Definition: Singleton.hpp:326
ls::primitives::sets::Singleton::setDimension
void setDimension(size_t dim, typename std::enable_if<(T_TDimension< 0)>::type *=nullptr)
Changes the dimension of the singleton.
Definition: Singleton.hpp:104
ls::primitives::sets::SetComplement
Relative complement of two SetExpressions.
Definition: SetComplement.hpp:28
ls::primitives::sets::Singleton::isSuperset
bool isSuperset(const SetExpression< TExpression > &expr)
Checks if this expression is a superset of expr.
Definition: Singleton.hpp:277
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::primitives::sets::Singleton::unionize
SetUnion< type, TExpression > unionize(const SetExpression< TExpression > &expr)
Creates a union between this expression and another SetExpression.
Definition: Singleton.hpp:247
ls::primitives::sets::Singleton::contains
std::enable_if< std::is_same< TExpression, type >::value, bool >::type contains(const SetExpression< TExpression > &expr, double tol=1e-6) const
Returns true if this expression contains expr.
Definition: Singleton.hpp:139
ls::primitives::sets::Singleton::isEmpty
bool isEmpty() const
Returns true if the expression is the empty set.
Definition: Singleton.hpp:289
ls::primitives::sets::Singleton::dimension
int dimension(typename std::enable_if<(T_TDimension< 0)>::type *=nullptr) const
Returns the singleton dimension.
Definition: Singleton.hpp:71
ls::primitives::sets::SetUnion
Union of two SetExpressions.
Definition: SetUnion.hpp:28
ls::primitives::sets::Singleton::Singleton
Singleton()
Value typedef.
Definition: Singleton.hpp:42
ls::primitives::sets::Singleton::relComplement
SetComplement< type, TExpression > relComplement(const SetExpression< TExpression > &expr)
Computes the relative complement of this expression and expr.
Definition: Singleton.hpp:310
ls::primitives::sets::Singleton
A singleton set.
Definition: Singleton.hpp:29