23 #ifndef GNASH_RANGE2D_H
24 #define GNASH_RANGE2D_H
75 friend std::ostream& operator<< (std::ostream& os, const Range2d<U>& rect);
151 assert(_xmin <= _xmax);
152 assert(_ymin <= _ymax);
157 template <
typename U>
162 }
else if ( from.
isNull() ) {
165 _xmin = roundMin(from.
getMinX());
166 _ymin = roundMin(from.
getMinY());
167 _xmax = roundMax(from.
getMaxX());
168 _ymax = roundMax(from.
getMaxY());
175 return _xmax < _xmin;
184 _xmin = std::numeric_limits<T>::max();
185 _xmax = std::numeric_limits<T>::min();
192 return _xmax == std::numeric_limits<T>::max()
193 && _xmin == std::numeric_limits<T>::min();
216 _xmin = std::numeric_limits<T>::min();
217 _xmax = std::numeric_limits<T>::max();
228 template <
typename U>
231 if (
isNull() )
return false;
233 if (x < _xmin || x > _xmax || y < _ymin || y > _ymax)
254 if ( other.
isWorld() )
return false;
256 return _xmin <= other._xmin &&
257 _xmax >= other._xmax &&
258 _ymin <= other._ymin &&
259 _ymax >= other._ymax;
274 if ( _xmin > other._xmax )
return false;
275 if ( _xmax < other._xmin )
return false;
276 if ( _ymin > other._ymax )
return false;
277 if ( _ymax < other._ymin )
return false;
296 _xmin = std::min(_xmin, x);
297 _ymin = std::min(_ymin, y);
298 _xmax = std::max(_xmax, x);
299 _ymax = std::max(_ymax, y);
352 assert(_xmin <= _xmax);
353 assert(_ymin <= _ymax);
417 return scale(factor, 1);
423 return scale(1, factor);
461 assert(xfactor >= 0 && yfactor >= 0);
465 if ( xfactor == 0 || yfactor == 0 )
472 _xmin = scaleMin(_xmin, xfactor);
473 _xmax = scaleMax(_xmax, xfactor);
474 assert(_xmin <= _xmax);
479 _ymin = scaleMin(_ymin, yfactor);
480 _ymax = scaleMax(_ymax, yfactor);
481 assert(_ymin <= _ymax);
490 return scale(factor, factor);
513 if ( amount < 0 )
return shrinkBy(-amount);
515 T newxmin = _xmin - amount;
516 if (newxmin > _xmin )
return setWorld();
517 else _xmin = newxmin;
519 T newxmax = _xmax + amount;
520 if (newxmax < _xmax )
return setWorld();
521 else _xmax = newxmax;
523 T newymin = _ymin - amount;
524 if (newymin > _ymin )
return setWorld();
525 else _ymin = newymin;
527 T newymax = _ymax + amount;
528 if (newymax < _ymax )
return setWorld();
529 else _ymax = newymax;
565 if ( amount < 0 )
return growBy(-amount);
572 if ( _xmax - _xmin <= amount )
return setNull();
573 if ( _ymax - _ymin <= amount )
return setNull();
629 return (_xmax - _xmin) * (_ymax - _ymin);
661 _xmin = std::min(_xmin, r._xmin);
662 _xmax = std::max(_xmax, r._xmax);
663 _ymin = std::min(_ymin, r._ymin);
664 _ymax = std::max(_ymax, r._ymax);
670 T _xmin, _xmax, _ymin, _ymax;
672 T scaleMin(
T min,
float scale)
const {
673 return roundMin(static_cast<float>(min) * scale);
676 T scaleMax(
T max,
float scale)
const {
677 return roundMax(static_cast<float>(max) * scale);
680 T roundMin(
float v)
const {
681 return static_cast<T>(
v);
684 T roundMax(
float v)
const {
685 return static_cast<T>(
v);
691 template <
typename T>
inline std::ostream&
692 operator<< (std::ostream& os, const Range2d<T>& rect)
694 if ( rect.isNull() )
return os <<
"Null range";
695 if ( rect.isWorld() )
return os <<
"World range";
697 return os <<
"Finite range (" << rect._xmin <<
"," << rect._ymin
698 <<
" " << rect._xmax <<
"," << rect._ymax <<
")";
701 template <
typename T>
inline bool
713 return r1._xmin == r2._xmin && r1._ymin == r2._ymin &&
714 r1._xmax == r2._xmax && r1._ymax == r2._ymax;
717 template <
typename T>
inline bool
720 return ! ( r1 == r2 );
724 template <
typename T>
inline bool
731 template <
typename T>
inline Range2d<T>
743 template <
typename T>
inline Range2d<T>
767 std::max(r1._xmin, r2._xmin),
768 std::max(r1._ymin, r2._ymin),
769 std::min(r1._xmax, r2._xmax),
770 std::min(r1._ymax, r2._ymax)
779 template<>
inline int
780 Range2d<int>::roundMin(
float min)
const
782 return static_cast<int>(std::floor(min));
789 template<>
inline unsigned int
790 Range2d<unsigned int>::roundMin(
float min)
const
792 return static_cast<unsigned int>(std::floor(min));
799 template<>
inline int
800 Range2d<int>::roundMax(
float max)
const
802 return static_cast<int>(std::ceil(max));
809 template<>
inline unsigned int
810 Range2d<unsigned int>::roundMax(
float max)
const
812 return static_cast<unsigned int>(std::ceil(max));
819 template<>
inline int
822 assert ( !isWorld() );
823 if ( isNull() )
return 0;
824 return (_xmax - _xmin + 1) * (_ymax - _ymin + 1);
831 template<>
inline unsigned int
835 return (_xmax - _xmin + 1) * (_ymax - _ymin + 1);
842 #endif // GNASH_RANGE2D_H
Range2d< T > & shiftY(T offset)
Shift this Range2dangle vertically.
Definition: Range2d.h:406
bool operator==(const Range2d< T > &r1, const Range2d< T > &r2)
Definition: Range2d.h:702
A NULL range is a range enclosing NO points.
Definition: Range2d.h:42
Range2d< T > & setNull()
Set the Range2d to the NULL value.
Definition: Range2d.h:182
A WORLD range2d is a range including all points on the plane.
Definition: Range2d.h:51
bool isFinite() const
Returns true if this is a finite Range2d.
Definition: Range2d.h:200
bool intersects(const Range2d< T > &other) const
Return true if this rectangle intersects the point with given coordinates (boundaries are inclusive)...
Definition: Range2d.h:269
friend bool operator==(const Range2d< U > &r1, const Range2d< U > &r2)
Equality operator.
T getMinY() const
Get min Y ordinate.
Definition: Range2d.h:608
2d Range template class
Definition: Range2d.h:69
bool contains(const Range2d< T > &other) const
Return true if this rectangle contains the given rectangle.
Definition: Range2d.h:250
Range2d< T > Union(const Range2d< T > &r1, const Range2d< T > &r2)
Return a rectangle being the union of the two rectangles.
Definition: Range2d.h:732
Definition: GnashKey.h:164
Range2d< T > & setTo(T x, T y)
Set ourself to bound the given point.
Definition: Range2d.h:327
Range2d< T > Intersection(const Range2d< T > &r1, const Range2d< T > &r2)
Return a rectangle being the intersetion of the two rectangles.
Definition: Range2d.h:744
Range2d< T > & scale(float xfactor, float yfactor)
Scale this Range2d.
Definition: Range2d.h:459
T getMaxX() const
Get max X ordinate.
Definition: Range2d.h:598
bool Intersect(const Range2d< T > &r1, const Range2d< T > &r2)
Return true of the two ranges intersect (boundaries included)
Definition: Range2d.h:725
Range2d< T > & expandToCircle(T x, T y, T radius)
Expand this Range2d to enclose the given circle.
Definition: Range2d.h:309
bool contains(U x, U y) const
Return true if this rectangle contains the point with given coordinates (boundaries are inclusive)...
Definition: Range2d.h:229
bool isFinite(double d)
Definition: GnashNumeric.h:48
Range2d< T > & scaleY(float factor)
Scale this Range2d vertically.
Definition: Range2d.h:421
Range2d(T xmin, T ymin, T xmax, T ymax)
Construct a finite Range2d with the given values.
Definition: Range2d.h:143
friend Range2d< U > Union(const Range2d< U > &r1, const Range2d< U > &r2)
Return a rectangle being the union of the two rectangles.
T width() const
Return width this Range2d.
Definition: Range2d.h:362
tuple v
Definition: test.py:11
boost::int32_t x
Definition: BitmapData_as.cpp:434
RangeKind
Kinds of a range.
Definition: Range2d.h:37
T getArea() const
Definition: Range2d.h:626
Range2d< T > & expandTo(T x, T y)
Expand this Range2d to enclose the given point.
Definition: Range2d.h:285
Range2d< T > & shiftX(T offset)
Shift this Range2dangle horizontally.
Definition: Range2d.h:389
Definition: GnashKey.h:133
Range2d< T > & scaleX(float factor)
Scale this Range2d horizontally.
Definition: Range2d.h:415
Range2d(const Range2d< U > &from)
Templated copy constructor, for casting between range types.
Definition: Range2d.h:158
Definition: GnashKey.h:132
friend Range2d< U > Intersection(const Range2d< U > &r1, const Range2d< U > &r2)
Return a rectangle being the intersetion of the two rectangles.
Range2d< T > & growBy(T amount)
Grow this range by the given amout in all directions.
Definition: Range2d.h:508
boost::int32_t y
Definition: BitmapData_as.cpp:435
Range2d< T > & setWorld()
Set the Range2d to the WORLD value.
Definition: Range2d.h:214
T getMinX() const
Get min X ordinate.
Definition: Range2d.h:588
Range2d< T > & setTo(T xmin, T ymin, T xmax, T ymax)
Set coordinates to given values.
Definition: Range2d.h:344
Range2d(RangeKind kind=nullRange)
Construct a Range2d of the given kind.
Definition: Range2d.h:114
Range2d< T > & shrinkBy(T amount)
Shirnk this range by the given amout in all directions.
Definition: Range2d.h:559
Valid range, using finite values.
Definition: Range2d.h:39
friend bool operator!=(const Range2d< U > &r1, const Range2d< U > &r2)
Inequality operator.
bool isWorld() const
Returns true if this is the WORLD Range2d.
Definition: Range2d.h:190
bool isNull() const
Returns true if this is the NULL Range2d.
Definition: Range2d.h:173
T height() const
Return height this Range2dangle.
Definition: Range2d.h:373
Range2d< T > & scale(float factor)
Scale this Range2d in both directions with the same factor.
Definition: Range2d.h:488
void expandTo(const Range2d< T > &r)
Expand this range to include the given Range2d.
Definition: Range2d.h:639
T getMaxY() const
Get max Y ordinate.
Definition: Range2d.h:618
bool operator!=(const Range2d< T > &r1, const Range2d< T > &r2)
Definition: Range2d.h:718