segment.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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
  14. {
  15. return range{position, position + size};
  16. }
  17. // NOTE: hmmmm, what about the dimensions thouuuuugh...
  18. friend bool operator==(const segment& one, const segment& other)
  19. { return one.size == other.size && one.position == other.position; }
  20. friend bool operator!=(const segment& one, const segment& other)
  21. { return !(one == other); }
  22. };
  23. template <typename T> segment(T, T) -> segment<T>;
  24. template <typename Type, typename AnchorType = Type>
  25. struct anchored_segment : public segment<Type>
  26. {
  27. using base = segment<Type>;
  28. AnchorType anchor;
  29. constexpr anchored_segment() = default;
  30. [[nodiscard]]
  31. constexpr operator typename base::range() const
  32. {
  33. auto lower = this->position -
  34. static_cast<Type>(anchor * static_cast<AnchorType>(this->size));
  35. return typename base::range{lower, lower + this->size};
  36. }
  37. friend bool operator==(const anchored_segment& one, const anchored_segment& other)
  38. { return segment<Type>::operator==(one, other) && one.anchor == other.anchor; }
  39. friend bool operator!=(const anchored_segment& one, const anchored_segment& other)
  40. { return !(one == other); }
  41. };
  42. template <typename Coordinate, size_t Dimensions = 2>
  43. using vector_segment = segment<vector<Coordinate, Dimensions>>;
  44. template <typename Coordinate, size_t Dimensions = 2, typename Anchor = Coordinate>
  45. using anchored_vector_segment = anchored_segment<vector<Coordinate, Dimensions>, vector<Anchor, Dimensions>>;
  46. } // namespace simple::geom
  47. #endif /* end of include guard */