HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
circle.hpp
Go to the documentation of this file.
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
8#pragma once
9
10#include "point.hpp"
12#include "quad.hpp"
13
14namespace hi {
15inline namespace v1 {
16
19class circle {
20public:
21 constexpr circle(circle const &other) noexcept = default;
22 constexpr circle(circle &&other) noexcept = default;
23 constexpr circle &operator=(circle const &other) noexcept = default;
24 constexpr circle &operator=(circle &&other) noexcept = default;
25
26 constexpr circle() noexcept : _v()
27 {
28 hi_axiom(holds_invariant());
29 }
30
31 [[nodiscard]] constexpr explicit circle(f32x4 v) noexcept : _v(v)
32 {
33 hi_axiom(holds_invariant());
34 }
35
36 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
37 {
38 return _v;
39 }
40
41 [[nodiscard]] constexpr circle(point3 point, float radius) noexcept :
42 _v(f32x4{point})
43 {
44 _v.w() = radius;
45 hi_axiom(holds_invariant());
46 }
47
48 [[nodiscard]] constexpr circle(aarectangle square) noexcept
49 {
50 hilet square_ = f32x4{square};
51
52 // center=(p3 + p0)/2, radius=(p3 - p0)/2
53 _v = (addsub<0b0011>(square_.zwzw(), square_.xyxy()) * 0.5f).xy0w();
54 hi_axiom(holds_invariant());
55 }
56
57 [[nodiscard]] constexpr bool empty() const noexcept
58 {
59 return _v.w() == 0.0f;
60 }
61
62 [[nodiscard]] explicit operator bool () const noexcept
63 {
64 return not empty();
65 }
66
67 [[nodiscard]] constexpr float radius() const noexcept
68 {
69 return _v.w();
70 }
71
72 [[nodiscard]] constexpr point3 center() const noexcept
73 {
74 return point3{_v.xyz1()};
75 }
76
77 [[nodiscard]] constexpr friend circle operator+(circle const &lhs, float rhs) noexcept
78 {
79 return circle{lhs._v + insert<3>(f32x4{}, rhs)};
80 }
81
82 [[nodiscard]] constexpr friend circle operator-(circle const &lhs, float rhs) noexcept
83 {
84 return circle{lhs._v - insert<3>(f32x4{}, rhs)};
85 }
86
87 [[nodiscard]] constexpr friend circle operator*(circle const &lhs, float rhs) noexcept
88 {
89 return circle{lhs._v * insert<3>(f32x4::broadcast(1.0f), rhs)};
90 }
91
92 [[nodiscard]] constexpr friend point3 midpoint(circle const &rhs) noexcept
93 {
94 return point3{rhs.center()};
95 }
96
97 [[nodiscard]] constexpr friend aarectangle bounding_rectangle(circle const &rhs) noexcept
98 {
99 hilet p = rhs._v.xyxy();
100 hilet r = neg<0b0011>(rhs._v.wwww());
101 return aarectangle{p + r};
102 }
103
104private:
105 // Stored as a center point (x, y, z), and radius (w).
106 f32x4 _v;
107
108 [[nodiscard]] constexpr bool holds_invariant() const noexcept
109 {
110 return _v.w() >= 0.0f;
111 }
112};
113
114
115}}
116
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:15
geometry/margins.hpp
Definition assert.hpp:18
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:28
A type defining a 2D circle.
Definition circle.hpp:19