GCC Code Coverage Report


./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
10 of 13, 0 excluded
76.9%
Functions:
2 of 4, 0 excluded
50.0%
Branches:
2 of 36, 0 excluded
5.6%

libs/base/src/eu/base/rect.h
Line Branch Exec Source
1 #pragma once
2
3 #include "eu/assert/assert.h"
4
5 #include "eu/base/vec2.h"
6 #include "eu/base/range.h"
7
8 // Bottom, Left of screen is (0,0)
9 // X-axis is positive right, Y-axis is positive up
10
11
12 namespace eu
13 {
14 enum class RectCut { left, right, top, bottom };
15
16 /// offset from sides
17 /// \note unlike css this follows the (left right) (top bottom) order
18 struct Lrtb
19 {
20 float left; float right; float top; float bottom;
21 constexpr explicit Lrtb(float all) : left(all), right(all), top(all), bottom(all) {}
22 constexpr Lrtb(float lr, float tb) : left(lr), right(lr), top(tb), bottom(tb) {}
23 constexpr Lrtb(float l, float r, float t, float b) : left(l), right(r), top(t), bottom(b) {}
24 };
25
26 struct Rect
27 {
28 float left; // min x
29 float right; // max x
30 float top; // max y
31 float bottom; // min y
32
33 Rect();
34
35 bool operator==(const Rect& rhs) = delete;
36
37
38 24 [[nodiscard]] static constexpr Rect from_left_right_bottom_top(float left_side, float right_side, float bottom_side, float top_side)
39 {
40
1/10
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 17 taken 24 times.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 38 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 36 not taken.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 44 not taken.
24 ASSERTX(left_side <= right_side, left_side, right_side);
41
1/10
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 32 taken 24 times.
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 54 not taken.
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 52 not taken.
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 31 not taken.
✗ Branch 58 → 59 not taken.
✗ Branch 58 → 60 not taken.
24 ASSERTX(top_side >= bottom_side, top_side, bottom_side);
42 24 return { left_side, right_side, top_side, bottom_side };
43 }
44
45 [[nodiscard]] static Rect from_left_right_top_bottom(float left_side, float right_side, float top_side, float bottom_side);
46 [[nodiscard]] static Rect from_bottom_left_size(const v2& bl, const v2& size);
47 [[nodiscard]] static Rect from_top_left_size(const v2& top_left, const v2& size);
48 [[nodiscard]] static Rect from_size(const v2& size);
49 [[nodiscard]] static Rect from_point(const v2& point);
50
51 // extend/include/union current rect with another point/rect
52 void extend(const Rect& o);
53
54 void translate(const v2& v);
55 void inset(const Lrtb& lrtb); // moves each side towards the center
56
57 void set_empty();
58
59 /// centers the self rectangle inside the other rectangle
60 [[nodiscard]] static Rect from_center_inside_other(const Rect& self, const Rect& other);
61
62 /// scaled copy around rect center
63 [[nodiscard]] static Rect from_scaled(const Rect& self, float scale);
64
65 /// does this contains the argument?
66 [[nodiscard]] bool contains_exclusive(const Rect& r) const;
67
68 [[nodiscard]] Rect with_inset(const Lrtb& lrtb) const;
69 [[nodiscard]] Rect with_translate(const v2& v) const;
70 [[nodiscard]] Rect with_top_left_at(const v2& p) const;
71 [[nodiscard]] Rect with_bottom_left_at(const v2& p) const;
72 [[nodiscard]] Rect with_offset(const v2& p) const;
73 [[nodiscard]] Rect with_scale(float hor, float vert) const;
74
75 [[nodiscard]] v2 get_bottom_left() const;
76 [[nodiscard]] v2 get_center_pos() const;
77
78 // position is from bottom left
79 [[nodiscard]] v2 get_relative_center_pos() const;
80
81 // Returns true if the rectangle is empty (left >= right or top <= bottom)
82 [[nodiscard]] bool is_empty() const;
83
84 // a 0 width/height is also considered valid
85 [[nodiscard]] bool is_valid() const;
86
87 [[nodiscard]] v2 get_size() const;
88
89 [[nodiscard]] R<float> get_range_y() const;
90 [[nodiscard]] R<float> get_range_x() const;
91 [[nodiscard]] v2 get_top_left() const;
92 [[nodiscard]] v2 get_top_right() const;
93 [[nodiscard]] v2 get_bottom_right() const;
94
95 Rect cut_left(float amount, float h_spacing = 0.0f);
96 Rect cut_right(float amount, float h_spacing = 0.0f);
97 Rect cut_bottom(float amount, float v_spacing = 0.0f);
98 Rect cut_top(float amount, float v_spacing = 0.0f);
99
100 Rect cut(RectCut side, float amount, float spacing = 0.0f);
101
102 // Same as cut, except they keep the input rect intact.
103 // Useful for decorations
104 Rect get_left(float amount) const;
105 Rect get_right(float amount) const;
106 Rect get_bottom(float amount) const;
107 Rect get_top(float amount) const;
108
109 // These will add a rectangle outside of the input rectangle.
110 // Useful for tooltips and other overlay elements.
111 Rect add_left(float amount) const;
112 Rect add_right(float amount) const;
113 Rect add_bottom(float amount) const;
114 Rect add_top(float amount) const;
115
116 private:
117 56 constexpr Rect(float left_side, float right_side, float top_side, float bottom_side)
118 56 : left(left_side)
119 56 , right(right_side)
120 56 , top(top_side)
121 56 , bottom(bottom_side)
122 {
123 56 }
124 };
125
126 [[nodiscard]] v2 to_01(const Rect& r, const v2& from);
127 [[nodiscard]] v2 from_01(const Rect& r, const v2& from);
128
129 /// is the point contained within the rect?
130 /// \note on the border is considered within
131 bool is_within(const v2& p, const Rect& r);
132
133 std::string to_string(const Rect& r);
134 }
135
136 ADD_DEFAULT_FORMATTER(eu::Rect, std::string, eu::to_string);
137