Main MRPT website > C++ reference
MRPT logo
AlignedVector3
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_ALIGNED_VECTOR3
00026 #define EIGEN_ALIGNED_VECTOR3
00027 
00028 #include <Eigen/Geometry>
00029 
00030 namespace Eigen {
00031 
00032 /** \ingroup Unsupported_modules
00033   * \defgroup AlignedVector3_Module Aligned vector3 module  
00034  * \ingroup eigen_grp
00035  * \ingroup eigen_grp
00036   *
00037   * \code
00038   * #include <unsupported/Eigen/AlignedVector3>
00039   * \endcode
00040   */
00041   //@{
00042 
00043 
00044 /** \class AlignedVector3
00045   *
00046   * \brief A vectorization friendly 3D vector
00047   *
00048   * This class represents a 3D vector internally using a 4D vector
00049   * such that vectorization can be seamlessly enabled. Of course,
00050   * the same result can be achieved by directly using a 4D vector.
00051   * This class makes this process simpler.
00052   *
00053   */
00054 // TODO specialize Cwise
00055 template<typename _Scalar> class AlignedVector3;
00056 
00057 namespace internal {
00058 template<typename _Scalar> struct traits<AlignedVector3<_Scalar> >
00059   : traits<Matrix<_Scalar,3,1,0,4,1> >
00060 {
00061 };
00062 }
00063 
00064 template<typename _Scalar> class AlignedVector3
00065   : public MatrixBase<AlignedVector3<_Scalar> >
00066 {
00067     typedef Matrix<_Scalar,4,1> CoeffType;
00068     CoeffType m_coeffs;
00069   public:
00070 
00071     typedef MatrixBase<AlignedVector3<_Scalar> > Base;  
00072     EIGEN_DENSE_PUBLIC_INTERFACE(AlignedVector3)
00073     using Base::operator*;
00074 
00075     inline Index rows() const { return 3; }
00076     inline Index cols() const { return 1; }
00077 
00078     inline const Scalar& coeff(Index row, Index col) const
00079     { return m_coeffs.coeff(row, col); }
00080 
00081     inline Scalar& coeffRef(Index row, Index col)
00082     { return m_coeffs.coeffRef(row, col); }
00083 
00084     inline const Scalar& coeff(Index index) const
00085     { return m_coeffs.coeff(index); }
00086 
00087     inline Scalar& coeffRef(Index index)
00088     { return m_coeffs.coeffRef(index);}
00089 
00090 
00091     inline AlignedVector3(const Scalar& x, const Scalar& y, const Scalar& z)
00092       : m_coeffs(x, y, z, Scalar(0))
00093     {}
00094 
00095     inline AlignedVector3(const AlignedVector3& other)
00096       : Base(), m_coeffs(other.m_coeffs)
00097     {}
00098 
00099     template<typename XprType, int Size=XprType::SizeAtCompileTime>
00100     struct generic_assign_selector {};
00101 
00102     template<typename XprType> struct generic_assign_selector<XprType,4>
00103     {
00104       inline static void run(AlignedVector3& dest, const XprType& src)
00105       {
00106         dest.m_coeffs = src;
00107       }
00108     };
00109 
00110     template<typename XprType> struct generic_assign_selector<XprType,3>
00111     {
00112       inline static void run(AlignedVector3& dest, const XprType& src)
00113       {
00114         dest.m_coeffs.template head<3>() = src;
00115         dest.m_coeffs.w() = Scalar(0);
00116       }
00117     };
00118 
00119     template<typename Derived>
00120     inline explicit AlignedVector3(const MatrixBase<Derived>& other)
00121     {
00122       generic_assign_selector<Derived>::run(*this,other.derived());
00123     }
00124 
00125     inline AlignedVector3& operator=(const AlignedVector3& other)
00126     { m_coeffs = other.m_coeffs; return *this; }
00127 
00128 
00129     inline AlignedVector3 operator+(const AlignedVector3& other) const
00130     { return AlignedVector3(m_coeffs + other.m_coeffs); }
00131 
00132     inline AlignedVector3& operator+=(const AlignedVector3& other)
00133     { m_coeffs += other.m_coeffs; return *this; }
00134 
00135     inline AlignedVector3 operator-(const AlignedVector3& other) const
00136     { return AlignedVector3(m_coeffs - other.m_coeffs); }
00137 
00138     inline AlignedVector3 operator-=(const AlignedVector3& other)
00139     { m_coeffs -= other.m_coeffs; return *this; }
00140 
00141     inline AlignedVector3 operator*(const Scalar& s) const
00142     { return AlignedVector3(m_coeffs * s); }
00143 
00144     inline friend AlignedVector3 operator*(const Scalar& s,const AlignedVector3& vec)
00145     { return AlignedVector3(s * vec.m_coeffs); }
00146 
00147     inline AlignedVector3& operator*=(const Scalar& s)
00148     { m_coeffs *= s; return *this; }
00149 
00150     inline AlignedVector3 operator/(const Scalar& s) const
00151     { return AlignedVector3(m_coeffs / s); }
00152 
00153     inline AlignedVector3& operator/=(const Scalar& s)
00154     { m_coeffs /= s; return *this; }
00155 
00156     inline Scalar dot(const AlignedVector3& other) const
00157     {
00158       eigen_assert(m_coeffs.w()==Scalar(0));
00159       eigen_assert(other.m_coeffs.w()==Scalar(0));
00160       return m_coeffs.dot(other.m_coeffs);
00161     }
00162 
00163     inline void normalize()
00164     {
00165       m_coeffs /= norm();
00166     }
00167 
00168     inline AlignedVector3 normalized()
00169     {
00170       return AlignedVector3(m_coeffs / norm());
00171     }
00172 
00173     inline Scalar sum() const
00174     {
00175       eigen_assert(m_coeffs.w()==Scalar(0));
00176       return m_coeffs.sum();
00177     }
00178 
00179     inline Scalar squaredNorm() const
00180     {
00181       eigen_assert(m_coeffs.w()==Scalar(0));
00182       return m_coeffs.squaredNorm();
00183     }
00184 
00185     inline Scalar norm() const
00186     {
00187       return internal::sqrt(squaredNorm());
00188     }
00189 
00190     inline AlignedVector3 cross(const AlignedVector3& other) const
00191     {
00192       return AlignedVector3(m_coeffs.cross3(other.m_coeffs));
00193     }
00194 
00195     template<typename Derived>
00196     inline bool isApprox(const MatrixBase<Derived>& other, RealScalar eps=NumTraits<Scalar>::dummy_precision()) const
00197     {
00198       return m_coeffs.template head<3>().isApprox(other,eps);
00199     }
00200 };
00201 
00202 //@}
00203 
00204 }
00205 
00206 #endif // EIGEN_ALIGNED_VECTOR3



Page generated by Doxygen 1.7.4 for MRPT 0.9.5 SVN:2717 at Sun Oct 16 16:08:03 PDT 2011 Hosted on:
SourceForge.net Logo