Eigen  5.0.1
 
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(CrossReturnType::RowsAtCompileTime == 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(CrossReturnType::ColsAtCompileTime == 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;
180 using RealScalar = typename NumTraits<Scalar>::Real;
182 EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) {
183 VectorType perp = VectorType::Zero(src.size());
184 Index maxi = 0;
185 Index sndi = 0;
186 src.cwiseAbs().maxCoeff(&maxi);
187 if (maxi == 0) sndi = 1;
188 RealScalar invnm = RealScalar(1) / (Vector2() << src.coeff(sndi), src.coeff(maxi)).finished().norm();
189 perp.coeffRef(maxi) = -numext::conj(src.coeff(sndi)) * invnm;
190 perp.coeffRef(sndi) = numext::conj(src.coeff(maxi)) * invnm;
191
192 return perp;
193 }
194};
195
196template <typename Derived>
197struct unitOrthogonal_selector<Derived, 3> {
198 using VectorType = typename plain_matrix_type<Derived>::type;
199 using Scalar = typename traits<Derived>::Scalar;
200 using RealScalar = typename NumTraits<Scalar>::Real;
201 EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) {
202 VectorType perp;
203 /* Let us compute the crossed product of *this with a vector
204 * that is not too close to being collinear to *this.
205 */
206
207 /* unless the x and y coords are both close to zero, we can
208 * simply take ( -y, x, 0 ) and normalize it.
209 */
210 if ((!isMuchSmallerThan(src.x(), src.z())) || (!isMuchSmallerThan(src.y(), src.z()))) {
211 RealScalar invnm = RealScalar(1) / src.template head<2>().norm();
212 perp.coeffRef(0) = -numext::conj(src.y()) * invnm;
213 perp.coeffRef(1) = numext::conj(src.x()) * invnm;
214 perp.coeffRef(2) = 0;
215 }
216 /* if both x and y are close to zero, then the vector is close
217 * to the z-axis, so it's far from collinear to the x-axis for instance.
218 * So we take the crossed product with (1,0,0) and normalize it.
219 */
220 else {
221 RealScalar invnm = RealScalar(1) / src.template tail<2>().norm();
222 perp.coeffRef(0) = 0;
223 perp.coeffRef(1) = -numext::conj(src.z()) * invnm;
224 perp.coeffRef(2) = numext::conj(src.y()) * invnm;
225 }
226
227 return perp;
228 }
229};
230
231template <typename Derived>
232struct unitOrthogonal_selector<Derived, 2> {
233 using VectorType = typename plain_matrix_type<Derived>::type;
234 EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) {
235 return VectorType(-numext::conj(src.y()), numext::conj(src.x())).normalized();
236 }
237};
238
239} // end namespace internal
240
252template <typename Derived>
253EIGEN_DEVICE_FUNC typename MatrixBase<Derived>::PlainObject MatrixBase<Derived>::unitOrthogonal() const {
254 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
255 return internal::unitOrthogonal_selector<Derived>::run(derived());
256}
257
258} // end namespace Eigen
259
260#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
const CrossReturnType cross(const MatrixBase< OtherDerived > &other) const
Definition OrthoMethods.h:150
PlainObject unitOrthogonal(void) const
Definition OrthoMethods.h:253
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