OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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
27Vector3D::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
58bool 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
63bool 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
73Vector3D 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
78Vector3D 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
83Vector3D operator*(const Vector3D &lhs, double rhs) {
84 return Vector3D(lhs(0) * rhs, lhs(1) * rhs, lhs(2) * rhs);
85}
86
87
88Vector3D 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
98void Vector3D::getComponents(double &x, double &y, double &z)
99const {
100 x = v[0];
101 y = v[1];
102 z = v[2];
103}
104
105
106bool Vector3D::isZero() const {
107 return v[0] == 0.0 && v[1] == 0.0 && v[2] == 0.0;
108}
109
110
111Vector3D 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
118double dot(const Vector3D &lhs, const Vector3D &rhs) {
119 return (lhs(0) * rhs(0) + lhs(1) * rhs(1) + lhs(2) * rhs(2));
120}
Vector3D cross(const Vector3D &lhs, const Vector3D &rhs)
Vector cross product.
Definition: Vector3D.cpp:111
Vector3D operator+(const Vector3D &lhs, const Vector3D &rhs)
Add.
Definition: Vector3D.cpp:73
Vector3D operator-(const Vector3D &lhs, const Vector3D &rhs)
Subtract.
Definition: Vector3D.cpp:78
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
Vector3D operator*(const Vector3D &lhs, double rhs)
Multiply.
Definition: Vector3D.cpp:83
A 3-dimension vector.
Definition: Vector3D.h:31
Vector3D()
Default constructor.
Definition: Vector3D.h:132
bool operator!=(const Vector3D &) const
Definition: Vector3D.cpp:63
void clear()
Set to zero.
Definition: Vector3D.cpp:93
Vector3D & operator-=(const Vector3D &vector)
Subtract and assign.
Definition: Vector3D.cpp:42
Vector3D & operator*=(double factor)
Scale and assign.
Definition: Vector3D.cpp:50
void getComponents(double &x, double &y, double &z) const
Get components.
Definition: Vector3D.cpp:98
bool operator==(const Vector3D &) const
Definition: Vector3D.cpp:58
Vector3D & operator+=(const Vector3D &vector)
Add and assign.
Definition: Vector3D.cpp:34
bool isZero() const
Test for zero.
Definition: Vector3D.cpp:106
double v[3]
Definition: Vector3D.h:103
Vector3D operator-() const
Negative vector.
Definition: Vector3D.cpp:68