[Top][All Lists]
[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: |
Sun, 09 Jan 2005 14:22:56 -0500 |
Index: usata2/src/math/vector.hpp
diff -u usata2/src/math/vector.hpp:1.10 usata2/src/math/vector.hpp:1.11
--- usata2/src/math/vector.hpp:1.10 Wed Jan 5 01:26:11 2005
+++ usata2/src/math/vector.hpp Sun Jan 9 18:47:01 2005
@@ -10,7 +10,7 @@
// included in the software distribution, or visit
// http://www.fsf.org/licenses/gpl.html.
//
-// $Id: vector.hpp,v 1.10 2005/01/05 01:26:11 Descender Exp $
+// $Id: vector.hpp,v 1.11 2005/01/09 18:47:01 Descender Exp $
#ifndef USATA_MATH_VECTOR_HPP
#define USATA_MATH_VECTOR_HPP
@@ -28,10 +28,78 @@
//! \brief Mathematical classes and functions
namespace math
{
+ template <typename T>
+ class Vector3;
+
+ template <typename T>
+ class Vector4;
+
+ // NOTE: I've chosen to use an array instead of one named
+ // variable for each component because:
+ //
+ // a. The [] operator is conveniently and safely implemented.
+ // b. The notation v[i] is more portable different vector
+ // spaces, e.g. (r, g, b)
+ // c. It easily allows generalizations
+ //
+ // - descender
//! \ingroup math
- //! \brief Homogeneous 3D vector
+ //! \brief 3D vector
+ template <typename T>
+ class Vector3
+ {
+ public:
+ //! \brief Components
+ T v[3];
+
+ //! \brief Default constructor
+ //!
+ //! Initializes all components to 0.
+ Vector3()
+ {
+ v[0] = v[1] = v[2] = 0;
+ }
+
+ //! \brief Component-wise constructor
+ //! \param[in] x 1st component.
+ //! \param[in] y 2nd component.
+ //! \param[in] z 3rd component.
+ Vector3(T x, T y, T z)
+ {
+ v[0] = x;
+ v[1] = y;
+ v[2] = z;
+ }
+
+ //! \brief Vector4<T> converter
+ //! \param[in] v Vector to convert.
+ Vector3(const Vector4<T>& v, bool divide = false)
+ {
+ v[0] = v.v[0];
+ v[1] = v.v[1];
+ v[2] = v.v[2];
+
+ if (divide)
+ {
+ v[0] /= v.v[3];
+ v[1] /= v.v[3];
+ v[2] /= v.v[3];
+ }
+ }
+
+ //! \brief Index operator
+ //! \param index Index of component.
+ //! \return Reference to component.
+ T&
+ operator[] (int index)
+ {
+ return v[index];
+ }
+ };
+ //! \ingroup math
+ //! \brief Homogeneous 3D vector
//! \todo Evaluate impact of replacing numeric casts with
//! boost::numeric_cast<>
//! \todo Convert to an expression template implementation.
@@ -65,15 +133,26 @@
v[3] = w;
}
+ Vector4(const Vector3<T>& v, T w = 0)
+ {
+ v[0] = v.v[0];
+ v[1] = v.v[1];
+ v[2] = v.v[2];
+ v[3] = w;
+ }
+
//! \brief Index operator
//! \param index Index of component.
- //! \return reference to component.
+ //! \return Reference to component.
T&
operator[] (int index)
{
return v[index];
}
+ //! \brief Const index operator
+ //! \param index Index of component.
+ //! \return Reference to component.
const T&
operator[] (int index) const
{
@@ -146,6 +225,18 @@
-operand.v[3]);
}
+ //! \brief Cartesian product
+ //! \param[in] lhs Left operand.
+ //! \param[in] rhs Right operand.
+ friend Vector4
+ operator * (const Vector4& lhs, const Vector4& rhs)
+ {
+ return Vector4(lhs.v[0] * lhs.v[0],
+ lhs.v[1] * lhs.v[1],
+ lhs.v[2] * lhs.v[2],
+ lhs.v[3] * lhs.v[3]);
+ }
+
//! \brief Scalar multiplication operator
//! \param[in] lhs Left operand.
//! \param[in] rhs Right operand.
@@ -256,6 +347,28 @@
v[0] * rhs.v[1] -
v[1] * rhs.v[0]);
}
+ //! \brief Returns cosine angle
+ //! \param[in] rhs Right operand.
+ //! \return Cosine angle.
+ template <typename ReturnT>
+ ReturnT
+ cos_angle(const Vector4& rhs) const
+ {
+ return ReturnT(dot(rhs)) / (length() *
rhs.length());
+ }
+
+ //! \brief Returns unit vector
+ //! \return Unit vector
+ Vector4
+ unit() const
+ {
+ T length_(length());
+ return Vector4(v[0] / length_,
+ v[1] / length_,
+ v[2] / length_,
+ v[3] / length_);
+ }
+
//! \brief Returns unit vector
//! \return Unit vector
template <typename ReturnT>
@@ -270,6 +383,13 @@
}
//! \brief Returns magnitude
+ T
+ length() const
+ {
+ return T(std::sqrt(dot(*this)));
+ }
+
+ //! \brief Returns magnitude
//! \return Magnitude.
template <typename ReturnT>
ReturnT
@@ -299,12 +419,16 @@
ReturnT(v[3]));
}
- }; // class Vector
+ }; // class Vector4
// Typedefs for common type parameters (naming inspired by
// OpenGL API)
+ typedef Vector3<int> Vector3i;
+ typedef Vector3<float> Vector3f;
+ typedef Vector3<double> Vector3d;
+
typedef Vector4<int> Vector4i;
typedef Vector4<float> Vector4f;
typedef Vector4<double> Vector4d;