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#include "../macros.hpp"
10#include <algorithm>
11#include <exception>
12#include <compare>
13
14hi_export_module(hikogui.geometry : scale2);
15
16hi_export namespace hi { inline namespace v1 {
17
18class scale2 {
19public:
20 using array_type = simd<float, 4>;
21 using value_type = array_type::value_type;
22
23 constexpr scale2(scale2 const&) noexcept = default;
24 constexpr scale2(scale2&&) noexcept = default;
25 constexpr scale2& operator=(scale2 const&) noexcept = default;
26 constexpr scale2& operator=(scale2&&) noexcept = default;
27
28 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
29 {
30 return _v;
31 }
32
33 [[nodiscard]] constexpr explicit scale2(f32x4 const& v) noexcept : _v(v)
34 {
35 hi_axiom(holds_invariant());
36 }
37
38 [[nodiscard]] constexpr explicit scale2(vector2 const& v) noexcept : _v(static_cast<f32x4>(v).xyz1()) {}
39
40 [[nodiscard]] constexpr scale2() noexcept : _v(1.0f, 1.0f, 1.0f, 1.0f) {}
41
42 [[nodiscard]] constexpr scale2(float value) noexcept : _v(value, value, value, 1.0f) {}
43
44 [[nodiscard]] constexpr scale2(float x, float y) noexcept : _v(x, y, 1.0f, 1.0f) {}
45
51 [[nodiscard]] constexpr static scale2 uniform(extent2 src_extent, extent2 dst_extent) noexcept
52 {
53 auto const non_uniform_scale = f32x4{dst_extent}.xyxy() / f32x4{src_extent}.xyxy();
54 auto const uniform_scale = std::min(non_uniform_scale.x(), non_uniform_scale.y());
55 return scale2{uniform_scale};
56 }
57
58 [[nodiscard]] constexpr friend bool operator==(scale2 const& lhs, scale2 const& rhs) noexcept
59 {
60 return equal(lhs._v, rhs._v);
61 }
62
63 [[nodiscard]] constexpr bool holds_invariant() const noexcept
64 {
65 return _v.z() == 1.0f and _v.w() == 1.0f;
66 }
67
68 [[nodiscard]] constexpr float& x() noexcept
69 {
70 return _v.x();
71 }
72
73 [[nodiscard]] constexpr float& y() noexcept
74 {
75 return _v.y();
76 }
77
78 [[nodiscard]] constexpr float x() const noexcept
79 {
80 return _v.x();
81 }
82
83 [[nodiscard]] constexpr float y() const noexcept
84 {
85 return _v.y();
86 }
87
88private:
89 array_type _v;
90};
91
92[[nodiscard]] constexpr scale2 operator/(extent2 const& lhs, extent2 const& rhs) noexcept
93{
94 hi_axiom(rhs.width() > 0.0f and rhs.height() > 0.0f);
95 return scale2{f32x4{lhs}.xy11() / f32x4{rhs}.xy11()};
96}
97
98}} // namespace hi::v1
Defined the geo::extent, extent2 and extent3 types.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition simd_intf.hpp:18
A high-level geometric extent.
Definition extent2.hpp:32
Definition scale2.hpp:18
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:51
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:27
T min(T... args)