TBTK
Need a break? Support the development by playing Polarity Puzzles
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  const Vector3d operator+(const Vector3d &rhs) const;
55 
57  const Vector3d operator-(const Vector3d &rhs) const;
58 
60  const Vector3d operator-() const;
61 
63  const Vector3d operator*(const Vector3d &rhs) const;
64 
66  const Vector3d operator*(double rhs) const;
67 
69  friend const Vector3d operator*(double lhs, const Vector3d &rhs);
70 
72  const Vector3d operator/(double rhs) const;
73 
76  const Vector3d unit() const;
77 
80  const Vector3d perpendicular(const Vector3d &v) const;
81 
84  const Vector3d parallel(const Vector3d &v) const;
85 
87  double norm() const;
88 
90  static double dotProduct(const Vector3d &lhs, const Vector3d &rhs);
91 
93  const std::vector<double> getStdVector() const;
94 
96  friend std::ostream& operator<<(std::ostream &stream, const Vector3d &v);
97 };
98 
99 inline const Vector3d Vector3d::operator+(const Vector3d &rhs) const{
100  Vector3d result;
101 
102  result.x = x + rhs.x;
103  result.y = y + rhs.y;
104  result.z = z + rhs.z;
105 
106  return result;
107 }
108 
109 inline const Vector3d Vector3d::operator-(const Vector3d &rhs) const{
110  Vector3d result;
111 
112  result.x = x - rhs.x;
113  result.y = y - rhs.y;
114  result.z = z - rhs.z;
115 
116  return result;
117 }
118 
119 inline const Vector3d Vector3d::operator-() const{
120  Vector3d result;
121 
122  result.x = -x;
123  result.y = -y;
124  result.z = -z;
125 
126  return result;
127 }
128 
129 inline const Vector3d Vector3d::operator*(const Vector3d &rhs) const{
130  Vector3d result;
131 
132  result.x = y*rhs.z - z*rhs.y;
133  result.y = z*rhs.x - x*rhs.z;
134  result.z = x*rhs.y - y*rhs.x;
135 
136  return result;
137 }
138 
139 inline const Vector3d Vector3d::operator*(double rhs) const{
140  Vector3d result;
141 
142  result.x = x*rhs;
143  result.y = y*rhs;
144  result.z = z*rhs;
145 
146  return result;
147 }
148 
149 inline const Vector3d operator*(double lhs, const Vector3d &rhs){
150  Vector3d result;
151 
152  result.x = lhs*rhs.x;
153  result.y = lhs*rhs.y;
154  result.z = lhs*rhs.z;
155 
156  return result;
157 }
158 
159 inline const Vector3d Vector3d::operator/(double rhs) const{
160  Vector3d result;
161 
162  result.x = x/rhs;
163  result.y = y/rhs;
164  result.z = z/rhs;
165 
166  return result;
167 }
168 
169 inline const Vector3d Vector3d::unit() const{
170  return (*this)/norm();
171 }
172 
173 inline const Vector3d Vector3d::perpendicular(const Vector3d &v) const{
174  return *this - dotProduct(*this, v.unit())*v.unit();
175 }
176 
177 inline const Vector3d Vector3d::parallel(const Vector3d &v) const{
178  return dotProduct(*this, v.unit())*v.unit();
179 }
180 
181 inline double Vector3d::norm() const{
182  return sqrt(x*x + y*y + z*z);
183 }
184 
185 inline double Vector3d::dotProduct(const Vector3d &lhs, const Vector3d &rhs){
186  return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
187 }
188 
189 inline const std::vector<double> Vector3d::getStdVector() const{
190  std::vector<double> result;
191 
192  result.push_back(x);
193  result.push_back(y);
194  result.push_back(z);
195 
196  return result;
197 }
198 
199 inline std::ostream& operator<<(std::ostream &stream, const Vector3d &v){
200  stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
201 
202  return stream;
203 }
204 
205 }; //End namespace TBTK
206 
207 #endif
const Vector3d perpendicular(const Vector3d &v) const
Definition: Vector3d.h:173
const std::vector< double > getStdVector() const
Definition: Vector3d.h:189
const Vector3d parallel(const Vector3d &v) const
Definition: Vector3d.h:177
const Vector3d operator/(double rhs) const
Definition: Vector3d.h:159
const Vector3d operator+(const Vector3d &rhs) const
Definition: Vector3d.h:99
double z
Definition: Vector3d.h:42
const Vector3d operator*(const Vector3d &rhs) const
Definition: Vector3d.h:129
double norm() const
Definition: Vector3d.h:181
double y
Definition: Vector3d.h:39
Definition: Vector3d.h:33
Definition: Boolean.h:32
double x
Definition: Vector3d.h:36
static double dotProduct(const Vector3d &lhs, const Vector3d &rhs)
Definition: Vector3d.h:185
const Vector3d operator-() const
Definition: Vector3d.h:119
const Vector3d unit() const
Definition: Vector3d.h:169
friend std::ostream & operator<<(std::ostream &stream, const Vector3d &v)
Definition: Vector3d.h:199