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
size_3d.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_SPECIAL_UNITS_SIZE_3D_HPP_INCLUDED
2#define SCIFIR_UNITS_SPECIAL_UNITS_SIZE_3D_HPP_INCLUDED
3
4#include "../derived_units/space_units.hpp"
5#include "../util/types.hpp"
6
7#include "boost/algorithm/string.hpp"
8
9#include <string>
10
11using namespace std;
12
13namespace scifir
14{
15 template<typename T = length>
16 class size_3d
17 {
18 public:
20 {}
21
23 {}
24
25 size_3d(size_3d<T>&& x) : width(std::move(x.width)),height(std::move(x.height)),depth(std::move(x.depth))
26 {}
27
30
31 explicit size_3d(const string& new_width,const string& new_height,const string& new_depth) : width(new_width),height(new_height),depth(new_depth)
32 {}
33
34 explicit size_3d(const string& init_size_3d) : size_3d()
35 {
37 }
38
40 {
41 width = x.width;
42 height = x.height;
43 depth = x.depth;
44 return *this;
45 }
46
48 {
49 width = std::move(x.width);
50 height = std::move(x.height);
51 depth = std::move(x.depth);
52 return *this;
53 }
54
56 {
58 return *this;
59 }
60
61 template<typename U>
63 {
64 return size_3d<T>(width + x.width,height + x.height,depth + x.depth);
65 }
66
67 template<typename U>
69 {
70 return size_3d<T>(width - x.width,height - x.height,depth - x.depth);
71 }
72
73 template<typename U>
74 void operator +=(const size_3d<U>& x)
75 {
76 width += x.width;
77 height += x.height;
78 depth += x.depth;
79 }
80
81 template<typename U>
82 void operator -=(const size_3d<U>& x)
83 {
84 width -= x.width;
85 height -= x.height;
86 depth -= x.depth;
87 }
88
90 {
91 return scalar_unit(width * height * depth);
92 }
93
94 string display() const
95 {
97 output << width << " * " << height << " * " << depth;
98 return output.str();
99 }
100
104
105 private:
107 {
108 vector<string> widths;
109 boost::split(widths,init_size_3d,boost::is_any_of("*"));
110 if (widths.size() == 3)
111 {
112 boost::trim(widths[0]);
113 boost::trim(widths[1]);
114 boost::trim(widths[2]);
115 width = T(widths[0]);
116 height = T(widths[1]);
117 depth = T(widths[2]);
118 }
119 }
120 };
121
122 template<>
124 {
125 public:
127 {}
128
131
132 size_3d(size_3d<float>&& x) : width(std::move(x.width)),height(std::move(x.height)),depth(std::move(x.depth))
133 {}
134
135 explicit size_3d(const float& new_width,const float& new_height,const float& new_depth) : width(new_width),height(new_height),depth(new_depth)
136 {}
137
138 explicit size_3d(const string& new_width,const string& new_height,const string& new_depth) : width(stof(new_width)),height(stof(new_height)),depth(stof(new_depth))
139 {}
140
141 explicit size_3d(const string& init_size_3d) : size_3d()
142 {
144 }
145
147 {
148 width = x.width;
149 height = x.height;
150 depth = x.depth;
151 return *this;
152 }
153
155 {
156 width = std::move(x.width);
157 height = std::move(x.height);
158 depth = std::move(x.depth);
159 return *this;
160 }
161
163 {
165 return *this;
166 }
167
169 {
170 return size_3d<float>(width + x.width,height + x.height,depth + x.depth);
171 }
172
174 {
175 return size_3d<float>(width - x.width,height - x.height,depth - x.depth);
176 }
177
179 {
180 width += x.width;
181 height += x.height;
182 depth += x.depth;
183 }
184
186 {
187 width -= x.width;
188 height -= x.height;
189 depth -= x.depth;
190 }
191
192 float get_volume() const
193 {
194 return width * height * depth;
195 }
196
197 string display() const
198 {
200 output << display_float(width,2) << " * " << display_float(height,2) << " * " << display_float(depth,2);
201 return output.str();
202 }
203
204 float width;
205 float height;
206 float depth;
207
208 private:
210 {
211 vector<string> widths;
212 boost::split(widths,init_size_3d,boost::is_any_of("*"));
213 if (widths.size() == 3)
214 {
215 boost::trim(widths[0]);
216 boost::trim(widths[1]);
217 boost::trim(widths[2]);
218 width = stof(widths[0]);
219 height = stof(widths[1]);
220 depth = stof(widths[2]);
221 }
222 }
223 };
224
225 template<typename T>
226 string to_string(const size_3d<T>& x)
227 {
228 return x.display();
229 }
230
231 string to_string(const size_3d<float>& x);
232}
233
234template<typename T>
236{
237 if (x.width == y.width and x.height == y.height and x.depth == y.depth)
238 {
239 return true;
240 }
241 else
242 {
243 return false;
244 }
245}
246
247template<typename T>
249{
250 return !(x == y);
251}
252
253template<typename T>
254bool operator ==(const scifir::size_3d<T>& x, const string& init_size_3d)
255{
256 scifir::size_3d<T> y(init_size_3d);
257 return (x == y);
258}
259
260template<typename T>
261bool operator !=(const scifir::size_3d<T>& x, const string& init_size_3d)
262{
263 return !(x == init_size_3d);
264}
265
266template<typename T>
267bool operator ==(const string& init_size_3d, const scifir::size_3d<T>& x)
268{
269 scifir::size_3d<T> y(init_size_3d);
270 return (x == y);
271}
272
273template<typename T>
274bool operator !=(const string& init_size_3d, const scifir::size_3d<T>& x)
275{
276 return !(init_size_3d == x);
277}
278
279template<typename T>
280void operator +=(string& x, const scifir::size_3d<T>& y)
281{
282 x += to_string(y);
283}
284
285template<typename T>
286string operator +(const string& x, const scifir::size_3d<T>& y)
287{
288 return x + to_string(y);
289}
290
291template<typename T>
292string operator +(const scifir::size_3d<T>& x, const string& y)
293{
294 return to_string(x) + y;
295}
296
297template<typename T>
298ostream& operator <<(ostream& os, const scifir::size_3d<T>& x)
299{
300 return os << to_string(x);
301}
302
303template<typename T>
304istream& operator >>(istream& is, scifir::size_3d<T>& x)
305{
306 char a[256];
307 is.getline(a, 256);
308 string b(a);
309 x = scifir::size_3d<T>(b);
310 return is;
311}
312
313#endif // SCIFIR_UNITS_SPECIAL_UNITS_SIZE_3D_HPP_INCLUDED
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
string display() const
Returns a string representation of size_3d<float>, with his width, height and depth.
Definition size_3d.hpp:197
float depth
Depth, stored as a float. The advantage related to size_3d<float> over size_3d<T> is that it sizes le...
Definition size_3d.hpp:206
size_3d(const float &new_width, const float &new_height, const float &new_depth)
Constructor. width is new_width, height is new_height and depth is new_depth.
Definition size_3d.hpp:135
size_3d()
Default constructor. Initializes width, height and depth to 0.
Definition size_3d.hpp:126
size_3d(const size_3d< float > &x)
Copy constructor. The width, height and depth are copied from the size_3d<float> x.
Definition size_3d.hpp:129
float height
Height, stored as a float. The advantage related to size_3d<float> over size_3d<T> is that it sizes l...
Definition size_3d.hpp:205
void initialize_from_string(const string &init_size_3d)
Internal function. Initializes the member-variables with an initialization string of size_3d<float>.
Definition size_3d.hpp:209
size_3d(size_3d< float > &&x)
Move constructor. The width, height and depth are moved from the size_3d<float> x.
Definition size_3d.hpp:132
size_3d(const string &init_size_3d)
Constructor. The member-variables are initialized by the initialization string init_size_3d specializ...
Definition size_3d.hpp:141
float width
Width, stored as a float. The advantage related to size_3d<float> over size_3d<T> is that it sizes le...
Definition size_3d.hpp:204
size_3d(const string &new_width, const string &new_height, const string &new_depth)
Constructor. width is new_width, height is new_height and depth is new_depth, they are initialized by...
Definition size_3d.hpp:138
float get_volume() const
Calculates the volume as the multiplication of the width, height and depth.
Definition size_3d.hpp:192
Class that allows to store and calculate size in 3D, with width, height and depth....
Definition size_3d.hpp:17
size_3d(const scalar_unit &new_width, const scalar_unit &new_height, const scalar_unit &new_depth)
Definition size_3d.hpp:28
size_3d(const string &init_size_3d)
Constructor. The member-variables are initialized by the initialization string init_size_3d.
Definition size_3d.hpp:34
T width
Width, usually stored as length.
Definition size_3d.hpp:101
scalar_unit get_volume() const
Calculates the volume as the multiplication of the width, height and depth.
Definition size_3d.hpp:89
size_3d(const string &new_width, const string &new_height, const string &new_depth)
Constructor. width is new_width, height is new_height and depth is new_depth, they are initialized by...
Definition size_3d.hpp:31
string display() const
Returns a string representation of size_3d<T>, with his width, height and depth.
Definition size_3d.hpp:94
T depth
Depth, usually stored as length.
Definition size_3d.hpp:103
size_3d(size_3d< T > &&x)
Move constructor. The width, height and depth are moved from the size_3d<T> x.
Definition size_3d.hpp:25
size_3d()
Default constructor. Both width, height and depth are initialized to 0, with the dimensions to the de...
Definition size_3d.hpp:19
size_3d(const size_3d< T > &x)
Copy constructor. The width, height and depth are copied from the size_3d<T> x.
Definition size_3d.hpp:22
void initialize_from_string(const string &init_size_3d)
Internal function. Initializes the member-variables with an initialization string of size_3d<T>.
Definition size_3d.hpp:106
void operator-=(const size_3d< U > &x)
Substracts the width of size_3d<T> x to the width, substracts the height of size_3d<T> x to the heigh...
Definition size_3d.hpp:82
size_3d< T > operator-(const size_3d< U > &x) const
Creates a new size_3d<T> by the difference of two size_3d<T> classes. The width, height and depth are...
Definition size_3d.hpp:68
size_3d< T > & operator=(const size_3d< T > &x)
Copy assignment. The width, height and depth are copied from the size_3d<T> x.
Definition size_3d.hpp:39
size_3d< T > operator+(const size_3d< U > &x) const
Creates a new size_3d<T> by the addition of two size_3d<T> classes. The width, height and depth are t...
Definition size_3d.hpp:62
T height
Height, usually stored as length.
Definition size_3d.hpp:102
void operator+=(const size_3d< U > &x)
Sums the width of size_3d<T> x to the width, sums the height of size_3d<T> x to the height and sums t...
Definition size_3d.hpp:74
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
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
string operator+(const string &x, const scifir::size_3d< T > &y)
Creates a new string as the concatenation of the string x with the string representation of size_3d<T...
Definition size_3d.hpp:286
void operator+=(string &x, const scifir::size_3d< T > &y)
Concatenates the string x with the string representation of size_3d<T> y.
Definition size_3d.hpp:280
bool operator!=(const scifir::size_3d< T > &x, const scifir::size_3d< T > &y)
Returns true if the width, height and depth of x and y are any of them different.
Definition size_3d.hpp:248
istream & operator>>(istream &is, scifir::size_3d< T > &x)
Allows that an istream initializes by string a size_3d<T> x.
Definition size_3d.hpp:304
ostream & operator<<(ostream &os, const scifir::size_3d< T > &x)
Adds the string representation of the size_3d<T> x to an output stream os.
Definition size_3d.hpp:298
bool operator==(const scifir::size_3d< T > &x, const scifir::size_3d< T > &y)
Returns true if the width, height and depth of x and y are both equal.
Definition size_3d.hpp:235