Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
AlignedVector3
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_ALIGNED_VECTOR3_MODULE_H
12#define EIGEN_ALIGNED_VECTOR3_MODULE_H
13
14#include "../../Eigen/Geometry"
15
16#include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
17
18namespace Eigen {
19
28
39// TODO: Specialize Cwise.
40template <typename Scalar_>
41class AlignedVector3;
42
43namespace internal {
44template <typename Scalar_>
45struct traits<AlignedVector3<Scalar_> > : traits<Matrix<Scalar_, 3, 1, 0, 4, 1> > {};
46} // namespace internal
47
48template <typename Scalar_>
49class AlignedVector3 : public MatrixBase<AlignedVector3<Scalar_> > {
50 typedef Matrix<Scalar_, 4, 1> CoeffType;
51 CoeffType m_coeffs;
52
53 public:
55 EIGEN_DENSE_PUBLIC_INTERFACE(AlignedVector3)
56 using Base::operator*;
57
58 inline Index rows() const { return 3; }
59 inline Index cols() const { return 1; }
60
61 Scalar* data() { return m_coeffs.data(); }
62 const Scalar* data() const { return m_coeffs.data(); }
63 Index innerStride() const { return 1; }
64 Index outerStride() const { return 3; }
65
66 inline const Scalar& coeff(Index row, Index col) const { return m_coeffs.coeff(row, col); }
67
68 inline Scalar& coeffRef(Index row, Index col) { return m_coeffs.coeffRef(row, col); }
69
70 inline const Scalar& coeff(Index index) const { return m_coeffs.coeff(index); }
71
72 inline Scalar& coeffRef(Index index) { return m_coeffs.coeffRef(index); }
73
74 inline AlignedVector3() {}
75
76 inline AlignedVector3(const Scalar& x, const Scalar& y, const Scalar& z) : m_coeffs(x, y, z, Scalar(0)) {}
77
78 inline AlignedVector3(const AlignedVector3& other) : Base(), m_coeffs(other.m_coeffs) {}
79
80 template <typename XprType, int Size = XprType::SizeAtCompileTime>
81 struct generic_assign_selector {};
82
83 template <typename XprType>
84 struct generic_assign_selector<XprType, 4> {
85 inline static void run(AlignedVector3& dest, const XprType& src) { dest.m_coeffs = src; }
86 };
87
88 template <typename XprType>
89 struct generic_assign_selector<XprType, 3> {
90 inline static void run(AlignedVector3& dest, const XprType& src) {
91 dest.m_coeffs.template head<3>() = src;
92 dest.m_coeffs.w() = Scalar(0);
93 }
94 };
95
96 template <typename Derived>
97 inline AlignedVector3(const MatrixBase<Derived>& other) {
98 generic_assign_selector<Derived>::run(*this, other.derived());
99 }
100
101 inline AlignedVector3& operator=(const AlignedVector3& other) {
102 m_coeffs = other.m_coeffs;
103 return *this;
104 }
105
106 template <typename Derived>
107 inline AlignedVector3& operator=(const MatrixBase<Derived>& other) {
108 generic_assign_selector<Derived>::run(*this, other.derived());
109 return *this;
110 }
111
112 inline AlignedVector3 operator+(const AlignedVector3& other) const {
113 return AlignedVector3(m_coeffs + other.m_coeffs);
114 }
115
116 inline AlignedVector3& operator+=(const AlignedVector3& other) {
117 m_coeffs += other.m_coeffs;
118 return *this;
119 }
120
121 inline AlignedVector3 operator-(const AlignedVector3& other) const {
122 return AlignedVector3(m_coeffs - other.m_coeffs);
123 }
124
125 inline AlignedVector3 operator-() const { return AlignedVector3(-m_coeffs); }
126
127 inline AlignedVector3& operator-=(const AlignedVector3& other) {
128 m_coeffs -= other.m_coeffs;
129 return *this;
130 }
131
132 inline AlignedVector3 operator*(const Scalar& s) const { return AlignedVector3(m_coeffs * s); }
133
134 inline friend AlignedVector3 operator*(const Scalar& s, const AlignedVector3& vec) {
135 return AlignedVector3(s * vec.m_coeffs);
136 }
137
138 inline AlignedVector3& operator*=(const Scalar& s) {
139 m_coeffs *= s;
140 return *this;
141 }
142
143 inline AlignedVector3 operator/(const Scalar& s) const { return AlignedVector3(m_coeffs / s); }
144
145 inline AlignedVector3& operator/=(const Scalar& s) {
146 m_coeffs /= s;
147 return *this;
148 }
149
150 inline Scalar dot(const AlignedVector3& other) const {
151 eigen_assert(m_coeffs.w() == Scalar(0));
152 eigen_assert(other.m_coeffs.w() == Scalar(0));
153 return m_coeffs.dot(other.m_coeffs);
154 }
155
156 inline void normalize() { m_coeffs /= norm(); }
157
158 inline AlignedVector3 normalized() const { return AlignedVector3(m_coeffs / norm()); }
159
160 inline Scalar sum() const {
161 eigen_assert(m_coeffs.w() == Scalar(0));
162 return m_coeffs.sum();
163 }
164
165 inline Scalar squaredNorm() const {
166 eigen_assert(m_coeffs.w() == Scalar(0));
167 return m_coeffs.squaredNorm();
168 }
169
170 inline Scalar norm() const {
171 using std::sqrt;
172 return sqrt(squaredNorm());
173 }
174
175 inline AlignedVector3 cross(const AlignedVector3& other) const {
176 return AlignedVector3(m_coeffs.cross3(other.m_coeffs));
177 }
178
179 template <typename Derived>
180 inline bool isApprox(const MatrixBase<Derived>& other,
181 const RealScalar& eps = NumTraits<Scalar>::dummy_precision()) const {
182 return m_coeffs.template head<3>().isApprox(other, eps);
183 }
184
185 CoeffType& coeffs() { return m_coeffs; }
186 const CoeffType& coeffs() const { return m_coeffs; }
187};
188
189namespace internal {
190
191template <typename Scalar_>
192struct eval<AlignedVector3<Scalar_>, Dense> {
193 typedef const AlignedVector3<Scalar_>& type;
194};
195
196template <typename Scalar>
197struct evaluator<AlignedVector3<Scalar> > : evaluator<Matrix<Scalar, 4, 1> > {
198 typedef AlignedVector3<Scalar> XprType;
199 typedef evaluator<Matrix<Scalar, 4, 1> > Base;
200
201 evaluator(const XprType& m) : Base(m.coeffs()) {}
202};
203
204} // namespace internal
205
207
208} // namespace Eigen
209
210#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
211
212#endif // EIGEN_ALIGNED_VECTOR3_MODULE_H
A vectorization friendly 3D vector.
Definition AlignedVector3:49
constexpr ColXpr col(Index i)
constexpr RowXpr row(Index i)
constexpr FixedSegmentReturnType< N >::Type head(Index n=N)
typename internal::traits< Derived >::Scalar Scalar
const MatrixSquareRootReturnValue< Derived > sqrt() const
Namespace containing all symbols from the Eigen library.