segment.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef SIMPLE_GEOM_BOX_HPP
  2. #define SIMPLE_GEOM_BOX_HPP
  3. #include "vector.hpp"
  4. #include "simple/support/range.hpp"
  5. namespace simple::geom
  6. {
  7. template <typename Type>
  8. struct segment
  9. {
  10. Type size, position;
  11. using range = support::range<Type>;
  12. [[nodiscard]]
  13. constexpr operator range() const { return to_range(); }
  14. [[nodiscard]]
  15. constexpr auto to_range() const
  16. {
  17. return range{position, position + size};
  18. }
  19. [[nodiscard]]
  20. constexpr bool operator==(const segment& one) const = default;
  21. };
  22. template <typename T> segment(T, T) -> segment<T>;
  23. template <typename Type, typename AnchorType = Type>
  24. struct anchored_segment : public segment<Type>
  25. {
  26. using base = segment<Type>;
  27. AnchorType anchor;
  28. [[nodiscard]]
  29. // NOTE: maybe return range<common type <Type, AnchorType>> if range<Type> doesn't work??
  30. // gotta have an actual use case to make it worth it though
  31. constexpr operator typename base::range() const { return to_range(); }
  32. [[nodiscard]]
  33. constexpr auto to_range() const
  34. {
  35. auto lower = this->position - anchor * this->size;
  36. return support::range{lower, lower + this->size};
  37. }
  38. [[nodiscard]]
  39. constexpr bool operator==(const anchored_segment& one) const = default;
  40. [[nodiscard]]
  41. constexpr bool operator==(const base& one) const = delete;
  42. };
  43. template <typename Coordinate, size_t Dimensions = 2>
  44. using vector_segment = segment<vector<Coordinate, Dimensions>>;
  45. template <typename Coordinate, size_t Dimensions = 2, typename Anchor = Coordinate>
  46. using anchored_vector_segment = anchored_segment<vector<Coordinate, Dimensions>, vector<Anchor, Dimensions>>;
  47. } // namespace simple::geom
  48. #endif /* end of include guard */