7#include "geometry_common.h"
12 template <std::
floating_po
int T>
15 using tvec = glm::tvec2<T>;
23 auto length()
const noexcept {
return glm::distance(start, end); }
26 static tsegment from_offset(tvec
const& start, tvec
const&
offset)
noexcept {
return { start, start +
offset }; }
27 static tsegment from_dir(tvec
const& start, tvec
const& dir, T len)
noexcept {
return { start, start + dir * len }; }
31 tsegment& set_position(tvec
const&
pos)
noexcept {
const auto d = vec(); start =
pos; end =
pos +
d;
return *
this; }
32 tsegment& operator+=(tvec
const&
offs)
noexcept { start +=
offs; end +=
offs;
return *
this; }
33 tsegment& operator-=(tvec
const&
offs)
noexcept { start -=
offs; end -=
offs;
return *
this; }
34 tsegment& translate(tvec
const&
offs)
noexcept {
return this->operator+=(
offs); }
36 tsegment& set_length(T len)
noexcept { end = start + dir() * len;
return *
this; }
37 tsegment& set_length_around_center(T len)
noexcept
41 const auto hlen = len / T(2);
47 tsegment& grow(T len)
noexcept {
const auto d = dir(); start -=
d * len; end +=
d * len;
return *
this; }
48 tsegment& shrink(T len)
noexcept {
const auto d = dir(); start +=
d * len; end -=
d * len;
return *
this; }
50 constexpr std::optional<tvec> intersection(tsegment
const&
other)
const noexcept
54 const auto s =
other.vec();
55 const auto rxs = glm::cross(
r,
s);
56 const auto qp =
other.start - start;
57 const auto qpxr = glm::cross(
qp,
r);
58 if (glm::abs(
rxs) < std::numeric_limits<T>::epsilon() && glm::abs(
qpxr) < std::numeric_limits<T>::epsilon())
61 const auto t0 = glm::dot(
qp,
r) / glm::dot(
r,
r);
62 const auto t1 =
t0 + glm::dot(
s,
r) / glm::dot(
r,
r);
63 if ((
t0 >= 0 &&
t0 <= 1) || (
t1 >= 0 &&
t1 <= 1))
67 if (glm::abs(
rxs) < std::numeric_limits<T>::epsilon() && glm::abs(
qpxr) > std::numeric_limits<T>::epsilon())
69 const auto t = glm::cross(
qp,
s) /
rxs;
70 const auto u = glm::cross(
qp,
r) /
rxs;
78 T edge_length()
const {
return length(); }
79 tvec edge_point_alpha(T
t)
const {
return glm::mix(start, end,
t); }
80 tvec edge_point(T
t)
const {
return glm::mix(start, end,
t / edge_length()); }
81 trec2<T> bounding_box()
const {
return trec2<T>::from_points({ &start, &start + 2 }); }
82 tvec projected(tvec
pt)
const
84 const auto dir = this->vec();
85 const auto d1 = glm::dot(
pt - start, dir);
88 const auto d2 = glm::dot(dir, dir);
91 return start + dir * (
d1 /
d2);
constexpr auto bit_count
Equal to the number of bits in the type.