vect3.hpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:9k
- /*
- * ===========================================================================
- * PRODUCTION $Log: vect3.hpp,v $
- * PRODUCTION Revision 1000.2 2004/06/01 19:49:07 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
- * PRODUCTION
- * ===========================================================================
- */
- #ifndef GUI_MATH___VECT3___HPP
- #define GUI_MATH___VECT3___HPP
- /* $Id: vect3.hpp,v 1000.2 2004/06/01 19:49:07 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software / database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software / database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Mike DiCuccio
- *
- * File Description:
- *
- */
- #include <corelib/ncbistd.hpp>
- #include <gui/math/math.hpp>
- /** @addtogroup GUI_MATH
- *
- * @{
- */
- BEGIN_NCBI_SCOPE
- template <class T>
- class CVect3
- {
- public: // interface
- // ctors
- CVect3();
- explicit CVect3(T val);
- explicit CVect3(T, T, T);
- //
- // operators
- // operator: indexing
- const T& operator[] (int i) const { return m_Xyz[i]; }
- T& operator[] (int i) { return m_Xyz[i]; }
- // operators: math
- CVect3<T>& operator+= (T);
- CVect3<T>& operator+= (const CVect3<T>&);
- CVect3<T>& operator-= (T);
- CVect3<T>& operator-= (const CVect3<T>&);
- CVect3<T>& operator*= (T);
- CVect3<T>& operator*= (const CVect3<T>&); // cross product!
- CVect3<T>& operator/= (T);
- CVect3<T>& operator/= (const CVect3<T>&);
- //
- // named functions
- // return length of vector
- float Length() const;
- // return length of vector squared
- float Length2() const;
- // return true if the length of the vector is 0
- bool Vanishing() const;
- // normalize a vector
- void Normalize();
- // return a unit vector in the direction of the current vector
- CVect3<T> Direction() const;
- // test whether a passed vector is parallel or normal to current
- bool Parallel(const CVect3<T>&) const;
- bool Normal(const CVect3<T>&) const;
- // perform dot product
- T Dot(const CVect3<T>&) const;
- // perform cross product(same as operator*=)
- CVect3<T> Cross(const CVect3<T>&) const;
- // accessor functions
- T& X() { return m_Xyz[0]; }
- const T& X() const { return m_Xyz[0]; }
- T& Y() { return m_Xyz[1]; }
- const T& Y() const { return m_Xyz[1]; }
- T& Z() { return m_Xyz[2]; }
- const T& Z() const { return m_Xyz[2]; }
- const T* GetData() const { return m_Xyz; }
- private:
- T m_Xyz[3];
- };
- END_NCBI_SCOPE
- //
- // global operations
- // this is included after the class declaration
- #include <gui/math/globals.hpp>
- BEGIN_NCBI_SCOPE
- //
- // default ctor
- //
- template <class T> inline
- CVect3<T>::CVect3()
- {
- m_Xyz[0] = m_Xyz[1] = m_Xyz[2] = (T)0;
- }
- //
- // conversion ctors
- //
- template <class T> inline
- CVect3<T>::CVect3 (T val)
- {
- m_Xyz[0] = m_Xyz[1] = m_Xyz[2] = val;
- }
- template <class T> inline
- CVect3<T>::CVect3 (T x, T y, T z)
- {
- m_Xyz[0] = x;
- m_Xyz[1] = y;
- m_Xyz[2] = z;
- }
- //
- // operator+ (scalar)
- //
- template <class T> inline CVect3<T>&
- CVect3<T>::operator+= (T scalar)
- {
- X() += scalar;
- Y() += scalar;
- Z() += scalar;
- return *this;
- }
- //
- // operator+ (vector)
- //
- template <class T> inline CVect3<T>&
- CVect3<T>::operator+= (const CVect3<T>& v)
- {
- X() += v.X();
- Y() += v.Y();
- Z() += v.Z();
- return *this;
- }
- //
- // operator- (scalar)
- //
- template <class T> inline CVect3<T>&
- CVect3<T>::operator-= (T scalar)
- {
- X() -= scalar;
- Y() -= scalar;
- Z() -= scalar;
- return *this;
- }
- //
- // operator- (vector)
- //
- template <class T> inline CVect3<T>&
- CVect3<T>::operator-= (const CVect3<T>& v)
- {
- X() -= v.X();
- Y() -= v.Y();
- Z() -= v.Z();
- return *this;
- }
- //
- // operator*= (scalar)
- //
- template <class T> inline CVect3<T>&
- CVect3<T>::operator*= (T scalar)
- {
- X() *= scalar;
- Y() *= scalar;
- Z() *= scalar;
- return *this;
- }
- //
- // operator* (vector)
- // (cross product!!!)
- //
- template <class T> inline CVect3<T>&
- CVect3<T>::operator*= (const CVect3<T>& v)
- {
- *this = Cross(v);
- return *this;
- }
- //
- // dot(vector)
- //
- template <class T> inline T
- CVect3<T>::Dot(const CVect3<T>& v) const
- {
- return (T)(X() * v.X() + Y() * v.Y() + Z() * v.Z());
- }
- //
- // cross()
- // cross product; same as operator*= except no assignment
- //
- template <class T> inline CVect3<T>
- CVect3<T>::Cross(const CVect3<T>& v) const
- {
- // cross product is as follows:
- //
- // a x b = <a_2 * b_3 - a_3 * b_2, -(a_1 * b_3 - a_3 * b_1), a_1 * b_2 - a_2 * b_1>
- //
- return CVect3<T> ( Y() * v.Z() - Z() * v.Y(),
- -(X() * v.Z() - Z() * v.X()),
- X() * v.Y() - Y() * v.X());
- }
- //
- // operator/= (scalar)
- //
- template <class T> inline CVect3<T>&
- CVect3<T>::operator/= (T scalar)
- {
- scalar = T(1) / scalar;
- X() *= scalar;
- Y() *= scalar;
- Z() *= scalar;
- return *this;
- }
- //
- // length()
- // return the length of a vector
- //
- template <class T> inline float
- CVect3<T>::Length() const
- {
- return ::sqrt(Length2());
- }
- template <class T> inline float
- CVect3<T>::Length2() const
- {
- return (X() * X() + Y() * Y() + Z() * Z());
- }
- template <class T> inline bool
- CVect3<T>::Vanishing() const
- {
- return (Length2() == 0.0);
- }
- //
- // normalize()
- // converts a vector to a unit vector
- //
- template <class T> inline void
- CVect3<T>::Normalize()
- {
- typedef NCBI_PROMOTE(T, float) TFloat;
- TFloat f = TFloat(1) / TFloat(Length());
- if (f != TFloat(0)) {
- X() = (X() * f);
- Y() = (Y() * f);
- Z() = (Z() * f);
- }
- }
- //
- // direction()
- // returns a unit vector in the direction of the current vector
- //
- template <class T> inline CVect3<T>
- CVect3<T>::Direction() const
- {
- return CVect3<T>(*this, true);
- }
- //
- // parallel()
- // tests to see whether the passed vector is parallel to the current vector
- //
- template <class T> inline bool
- CVect3<T>::Parallel(const CVect3<T>& v) const
- {
- typedef NCBI_PROMOTE(T, float) TFloat;
- TFloat result = (TFloat)Dot(v);
- TFloat l = Length() * v.Length();
- if (result == l || result == -l) {
- return true;
- }
- return false;
- }
- //
- // normal()
- // tests to see whether the passed vector is nromal to the current vector
- //
- template <class T> inline bool
- CVect3<T>::Normal(const CVect3<T>& v) const
- {
- typedef NCBI_PROMOTE(T, float) TFloat;
- TFloat result = (TFloat)Dot(v);
- if (result == TFloat(0) &&
- Length() != TFloat(0) &&
- v.Length() != TFloat(0)) {
- return true;
- }
- return false;
- }
- END_NCBI_SCOPE
- /* @} */
- /*
- * ===========================================================================
- * $Log: vect3.hpp,v $
- * Revision 1000.2 2004/06/01 19:49:07 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
- *
- * Revision 1.6 2004/05/11 18:53:50 dicuccio
- * Added doxygen modules info
- *
- * Revision 1.5 2004/05/03 13:10:01 dicuccio
- * Removed gui/gui.hpp --> not necessary for math projects
- *
- * Revision 1.4 2004/05/03 12:43:35 dicuccio
- * Added #include for gui/gui.hpp
- *
- * Revision 1.3 2004/03/11 17:16:56 dicuccio
- * FIxed include guards. Fixed use of dead functions (upper case first letter)
- *
- * Revision 1.2 2003/12/22 19:14:21 dicuccio
- * Fixed lots of bugs in referencing non-existent functions
- *
- * Revision 1.1 2003/06/09 19:30:50 dicuccio
- * Initial revision
- *
- * ===========================================================================
- */
- #endif // GUI_MATH___VECT3___HPP