scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
vector_unit_3d.cpp
Go to the documentation of this file.
2
3#include "../coordinates/coordinates_3d.hpp"
4#include "../util/types.hpp"
5
6using namespace std;
7
8namespace scifir
9{
12
13 vector_unit_3d::vector_unit_3d(const vector_unit_3d& x) : scalar_unit(x),theta(x.theta),phi(x.phi)
14 {}
15
16 vector_unit_3d::vector_unit_3d(vector_unit_3d&& x) : scalar_unit(std::move(x)),theta(std::move(x.theta)),phi(std::move(x.phi))
17 {}
18
21
24
27
30
33
36
39
42
45
48
51
54
57
60
63
66
69
72
75
78
81
84
87
90
93
96
99
102
107
109 {
111 theta = x.theta;
112 phi = x.phi;
113 return *this;
114 }
115
117 {
118 scalar_unit::operator =(std::move(x));
119 theta = std::move(x.theta);
120 phi = std::move(x.phi);
121 return *this;
122 }
123
125 {
127 return *this;
128 }
129
131 {
132 scalar_unit::operator =(std::move(x));
133 return *this;
134 }
135
141
143 {
144 x.change_dimensions(*this);
146 {
147 return true;
148 }
149 else
150 {
151 return false;
152 }
153 }
154
156 {
157 if (x == direction::LEFT)
158 {
159 theta = 270.0f;
160 phi = 90.0f;
161 }
162 else if(x == direction::RIGHT)
163 {
164 theta = 90.0f;
165 phi = 90.0f;
166 }
167 else if(x == direction::TOP)
168 {
169 theta = 0.0f;
170 phi = 0.0f;
171 }
172 else if(x == direction::BOTTOM)
173 {
174 theta = 0.0f;
175 phi = 180.0f;
176 }
177 else if(x == direction::LEFT_TOP)
178 {
179 theta = 270.0f;
180 phi = 45.0f;
181 }
182 else if(x == direction::RIGHT_TOP)
183 {
184 theta = 90.0f;
185 phi = 45.0f;
186 }
187 else if(x == direction::RIGHT_BOTTOM)
188 {
189 theta = 90.0f;
190 phi = 135.0f;
191 }
192 else if(x == direction::LEFT_BOTTOM)
193 {
194 theta = 270.0f;
195 phi = 135.0f;
196 }
197 else if(x == direction::FRONT)
198 {
199 theta = 0.0f;
200 phi = 90.0f;
201 }
202 else if(x == direction::BACK)
203 {
204 theta = 180.0f;
205 phi = 90.0f;
206 }
207 else if(x == direction::LEFT_FRONT)
208 {
209 theta = 315.0f;
210 phi = 90.0f;
211 }
212 else if(x == direction::RIGHT_FRONT)
213 {
214 theta = 45.0f;
215 phi = 90.0f;
216 }
217 else if(x == direction::TOP_FRONT)
218 {
219 theta = 0.0f;
220 phi = 45.0f;
221 }
222 else if(x == direction::BOTTOM_FRONT)
223 {
224 theta = 0.0f;
225 phi = 135.0f;
226 }
227 else if(x == direction::LEFT_BACK)
228 {
229 theta = 225.0f;
230 phi = 90.0f;
231 }
232 else if(x == direction::RIGHT_BACK)
233 {
234 theta = 135.0f;
235 phi = 90.0f;
236 }
237 else if(x == direction::TOP_BACK)
238 {
239 theta = 180.0f;
240 phi = 45.0f;
241 }
242 else if(x == direction::BOTTOM_BACK)
243 {
244 theta = 180.0f;
245 phi = 135.0f;
246 }
247 else if(x == direction::LEFT_TOP_FRONT)
248 {
249 theta = 315.0f;
250 phi = 45.0f;
251 }
252 else if(x == direction::RIGHT_TOP_FRONT)
253 {
254 theta = 45.0f;
255 phi = 45.0f;
256 }
257 else if(x == direction::LEFT_BOTTOM_FRONT)
258 {
259 theta = 315.0f;
260 phi = 135.0f;
261 }
263 {
264 theta = 45.0f;
265 phi = 135.0f;
266 }
267 else if(x == direction::LEFT_TOP_BACK)
268 {
269 theta = 225.0f;
270 phi = 45.0f;
271 }
272 else if(x == direction::RIGHT_TOP_BACK)
273 {
274 theta = 135.0f;
275 phi = 45.0f;
276 }
277 else if(x == direction::LEFT_BOTTOM_BACK)
278 {
279 theta = 225.0f;
280 phi = 135.0f;
281 }
282 else if(x == direction::RIGHT_BOTTOM_BACK)
283 {
284 theta = 135.0f;
285 phi = 135.0f;
286 }
287 }
288
290 {
291 if(has_dimensions(x))
292 {
293 float new_x = float(x_projection() + x.x_projection());
294 float new_y = float(y_projection() + x.y_projection());
295 float new_z = float(z_projection() + x.z_projection());
299 }
300 else
301 {
302 cerr << "Cannot sum vectors of different dimensions" << endl;
303 }
304 }
305
307 {
308 if(has_dimensions(x))
309 {
310 x.invert();
311 *this += x;
312 }
313 else
314 {
315 cerr << "Cannot substract vectors of different dimensions" << endl;
316 }
317 }
318
336
355
362
369
371 {
373 {
376 }
377 else
378 {
379 cerr << "Cannot power with as exponent a unit with dimensions" << endl;
380 return vector_unit_3d();
381 }
382 }
383
384#ifdef IS_UNIX
386 {
389 return out.str();
390 }
391
392 string vector_unit_3d::vectorial_base_display(int number_of_decimals) const
393 {
394 ostringstream out;
395 out << base_display(number_of_decimals) << " " << display_float(theta.get_value(),number_of_decimals) << "θ " << display_float(phi.get_value(),number_of_decimals) << "Φ";
396 return out.str();
397 }
398
399 string vector_unit_3d::vectorial_custom_display(const string& init_dimensions,int number_of_decimals) const
400 {
401 ostringstream out;
402 out << custom_display(init_dimensions,number_of_decimals) << " " << display_float(theta.get_value(),number_of_decimals) << "θ " << display_float(phi.get_value(),number_of_decimals) << "Φ";
403 return out.str();
404 }
405
406 string to_string(const vector_unit_3d& x)
407 {
408 return x.vectorial_display(2);
409 }
410#elif IS_WINDOWS
411 string vector_unit_3d::vectorial_display(int number_of_decimals) const
412 {
413 ostringstream out;
414 out << display(number_of_decimals) << " " << display_float(theta.get_value(),number_of_decimals) << "θ " << display_float(phi.get_value(),number_of_decimals) << "Φ";
415 return out.str();
416 }
417
418 string vector_unit_3d::vectorial_base_display(int number_of_decimals) const
419 {
420 ostringstream out;
421 out << base_display(number_of_decimals) << " " << display_float(theta.get_value(),number_of_decimals) << "θ " << display_float(phi.get_value(),number_of_decimals) << "Φ";
422 return out.str();
423 }
424
425 string vector_unit_3d::vectorial_custom_display(const string& init_dimensions,int number_of_decimals) const
426 {
427 ostringstream out;
428 out << custom_display(init_dimensions,number_of_decimals) << " " << display_float(theta.get_value(),number_of_decimals) << "θ " << display_float(phi.get_value(),number_of_decimals) << "Φ";
429 return out.str();
430 }
431
432 string to_string(const vector_unit_3d& x)
433 {
434 return x.vectorial_display(2);
435 }
436#endif
437
439 {
440 vector<string> values;
441 boost::split(values,init_vector_3d,boost::is_any_of(" "));
442 if (values.size() == 4)
443 {
444 scalar_unit::initialize_from_string(values[0] + " " + values[1],vector<dimension>());
445 theta = angle(values[2]);
446 phi = angle(values[3]);
447 }
448 }
449
451 {
452 return scalar_unit(std::abs(x.get_value()),x.get_dimensions());
453 }
454
460
462 {
464 return vector_unit_3d(new_value, x.theta, x.phi);
465 }
466
473
495
497 {
498 return scifir::acos(float(dot_product(x,y)/(norm(x)*norm(y))));
499 }
500
502 {
503 if (x.theta == y.theta and x.phi == y.phi)
504 {
505 return true;
506 }
507 else
508 {
509 return false;
510 }
511 }
512
513 bool parallel(const vector_unit_3d& x, const vector_unit_3d& y)
514 {
515 if ((x.theta == y.theta and x.phi == y.phi) or (x.theta == (180.0f + y.theta) and x.phi == (180.0f - y.phi)))
516 {
517 return true;
518 }
519 else
520 {
521 return false;
522 }
523 }
524
526 {
528 return (x_angle == 90.0f);
529 }
530}
531
533{
534 long double new_value = x.get_value() * y.get_value();
535 vector<scifir::dimension> new_dimensions = multiply_dimensions(x.get_dimensions(), y.get_dimensions(),new_value);
536 return scifir::vector_unit_3d(float(new_value), new_dimensions, y.theta, y.phi);
537}
538
540{
541 return !(x == y);
542}
543
544bool operator ==(const scifir::vector_unit_3d& x, const string& init_vector_3d)
545{
546 scifir::vector_unit_3d y(init_vector_3d);
547 return (x == y);
548}
549
550bool operator !=(const scifir::vector_unit_3d& x, const string& init_vector_3d)
551{
552 return !(x == init_vector_3d);
553}
554
555bool operator ==(const string& init_vector_3d, const scifir::vector_unit_3d& y)
556{
557 scifir::vector_unit_3d x(init_vector_3d);
558 return (x == y);
559}
560
561bool operator !=(const string& init_vector_3d, const scifir::vector_unit_3d& y)
562{
563 return !(init_vector_3d == y);
564}
565
566void operator +=(string& x, const scifir::vector_unit_3d& y)
567{
568 ostringstream output;
569 output << y;
570 x += output.str();
571}
572
573string operator +(const string& x, const scifir::vector_unit_3d& y)
574{
575 ostringstream output;
576 output << x;
577 output << y;
578 return output.str();
579}
580
581string operator +(const scifir::vector_unit_3d& y, const string& x)
582{
583 ostringstream output;
584 output << y;
585 output << x;
586 return output.str();
587}
588
589ostream& operator <<(ostream& os, const scifir::vector_unit_3d& x)
590{
591 return os << to_string(x);
592}
593
594istream& operator >>(istream& is, scifir::vector_unit_3d& x)
595{
596 char a[256];
597 is.getline(a, 256);
598 string b(a);
599 boost::trim(b);
601 return is;
602}
Class that allows to work with angles. Each angle sizes 4 bytes. Initialization string example: "20°"...
Definition angle.hpp:77
const float & get_value() const
Gets the value of the angle, in degrees.
Definition angle.hpp:102
position
Represents the position of the dimension, which can be at the numerator or at the denominator....
Definition dimension.hpp:38
type
Represents a dimension of the SI system of units. All the dimensions of the SI system of units are su...
Definition dimension.hpp:34
type
Represents a prefix of the SI system of units. All the prefixes of the SI system of units are support...
Definition prefix.hpp:16
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
bool has_empty_dimensions() const
Checks if there aren't base dimensions.
scalar_unit & operator=(const scalar_unit &x)
Copy assignment, it assigns a copy of the scalar_unit.
const vector< dimension > & get_dimensions() const
Read-only getter of the dimensions.
bool has_dimensions(const string &init_dimensions) const
Checks if the basic dimensions are the same as the initialization string of dimensions.
string display(int number_of_decimals=2, bool with_brackets=false, bool use_close_prefix=false) const
Generates a string representation of the scalar_unit, with the value and the dimensions....
float value
Value of the scalar_unit. It changes automatically when the dimensions change.
void change_dimensions(const string &init_dimensions)
Changes the dimensions to the dimensions specified by the initialization string of dimensions.
void initialize_from_string(string init_scalar, const vector< dimension > &real_dimensions)
Internal function. It sets the value and the dimensions of the scalar_unit to the value and dimension...
string custom_display(const string &init_dimensions, int number_of_decimals=2, bool with_brackets=false) const
Generates a string representation of the scalar_unit, with the dimensions changed to any set of dimen...
string base_display(int number_of_decimals=2, bool with_brackets=false, bool use_close_prefix=false) const
Generates a string representation of the scalar_unit, with its dimensions converted to their base cou...
const float & get_value() const
Read-only getter of the value.
scalar_unit operator^(const scalar_unit &x) const
Power operator, it powers a scalar_unit class with another, if that second scalar_unit class,...
Class that creates a vector unit in 3D. The vector is in spherical coordinates with a value and dimen...
void operator+=(const vector_unit_3d &x)
The vector_unit_3d is summed as vector, in spherical coordinates. The addition of vectors is used for...
void operator-=(vector_unit_3d x)
The vector_unit_3d is substracted as vector, in spherical coordinates. The substraction of vectors is...
vector_unit_3d operator/(const scalar_unit &x) const
It creates a new vector_unit_3d scaling a vector_unit_3d by the inverse of the scalar_unit x.
void invert()
Changes the direction of the vector to the opposite direction. It does that by adding 180 degrees to ...
scalar_unit y_projection() const
It creates the y projection of the vector, returning it as a scalar_unit of the same dimensions,...
void initialize_from_string(string init_vector_3d)
Initializes the member-variables with the initialization string of vector_unit_3d init_vector_3d.
void point_to(direction::name x)
Theta is set to the direction specified in 3D. Possible values are LEFT, RIGHT, TOP,...
vector_unit_3d operator^(const scalar_unit &x) const
It powers a vector by a scalar_unit x if that scalar_unit has empty dimensions.
scalar_unit z_projection() const
It creates the z projection of the vector, returning it as a scalar_unit of the same dimensions,...
string vectorial_custom_display(const string &init_dimensions, int number_of_decimals=2) const
Displays the vector as the string representation of the scalar unit adding also the angle theta and t...
scalar_unit x_projection() const
It creates the x projection of the vector, returning it as a scalar_unit of the same dimensions,...
string vectorial_base_display(int number_of_decimals=2) const
Displays the vector as the string representation of the scalar unit adding also the angle theta and t...
vector_unit_3d & operator=(const vector_unit_3d &x)
Copy assignment. The member-variables are copied from the vector_unit_3d x.
vector_unit_3d()
Default constructor. The value is set to 0, the dimensions are empty and theta and phi are 0.
angle theta
Angle theta of the vector in 3D space, in spherical coordinates. As all angles of scifir-units,...
vector_unit_3d operator-(vector_unit_3d x) const
Substraction of vectors in 3D. It creates a new vector as the difference of the other two.
bool operator==(vector_unit_3d x) const
Comparison operator. Two vector_unit_3d are equal if their value, dimensions, theta and phi are the s...
vector_unit_3d operator+(const vector_unit_3d &x) const
Addition of vectors in 3D. It creates a new vector as the addition of the other two.
string vectorial_display(int number_of_decimals=2) const
Displays the vector as the string representation of the scalar unit adding also the angle theta and t...
vector_unit_3d operator*(const scalar_unit &x) const
It creates a new vector_unit_3d scaling a vector_unit_3d by the scalar_unit x.
angle phi
Angle phi of the vector in 3D space, in spherical coordinates. As all angles of scifir-units,...
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
angle angle_between(const vector_unit_2d &x, const vector_unit_2d &y)
Returns the angle between two vectors x and y inside a 2D space.
angle sqrt_nth(const angle &x, int index)
Calculates the nth root of the angle x and returns that new angle.
Definition angle.cpp:421
bool same_direction(const vector_unit_2d &x, const vector_unit_2d &y)
Checks if two vectors x and y have the same direction.
vector< dimension > divide_dimensions(vector< dimension > x, const vector< dimension > &y, long double &value)
Divides the first vector of dimensions with the other. The result is normalized after,...
angle cartesian_3d_to_spherical_theta(const scalar_unit &x, scalar_unit y, const scalar_unit &z)
Returns the theta coordinate of the spherical coordinates given the x, y and z of cartesian coordinat...
bool orthogonal(const angle &x, const angle &y)
Checks if two angles in a 2D correspond to orthogonal lines (or orthogonal vectors).
Definition angle.cpp:403
string to_string(const aid &x)
Creates a string representation of aid, it's for aid equivalent to the display() function of aid.
Definition aid.cpp:582
string display_float(const float &value, int number_of_decimals)
Definition types.cpp:36
scalar_unit dot_product(const vector_unit_2d &x, const vector_unit_2d &y)
Creates a scalar_unit as the dot product of the two vectors x and y.
scalar_unit cartesian_3d_to_spherical_r(const scalar_unit &x, scalar_unit y, scalar_unit z)
Returns the r coordinate of the spherical coordinates given the x, y and z of cartesian coordinates i...
vector< dimension > multiply_dimensions(const vector< dimension > &x, const vector< dimension > &y)
Multiplies two vectors of dimensions. The result is normalized after, which means that equal dimensio...
angle cartesian_3d_to_spherical_phi(const scalar_unit &x, scalar_unit y, scalar_unit z)
Returns the phi coordinate of the spherical coordinates given the x, y and z of cartesian coordinates...
vector_unit_3d cross_product(const vector_unit_3d &x, const vector_unit_3d &y)
Creates a vector_unit_3d as the cross product of the two vectors x and y.
scalar_unit norm(const vector_unit_2d &x)
It returns the value of the vector in polar coordinates, p.
angle acos(float x)
Calculates the acos of some value x and returns the result as angle in degrees.
Definition angle.cpp:446
angle sqrt(const angle &x)
Calculates the square root of the angle x and returns that new angle.
Definition angle.cpp:416
bool parallel(const angle &x, const angle &y)
Checks if two angles in a 2D correspond to parallel lines (or parallel vectors).
Definition angle.cpp:391
bool operator==(const scifir::vector_unit_3d &x, const string &init_vector_3d)
Returns true if x is equal to the vector_unit_3d initialized with the string being compared....
scifir::vector_unit_3d operator*(const scifir::scalar_unit &x, const scifir::vector_unit_3d &y)
It creates a new vector_unit_3d scaling a vector_unit_3d by the scalar_unit x.
istream & operator>>(istream &is, scifir::vector_unit_3d &x)
Allows that an istream initializes by string a vector_unit_3d x.
ostream & operator<<(ostream &os, const scifir::vector_unit_3d &x)
Adds the string representation of the vector_unit_3d x to an output stream os.
bool operator!=(const scifir::vector_unit_3d &x, const scifir::vector_unit_3d &y)
Comparison operator. Two vector_unit_3d are not equal if their value, dimensions, theta or phi are di...
string operator+(const string &x, const scifir::vector_unit_3d &y)
Creates a new string as the concatenation of the string x with the representation string of the vecto...
void operator+=(string &x, const scifir::vector_unit_3d &y)
Concatenates the string representation of the vector_unit_3d y to the string x.