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

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