20 #ifndef GNASH_SNAPPINGRANGE_H 21 #define GNASH_SNAPPINGRANGE_H 74 template<
typename T>
inline bool snaptest(
75 const geometry::Range2d<T>& range1,
76 const geometry::Range2d<T>& range2,
const float snapFactor);
87 template<
typename U>
friend std::ostream&
operator<<(std::ostream& os,
100 template <
typename U>
115 for (size_type
i = 0,
e = from.
size();
i !=
e; ++
i) {
126 assert(factor > 1.0
f);
127 _snapFactor = factor;
146 _rangesLimit = limit;
156 _snapFactor = from._snapFactor;
157 _singleMode = from._singleMode;
167 _snapFactor(snapFactor)
171 if (snaptest(r, _rt, _snapFactor)) {
178 const RangeType& _rt;
179 const float _snapFactor;
196 GrowBy(
const float factor) : _factor(factor) {}
223 const RangeType& _range;
245 const RangeType& _range;
250 void add(
const RangeType& range) {
256 if (range.
isNull())
return;
259 if (_ranges.empty()) _ranges.resize(1);
260 _ranges[0].expandTo(range);
265 if (
visit(exp))
return;
268 _ranges.push_back(range);
275 const RangeList& rl = other._ranges;
284 std::for_each(_ranges.begin(), _ranges.end(), GrowBy(amount));
293 std::for_each(_ranges.begin(), _ranges.end(), Scale(factor));
306 _ranges[0].setWorld();
311 return ((
size()==1) && (_ranges.front().isWorld()));
316 return _ranges.empty();
322 return _ranges.size();
328 assert(index<
size());
329 return _ranges[index];
339 int rcount = _ranges.size();
341 for (
int rno=0; rno<rcount; rno++)
356 return std::find_if(_ranges.begin(), _ranges.end(), IntersectsRange(r))
364 return std::find_if(_ranges.begin(), _ranges.end(), ContainsPoint(x, y))
376 return std::find_if(_ranges.begin(), _ranges.end(), ContainsRange(r))
395 if (
isNull() )
return false;
396 if ( o.
isNull() )
return false;
406 for (
unsigned rno=0, rcount=o.
size(); rno<rcount; rno++)
438 std::vector<SnappingRanges2d<T> > list;
439 list.reserve(o.
size());
442 for (
unsigned rno=0, rcount=o.
size(); rno<rcount; rno++) {
445 list.push_back(*
this);
448 list.back().intersect(o.
getRange(rno));
454 for (
auto& range : list) {
484 for (
int rno=_ranges.size()-1; rno>=0; rno--) {
489 _ranges.erase(_ranges.begin() + rno);
491 _ranges[rno] = newrange;
500 if (_singleMode)
return;
508 int rcount = _ranges.size();
512 for (
int i=0;
i<rcount;
i++) {
514 for (
int j=
i+1;
j<rcount;
j++) {
516 if (snaptest(_ranges[
i], _ranges[
j], _snapFactor)) {
518 _ranges[
i].expandTo(_ranges[j]);
520 _ranges.erase(_ranges.begin() +
j);
533 if (_ranges.size() > _rangesLimit) {
557 template<
class V>
inline bool visit(
V& visitor)
const 559 typename RangeList::iterator it,
e;
560 for (it = _ranges.begin(), e = _ranges.end(); it !=
e; ++it) {
561 if (!visitor(*it))
break;
563 return it != _ranges.end();
573 template<
class V>
inline void visitAll(
V& visitor)
const 575 for_each(_ranges.begin(), _ranges.end(), visitor);
583 void combineRangesLazy()
const {
584 const size_type max = 5;
589 void finalize()
const {
597 mutable RangeList _ranges;
606 size_type _rangesLimit;
609 mutable size_type _combineCounter;
615 operator<< (std::ostream& os, const SnappingRanges2d<T>&
r)
617 if (
r.isNull() )
return os <<
"NULL";
618 if (
r.isWorld() )
return os <<
"WORLD";
622 const R& ranges =
r._ranges;
624 std::copy(ranges.begin(), ranges.end(),
625 std::ostream_iterator<typename R::value_type>(os,
","));
void for_each(C &container, R(T::*pmf)(const A &), const A &arg)
Definition: Renderer_ogl.cpp:690
size_type size() const
Returns the number of ranges in the list.
Definition: snappingrange.h:320
bool isNull() const
Returns true if this is the NULL Range2d.
Definition: Range2d.h:181
void combineRanges() const
Definition: snappingrange.h:497
detail::Promote< T >::type getArea() const
Get area (width*height)
Definition: Range2d.h:643
Range2d< T > & setNull()
Set the Range2d to the NULL value.
Definition: Range2d.h:190
AddTo(SnappingRanges2d< T > &us)
Definition: snappingrange.h:207
void add(const RangeType &range)
Add a Range to the set, merging when possible and appropriate.
Definition: snappingrange.h:250
void add(const SnappingRanges2d< T > &other)
combines two snapping ranges
Definition: snappingrange.h:274
Definition: snappingrange.h:182
bool operator()(const RangeType &us)
Definition: snappingrange.h:219
float getSnapFactor() const
Definition: snappingrange.h:130
SnappingRanges2d(const SnappingRanges2d< U > &from)
Templated copy constructor, for casting between range types.
Definition: snappingrange.h:101
void operator()(RangeType &r)
Definition: snappingrange.h:186
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
GrowBy(const float factor)
Definition: snappingrange.h:196
Definition: GnashKey.h:152
size_type getRangeCountLimit() const
Definition: snappingrange.h:149
const RangeType & getRange(size_type index) const
Returns the range at the specified index.
Definition: snappingrange.h:326
Definition: GnashKey.h:161
2d Range template class
Definition: Range2d.h:77
void setRangeCountLimit(const size_type limit)
Definition: snappingrange.h:145
Definition: GnashKey.h:156
RangeList::size_type size_type
Definition: snappingrange.h:85
Definition: snappingrange.h:215
Definition: GnashKey.h:164
void setSingleMode(const bool mode)
if mode==true, then the snapping ranges will act like a normal Range2d
Definition: snappingrange.h:135
Range2d< T > Intersection(const Range2d< T > &r1, const Range2d< T > &r2)
Return a rectangle being the intersetion of the two rectangles.
Definition: Range2d.h:762
Scale(const float scale)
Definition: snappingrange.h:185
Range2d< T > & scale(float xfactor, float yfactor)
Scale this Range2d.
Definition: Range2d.h:471
ContainsPoint(const T x, const T y)
Definition: snappingrange.h:229
void intersect(const SnappingRanges2d< T > &o)
Definition: snappingrange.h:425
std::vector< RangeType > RangeList
Definition: snappingrange.h:84
bool intersects(const RangeType &r) const
Returns true if any of the ranges intersect the given range.
Definition: snappingrange.h:353
Definition: snappingrange.h:80
geometry::Range2d< T > RangeType
Definition: snappingrange.h:83
Definition: GnashKey.h:134
std::int32_t x
Definition: BitmapData_as.cpp:434
void setSnapFactor(const float factor)
Definition: snappingrange.h:125
void setWorld()
Resets to one range with world flags.
Definition: snappingrange.h:303
geometry::SnappingRanges2d< std::int32_t > InvalidatedRanges
Standard snapping 2d ranges type for invalidated bounds calculation.
Definition: snappingrange.h:655
Definition: GnashKey.h:130
Definition: snappingrange.h:237
Range2d< T > & expandTo(T x, T y)
Expand this Range2d to enclose the given point.
Definition: Range2d.h:297
bool operator()(const RangeType &us)
Definition: snappingrange.h:241
void visitAll(V &visitor) const
Visit the current Ranges set.
Definition: snappingrange.h:573
bool operator()(RangeType &r)
Definition: snappingrange.h:170
bool isWorld() const
Returns true if this is the WORLD Range2d.
Definition: Range2d.h:200
bool isWorld() const
Returns true, when the ranges equal world range.
Definition: snappingrange.h:310
bool contains(const RangeType &r) const
Returns true if any of the ranges contains the range.
Definition: snappingrange.h:373
void inheritConfig(const SnappingRanges2d< T > &from)
Definition: snappingrange.h:155
bool isNull() const
Returns true, when there is no range.
Definition: snappingrange.h:315
Definition: snappingrange.h:226
void scale(const float factor)
Scale all ranges by the specified factor.
Definition: snappingrange.h:289
Definition: GnashKey.h:132
Range2d< T > & growBy(T amount)
Grow this range by the given amout in all directions.
Definition: Range2d.h:520
Definition: snappingrange.h:204
std::int32_t y
Definition: BitmapData_as.cpp:435
void growBy(const T amount)
Grows all ranges by the specified amount.
Definition: snappingrange.h:280
Definition: GnashKey.h:155
bool contains(T x, T y) const
Returns true if any of the ranges contains the point.
Definition: snappingrange.h:361
Definition: GnashKey.h:151
bool operator()(const RangeType &us)
Definition: snappingrange.h:230
SnappingRanges2d()
Definition: snappingrange.h:90
ContainsRange(const RangeType &range)
Definition: snappingrange.h:240
bool contains(const SnappingRanges2d< T > &o) const
Returns true if all ranges in the given SnappingRanges2d are contained in at least one of the ranges ...
Definition: snappingrange.h:388
void operator()(const RangeType &r)
Definition: snappingrange.h:208
RangeType getFullArea() const
Definition: snappingrange.h:334
IntersectsRange(const RangeType &range)
Definition: snappingrange.h:218
Definition: snappingrange.h:193
bool contains(U x, U y) const
Return true if this rectangle contains the point with given coordinates (boundaries are inclusive)...
Definition: Range2d.h:241
void setNull()
Resets to NULL range.
Definition: snappingrange.h:298
Merge two ranges based on snaptest.
Definition: snappingrange.h:161
friend std::ostream & operator<<(std::ostream &os, const SnappingRanges2d< U > &r)
bool intersects(const Range2d< T > &other) const
Return true if this rectangle intersects the point with given coordinates (boundaries are inclusive)...
Definition: Range2d.h:281
bool getSingleMode() const
Definition: snappingrange.h:139
ExpandToIfSnap(const RangeType &rt, const float snapFactor)
Definition: snappingrange.h:164
void operator()(RangeType &r)
Definition: snappingrange.h:197
bool visit(V &visitor) const
Visit the current Ranges set.
Definition: snappingrange.h:557
void intersect(const RangeType &r)
Definition: snappingrange.h:463