Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Math: support inverted ranges #286

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/Magnum/Math/Range.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ namespace Implementation {
Axis-aligned line (in 1D), rectangle (in 2D) or cube (in 3D). Minimal
coordinate is inclusive, maximal exclusive. See @ref Range1D, @ref Range2D and
@ref Range3D specializations for given dimension count.

@section Math-Range-inverted Inverted ranges

It is valid to create ranges where a maximal coordinate is less than the
matching minimal coordinate in some dimension. The range is then interpreted as
inverted in given dimension, affecting result of @ref contains(), @ref intersect(),
@ref intersects() and @ref join().
*/
template<UnsignedInt dimensions, class T> class Range {
template<UnsignedInt, class> friend class Range;
Expand Down Expand Up @@ -234,7 +241,6 @@ template<UnsignedInt dimensions, class T> class Range {
* @f]
*
* The range minimum is interpreted as inclusive, maximum as exclusive.
* Results are undefined if the range has negative size.
* @see @ref intersects(), @ref contains(const Range<dimensions, T>&) const,
* @ref min(), @ref max()
*/
Expand All @@ -252,7 +258,6 @@ template<UnsignedInt dimensions, class T> class Range {
* (\operatorname{max}(B)_i \le \operatorname{max}(A)_i)
* @f]
*
* Results are undefined if the range has negative size.
* @see @ref intersects(), @ref contains(const VectorType&) const,
* @ref min(), @ref max()
*/
Expand Down Expand Up @@ -646,8 +651,7 @@ template<UnsignedInt dimensions, class T> inline Range<dimensions, T> join(const

Returns a range that covers the intersection of both ranges. If the
intersection is empty, a default-constructed range is returned. The range
minimum is interpreted as inclusive, maximum as exclusive. Results are
undefined if any range has a negative size.
minimum is interpreted as inclusive, maximum as exclusive.
@see @ref intersects()
*/
template<UnsignedInt dimensions, class T> inline Range<dimensions, T> intersect(const Range<dimensions, T>& a, const Range<dimensions, T>& b) {
Expand All @@ -664,13 +668,13 @@ Returns @cpp true @ce if the following holds for all dimensions @f$ i @f$,
(\operatorname{max}(A)_i > \operatorname{min}(B)_i) \land
(\operatorname{min}(A)_i < \operatorname{max}(B)_i)
@f]
The range minimum is interpreted as inclusive, maximum as exclusive. Results
are undefined if any range has a negative size.
The range minimum is interpreted as inclusive, maximum as exclusive.
@see @ref Range::contains(), @ref intersect(), @ref join(), @ref Range::min(),
@ref Range::max()
*/
template<UnsignedInt dimensions, class T> inline bool intersects(const Range<dimensions, T>& a, const Range<dimensions, T>& b) {
return (a.max() > b.min()).all() && (a.min() < b.max()).all();
return ((a.min() > b.min()) & (a.min() < b.max())).all() ||
((b.min() > a.min()) & (b.min() < a.max())).all();
}

/** @debugoperator{Range} */
Expand Down
12 changes: 12 additions & 0 deletions src/Magnum/Math/Test/RangeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ struct RangeTest: Corrade::TestSuite::Tester {

void containsVector();
void containsRange();
void containsRangeInverted();
void intersectIntersects();
void join();

Expand Down Expand Up @@ -161,6 +162,7 @@ RangeTest::RangeTest() {

&RangeTest::containsVector,
&RangeTest::containsRange,
&RangeTest::containsRangeInverted,
&RangeTest::intersectIntersects,
&RangeTest::join,

Expand Down Expand Up @@ -581,6 +583,16 @@ void RangeTest::containsRange() {
CORRADE_VERIFY(!a.contains(j));
}

void RangeTest::containsRangeInverted() {
/* Inverse range contains things that are only outside */
Range2Di b({40, 50}, {10, 30});
CORRADE_VERIFY( b.contains({{45, 55}, { 5, 25}}));
CORRADE_VERIFY(!b.contains({{35, 45}, {15, 35}})); /* B contains A */
CORRADE_VERIFY(!b.contains({{15, 35}, {35, 45}})); /* Fully inside */
CORRADE_VERIFY(!b.contains({{45, 55}, {15, 25}})); /* "Leaks" on max X */
CORRADE_VERIFY(!b.contains({{45, 45}, { 5, 25}})); /* "Leaks" on min Y */
}

void RangeTest::intersectIntersects() {
Range2Di a({34, 23}, {47, 30});

Expand Down