InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PMRect.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Lonnie Millett
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //========================================================================================
23 
24 #ifndef __PMRECT__
25 #define __PMRECT__
26 
27 #include "PMReal.h"
28 #include "PMPoint.h"
29 
30 //----------------------------------------------------------------------------------------
31 // Forward declarations
32 //----------------------------------------------------------------------------------------
33 class PMRect;
34 class IPMStream;
35 class PMMatrix;
36 namespace dvacore {
37  namespace geom {
38  template <class T> class RectT;
39  }
40 }
41 
42 //----------------------------------------------------------------------------------------
43 // Typedefs
44 // The following types are designed primarily for use in parameter lists to aid in
45 // determining what coordinate space a method or function expects a parameter to be in.
46 // They are just synonyms for PMRect
47 //----------------------------------------------------------------------------------------
48 typedef PMRect PBPMRect; // rect in "Pasteboard" coordinates
49 typedef PMRect IPMRect; // rect in "Inner" coordinates of page item
51 
52 #ifdef PUBLIC_BUILD
53 #endif
54 
55 extern const PMRect kEmptyRect;
56 
57 
58  SysRect ToSys(const PMRect& r);
59  bool16 Intersect(const PMRect& r1, const PMRect& r2);
60  bool16 Intersect(const PMRect& r1, const PMMatrix& r1_to_r2_xform, const PMRect& r2);
61  PMRect MakeIntersect(const PMRect& r1, const PMMatrix& r1_to_r2_xform, const PMRect& r2);
62  bool16 IntersectOrAbut(const PMRect& r1, const PMRect& r2);
63  bool16 IntersectOrAbut(const PMRect& r1, const PMMatrix& r1_to_r2_xform, const PMRect& r2);
64  PMRect MakeIntersectOrAbut(const PMRect& r1, const PMMatrix& r1_to_r2_xform, const PMRect& r2);
65  PMRect Intersection(const PMRect& r1, const PMRect& r2, const PMRect& emptyRect = kEmptyRect);
66  PMRect IntersectionOrAbut(const PMRect& r1, const PMRect& r2, const PMRect& emptyRect = kEmptyRect);
67  PMRect Union(const PMRect& r1, const PMRect& r2);
68  bool16 UnionIsRect(const PMRect& r1, const PMRect& r2);
69 //find a rectangle of greatest area centered and contained within the parallelogram formed by transforming rectangle r with matrix m.
70  PMRect MaximalInscribedRectangle( const PMRect& r, const PMMatrix& m);
71 //return the area of r2 contained in the parallelogram you get when r1 is transformed by r1_to_r2_xform
72  PMReal IntersectionArea( const PMRect& r1, const PMMatrix& r1_to_r2_xform, const PMRect& r2);
73 
74 //----------------------------------------------------------------------------------------
75 // PMRect
76 //----------------------------------------------------------------------------------------
77 
84 class PMRect
85 {
86  public:
87  typedef base_type data_type;
88 
92  enum PointIndex {
97 
106 
108  kLeftTop = 4,
115 
117  kCenter = 8,
118 
121  };
122 
127  constexpr PMRect() noexcept : left(0.0), top(0.0), right(0.0), bottom(0.0)
128  {
129  }
130 
138  explicit PMRect(const PMPoint& point) noexcept;
139 
146  PMRect(const PMPoint& one_corner, const PMPoint& opposing_corner) noexcept;
147 
157  PMRect(const PMReal& l, const PMReal& t, const PMReal& r, const PMReal& b) noexcept;
158 
164  PMRect(const SysRect& r) noexcept;
165 
166 
172  explicit PMRect( const dvacore::geom::RectT<float>& r) noexcept;
173 
174 
175 #if MACINTOSH //on Windows, an Int32Rect is a SysRect
176 
182  explicit PMRect( const Int32Rect& r) noexcept;
183 #endif
184 
185  constexpr PMRect(const PMRect &) noexcept = default;
186  constexpr PMRect(PMRect &&) noexcept = default;
187  PMRect &operator =(const PMRect &) noexcept = default;
188  PMRect &operator =(PMRect &&) noexcept = default;
189 
190  // Outbound Conversions
191 
198  friend SysRect ToSys(const PMRect& r);
199 
206  friend dvacore::geom::RectT<float> ToRectF32_(const PMRect& r);
207 
208 
215  friend Int32Rect ToInt32Rect_(const PMRect& r);
216 
217 
218  // Query Methods
219 
226  bool16 IsEmpty() const noexcept;
227 
233  bool16 IsCorner(PointIndex whichPoint) const noexcept;
234 
240  bool16 IsMiddle(PointIndex whichPoint) const noexcept;
241 
248  bool16 PointIn(const PMPoint& pt) const noexcept;
249 
258  bool16 ContainsPoint(const PMPoint& pt) const noexcept;
259 
267  bool16 RectIn(const PMRect& r) const noexcept;
268 
277  bool16 ContainsRect( const PMRect& r) const noexcept;
278 
287  bool16 ContainsRect( const PMRect& r, const PMMatrix& r_to_this_xform) const;
288 
296  PMReal AspectRatio() const;
297 
305  PMReal DistanceTo(const PMPoint& pt) const;
306 
316  friend bool16 Intersect(const PMRect& r1, const PMRect& r2);
317  friend bool16 Intersect_exact( const PMRect&r1, const PMRect& r2);
318  friend bool16 Intersect(const PMRect& r1, const PMMatrix& xform, const PMRect& r2);
319  friend PMRect MakeIntersect(const PMRect& r1, const PMMatrix& xform, const PMRect& r2);
320 
329  friend bool16 IntersectOrAbut(const PMRect& r1, const PMRect& r2);
330  friend bool16 IntersectOrAbut_exact(const PMRect&r1, const PMRect& r2);
331  friend bool16 IntersectOrAbut(const PMRect& r1, const PMMatrix& xform, const PMRect& r2);
332  friend PMRect MakeIntersectOrAbut(const PMRect& r1, const PMMatrix& xform, const PMRect& r2);
333 
343  friend PMRect Intersection(const PMRect& r1, const PMRect& r2, const PMRect& emptyRect);
344 
355  friend PMRect IntersectionOrAbut(const PMRect& r1, const PMRect& r2, const PMRect& emptyRect);
356 
357  // Accessor Methods
358 
364  const PMReal& Left() const noexcept;
365 
371  PMReal& Left() noexcept;
372 
378  void Left(const PMReal& c) noexcept;
379 
385  const PMReal& Top() const noexcept;
386 
392  PMReal& Top() noexcept;
393 
399  void Top(const PMReal& c) noexcept;
400 
406  const PMReal& Right() const noexcept;
407 
413  PMReal& Right() noexcept;
414 
420  void Right(const PMReal& c) noexcept;
421 
427  const PMReal& Bottom() const noexcept;
428 
434  PMReal& Bottom() noexcept;
435 
441  void Bottom(const PMReal& c) noexcept;
442 
448  PMPoint LeftTop(void) const noexcept;
449 
455  void LeftTop(const PMPoint& pt) noexcept;
456 
462  PMPoint RightBottom(void) const noexcept;
463 
469  void RightBottom(const PMPoint& pt) noexcept;
470 
476  PMPoint RightTop(void) const noexcept;
477 
483  void RightTop(const PMPoint& pt) noexcept;
484 
490  PMPoint LeftBottom(void) const noexcept;
491 
497  void LeftBottom(const PMPoint& pt) noexcept;
498 
506  PMReal Height() const noexcept;
507 
515  PMReal Width() const noexcept;
516 
524  PMPoint Dimensions() const noexcept;
525 
532  void SetHeight(const PMReal& height);
533 
540  void SetWidth(const PMReal& width);
541 
549  void SetDimensions(const PMReal& width, const PMReal& height);
550 
556  PMReal GetHCenter() const;
557 
563  PMReal GetVCenter() const;
564 
570  PMPoint GetCenter() const;
571 
577  PMPoint GetPoint(PointIndex whichPoint) const;
578 
586  PointIndex GetPointIndex(const PMPoint& aPoint) const;
587 
593  PMPoint GetOppositePoint(PointIndex whichPoint) const;
594 
602  PointIndex GetOppositePointIndex(PointIndex whichPoint) const;
603 
604  // Manipulation Methods
605 
612  PMRect& SetEmpty() noexcept;
613 
620  bool16 Normalize() noexcept;
621 
628  PMRect& Round();
629 
641  PointIndex MovePointTo(PointIndex whichPoint, const PMPoint& toPt);
642 
643  PMRect& MoveRel(const PMReal& dx, const PMReal& dy);
644  PMRect& MoveRel(const PMPoint& point);
645  PMRect& MoveTo(const PMReal& dx, const PMReal& dy);
646  PMRect& MoveTo(const PMPoint& point);
647  PMRect& CenterOn(const PMReal& dx, const PMReal& dy);
648  PMRect& CenterOn(const PMPoint& point);
649 
658  PMRect& Inset(const PMReal& dx, const PMReal& dy);
659 
666  bool16 ConstrainTo(PMRect& constrainedRect);
667 
689  void Union (const PMRect& rhs);
690 
698  void Expand (const PMPoint& pt);
699 
717  friend PMRect Union(const PMRect& r1, const PMRect& r2);
718  friend bool16 UnionIsRect(const PMRect& r1, const PMRect& r2);
719 
720  // Comparison Operators
721 
731  friend bool16 operator ==(const PMRect& a, const PMRect& b) noexcept;
732 
742  friend bool16 operator !=(const PMRect& a, const PMRect& b) noexcept;
743 
744  // Utility functions
745 
753  static int32 PointIndexToIndex (PMRect::PointIndex ptIndex);
754 
762  static PMRect::PointIndex IndexToPointIndex (int32 index);
763 
764  // Streaming
765 
771  void ReadWrite (IPMStream* iPMStream);
772 
773 // VC6 compiler error, CWE
774 #if (_MSC_VER >= 1200) // VC6 or greater
775  public:
776 #else
777  private:
778 #endif
779  PMReal left;
780  PMReal top;
781  PMReal right;
782  PMReal bottom;
783  friend bool16 Intersect_( const PMRect& r1, const PMMatrix& r1_to_r2_xform, const PMRect& r2, PMPoint* p);
784  friend bool16 IntersectOrAbut_( const PMRect& r1, const PMMatrix& r1_to_r2_xform, const PMRect& r2, PMPoint* p);
785 
786 };
787 
788 
789 
790 //----------------------------------------------------------------------------------------
791 // Inlines:
792 //----------------------------------------------------------------------------------------
793 
794 inline bool16 PMRect::IsEmpty() const noexcept
795 {
796  return ((left == right) || (top == bottom));
797 }
798 
799 inline bool16 PMRect::IsCorner(PointIndex whichPoint) const noexcept
800 {
801  ASSERT_MSG(whichPoint >= kFirstPoint && whichPoint <= kLastPoint, "PMRect::IsCorner - whichPoint >= kFirstPoint && whichPoint <= kLastPoint");
802  return (whichPoint >= kLeftTop && whichPoint != kCenter);
803 }
804 
805 inline bool16 PMRect::IsMiddle(PointIndex whichPoint) const noexcept
806 {
807  ASSERT_MSG(whichPoint >= kFirstPoint && whichPoint <= kLastPoint, "PMRect::IsMiddle - whichPoint >= kFirstPoint && whichPoint <= kLastPoint");
808  return (whichPoint >= kMiddleTop && whichPoint <= kRightMiddle);
809 }
810 
811 inline bool16 PMRect::PointIn(const PMPoint& pt) const noexcept
812 {
813  return (pt.x >= left) && (pt.x <= right) && (pt.y >= top) && (pt.y <= bottom);
814 }
815 
816 inline bool16 PMRect::ContainsPoint(const PMPoint& pt) const noexcept
817 {
818  return
819  ::ToDouble(pt.x) >= ::ToDouble(left)
820  && ::ToDouble(pt.x) <= ::ToDouble(right)
821  && ::ToDouble(pt.y) >= ::ToDouble(top)
822  && ::ToDouble(pt.y) <= ::ToDouble(bottom);
823 }
824 
825 
826 inline bool16 PMRect::RectIn(const PMRect& r) const noexcept
827 {
828  return (top <= r.top && bottom >= r.bottom) && (left <= r.left && right >= r.right);
829 }
830 
831 inline bool16 PMRect::ContainsRect(const PMRect& r) const noexcept
832 {
833  return
834  ::ToDouble(top) <= ::ToDouble(r.top)
835  && ::ToDouble(bottom) >= ::ToDouble(r.bottom)
836  && ::ToDouble(left) <= ::ToDouble(r.left)
837  && ::ToDouble(right) >= ::ToDouble(r.right);
838 }
839 
840 inline const PMReal& PMRect::Left() const noexcept
841 {
842  return left;
843 }
844 
845 inline PMReal& PMRect::Left() noexcept
846 {
847  return left;
848 }
849 
850 inline void PMRect::Left(const PMReal& c) noexcept
851 {
852  left = c;
853 }
854 
855 inline const PMReal& PMRect::Top() const noexcept
856 {
857  return top;
858 }
859 
860 inline PMReal& PMRect::Top() noexcept
861 {
862  return top;
863 }
864 
865 inline void PMRect::Top(const PMReal& c) noexcept
866 {
867  top = c;
868 }
869 
870 inline const PMReal& PMRect::Right() const noexcept
871 {
872  return right;
873 }
874 
875 inline PMReal& PMRect::Right() noexcept
876 {
877  return right;
878 }
879 
880 inline void PMRect::Right(const PMReal& c) noexcept
881 {
882  right = c;
883 }
884 
885 inline const PMReal& PMRect::Bottom() const noexcept
886 {
887  return bottom;
888 }
889 
890 inline PMReal& PMRect::Bottom() noexcept
891 {
892  return bottom;
893 }
894 
895 inline void PMRect::Bottom(const PMReal& c) noexcept
896 {
897  bottom = c;
898 }
899 
900 inline PMPoint PMRect::LeftTop() const noexcept
901 {
902  return PMPoint(left, top);
903 }
904 
905 inline void PMRect::LeftTop(const PMPoint& pt) noexcept
906 {
907  left = pt.X(); top = pt.Y();
908 }
909 
910 inline PMPoint PMRect::RightBottom() const noexcept
911 {
912  return PMPoint(right, bottom);
913 }
914 
915 inline void PMRect::RightBottom(const PMPoint& pt) noexcept
916 {
917  right = pt.X(); bottom = pt.Y();
918 }
919 
920 inline PMPoint PMRect::RightTop() const noexcept
921 {
922  return PMPoint(right, top);
923 }
924 
925 inline void PMRect::RightTop(const PMPoint& pt) noexcept
926 {
927  right = pt.X(); top = pt.Y();
928 }
929 
930 inline PMPoint PMRect::LeftBottom() const noexcept
931 {
932  return PMPoint(left, bottom);
933 }
934 
935 inline void PMRect::LeftBottom(const PMPoint& pt) noexcept
936 {
937  left = pt.X(); bottom = pt.Y();
938 }
939 
940 inline PMReal PMRect::Height() const noexcept
941 {
942  return bottom - top;
943 }
944 
945 inline PMReal PMRect::Width() const noexcept
946 {
947  return right - left;
948 }
949 
950 inline PMPoint PMRect::Dimensions() const noexcept
951 {
952  return PMPoint(Width(), Height());
953 }
954 
955 inline PMRect::PointIndex PMRect::GetOppositePointIndex(PointIndex whichPoint) const
956 {
957  return (whichPoint == kCenter) ? kCenter :
958  PointIndex((whichPoint + 2) % 4 + (IsCorner(whichPoint) ? 4 : 0));
959 }
960 
961 
962 inline PMPoint PMRect::GetOppositePoint(PointIndex whichPoint) const
963 {
964  return GetPoint(GetOppositePointIndex(whichPoint));
965 }
966 
967 
968 inline PMRect& PMRect::SetEmpty() noexcept
969 {
970  right = left;
971  bottom = top;
972 
973  return *this;
974 }
975 
976 inline PMRect& PMRect::MoveRel(const PMReal& dx, const PMReal& dy)
977 {
978  left += dx; right += dx; top += dy; bottom += dy;
979  return *this;
980 }
981 
982 inline PMRect& PMRect::MoveRel(const PMPoint& point)
983 {
984  MoveRel(point.X(), point.Y());
985  return *this;
986 }
987 
988 inline PMRect& PMRect::MoveTo(const PMReal& dx, const PMReal& dy)
989 {
990  right += (dx - left); bottom += (dy - top); left = dx; top = dy;
991  return *this;
992 }
993 
994 inline PMRect& PMRect::MoveTo(const PMPoint& point)
995 {
996  MoveTo(point.X(), point.Y());
997  return *this;
998 }
999 
1000 inline PMRect& PMRect::CenterOn(const PMPoint& point)
1001 {
1002  CenterOn(point.X(), point.Y());
1003  return *this;
1004 }
1005 
1006 inline PMRect& PMRect::Inset(const PMReal& dx, const PMReal& dy)
1007 {
1008  left += dx; top += dy; right -= dx; bottom -= dy;
1009  return *this;
1010 }
1011 
1012 inline bool16 operator ==(const PMRect& a, const PMRect& b) noexcept
1013 {
1014  return (a.left == b.left && a.top == b.top && a.right == b.right && a.bottom == b.bottom);
1015 }
1016 
1017 inline bool16 operator !=(const PMRect& a, const PMRect& b) noexcept
1018 {
1019  return (a.left != b.left || a.top != b.top || a.right != b.right || a.bottom != b.bottom);
1020 }
1021 
1022 constexpr PMRect kZeroRect; //zeroed out in default constructor
1023 
1024 #endif