header_utils
Loading...
Searching...
No Matches
ray.h
1
4
5#pragma once
6
7#include "geometry_common.h"
8
10{
11
14 template <std::floating_point T>
15 struct tray
16 {
18
19 using tvec = glm::tvec2<T>;
20 using value_type = T;
21 using segment = tsegment<T>;
22
23 tvec start{};
24 tvec dir{};
25
26 basic_line_t<T> line() const noexcept { return line_from_dir(dir); }
27
28 tray from_dir(tvec const& start, tvec const& dir) noexcept { return { start, glm::normalize(dir) }; }
29 tray from_points(tvec const& start, tvec const& second) noexcept { return from_dir(start, second - start); }
30
31 segment& set_position(tvec const& pos) noexcept { start = pos; return *this; }
32 segment& operator+=(tvec const& offs) noexcept { start += offs; return *this; }
33 segment& operator-=(tvec const& offs) noexcept { start -= offs; return *this; }
34 segment& translate(tvec const& offs) noexcept { return this->operator+=(offs); }
35
36 T edge_length() const noexcept { return std::numeric_limits<T>::infinity(); }
37
38 tvec edge_point(T t) const { return start + dir * t; }
39 trec2<T> bounding_box() const { return trec2<T>::from_points(edge_point(T{}), edge_point(edge_length())); }
40 tvec closest_point_to(tvec const& pt) const
41 {
42 const auto a = projected_alpha(pt);
43 if (a < T(0)) return start;
44 return start + dir * a;
45 }
46 T projected_alpha(tvec const& pt) const
47 {
48 const auto d = pt - start;
49 return glm::dot(d, dir);
50 }
51 };
52
53 using ray = tray<float>;
54
55}
TODO: This all needs to be tested.
Represents a 2D ray.
Definition ray.h:16
Represents a 2D rectangle polygon_shape.
Definition rec2.h:31
Represents a 2D segment (technically a polygon_shape).
Definition segment.h:15