MBR最小外包矩形對象的封裝

最小外包矩形對象的封裝

最小外包矩形MBR就是包圍圖元,且平行於X,Y軸的最小外接矩形。

  1.   template<class T>  
  2.   struct CGeoRect  
  3.   {  
  4.       // Four corner points  
  5.       T m_minX;   
  6.       T m_minY;  
  7.       T m_maxX;  
  8.       T m_maxY;  
  9.   
  10.       //  
  11.       // Constructors and deconstructor  
  12.       //  
  13.       /** 
  14.       * Default constructor with no any sense. 
  15. * 
  16. * Note:  
  17. * 1) If passed by self-defined data type, it must have copy constructor being responsible for 
  18. * converting doulbe to it. 
  19. * 2) Since we adopt UEZERO as invalid point which makes sense when not initialized before being used,  
  20. * we had better notice the assertion happen before correctly using one geometry. 
  21.       */  
  22.       CGeoRect() : m_minX(-1.), m_minY(-1.), m_maxX(-1.), m_maxY(-1.)  
  23.       {  
  24.       }  
  25.         
  26.       /** 
  27.       * Default constructor with specified corner values. 
  28.       */  
  29.       CGeoRect(const T &minX, const T &minY, const T &maxX, const T& maxY) : m_minX(minX),   
  30.     m_minY(minY), m_maxX(maxX), m_maxY(maxY)  
  31.       {  
  32.       }  
  33.   
  34.       /** 
  35.       * Copy constructor. 
  36. * 
  37. * Note: If passed by self-defined data type, it must overload assignment operator 
  38.       */  
  39.       CGeoRect(const CGeoRect &other)  
  40.       {  
  41.           m_minX = other.m_minX;  
  42.           m_maxY = other.m_maxY;  
  43.           m_maxX = other.m_maxX;  
  44.           m_minY = other.m_minY;  
  45.       }  
  46.   
  47.       /** 
  48. * Deconstructor. 
  49. * 
  50.       * Note:Since we want to take it as inner built-in type, it no need to inherit children and 
  51.       * virtual constructor may cause different behavior when against different compilers 
  52.       */  
  53.       ~CGeoRect()  
  54.       {  
  55.       }  
  56.   
  57.       //  
  58.       // Simple interfaces  
  59.       //  
  60. /** 
  61.       * Check whether is an valid rectangle. 
  62. **/  
  63. bool IsValid() const  
  64. {  
  65.     return !((m_minX == m_maxX && m_minX == static_cast<T>(UE_INVALIDCOORD)) ||  
  66.         (m_minY == m_maxY && m_minY == static_cast<T>(UE_INVALIDCOORD)));  
  67. }  
  68.   
  69.       /** 
  70.       * Disable this rectangle. 
  71.       */  
  72.       void Empty()  
  73.       {  
  74.           m_minX = m_maxX = m_minY = m_maxY = static_cast<T>(UE_ZERO);  
  75.       }  
  76.   
  77.       /** 
  78.       * Check whether is an rectangle have an validate area. 
  79.       */  
  80.       bool IsEmpty() const  
  81.       {  
  82.     //assert(IsValid());  
  83.   
  84.     return (m_minX == m_maxX ||   
  85.         m_maxY == m_minY ||   
  86.         (m_maxX - m_minX) < static_cast<T>(RECTLIMIT) ||   
  87.         (m_maxY - m_minY) < static_cast<T>(RECTLIMIT));  
  88.       }  
  89.   
  90.       /** 
  91.       * Check whether intersect another rectangle. 
  92.       */  
  93.       inline bool IsIntersect(const CGeoRect<T> &other) const  
  94.       {  
  95.     //assert(IsValid());  
  96.           if(m_maxY < other.m_minY ||  
  97.         m_minY > other.m_maxY ||  
  98.         m_maxX < other.m_minX ||   
  99.         m_minX > other.m_maxX)  
  100.           {  
  101.               return false;  
  102.           }  
  103.   
  104.           return true;  
  105.       }  
  106.   
  107.       /** 
  108.       * Check whether contains another smaller rectangle including overstacked borders. 
  109.       */  
  110.       bool IsContain(const CGeoRect<T> &other) const  
  111.       {  
  112.     if(IsValid())  
  113.     {  
  114.         if(other.m_minX >= m_minX &&   
  115.             other.m_maxX <= m_maxX &&   
  116.             other.m_minY >= m_minY &&   
  117.             other.m_maxY <= m_maxY)  
  118.         {  
  119.             return true;  
  120.         }  
  121.     }  
  122.   
  123.           return false;  
  124.       }  
  125.   
  126.       /** 
  127.       * Check whether contain one point. 
  128.       */  
  129.       bool IsContain(const CGeoPoint<T> &point)  
  130.       {  
  131.     if(IsValid())  
  132.     {  
  133.         // Exception even if given this rectangle as one line segment  
  134.         double minX = m_minX;  
  135.         double maxX = m_maxX;  
  136.         if(m_minX == m_maxX)  
  137.         {  
  138.             minX -= 5;  
  139.             maxX += 5;  
  140.         }  
  141.   
  142.         // Exception even if given this rectangle as one line segment  
  143.         double minY = m_minY;  
  144.         double maxY = m_maxY;  
  145.         if(m_minY == m_maxY)  
  146.         {  
  147.             minY -= 5;  
  148.             maxY += 5;  
  149.         }  
  150.   
  151.         if(point.m_x < minX ||  
  152.             point.m_x > maxX ||  
  153.             point.m_y < minY ||  
  154.             point.m_y > maxY)  
  155.         {  
  156.             return false;  
  157.         }  
  158.   
  159.            return true;  
  160.     }  
  161.   
  162.     return false;  
  163.       }  
  164.   
  165.       /** 
  166.       * Check whether contain one point. 
  167.       */  
  168.       bool IsContain(const CGeoPoint<T> &point) const  
  169.       {  
  170.     //assert(IsValid());  
  171.           if(point.m_x < m_minX ||  
  172.               point.m_x > m_maxX ||  
  173.               point.m_y < m_minY ||  
  174.               point.m_y > m_maxY)  
  175.           {  
  176.               return false;  
  177.           }  
  178.   
  179.           return true;  
  180.       }  
  181.   
  182.       /** 
  183.       * Union two rectangles. 
  184.       */  
  185.       void Union(const CGeoRect<T> &other)  
  186.       {  
  187.     //assert(IsValid());  
  188.   
  189.           if(m_minX > other.m_minX)  
  190.           {  
  191.               m_minX = other.m_minX;  
  192.           }  
  193.           if(m_maxX < other.m_maxX)  
  194.           {  
  195.               m_maxX = other.m_maxX;  
  196.           }  
  197.   
  198.           if(m_minY > other.m_minY)  
  199.           {  
  200.               m_minY = other.m_minY;  
  201.           }  
  202.           if(m_maxY < other.m_maxY)  
  203.           {  
  204.               m_maxY = other.m_maxY;  
  205.           }  
  206.       }  
  207.   
  208. /** 
  209. * Enlarge this rectangle. 
  210. */  
  211. bool Inflate(T xShift, T yShift)  
  212. {  
  213.     //assert(IsValid());  
  214.   
  215.     m_minX -= xShift;  
  216.     m_maxX += xShift;  
  217.     m_minY -= yShift;  
  218.     m_maxY += yShift;  
  219.   
  220.     return !IsEmpty();  
  221. }  
  222.   
  223. /** 
  224. * Shrink this rectangle. 
  225. */  
  226. bool Deflate(T xShift, T yShift)  
  227. {  
  228.     //assert(IsValid());  
  229.   
  230.     m_minX += xShift;  
  231.     m_maxX -= xShift;  
  232.     m_minY += yShift;  
  233.     m_maxY -= yShift;  
  234.   
  235.     return !IsEmpty();  
  236. }  
  237.   
  238.       /** 
  239.       * Get rectangle width. 
  240.       */  
  241.       T Width() const  
  242.       {  
  243.     //assert(IsValid());  
  244.   
  245.           return m_maxX - m_minX;  
  246.       }  
  247.   
  248.       /** 
  249.       * Get rectangle height. 
  250.       */  
  251.       T Height() const  
  252.       {  
  253.     //assert(IsValid());  
  254.           return m_maxY - m_minY;  
  255.       }  
  256.   
  257.       //  
  258.       // Useful overloaded operators  
  259.       //  
  260.       /** 
  261.       * Overload assignment operator. 
  262.       */  
  263.       const CGeoRect &operator = (const CGeoRect &other)  
  264.       {  
  265.     //assert(other.IsValid());  
  266.           if(this == &other)  
  267.           {  
  268.               return *this;  
  269.           }  
  270.   
  271.           m_minX = other.m_minX;  
  272.           m_maxY = other.m_maxY;  
  273.           m_maxX = other.m_maxX;  
  274.           m_minY = other.m_minY;  
  275.   
  276.           return *this;  
  277.       }  
  278.   
  279.       /** 
  280.       * Overload bool operator. 
  281.       */  
  282.       bool operator == (const CGeoRect &other) const  
  283.       {  
  284.     //assert(IsValid());  
  285.     if(m_minX == other.m_minX &&   
  286.         m_maxY == other.m_maxY &&   
  287.         m_maxX == other.m_maxX &&   
  288.         m_minY == other.m_minY)  
  289.           {  
  290.               return true;  
  291.           }  
  292.   
  293.           return false;  
  294.       }  
  295.   }; 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章