OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Vector3D.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Vector3D.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Vector3D
10 // Vector3D represents an 3-dimension vector.
11 //
12 // ------------------------------------------------------------------------
13 // Class category: BeamlineGeometry
14 // ------------------------------------------------------------------------
15 //
16 // $Date: 2000/03/27 09:32:34 $
17 // $Author: fci $
18 //
19 // ------------------------------------------------------------------------
20 
22 
23 
24 // Class Vector3D
25 // ------------------------------------------------------------------------
26 
27 Vector3D::Vector3D(double x, double y, double z) {
28  v[0] = x;
29  v[1] = y;
30  v[2] = z;
31 }
32 
33 
35  v[0] += rhs.v[0];
36  v[1] += rhs.v[1];
37  v[2] += rhs.v[2];
38  return *this;
39 }
40 
41 
43  v[0] -= rhs.v[0];
44  v[1] -= rhs.v[1];
45  v[2] -= rhs.v[2];
46  return *this;
47 }
48 
49 
51  v[0] *= rhs;
52  v[1] *= rhs;
53  v[2] *= rhs;
54  return *this;
55 }
56 
57 
58 bool Vector3D::operator==(const Vector3D &rhs) const {
59  return (v[0] == rhs.v[0]) && (v[1] == rhs.v[1]) && (v[0] == rhs.v[1]);
60 }
61 
62 
63 bool Vector3D::operator!=(const Vector3D &rhs) const {
64  return (v[0] != rhs.v[0]) || (v[1] != rhs.v[1]) || (v[0] != rhs.v[1]);
65 }
66 
67 
69  return Vector3D(- v[0], - v[1], - v[2]);
70 }
71 
72 
73 Vector3D operator+(const Vector3D &lhs, const Vector3D &rhs) {
74  return Vector3D(lhs(0) + rhs(0), lhs(1) + rhs(1), lhs(2) + rhs(2));
75 }
76 
77 
78 Vector3D operator-(const Vector3D &lhs, const Vector3D &rhs) {
79  return Vector3D(lhs(0) - rhs(0), lhs(1) - rhs(1), lhs(2) - rhs(2));
80 }
81 
82 
83 Vector3D operator*(const Vector3D &lhs, double rhs) {
84  return Vector3D(lhs(0) * rhs, lhs(1) * rhs, lhs(2) * rhs);
85 }
86 
87 
88 Vector3D operator*(double lhs, const Vector3D &rhs) {
89  return Vector3D(lhs * rhs(0), lhs * rhs(1), lhs * rhs(2));
90 }
91 
92 
94  v[0] = v[1] = v[2] = 0.0;
95 }
96 
97 
98 void Vector3D::getComponents(double &x, double &y, double &z)
99 const {
100  x = v[0];
101  y = v[1];
102  z = v[2];
103 }
104 
105 
106 bool Vector3D::isZero() const {
107  return v[0] == 0.0 && v[1] == 0.0 && v[2] == 0.0;
108 }
109 
110 
111 Vector3D cross(const Vector3D &lhs, const Vector3D &rhs) {
112  return Vector3D(lhs(1) * rhs(2) - lhs(2) * rhs(1),
113  lhs(2) * rhs(0) - lhs(0) * rhs(2),
114  lhs(0) * rhs(1) - lhs(1) * rhs(0));
115 }
116 
117 
118 double dot(const Vector3D &lhs, const Vector3D &rhs) {
119  return (lhs(0) * rhs(0) + lhs(1) * rhs(1) + lhs(2) * rhs(2));
120 }
Matrix< T > operator+(const Matrix< T > &, const Matrix< T > &)
Matrix addition.
Definition: Matrix.h:275
void clear()
Set to zero.
Definition: Vector3D.cpp:93
bool operator!=(const Vector3D &) const
Definition: Vector3D.cpp:63
Matrix< T > operator*(const Matrix< T > &, const Matrix< T > &)
Matrix multiply.
Definition: Matrix.h:297
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
bool operator==(const Vector3D &) const
Definition: Vector3D.cpp:58
double v[3]
Definition: Vector3D.h:103
Vector3D & operator-=(const Vector3D &vector)
Subtract and assign.
Definition: Vector3D.cpp:42
bool isZero() const
Test for zero.
Definition: Vector3D.cpp:106
Vector3D cross(const Vector3D &lhs, const Vector3D &rhs)
Vector cross product.
Definition: Vector3D.cpp:111
Vector3D()
Default constructor.
Definition: Vector3D.h:132
void getComponents(double &x, double &y, double &z) const
Get components.
Definition: Vector3D.cpp:98
Vector3D & operator*=(double factor)
Scale and assign.
Definition: Vector3D.cpp:50
A 3-dimension vector.
Definition: Vector3D.h:31
Vector3D operator-() const
Negative vector.
Definition: Vector3D.cpp:68
Vector3D & operator+=(const Vector3D &vector)
Add and assign.
Definition: Vector3D.cpp:34
Matrix< T > operator-(const Matrix< T > &, const Matrix< T > &)
Matrix subtraction.
Definition: Matrix.h:282