Wm4Vector2.h

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2007
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 // The license applies to versions 0 through 4 of Wild Magic.
00014 //
00015 // Version: 4.0.2 (2006/07/25)
00016 
00017 #ifndef WM4VECTOR2_H
00018 #define WM4VECTOR2_H
00019 
00020 #include "Wm4FoundationLIB.h"
00021 #include "Wm4Math.h"
00022 
00023 namespace Wm4
00024 {
00025 template <class Real>
00026 class Vector2
00027 {
00028 public:
00029     // construction
00030     Vector2 ();  // uninitialized
00031     Vector2 (Real fX, Real fY);
00032     Vector2 (const Real* afTuple);
00033     Vector2 (const Vector2& rkV);
00034 
00035     // coordinate access
00036     operator const Real* () const;
00037     operator Real* ();
00038     Real operator[] (int i) const;
00039     Real& operator[] (int i);
00040     Real X () const;
00041     Real& X ();
00042     Real Y () const;
00043     Real& Y ();
00044 
00045     // assignment
00046     Vector2& operator= (const Vector2& rkV);
00047 
00048     // comparison
00049     bool operator== (const Vector2& rkV) const;
00050     bool operator!= (const Vector2& rkV) const;
00051     bool operator<  (const Vector2& rkV) const;
00052     bool operator<= (const Vector2& rkV) const;
00053     bool operator>  (const Vector2& rkV) const;
00054     bool operator>= (const Vector2& rkV) const;
00055 
00056     // arithmetic operations
00057     Vector2 operator+ (const Vector2& rkV) const;
00058     Vector2 operator- (const Vector2& rkV) const;
00059     Vector2 operator* (Real fScalar) const;
00060     Vector2 operator/ (Real fScalar) const;
00061     Vector2 operator- () const;
00062 
00063     // arithmetic updates
00064     Vector2& operator+= (const Vector2& rkV);
00065     Vector2& operator-= (const Vector2& rkV);
00066     Vector2& operator*= (Real fScalar);
00067     Vector2& operator/= (Real fScalar);
00068 
00069     // vector operations
00070     Real Length () const;
00071     Real SquaredLength () const;
00072     Real Dot (const Vector2& rkV) const;
00073     Real Normalize ();
00074 
00075     // returns (y,-x)
00076     Vector2 Perp () const;
00077 
00078     // returns (y,-x)/sqrt(x*x+y*y)
00079     Vector2 UnitPerp () const;
00080 
00081     // returns DotPerp((x,y),(V.x,V.y)) = x*V.y - y*V.x
00082     Real DotPerp (const Vector2& rkV) const;
00083 
00084     // Compute the barycentric coordinates of the point with respect to the
00085     // triangle <V0,V1,V2>, P = b0*V0 + b1*V1 + b2*V2, where b0 + b1 + b2 = 1.
00086     void GetBarycentrics (const Vector2& rkV0, const Vector2& rkV1,
00087         const Vector2& rkV2, Real afBary[3]) const;
00088 
00089     // Gram-Schmidt orthonormalization.  Take linearly independent vectors U
00090     // and V and compute an orthonormal set (unit length, mutually
00091     // perpendicular).
00092     static void Orthonormalize (Vector2& rkU, Vector2& rkV);
00093 
00094     // Input V must be a nonzero vector.  The output is an orthonormal basis
00095     // {U,V}.  The input V is normalized by this function.  If you know V is
00096     // already unit length, use U = V.Perp().
00097     static void GenerateOrthonormalBasis (Vector2& rkU, Vector2& rkV);
00098 
00099     // Compute the extreme values.
00100     static void ComputeExtremes (int iVQuantity, const Vector2* akPoint,
00101         Vector2& rkMin, Vector2& rkMax);
00102 
00103     // special vectors
00104     WM4_FOUNDATION_ITEM static const Vector2 ZERO;    // (0,0)
00105     WM4_FOUNDATION_ITEM static const Vector2 UNIT_X;  // (1,0)
00106     WM4_FOUNDATION_ITEM static const Vector2 UNIT_Y;  // (0,1)
00107     WM4_FOUNDATION_ITEM static const Vector2 ONE;     // (1,1)
00108 
00109 private:
00110     // support for comparisons
00111     int CompareArrays (const Vector2& rkV) const;
00112 
00113     Real m_afTuple[2];
00114 };
00115 
00116 // arithmetic operations
00117 template <class Real>
00118 Vector2<Real> operator* (Real fScalar, const Vector2<Real>& rkV);
00119 
00120 // debugging output
00121 template <class Real>
00122 std::ostream& operator<< (std::ostream& rkOStr, const Vector2<Real>& rkV);
00123 
00124 }
00125 
00126 #include "Wm4Vector2.inl"
00127 
00128 namespace Wm4
00129 {
00130 typedef Vector2<float> Vector2f;
00131 typedef Vector2<double> Vector2d;
00132 
00133 }
00134 
00135 #endif

Generated on Wed Nov 23 19:01:10 2011 for FreeCAD by  doxygen 1.6.1