123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- #pragma once
- #include <Mathematics/DisjointIntervals.h>
- #include <functional>
- namespace WwiseGTE
- {
-
-
- template <typename Scalar>
- class DisjointRectangles
- {
- public:
-
- typedef DisjointIntervals<Scalar> ISet;
-
-
- DisjointRectangles()
- :
- mNumRectangles(0)
- {
- }
- DisjointRectangles(Scalar const& xmin, Scalar const& xmax, Scalar const& ymin, Scalar const& ymax)
- {
- if (xmin < xmax && ymin < ymax)
- {
- mNumRectangles = 1;
- mStrips.push_back(Strip(ymin, ymax, ISet(xmin, xmax)));
- }
- else
- {
- mNumRectangles = 0;
- }
- }
- ~DisjointRectangles()
- {
- }
-
- DisjointRectangles(DisjointRectangles const& other)
- {
- *this = other;
- }
- DisjointRectangles& operator=(DisjointRectangles const& other)
- {
- mNumRectangles = other.mNumRectangles;
- mStrips = other.mStrips;
- return *this;
- }
-
- DisjointRectangles(DisjointRectangles&& other)
- {
- *this = std::move(other);
- }
- DisjointRectangles& operator=(DisjointRectangles&& other)
- {
- mNumRectangles = other.mNumRectangles;
- mStrips = std::move(other.mStrips);
- return *this;
- }
-
- class Strip
- {
- public:
-
- Strip()
- :
- ymin((Scalar)0),
- ymax((Scalar)0)
- {
- }
- Strip(Scalar const& inYMin, Scalar const& inYMax, ISet const& inIntervalSet)
- :
- ymin(inYMin),
- ymax(inYMax),
- intervalSet(inIntervalSet)
- {
- }
- ~Strip()
- {
- }
-
- Strip(Strip const& other)
- {
- *this = other;
- }
- Strip& operator=(Strip const& other)
- {
- ymin = other.ymin;
- ymax = other.ymax;
- intervalSet = other.intervalSet;
- return *this;
- }
-
- Strip(Strip&& other)
- {
- *this = std::move(other);
- }
- Strip& operator=(Strip&& other)
- {
- ymin = other.ymin;
- ymax = other.ymax;
- intervalSet = std::move(other.intervalSet);
- other.ymin = (Scalar)0;
- other.ymax = (Scalar)0;
- return *this;
- }
-
- Scalar ymin, ymax;
- ISet intervalSet;
- };
-
- inline int GetNumRectangles() const
- {
- return mNumRectangles;
- }
-
-
- bool GetRectangle(int i, Scalar& xmin, Scalar& xmax, Scalar& ymin, Scalar& ymax) const
- {
- int totalQuantity = 0;
- for (auto const& strip : mStrips)
- {
- ISet const& intervalSet = strip.intervalSet;
- int xQuantity = intervalSet.GetNumIntervals();
- int nextTotalQuantity = totalQuantity + xQuantity;
- if (i < nextTotalQuantity)
- {
- i -= totalQuantity;
- intervalSet.GetInterval(i, xmin, xmax);
- ymin = strip.ymin;
- ymax = strip.ymax;
- return true;
- }
- totalQuantity = nextTotalQuantity;
- }
- return false;
- }
-
- inline void Clear()
- {
- mNumRectangles = 0;
- mStrips.clear();
- }
-
- inline int GetNumStrips() const
- {
- return static_cast<int>(mStrips.size());
- }
-
-
- bool GetStrip(int i, Scalar& ymin, Scalar& ymax, ISet& xIntervalSet) const
- {
- if (0 <= i && i < GetNumStrips())
- {
- Strip const& strip = mStrips[i];
- ymin = strip.ymin;
- ymax = strip.ymax;
- xIntervalSet = strip.intervalSet;
- return true;
- }
- return false;
- }
-
-
-
- bool Insert(Scalar const& xmin, Scalar const& xmax, Scalar const& ymin, Scalar const& ymax)
- {
- if (xmin < xmax && ymin < ymax)
- {
- DisjointRectangles input(xmin, xmax, ymin, ymax);
- DisjointRectangles output = *this | input;
- *this = std::move(output);
- return true;
- }
- return false;
- }
-
-
-
- bool Remove(Scalar const& xmin, Scalar const& xmax, Scalar const& ymin, Scalar const& ymax)
- {
- if (xmin < xmax && ymin < ymax)
- {
- DisjointRectangles input(xmin, xmax, ymin, ymax);
- DisjointRectangles output = *this - input;
- *this = std::move(output);
- return true;
- }
- return false;
- }
-
- friend DisjointRectangles operator|(DisjointRectangles const& input0, DisjointRectangles const& input1)
- {
- return Execute(
- [](ISet const& i0, ISet const& i1) { return i0 | i1; },
- true, true, input0, input1);
- }
-
- friend DisjointRectangles operator&(DisjointRectangles const& input0, DisjointRectangles const& input1)
- {
- return Execute(
- [](ISet const& i0, ISet const& i1) { return i0 & i1; },
- false, false, input0, input1);
- }
-
- friend DisjointRectangles operator-(DisjointRectangles const& input0, DisjointRectangles const& input1)
- {
- return Execute(
- [](ISet const& i0, ISet const& i1) { return i0 - i1; },
- false, true, input0, input1);
- }
-
-
- friend DisjointRectangles operator^(DisjointRectangles const& input0, DisjointRectangles const& input1)
- {
- return Execute(
- [](ISet const& i0, ISet const& i1) { return i0 ^ i1; },
- true, true, input0, input1);
- }
- private:
- static DisjointRectangles Execute(
- std::function<ISet(ISet const&, ISet const&)> const& operation,
- bool unionExclusiveOr, bool unionExclusiveOrDifference,
- DisjointRectangles const& input0, DisjointRectangles const& input1)
- {
- DisjointRectangles output;
- size_t const numStrips0 = input0.GetNumStrips();
- size_t const numStrips1 = input1.GetNumStrips();
- size_t i0 = 0, i1 = 0;
- bool getOriginal0 = true, getOriginal1 = true;
- Scalar ymin0 = (Scalar)0;
- Scalar ymax0 = (Scalar)0;
- Scalar ymin1 = (Scalar)0;
- Scalar ymax1 = (Scalar)0;
- while (i0 < numStrips0 && i1 < numStrips1)
- {
- ISet const& intr0 = input0.mStrips[i0].intervalSet;
- if (getOriginal0)
- {
- ymin0 = input0.mStrips[i0].ymin;
- ymax0 = input0.mStrips[i0].ymax;
- }
- ISet const& intr1 = input1.mStrips[i1].intervalSet;
- if (getOriginal1)
- {
- ymin1 = input1.mStrips[i1].ymin;
- ymax1 = input1.mStrips[i1].ymax;
- }
-
- if (ymax1 <= ymin0)
- {
-
- if (unionExclusiveOr)
- {
- output.mStrips.push_back(Strip(ymin1, ymax1, intr1));
- }
- ++i1;
- getOriginal0 = false;
- getOriginal1 = true;
- continue;
- }
-
- if (ymin1 >= ymax0)
- {
-
- if (unionExclusiveOrDifference)
- {
- output.mStrips.push_back(Strip(ymin0, ymax0, intr0));
- }
- ++i0;
- getOriginal0 = true;
- getOriginal1 = false;
- continue;
- }
-
- if (ymin1 < ymin0)
- {
-
- if (unionExclusiveOr)
- {
- output.mStrips.push_back(Strip(ymin1, ymin0, intr1));
- }
- ymin1 = ymin0;
- getOriginal1 = false;
- }
-
- if (ymin1 > ymin0)
- {
-
- if (unionExclusiveOrDifference)
- {
- output.mStrips.push_back(Strip(ymin0, ymin1, intr0));
- }
- ymin0 = ymin1;
- getOriginal0 = false;
- }
-
- if (ymax1 < ymax0)
- {
-
- auto result = operation(intr0, intr1);
- output.mStrips.push_back(Strip(ymin1, ymax1, result));
- ymin0 = ymax1;
- ++i1;
- getOriginal0 = false;
- getOriginal1 = true;
- continue;
- }
-
- if (ymax1 == ymax0)
- {
-
- auto result = operation(intr0, intr1);
- output.mStrips.push_back(Strip(ymin1, ymax1, result));
- ++i0;
- ++i1;
- getOriginal0 = true;
- getOriginal1 = true;
- continue;
- }
-
- if (ymax1 > ymax0)
- {
-
- auto result = operation(intr0, intr1);
- output.mStrips.push_back(Strip(ymin1, ymax0, result));
- ymin1 = ymax0;
- ++i0;
- getOriginal0 = true;
- getOriginal1 = false;
-
- }
- }
- if (unionExclusiveOrDifference)
- {
- while (i0 < numStrips0)
- {
- if (getOriginal0)
- {
- ymin0 = input0.mStrips[i0].ymin;
- ymax0 = input0.mStrips[i0].ymax;
- }
- else
- {
- getOriginal0 = true;
- }
-
- output.mStrips.push_back(Strip(ymin0, ymax0,
- input0.mStrips[i0].intervalSet));
- ++i0;
- }
- }
- if (unionExclusiveOr)
- {
- while (i1 < numStrips1)
- {
- if (getOriginal1)
- {
- ymin1 = input1.mStrips[i1].ymin;
- ymax1 = input1.mStrips[i1].ymax;
- }
- else
- {
- getOriginal1 = true;
- }
-
- output.mStrips.push_back(Strip(ymin1, ymax1,
- input1.mStrips[i1].intervalSet));
- ++i1;
- }
- }
- output.ComputeRectangleQuantity();
- return output;
- }
- void ComputeRectangleQuantity()
- {
- mNumRectangles = 0;
- for (auto strip : mStrips)
- {
- mNumRectangles += strip.intervalSet.GetNumIntervals();
- }
- }
-
- int mNumRectangles;
-
- std::vector<Strip> mStrips;
- };
- }
|