Directory: | ./ |
---|---|
File: | include/hpp/pinocchio/liegroup-element.hh |
Date: | 2025-05-04 12:09:19 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 29 | 30 | 96.7% |
Branches: | 10 | 20 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2017, CNRS | ||
2 | // Authors: Florent Lamiraux | ||
3 | // | ||
4 | |||
5 | // Redistribution and use in source and binary forms, with or without | ||
6 | // modification, are permitted provided that the following conditions are | ||
7 | // met: | ||
8 | // | ||
9 | // 1. Redistributions of source code must retain the above copyright | ||
10 | // notice, this list of conditions and the following disclaimer. | ||
11 | // | ||
12 | // 2. Redistributions in binary form must reproduce the above copyright | ||
13 | // notice, this list of conditions and the following disclaimer in the | ||
14 | // documentation and/or other materials provided with the distribution. | ||
15 | // | ||
16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
20 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | ||
27 | // DAMAGE. | ||
28 | |||
29 | #ifndef HPP_PINOCCHIO_LIEGROUP_ELEMENT_HH | ||
30 | #define HPP_PINOCCHIO_LIEGROUP_ELEMENT_HH | ||
31 | |||
32 | #include <hpp/pinocchio/deprecated.hh> | ||
33 | #include <hpp/pinocchio/liegroup-space.hh> | ||
34 | #include <pinocchio/math/quaternion.hpp> | ||
35 | |||
36 | namespace hpp { | ||
37 | namespace pinocchio { | ||
38 | /// \addtogroup liegroup | ||
39 | /// \{ | ||
40 | |||
41 | /// Const reference to a \ref LiegroupElement | ||
42 | /// | ||
43 | /// \sa LiegroupSpace, LiegroupElementConstRef | ||
44 | template <typename vector_type> | ||
45 | class LiegroupElementConstBase { | ||
46 | public: | ||
47 | /// Constructor | ||
48 | /// \param value vector representation, | ||
49 | /// \param liegroupSpace space the element belongs to. | ||
50 | template <typename Derived> | ||
51 | 633 | LiegroupElementConstBase(const Eigen::EigenBase<Derived>& value, | |
52 | const LiegroupSpacePtr_t& liegroupSpace) | ||
53 | 633 | : value_(value.derived()), space_(liegroupSpace) { | |
54 | 633 | check(); | |
55 | 633 | } | |
56 | |||
57 | /// Constructor | ||
58 | /// \param value vector representation, | ||
59 | /// | ||
60 | /// By default the space containing the value is a vector space. | ||
61 | template <typename Derived> | ||
62 | explicit LiegroupElementConstBase(const Eigen::EigenBase<Derived>& value) | ||
63 | : value_(value.derived()), | ||
64 | space_(LiegroupSpace::create(value.derived().size())) {} | ||
65 | |||
66 | /// Constructor to allow *casting* LiegroupElement and LiegroupElementRef | ||
67 | /// into a LiegroupElementConstRef | ||
68 | template <typename vector_type2> | ||
69 | 1 | LiegroupElementConstBase(const LiegroupElementConstBase<vector_type2>& other) | |
70 | 1 | : value_(other.vector()), space_(other.space()) {} | |
71 | |||
72 | /// get reference to vector of Lie groups | ||
73 | 4687 | const LiegroupSpacePtr_t& space() const { return space_; } | |
74 | |||
75 | /// Const vector representation | ||
76 | 2652 | const vector_type& vector() const { return value_; } | |
77 | |||
78 | /// Size of the vector representation | ||
79 | ✗ | size_type size() const { return value_.size(); } | |
80 | |||
81 | /// Check that size of vector fits size of space | ||
82 | /// \note only in debug mode | ||
83 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 633 times.
|
633 | void check() const { assert(value_.size() == space_->nq()); } |
84 | |||
85 | /// Equality operator | ||
86 | template <typename vector_type2> | ||
87 | bool operator==(const LiegroupElementConstBase<vector_type2>& other) { | ||
88 | return ((*space_ == *(other.space_)) && (value_ == other.value_)); | ||
89 | } | ||
90 | |||
91 | /// Equality operator | ||
92 | template <typename vector_type2> | ||
93 | bool operator!=(const LiegroupElementConstBase<vector_type2>& other) { | ||
94 | return ((*space_ != *(other.space_)) || (value_ != other.value_)); | ||
95 | } | ||
96 | |||
97 | protected: | ||
98 | template <typename Derived> | ||
99 | 3336 | LiegroupElementConstBase(const Eigen::EigenBase<Derived>& value, | |
100 | const LiegroupSpacePtr_t& space, | ||
101 | void* /*to enable this constructor*/) | ||
102 | 3336 | : value_(const_cast<Derived&>(value.derived())), space_(space) {} | |
103 | |||
104 | vector_type value_; | ||
105 | LiegroupSpacePtr_t space_; | ||
106 | |||
107 | template <typename vector_type2> | ||
108 | friend class LiegroupElementConstBase; | ||
109 | template <typename vector_type2> | ||
110 | friend class LiegroupElementBase; | ||
111 | }; | ||
112 | |||
113 | /// Writable element of a Lie group | ||
114 | /// | ||
115 | /// \sa LiegroupSpace, LiegroupElement, LiegroupElementRef | ||
116 | template <typename vector_type> | ||
117 | class LiegroupElementBase : public LiegroupElementConstBase<vector_type> { | ||
118 | public: | ||
119 | typedef LiegroupElementConstBase<vector_type> Base; | ||
120 | |||
121 | /// Constructor | ||
122 | /// \param value vector representation, | ||
123 | /// \param space space the element belongs to. | ||
124 | 2583 | LiegroupElementBase(const vector_type& value, const LiegroupSpacePtr_t& space) | |
125 | 2583 | : Base(value, space, NULL) {} | |
126 | |||
127 | /// Constructor | ||
128 | /// \param space space the element belongs to. | ||
129 | 14 | LiegroupElementBase(const LiegroupSpacePtr_t& space) | |
130 |
2/4✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
|
14 | : Base(vector_t(space->nq()), space) {} |
131 | |||
132 | /// Constructor | ||
133 | /// \param value vector representation, | ||
134 | /// | ||
135 | /// By default the space containing the value is a vector space. | ||
136 | 201 | explicit LiegroupElementBase(const vector_type& value) | |
137 |
2/4✓ Branch 2 taken 201 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 201 times.
✗ Branch 6 not taken.
|
201 | : Base(value, LiegroupSpace::create(value.size()), NULL) {} |
138 | |||
139 | /// Copy constructor. Handles the following case: | ||
140 | /// \li LiegroupElement <- LiegroupElementConstRef | ||
141 | template <typename vector_type2> | ||
142 | 1204 | LiegroupElementBase(const LiegroupElementConstBase<vector_type2>& other) | |
143 | 1204 | : Base(other.value_, other.space()) {} | |
144 | |||
145 | /// Copy constructor. Handles the following cases: | ||
146 | /// \li LiegroupElement <- LiegroupElement | ||
147 | /// \li LiegroupElementRef <- LiegroupElementRef | ||
148 | /// \li LiegroupElement <- LiegroupElementRef | ||
149 | template <typename vector_type2> | ||
150 | LiegroupElementBase(const LiegroupElementBase<vector_type2>& other) | ||
151 | : Base(other.value_, other.space()) {} | ||
152 | |||
153 | /// *Casting* operator from LiegroupElement to LiegroupElementRef | ||
154 | template <typename vector_type2> | ||
155 | 1 | LiegroupElementBase(LiegroupElementBase<vector_type2>& other) | |
156 | 1 | : Base(other.value_, other.space(), NULL) {} | |
157 | |||
158 | /// Constructor of trivial element | ||
159 |
2/4✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
|
12 | LiegroupElementBase() : Base(vector_t(), LiegroupSpace::empty()) {} |
160 | |||
161 | /// Const vector representation | ||
162 | 416 | const vector_type& vector() const { return Base::vector(); } | |
163 | |||
164 | /// Modifiable vector representation | ||
165 | 3156 | vector_type& vector() { return this->value_; } | |
166 | |||
167 | /// Set element to neutral element | ||
168 |
1/2✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
|
14 | void setNeutral() { this->value_ = this->space_->neutral().vector(); } |
169 | |||
170 | /// Inplace integration of a velocity vector | ||
171 | LiegroupElementBase& operator+=(vectorIn_t v); | ||
172 | |||
173 | /// Assignment from another LiegroupElement | ||
174 | template <typename vector_type2> | ||
175 | LiegroupElementBase& operator=( | ||
176 | const LiegroupElementConstBase<vector_type2>& other) { | ||
177 | this->space_ = other.space(); | ||
178 | this->value_ = other.vector(); | ||
179 | return *this; | ||
180 | } | ||
181 | |||
182 | /// Assignment from a vector | ||
183 | template <typename Vector> | ||
184 | 1 | LiegroupElementBase& operator=(const Eigen::MatrixBase<Vector>& v) { | |
185 | EIGEN_STATIC_ASSERT_VECTOR_ONLY(Vector); | ||
186 |
1/2✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | assert(this->space_->nq() == v.derived().size()); |
187 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | this->value_.noalias() = v.derived(); |
188 | 1 | return *this; | |
189 | } | ||
190 | }; | ||
191 | |||
192 | /// Integration of a velocity vector from a configuration | ||
193 | /// | ||
194 | /// \param e element of the Lie group, | ||
195 | /// \param v element of the tangent space of the Lie group. | ||
196 | /// \return the element of the Lie group resulting from the integration | ||
197 | /// | ||
198 | /// By extension of the vector space case, we represent the integration | ||
199 | /// of a constant velocity during unit time by an addition | ||
200 | template <typename vector_type> | ||
201 | LiegroupElement operator+(const LiegroupElementConstBase<vector_type>& e, | ||
202 | vectorIn_t v); | ||
203 | |||
204 | /// Difference between two configurations | ||
205 | /// | ||
206 | /// \param e1, e2 elements of the Lie group, | ||
207 | /// \return the velocity that integrated from e2 yields e1 | ||
208 | /// | ||
209 | /// By extension of the vector space case, we represent the integration | ||
210 | /// of a constant velocity during unit time by an addition | ||
211 | template <typename vector_type1, typename vector_type2> | ||
212 | vector_t operator-(const LiegroupElementConstBase<vector_type1>& e1, | ||
213 | const LiegroupElementConstBase<vector_type2>& e2); | ||
214 | |||
215 | /// Check if a configuration is normalized | ||
216 | /// | ||
217 | /// \param e1 configuration to be checked | ||
218 | /// \param eps the error threshold | ||
219 | /// \return whether the configuration is normalized | ||
220 | /// | ||
221 | template <typename vector_type> | ||
222 | bool checkNormalized( | ||
223 | const LiegroupElementConstBase<vector_type>& e1, | ||
224 | const value_type& eps = PINOCCHIO_DEFAULT_QUATERNION_NORM_TOLERANCE_VALUE); | ||
225 | /// \} | ||
226 | |||
227 | /// Compute the log as a tangent vector of a Lie group element | ||
228 | template <typename vector_type> | ||
229 | vector_t log(const LiegroupElementConstBase<vector_type>& lge); | ||
230 | |||
231 | template <typename vector_type> | ||
232 | inline std::ostream& operator<<( | ||
233 | std::ostream& os, const LiegroupElementConstBase<vector_type>& e) { | ||
234 | os << "Lie group element in " << *(e.space()) << " represented by vector (" | ||
235 | << e.vector().transpose() << ")"; | ||
236 | return os; | ||
237 | } | ||
238 | } // namespace pinocchio | ||
239 | } // namespace hpp | ||
240 | |||
241 | HPP_SERIALIZABLE_SPLIT_FREE(hpp::pinocchio::LiegroupElement) | ||
242 | |||
243 | #endif // HPP_PINOCCHIO_LIEGROUP_ELEMENT_HH | ||
244 |