Wm4Vector3.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-2008
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 //
00014 // Version: 4.0.3 (2007/03/07)
00015 
00016 #ifndef WM4VECTOR3_H
00017 #define WM4VECTOR3_H
00018 
00019 #include "Wm4FoundationLIB.h"
00020 #include "Wm4Math.h"
00021 
00022 namespace Wm4
00023 {
00024 
00025 template <class Real>
00026 class Vector3
00027 {
00028 public:
00029     // construction
00030     Vector3 ();  // uninitialized
00031     Vector3 (Real fX, Real fY, Real fZ);
00032     Vector3 (const Real* afTuple);
00033     Vector3 (const Vector3& rkV);
00034 
00035     // coordinate access
00036     inline operator const Real* () const;
00037     inline operator Real* ();
00038     inline Real operator[] (int i) const;
00039     inline Real& operator[] (int i);
00040     inline Real X () const;
00041     inline Real& X ();
00042     inline Real Y () const;
00043     inline Real& Y ();
00044     inline Real Z () const;
00045     inline Real& Z ();
00046 
00047     // assignment
00048     inline Vector3& operator= (const Vector3& rkV);
00049 
00050     // comparison
00051     bool operator== (const Vector3& rkV) const;
00052     bool operator!= (const Vector3& rkV) const;
00053     bool operator<  (const Vector3& rkV) const;
00054     bool operator<= (const Vector3& rkV) const;
00055     bool operator>  (const Vector3& rkV) const;
00056     bool operator>= (const Vector3& rkV) const;
00057 
00058     // arithmetic operations
00059     inline Vector3 operator+ (const Vector3& rkV) const;
00060     inline Vector3 operator- (const Vector3& rkV) const;
00061     inline Vector3 operator* (Real fScalar) const;
00062     inline Vector3 operator/ (Real fScalar) const;
00063     inline Vector3 operator- () const;
00064 
00065     // arithmetic updates
00066     inline Vector3& operator+= (const Vector3& rkV);
00067     inline Vector3& operator-= (const Vector3& rkV);
00068     inline Vector3& operator*= (Real fScalar);
00069     inline Vector3& operator/= (Real fScalar);
00070 
00071     // vector operations
00072     inline Real Length () const;
00073     inline Real SquaredLength () const;
00074     inline Real Dot (const Vector3& rkV) const;
00075     inline Real Normalize ();
00076 
00077     // The cross products are computed using the right-handed rule.  Be aware
00078     // that some graphics APIs use a left-handed rule.  If you have to compute
00079     // a cross product with these functions and send the result to the API
00080     // that expects left-handed, you will need to change sign on the vector
00081     // (replace each component value c by -c).
00082     inline Vector3 Cross (const Vector3& rkV) const;
00083     inline Vector3 UnitCross (const Vector3& rkV) const;
00084 
00085     // Compute the barycentric coordinates of the point with respect to the
00086     // tetrahedron <V0,V1,V2,V3>, P = b0*V0 + b1*V1 + b2*V2 + b3*V3, where
00087     // b0 + b1 + b2 + b3 = 1.
00088     void GetBarycentrics (const Vector3& rkV0, const Vector3& rkV1,
00089         const Vector3& rkV2, const Vector3& rkV3, Real afBary[4]) const;
00090 
00091     // Gram-Schmidt orthonormalization.  Take linearly independent vectors
00092     // U, V, and W and compute an orthonormal set (unit length, mutually
00093     // perpendicular).
00094     static void Orthonormalize (Vector3& rkU, Vector3& rkV, Vector3& rkW);
00095     static void Orthonormalize (Vector3* akV);
00096 
00097     // Input W must be a nonzero vector. The output is an orthonormal basis
00098     // {U,V,W}.  The input W is normalized by this function.  If you know
00099     // W is already unit length, use GenerateComplementBasis to compute U
00100     // and V.
00101     static void GenerateOrthonormalBasis (Vector3& rkU, Vector3& rkV,
00102         Vector3& rkW);
00103 
00104     // Input W must be a unit-length vector.  The output vectors {U,V} are
00105     // unit length and mutually perpendicular, and {U,V,W} is an orthonormal
00106     // basis.
00107     static void GenerateComplementBasis (Vector3& rkU, Vector3& rkV,
00108         const Vector3& rkW);
00109 
00110     // Compute the extreme values.
00111     static void ComputeExtremes (int iVQuantity, const Vector3* akPoint,
00112         Vector3& rkMin, Vector3& rkMax);
00113 
00114     // special vectors
00115     WM4_FOUNDATION_ITEM static const Vector3 ZERO;    // (0,0,0)
00116     WM4_FOUNDATION_ITEM static const Vector3 UNIT_X;  // (1,0,0)
00117     WM4_FOUNDATION_ITEM static const Vector3 UNIT_Y;  // (0,1,0)
00118     WM4_FOUNDATION_ITEM static const Vector3 UNIT_Z;  // (0,0,1)
00119     WM4_FOUNDATION_ITEM static const Vector3 ONE;     // (1,1,1)
00120 
00121 private:
00122     // support for comparisons
00123     int CompareArrays (const Vector3& rkV) const;
00124 
00125     Real m_afTuple[3];
00126 };
00127 
00128 // arithmetic operations
00129 template <class Real>
00130 Vector3<Real> operator* (Real fScalar, const Vector3<Real>& rkV);
00131 
00132 // debugging output
00133 template <class Real>
00134 std::ostream& operator<< (std::ostream& rkOStr, const Vector3<Real>& rkV);
00135 
00136 #include "Wm4Vector3.inl"
00137 
00138 typedef Vector3<float> Vector3f;
00139 typedef Vector3<double> Vector3d;
00140 
00141 }
00142 
00143 #endif

Generated on Fri Feb 13 13:58:10 2009 for meshmorph by  doxygen 1.5.1