"Fossies" - the Fresh Open Source Software Archive

Member "muscle/support/Rect.h" (28 Nov 2019, 12478 Bytes) of package /linux/privat/muscle7.52.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Rect.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 7.41_vs_7.50.

    1 /* This file is Copyright 2000-2013 Meyer Sound Laboratories Inc.  See the included LICENSE.txt file for details. */
    2 
    3 /*******************************************************************************
    4 /
    5 /   File:      Rect.h
    6 /
    7 /   Description:     version of Be's BRect class
    8 /
    9 *******************************************************************************/
   10 
   11 #ifndef MuscleRect_h
   12 #define MuscleRect_h
   13 
   14 #include "support/PseudoFlattenable.h"
   15 #include "support/Point.h"
   16 
   17 namespace muscle {
   18 
   19 /** A portable version of Be's BRect class. */
   20 class Rect MUSCLE_FINAL_CLASS : public Tuple<4,float>, public PseudoFlattenable
   21 {
   22 public:
   23    /** Default Constructor.  
   24      * Creates a rectangle with upper left point (0,0), and lower right point (-1,-1).
   25      * Note that this rectangle has a negative area!  (that is to say, it's imaginary)
   26      */ 
   27    Rect() {Set(0.0f,0.0f,-1.0f,-1.0f);}
   28 
   29    /** Constructor where you specify the left, top, right, and bottom coordinates 
   30      * @param l the left-edge coordinate
   31      * @param t the top-edge coordinate
   32      * @param r the right-edge coordinate
   33      * @param b the bottom-edge coordinate
   34      */
   35    Rect(float l, float t, float r, float b) {Set(l,t,r,b);}
   36 
   37    /** @copydoc DoxyTemplate::DoxyTemplate(const DoxyTemplate &) */
   38    Rect(const Rect & rhs) : Tuple<4,float>(rhs) {/* empty */}
   39 
   40    /** Constructor where you specify the leftTop point and the rightBottom point.
   41      * @param leftTop a Point to indicate the left/top corner of this Rect
   42      * @param rightBottom a Point to indicate the right/bottom corner of this Rect
   43      */
   44    Rect(const Point leftTop, Point rightBottom) {Set(leftTop.x(), leftTop.y(), rightBottom.x(), rightBottom.y());}
   45 
   46    /** Destructor */
   47    ~Rect() {/* empty */}
   48 
   49    /** convenience method to get the left edge of this Rect */
   50    inline float   left()   const {return (*this)[0];}
   51 
   52    /** convenience method to set the left edge of this Rect */
   53    inline float & left()         {return (*this)[0];}
   54 
   55    /** convenience method to get the top edge of this Rect */
   56    inline float   top()    const {return (*this)[1];}
   57 
   58    /** convenience method to set the top edge of this Rect */
   59    inline float & top()          {return (*this)[1];}
   60 
   61    /** convenience method to get the right edge of this Rect */
   62    inline float   right()  const {return (*this)[2];}
   63 
   64    /** convenience method to set the right edge of this Rect */
   65    inline float & right()        {return (*this)[2];}
   66 
   67    /** convenience method to get the bottom edge of this Rect */
   68    inline float   bottom() const {return (*this)[3];}
   69 
   70    /** convenience method to set the bottom edge of this Rect */
   71    inline float & bottom()       {return (*this)[3];}
   72 
   73    /** Set a new position for the rectangle.
   74      * @param l the new left-edge coordinate
   75      * @param t the new top-edge coordinate
   76      * @param r the new right-edge coordinate
   77      * @param b the new bottom-edge coordinate
   78      */
   79    inline void Set(float l, float t, float r, float b)
   80    {
   81       left()   = l;
   82       top()    = t;
   83       right()  = r;
   84       bottom() = b;
   85    }
   86 
   87    /** Print debug information about this rectangle to stdout or to a file you specify.
   88      * @param optFile If non-NULL, the text will be printed to this file.  If left as NULL, stdout will be used as a default.
   89      */
   90    void PrintToStream(FILE * optFile = NULL) const
   91    {
   92       if (optFile == NULL) optFile = stdout;
   93       fprintf(optFile, "Rect: leftTop=(%f,%f) rightBottom=(%f,%f)\n", left(), top(), right(), bottom());
   94     }
   95 
   96    /** Returns the left top corner of the rectangle. */
   97    inline Point LeftTop() const {return Point(left(), top());}
   98 
   99    /** Returns the right bottom corner of the rectangle. */
  100    inline Point RightBottom() const {return Point(right(), bottom());}
  101 
  102    /** Returns the left bottom corner of the rectangle. */
  103    inline Point LeftBottom() const {return Point(left(), bottom());}
  104 
  105    /** Returns the right top corner of the rectangle. */
  106    inline Point RightTop() const {return Point(right(), top());}
  107 
  108    /** Set the left top corner of the rectangle.
  109      * @param p the new left/top corner for this Rect
  110      */
  111    inline void SetLeftTop(const Point & p) {left() = p.x(); top() = p.y();}
  112 
  113    /** Set the right bottom corner of the rectangle.
  114      * @param p the new right/bottom corner for this Rect
  115      */
  116    inline void SetRightBottom(const Point & p) {right() = p.x(); bottom() = p.y();}
  117 
  118    /** Set the left bottom corner of the rectangle.
  119      * @param p the new left/bottom corner for this Rect
  120      */
  121    inline void SetLeftBottom(const Point & p) {left() = p.x(); bottom() = p.y();}
  122 
  123    /** Set the right top corner of the rectangle. 
  124      * @param p the new right/top corner for this Rect
  125      */
  126    inline void SetRightTop(const Point & p) {right() = p.x(); top() = p.y();}
  127 
  128    /** Makes the rectangle smaller by the amount specified in both the x and y dimensions
  129      * @param p a Point whose dimensions indicate how much smaller to make our x and y dimensions on each edge, respectively
  130      */
  131    inline void InsetBy(const Point & p) {InsetBy(p.x(), p.y());}
  132 
  133    /** Makes the rectangle smaller by the amount specified in both the x and y dimensions
  134      * @param dx the number of pixels right to move the left edge; and the number of pixels left to move the right edge
  135      * @param dy the number of pixels down to move the top edge; and the number of pixels up to move the bottom edge
  136      */
  137    inline void InsetBy(float dx, float dy) {left() += dx; top() += dy; right() -= dx; bottom() -= dy;}
  138 
  139    /** Translates the rectangle by the amount specified in both the x and y dimensions 
  140      * @param p a Point whose dimensions indicate how far to translate this Rect in each direction
  141      */
  142    inline void OffsetBy(const Point & p) {OffsetBy(p.x(), p.y());}
  143 
  144    /** Translates the rectangle by the amount specified in both the x and y dimensions
  145      * @param dx how far to the right to move our left and right edges
  146      * @param dy how far down to move our top and bottom edges
  147      */
  148    inline void OffsetBy(float dx, float dy) {left() += dx; top() += dy; right() += dx; bottom() += dy;}
  149 
  150    /** Translates the rectangle so that its top left corner is at the point specified. 
  151      * @param p the new upper-left corner for this rectangle
  152      */
  153    inline void OffsetTo(const Point & p) {OffsetTo(p.x(), p.y());}
  154 
  155    /** Translates the rectangle so that its top left corner is at the point specified.
  156      * @param x the new left edge for this Rectangle
  157      * @param y the new top edge for this Rectangle
  158      */
  159    inline void OffsetTo(float x, float y) {right() = x + Width(); bottom() = y + Height(); left() = x; top() = y;}
  160 
  161    /** If this Rect has negative width or height, modifies it to have positive width and height.   */
  162    void Rationalize() 
  163    {
  164       if (left() > right()) {float t = left(); left() = right(); right() = t;}
  165       if (top() > bottom()) {float t = top(); top() = bottom(); bottom() = t;}
  166    }
  167 
  168    /** Returns a rectangle whose area is the intersecting subset of this rectangle's and (r)'s
  169      * @param r the Rect to intersect with this rectangle
  170      */
  171    inline Rect operator&(const Rect & r) const 
  172    {
  173       Rect ret(*this);
  174       if (this != &r)
  175       {
  176          if (ret.left()   < r.left())   ret.left()   = r.left();
  177          if (ret.right()  > r.right())  ret.right()  = r.right();
  178          if (ret.top()    < r.top())    ret.top()    = r.top();
  179          if (ret.bottom() > r.bottom()) ret.bottom() = r.bottom();
  180       }
  181       return ret;
  182    }
  183 
  184    /** Returns a rectangle whose area is a superset of the union of this rectangle's and (r)'s
  185      * @param r the Rect to unify with this rectangle
  186      */
  187    inline Rect operator|(const Rect & r) const 
  188    {
  189       Rect ret(*this);
  190       if (this != &r)
  191       {
  192          if (r.left()   < ret.left())   ret.left()   = r.left();
  193          if (r.right()  > ret.right())  ret.right()  = r.right();
  194          if (r.top()    < ret.top())    ret.top()    = r.top();
  195          if (r.bottom() > ret.bottom()) ret.bottom() = r.bottom();
  196       }
  197       return ret;
  198    }
  199 
  200    /** Causes this rectangle to be come the union of itself and (rhs).
  201      * @param rhs the rectangle to unify with this one
  202      */
  203    inline Rect & operator |= (const Rect & rhs) {(*this) = (*this) | rhs; return *this;}
  204 
  205    /** Causes this rectangle to be come the intersection of itself and (rhs). 
  206      * @param rhs the rectangle to intersect with this one
  207      */
  208    inline Rect & operator &= (const Rect & rhs) {(*this) = (*this) & rhs; return *this;}
  209 
  210    /** Returns true iff this rectangle and (r) overlap in space. 
  211      * @param r the Rect to check to see if it intersects with this Rect
  212      */
  213    inline bool Intersects(const Rect & r) const {return (r&(*this)).IsValid();}
  214 
  215    /** Returns true iff this rectangle's area is non imaginary (i.e. Width() and Height()) are both non-negative) */
  216    inline bool IsValid() const {return ((Width() >= 0.0f)&&(Height() >= 0.0f));}
  217 
  218    /** Returns the area of this rectangle. */
  219    inline float Area() const {return IsValid() ? (Width()*Height()) : 0.0f;}
  220 
  221    /** Returns the width of this rectangle. */
  222    inline float Width() const {return right() - left();}
  223 
  224    /** Returns the width of this rectangle as an integer. */
  225    inline int32 IntegerWidth() const {return (int32)ceil(Width());}
  226 
  227    /** Returns the height of this rectangle. */
  228    inline float Height() const {return bottom()-top();}
  229 
  230    /** Returns the height of this rectangle as an integer. */
  231    inline int32 IntegerHeight() const {return (int32)ceil(Height());}
  232 
  233    /** Returns true iff this rectangle contains the specified point.
  234      * @param p the Point to check to see if it falls within this Rectangle's area
  235      */
  236    inline bool Contains(const Point & p) const {return ((p.x() >= left())&&(p.x() <= right())&&(p.y() >= top())&&(p.y() <= bottom()));}
  237 
  238    /** Returns true iff this rectangle fully encompasses the specified rectangle. 
  239      * @param p the Rect to check to see if it's entirely inside this Rect.
  240      */
  241    inline bool Contains(Rect p) const {return ((Contains(p.LeftTop()))&&(Contains(p.RightTop()))&&(Contains(p.LeftBottom()))&&(Contains(p.RightBottom())));}
  242 
  243    /** Part of the pseudo-Flattenable API:  Returns true. */
  244    bool IsFixedSize() const {return true;}
  245 
  246    /** Part of the pseudo-Flattenable API:  Returns B_RECT_TYPE. */
  247    uint32 TypeCode() const {return B_RECT_TYPE;}
  248 
  249    /** Returns true iff (tc) equals B_RECT_TYPE.
  250      * @param tc the type code to examine
  251      */
  252    bool AllowsTypeCode(uint32 tc) const {return (TypeCode()==tc);}
  253 
  254    /** Part of the PseudoFlattenable API:  Returns 4*sizeof(float). */
  255    uint32 FlattenedSize() const {return 4*sizeof(float);}
  256 
  257    /** @copydoc DoxyTemplate::CalculateChecksum() const */
  258    uint32 CalculateChecksum() const {return CalculateChecksumForFloat(left()) + (3*CalculateChecksumForFloat(top())) + (5*CalculateChecksumForFloat(right())) + (7*CalculateChecksumForFloat(bottom()));}
  259 
  260    /** @copydoc DoxyTemplate::Flatten(uint8 *) const */
  261    void Flatten(uint8 * buffer) const
  262    {
  263       muscleCopyOut(&buffer[0*sizeof(int32)], B_HOST_TO_LENDIAN_IFLOAT(left()));
  264       muscleCopyOut(&buffer[1*sizeof(int32)], B_HOST_TO_LENDIAN_IFLOAT(top()));
  265       muscleCopyOut(&buffer[2*sizeof(int32)], B_HOST_TO_LENDIAN_IFLOAT(right()));
  266       muscleCopyOut(&buffer[3*sizeof(int32)], B_HOST_TO_LENDIAN_IFLOAT(bottom()));
  267    }
  268 
  269    /** @copydoc DoxyTemplate::Unflatten(const uint8 *, uint32) */
  270    status_t Unflatten(const uint8 * buffer, uint32 size)
  271    {
  272       if (size >= FlattenedSize())
  273       {
  274          left()   = B_LENDIAN_TO_HOST_IFLOAT(muscleCopyIn<int32>(&buffer[0*sizeof(int32)]));
  275          top()    = B_LENDIAN_TO_HOST_IFLOAT(muscleCopyIn<int32>(&buffer[1*sizeof(int32)]));
  276          right()  = B_LENDIAN_TO_HOST_IFLOAT(muscleCopyIn<int32>(&buffer[2*sizeof(int32)]));
  277          bottom() = B_LENDIAN_TO_HOST_IFLOAT(muscleCopyIn<int32>(&buffer[3*sizeof(int32)]));
  278          return B_NO_ERROR;
  279       }
  280       else return B_BAD_DATA;
  281    }
  282 
  283    /** This is implemented so that if Rect is used as the key in a Hashtable, the Tuple HashCode() method will be 
  284      * selected by the AutoChooseHashFunctor template logic, instead of the PODHashFunctor.  (Unfortunately 
  285      * AutoChooseHashFunctor doesn't check the superclasses when it looks for a HashCode method)
  286      */
  287    uint32 HashCode() const {return Tuple<4,float>::HashCode();}
  288 };
  289 
  290 DECLARE_ALL_TUPLE_OPERATORS(Rect,float);
  291 
  292 } // end namespace muscle
  293 
  294 #endif