1// This file is part of Eigen, a lightweight C++ template library
4// Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr>
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/.
10#ifndef EIGEN_ALIGNED_VECTOR3_MODULE_H
11#define EIGEN_ALIGNED_VECTOR3_MODULE_H
13#include "../../Eigen/Geometry"
15#include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
20 * \defgroup AlignedVector3_Module Aligned vector3 module
23 * #include <unsupported/Eigen/AlignedVector3>
28/** \class AlignedVector3
30 * \brief A vectorization friendly 3D vector
32 * This class represents a 3D vector internally using a 4D vector
33 * such that vectorization can be seamlessly enabled. Of course,
34 * the same result can be achieved by directly using a 4D vector.
35 * This class makes this process simpler.
38// TODO specialize Cwise
39template <typename Scalar_>
43template <typename Scalar_>
44struct traits<AlignedVector3<Scalar_> > : traits<Matrix<Scalar_, 3, 1, 0, 4, 1> > {};
45} // namespace internal
47template <typename Scalar_>
48class AlignedVector3 : public MatrixBase<AlignedVector3<Scalar_> > {
49 typedef Matrix<Scalar_, 4, 1> CoeffType;
53 typedef MatrixBase<AlignedVector3<Scalar_> > Base;
54 EIGEN_DENSE_PUBLIC_INTERFACE(AlignedVector3)
55 using Base::operator*;
57 inline Index rows() const { return 3; }
58 inline Index cols() const { return 1; }
60 Scalar* data() { return m_coeffs.data(); }
61 const Scalar* data() const { return m_coeffs.data(); }
62 Index innerStride() const { return 1; }
63 Index outerStride() const { return 3; }
65 inline const Scalar& coeff(Index row, Index col) const { return m_coeffs.coeff(row, col); }
67 inline Scalar& coeffRef(Index row, Index col) { return m_coeffs.coeffRef(row, col); }
69 inline const Scalar& coeff(Index index) const { return m_coeffs.coeff(index); }
71 inline Scalar& coeffRef(Index index) { return m_coeffs.coeffRef(index); }
73 inline AlignedVector3() {}
75 inline AlignedVector3(const Scalar& x, const Scalar& y, const Scalar& z) : m_coeffs(x, y, z, Scalar(0)) {}
77 inline AlignedVector3(const AlignedVector3& other) : Base(), m_coeffs(other.m_coeffs) {}
79 template <typename XprType, int Size = XprType::SizeAtCompileTime>
80 struct generic_assign_selector {};
82 template <typename XprType>
83 struct generic_assign_selector<XprType, 4> {
84 inline static void run(AlignedVector3& dest, const XprType& src) { dest.m_coeffs = src; }
87 template <typename XprType>
88 struct generic_assign_selector<XprType, 3> {
89 inline static void run(AlignedVector3& dest, const XprType& src) {
90 dest.m_coeffs.template head<3>() = src;
91 dest.m_coeffs.w() = Scalar(0);
95 template <typename Derived>
96 inline AlignedVector3(const MatrixBase<Derived>& other) {
97 generic_assign_selector<Derived>::run(*this, other.derived());
100 inline AlignedVector3& operator=(const AlignedVector3& other) {
101 m_coeffs = other.m_coeffs;
105 template <typename Derived>
106 inline AlignedVector3& operator=(const MatrixBase<Derived>& other) {
107 generic_assign_selector<Derived>::run(*this, other.derived());
111 inline AlignedVector3 operator+(const AlignedVector3& other) const {
112 return AlignedVector3(m_coeffs + other.m_coeffs);
115 inline AlignedVector3& operator+=(const AlignedVector3& other) {
116 m_coeffs += other.m_coeffs;
120 inline AlignedVector3 operator-(const AlignedVector3& other) const {
121 return AlignedVector3(m_coeffs - other.m_coeffs);
124 inline AlignedVector3 operator-() const { return AlignedVector3(-m_coeffs); }
126 inline AlignedVector3 operator-=(const AlignedVector3& other) {
127 m_coeffs -= other.m_coeffs;
131 inline AlignedVector3 operator*(const Scalar& s) const { return AlignedVector3(m_coeffs * s); }
133 inline friend AlignedVector3 operator*(const Scalar& s, const AlignedVector3& vec) {
134 return AlignedVector3(s * vec.m_coeffs);
137 inline AlignedVector3& operator*=(const Scalar& s) {
142 inline AlignedVector3 operator/(const Scalar& s) const { return AlignedVector3(m_coeffs / s); }
144 inline AlignedVector3& operator/=(const Scalar& s) {
149 inline Scalar dot(const AlignedVector3& other) const {
150 eigen_assert(m_coeffs.w() == Scalar(0));
151 eigen_assert(other.m_coeffs.w() == Scalar(0));
152 return m_coeffs.dot(other.m_coeffs);
155 inline void normalize() { m_coeffs /= norm(); }
157 inline AlignedVector3 normalized() const { return AlignedVector3(m_coeffs / norm()); }
159 inline Scalar sum() const {
160 eigen_assert(m_coeffs.w() == Scalar(0));
161 return m_coeffs.sum();
164 inline Scalar squaredNorm() const {
165 eigen_assert(m_coeffs.w() == Scalar(0));
166 return m_coeffs.squaredNorm();
169 inline Scalar norm() const {
171 return sqrt(squaredNorm());
174 inline AlignedVector3 cross(const AlignedVector3& other) const {
175 return AlignedVector3(m_coeffs.cross3(other.m_coeffs));
178 template <typename Derived>
179 inline bool isApprox(const MatrixBase<Derived>& other,
180 const RealScalar& eps = NumTraits<Scalar>::dummy_precision()) const {
181 return m_coeffs.template head<3>().isApprox(other, eps);
184 CoeffType& coeffs() { return m_coeffs; }
185 const CoeffType& coeffs() const { return m_coeffs; }
190template <typename Scalar_>
191struct eval<AlignedVector3<Scalar_>, Dense> {
192 typedef const AlignedVector3<Scalar_>& type;
195template <typename Scalar>
196struct evaluator<AlignedVector3<Scalar> > : evaluator<Matrix<Scalar, 4, 1> > {
197 typedef AlignedVector3<Scalar> XprType;
198 typedef evaluator<Matrix<Scalar, 4, 1> > Base;
200 evaluator(const XprType& m) : Base(m.coeffs()) {}
203} // namespace internal
209#include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
211#endif // EIGEN_ALIGNED_VECTOR3_MODULE_H