vect2.hpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:7k
- /*
- * ===========================================================================
- * PRODUCTION $Log: vect2.hpp,v $
- * PRODUCTION Revision 1000.2 2004/06/01 19:49:04 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.5
- * PRODUCTION
- * ===========================================================================
- */
- #ifndef GUI_MATH___VECT2___HPP
- #define GUI_MATH___VECT2___HPP
- /* $Id: vect2.hpp,v 1000.2 2004/06/01 19:49:04 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 CVect2
- {
- public:
- // ctors
- CVect2();
- CVect2(T val);
- CVect2(T, T);
- //
- // operators
- // operator: indexing
- T operator[] (int i) const { return m_Xy[i]; }
- T& operator[] (int i) { return m_Xy[i]; }
- // operators: math
- CVect2<T>& operator+= (T);
- CVect2<T>& operator+= (const CVect2<T>&);
- CVect2<T>& operator-= (T);
- CVect2<T>& operator-= (const CVect2<T>&);
- CVect2<T>& operator*= (T);
- CVect2<T>& operator*= (const CVect2<T>&); // cross product!
- CVect2<T>& operator/= (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
- CVect2<T> Direction() const;
- // test whether a passed vector is parallel or normal to current
- bool Parallel(const CVect2<T>&) const;
- bool Normal(const CVect2<T>&) const;
- // perform dot product
- T Dot(const CVect2<T>&) const;
- // accessor functions
- T& X() { return m_Xy[0]; }
- const T& X() const { return m_Xy[0]; }
- T& Y() { return m_Xy[1]; }
- const T& Y() const { return m_Xy[1]; }
- const T* GetData() const { return m_Xy; }
- protected:
- T m_Xy[2];
- };
- 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
- CVect2<T>::CVect2()
- {
- m_Xy[0] = m_Xy[1] = (T)0;
- }
- //
- // conversion ctors
- //
- template <class T> inline
- CVect2<T>::CVect2 (T val)
- {
- m_Xy[0] = m_Xy[1] = val;
- }
- template <class T> inline
- CVect2<T>::CVect2 (T x, T y)
- {
- m_Xy[0] = x;
- m_Xy[1] = y;
- }
- //
- // operator+ (scalar)
- //
- template <class T> inline CVect2<T>&
- CVect2<T>::operator+= (T scalar)
- {
- X() += scalar;
- Y() += scalar;
- return *this;
- }
- //
- // operator+ (vector)
- //
- template <class T> inline CVect2<T>&
- CVect2<T>::operator+= (const CVect2<T>& v)
- {
- X() += v.X();
- Y() += v.Y();
- return *this;
- }
- //
- // operator- (scalar)
- //
- template <class T> inline CVect2<T>&
- CVect2<T>::operator-= (T scalar)
- {
- X() -= scalar;
- Y() -= scalar;
- return *this;
- }
- //
- // operator- (vector)
- //
- template <class T> inline CVect2<T>&
- CVect2<T>::operator-= (const CVect2<T>& v)
- {
- X() -= v.X();
- Y() -= v.Y();
- return *this;
- }
- //
- // operator*= (scalar)
- //
- template <class T> inline CVect2<T>&
- CVect2<T>::operator*= (T scalar)
- {
- X() *= scalar;
- Y() *= scalar;
- return *this;
- }
- //
- // operator* (vector)
- // (cross product!!!)
- //
- template <class T> inline CVect2<T>&
- CVect2<T>::operator*= (const CVect2<T>& v)
- {
- *this = Cross(v);
- return *this;
- }
- //
- // dot(vector)
- //
- template <class T> inline T
- CVect2<T>::Dot(const CVect2<T>& v) const
- {
- return (T)(X() * v.X() + Y() * v.Y());
- }
- //
- // operator/= (scalar)
- //
- template <class T> inline CVect2<T>&
- CVect2<T>::operator/= (T scalar)
- {
- scalar = T(1) / scalar;
- X() *= scalar;
- Y() *= scalar;
- return *this;
- }
- //
- // length()
- // return the length of a vector
- //
- template <class T> inline float
- CVect2<T>::Length() const
- {
- return ::sqrt(Length2());
- }
- template <class T> inline float
- CVect2<T>::Length2() const
- {
- return (X() * X() + Y() * Y());
- }
- template <class T> inline bool
- CVect2<T>::Vanishing() const
- {
- return (Length2() == 0.0);
- }
- //
- // normalize()
- // converts a vector to a unit vector
- //
- template <class T> inline void
- CVect2<T>::Normalize()
- {
- typedef NCBI_PROMOTE(T, float) TFloat;
- TFloat f = TFloat(1) / TFloat(Length());
- if (f != TFloat(0)) {
- X() = (X() * f);
- Y() = (Y() * f);
- }
- }
- //
- // direction()
- // returns a unit vector in the direction of the current vector
- //
- template <class T> inline CVect2<T>
- CVect2<T>::Direction() const
- {
- return CVect2<T>(*this, true);
- }
- //
- // parallel()
- // tests to see whether the passed vector is parallel to the current vector
- //
- template <class T> inline bool
- CVect2<T>::Parallel(const CVect2<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
- CVect2<T>::Normal(const CVect2<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: vect2.hpp,v $
- * Revision 1000.2 2004/06/01 19:49:04 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.5
- *
- * Revision 1.5 2004/05/11 18:53:50 dicuccio
- * Added doxygen modules info
- *
- * Revision 1.4 2004/05/03 13:10:01 dicuccio
- * Removed gui/gui.hpp --> not necessary for math projects
- *
- * Revision 1.3 2004/05/03 12:43:35 dicuccio
- * Added #include for gui/gui.hpp
- *
- * Revision 1.2 2004/03/11 17:16:56 dicuccio
- * FIxed include guards. Fixed use of dead functions (upper case first letter)
- *
- * Revision 1.1 2003/06/09 19:30:50 dicuccio
- * Initial revision
- *
- * ===========================================================================
- */
- #endif // GUI_MATH___VECT2___HPP///