TBTK
Vector3d.h
Go to the documentation of this file.
1 /* Copyright 2016 Kristofer Bj√∂rnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_VECTOR_3D
24 #define COM_DAFER45_TBTK_VECTOR_3D
25 
26 #include <cmath>
27 #include <initializer_list>
28 #include <ostream>
29 #include <vector>
30 
31 namespace TBTK{
32 
33 class Vector3d{
34 public:
36  double x;
37 
39  double y;
40 
42  double z;
43 
45  Vector3d();
46 
48  Vector3d(std::initializer_list<double> components);
49 
51  Vector3d(const std::vector<double> &components);
52 
54  ~Vector3d();
55 
57  const Vector3d operator+(const Vector3d &rhs) const;
58 
60  const Vector3d operator-(const Vector3d &rhs) const;
61 
63  const Vector3d operator-() const;
64 
66  const Vector3d operator*(const Vector3d &rhs) const;
67 
69  const Vector3d operator*(double rhs) const;
70 
72  friend const Vector3d operator*(double lhs, const Vector3d &rhs);
73 
75  const Vector3d operator/(double rhs) const;
76 
79  const Vector3d unit() const;
80 
83  const Vector3d perpendicular(const Vector3d &v) const;
84 
87  const Vector3d parallel(const Vector3d &v) const;
88 
90  double norm() const;
91 
93  static double dotProduct(const Vector3d &lhs, const Vector3d &rhs);
94 
96  const std::vector<double> getStdVector() const;
97 
99  friend std::ostream& operator<<(std::ostream &stream, const Vector3d &v);
100 };
101 
102 inline const Vector3d Vector3d::operator+(const Vector3d &rhs) const{
103  Vector3d result;
104 
105  result.x = x + rhs.x;
106  result.y = y + rhs.y;
107  result.z = z + rhs.z;
108 
109  return result;
110 }
111 
112 inline const Vector3d Vector3d::operator-(const Vector3d &rhs) const{
113  Vector3d result;
114 
115  result.x = x - rhs.x;
116  result.y = y - rhs.y;
117  result.z = z - rhs.z;
118 
119  return result;
120 }
121 
122 inline const Vector3d Vector3d::operator-() const{
123  Vector3d result;
124 
125  result.x = -x;
126  result.y = -y;
127  result.z = -z;
128 
129  return result;
130 }
131 
132 inline const Vector3d Vector3d::operator*(const Vector3d &rhs) const{
133  Vector3d result;
134 
135  result.x = y*rhs.z - z*rhs.y;
136  result.y = z*rhs.x - x*rhs.z;
137  result.z = x*rhs.y - y*rhs.x;
138 
139  return result;
140 }
141 
142 inline const Vector3d Vector3d::operator*(double rhs) const{
143  Vector3d result;
144 
145  result.x = x*rhs;
146  result.y = y*rhs;
147  result.z = z*rhs;
148 
149  return result;
150 }
151 
152 inline const Vector3d operator*(double lhs, const Vector3d &rhs){
153  Vector3d result;
154 
155  result.x = lhs*rhs.x;
156  result.y = lhs*rhs.y;
157  result.z = lhs*rhs.z;
158 
159  return result;
160 }
161 
162 inline const Vector3d Vector3d::operator/(double rhs) const{
163  Vector3d result;
164 
165  result.x = x/rhs;
166  result.y = y/rhs;
167  result.z = z/rhs;
168 
169  return result;
170 }
171 
172 inline const Vector3d Vector3d::unit() const{
173  return (*this)/norm();
174 }
175 
176 inline const Vector3d Vector3d::perpendicular(const Vector3d &v) const{
177  return *this - dotProduct(*this, v.unit())*v.unit();
178 }
179 
180 inline const Vector3d Vector3d::parallel(const Vector3d &v) const{
181  return dotProduct(*this, v.unit())*v.unit();
182 }
183 
184 inline double Vector3d::norm() const{
185  return sqrt(x*x + y*y + z*z);
186 }
187 
188 inline double Vector3d::dotProduct(const Vector3d &lhs, const Vector3d &rhs){
189  return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
190 }
191 
192 inline const std::vector<double> Vector3d::getStdVector() const{
193  std::vector<double> result;
194 
195  result.push_back(x);
196  result.push_back(y);
197  result.push_back(z);
198 
199  return result;
200 }
201 
202 inline std::ostream& operator<<(std::ostream &stream, const Vector3d &v){
203  stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
204 
205  return stream;
206 }
207 
208 }; //End namespace TBTK
209 
210 #endif
const Vector3d perpendicular(const Vector3d &v) const
Definition: Vector3d.h:176
const Vector3d operator-() const
Definition: Vector3d.h:122
const Vector3d unit() const
Definition: Vector3d.h:172
double norm() const
Definition: Vector3d.h:184
const Vector3d parallel(const Vector3d &v) const
Definition: Vector3d.h:180
double z
Definition: Vector3d.h:42
const Vector3d operator*(const Vector3d &rhs) const
Definition: Vector3d.h:132
double y
Definition: Vector3d.h:39
Definition: Vector3d.h:33
Definition: ModelFactory.h:35
double x
Definition: Vector3d.h:36
const std::vector< double > getStdVector() const
Definition: Vector3d.h:192
static double dotProduct(const Vector3d &lhs, const Vector3d &rhs)
Definition: Vector3d.h:188
const Vector3d operator/(double rhs) const
Definition: Vector3d.h:162
const Vector3d operator+(const Vector3d &rhs) const
Definition: Vector3d.h:102
friend std::ostream & operator<<(std::ostream &stream, const Vector3d &v)
Definition: Vector3d.h:202