HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
point.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../numeric_array.hpp"
8#include "vector.hpp"
9
10namespace tt {
11namespace geo {
12
20template<int D>
21class point {
22public:
23 static_assert(D == 2 || D == 3, "Only 2D or 3D points are supported");
24
25 constexpr point(point const &) noexcept = default;
26 constexpr point(point &&) noexcept = default;
27 constexpr point &operator=(point const &) noexcept = default;
28 constexpr point &operator=(point &&) noexcept = default;
29
32 template<int E>
33 requires(E < D) [[nodiscard]] constexpr point(point<E> const &other) noexcept : _v(static_cast<f32x4>(other))
34 {
35 tt_axiom(is_valid());
36 }
37
40 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
41 {
42 tt_axiom(is_valid());
43 return _v;
44 }
45
48 [[nodiscard]] constexpr explicit point(f32x4 const &other) noexcept : _v(other)
49 {
50 tt_axiom(is_valid());
51 }
52
55 [[nodiscard]] constexpr point() noexcept : _v(0.0, 0.0, 0.0, 1.0) {}
56
61 [[nodiscard]] constexpr point(float x, float y) noexcept requires(D == 2) : _v(x, y, 0.0, 1.0) {}
62
68 [[nodiscard]] constexpr point(float x, float y, float z = 0.0) noexcept requires(D == 3) : _v(x, y, z, 1.0) {}
69
73 [[nodiscard]] constexpr float &x() noexcept
74 {
75 return _v.x();
76 }
77
81 [[nodiscard]] constexpr float &y() noexcept
82 {
83 return _v.y();
84 }
85
89 [[nodiscard]] constexpr float &z() noexcept requires(D == 3)
90 {
91 return _v.z();
92 }
93
97 [[nodiscard]] constexpr float const &x() const noexcept
98 {
99 return _v.x();
100 }
101
105 [[nodiscard]] constexpr float const &y() const noexcept
106 {
107 return _v.y();
108 }
109
113 [[nodiscard]] constexpr float const &z() const noexcept requires(D == 3)
114 {
115 return _v.z();
116 }
117
123 template<int E>
124 [[nodiscard]] constexpr friend point<std::max(D, E)> operator+(point const &lhs, vector<E> const &rhs) noexcept
125 {
126 tt_axiom(lhs.is_valid() && rhs.is_valid());
127 return point<std::max(D, E)>{lhs._v + static_cast<f32x4>(rhs)};
128 }
129
135 template<int E>
136 [[nodiscard]] constexpr friend point<std::max(D, E)> operator+(vector<E> const &rhs, point const &lhs) noexcept
137 {
138 tt_axiom(lhs.is_valid() && rhs.is_valid());
139 return point<std::max(D, E)>{lhs._v + static_cast<f32x4>(rhs)};
140 }
141
147 template<int E>
148 [[nodiscard]] constexpr friend point<std::max(D, E)> operator-(point const &lhs, vector<E> const &rhs) noexcept
149 {
150 tt_axiom(lhs.is_valid() && rhs.is_valid());
151 return point<std::max(D, E)>{lhs._v - static_cast<f32x4>(rhs)};
152 }
153
159 [[nodiscard]] constexpr friend vector<D> operator-(point const &lhs, point const &rhs) noexcept
160 {
161 tt_axiom(lhs.is_valid() && rhs.is_valid());
162 return vector<D>{lhs._v - rhs._v};
163 }
164
170 [[nodiscard]] constexpr friend bool operator==(point const &lhs, point const &rhs) noexcept
171 {
172 tt_axiom(lhs.is_valid() && rhs.is_valid());
173 return lhs._v == rhs._v;
174 }
175
176 template<int E>
177 [[nodiscard]] friend constexpr auto midpoint(point const &lhs, point<E> const &rhs) noexcept
178 {
179 return point<std::max(D,E)>{midpoint(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
180 }
181
182 template<int E>
183 [[nodiscard]] friend constexpr auto reflect(point const &lhs, point<E> const &rhs) noexcept
184 {
185 return point<std::max(D, E)>{reflect_point(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
186 }
187
191 [[nodiscard]] constexpr bool is_valid() const noexcept
192 {
193 return _v.w() != 0.0f && (D == 3 || _v.z() == 0.0f);
194 }
195
196private:
197 f32x4 _v;
198};
199
200} // namespace geo
201
202using point2 = geo::point<2>;
203using point3 = geo::point<3>;
204
205} // namespace tt
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point.hpp:21
constexpr friend point< std::max(D, E)> operator+(vector< E > const &rhs, point const &lhs) noexcept
Move a point along a vector.
Definition point.hpp:136
constexpr float & x() noexcept
Access the x element from the point.
Definition point.hpp:73
constexpr float const & y() const noexcept
Access the y element from the point.
Definition point.hpp:105
constexpr point(float x, float y) noexcept
Construct a 2D point from x and y elements.
Definition point.hpp:61
constexpr friend point< std::max(D, E)> operator+(point const &lhs, vector< E > const &rhs) noexcept
Move a point along a vector.
Definition point.hpp:124
constexpr point(float x, float y, float z=0.0) noexcept
Construct a 3D point from x, y and z elements.
Definition point.hpp:68
constexpr point(point< E > const &other) noexcept
Construct a point from a lower dimension point.
Definition point.hpp:33
constexpr point(f32x4 const &other) noexcept
Construct a point from a f32x4-numeric_array.
Definition point.hpp:48
constexpr float const & z() const noexcept
Access the z element from the point.
Definition point.hpp:113
constexpr friend vector< D > operator-(point const &lhs, point const &rhs) noexcept
Find the vector between two points.
Definition point.hpp:159
constexpr point() noexcept
Construct a point at the origin of the coordinate system.
Definition point.hpp:55
constexpr friend bool operator==(point const &lhs, point const &rhs) noexcept
Compare if two points are equal.
Definition point.hpp:170
constexpr bool is_valid() const noexcept
Check if the point is valid.
Definition point.hpp:191
constexpr friend point< std::max(D, E)> operator-(point const &lhs, vector< E > const &rhs) noexcept
Move a point backward along the vector.
Definition point.hpp:148
constexpr float const & x() const noexcept
Access the x element from the point.
Definition point.hpp:97
constexpr float & z() noexcept
Access the z element from the point.
Definition point.hpp:89
constexpr float & y() noexcept
Access the y element from the point.
Definition point.hpp:81
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:20
T max(T... args)