Size.h
1/* Copyright (C) 2006-2015 Laurentiu-Gheorghe Crisan
2 Copyright (C) 2006-2015 Marc Boris Duerner
3 Copyright (C) 2010 Aloysius Indrayanto
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 As a special exception, you may use this file as part of a free
11 software library without restriction. Specifically, if other files
12 instantiate templates or use macros or inline functions from this
13 file, or you compile this file and link it with other files to
14 produce an executable, this file does not by itself cause the
15 resulting executable to be covered by the GNU General Public
16 License. This exception does not however invalidate any other
17 reasons why the executable file might be covered by the GNU Library
18 General Public License.
19
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
24
25 You should have received a copy of the GNU Lesser General Public
26 License along with this library; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 02110-1301 USA
29*/
30
31#ifndef PT_GFX_SIZE_H
32#define PT_GFX_SIZE_H
33
34#include <Pt/Gfx/Api.h>
35#include <Pt/Types.h>
36#include <Pt/Math.h>
37
38namespace Pt {
39
40namespace Gfx {
41
42class SizeI;
43
68class Size
69{
70 public:
71 Size()
72 : _w(0), _h(0)
73 {}
74
75 Size(Float w, Float h)
76 : _w(w), _h(h)
77 {}
78
79 Size(const Size& other)
80 : _w(other._w), _h(other._h)
81 {}
82
83 void clear()
84 {
85 _w = 0;
86 _h = 0;
87 }
88
89 void set(Float w, Float h)
90 {
91 _w = w;
92 _h = h;
93 }
94
95 bool isEmpty() const
96 {
97 return _w == 0 || _h == 0;
98 }
99
102 Float area() const
103 { return _w * _h; }
104
108 {
109 Float tmp = _w;
110 _w = _h;
111 _h = tmp;
112 }
113
116 Size toTransposed() const
117 { return Size(_h, _w); }
118
121 explicit Size(const SizeI& s);
122
125 Size& operator=(const SizeI& s);
126
129 SizeI round() const;
130
133 SizeI floor() const;
134
137 SizeI ceil() const;
138
139 Float width() const
140 { return _w; }
141
142 Float height() const
143 { return _h; }
144
145 void setWidth(Float w)
146 { _w = w; }
147
148 void setHeight(Float h)
149 { _h = h; }
150
151 void setWidthHeight(Float w, Float h)
152 {
153 _w = w;
154 _h = h;
155 }
156
157 const Size& addWidth(Float w)
158 {
159 _w += w;
160 return *this;
161 }
162
163 const Size& subWidth(Float w)
164 {
165 _w -= w;
166 return *this;
167 }
168
169 const Size& addHeight(Float h)
170 {
171 _h += h;
172 return *this;
173 }
174
175 const Size& subHeight(Float h)
176 {
177 _h -= h;
178 return *this;
179 }
180
181 void add(const Size& s)
182 {
183 _w += s._w;
184 _h += s._h;
185 }
186
187 Size& operator=(const Size& other)
188 {
189 _w = other._w;
190 _h = other._h;
191 return *this;
192 }
193
194 Size& operator+=(const Size& s)
195 {
196 _w += s._w;
197 _h += s._h;
198 return *this;
199 }
200
201 bool isEqual(const Size& other, Float eps = FloatNearlyZero) const
202 {
203 return std::abs(_w - other._w) <= eps &&
204 std::abs(_h - other._h) <= eps;
205 }
206
207 bool operator<(const Size& other) const
208 {
209 return _h < other._h || (_h == other._h && _w < other._w);
210 }
211
212 Size operator*(Float v) const
213 {
214 return Size(_w * v, _h * v);
215 }
216
217 Size operator/(Float v) const
218 {
219 return Size(_w / v, _h / v);
220 }
221
222 Size operator+(Float v) const
223 {
224 return Size(_w + v, _h + v);
225 }
226
227 Size operator-(Float v) const
228 {
229 return Size(_w - v, _h - v);
230 }
231
232 Size& operator*=(Float v)
233 {
234 _w *= v;
235 _h *= v;
236 return *this;
237 }
238
239 Size& operator/=(Float v)
240 {
241 _w /= v;
242 _h /= v;
243 return *this;
244 }
245
246 Size& operator+=(Float v)
247 {
248 _w += v;
249 _h += v;
250 return *this;
251 }
252
253 Size& operator-=(Float v)
254 {
255 _w -= v;
256 _h -= v;
257 return *this;
258 }
259
260 private:
261 Float _w;
262 Float _h;
263};
264
265typedef Size SizeF;
266
278class SizeI
279{
280 public:
281 SizeI()
282 : _w(0), _h(0)
283 {}
284
285 SizeI(Int w, Int h)
286 : _w(w), _h(h)
287 {}
288
289 SizeI(const SizeI& other)
290 : _w(other._w), _h(other._h)
291 {}
292
293 void clear()
294 {
295 _w = 0;
296 _h = 0;
297 }
298
299 void set(Int w, Int h)
300 {
301 _w = w;
302 _h = h;
303 }
304
305 bool isEmpty() const
306 {
307 return _w == 0 || _h == 0;
308 }
309
312 Int area() const
313 { return _w * _h; }
314
318 {
319 Int tmp = _w;
320 _w = _h;
321 _h = tmp;
322 }
323
326 SizeI toTransposed() const
327 { return SizeI(_h, _w); }
328
329 Int width() const
330 { return _w; }
331
332 Int height() const
333 { return _h; }
334
335 void setWidth(Int w)
336 { _w = w; }
337
338 void setHeight(Int h)
339 { _h = h; }
340
341 void setWidthHeight(Int w, Int h)
342 {
343 _w = w;
344 _h = h;
345 }
346
347 const SizeI& addWidth(Int w)
348 {
349 _w += w;
350 return *this;
351 }
352
353 const SizeI& subWidth(Int w)
354 {
355 _w -= w;
356 return *this;
357 }
358
359 const SizeI& addHeight(Int h)
360 {
361 _h += h;
362 return *this;
363 }
364
365 const SizeI& subHeight(Int h)
366 {
367 _h -= h;
368 return *this;
369 }
370
371 void add(const SizeI& s)
372 {
373 _w += s._w;
374 _h += s._h;
375 }
376
377 SizeI& operator=(const SizeI& other)
378 {
379 _w = other._w;
380 _h = other._h;
381 return *this;
382 }
383
384 SizeI& operator+=(const SizeI& s)
385 {
386 _w += s._w;
387 _h += s._h;
388 return *this;
389 }
390
391 bool operator==(const SizeI& other) const
392 {
393 return (_w == other._w && _h == other._h);
394 }
395
396 bool operator!=(const SizeI& other) const
397 {
398 return (_w != other._w || _h != other._h);
399 }
400
401 SizeI operator*(Int v) const
402 {
403 return SizeI(_w * v, _h * v);
404 }
405
406 SizeI operator+(Int v) const
407 {
408 return SizeI(_w + v, _h + v);
409 }
410
411 SizeI operator-(Int v) const
412 {
413 return SizeI(_w - v, _h - v);
414 }
415
416 SizeI& operator*=(Int v)
417 {
418 _w *= v;
419 _h *= v;
420 return *this;
421 }
422
423 SizeI& operator+=(Int v)
424 {
425 _w += v;
426 _h += v;
427 return *this;
428 }
429
430 SizeI& operator-=(Int v)
431 {
432 _w -= v;
433 _h -= v;
434 return *this;
435 }
436
437 private:
438 Int _w;
439 Int _h;
440};
441
442inline Size::Size(const SizeI& s)
443: _w(static_cast<Float>(s.width())), _h(static_cast<Float>(s.height()))
444{}
445
446inline Size& Size::operator=(const SizeI& s)
447{
448 _w = static_cast<Float>(s.width());
449 _h = static_cast<Float>(s.height());
450 return *this;
451}
452
453inline SizeI Size::round() const
454{
455 return SizeI(static_cast<Int>(std::lround(_w)),
456 static_cast<Int>(std::lround(_h)));
457}
458
459inline SizeI Size::floor() const
460{
461 return SizeI(static_cast<Int>(std::floor(_w)),
462 static_cast<Int>(std::floor(_h)));
463}
464
465inline SizeI Size::ceil() const
466{
467 return SizeI(static_cast<Int>(std::ceil(_w)),
468 static_cast<Int>(std::ceil(_h)));
469}
470
471} //namespace
472
473} //namespace
474
475#endif
Integer width and height.
Definition Size.h:279
Int area() const
Returns the area (width * height).
Definition Size.h:312
SizeI toTransposed() const
Returns a copy with width and height swapped.
Definition Size.h:326
void transpose()
Swaps width and height in place.
Definition Size.h:317
Floating-point width and height.
Definition Size.h:69
Size toTransposed() const
Returns a copy with width and height swapped.
Definition Size.h:116
Size & operator=(const SizeI &s)
Assigns from a SizeI by widening the dimensions.
Definition Size.h:446
SizeI floor() const
Floors each dimension and returns a SizeI.
Definition Size.h:459
Float area() const
Returns the area (width * height).
Definition Size.h:102
SizeI round() const
Rounds each dimension to the nearest integer and returns a SizeI.
Definition Size.h:453
SizeI ceil() const
Ceils each dimension and returns a SizeI.
Definition Size.h:465
void transpose()
Swaps width and height in place.
Definition Size.h:107
Graphics and imaging services.
Definition Api-Argb32Image.h:12
Core module.
Definition Allocator.h:33