Skip to content

Commit

Permalink
add 3d and merge_segment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luk036 committed Aug 2, 2021
1 parent b7b019a commit df108d5
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 141 deletions.
31 changes: 25 additions & 6 deletions include/recti/interval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ namespace recti {
return x -= alpha;
}

/**
* @brief Enlarge with
*
* @param[in] alpha
* @return interval<T>&
*/
constexpr auto enlarge_with(const T& alpha) -> interval<T>& {
this->_lower -= alpha;
this->_upper += alpha;
return *this;
}

///@}

/**
Expand Down Expand Up @@ -338,15 +350,17 @@ namespace recti {
* @param[in] other
* @return constexpr auto
*/
constexpr auto intersection(const T& other) const -> interval { return {other, other}; }
constexpr auto intersection_with(const T& other) const -> interval {
return {other, other};
}

/**
* @brief intersection with
*
* @param[in] other
* @return constexpr auto
*/
constexpr auto intersection(const interval& other) const -> interval {
constexpr auto intersection_with(const interval& other) const -> interval {
return {std::max(this->_lower, other._lower), std::min(this->_upper, other._upper)};
}

Expand Down Expand Up @@ -419,10 +433,6 @@ namespace recti {
*this = other = this->intersection(other);
return 0;
}

// ???
template <typename U1, typename U2> //
friend inline constexpr auto min_dist_change(U1& lhs, U2& rhs);
};
#pragma pack(pop)

Expand Down Expand Up @@ -480,6 +490,15 @@ namespace recti {
}
}

template <typename U1, typename U2> //
inline constexpr auto enlarge(U1 lhs, const U2& rhs) {
if constexpr (std::is_scalar_v<U1>) {
return interval<U1>{lhs - rhs, lhs + rhs};
} else {
return lhs.enlarge_with(rhs);
}
}

template <typename U1, typename U2> //
inline constexpr auto min_dist_change(U1& lhs, U2& rhs) {
if constexpr (std::is_scalar_v<U1>) {
Expand Down
42 changes: 38 additions & 4 deletions include/recti/merge_obj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm> // import std::min, std::max
#include <cassert>
#include <utility> // import std::move

#include "point.hpp"

namespace recti {
Expand All @@ -23,7 +24,15 @@ namespace recti {
* @param[in] x
* @param[in] y
*/
constexpr merge_obj(const T1& x, const T2& y) : point<T1, T2>{x + y, x - y} {}
constexpr merge_obj(T1&& x, T2&& y) noexcept : point<T1, T2>{std::move(x), std::move(y)} {}

// /**
// * @brief Construct a new point object
// *
// * @param[in] x
// * @param[in] y
// */
// constexpr merge_obj(const T1& x, const T2& y) : point<T1, T2>{x + y, x - y} {}

/**
* @brief Add a vector (translation)
Expand Down Expand Up @@ -93,11 +102,36 @@ namespace recti {
return std::max(min_dist(this->_x, other._x), min_dist(this->_y, other._y));
}

template <typename R> //
friend constexpr auto enlarge(const merge_obj& lhs, const R& alpha) {
auto x = enlarge(lhs.x(), alpha);
auto y = enlarge(lhs.y(), alpha);
return merge_obj<decltype(x), decltype(y)>{std::move(x), std::move(y)};
}

/**
* @brief overlap
*
* @tparam U1
* @tparam U2
* @param other
* @return true
* @return false
*/
template <typename U1, typename U2> //
[[nodiscard]] constexpr auto merge(const merge_obj<U1, U2>& other) const {
auto alpha = this->min_dist_with(other);
auto half = alpha / 2;
auto trr1 = enlarge(*this, half);
auto trr2 = ehlarge(other, alpha - half);
return trr1.intersection_with(trr2);
}

// /**
// * @brief minimum distance with
// *
// * @param[in] other
// * @return constexpr auto
// *
// * @param[in] other
// * @return constexpr auto
// */
// [[nodiscard]] constexpr auto min_dist_change_with(merge_obj& other) {
// auto minDist = this->min_dist_with(other);
Expand Down
97 changes: 64 additions & 33 deletions include/recti/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
#include <type_traits> // import std::is_scalar_v
#include <utility> // import std::move

#include "vector2.hpp"
#include "interval.hpp"
#include "vector2.hpp"

namespace recti {

/**
* @brief Forward declaration
*
* @tparam U1
* @tparam U2
*/
template <typename U1, typename U2> class point;

#pragma pack(push, 1)
/**
* @brief 2D point
Expand All @@ -19,6 +27,14 @@ namespace recti {
template <typename T1, typename T2 = T1> class point {
using Self = point<T1, T2>;

/**
* @brief
*
* @tparam U1
* @tparam U2
*/
template <typename U1, typename U2> friend class point;

protected:
T1 _x; //!< x coordinate
T2 _y; //!< y coordinate
Expand Down Expand Up @@ -59,7 +75,7 @@ namespace recti {
*
* @return auto
*/
constexpr auto _tie() const { return std::tie(_x, _y); }
constexpr auto _tie() const { return std::tie(this->_x, this->_y); }

/** @name Comparison operators
* definie ==, !=, <, >, <=, >=.
Expand Down Expand Up @@ -141,6 +157,8 @@ namespace recti {
/**
* @brief Greater than or equal to
*
* @tparam U1
* @tparam U2
* @param[in] x
* @param[in] y
* @return true
Expand All @@ -161,11 +179,13 @@ namespace recti {
/**
* @brief Add a vector (translation)
*
* @tparam U
* @tparam U1
* @tparam U2
* @param[in] rhs
* @return Self&
*/
template <typename U> constexpr auto operator+=(const vector2<U>& rhs) -> Self& {
template <typename U1, typename U2> constexpr auto operator+=(const vector2<U1, U2>& rhs)
-> Self& {
this->_x += rhs.x();
this->_y += rhs.y();
return *this;
Expand All @@ -174,11 +194,13 @@ namespace recti {
/**
* @brief Substract a vector (translation)
*
* @tparam U
* @tparam U1
* @tparam U2
* @param[in] rhs
* @return Self&
*/
template <typename U> constexpr auto operator-=(const vector2<U>& rhs) -> Self& {
template <typename U1, typename U2> constexpr auto operator-=(const vector2<U1, U2>& rhs)
-> Self& {
this->_x -= rhs.x();
this->_y -= rhs.y();
return *this;
Expand All @@ -187,37 +209,41 @@ namespace recti {
/**
* @brief Add
*
* @tparam U
* @tparam U1
* @tparam U2
* @param[in] x
* @param[in] y
* @return vector2<T>
*/
template <typename U> //
friend constexpr auto operator+(point x, const vector2<U>& y) -> point {
template <typename U1, typename U2> //
friend constexpr auto operator+(point x, const vector2<U1, U2>& y) -> point {
return x += y;
}

/**
* @brief Substract
*
* @tparam U
* @tparam U1
* @tparam U2
* @param[in] x
* @param[in] y
* @return vector2<T>
*/
template <typename U> //
friend constexpr auto operator-(point x, const vector2<U>& y) -> point {
template <typename U1, typename U2> //
friend constexpr auto operator-(point x, const vector2<U1, U2>& y) -> point {
return x -= y;
}

/**
* @brief Different
*
* @param[in] rhs
* @return vector2
* @return constexpr auto
*/
constexpr auto operator-(const Self& rhs) const -> vector2<T1> {
return {this->x() - rhs.x(), this->y() - rhs.y()};
constexpr auto operator-(const Self& rhs) const {
auto x = this->x() - rhs.x();
auto y = this->y() - rhs.y();
return vector2<decltype(x), decltype(y)>{std::move(x), std::move(y)};
}

/**
Expand Down Expand Up @@ -280,7 +306,6 @@ namespace recti {
return contain(this->x(), other.x()) && contain(this->y(), other.y());
}


/**
* @brief overlap
*
Expand All @@ -292,7 +317,7 @@ namespace recti {
*/
template <typename U1, typename U2> //
[[nodiscard]] constexpr auto min_dist_with(const point<U1, U2>& other) const {
return min_dist(this->_x, other._x) + min_dist(this->_y, other._y);
return min_dist(this->x(), other.x()) + min_dist(this->y(), other.y());
}

/**
Expand All @@ -308,25 +333,31 @@ namespace recti {
[[nodiscard]] constexpr auto min_dist_change_with(point<U1, U2>& other) {
return min_dist_change(this->_x, other._x) + min_dist_change(this->_y, other._y);
}

template <typename R> //
friend constexpr auto enlarge(const point& lhs, const R& alpha) {
auto x = enlarge(lhs.x(), alpha);
auto y = enlarge(lhs.y(), alpha);
return point<decltype(x), decltype(y)>{std::move(x), std::move(y)};
}

/**
* @brief
*
* @tparam T1
* @tparam T2
* @tparam Stream
* @param[out] out
* @param[in] p
* @return Stream&
*/
template <class Stream> friend auto operator<<(Stream& out, const point& p) -> Stream& {
out << "(" << p.x() << ", " << p.y() << ")";
return out;
}
};
#pragma pack(pop)

/**
* @brief
*
* @tparam T1
* @tparam T2
* @tparam Stream
* @param[out] out
* @param[in] p
* @return Stream&
*/
template <typename T1, typename T2, class Stream>
auto operator<<(Stream& out, const point<T1, T2>& p) -> Stream& {
out << "(" << p.x() << ", " << p.y() << ")";
return out;
}

#pragma pack(push, 1)
/**
* @brief 2D point
Expand Down
Loading

0 comments on commit df108d5

Please sign in to comment.