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_2d.cpp
Go to the documentation of this file.
2
3#include "../coordinates/coordinates_2d.hpp"
4#include "../util/types.hpp"
5
6#include "boost/algorithm/string.hpp"
7
8using namespace std;
9
10namespace scifir
11{
14
16 {}
17
18 vector_unit_2d::vector_unit_2d(vector_unit_2d&& x) : scalar_unit(std::move(x)),theta(std::move(x.theta))
19 {}
20
23
26
29
32
35
38
41
44
47
50
53
56
59
62
65
68
71
74
77
80
83
86
89
92
95
98
101
104
109
111 {
113 theta = x.theta;
114 return *this;
115 }
116
118 {
119 scalar_unit::operator =(std::move(x));
120 theta = std::move(x.theta);
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 = 180.0f;
160 }
161 else if(x == direction::RIGHT)
162 {
163 theta = 0.0f;
164 }
165 else if(x == direction::TOP)
166 {
167 theta = 90.0f;
168 }
169 else if(x == direction::BOTTOM)
170 {
171 theta = 270.0f;
172 }
173 else if(x == direction::LEFT_TOP)
174 {
175 theta = 135.0f;
176 }
177 else if(x == direction::RIGHT_TOP)
178 {
179 theta = 45.0f;
180 }
181 else if(x == direction::RIGHT_BOTTOM)
182 {
183 theta = 315.0f;
184 }
185 else if(x == direction::LEFT_BOTTOM)
186 {
187 theta = 225.0f;
188 }
189 }
190
192 {
193 if(has_dimensions(x))
194 {
195 float new_x = float(x_projection() + x.x_projection());
196 float new_y = float(y_projection() + x.y_projection());
199 }
200 else
201 {
202 cerr << "Cannot sum vectors of different dimensions" << endl;
203 }
204 }
205
207 {
208 if(has_dimensions(x))
209 {
210 x.invert();
211 *this += x;
212 }
213 else
214 {
215 cerr << "Cannot substract vectors of different dimensions" << endl;
216 }
217 }
218
220 {
221 if (has_dimensions(x))
222 {
223 float new_x = float(x_projection() + x.x_projection());
224 float new_y = float(y_projection() + x.y_projection());
228 }
229 else
230 {
231 return vector_unit_2d();
232 }
233 }
234
236 {
237 if (has_dimensions(x))
238 {
239 x.invert();
240 float new_x = float(x_projection() + x.x_projection());
241 float new_y = float(y_projection() + x.y_projection());
245 }
246 else
247 {
248 return vector_unit_2d();
249 }
250 }
251
258
265
267 {
269 {
272 }
273 else
274 {
275 cerr << "Cannot power with as exponent a unit with dimensions" << endl;
276 return vector_unit_2d();
277 }
278 }
279
280#ifdef IS_UNIX
282 {
285 return out.str();
286 }
287
288 string vector_unit_2d::vectorial_base_display(int number_of_decimals) const
289 {
290 ostringstream out;
291 out << base_display(number_of_decimals) << " " << display_float(theta.get_value(),number_of_decimals) << "θ";
292 return out.str();
293 }
294
295 string vector_unit_2d::vectorial_custom_display(const string& init_dimensions,int number_of_decimals) const
296 {
297 ostringstream out;
298 out << custom_display(init_dimensions,number_of_decimals) << " " << display_float(theta.get_value(),number_of_decimals) << "θ";
299 return out.str();
300 }
301
302 string to_string(const vector_unit_2d& x)
303 {
304 return x.vectorial_display(2);
305 }
306#elif IS_WINDOWS
307 string vector_unit_2d::vectorial_display(int number_of_decimals) const
308 {
309 ostringstream out;
310 out << display(number_of_decimals) << " " << display_float(theta.get_value(),number_of_decimals) << "θ";
311 return out.str();
312 }
313
314 string vector_unit_2d::vectorial_base_display(int number_of_decimals) const
315 {
316 ostringstream out;
317 out << base_display(number_of_decimals) << " " << display_float(theta.get_value(),number_of_decimals) << "θ";
318 return out.str();
319 }
320
321 string vector_unit_2d::vectorial_custom_display(const string& init_dimensions,int number_of_decimals) const
322 {
323 ostringstream out;
324 out << custom_display(init_dimensions,number_of_decimals) << " " << display_float(theta.get_value(),number_of_decimals) << "θ";
325 return out.str();
326 }
327
328 string to_string(const vector_unit_2d& x)
329 {
330 return x.vectorial_display(2);
331 }
332#endif
333
335 {
336 vector<string> values;
337 boost::split(values,init_vector_2d,boost::is_any_of(" "));
338 if (values.size() == 3)
339 {
340 scalar_unit::initialize_from_string(values[0] + " " + values[1],vector<dimension>());
341 theta = angle(values[2]);
342 }
343 }
344
346 {
347 return scalar_unit(std::abs(x.get_value()),x.get_dimensions());
348 }
349
355
357 {
359 return vector_unit_2d(new_value, x.theta);
360 }
361
368
370 {
371 return scifir::atan2(float(y.y_projection() * x.x_projection() - y.x_projection() * x.y_projection()),float(y.x_projection() * x.x_projection() + y.y_projection() * x.y_projection()));
372 }
373
375 {
376 return x.theta == y.theta;
377 }
378
379 bool parallel(const vector_unit_2d& x, const vector_unit_2d& y)
380 {
381 return scifir::parallel(x.theta,y.theta);
382 }
383
385 {
386 return scifir::orthogonal(x.theta,y.theta);
387 }
388}
389
391{
392 long double new_value = x.get_value() * y.get_value();
393 vector<scifir::dimension> new_dimensions = multiply_dimensions(x.get_dimensions(), y.get_dimensions(),new_value);
394 return scifir::vector_unit_2d(float(new_value), new_dimensions, y.theta);
395}
396
398{
399 return !(x == y);
400}
401
402bool operator ==(const scifir::vector_unit_2d& x, const string& init_vector_2d)
403{
404 scifir::vector_unit_2d y(init_vector_2d);
405 return (x == y);
406}
407
408bool operator !=(const scifir::vector_unit_2d& x, const string& init_vector_2d)
409{
410 return !(x == init_vector_2d);
411}
412
413bool operator ==(const string& init_vector_2d, const scifir::vector_unit_2d& x)
414{
415 scifir::vector_unit_2d y(init_vector_2d);
416 return (y == x);
417}
418
419bool operator !=(const string& init_vector_2d, const scifir::vector_unit_2d& x)
420{
421 return !(init_vector_2d == x);
422}
423
424void operator +=(string& x, const scifir::vector_unit_2d& y)
425{
426 ostringstream output;
427 output << y;
428 x += output.str();
429}
430
431string operator +(const string& x, const scifir::vector_unit_2d& y)
432{
433 ostringstream output;
434 output << x;
435 output << y;
436 return output.str();
437}
438
439string operator +(const scifir::vector_unit_2d& y, const string& x)
440{
441 ostringstream output;
442 output << y;
443 output << x;
444 return output.str();
445}
446
447ostream& operator <<(ostream& os, const scifir::vector_unit_2d& x)
448{
449 return os << to_string(x);
450}
451
452istream& operator >>(istream& is, scifir::vector_unit_2d& x)
453{
454 char a[256];
455 is.getline(a, 256);
456 string b(a);
457 boost::trim(b);
459 return is;
460}
461
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 2D. The vector is in polar coordinates with a value and dimension...
angle theta
Angle of the vector in 2D space, in polar coordinates. As all angles of scifir-units,...
scalar_unit y_projection() const
It creates the y projection of the vector, returning it as a scalar_unit of the same dimensions,...
vector_unit_2d operator+(const vector_unit_2d &x) const
Addition of vectors in 2D. It creates a new vector as the addition of the other two.
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....
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.
void initialize_from_string(string init_vector_2d)
Initializes the member-variables with the initialization string of vector_unit_2d init_vector_2d.
void operator-=(vector_unit_2d x)
The vector_unit_2d is substracted as vector, in polar coordinates. The substraction of vectors is use...
void point_to(direction::name x)
Theta is set to the direction specified in 2D. Possible values are LEFT, RIGHT, TOP,...
void operator+=(const vector_unit_2d &x)
The vector_unit_2d is summed as vector, in polar coordinates. The addition of vectors is used for the...
vector_unit_2d operator*(const scalar_unit &x) const
It creates a new vector_unit_2d scaling a vector_unit_2d by the scalar_unit x.
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....
void invert()
Changes the direction of the vector to the opposite direction. It does that by adding 180 degrees to ...
vector_unit_2d & operator=(const vector_unit_2d &x)
Copy assignment. The member-variables are copied from the vector_unit_2d x.
vector_unit_2d operator-(vector_unit_2d x) const
Substraction of vectors in 2D. It creates a new vector as the difference of the other two.
bool operator==(vector_unit_2d x) const
Comparison operator. Two vector_unit_2d are equal if their value, dimensions and theta are the same.
scalar_unit x_projection() const
It creates the x projection of the vector, returning it as a scalar_unit of the same dimensions,...
vector_unit_2d()
Default constructor. The value is set to 0, the dimensions are empty and theta is 0.
vector_unit_2d operator^(const scalar_unit &x) const
It powers a vector by a scalar_unit x if that scalar_unit has empty dimensions.
vector_unit_2d operator/(const scalar_unit &x) const
It creates a new vector_unit_2d scaling a vector_unit_2d by the inverse of the scalar_unit x.
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_2d_to_polar_theta(const scalar_unit &x, scalar_unit y)
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
angle atan2(float y, float x)
Definition angle.cpp:456
scalar_unit cartesian_2d_to_polar_p(const scalar_unit &x, scalar_unit y)
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.
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...
scalar_unit norm(const vector_unit_2d &x)
It returns the value of the vector in polar coordinates, p.
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
scifir::vector_unit_2d operator*(const scifir::scalar_unit &x, const scifir::vector_unit_2d &y)
It creates a new vector_unit_3d scaling a vector_unit_3d by the scalar_unit x.
string operator+(const string &x, const scifir::vector_unit_2d &y)
Creates a new string as the concatenation of the string x with the representation string of the vecto...
bool operator==(const scifir::vector_unit_2d &x, const string &init_vector_2d)
Returns true if x is equal to the vector_unit_2d initialized with the string being compared....
istream & operator>>(istream &is, scifir::vector_unit_2d &x)
Allows that an istream initializes by string a vector_unit_2d x.
void operator+=(string &x, const scifir::vector_unit_2d &y)
Concatenates the string representation of the vector_unit_2d y to the string x.
bool operator!=(const scifir::vector_unit_2d &x, const scifir::vector_unit_2d &y)
Comparison operator. Two vector_unit_2d are not equal if their value, dimensions or theta are differe...
ostream & operator<<(ostream &os, const scifir::vector_unit_2d &x)
Adds the string representation of the vector_unit_2d x to an output stream os.