usata-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Usata-commits] Changes to usata2/src/math/vector.hpp


From: Chong Kai Xiong
Subject: [Usata-commits] Changes to usata2/src/math/vector.hpp
Date: Tue, 04 Jan 2005 10:16:40 -0500

Index: usata2/src/math/vector.hpp
diff -u usata2/src/math/vector.hpp:1.7 usata2/src/math/vector.hpp:1.8
--- usata2/src/math/vector.hpp:1.7      Tue Jan  4 13:15:56 2005
+++ usata2/src/math/vector.hpp  Tue Jan  4 15:04:37 2005
@@ -10,13 +10,14 @@
 // included in the software distribution, or visit
 // http://www.fsf.org/licenses/gpl.html.
 //
-// $Id: vector.hpp,v 1.7 2005/01/04 13:15:56 Descender Exp $
+// $Id: vector.hpp,v 1.8 2005/01/04 15:04:37 Descender Exp $
 
 #ifndef USATA_MATH_VECTOR_HPP
 #define USATA_MATH_VECTOR_HPP
 
 #include <iostream>
 #include <cmath>
+#include <boost/cast.hpp>
 
 namespace usata
 {
@@ -28,6 +29,9 @@
 
                //! \ingroup math
                //! \brief Homogeneous 3D vector
+
+               //! \todo Evaluate impact of replacing numeric casts with
+               //!       boost::numeric_cast<>
                //! \todo Convert to an expression template implementation.
                //! \param T Component type.
                template <typename T>
@@ -63,7 +67,7 @@
                        //! \param index Index of component.
                        //! \return reference to component.
                        T&
-                       operator[] (int index) const
+                       operator[] (int index)
                        {
                                return v[index];
                        }
@@ -135,8 +139,9 @@
                        //! \brief Scalar multiplication operator
                        //! \param[in] lhs Left operand.
                        //! \param[in] rhs Right operand.
+                       template <typename ScalarT>
                        friend Vector4
-                       operator * (const Vector4& lhs, const T& rhs)
+                       operator * (const Vector4& lhs, const ScalarT& rhs)
                        {
                                return Vector4(lhs.v[0] * rhs, lhs.v[1] * rhs,
                                                           lhs.v[2] * rhs, 
lhs.v[3] * rhs);
@@ -145,8 +150,9 @@
                        //! \brief Scalar multiplication operator
                        //! \param[in] lhs Left operand.
                        //! \param[in] rhs Right operand.
+                       template <typename ScalarT>
                        friend Vector4
-                       operator * (const T& lhs, Vector4& rhs)
+                       operator * (const ScalarT& lhs, Vector4& rhs)
                        {
                                return rhs * lhs;
                        }
@@ -154,8 +160,9 @@
                        //! \brief Scalar division operator
                        //! \param[in] lhs Left operand.
                        //! \param[in] rhs Right operand.
+                       template <typename ScalarT>
                        friend Vector4
-                       operator / (const Vector4& lhs, const T& rhs)
+                       operator / (const Vector4& lhs, const ScalarT& rhs)
                        {
                                return Vector4(lhs.v[0] / rhs,
                                                           lhs.v[1] / rhs,
@@ -171,7 +178,7 @@
                                v[0] += rhs.v[0];
                                v[1] += rhs.v[1];
                                v[2] += rhs.v[2];
-                               v[4] += rhs.v[3];
+                               v[3] += rhs.v[3];
                                return *this;
                        }
 
@@ -189,8 +196,9 @@
 
                        //! \brief Assignment-scalar multiplication operator
                        //! \param[in] rhs Right operand.
+                       template <typename ScalarT>
                        Vector4&
-                       operator *= (const T& rhs)
+                       operator *= (const ScalarT& rhs)
                        {
                                v[0] *= rhs;
                                v[1] *= rhs;
@@ -201,8 +209,9 @@
 
                        //! \brief Assignment-scalar division operator
                        //! \param[in] rhs Right operand.
+                       template <typename ScalarT>
                        Vector4&
-                       operator /= (const T& rhs)
+                       operator /= (const ScalarT& rhs)
                        {
                                v[0] /= rhs;
                                v[1] /= rhs;
@@ -229,7 +238,7 @@
                        //! \brief Returns cross product
                        //! \param[in] rhs Right operand.
                        //! \return Cross product.
-                       T
+                       Vector4
                        cross(const Vector4& rhs) const
                        {
                                return Vector4(v[1] * rhs.v[2] - v[2] * 
rhs.v[1],
@@ -238,30 +247,46 @@
                        }
 
                        //! \brief Returns unit vector
-                       Vector4
+                       //! \return Unit vector
+                       template <typename ReturnT>
+                       Vector4<ReturnT>
                        unit() const
                        {
-                               T length_(length());
-
-                               return Vector4(v[0] / length_,
-                                                          v[1] / length_,
-                                                          v[2] / length_,
-                                                          v[3] / length_);
+                               ReturnT length_(length<ReturnT>());
+                               return Vector4<ReturnT>(v[0] / length_,
+                                                                               
v[1] / length_,
+                                                                               
v[2] / length_,
+                                                                               
v[3] / length_);
                        }
 
                        //! \brief Returns magnitude
                        //! \return Magnitude.
-                       T
+                       template <typename ReturnT>
+                       ReturnT
                        length() const
                        {
-                               return std::sqrt(dot(*this));
+                               return ReturnT(std::sqrt(dot(*this)));
                        }
 
                        //! \brief Normalizes vector
                        void
                        normalize()
                        {
-                               *this /= length();
+                               // FIXME: should we use length<double>() (or 
float)
+                               // for accuracy with Vector4<int>? - descender
+                               *this /= length<T>();
+                       }
+
+                       //! \brief Conversion to Vector4 of another type
+                       //! \return Converted Vector4.
+                   template <typename ReturnT>
+                       Vector4<ReturnT>
+                       to() const
+                       {
+                               return Vector4<ReturnT>(ReturnT(v[0]),
+                                                                               
ReturnT(v[1]),
+                                                                               
ReturnT(v[2]),
+                                                                               
ReturnT(v[3]));
                        }
 
                }; // class Vector




reply via email to

[Prev in Thread] Current Thread [Next in Thread]