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

© 2007-2017 Witold Gantzke and Kirill Lepskiy