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
coordinates_1d.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_COORDINATES_COORDINATES_1D_HPP_INCLUDED
2#define SCIFIR_UNITS_COORDINATES_COORDINATES_1D_HPP_INCLUDED
3
4#include "../units/base_units.hpp"
5#include "../derived_units/physics_units.hpp"
6#include "../util/types.hpp"
7
8#include <cmath>
9#include <iostream>
10#include <string>
11
12using namespace std;
13
14namespace scifir
15{
16 template<typename T = length>
18 {
19 public:
21 {}
22
23 coordinates_1d(const coordinates_1d<T>& x_coordinates) : x(x_coordinates.x)
24 {}
25
26 coordinates_1d(coordinates_1d<T>&& x_coordinates) : x(std::move(x_coordinates.x))
27 {}
28
29 explicit coordinates_1d(const scalar_unit& new_x) : x(new_x)
30 {}
31
32 explicit coordinates_1d(const string& init_coordinates_1d) : coordinates_1d()
33 {
34 initialize_from_string(init_coordinates_1d);
35 }
36
38 {
39 x = x_coordinates.x;
40 return *this;
41 }
42
44 {
45 x = std::move(x_coordinates.x);
46 return *this;
47 }
48
49 coordinates_1d<T>& operator =(const string& init_coordinates_1d)
50 {
51 initialize_from_string(init_coordinates_1d);
52 return *this;
53 }
54
56 {
57 coordinates_1d<T> new_coordinates(origin);
58 new_coordinates.move(coordinates.x);
59 return new_coordinates;
60 }
61
62 void set_position(const scalar_unit& new_x)
63 {
64 x = new_x;
65 }
66
67 void move(const scalar_unit& x_value)
68 {
69 x += x_value;
70 }
71
73 {
74 return T(std::abs(x.get_value()),x.get_dimensions());
75 }
76
77 string display_cartesian() const
78 {
79 ostringstream out;
80 out << "(" << x << ")";
81 return out.str();
82 }
83
84 T x;
85
86 private:
87 void initialize_from_string(string init_coordinates_1d)
88 {
89 if (init_coordinates_1d.front() == '(')
90 {
91 init_coordinates_1d.erase(0,1);
92 }
93 if (init_coordinates_1d.back() == ')')
94 {
95 init_coordinates_1d.erase(init_coordinates_1d.size()-1,1);
96 }
97 x = scalar_unit(T(init_coordinates_1d));
98 }
99 };
100
101 template<>
102 class coordinates_1d<float>
103 {
104 public:
106 {}
107
108 coordinates_1d(const coordinates_1d<float>& x_coordinates) : x(x_coordinates.x)
109 {}
110
111 coordinates_1d(coordinates_1d<float>&& x_coordinates) : x(std::move(x_coordinates.x))
112 {}
113
114 explicit coordinates_1d(float new_x) : x(new_x)
115 {}
116
117 explicit coordinates_1d(const string& init_coordinates_1d) : coordinates_1d()
118 {
119 initialize_from_string(init_coordinates_1d);
120 }
121
123 {
124 x = x_coordinates.x;
125 return *this;
126 }
127
129 {
130 x = std::move(x_coordinates.x);
131 return *this;
132 }
133
134 coordinates_1d<float>& operator =(const string& init_coordinates_1d)
135 {
136 initialize_from_string(init_coordinates_1d);
137 return *this;
138 }
139
140 void set_position(float new_x)
141 {
142 x = new_x;
143 }
144
145 void move(float x_value)
146 {
147 x += x_value;
148 }
149
150 float distance_to_origin() const
151 {
152 return float(std::abs(x));
153 }
154
155 string display_cartesian() const
156 {
157 ostringstream out;
158 out << "(" << display_float(x) << ")";
159 return out.str();
160 }
161
162 float x;
163
164 private:
165 void initialize_from_string(string init_coordinates_1d)
166 {
167 if (init_coordinates_1d.front() == '(')
168 {
169 init_coordinates_1d.erase(0,1);
170 }
171 if (init_coordinates_1d.back() == ')')
172 {
173 init_coordinates_1d.erase(init_coordinates_1d.size()-1,1);
174 }
175 if (init_coordinates_1d != "")
176 {
177 x = stof(init_coordinates_1d);
178 }
179 }
180 };
181
182 template<typename T>
184 {
185 return x.display_cartesian();
186 }
187
188 string to_string(const coordinates_1d<float>& x);
189
190 template<typename T,typename U>
192 {
193 return T(scifir::sqrt(scifir::pow(x.x - y.x,2)));
194 }
195
196 float distance(const coordinates_1d<float>& x,const coordinates_1d<float>& y);
197}
198
199template<typename T,typename U>
201{
202 if (x.x == y.x)
203 {
204 return true;
205 }
206 else
207 {
208 return false;
209 }
210}
211
212template<typename T,typename U>
214{
215 return !(x == y);
216}
217
218template<typename T>
219bool operator ==(const scifir::coordinates_1d<T>& x, const string& init_coordinates_1d)
220{
221 scifir::coordinates_1d<T> y(init_coordinates_1d);
222 return (x == y);
223}
224
225template<typename T>
226bool operator !=(const scifir::coordinates_1d<T>& x, const string& init_coordinates_1d)
227{
228 return !(x == init_coordinates_1d);
229}
230
231template<typename T>
232bool operator ==(const string& init_coordinates_1d, const scifir::coordinates_1d<T>& x)
233{
234 scifir::coordinates_1d<T> y(init_coordinates_1d);
235 return (x == y);
236}
237
238template<typename T>
239bool operator !=(const string& init_coordinates_1d, const scifir::coordinates_1d<T>& x)
240{
241 return !(init_coordinates_1d == x);
242}
243
244template<typename T>
245void operator +=(string& x, const scifir::coordinates_1d<T>& y)
246{
247 x += to_string(y);
248}
249
250template<typename T>
251string operator +(const string& x,const scifir::coordinates_1d<T>& y)
252{
253 return x + to_string(y);
254}
255
256template<typename T>
257string operator +(const scifir::coordinates_1d<T>& x,const string& y)
258{
259 return to_string(x) + y;
260}
261
262template<typename T>
263ostream& operator <<(ostream& os,const scifir::coordinates_1d<T>& x)
264{
265 return os << to_string(x);
266}
267
268ostream& operator <<(ostream& os,const scifir::coordinates_1d<float>& x);
269
270template<typename T>
272{
273 char a[256];
274 is.getline(a, 256);
275 string b(a);
276 boost::trim(b);
278 return is;
279}
280
281#endif // SCIFIR_UNITS_COORDINATES_COORDINATES_1D_HPP_INCLUDED
coordinates_1d(const string &init_coordinates_1d)
coordinates_1d(coordinates_1d< float > &&x_coordinates)
coordinates_1d(const coordinates_1d< float > &x_coordinates)
void initialize_from_string(string init_coordinates_1d)
coordinates_1d(const scalar_unit &new_x)
coordinates_1d(coordinates_1d< T > &&x_coordinates)
coordinates_1d< T > & operator=(const coordinates_1d< T > &x_coordinates)
static coordinates_1d< T > origin(const coordinates_1d< T > &origin, const coordinates_1d< T > &coordinates)
string display_cartesian() const
coordinates_1d(const string &init_coordinates_1d)
void initialize_from_string(string init_coordinates_1d)
coordinates_1d(const coordinates_1d< T > &x_coordinates)
void set_position(const scalar_unit &new_x)
void move(const scalar_unit &x_value)
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
bool operator==(const scifir::coordinates_1d< T > &x, const scifir::coordinates_1d< U > &y)
istream & operator>>(istream &is, scifir::coordinates_1d< T > &x)
string operator+(const string &x, const scifir::coordinates_1d< T > &y)
ostream & operator<<(ostream &os, const scifir::coordinates_1d< T > &x)
void operator+=(string &x, const scifir::coordinates_1d< T > &y)
bool operator!=(const scifir::coordinates_1d< T > &x, const scifir::coordinates_1d< U > &y)
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
scalar_unit pow(const scalar_unit &x, int exponent)
Exponentiates a scalar_unit to some numeric type, the dimensions are also exponentiated.
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
float distance(const coordinates_1d< float > &x, const coordinates_1d< float > &y)
string display_float(const float &value, int number_of_decimals)
Definition types.cpp:36
angle sqrt(const angle &x)
Calculates the square root of the angle x and returns that new angle.
Definition angle.cpp:416