HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
scale.hpp
1// Copyright Take Vos 2021-2022.
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 "matrix.hpp"
8#include "identity.hpp"
9#include "translate.hpp"
10#include "extent.hpp"
11
12namespace hi::inline v1 {
13namespace geo {
14
15template<int D>
16class scale {
17public:
18 static_assert(D == 2 || D == 3, "Only 2D or 3D scale-matrices are supported");
19
20 constexpr scale(scale const &) noexcept = default;
21 constexpr scale(scale &&) noexcept = default;
22 constexpr scale &operator=(scale const &) noexcept = default;
23 constexpr scale &operator=(scale &&) noexcept = default;
24
25 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
26 {
27 hi_axiom(holds_invariant());
28 return _v;
29 }
30
31 [[nodiscard]] constexpr explicit operator extent<float, 2>() const noexcept requires(D == 2)
32 {
33 return extent<2>{_v.xy00()};
34 }
35
36 [[nodiscard]] constexpr explicit operator extent<float, 3>() const noexcept
37 requires(D == 3)
38 {
39 return extent<3>{_v.xyz0()};
40 }
41
42 [[nodiscard]] constexpr explicit scale(f32x4 const &v) noexcept : _v(v)
43 {
44 hi_axiom(holds_invariant());
45 }
46
47 template<int E>
48 requires(E <= D)
49 [[nodiscard]] constexpr explicit scale(vector<float, E> const& v) noexcept : _v(static_cast<f32x4>(v).xyz1())
50 {
51 hi_axiom(holds_invariant());
52 }
53
54 [[nodiscard]] constexpr operator matrix<D>() const noexcept
55 {
56 hi_axiom(holds_invariant());
57 return matrix<D>{_v.x000(), _v._0y00(), _v._00z0(), _v._000w()};
58 }
59
60 [[nodiscard]] constexpr scale() noexcept : _v(1.0, 1.0, 1.0, 1.0) {}
61
62 [[nodiscard]] constexpr scale(identity const &) noexcept : _v(1.0, 1.0, 1.0, 1.0) {}
63
64 [[nodiscard]] constexpr scale(float value) noexcept requires(D == 2) : _v(value, value, 1.0, 1.0) {}
65
66 [[nodiscard]] constexpr scale(float value) noexcept requires(D == 3) : _v(value, value, value, 1.0) {}
67
68 [[nodiscard]] constexpr scale(float x, float y) noexcept requires(D == 2) : _v(x, y, 1.0, 1.0) {}
69
70 [[nodiscard]] constexpr scale(float x, float y, float z = 1.0) noexcept requires(D == 3) : _v(x, y, z, 1.0) {}
71
77 template<int E, int F>
78 requires(E <= D && F <= D)
79 [[nodiscard]] static constexpr scale uniform(extent<float, E> src_extent, extent<float, F> dst_extent) noexcept
80 {
82 dst_extent.width() != 0.0f && src_extent.width() != 0.0f && dst_extent.height() != 0.0f &&
83 src_extent.height() != 0.0f);
84
85 if constexpr (D == 2) {
86 hilet non_uniform_scale = static_cast<f32x4>(dst_extent).xyxy() / static_cast<f32x4>(src_extent).xyxy();
87 hilet uniform_scale = std::min(non_uniform_scale.x(), non_uniform_scale.y());
88 return scale{uniform_scale};
89
90 } else if constexpr (D == 3) {
91 hi_axiom(dst_extent.z() != 0.0f && src_extent.z() != 0.0f);
92 hilet non_uniform_scale = static_cast<f32x4>(dst_extent).xyzx() / static_cast<f32x4>(src_extent).xyzx();
93 hilet uniform_scale = std::min({non_uniform_scale.x(), non_uniform_scale.y(), non_uniform_scale.z()});
94 return scale{uniform_scale};
95
96 } else {
98 }
99 }
100
101 template<int E>
102 [[nodiscard]] constexpr vector<float, E> operator*(vector<float, E> const& rhs) const noexcept
103 {
104 hi_axiom(holds_invariant() && rhs.holds_invariant());
105 return vector<float, E>{_v * static_cast<f32x4>(rhs)};
106 }
107
108 template<int E>
109 [[nodiscard]] constexpr extent<float, E> operator*(extent<float, E> const& rhs) const noexcept
110 {
111 hi_axiom(holds_invariant() && rhs.holds_invariant());
112 return extent<float, E>{_v * static_cast<f32x4>(rhs)};
113 }
114
115 template<int E>
116 [[nodiscard]] constexpr point<float, E> operator*(point<float, E> const& rhs) const noexcept
117 {
118 hi_axiom(holds_invariant() && rhs.holds_invariant());
119 return point<float, E>{_v * static_cast<f32x4>(rhs)};
120 }
121
124 [[nodiscard]] constexpr aarectangle operator*(aarectangle const &rhs) const noexcept requires(D == 2)
125 {
126 return aarectangle{*this * get<0>(rhs), *this * get<3>(rhs)};
127 }
128
129 [[nodiscard]] constexpr rectangle operator*(rectangle const &rhs) const noexcept
130 {
131 return rectangle{*this * get<0>(rhs), *this * get<1>(rhs), *this * get<2>(rhs), *this * get<3>(rhs)};
132 }
133
134 [[nodiscard]] constexpr quad operator*(quad const &rhs) const noexcept
135 {
136 return quad{*this * rhs.p0, *this * rhs.p1, *this * rhs.p2, *this * rhs.p3};
137 }
138
147 [[nodiscard]] friend constexpr quad scale_from_center(quad const &lhs, scale const &rhs) noexcept requires(D == 2)
148 {
149 hilet top_extra = (lhs.top() * rhs._v.x() - lhs.top()) * 0.5f;
150 hilet bottom_extra = (lhs.bottom() * rhs._v.x() - lhs.bottom()) * 0.5f;
151 hilet left_extra = (lhs.left() * rhs._v.y() - lhs.left()) * 0.5f;
152 hilet right_extra = (lhs.right() * rhs._v.y() - lhs.right()) * 0.5f;
153
154 return {
155 lhs.p0 - bottom_extra - left_extra,
156 lhs.p1 + bottom_extra - right_extra,
157 lhs.p2 - top_extra + left_extra,
158 lhs.p3 + top_extra + right_extra};
159 }
160
161 [[nodiscard]] constexpr scale operator*(identity const &) const noexcept
162 {
163 hi_axiom(holds_invariant());
164 return *this;
165 }
166
167 template<int E>
168 [[nodiscard]] constexpr auto operator*(scale<E> const &rhs) const noexcept
169 {
170 hi_axiom(holds_invariant() && rhs.holds_invariant());
171 return scale<std::max(D, E)>{_v * static_cast<f32x4>(rhs)};
172 }
173
174 template<int E>
175 [[nodiscard]] constexpr bool operator==(scale<E> const &rhs) const noexcept
176 {
177 hi_axiom(holds_invariant() && rhs.holds_invariant());
178 return equal(_v, static_cast<f32x4>(rhs));
179 }
180
181 [[nodiscard]] constexpr bool holds_invariant() const noexcept
182 {
183 if constexpr (D == 3) {
184 return _v.w() == 1.0f;
185 } else {
186 return _v.z() == 1.0f and _v.w() == 1.0f;
187 }
188 }
189
190private:
191 f32x4 _v;
192};
193
194[[nodiscard]] constexpr scale<2> operator/(extent<float, 2> const& lhs, extent<float, 2> const& rhs) noexcept
195{
196 hi_axiom(rhs._v.x() != 0.0f);
197 hi_axiom(rhs._v.y() != 0.0f);
198 return scale<2>{lhs._v.xy11() / rhs._v.xy11()};
199}
200
201[[nodiscard]] constexpr scale<3> operator/(extent<float, 3> const& lhs, extent<float, 3> const& rhs) noexcept
202{
203 hi_axiom(rhs._v.x() != 0.0f);
204 hi_axiom(rhs._v.y() != 0.0f);
205 hi_axiom(rhs._v.z() != 0.0f);
206 return scale<3>{lhs._v.xyz1() / rhs._v.xyz1()};
207}
208
209template<int D>
210[[nodiscard]] constexpr matrix<D>
211matrix<D>::uniform(aarectangle src_rectangle, aarectangle dst_rectangle, alignment alignment) noexcept
212{
213 hilet scale = hi::geo::scale<D>::uniform(src_rectangle.size(), dst_rectangle.size());
214 hilet scaled_rectangle = scale * src_rectangle;
215 hilet translation = translate<D>::align(scaled_rectangle, dst_rectangle, alignment);
216 return translation * scale;
217}
218
219} // namespace geo
220
221using scale2 = geo::scale<2>;
222using scale3 = geo::scale<3>;
223
224} // namespace hi::inline v1
Defines identity type.
Defined the geo::extent, extent2 and extent3 types.
Defines geo::matrix, matrix2 and matrix3.
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:308
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:26
Definition extent.hpp:20
A 2D or 3D homogenius matrix for transforming homogenious vectors and points.
Definition matrix.hpp:33
Definition quad.hpp:17
point3 p0
Left-bottom.
Definition quad.hpp:19
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:20
Definition scale.hpp:16
constexpr aarectangle operator*(aarectangle const &rhs) const noexcept
Scale a rectangle around it's center.
Definition scale.hpp:124
friend constexpr quad scale_from_center(quad const &lhs, scale const &rhs) noexcept
scale the quad.
Definition scale.hpp:147
static constexpr scale uniform(extent< float, E > src_extent, extent< float, F > dst_extent) noexcept
Get a uniform-scale-transform to scale an extent to another extent.
Definition scale.hpp:79
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:21
T equal(T... args)
T max(T... args)
T min(T... args)