![]() |
VR-Forces 4.0.4 Class Documentation
|
00001 /********************************************************************* 00002 ** Copyright (c) 2010 MaK Technologies, Inc. 00003 ** All rights reserved. 00004 *********************************************************************/ 00005 /********************************************************************* 00006 ** $RCSfile: DtVdcVector2D.h,v $ $Revision: 1.3 $ $State: Exp $ 00007 *********************************************************************/ 00008 #pragma once 00009 00010 #include <vlutil/vlString.h> 00011 #include <matrix/vlVector.h> 00012 #include <iosfwd> 00013 00014 #include <vrvDrawControl/vrvDrawControlDefines.h> 00015 00016 // \file DtVdcVector2D.h 00017 // \brief DtVdcVector2D.h provides an implimentation of a 2D Vector class as well as 00018 // many methods which operate on DtVdcVector2Ds. 00019 namespace vrvControlToolkit 00020 { 00021 00022 enum DtVdcVector2DOffset 00023 { 00024 DtVdcX, 00025 DtVdcY 00026 }; 00027 00028 class DtVdcVector2D; 00029 00030 typedef const DtVdcVector2D &DtVdcConstVector2D; 00031 typedef DtVdcVector2D &DtVdcVector2DRef; 00032 00033 // \brief DtVdcVector2Ds represent vectors in 2-d space, X, Y 00034 // 00035 // A DtVdcVector2D is the 2D equivalent of the 3D DtVector 00036 class DT_DLL_VRVDRAWCONTROL DtVdcVector2D 00037 { 00038 public: 00039 00040 DtVdcVector2D(); 00041 00042 // Construct from two doubles. 00043 DtVdcVector2D( const double& x, const double& y ); 00044 00045 // destructor 00046 ~DtVdcVector2D(); 00047 00048 // copy constructor 00049 DtVdcVector2D(const DtVdcVector2D& vec); 00050 00051 // assignment operator 00052 DtVdcVector2D& operator=(const DtVdcVector2D& vec); 00053 00054 // set Vector to all zeros 00055 void setToZero(); 00056 00057 // @{ 00058 // gets/sets the x component of a Vector 00059 const double& x() const; 00060 void setX(const double& x); 00061 // @} 00062 00063 // @{ 00064 // gets/sets the y component of a Vector 00065 const double& y() const; 00066 void setY(const double& y); 00067 // @} 00068 00069 // adds other to this vector 00070 DtVdcVector2D &operator+=(const DtVdcVector2D &other); 00071 00072 // subtracts other from this vector (this - other) 00073 DtVdcVector2D &operator-=(const DtVdcVector2D &other); 00074 00075 // subtraction 00076 DtVdcVector2D operator-(const DtVdcVector2D &other) const; 00077 00078 // addition 00079 DtVdcVector2D operator+(const DtVdcVector2D &other) const; 00080 00081 // multiplies this vector by scalar 00082 DtVdcVector2D operator*(float scale) const; 00083 00084 // multiplies this vector by scalar 00085 DtVdcVector2D& operator*=(float scale); 00086 00087 // Get the value for the specified subscript. (int version) 00088 double& operator[](int ind); 00089 00090 // A const version to get the value for the specified subscript 00091 double operator[](int ind) const; 00092 00093 // Get the value for the specified subscript. (enum version) 00094 double& operator[](DtVdcVector2DOffset ind); 00095 00096 // a const version to get the value for the specified subscript. (enum version) 00097 double operator[](DtVdcVector2DOffset ind) const; 00098 00099 // Get whether another vector equals this vector. Warning: This 00100 // operation compares floats of exact equality. 00101 int operator==(const DtVdcVector2D& vec) const; 00102 00103 // Get whether another vector does not equal this vector. Warning: 00104 // this operation calles operator== which tests floats for exact equality 00105 int operator!=(const DtVdcVector2D& vec) const; 00106 00107 // Get a string representation of the vector. 00108 DtString string() const; 00109 00110 // Create a DtVector (with z==0) from a DtVdcVector2D 00111 DtVector vector3D() const; 00112 00113 // Get the magnitude of the vector squared, 00114 // i.e. x^2 + y^2 00115 double magnitudeSquared() const; 00116 00117 // Normalize the vector; i.e., magnitude = 1. 00118 void normalize(); 00119 00120 // Returns true of the vector is Normalized 00121 bool isNormalized() const; 00122 00123 // Compute dot product with another vector 00124 double dotProduct(const DtVdcVector2D& v) const; 00125 00126 // check if the vector is zero-ed out withing the tolerace passed 00127 bool approxEq(const DtVdcVector2D& v, const double& tolerance) const; 00128 00129 // check if the magnitide of this vector is zero 00130 bool magnitudeIsZero() const; 00131 00132 // check if the magnitide of this vector is zero, within the tolerance passed. 00133 // \Note This tolerance is comparied against each component of the vector, 00134 // not the magnitude. 00135 bool magnitudeIsZero(double tolerance) const; 00136 00137 // prints out a formated string to the stream 00138 std::ostream &printDataToStream(std::ostream &str) const; 00139 00140 public: 00141 00142 // @name These functions reveal the internal representation of DtVector 00143 // and should not be used. They are provided for backwards compatability 00144 // @{ 00145 operator double* (); 00146 operator const double* () const; 00147 // @} 00148 00149 public: 00150 00151 00152 // Get a zero'ed vector. 00153 static const DtVdcVector2D& zero(); 00154 00155 // Get a (1,0) vector. 00156 static const DtVdcVector2D& i(); 00157 00158 // Get a (0,1) vector. 00159 static const DtVdcVector2D& j(); 00160 00161 // Get a vector set to all ones. 00162 static const DtVdcVector2D& ones(); 00163 00164 00165 protected: 00166 00167 double myRep[2]; 00168 00169 00170 }; 00171 00172 // 00173 // Vector routines. 00174 // 00175 00176 #define DtSetVec2D(v, x, y) v[DtSdcX]=x;v[DtSdcY]=y; 00177 00178 DT_DLL_VRVDRAWCONTROL void DtVecScale(const DtVdcVector2D& v, double scale_factor, DtVdcVector2D& result); 00179 DT_DLL_VRVDRAWCONTROL void DtVecNeg(const DtVdcVector2D& v1, DtVdcVector2D& result); // result = -vec 00180 00181 // cos of angle between v1 and V2 00182 DT_DLL_VRVDRAWCONTROL double DtVecCosProd(const DtVdcVector2D& v1, const DtVdcVector2D& v2); 00183 00184 DT_DLL_VRVDRAWCONTROL double DtVecInfNorm(const DtVdcVector2D& v1); 00185 DT_DLL_VRVDRAWCONTROL void DtVecInit(DtVdcVector2D& v); // Initialize to all zeros. 00186 00187 /* ||p1-p2||_{2} */ 00188 DT_DLL_VRVDRAWCONTROL double DtDistance(const DtVdcVector2D& p1, const DtVdcVector2D& p2); 00189 00190 DT_DLL_VRVDRAWCONTROL double DtDistSqr(const DtVdcVector2D& p1, const DtVdcVector2D& p2); 00191 } 00192 00193 std::ostream & operator << (std::ostream &str, const vrvControlToolkit::DtVdcVector2D &vec); 00194 00195 00196 // Include inline definitions for vlVector 00197 #define DtVdcVector2D_inl 00198 #include <vrvDrawControl/DtVdcVector2D.inl> 00199 #undef DtVdcVector2D_inl 00200 00201