Skip to content

Commit

Permalink
minor change
Browse files Browse the repository at this point in the history
  • Loading branch information
luk036 committed May 14, 2024
1 parent 2d49b24 commit a51080b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 22 deletions.
8 changes: 4 additions & 4 deletions include/recti/generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ namespace recti {
*/
template <typename U1, typename U2> //
constexpr auto intersection(const U1 &lhs, const U2 &rhs) {
if constexpr (requires { lhs.intersection_with(rhs); }) {
return lhs.intersection_with(rhs);
} else if constexpr (requires { rhs.intersection_with(lhs); }) {
return rhs.intersection_with(lhs);
if constexpr (requires { lhs.intersect_with(rhs); }) {
return lhs.intersect_with(rhs);
} else if constexpr (requires { rhs.intersect_with(lhs); }) {
return rhs.intersect_with(lhs);
} else /* constexpr */ {
assert(lhs == rhs);
return lhs;
Expand Down
11 changes: 7 additions & 4 deletions include/recti/interval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ namespace recti {
*/
constexpr auto len() const -> T { return this->ub() - this->lb(); }

constexpr auto is_invalid() const -> bool { return this->lb() > this->ub(); }

/** @name Comparison operators
* definie ==, !=, <, >, <=, >=.
*/
Expand Down Expand Up @@ -346,7 +348,7 @@ namespace recti {
/**
* @brief intersection with
*
* The above code is defining a template function called `intersection_with` that takes a
* The above code is defining a template function called `intersect_with` that takes a
* parameter `other`. The function returns the intersection of the current object (an
* `Interval`) with `other`.
*
Expand All @@ -355,12 +357,13 @@ namespace recti {
* @return constexpr auto
*/
template <typename U> //
constexpr auto intersection_with(const U &other) const {
constexpr auto intersect_with(const U &other) const {
if constexpr (requires { other.lb(); }) {
return Interval<T>{this->lb() > other.lb() ? this->lb() : T(other.lb()),
this->ub() < other.ub() ? this->ub() : T(other.ub())};
} else /* constexpr */ { // assume scalar
return Interval<U>{other, other};
return Interval<T>{this->lb() > other ? this->lb() : T(other),
this->ub() < other ? this->ub() : T(other)};
}
}

Expand Down Expand Up @@ -405,7 +408,7 @@ namespace recti {
}

if constexpr (requires { other.lb(); }) {
*this = other = this->intersection_with(other);
*this = other = this->intersect_with(other);
} else /* constexpr */ { // assume scalar
this->_ub = this->_lb = other;
}
Expand Down
6 changes: 3 additions & 3 deletions include/recti/merge_obj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ namespace recti {
* @return false
*/
template <typename U1, typename U2> //
constexpr auto intersection_with(const MergeObj<U1, U2> &other) const {
constexpr auto intersect_with(const MergeObj<U1, U2> &other) const {
auto xcoord = intersection(this->xcoord(), other.xcoord());
auto ycoord = intersection(this->ycoord(), other.ycoord());
return MergeObj<decltype(xcoord), decltype(ycoord)>{std::move(xcoord),
Expand Down Expand Up @@ -249,8 +249,8 @@ namespace recti {
// auto minDist = this->min_dist_with(other);
// auto mobj1 = this->enlarge_with(minDist);
// auto mobj2 = other.enlarge_with(minDist);
// other = mobj1.intersection_with(other);
// *this = mobj2.intersection_with(*this);
// other = mobj1.intersect_with(other);
// *this = mobj2.intersect_with(*this);
// return minDist;
// }
};
Expand Down
2 changes: 1 addition & 1 deletion include/recti/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ namespace recti {
* @return The intersection point if the points intersect.
*/
template <typename U1, typename U2> //
constexpr auto intersection_with(const Point<U1, U2> &other) const {
constexpr auto intersect_with(const Point<U1, U2> &other) const {
auto xcoord = intersection(this->xcoord(), other.xcoord());
auto ycoord = intersection(this->ycoord(), other.ycoord());
return Point<decltype(xcoord), decltype(ycoord)>{std::move(xcoord), std::move(ycoord)};
Expand Down
9 changes: 5 additions & 4 deletions test/source/test_interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ TEST_CASE("Interval test") {

CHECK(a.contains(4));
CHECK(a.contains(8));
CHECK(a.intersection_with(8) == Interval{8, 8});
CHECK(a.intersect_with(8) == Interval{8, 8});
CHECK(a.intersect_with(10).is_invalid());
CHECK(a.contains(b));
CHECK(a.intersection_with(b) == b);
CHECK(a.intersect_with(b) == b);
CHECK(!b.contains(a));
CHECK(a.overlaps(b));
CHECK(b.overlaps(a));
Expand Down Expand Up @@ -61,10 +62,10 @@ TEST_CASE("Interval of Interval test") {
CHECK(a.contains(Interval{4, 5}));
CHECK(a.contains(Interval{7, 8}));

CHECK(a.intersection_with(Interval{7, 8}) == Interval{Interval{7, 7}, Interval{8, 8}}); // ???
CHECK(a.intersect_with(Interval{7, 8}) == Interval{Interval{7, 7}, Interval{8, 8}}); // ???

CHECK(a.contains(b));
CHECK(a.intersection_with(b) == b);
CHECK(a.intersect_with(b) == b);
CHECK(!b.contains(a));
CHECK(a.overlaps(b));
CHECK(b.overlaps(a));
Expand Down
2 changes: 1 addition & 1 deletion test/source/test_recti3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ TEST_CASE("Interval test") {
CHECK(!(b == a));
CHECK(b != a);
CHECK(a.contains(b));
CHECK(a.intersection_with(b) == b);
CHECK(a.intersect_with(b) == b);
CHECK(!b.contains(a));
CHECK(a.overlaps(b));
CHECK(b.overlaps(a));
Expand Down
8 changes: 4 additions & 4 deletions test_interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ TEST_CASE("Interval test") {

CHECK(a.contains(4));
CHECK(a.contains(8));
CHECK(a.intersection_with(8) == Interval{8, 8});
CHECK(a.intersect_with(8) == Interval{8, 8});
CHECK(a.contains(b));
CHECK(a.intersection_with(b) == b);
CHECK(a.intersect_with(b) == b);
CHECK(!b.contains(a));
CHECK(a.overlaps(b));
CHECK(b.overlaps(a));
Expand Down Expand Up @@ -61,10 +61,10 @@ TEST_CASE("Interval of Interval test") {
CHECK(a.contains(Interval{4, 5}));
CHECK(a.contains(Interval{7, 8}));

CHECK(a.intersection_with(Interval{7, 8}) == Interval{Interval{7, 7}, Interval{8, 8}}); // ???
CHECK(a.intersect_with(Interval{7, 8}) == Interval{Interval{7, 7}, Interval{8, 8}}); // ???

CHECK(a.contains(b));
CHECK(a.intersection_with(b) == b);
CHECK(a.intersect_with(b) == b);
CHECK(!b.contains(a));
CHECK(a.overlaps(b));
CHECK(b.overlaps(a));
Expand Down
2 changes: 1 addition & 1 deletion test_recti3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST_CASE("Interval test") {
CHECK((a - v) + v == a);

CHECK(a.contains(b));
CHECK(a.intersection_with(b) == b);
CHECK(a.intersect_with(b) == b);
CHECK(!b.contains(a));
CHECK(a.overlaps(b));
CHECK(b.overlaps(a));
Expand Down

0 comments on commit a51080b

Please sign in to comment.