opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
rect.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <iosfwd>
8
9namespace osc
10{
11 // Represents a 2D axis-aligned bounding box in a caller-defined coordinate
12 // system in which X always points towards the right, but Y can point either up
13 // (methods prefixed with `ypu_`) or down (methods prefixed with `ypd_`).
14 //
15 // The 1D equivalent to a `Rect` is a `ClosedInterval`. The 3D equivalent is an
16 // `AABB`.
17 class Rect final {
18 public:
19 // Returns a `Rect` with an `origin` of `point` and an area of zero (i.e.
20 // dimensions = {0, 0}) in the coordinate system of `point`.
21 static constexpr Rect from_point(const Vector2& point)
22 {
23 return Rect{point, Vector2{}};
24 }
25
26 // Returns a `Rect` with a centroid of `origin` and dimensions of `dimensions`.
28 {
29 return Rect{origin, dimensions};
30 }
31
32 // Returns a `Rect` constructed from two opposite corner points in the coordinate
33 // system of those points.
34 static Rect from_corners(const Vector2& p1, const Vector2& p2)
35 {
36 return Rect{0.5f * (p1 + p2), abs(p1 - p2)};
37 }
38
39 // Constructs an empty `Rect` with an `origin` of zero and `dimensions` of zero.
40 Rect() = default;
41
42 friend bool operator==(const Rect&, const Rect&) = default;
43
44 // Returns the origin (centroid) of this `Rect` in its (caller-defined)
45 // coordinate system.
46 Vector2 origin() const { return origin_; }
47
48 // Returns the dimensions of this `Rect`.
49 Vector2 dimensions() const { return dimensions_; }
50
51 // Returns the width of this `Rect`.
52 float width() const { return dimensions_.x(); }
53
54 // Returns the height of this `Rect`.
55 float height() const { return dimensions_.y(); }
56
57 // Returns the half extents of this `Rect`, which represents the distance from
58 // the origin to the edge of the `Rect` in each dimension.
59 Vector2 half_extents() const { return 0.5f * dimensions_; }
60
61 // Returns the area of this `Rect`.
62 float area() const { return dimensions_.x() * dimensions_.y(); }
63
64 // Returns the X coordinate of this `Rect`'s left edge. Assumes X "points right"
65 // in the coordinate system of this `Rect`.
66 float left() const { return origin_.x() - 0.5f*dimensions_.x(); }
67
68 // Returns the X coordinate of this `Rect`'s right edge. Assumes X "points right"
69 // in the coordinate system of this `Rect`.
70 float right() const { return origin_.x() + 0.5f*dimensions_.x(); }
71
72 // Assuming Y "points down" in the coordinate system of this `Rect`, returns the
73 // Y coordinate of this `Rect`'s top edge.
74 float ypd_top() const { return origin_.y() - 0.5f*dimensions_.y(); }
75
76 // Assuming Y "points down" in the coordinate system of this `Rect`, returns the
77 // Y coordinate of this `Rect`'s bottom edge.
78 float ypd_bottom() const { return origin_.y() + 0.5f*dimensions_.y(); }
79
80 // Assuming Y "points up" in the coordinate system of this `Rect`, returns the
81 // Y coordinate of this `Rect`'s top edge.
82 float ypu_top() const { return origin_.y() + 0.5f*dimensions_.y(); }
83
84 // Assuming Y "points up" in the coordinate system of this `Rect`, returns the
85 // Y coordinate of this `Rect`'s bottom edge.
86 float ypu_bottom() const { return origin_.y() - 0.5f*dimensions_.y(); }
87
88 // Returns the minimum and maximum opposite corner points of this `Rect`.
90 {
92 return {.min = origin_ - half_extentz, .max = origin_ + half_extentz};
93 }
94
95 // Returns the minimum corner point of this `Rect`, which is the point in this
96 // `Rect`'s coordinate system that has the smallest X and Y within the `Rect`'s
97 // bounds.
98 //
99 // What minimum "means" depends on the coordinate system of this `Rect`:
100 //
101 // - If the `Rect`'s data is in a coordinate system where Y points down (e.g.
102 // the UI coordinate system), then it means "top left".
103 // - If the `Rect`'s data is in a coordinate system where Y points up (e.g.
104 // viewport coordinate system), then it means "bottom left".
105 Vector2 min_corner() const { return origin_ - half_extents(); }
106
107 // Returns the maximum corner point of this `Rect`, which is the point in this `Rect`'s
108 // coordinate system that has the smallest X and Y within the `Rect`'s bounds.
109 //
110 // What "maximum" contextually means depends on the coordinate system of this `Rect`:
111 //
112 // - If the `Rect`'s data is in a coordinate system where Y points down (e.g.
113 // the UI coordinate system), then it means "bottom right".
114 // - If the `Rect`'s data is in a coordinate system where Y points up (e.g.
115 // viewport coordinate system), then it means "top right".
116 Vector2 max_corner() const { return origin_ + half_extents(); }
117
118 // Assuming Y "points down" in the coordinate system of this `Rect`, returns a point
119 // that represents the top-left corner of this `Rect` in that coordinate system.
120 Vector2 ypd_top_left() const { return min_corner(); }
121
122 // Assuming Y "points down" in the coordinate system of this `Rect`, returns a point
123 // that represents the top-right corner of this `Rect` in that coordinate system.
124 Vector2 ypd_top_right() const { const auto c = corners(); return Vector2{c.max.x(), c.min.y()}; }
125
126 // Assuming Y "points down" in the coordinate system of this `Rect`, returns a point
127 // that represents the bottom-left corner of this `Rect` in that coordinate system.
128 Vector2 ypd_bottom_left() const { const auto c = corners(); return Vector2{c.min.x(), c.max.y()}; }
129
130 // Assuming Y "points down" in the coordinate system of this `Rect`, returns a point
131 // that represents the bottom-right corner of this `Rect` in that coordinate system.
132 Vector2 ypd_bottom_right() const { return max_corner(); }
133
134 // Assuming Y "points up" in the coordinate system of this `Rect`, returns a point
135 // that represents the top-left corner of this `Rect` in that coordinate system.
136 Vector2 ypu_top_left() const { const auto c = corners(); return Vector2{c.min.x(), c.max.y()}; }
137
138 // Assuming Y "points up" in the coordinate system of this `Rect`, returns a point
139 // that represents the top-right corner of this `Rect` in that coordinate system.
140 Vector2 ypu_top_right() const { return max_corner(); }
141
142 // Assuming Y "points up" in the coordinate system of this `Rect`, returns a point
143 // that represents the bottom-left corner of this `Rect` in that coordinate system.
144 Vector2 ypu_bottom_left() const { return min_corner(); }
145
146 // Assuming Y "points up" in the coordinate system of this `Rect`, returns a point
147 // that represents the bottom-right corner of this `Rect` in that coordinate system.
148 Vector2 ypu_bottom_right() const { const auto c = corners(); return Vector2{c.max.x(), c.min.y()}; }
149
150 // Assuming the Y axis in the coordinate system of this `Rect` points in one direction
151 // (up or down), returns a new `Rect` in a flipped coordinate system that has the
152 // same scale and x axis origin, but has a new Y axis origin that point in the opposite
153 // direction.
154 //
155 // `distance_between_x_axes` should be the distance between the source X axis
156 // line (Y=0) and the flipped Y axis line. For example, if transforming between
157 // the viewport coordinate space (ypu) to the ui coordinate space (ypd), the
158 // `distance_between_x_axes` is the distance between the bottom of the viewport
159 // (ypu) and the top of the viewport (ypd): i.e. the height of the viewport.
161 {
162 return Rect{Vector2{origin_.x(), distance_between_x_axes - origin_.y()}, dimensions_};
163 }
164
165 // Returns a new `Rect` with the same `origin` as this `Rect`, but with the given
166 // new dimensions.
168 {
169 return Rect{origin_, new_dimensions};
170 }
171
172 // Returns a new `Rect` with the same `origin` as this `Rect`, but with its `dimensions`
173 // scaled by the given `scale_factors`
175 {
176 return Rect{origin_, scale_factors * dimensions_};
177 }
178
179 // Returns a new `Rect` with and `origin` equivalent to `scale_factor * original_origin` and
180 // `dimensions` equivalent to `scale_factor * original_dimensions`.
182 {
183 return Rect{scale_factor * origin_, scale_factor * dimensions_};
184 }
185 private:
186 // Constructs a `Rect` from its data members.
187 explicit constexpr Rect(const Vector2& origin, const Vector2& dimensions) :
188 origin_{origin},
189 dimensions_{dimensions}
190 {}
191
192 Vector2 origin_{};
193 Vector2 dimensions_{};
194 };
195
196 std::ostream& operator<<(std::ostream&, const Rect&);
197}
Definition rect.h:17
static constexpr Rect from_point(const Vector2 &point)
Definition rect.h:21
Vector2 ypu_top_right() const
Definition rect.h:140
Vector2 ypu_bottom_right() const
Definition rect.h:148
static Rect from_corners(const Vector2 &p1, const Vector2 &p2)
Definition rect.h:34
Vector2 ypu_bottom_left() const
Definition rect.h:144
float ypd_bottom() const
Definition rect.h:78
Rect with_flipped_y(float distance_between_x_axes) const
Definition rect.h:160
Vector2 ypd_bottom_left() const
Definition rect.h:128
Vector2 max_corner() const
Definition rect.h:116
Rect with_origin_and_dimensions_scaled_by(float scale_factor) const
Definition rect.h:181
Vector2 min_corner() const
Definition rect.h:105
RectCorners corners() const
Definition rect.h:89
float area() const
Definition rect.h:62
Rect()=default
Vector2 ypu_top_left() const
Definition rect.h:136
Vector2 origin() const
Definition rect.h:46
float left() const
Definition rect.h:66
float width() const
Definition rect.h:52
Rect with_dimensions(const Vector2 &new_dimensions) const
Definition rect.h:167
Vector2 ypd_bottom_right() const
Definition rect.h:132
Vector2 ypd_top_right() const
Definition rect.h:124
float ypu_bottom() const
Definition rect.h:86
float ypd_top() const
Definition rect.h:74
static constexpr Rect from_origin_and_dimensions(const Vector2 &origin, const Vector2 &dimensions)
Definition rect.h:27
float right() const
Definition rect.h:70
Vector2 half_extents() const
Definition rect.h:59
Rect with_dimensions_scaled_by(const Vector2 &scale_factors) const
Definition rect.h:174
Vector2 ypd_top_left() const
Definition rect.h:120
float ypu_top() const
Definition rect.h:82
float height() const
Definition rect.h:55
friend bool operator==(const Rect &, const Rect &)=default
Vector2 dimensions() const
Definition rect.h:49
constexpr reference x()
Returns (*this)[0]
Definition vector.h:157
constexpr reference y()
Returns (*this)[1]
Definition vector.h:162
Definition custom_decoration_generator.h:5
std::ostream & operator<<(std::ostream &, const Object &)
Vector< float, 2 > Vector2
Definition vector.h:636
T abs(T num)
Definition common_functions.h:24
constexpr U to(T &&value)
Definition conversion.h:56
Definition rect_corners.h:7