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
pixel.cpp
Go to the documentation of this file.
1#include "./pixel.hpp"
2
3#include "../util/types.hpp"
4
5#include "boost/algorithm/string.hpp"
6
7#include <cmath>
8#include <iostream>
9#include <sstream>
10#include <string>
11
12using namespace std;
13
14namespace scifir
15{
16 pixel::pixel() : value(0)
17 {}
18
19 pixel::pixel(const pixel& x) : value(x.get_value())
20 {}
21
22 pixel::pixel(pixel&& x) : value(std::move(x.get_value()))
23 {}
24
26 {}
27
29 {}
30
31 pixel::pixel(long double new_value) : value(float(new_value))
32 {}
33
35 {}
36
37 pixel::pixel(const string& init_pixel) : value()
38 {
40 }
41
43 {
45 {
46 value = float(x);
47 }
48 else
49 {
50 cerr << "An pixel cannot be initialized with dimensions" << endl;
51 value = 0;
52 }
53 }
54
56 {
57 value = x.get_value();
58 return *this;
59 }
60
62 {
63 value = std::move(x.get_value());
64 return *this;
65 }
66
68 {
70 return *this;
71 }
72
74 {
76 return *this;
77 }
78
80 {
82 {
83 value = x.get_value();
84 }
85 else
86 {
87 cerr << "An pixel cannot be initialized with dimensions" << endl;
88 }
89 return *this;
90 }
91
93 {
94 return pixel(value + x.get_value());
95 }
96
98 {
99 return pixel(value - x.get_value());
100 }
101
103 {
104 return pixel(value * x.get_value());
105 }
106
108 {
109 return pixel(value / x.get_value());
110 }
111
113 {
114 return pixel(std::pow(value,x.get_value()));
115 }
116
118 {
119 value += x.get_value();
120 }
121
123 {
124 value -= x.get_value();
125 }
126
128 {
129 value *= x.get_value();
130 }
131
133 {
134 value /= x.get_value();
135 }
136
138 {
139 value = std::pow(value,x.get_value());
140 }
141
143 {
144 value++;
145 return *this;
146 }
147
149 {
150 pixel tmp = pixel(*this);
151 operator ++();
152 return tmp;
153 }
154
156 {
157 value--;
158 return *this;
159 }
160
162 {
163 pixel tmp = pixel(*this);
164 operator --();
165 return tmp;
166 }
167
172
174 {
177 return output.str();
178 }
179
181 {
182 if (init_pixel.substr(init_pixel.length() - 3,3) == " px")
183 {
184 value = stof(init_pixel.substr(0,init_pixel.length() - 3));
185 }
186 else if (init_pixel.substr(init_pixel.length() - 2,2) == "px")
187 {
188 value = stof(init_pixel.substr(0,init_pixel.length() - 2));
189 }
190 else
191 {
193 }
194 }
195
196 string to_string(const pixel& x)
197 {
198 return x.display();
199 }
200
201 bool is_pixel(const string& init_pixel)
202 {
203 int iteration_limit;
204 if (init_pixel.substr(init_pixel.length() - 3,3) == " px")
205 {
206 iteration_limit = int(init_pixel.length()) - 3;
207 }
208 else if (init_pixel.substr(init_pixel.length() - 2,2) == "px")
209 {
210 iteration_limit = int(init_pixel.length()) - 2;
211 }
212 else
213 {
214 iteration_limit = int(init_pixel.length());
215 }
216 bool dot_present = false;
217 for (int i = 0; i < iteration_limit; i++)
218 {
219 if (init_pixel[i] == '.')
220 {
221 if (dot_present)
222 {
223 return false;
224 }
225 else
226 {
227 dot_present = true;
228 }
229 }
230 else if (!std::isdigit(init_pixel[i]))
231 {
232 return false;
233 }
234 }
235 return true;
236 }
237
238 pixel sqrt(const pixel& x)
239 {
240 return pixel(std::sqrt(x.get_value()));
241 }
242
243 pixel sqrt_nth(const pixel& x, int index)
244 {
245 return pixel(std::pow(x.get_value(), 1.0f / index));
246 }
247}
248
250{
251 if(x.get_value() == y.get_value())
252 {
253 return true;
254 }
255 else
256 {
257 return false;
258 }
259}
260
262{
263 return !(x == y);
264}
265
266bool operator <(const scifir::pixel& x, const scifir::pixel& y)
267{
268 if(x.get_value() < y.get_value())
269 {
270 return true;
271 }
272 else
273 {
274 return false;
275 }
276}
277
278bool operator >(const scifir::pixel& x, const scifir::pixel& y)
279{
280 if(x.get_value() > y.get_value())
281 {
282 return true;
283 }
284 else
285 {
286 return false;
287 }
288}
289
291{
292 return !(x > y);
293}
294
296{
297 return !(x < y);
298}
299
300bool operator ==(const scifir::pixel& x, const string& init_pixel)
301{
302 scifir::pixel y = scifir::pixel(init_pixel);
303 return (x == y);
304}
305
306bool operator !=(const scifir::pixel& x, const string& init_pixel)
307{
308 return !(x == init_pixel);
309}
310
311bool operator ==(const string& init_pixel, const scifir::pixel& x)
312{
313 scifir::pixel y = scifir::pixel(init_pixel);
314 return (y == x);
315}
316
317bool operator !=(const string& init_pixel, const scifir::pixel& x)
318{
319 return !(init_pixel == x);
320}
321
322void operator +=(string& x, const scifir::pixel& y)
323{
324 ostringstream output;
325 output << y;
326 x += output.str();
327}
328
329string operator +(const string& x, const scifir::pixel& y)
330{
331 ostringstream output;
332 output << x;
333 output << y;
334 return output.str();
335}
336
337string operator +(const scifir::pixel& y, const string& x)
338{
339 ostringstream output;
340 output << y;
341 output << x;
342 return output.str();
343}
344
345ostream& operator <<(ostream& os, const scifir::pixel& x)
346{
347 return os << to_string(x);
348}
349
350istream& operator >>(istream& is, scifir::pixel& x)
351{
352 char a[256];
353 is.getline(a, 256);
354 string b(a);
355 boost::trim(b);
356 x = scifir::pixel(b);
357 return is;
358}
359
Class that represents dimensions of the SI system of units. Each dimension sizes 6 bytes,...
Definition dimension.hpp:31
@ NUMERATOR
The dimension is at the numerator.
Definition dimension.hpp:38
@ PIXEL
Pixel, plural pixels. it represents the amount of pixels, it can be used as a replacemet of the metre...
Definition dimension.hpp:35
void operator^=(const pixel &x)
Definition pixel.cpp:137
float value
Definition pixel.hpp:133
void operator+=(const pixel &x)
Definition pixel.cpp:117
pixel & operator++()
Definition pixel.cpp:142
pixel operator^(const pixel &x) const
Definition pixel.cpp:112
pixel operator-(const pixel &x) const
Definition pixel.cpp:97
pixel & operator=(const pixel &x)
Definition pixel.cpp:55
const float & get_value() const
Definition pixel.hpp:41
scalar_unit to_scalar_unit() const
Definition pixel.cpp:168
pixel operator*(const pixel &x) const
Definition pixel.cpp:102
void operator-=(const pixel &x)
Definition pixel.cpp:122
pixel operator+(const pixel &x) const
Definition pixel.cpp:92
string display(int number_of_decimals=2) const
Definition pixel.cpp:173
pixel & operator--()
Definition pixel.cpp:155
void operator*=(const pixel &x)
Definition pixel.cpp:127
void operator/=(const pixel &x)
Definition pixel.cpp:132
pixel operator/(const pixel &x) const
Definition pixel.cpp:107
void initialize_from_string(const string &init_pixel)
Definition pixel.cpp:180
@ NONE
There is no prefix. Then, the dimension is not increased or decreased by some factor.
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.
const float & get_value() const
Read-only getter of the value.
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
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
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
bool is_pixel(const string &init_pixel)
Definition pixel.cpp:201
angle sqrt(const angle &x)
Calculates the square root of the angle x and returns that new angle.
Definition angle.cpp:416
bool operator!=(const scifir::pixel &x, const scifir::pixel &y)
Definition pixel.cpp:261
void operator+=(string &x, const scifir::pixel &y)
Definition pixel.cpp:322
bool operator<=(const scifir::pixel &x, const scifir::pixel &y)
Definition pixel.cpp:290
bool operator>=(const scifir::pixel &x, const scifir::pixel &y)
Definition pixel.cpp:295
bool operator<(const scifir::pixel &x, const scifir::pixel &y)
Definition pixel.cpp:266
bool operator==(const scifir::pixel &x, const scifir::pixel &y)
Definition pixel.cpp:249
istream & operator>>(istream &is, scifir::pixel &x)
Definition pixel.cpp:350
ostream & operator<<(ostream &os, const scifir::pixel &x)
Definition pixel.cpp:345
string operator+(const string &x, const scifir::pixel &y)
Definition pixel.cpp:329
bool operator>(const scifir::pixel &x, const scifir::pixel &y)
Definition pixel.cpp:278