HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
scale2.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 "vector2.hpp"
8#include "extent2.hpp"
9
10namespace hi { inline namespace v1 {
11
12class scale2 {
13public:
14 using array_type = simd<float, 4>;
15 using value_type = array_type::value_type;
16
17 constexpr scale2(scale2 const&) noexcept = default;
18 constexpr scale2(scale2&&) noexcept = default;
19 constexpr scale2& operator=(scale2 const&) noexcept = default;
20 constexpr scale2& operator=(scale2&&) noexcept = default;
21
22 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
23 {
24 return _v;
25 }
26
27 [[nodiscard]] constexpr explicit scale2(f32x4 const& v) noexcept : _v(v)
28 {
29 hi_axiom(holds_invariant());
30 }
31
32 [[nodiscard]] constexpr explicit scale2(vector2 const& v) noexcept : _v(static_cast<f32x4>(v).xyz1()) {}
33
34 [[nodiscard]] constexpr scale2() noexcept : _v(1.0f, 1.0f, 1.0f, 1.0f) {}
35
36 [[nodiscard]] constexpr scale2(float value) noexcept : _v(value, value, value, 1.0f) {}
37
38 [[nodiscard]] constexpr scale2(float x, float y) noexcept : _v(x, y, 1.0f, 1.0f) {}
39
45 [[nodiscard]] static constexpr scale2 uniform(extent2 src_extent, extent2 dst_extent) noexcept
46 {
47 hilet non_uniform_scale = f32x4{dst_extent}.xyxy() / f32x4{src_extent}.xyxy();
48 hilet uniform_scale = std::min(non_uniform_scale.x(), non_uniform_scale.y());
49 return scale2{uniform_scale};
50 }
51
52 [[nodiscard]] constexpr friend bool operator==(scale2 const& lhs, scale2 const& rhs) noexcept
53 {
54 return equal(lhs._v, rhs._v);
55 }
56
57 [[nodiscard]] constexpr bool holds_invariant() const noexcept
58 {
59 return _v.z() == 1.0f and _v.w() == 1.0f;
60 }
61
62 [[nodiscard]] constexpr float& x() noexcept
63 {
64 return _v.x();
65 }
66
67 [[nodiscard]] constexpr float& y() noexcept
68 {
69 return _v.y();
70 }
71
72 [[nodiscard]] constexpr float x() const noexcept
73 {
74 return _v.x();
75 }
76
77 [[nodiscard]] constexpr float y() const noexcept
78 {
79 return _v.y();
80 }
81
82private:
83 array_type _v;
84};
85
86[[nodiscard]] constexpr scale2 operator/(extent2 const& lhs, extent2 const& rhs) noexcept
87{
88 hi_axiom(rhs.width() > 0.0f and rhs.height() > 0.0f);
89 return scale2{f32x4{lhs}.xy11() / f32x4{rhs}.xy11()};
90}
91
92}} // namespace hi::v1
Defined the geo::extent, extent2 and extent3 types.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
A high-level geometric extent.
Definition extent2.hpp:26
Definition scale2.hpp:12
static constexpr scale2 uniform(extent2 src_extent, extent2 dst_extent) noexcept
Get a uniform-scale-transform to scale an extent to another extent.
Definition scale2.hpp:45
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:18
T min(T... args)