ICF 3.0.5.47
Technical documentation of ICF Libraries
CRect.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#pragma once
7
8
9// ICF includes
10#include <istd/CIndex2d.h>
11#include <istd/TRange.h>
12
13#include <i2d/CRectangle.h>
14
15#include <ibase/CSize.h>
16
17
18namespace i2d
19{
20
21
25class CRect
26{
27public:
28 CRect();
29 CRect(const CRect& rect);
30 CRect(int left, int top, int right, int bottom);
31 CRect(const istd::CIndex2d& leftTop, const istd::CIndex2d& rightBottom);
32 CRect(const istd::CIndex2d& leftTop, const ibase::CSize& size);
33 CRect(const i2d::CRectangle& rect);
34 explicit CRect(const ibase::CSize& size);
35
37
39 void SetLeftTop(istd::CIndex2d position);
41 void SetRightTop(istd::CIndex2d position);
43 void SetLeftBottom(istd::CIndex2d position);
45 void SetRightBottom(istd::CIndex2d position);
46
47 int GetLeft() const;
48 void SetLeft(int left);
49 int GetTop() const;
50 void SetTop(int top);
51 int GetRight() const;
52 void SetRight(int right);
53 int GetBottom() const;
54 void SetBottom(int bottom);
55
56 int GetWidth() const;
57 int GetHeight() const;
58
61 void SetHorizontalRange(const istd::CIntRange& range);
62
63 const istd::CIntRange& GetVerticalRange() const;
65 void SetVerticalRange(const istd::CIntRange& range);
66
68
72 bool IsValid() const;
73
77 bool IsEmpty() const;
78
82 bool IsValidNonEmpty() const;
83
87 bool IsNull() const;
88
92 ibase::CSize GetSize() const;
93
97 bool IsInside(const istd::CIndex2d& point) const;
98
102 bool IsInside(const CRect& rect) const;
103
107 bool IsOutside(const CRect& rect) const;
108
112 void Reset();
113
117 void Union(const CRect& rect);
118
122 void GetUnion(const CRect& rect, CRect& result) const;
123
128 CRect GetUnion(const CRect& rect) const;
129
134 void Union(istd::CIndex2d point);
135
139 CRect GetUnion(istd::CIndex2d point) const;
140
145 void Expand(const CRect& rect);
146
151 CRect GetExpanded(const CRect& rect) const;
152
156 void Intersection(const CRect& rect);
157
161 CRect GetIntersection(const CRect& rect) const;
162
166 void Translate(istd::CIndex2d point);
167
172
173 // operators
174 CRect& operator=(const CRect& rect);
175 bool operator==(const CRect& rect) const;
176 bool operator!=(const CRect& rect) const;
177
178 // static methods
182 static const i2d::CRect& GetEmpty();
186 static const i2d::CRect& GetInvalid();
187
188private:
189 istd::CIntRange m_horizontalRange;
190 istd::CIntRange m_verticalRange;
191
192 // static atributes
193 static CRect s_empty;
194 static CRect s_invalid;
195};
196
197
198// inline methods
199
201{
202#ifdef _DEBUG
203 // set to be invalid
204 m_horizontalRange = istd::CIntRange::GetInvalid();
205 m_verticalRange = istd::CIntRange::GetInvalid();
206#endif //_DEBUG
207}
208
209
210inline CRect::CRect(const CRect& rect)
211{
212 *this = rect;
213}
214
215
216inline CRect::CRect(int left, int top, int right, int bottom)
217{
218 m_horizontalRange = istd::CIntRange(left, right);
219 m_verticalRange = istd::CIntRange(top, bottom);
220
221 Q_ASSERT(IsValid());
222}
223
224
225inline CRect::CRect(const istd::CIndex2d& leftTop, const istd::CIndex2d& rightBottom)
226{
227 m_horizontalRange = istd::CIntRange(leftTop.GetX(), rightBottom.GetX());
228 m_verticalRange = istd::CIntRange(leftTop.GetY(), rightBottom.GetY());
229
230 Q_ASSERT(IsValid());
231}
232
233
234inline CRect::CRect(const istd::CIndex2d& leftTop, const ibase::CSize& size)
235{
236 m_horizontalRange = istd::CIntRange(leftTop.GetX(), leftTop.GetX() + size.GetX());
237 m_verticalRange = istd::CIntRange(leftTop.GetY(), leftTop.GetY() + size.GetY());
238
239 Q_ASSERT(IsValid());
240}
241
242
243inline CRect::CRect(const i2d::CRectangle& rect)
244{
245 m_horizontalRange = istd::CIntRange(int(rect.GetLeft()), int(rect.GetRight()));
246 m_verticalRange = istd::CIntRange(int(rect.GetTop()), int(rect.GetBottom()));
247
248 Q_ASSERT(IsValid());
249}
250
251
252inline CRect::CRect(const ibase::CSize& size)
253{
254 m_horizontalRange = istd::CIntRange(0, size.GetX());
255 m_verticalRange = istd::CIntRange(0, size.GetY());
256
257 Q_ASSERT(IsValid());
258}
259
260
262{
263 return i2d::CRectangle(
264 m_horizontalRange.GetMinValue(),
265 m_verticalRange.GetMinValue(),
266 m_horizontalRange.GetLength(),
267 m_verticalRange.GetLength());
268}
269
270
272{
273 return istd::CIndex2d(m_horizontalRange.GetMinValue(), m_verticalRange.GetMinValue());
274}
275
276
278{
279 m_horizontalRange.SetMinValue(position.GetX());
280 m_verticalRange.SetMinValue(position.GetY());
281}
282
283
285{
286 return istd::CIndex2d(m_horizontalRange.GetMaxValue(), m_verticalRange.GetMinValue());
287}
288
289
291{
292 m_horizontalRange.SetMaxValue(position.GetX());
293 m_verticalRange.SetMinValue(position.GetY());
294}
295
296
298{
299 return istd::CIndex2d(m_horizontalRange.GetMinValue(), m_verticalRange.GetMaxValue());
300}
301
302
304{
305 m_horizontalRange.SetMinValue(position.GetX());
306 m_verticalRange.SetMaxValue(position.GetY());
307}
308
309
311{
312 return istd::CIndex2d(m_horizontalRange.GetMaxValue(), m_verticalRange.GetMaxValue());
313}
314
315
317{
318 m_horizontalRange.SetMaxValue(position.GetX());
319 m_verticalRange.SetMaxValue(position.GetY());
320}
321
322
323inline int CRect::GetLeft() const
324{
325 return m_horizontalRange.GetMinValue();
326}
327
328inline void CRect::SetLeft(int left)
329{
330 m_horizontalRange.SetMinValue(left);
331}
332
333
334inline int CRect::GetTop() const
335{
336 return m_verticalRange.GetMinValue();
337}
338
339
340inline void CRect::SetTop(int top)
341{
342 return m_verticalRange.SetMinValue(top);
343}
344
345
346inline int CRect::GetRight() const
347{
348 return m_horizontalRange.GetMaxValue();
349}
350
351
352inline void CRect::SetRight(int right)
353{
354 return m_horizontalRange.SetMaxValue(right);
355}
356
357
358inline int CRect::GetBottom() const
359{
360 return m_verticalRange.GetMaxValue();
361}
362
363
364inline void CRect::SetBottom(int bottom)
365{
366 return m_verticalRange.SetMaxValue(bottom);
367}
368
369
370inline int CRect::GetWidth() const
371{
372 return m_horizontalRange.GetLength();
373}
374
375
376inline int CRect::GetHeight() const
377{
378 return m_verticalRange.GetLength();
379}
380
381
383{
384 return m_horizontalRange;
385}
386
387
389{
390 return m_horizontalRange;
391}
392
393
395{
396 m_horizontalRange = range;
397}
398
399
401{
402 return m_verticalRange;
403}
404
405
407{
408 return m_verticalRange;
409}
410
411
413{
414 m_verticalRange = range;
415}
416
417
419{
420 return istd::CIndex2d(
421 m_horizontalRange.GetMinValue() + m_horizontalRange.GetLength() / 2,
422 m_verticalRange.GetMinValue() + m_verticalRange.GetLength() / 2);
423}
424
425
426inline bool CRect::IsValid() const
427{
428 return (m_horizontalRange.IsValid()) && (m_verticalRange.IsValid());
429}
430
431
432inline bool CRect::IsEmpty() const
433{
434 return (m_horizontalRange.IsEmpty()) || (m_verticalRange.IsEmpty());
435}
436
437
438inline bool CRect::IsValidNonEmpty() const
439{
440 return (m_horizontalRange.IsValidNonEmpty()) && (m_verticalRange.IsValidNonEmpty());
441}
442
443
444inline bool CRect::IsNull() const
445{
446 return (m_horizontalRange == istd::CIntRange::GetNull()) && (m_verticalRange == istd::CIntRange::GetNull());
447}
448
449
451{
452 return ibase::CSize(m_horizontalRange.GetLength(), m_verticalRange.GetLength());
453}
454
455
456inline bool CRect::IsInside(const istd::CIndex2d& point) const
457{
458 Q_ASSERT(IsValid());
459
460 return m_horizontalRange.Contains(point.GetX()) && m_verticalRange.Contains(point.GetY());
461}
462
463
464inline bool CRect::IsInside(const CRect& rect) const
465{
466 Q_ASSERT(IsValid());
467 Q_ASSERT(rect.IsValid());
468
469 if (rect.IsEmpty()){
470 return true;
471 }
472 else{
473 return m_horizontalRange.Contains(rect.m_horizontalRange) && m_verticalRange.Contains(rect.m_verticalRange);
474 }
475}
476
477
478inline bool CRect::IsOutside(const CRect& rect) const
479{
480 Q_ASSERT(IsValid());
481 Q_ASSERT(rect.IsValid());
482
483 if (rect.IsEmpty()){
484 return true;
485 }
486 else{
487 return m_horizontalRange.IsOutsideOf(rect.m_horizontalRange) || m_verticalRange.IsOutsideOf(rect.m_verticalRange);
488 }
489}
490
491
492inline void CRect::Reset()
493{
494 m_horizontalRange.Reset();
495 m_verticalRange.Reset();
496}
497
498
499inline void CRect::Union(const CRect& rect)
500{
501 Q_ASSERT(rect.IsValid());
502 Q_ASSERT(IsValid());
503
504 if (IsEmpty()){
505 *this = rect;
506 }
507 else if (!rect.IsEmpty()){
508 m_horizontalRange.Unite(rect.m_horizontalRange);
509 m_verticalRange.Unite(rect.m_verticalRange);
510 }
511}
512
513
514inline void CRect::GetUnion(const CRect& rect, CRect& result) const
515{
516 Q_ASSERT(rect.IsValid());
517 Q_ASSERT(IsValid());
518
519 if (IsEmpty()){
520 result = rect;
521 }
522 else if (!rect.IsEmpty()){
523 result = *this;
524
525 result.m_horizontalRange.Unite(rect.m_horizontalRange);
526 result.m_verticalRange.Unite(rect.m_verticalRange);
527 }
528 else{
529 result.Reset();
530 }
531}
532
533
534inline CRect CRect::GetUnion(const CRect& rect) const
535{
536 Q_ASSERT(rect.IsValid());
537 Q_ASSERT(IsValid());
538
539 CRect result;
540
541 GetUnion(rect, result);
542
543 return result;
544}
545
546
547inline void CRect::Union(istd::CIndex2d point)
548{
549 Q_ASSERT(IsValid());
550
551 int left = qMin(GetLeft(), point.GetX());
552 int top = qMin(GetTop(), point.GetY());
553 int right = qMax(GetRight(), point.GetX());
554 int bottom = qMax(GetBottom(), point.GetY());
555
556 m_horizontalRange = istd::CIntRange(left, right);
557 m_verticalRange = istd::CIntRange(top, bottom);
558}
559
560
562{
563 Q_ASSERT(IsValid());
564
565 CRect result = *this;
566
567 result.Union(point);
568
569 return result;
570}
571
572
573inline void CRect::Expand(const CRect& rect)
574{
575 Q_ASSERT(rect.IsValid());
576 Q_ASSERT(IsValid());
577
578 m_horizontalRange.Expand(rect.m_horizontalRange);
579 m_verticalRange.Expand(rect.m_verticalRange);
580}
581
582
583inline CRect CRect::GetExpanded(const CRect& rect) const
584{
585 Q_ASSERT(rect.IsValid());
586 Q_ASSERT(IsValid());
587
588 CRect result = *this;
589
590 result.Expand(rect);
591
592 return result;
593}
594
595
596inline void CRect::Intersection(const CRect& rect)
597{
598 Q_ASSERT(rect.IsValid());
599 Q_ASSERT(IsValid());
600
601 m_horizontalRange.Intersection(rect.m_horizontalRange);
602 m_verticalRange.Intersection(rect.m_verticalRange);
603
604 if (!m_horizontalRange.IsValid() || !m_verticalRange.IsValid()){
605 Reset();
606 }
607}
608
609
610inline CRect CRect::GetIntersection(const CRect& rect) const
611{
612 Q_ASSERT(rect.IsValid());
613 Q_ASSERT(IsValid());
614
615 CRect result = *this;
616
617 result.Intersection(rect);
618
619 return result;
620}
621
622
624{
625 m_horizontalRange.SetMinValue(m_horizontalRange.GetMinValue() + point.GetX());
626 m_horizontalRange.SetMaxValue(m_horizontalRange.GetMaxValue() + point.GetX());
627 m_verticalRange.SetMinValue(m_verticalRange.GetMinValue() + point.GetY());
628 m_verticalRange.SetMaxValue(m_verticalRange.GetMaxValue() + point.GetY());
629}
630
631
633{
634 Q_ASSERT(IsValid());
635
636 CRect result = *this;
637
638 result.Translate(point);
639
640 return result;
641}
642
643
644inline CRect& CRect::operator =(const CRect& rect)
645{
646 m_horizontalRange = rect.m_horizontalRange;
647 m_verticalRange = rect.m_verticalRange;
648
649 return *this;
650}
651
652
653inline bool CRect::operator==(const CRect& rect) const
654{
655 return (m_horizontalRange == rect.m_horizontalRange) && (m_verticalRange == rect.m_verticalRange);
656}
657
658
659inline bool CRect::operator!=(const CRect& rect) const
660{
661 return !operator==(rect);
662}
663
664
665// static methods
666
668{
669 return s_empty;
670}
671
672
674{
675 return s_invalid;
676}
677
678
679} // namespace i2d
680
681
Simple rectangle with integer bounds.
Definition CRect.h:26
int GetHeight() const
Definition CRect.h:376
void SetLeft(int left)
Definition CRect.h:328
ibase::CSize GetSize() const
Get size of rectangle.
Definition CRect.h:450
static const i2d::CRect & GetInvalid()
Get invalid rectangle.
Definition CRect.h:673
void SetTop(int top)
Definition CRect.h:340
istd::CIndex2d GetLeftTop() const
Definition CRect.h:271
void SetLeftTop(istd::CIndex2d position)
Definition CRect.h:277
void SetHorizontalRange(const istd::CIntRange &range)
Definition CRect.h:394
void Expand(const CRect &rect)
Expand rectangle using second rectangle.
Definition CRect.h:573
void SetRightTop(istd::CIndex2d position)
Definition CRect.h:290
istd::CIndex2d GetLeftBottom() const
Definition CRect.h:297
static const i2d::CRect & GetEmpty()
Get empty rectangle with all values set to 0.
Definition CRect.h:667
void Intersection(const CRect &rect)
Calculate intersection of this and second rectangle and stores result in this object.
Definition CRect.h:596
bool IsNull() const
Check if all parameters are 0.
Definition CRect.h:444
void Translate(istd::CIndex2d point)
Move rectangle.
Definition CRect.h:623
void SetRightBottom(istd::CIndex2d position)
Definition CRect.h:316
bool operator==(const CRect &rect) const
Definition CRect.h:653
int GetTop() const
Definition CRect.h:334
void Union(const CRect &rect)
Calculate union of this and second rectangle and stores result in this object.
Definition CRect.h:499
void SetLeftBottom(istd::CIndex2d position)
Definition CRect.h:303
void SetBottom(int bottom)
Definition CRect.h:364
void SetRight(int right)
Definition CRect.h:352
istd::CIndex2d GetCenter() const
Definition CRect.h:418
int GetRight() const
Definition CRect.h:346
CRect GetExpanded(const CRect &rect) const
Get expanded rectangle.
Definition CRect.h:583
istd::CIntRange & GetHorizontalRangeRef()
Definition CRect.h:388
int GetBottom() const
Definition CRect.h:358
bool IsInside(const istd::CIndex2d &point) const
Check if specified point lies inside.
Definition CRect.h:456
CRect GetTranslated(istd::CIndex2d point) const
Get moved rectangle.
Definition CRect.h:632
const istd::CIntRange & GetHorizontalRange() const
Definition CRect.h:382
CRect GetIntersection(const CRect &rect) const
Get intersection of two rectangles.
Definition CRect.h:610
CRect & operator=(const CRect &rect)
Definition CRect.h:644
void SetVerticalRange(const istd::CIntRange &range)
Definition CRect.h:412
int GetLeft() const
Definition CRect.h:323
istd::CIndex2d GetRightBottom() const
Definition CRect.h:310
int GetWidth() const
Definition CRect.h:370
void GetUnion(const CRect &rect, CRect &result) const
Get union of two rectangles.
Definition CRect.h:514
bool operator!=(const CRect &rect) const
Definition CRect.h:659
istd::CIntRange & GetVerticalRangeRef()
Definition CRect.h:406
bool IsEmpty() const
Check if rectangle is empty.
Definition CRect.h:432
const istd::CIntRange & GetVerticalRange() const
Definition CRect.h:400
bool IsValid() const
Check if rectangle is valid.
Definition CRect.h:426
i2d::CRectangle GetRectangle() const
Definition CRect.h:261
void Reset()
Set all members to 0.
Definition CRect.h:492
bool IsOutside(const CRect &rect) const
Check if specified rectangle lies fully outside.
Definition CRect.h:478
istd::CIndex2d GetRightTop() const
Definition CRect.h:284
bool IsValidNonEmpty() const
Return true if the rectangle is valid and it is not empty.
Definition CRect.h:438
Definition of rectangle area orthogonal to axis of coordination system.
Definition CRectangle.h:31
double GetTop() const
Get value of top boundary.
Definition CRectangle.h:345
double GetLeft() const
Get value of left boundary.
Definition CRectangle.h:339
double GetBottom() const
Get value of bottom boundary.
Definition CRectangle.h:357
double GetRight() const
Get value of right boundary.
Definition CRectangle.h:351
Definition of simple 2D size based on integer values.
Definition CSize.h:22
Index implementation for addressing elements in 2D-space.
Definition CIndex2d.h:25
int GetX() const
Definition CIndex2d.h:105
int GetY() const
Definition CIndex2d.h:117
bool IsOutsideOf(const TRange &range) const
Check if this range is outside of the given range.
Definition TRange.h:442
ValueType GetLength() const
Get length of this range.
Definition TRange.h:385
void Unite(const TRange &range)
Set this range to be union of two ranges.
Definition TRange.h:520
void Reset()
Set this range to be empty.
Definition TRange.h:377
void SetMinValue(ValueType minValue)
Set the bottom value.
Definition TRange.h:349
ValueType GetMaxValue() const
Get the top value.
Definition TRange.h:356
bool Contains(ValueType value) const
Returns true, if value is in range between top and bottom.
Definition TRange.h:416
void Expand(const TRange &range)
Set this range to be expanded.
Definition TRange.h:566
void Intersection(const TRange &range)
Set this range to be intersection of two ranges.
Definition TRange.h:476
ValueType GetMinValue() const
Get the bottom value.
Definition TRange.h:335
bool IsEmpty() const
Return true if the bottom value is equal to the top value.
Definition TRange.h:321
bool IsValidNonEmpty() const
Return true if the range is valid and it is not empty.
Definition TRange.h:328
bool IsValid() const
Return true if the bottom value is smaller or equal then the top value.
Definition TRange.h:314
static constexpr TRange & GetInvalid()
Definition TRange.h:790
void SetMaxValue(ValueType maxValue)
Set the top value.
Definition TRange.h:370
static constexpr TRange & GetNull()
Definition TRange.h:783
Contains the 2D objects.
Definition CAffine2d.h:15
istd::TRange< int > CIntRange
Definition TRange.h:808

© Witold Gantzke and Kirill Lepskiy