13#include <QtCore/QList>
28template <
typename ValueType>
87 bool IsInside(ValueType point)
const;
160 void Erode(ValueType leftValue, ValueType rightValue);
167 void Dilate(ValueType leftValue, ValueType rightValue);
174 void RemoveGaps(ValueType value,
bool gapState =
false);
199template <
typename ValueType>
206template <
typename ValueType>
208 : m_beginState(false)
211 m_switchPoints.insert(range.GetMinValue());
212 m_switchPoints.insert(range.GetMaxValue());
217template <
typename ValueType>
220 m_switchPoints.clear();
221 m_beginState =
false;
225template <
typename ValueType>
228 return m_switchPoints.empty() && (m_beginState ==
false);
232template <
typename ValueType>
235 return m_switchPoints;
239template <
typename ValueType>
242 return m_switchPoints;
246template <
typename ValueType>
249 std::pair<typename SwitchPoints::iterator, bool> insertionStatus = m_switchPoints.insert(point);
250 if (!insertionStatus.second) {
251 m_switchPoints.erase(insertionStatus.first);
256template <
typename ValueType>
263template <
typename ValueType>
266 m_beginState = state;
270template <
typename ValueType>
273 bool state = m_beginState;
274 for (
typename SwitchPoints::const_iterator iter = m_switchPoints.begin();
275 iter != m_switchPoints.end();
288template <
typename ValueType>
291 bool state = m_beginState;
292 for (
typename SwitchPoints::const_iterator iter = m_switchPoints.begin();
293 iter != m_switchPoints.end();
300 return (iter == m_switchPoints.end()) || (*iter >= range.
GetMaxValue());
314template <
typename ValueType>
317 typename SwitchPoints::const_iterator iter = m_switchPoints.begin();
318 typename SwitchPoints::const_iterator listIter = rangesList.m_switchPoints.begin();
320 bool state = m_beginState;
321 bool listState = rangesList.m_beginState;
324 bool isListCovered = state || !listState;
325 if (!isListCovered) {
329 if (iter != m_switchPoints.end()) {
330 ValueType point = *iter;
332 if (listIter != rangesList.m_switchPoints.end()) {
333 ValueType listPoint = *listIter;
335 if (point <= listPoint) {
340 if (listPoint <= point) {
342 listState = !listState;
351 return listIter == rangesList.m_switchPoints.end();
357template <
typename ValueType>
360 if (clipRangePtr !=
nullptr) {
362 result.m_beginState =
false;
366 bool state = m_beginState;
368 for (
typename SwitchPoints::const_iterator iter = m_switchPoints.begin();
369 iter != m_switchPoints.end();
371 ValueType position = *iter;
376 if (position > prevPosition) {
378 result.m_switchPoints.insert(prevPosition);
379 result.m_switchPoints.insert(position);
382 prevPosition = position;
389 result.m_switchPoints.insert(prevPosition);
390 result.m_switchPoints.insert(clipRangePtr->
GetMaxValue());
394 result.m_switchPoints = m_switchPoints;
395 result.m_beginState = !m_beginState;
400template <
typename ValueType>
403 if (clipRangePtr !=
nullptr) {
406 bool state = m_beginState;
409 for (
typename SwitchPoints::const_iterator iter = m_switchPoints.begin();
410 (iter != m_switchPoints.end()) && (*iter < prevPosition);
411 m_switchPoints.erase(iter++)) {
415 typename SwitchPoints::const_iterator iter = m_switchPoints.begin();
418 if ((iter != m_switchPoints.end()) && (*iter > prevPosition)) {
419 m_switchPoints.insert(prevPosition);
423 for (; iter != m_switchPoints.end(); ++iter) {
424 ValueType position = *iter;
426 m_switchPoints.erase(iter, m_switchPoints.end());
435 m_switchPoints.insert(clipRangePtr->
GetMaxValue());
438 m_beginState =
false;
441 m_beginState = !m_beginState;
446template <
typename ValueType>
451 GetUnion(rangesList, retVal);
457template <
typename ValueType>
460 typename SwitchPoints::const_iterator iter = m_switchPoints.begin();
461 typename SwitchPoints::const_iterator listIter = rangesList.m_switchPoints.begin();
463 bool state = m_beginState;
464 bool listState = rangesList.m_beginState;
466 bool resultState = state || listState;
468 result.m_beginState = resultState;
469 result.m_switchPoints.clear();
472 if (iter != m_switchPoints.end()) {
473 ValueType point = *iter;
475 if (listIter != rangesList.m_switchPoints.end()) {
476 ValueType listPoint = *listIter;
478 if (point <= listPoint) {
483 if (listPoint <= point) {
485 listState = !listState;
488 bool unitedStateAfter = state || listState;
489 if (unitedStateAfter != resultState) {
490 result.m_switchPoints.insert(qMin(point, listPoint));
492 resultState = unitedStateAfter;
497 result.m_switchPoints.insert(iter, m_switchPoints.end());
504 if (!state && (listIter != rangesList.m_switchPoints.end())) {
505 result.m_switchPoints.insert(listIter, rangesList.m_switchPoints.end());
514template <
typename ValueType>
517 typename SwitchPoints::iterator iter = m_switchPoints.begin();
518 typename SwitchPoints::const_iterator listIter = rangesList.m_switchPoints.begin();
520 bool state = m_beginState;
521 bool listState = rangesList.m_beginState;
523 m_beginState = state || listState;
526 if (iter != m_switchPoints.end()) {
527 ValueType point = *iter;
529 if (listIter != rangesList.m_switchPoints.end()) {
530 ValueType listPoint = *listIter;
532 if (point == listPoint) {
536 listState = !listState;
538 else if (point < listPoint) {
540 iter = m_switchPoints.remove(iter);
549 iter = m_switchPoints.insert(iter, listPoint);
552 listState = !listState;
557 m_switchPoints.remove(iter, m_switchPoints.end());
564 if (!state && (listIter != rangesList.m_switchPoints.end())) {
565 m_switchPoints.insert(listIter, rangesList.m_switchPoints.end());
574template <
typename ValueType>
581 bool firstPointState = m_beginState;
582 typename SwitchPoints::const_iterator firstIter = m_switchPoints.begin();
583 for (; firstIter != m_switchPoints.end(); ++firstIter) {
588 firstPointState = !firstPointState;
591 bool secondPointState = firstPointState;
592 typename SwitchPoints::const_iterator secondIter = firstIter;
593 for (; secondIter != m_switchPoints.end(); ++secondIter) {
598 secondPointState = !secondPointState;
601 if (firstIter != secondIter) {
604 m_switchPoints.erase(m_switchPoints.begin(), firstIter);
605 m_switchPoints.erase(m_switchPoints.begin(), secondIter);
608 m_switchPoints.erase(firstIter, secondIter);
613 m_switchPoints.clear();
617 m_beginState = m_beginState || isInverted;
619 if (firstPointState == isInverted) {
623 if (secondPointState == isInverted) {
629template <
typename ValueType>
634 GetIntersection(rangesList, retVal);
640template <
typename ValueType>
643 typename SwitchPoints::const_iterator iter = m_switchPoints.begin();
644 typename SwitchPoints::const_iterator listIter = rangesList.m_switchPoints.begin();
646 bool state = m_beginState;
647 bool listState = rangesList.m_beginState;
649 bool resultState = state && listState;
651 result.m_beginState = resultState;
652 result.m_switchPoints.clear();
655 if (iter != m_switchPoints.end()) {
656 ValueType point = *iter;
658 if (listIter != rangesList.m_switchPoints.end()) {
659 ValueType listPoint = *listIter;
661 if (point <= listPoint) {
666 if (listPoint <= point) {
668 listState = !listState;
671 bool unitedStateAfter = state && listState;
672 if (unitedStateAfter != resultState) {
673 result.m_switchPoints.insert(qMin(point, listPoint));
675 resultState = unitedStateAfter;
680 result.m_switchPoints.insert(iter, m_switchPoints.end());
687 if (state && (listIter != rangesList.m_switchPoints.end())) {
688 result.m_switchPoints.insert(listIter, rangesList.m_switchPoints.end());
697template <
typename ValueType>
700 typename SwitchPoints::iterator iter = m_switchPoints.begin();
701 typename SwitchPoints::const_iterator listIter = rangesList.m_switchPoints.begin();
703 bool state = m_beginState;
704 bool listState = rangesList.m_beginState;
706 m_beginState = state && listState;
709 if (iter != m_switchPoints.end()) {
710 ValueType point = *iter;
712 if (listIter != rangesList.m_switchPoints.end()) {
713 ValueType listPoint = *listIter;
715 if (point == listPoint) {
719 listState = !listState;
721 else if (point < listPoint) {
723 iter = m_switchPoints.remove(iter);
732 iter = m_switchPoints.insert(iter, listPoint);
735 listState = !listState;
740 m_switchPoints.remove(iter, m_switchPoints.end());
747 if (state && (listIter != rangesList.m_switchPoints.end())) {
748 m_switchPoints.insert(listIter, rangesList.m_switchPoints.end());
757template <
typename ValueType>
760 m_beginState = !m_beginState;
762 Union(range, !isInverted);
764 m_beginState = !m_beginState;
768template <
typename ValueType>
775template <
typename ValueType>
778 if (leftValue * rightValue < 0) {
779 Dilate(leftValue, 0);
780 Dilate(0, rightValue);
785 ValueType absoluteSum = qAbs(leftValue + rightValue);
786 if (absoluteSum <= 0) {
790 bool isErosion = (leftValue + rightValue < 0);
791 ValueType leftValueCorr = qAbs(leftValue);
792 ValueType rightValueCorr = qAbs(rightValue);
794 bool state = m_beginState;
796 typename SwitchPoints::iterator iter = m_switchPoints.begin();
797 while (iter != m_switchPoints.end()) {
798 ValueType point = *iter;
802 if (!state == isErosion) {
804 if (iter != m_switchPoints.begin()) {
805 typename SwitchPoints::iterator prevIter = iter;
808 if (*prevIter > point - absoluteSum) {
810 m_switchPoints.erase(prevIter);
811 m_switchPoints.erase(iter++);
818 if (rightValueCorr > 0) {
819 m_switchPoints.erase(iter++);
820 iter = m_switchPoints.insert(iter, point - rightValueCorr);
825 typename SwitchPoints::iterator nextIter = iter;
828 if (nextIter != m_switchPoints.end()) {
829 if (*nextIter < point + absoluteSum) {
831 m_switchPoints.erase(iter++);
832 m_switchPoints.erase(iter++);
838 if (leftValueCorr > 0) {
840 m_switchPoints.erase(iter++);
841 iter = m_switchPoints.insert(iter, point + leftValueCorr);
850template <
typename ValueType>
854 Dilate(value, value);
859 Dilate(value, value);
864template <
typename ValueType>
868 for (
typename SwitchPoints::iterator iter = m_switchPoints.begin();
869 iter != m_switchPoints.end();
871 newSwitchPoints.insert(*iter + offset);
874 m_switchPoints.swap(newSwitchPoints);
878template <
typename ValueType>
883 bool state = m_beginState;
885 for (
typename SwitchPoints::const_iterator iter = m_switchPoints.begin();
886 iter != m_switchPoints.end();
888 ValueType position = *iter;
893 if (position > prevPosition) {
898 prevPosition = position;
910template <
typename ValueType>
913 return (m_beginState == ranges.m_beginState) && (m_switchPoints == ranges.m_switchPoints);
917template <
typename ValueType>
920 return (m_beginState != ranges.m_beginState) || (m_switchPoints != ranges.m_switchPoints);
932template <
typename ValueType>
935 size_t retVal = seed;
942 retVal = retVal ^ size_t(pos);
Implementation of a abstract range defined by two values - minimum and maximum.
ValueType GetMaxValue() const
Get the top value.
ValueType GetMinValue() const
Get the bottom value.
bool IsEmpty() const
Return true if the bottom value is equal to the top value.
void Invert(const TRange< ValueType > *clipRangePtr)
Invert this range in place.
bool IsInside(ValueType point) const
Check if some point belongs to set.
void Dilate(ValueType leftValue, ValueType rightValue)
Calculate dilatation of this range list.
SwitchPoints & GetSwitchPointsRef()
Allows access to stored switch points.
void SetBeginState(bool state)
Set begin state.
void InsertSwitchPoint(ValueType point)
Insert new switch point.
bool IsEmpty() const
Check if this set is empty.
void Erode(ValueType leftValue, ValueType rightValue)
Calculate erosion of this range list.
bool operator==(const TRanges< ValueType > &ranges) const
bool GetBeginState() const
Get begin state.
QList< TRange< ValueType > > RangeList
TRanges< ValueType > GetUnion(const TRanges< ValueType > &rangesList) const
Get union of two range list.
void ShiftRanges(ValueType offset)
ShiftRanges all points in this set using specified offset.
std::set< ValueType > SwitchPoints
bool operator!=(const TRanges< ValueType > &ranges) const
const SwitchPoints & GetSwitchPoints() const
Get stored switch points.
void RemoveGaps(ValueType value, bool gapState=false)
Remove gaps with some length.
TRanges()
Default constructor initializing this set to be empty.
void Union(const TRanges< ValueType > &rangesList)
Calculate union of this range list and the other one.
void Intersection(const TRanges< ValueType > &rangesList)
Calculate intersection of this range list and the other one.
void Reset()
Set this set to be empty.
void GetInverted(TRanges< ValueType > &result, const TRange< ValueType > *clipRangePtr) const
Get inverted range.
void GetAsList(const TRange< ValueType > &range, RangeList &result) const
Get this set as list of istd::TRange objects.
TRanges< ValueType > GetIntersection(const TRanges< ValueType > &rangesList) const
Get intersection of two range list.
TRanges< double > CRanges
size_t qHash(const CVarIndex &index, size_t seed=0)
TRanges< int > CIntRanges