vector3D.h
Go to the documentation of this file.00001
00015 #ifndef _DLR_VECTOR3D_H_
00016 #define _DLR_VECTOR3D_H_
00017
00018 #include <iostream>
00019 #include <dlrCommon/exception.h>
00020
00021 namespace dlr {
00022
00023 namespace numeric {
00024
00028 class Vector3D {
00029 public:
00033 Vector3D()
00034 : m_x(0), m_y(0), m_z(0) {}
00035
00046 Vector3D(double xCoord, double yCoord, double zCoord)
00047 : m_x(xCoord), m_y(yCoord), m_z(zCoord) {}
00048
00059 Vector3D(double xCoord, double yCoord, double zCoord, double alpha)
00060 : m_x(xCoord), m_y(yCoord), m_z(zCoord) {this->normalize(alpha);}
00061
00067 Vector3D(const Vector3D& source)
00068 : m_x(source.m_x), m_y(source.m_y), m_z(source.m_z) {}
00069
00070
00074 ~Vector3D() {}
00075
00076
00082 inline Vector3D& clear() {
00083 m_x = 0.0; m_y = 0.0; m_z = 0.0;
00084 return *this;
00085 }
00086
00087
00095 inline void setValue(double xCoord, double yCoord, double zCoord) {
00096 m_x = xCoord; m_y = yCoord; m_z = zCoord;
00097 }
00098
00107 inline void setValue(double xCoord, double yCoord, double zCoord,
00108 double alpha) {
00109 m_x = xCoord; m_y = yCoord; m_z = zCoord; normalize(alpha);
00110 }
00111
00117 inline double& x() {return m_x;}
00118
00124 inline double x() const {return m_x;}
00125
00131 inline double& y() {return m_y;}
00132
00138 inline double y() const {return m_y;}
00139
00145 inline double& z() {return m_z;}
00146
00152 inline double z() const {return m_z;}
00153
00160 Vector3D& operator=(const Vector3D& source) {
00161 setValue(source.m_x, source.m_y, source.m_z); return *this;
00162 }
00163
00170 Vector3D& operator*=(double scalar) {
00171 m_x *= scalar; m_y *= scalar; m_z *= scalar; return *this;
00172 }
00173
00180 Vector3D& operator/=(double scalar) {
00181 if (scalar == 0) {
00182 DLR_THROW(ValueException, "Vector3D::operator/=(double)",
00183 "Can't divide by zero.");
00184 }
00185 m_x /= scalar; m_y /= scalar; m_z /= scalar; return *this;
00186 }
00187
00194 Vector3D& operator+=(const Vector3D& vec) {
00195 m_x += vec.m_x; m_y += vec.m_y; m_z += vec.m_z; return *this;
00196 }
00197
00204 Vector3D& operator-=(const Vector3D& vec) {
00205 m_x -= vec.m_x; m_y -= vec.m_y; m_z -= vec.m_z; return *this;
00206 }
00207
00213 Vector3D operator-() {
00214 return Vector3D(-m_x, -m_y, -m_z);
00215 }
00216
00217 private:
00218
00219 inline void normalize(double alpha) {
00220 if(alpha == 1.0) {return;}
00221 if(alpha == 0.0) {
00222 DLR_THROW(ValueException, "Vector3D::normalize()",
00223 "Bad alpha (0.0).");
00224 }
00225 m_x /= alpha; m_y /= alpha; m_z /= alpha;
00226 return;
00227 }
00228
00229
00230 double m_x;
00231 double m_y;
00232 double m_z;
00233 };
00234
00235
00236
00237
00248 Vector3D
00249 operator+(const Vector3D& vector0, const Vector3D& vector1);
00250
00263 Vector3D
00264 operator-(const Vector3D& vector0, const Vector3D& vector1);
00265
00276 Vector3D
00277 operator*(const Vector3D& vector0, const Vector3D& vector1);
00278
00290 Vector3D
00291 operator/(const Vector3D& vector0, const Vector3D& vector1);
00292
00304 Vector3D operator+(const Vector3D& vector0, double scalar0);
00305
00317 Vector3D operator-(const Vector3D& vector0, double scalar0);
00318
00330 Vector3D operator*(const Vector3D& vector0, double scalar0);
00331
00343 Vector3D operator/(const Vector3D& vector0, double scalar0);
00344
00352 bool operator==(const Vector3D& vector0, const Vector3D& vector1);
00353
00362 bool operator!=(const Vector3D& vector0, const Vector3D& vector1);
00363
00364
00377 Vector3D operator+(double scalar0, const Vector3D& vector0);
00378
00379
00392 Vector3D operator*(double scalar0, const Vector3D& vector0);
00393
00394
00410 std::ostream& operator<<(std::ostream& stream, const Vector3D& vector0);
00411
00412
00427 std::istream& operator>>(std::istream& stream, Vector3D& vector0);
00428
00429 }
00430
00431 }
00432
00433
00434
00435
00436 namespace dlr {
00437
00438 using numeric::Vector3D;
00439
00440 }
00441
00442
00443
00444 namespace dlr {
00445
00446 namespace numeric {
00447
00448 inline Vector3D operator+(double scalar0, const Vector3D& vector0)
00449 {
00450 return vector0 + scalar0;
00451 }
00452
00453 inline Vector3D operator*(double scalar0, const Vector3D& vector0)
00454 {
00455 return vector0 * scalar0;
00456 }
00457
00458 }
00459
00460 }
00461
00462 #endif // #ifndef _DLR_VECTOR3D_H_