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 
38 namespace Pt {
39 
40 namespace Gfx {
41 
42 class SizeI;
43 
46 class Size
47 {
48  public:
49  Size()
50  : _w(0), _h(0)
51  {}
52 
53  Size(Float w, Float h)
54  : _w(w), _h(h)
55  {}
56 
57  Size(const Size& other)
58  : _w(other._w), _h(other._h)
59  {}
60 
61  void clear()
62  {
63  _w = 0;
64  _h = 0;
65  }
66 
67  void set(Float w, Float h)
68  {
69  _w = w;
70  _h = h;
71  }
72 
73  bool isEmpty() const
74  {
75  return _w == 0 || _h == 0;
76  }
77 
80  Float area() const
81  { return _w * _h; }
82 
85  void transpose()
86  {
87  Float tmp = _w;
88  _w = _h;
89  _h = tmp;
90  }
91 
95  { return Size(_h, _w); }
96 
99  explicit Size(const SizeI& s);
100 
103  Size& operator=(const SizeI& s);
104 
107  SizeI round() const;
108 
111  SizeI floor() const;
112 
115  SizeI ceil() const;
116 
117  Float width() const
118  { return _w; }
119 
120  Float height() const
121  { return _h; }
122 
123  void setWidth(Float w)
124  { _w = w; }
125 
126  void setHeight(Float h)
127  { _h = h; }
128 
129  void setWidthHeight(Float w, Float h)
130  {
131  _w = w;
132  _h = h;
133  }
134 
135  const Size& addWidth(Float w)
136  {
137  _w += w;
138  return *this;
139  }
140 
141  const Size& subWidth(Float w)
142  {
143  _w -= w;
144  return *this;
145  }
146 
147  const Size& addHeight(Float h)
148  {
149  _h += h;
150  return *this;
151  }
152 
153  const Size& subHeight(Float h)
154  {
155  _h -= h;
156  return *this;
157  }
158 
159  void add(const Size& s)
160  {
161  _w += s._w;
162  _h += s._h;
163  }
164 
165  Size& operator=(const Size& other)
166  {
167  _w = other._w;
168  _h = other._h;
169  return *this;
170  }
171 
172  Size& operator+=(const Size& s)
173  {
174  _w += s._w;
175  _h += s._h;
176  return *this;
177  }
178 
179  bool isEqual(const Size& other, Float eps = FloatNearlyZero) const
180  {
181  return std::abs(_w - other._w) <= eps &&
182  std::abs(_h - other._h) <= eps;
183  }
184 
185  bool operator<(const Size& other) const
186  {
187  return _h < other._h || (_h == other._h && _w < other._w);
188  }
189 
190  Size operator*(Float v) const
191  {
192  return Size(_w * v, _h * v);
193  }
194 
195  Size operator/(Float v) const
196  {
197  return Size(_w / v, _h / v);
198  }
199 
200  Size operator+(Float v) const
201  {
202  return Size(_w + v, _h + v);
203  }
204 
205  Size operator-(Float v) const
206  {
207  return Size(_w - v, _h - v);
208  }
209 
210  Size& operator*=(Float v)
211  {
212  _w *= v;
213  _h *= v;
214  return *this;
215  }
216 
217  Size& operator/=(Float v)
218  {
219  _w /= v;
220  _h /= v;
221  return *this;
222  }
223 
224  Size& operator+=(Float v)
225  {
226  _w += v;
227  _h += v;
228  return *this;
229  }
230 
231  Size& operator-=(Float v)
232  {
233  _w -= v;
234  _h -= v;
235  return *this;
236  }
237 
238  private:
239  Float _w;
240  Float _h;
241 };
242 
243 typedef Size SizeF;
244 
247 class SizeI
248 {
249  public:
250  SizeI()
251  : _w(0), _h(0)
252  {}
253 
254  SizeI(Int w, Int h)
255  : _w(w), _h(h)
256  {}
257 
258  SizeI(const SizeI& other)
259  : _w(other._w), _h(other._h)
260  {}
261 
262  void clear()
263  {
264  _w = 0;
265  _h = 0;
266  }
267 
268  void set(Int w, Int h)
269  {
270  _w = w;
271  _h = h;
272  }
273 
274  bool isEmpty() const
275  {
276  return _w == 0 || _h == 0;
277  }
278 
281  Int area() const
282  { return _w * _h; }
283 
286  void transpose()
287  {
288  Int tmp = _w;
289  _w = _h;
290  _h = tmp;
291  }
292 
296  { return SizeI(_h, _w); }
297 
298  Int width() const
299  { return _w; }
300 
301  Int height() const
302  { return _h; }
303 
304  void setWidth(Int w)
305  { _w = w; }
306 
307  void setHeight(Int h)
308  { _h = h; }
309 
310  void setWidthHeight(Int w, Int h)
311  {
312  _w = w;
313  _h = h;
314  }
315 
316  const SizeI& addWidth(Int w)
317  {
318  _w += w;
319  return *this;
320  }
321 
322  const SizeI& subWidth(Int w)
323  {
324  _w -= w;
325  return *this;
326  }
327 
328  const SizeI& addHeight(Int h)
329  {
330  _h += h;
331  return *this;
332  }
333 
334  const SizeI& subHeight(Int h)
335  {
336  _h -= h;
337  return *this;
338  }
339 
340  void add(const SizeI& s)
341  {
342  _w += s._w;
343  _h += s._h;
344  }
345 
346  SizeI& operator=(const SizeI& other)
347  {
348  _w = other._w;
349  _h = other._h;
350  return *this;
351  }
352 
353  SizeI& operator+=(const SizeI& s)
354  {
355  _w += s._w;
356  _h += s._h;
357  return *this;
358  }
359 
360  bool operator==(const SizeI& other) const
361  {
362  return (_w == other._w && _h == other._h);
363  }
364 
365  bool operator!=(const SizeI& other) const
366  {
367  return (_w != other._w || _h != other._h);
368  }
369 
370  SizeI operator*(Int v) const
371  {
372  return SizeI(_w * v, _h * v);
373  }
374 
375  SizeI operator+(Int v) const
376  {
377  return SizeI(_w + v, _h + v);
378  }
379 
380  SizeI operator-(Int v) const
381  {
382  return SizeI(_w - v, _h - v);
383  }
384 
385  SizeI& operator*=(Int v)
386  {
387  _w *= v;
388  _h *= v;
389  return *this;
390  }
391 
392  SizeI& operator+=(Int v)
393  {
394  _w += v;
395  _h += v;
396  return *this;
397  }
398 
399  SizeI& operator-=(Int v)
400  {
401  _w -= v;
402  _h -= v;
403  return *this;
404  }
405 
406  private:
407  Int _w;
408  Int _h;
409 };
410 
411 inline Size::Size(const SizeI& s)
412 : _w(static_cast<Float>(s.width())), _h(static_cast<Float>(s.height()))
413 {}
414 
415 inline Size& Size::operator=(const SizeI& s)
416 {
417  _w = static_cast<Float>(s.width());
418  _h = static_cast<Float>(s.height());
419  return *this;
420 }
421 
422 inline SizeI Size::round() const
423 {
424  return SizeI(static_cast<Int>(std::lround(_w)),
425  static_cast<Int>(std::lround(_h)));
426 }
427 
428 inline SizeI Size::floor() const
429 {
430  return SizeI(static_cast<Int>(std::floor(_w)),
431  static_cast<Int>(std::floor(_h)));
432 }
433 
434 inline SizeI Size::ceil() const
435 {
436  return SizeI(static_cast<Int>(std::ceil(_w)),
437  static_cast<Int>(std::ceil(_h)));
438 }
439 
440 } //namespace
441 
442 } //namespace
443 
444 #endif
Core module.
Definition: Allocator.h:33
Int area() const
Returns the area (width * height).
Definition: Size.h:281
void transpose()
Swaps width and height in place.
Definition: Size.h:286
Size with integer width and height.
Definition: Size.h:248
Float area() const
Returns the area (width * height).
Definition: Size.h:80
Size toTransposed() const
Returns a copy with width and height swapped.
Definition: Size.h:94
void transpose()
Swaps width and height in place.
Definition: Size.h:85
Size with floating-point width and height.
Definition: Size.h:47
SizeI round() const
Rounds each dimension to the nearest integer and returns a SizeI.
Definition: Size.h:422
SizeI floor() const
Floors each dimension and returns a SizeI.
Definition: Size.h:428
Size & operator=(const SizeI &s)
Assigns from a SizeI by widening the dimensions.
Definition: Size.h:415
SizeI ceil() const
Ceils each dimension and returns a SizeI.
Definition: Size.h:434
SizeI toTransposed() const
Returns a copy with width and height swapped.
Definition: Size.h:295