Mobile API Reference  MicroStrategy 2019
Vector3D.h
Go to the documentation of this file.
1 //==============================================================================================
2 // FILENAME : Vector3D.h
3 // AUTHOR : Liang Liu
4 // CREATION : 2009-05-18
5 // Copyright (C) MicroStrategy Incorporated 2009
6 // All Rights Reserved
7 //==============================================================================================
8 
9 #ifndef MsiChart_Vector3D_h
10 #define MsiChart_Vector3D_h
11 
12 #include "Point3D.h"
13 #include "PDCHeader/PDCcmath"
14 
15 namespace MsiChart
16 {
20  template <class T = float>
21  class Vector3D
22  {
23  public:
30  Vector3D(double iX = T(), double iY = T(), double iZ = T()) : x(static_cast<T>(iX)), y(static_cast<T>(iY)), z(static_cast<T>(iZ)){}
35  Vector3D(const Point3D<T>& irPoint) : x(irPoint.x), y(irPoint.y), z(irPoint.z){}
42  template <typename U>
43  Vector3D(const Point3D<U>& irStart, const Point3D<U>& irEnd)
44  : x(static_cast<T>(irEnd.x - irStart.x)),
45  y(static_cast<T>(irEnd.y - irStart.y)),
46  z(static_cast<T>(irEnd.z - irStart.z)){}
47 
52  double LengthSquared() const;
57  double Length() const;
63  double IncludedAngle(const Vector3D<T>& irVector) const;
69  double InnerProduct(const Vector3D<T>& irVector) const;
75  double NormalizedInnerProduct(const Vector3D<T>& irVector) const;
81  void CrossProduct(const Vector3D<T>& irVector, Vector3D<T>& orCrossProduct) const;
82 
88  Vector3D<T> CrossProduct(const Vector3D<T>& irVector) const;
89 
91  void Normalize();
92 
97  void Swap(Vector3D<T>& iorVector);
98 
103  Vector3D<T> operator - () const;
109  Vector3D<T> operator - (const Vector3D<T>& irVector) const;
115  Vector3D<T> operator + (const Vector3D<T>& irVector) const;
116 
122  Vector3D<T> MultiplyBy(double iFactor) const;
123 
124  public:
125  T x;
126  T y;
127  T z;
128  };
129 
130  //get the square length of a vector
131  template <class T>
132  inline double Vector3D<T>::LengthSquared() const
133  {
134  return x * x + y * y + z * z;
135  }
136 
137  //A . B
138  template <class T>
139  inline double Vector3D<T>::InnerProduct(const Vector3D<T>& irVector) const
140  {
141  return (x * irVector.x + y * irVector.y + z * irVector.z);
142  }
143 
144  //A . B / (||A|| * ||B||)
145  template <class T>
146  inline double Vector3D<T>::NormalizedInnerProduct(const Vector3D<T>& irVector) const
147  {
148  return InnerProduct(irVector) / (std::sqrt(LengthSquared() * irVector.LengthSquared()));
149  }
150 
151  //A X B
152  template <class T>
153  inline void Vector3D<T>::CrossProduct(const Vector3D<T>& irVector, Vector3D<T>& orCrossProduct) const
154  {
155  orCrossProduct.x = y * irVector.z - z * irVector.y;
156  orCrossProduct.y = z * irVector.x - x * irVector.z;
157  orCrossProduct.z = x * irVector.y - y * irVector.x;
158  }
159 
160  //A X B
161  template <class T>
162  inline Vector3D<T> Vector3D<T>::CrossProduct(const Vector3D<T>& irVector) const
163  {
164  return Vector3D<T>(y * irVector.z - z * irVector.y, z * irVector.x - x * irVector.z, x * irVector.y - y * irVector.x);
165  }
166 
167  template <class T>
168  double Vector3D<T>::IncludedAngle(const Vector3D<T>& irVector) const
169  {
170  double lTemp = InnerProduct(irVector) / (std::sqrt(LengthSquared() * irVector.LengthSquared()));
171  lTemp = (lTemp > 1.0f) ? 1.0f : lTemp;
172  lTemp = (lTemp < -1.0f) ? -1.0f : lTemp;
173  return std::acos(lTemp);
174  }
175 
176  template <class T>
177  double Vector3D<T>::Length() const
178  {
179  return std::sqrt(LengthSquared());
180  }
181 
182  template <class T>
184  {
185  double lLength = Length();
186  x = x / lLength;
187  y = y / lLength;
188  z = z / lLength;
189  }
190 
191  template<typename T>
193  {
194  std::swap(x, iorVector.x);
195  std::swap(y, iorVector.y);
196  std::swap(z, iorVector.z);
197 
198  }
199  template <class T>
201  {
202  return Vector3D<T>(-x, -y, -z);
203  }
204  template <class T>
205  inline Vector3D<T> Vector3D<T>::operator - (const Vector3D<T>& irVector) const
206  {
207  return Vector3D<T>(x - irVector.x, y - irVector.y, z - irVector.z);
208  }
209  template <class T>
210  inline Vector3D<T> Vector3D<T>::operator + (const Vector3D<T>& irVector) const
211  {
212  return Vector3D<T>(x + irVector.x, y + irVector.y, z + irVector.z);
213  }
214  template <class T>
215  Vector3D<T> Vector3D<T>::MultiplyBy(double iFactor) const
216  {
217  return Vector3D<T>(iFactor * x, iFactor * y, iFactor * z);
218  }
219 
222 }
223 #endif
Vector3D< T > MultiplyBy(double iFactor) const
Definition: Vector3D.h:215
T z
Z component of current vector.
Definition: Vector3D.h:127
void Swap(Vector3D< T > &iorVector)
Definition: Vector3D.h:192
Vector3D(double iX=T(), double iY=T(), double iZ=T())
Definition: Vector3D.h:30
void Normalize()
Normalize the current vector.
Definition: Vector3D.h:183
Vector3D< float > FVector3D
Definition: Vector3D.h:220
double Length() const
Definition: Vector3D.h:177
T x
X component of current vector.
Definition: Vector3D.h:125
Definition: Vector3D.h:21
Definition: Point3D.h:21
double IncludedAngle(const Vector3D< T > &irVector) const
Definition: Vector3D.h:168
double LengthSquared() const
Definition: Vector3D.h:132
Vector3D(const Point3D< T > &irPoint)
Definition: Vector3D.h:35
Definition: ABLPlot.h:21
T y
Y component of current vector.
Definition: Vector3D.h:126
double InnerProduct(const Vector3D< T > &irVector) const
Definition: Vector3D.h:139
Vector3D< double > DVector3D
Definition: Vector3D.h:221
double NormalizedInnerProduct(const Vector3D< T > &irVector) const
Definition: Vector3D.h:146
Vector3D< T > operator+(const Vector3D< T > &irVector) const
Definition: Vector3D.h:210
Vector3D(const Point3D< U > &irStart, const Point3D< U > &irEnd)
Definition: Vector3D.h:43
Vector3D< T > operator-() const
Definition: Vector3D.h:200
void CrossProduct(const Vector3D< T > &irVector, Vector3D< T > &orCrossProduct) const
Definition: Vector3D.h:153