aligator  0.16.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Loading...
Searching...
No Matches
constraint.hpp
Go to the documentation of this file.
1
5#pragma once
6
8
10
11namespace aligator {
12
14template <typename Scalar> struct ConstraintStackTpl {
16 using PolyFunc = xyz::polymorphic<StageFunctionTpl<Scalar>>;
17 using PolySet = xyz::polymorphic<ConstraintSetTpl<Scalar>>;
18
25
26 std::size_t size() const { return funcs.size(); }
27 bool empty() const { return size() == 0; }
28 void clear();
29
30 template <typename Cstr> ALIGATOR_DEPRECATED void pushBack(Cstr &&el) {
31 assert(!el.func.valueless_after_move() &&
32 "constraint must have non-null underlying function.");
33 assert(!el.set.valueless_after_move() &&
34 "constraint must have non-null underlying set.");
35 funcs.emplace_back(el.func);
36 sets.emplace_back(el.set);
37 addDim(el.func->nr);
38 }
39
40 void pushBack(const PolyFunc &func, const PolySet &cstr_set) {
41 assert(!func.valueless_after_move() &&
42 "constraint must have non-null underlying function.");
43 assert(!cstr_set.valueless_after_move() &&
44 "constraint must have non-null underlying set.");
45 funcs.emplace_back(func);
46 sets.emplace_back(cstr_set);
47 addDim(func->nr);
48 }
49
51 const std::vector<long> &dims() const { return dims_; }
52
53 long totalDim() const { return total_dim_; }
54
56 template <typename Derived> Derived *getConstraint(const size_t id) {
57 return dynamic_cast<Derived *>(&*funcs[id]);
58 }
59
61 template <typename Derived>
62 const Derived *getConstraint(const size_t id) const {
63 return dynamic_cast<const Derived *>(&*funcs[id]);
64 }
65
66 std::vector<PolyFunc> funcs;
67 std::vector<PolySet> sets;
68
69protected:
70 std::vector<long> indices_;
71 std::vector<long> dims_;
72 long total_dim_ = 0;
73
74private:
75 void addDim(const long nr) {
76 const long last_cursor = indices_.back();
77 indices_.push_back(last_cursor + nr);
78 dims_.push_back(nr);
79 total_dim_ += nr;
80 }
81};
82
83template <typename Scalar> void ConstraintStackTpl<Scalar>::clear() {
84 funcs.clear();
85 sets.clear();
86 indices_ = {0};
87 dims_.clear();
88 total_dim_ = 0;
89}
90
91#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
92extern template struct ConstraintStackTpl<context::Scalar>;
93#endif
94} // namespace aligator
Base definitions for ternary functions.
Main package namespace.
Convenience class to manage a stack of constraints.
const std::vector< long > & dims() const
Get the set of dimensions for each constraint in the stack.
xyz::polymorphic< StageFunctionTpl< Scalar > > PolyFunc
ConstraintStackTpl & operator=(const ConstraintStackTpl &)=default
ConstraintStackTpl & operator=(ConstraintStackTpl &&)=default
std::vector< PolyFunc > funcs
std::size_t size() const
std::vector< PolySet > sets
std::vector< long > indices_
ALIGATOR_DEPRECATED void pushBack(Cstr &&el)
void pushBack(const PolyFunc &func, const PolySet &cstr_set)
ConstraintStackTpl(ConstraintStackTpl &&)=default
const Derived * getConstraint(const size_t id) const
Get constraint function, cast down to the specified type.
std::vector< long > dims_
ConstraintStackTpl(const ConstraintStackTpl &)=default
Derived * getConstraint(const size_t id)
Get constraint function, cast down to the specified type.
xyz::polymorphic< ConstraintSetTpl< Scalar > > PolySet