Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
OrthoMethods.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_ORTHOMETHODS_H
13#define EIGEN_ORTHOMETHODS_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
22// Vector3 version (default)
23template <typename Derived, typename OtherDerived, int Size>
24struct cross_impl {
25 using Scalar = typename ScalarBinaryOpTraits<typename internal::traits<Derived>::Scalar,
26 typename internal::traits<OtherDerived>::Scalar>::ReturnType;
27 using return_type = Matrix<Scalar, MatrixBase<Derived>::RowsAtCompileTime, MatrixBase<Derived>::ColsAtCompileTime>;
28
29 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE return_type run(const MatrixBase<Derived>& first,
30 const MatrixBase<OtherDerived>& second) {
31 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3)
32 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived, 3)
33
34 // Note that there is no need for an expression here since the compiler
35 // optimizes such a small temporary very well (even within a complex expression)
36 typename internal::nested_eval<Derived, 2>::type lhs(first.derived());
37 typename internal::nested_eval<OtherDerived, 2>::type rhs(second.derived());
38 return return_type(numext::conj(lhs.coeff(1) * rhs.coeff(2) - lhs.coeff(2) * rhs.coeff(1)),
39 numext::conj(lhs.coeff(2) * rhs.coeff(0) - lhs.coeff(0) * rhs.coeff(2)),
40 numext::conj(lhs.coeff(0) * rhs.coeff(1) - lhs.coeff(1) * rhs.coeff(0)));
41 }
42};
43
44// Vector2 version
45template <typename Derived, typename OtherDerived>
46struct cross_impl<Derived, OtherDerived, 2> {
47 using Scalar = typename ScalarBinaryOpTraits<typename internal::traits<Derived>::Scalar,
48 typename internal::traits<OtherDerived>::Scalar>::ReturnType;
49 using return_type = Scalar;
50
51 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE return_type run(const MatrixBase<Derived>& first,
52 const MatrixBase<OtherDerived>& second) {
53 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 2)
54 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived, 2)
55 typename internal::nested_eval<Derived, 2>::type lhs(first.derived());
56 typename internal::nested_eval<OtherDerived, 2>::type rhs(second.derived());
57 return numext::conj(lhs.coeff(0) * rhs.coeff(1) - lhs.coeff(1) * rhs.coeff(0));
58 }
59};
60
61} // end namespace internal
62
89template <typename Derived>
90template <typename OtherDerived>
91EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename internal::cross_impl<Derived, OtherDerived>::return_type
92MatrixBase<Derived>::cross(const MatrixBase<OtherDerived>& other) const {
93 return internal::cross_impl<Derived, OtherDerived>::run(*this, other);
94}
95
96namespace internal {
97
98template <int Arch, typename VectorLhs, typename VectorRhs, typename Scalar = typename VectorLhs::Scalar,
99 bool Vectorizable =
100 bool((int(evaluator<VectorLhs>::Flags) & int(evaluator<VectorRhs>::Flags)) & PacketAccessBit)>
101struct cross3_impl {
102 EIGEN_DEVICE_FUNC static inline typename internal::plain_matrix_type<VectorLhs>::type run(const VectorLhs& lhs,
103 const VectorRhs& rhs) {
104 return typename internal::plain_matrix_type<VectorLhs>::type(
105 numext::conj(lhs.coeff(1) * rhs.coeff(2) - lhs.coeff(2) * rhs.coeff(1)),
106 numext::conj(lhs.coeff(2) * rhs.coeff(0) - lhs.coeff(0) * rhs.coeff(2)),
107 numext::conj(lhs.coeff(0) * rhs.coeff(1) - lhs.coeff(1) * rhs.coeff(0)), 0);
108 }
109};
110
111} // namespace internal
112
122template <typename Derived>
123template <typename OtherDerived>
124EIGEN_DEVICE_FUNC inline typename MatrixBase<Derived>::PlainObject MatrixBase<Derived>::cross3(
125 const MatrixBase<OtherDerived>& other) const {
126 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 4)
127 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived, 4)
128
129 using DerivedNested = typename internal::nested_eval<Derived, 2>::type;
130 using OtherDerivedNested = typename internal::nested_eval<OtherDerived, 2>::type;
131 DerivedNested lhs(derived());
132 OtherDerivedNested rhs(other.derived());
133
134 return internal::cross3_impl<Architecture::Target, internal::remove_all_t<DerivedNested>,
135 internal::remove_all_t<OtherDerivedNested>>::run(lhs, rhs);
136}
137
147template <typename ExpressionType, int Direction>
148template <typename OtherDerived>
149EIGEN_DEVICE_FUNC const typename VectorwiseOp<ExpressionType, Direction>::CrossReturnType
151 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived, 3)
152 EIGEN_STATIC_ASSERT(
153 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
154 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
155
156 typename internal::nested_eval<ExpressionType, 2>::type mat(_expression());
157 typename internal::nested_eval<OtherDerived, 2>::type vec(other.derived());
158
159 CrossReturnType res(_expression().rows(), _expression().cols());
160 EIGEN_IF_CONSTEXPR (Direction == Vertical) {
161 eigen_assert(mat.rows() == 3 && "the matrix must have exactly 3 rows");
162 res.row(0) = (mat.row(1) * vec.coeff(2) - mat.row(2) * vec.coeff(1)).conjugate();
163 res.row(1) = (mat.row(2) * vec.coeff(0) - mat.row(0) * vec.coeff(2)).conjugate();
164 res.row(2) = (mat.row(0) * vec.coeff(1) - mat.row(1) * vec.coeff(0)).conjugate();
165 } else {
166 eigen_assert(mat.cols() == 3 && "the matrix must have exactly 3 columns");
167 res.col(0) = (mat.col(1) * vec.coeff(2) - mat.col(2) * vec.coeff(1)).conjugate();
168 res.col(1) = (mat.col(2) * vec.coeff(0) - mat.col(0) * vec.coeff(2)).conjugate();
169 res.col(2) = (mat.col(0) * vec.coeff(1) - mat.col(1) * vec.coeff(0)).conjugate();
170 }
171 return res;
172}
173
174namespace internal {
175
176template <typename Derived, int Size = Derived::SizeAtCompileTime>
177struct unitOrthogonal_selector {
178 using VectorType = typename plain_matrix_type<Derived>::type;
179 using Scalar = typename traits<Derived>::Scalar;
181 EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) {
182 VectorType perp = VectorType::Zero(src.size());
183 Index maxi = 0;
184 Index sndi = 0;
185 src.cwiseAbs().maxCoeff(&maxi);
186 if (maxi == 0) sndi = 1;
187 const Vector2 unit = Vector2(-numext::conj(src.coeff(sndi)), numext::conj(src.coeff(maxi))).stableNormalized();
188 perp.coeffRef(maxi) = unit.coeff(0);
189 perp.coeffRef(sndi) = unit.coeff(1);
190
191 return perp;
192 }
193};
194
195template <typename Derived>
196struct unitOrthogonal_selector<Derived, 3> {
197 using VectorType = typename plain_matrix_type<Derived>::type;
198 using Scalar = typename traits<Derived>::Scalar;
199 using Vector2 = Matrix<Scalar, 2, 1>;
200 EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) {
201 VectorType perp;
202 /* Let us compute the crossed product of *this with a vector
203 * that is not too close to being collinear to *this.
204 */
205
206 /* unless the x and y coords are both close to zero, we can
207 * simply take ( -y, x, 0 ) and normalize it.
208 */
209 if ((!isMuchSmallerThan(src.x(), src.z())) || (!isMuchSmallerThan(src.y(), src.z()))) {
210 const Vector2 unit = Vector2(-numext::conj(src.y()), numext::conj(src.x())).stableNormalized();
211 perp.coeffRef(0) = unit.coeff(0);
212 perp.coeffRef(1) = unit.coeff(1);
213 perp.coeffRef(2) = Scalar(0);
214 }
215 /* if both x and y are close to zero, then the vector is close
216 * to the z-axis, so it's far from collinear to the x-axis for instance.
217 * So we take the crossed product with (1,0,0) and normalize it.
218 */
219 else {
220 const Vector2 unit = Vector2(-numext::conj(src.z()), numext::conj(src.y())).stableNormalized();
221 perp.coeffRef(0) = Scalar(0);
222 perp.coeffRef(1) = unit.coeff(0);
223 perp.coeffRef(2) = unit.coeff(1);
224 }
225
226 return perp;
227 }
228};
229
230template <typename Derived>
231struct unitOrthogonal_selector<Derived, 2> {
232 using VectorType = typename plain_matrix_type<Derived>::type;
233 EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) {
234 return VectorType(-numext::conj(src.y()), numext::conj(src.x())).stableNormalized();
235 }
236};
237
238} // end namespace internal
239
251template <typename Derived>
252EIGEN_DEVICE_FUNC typename MatrixBase<Derived>::PlainObject MatrixBase<Derived>::unitOrthogonal() const {
253 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
254 return internal::unitOrthogonal_selector<Derived>::run(derived());
255}
256
257} // end namespace Eigen
258
259#endif // EIGEN_ORTHOMETHODS_H
@ ColsAtCompileTime
Definition DenseBase.h:103
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
constexpr Scalar & coeffRef(Index rowId, Index colId)
Definition PlainObjectBase.h:205
constexpr const Scalar & coeff(Index rowId, Index colId) const
Definition PlainObjectBase.h:187
const CrossReturnType cross(const MatrixBase< OtherDerived > &other) const
Definition OrthoMethods.h:150
PlainObject unitOrthogonal(void) const
Definition OrthoMethods.h:252
PlainObject cross3(const MatrixBase< OtherDerived > &other) const
Definition OrthoMethods.h:124
internal::cross_impl< Derived, OtherDerived >::return_type cross(const MatrixBase< OtherDerived > &other) const
Definition OrthoMethods.h:92
@ Vertical
Definition Constants.h:267
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
Matrix< Type, 2, 1 > Vector2
2×1 vector of type Type.
Definition Matrix.h:521