xref: /aosp_15_r20/external/libchrome/ui/gfx/geometry/box_f.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef UI_GFX_GEOMETRY_BOX_F_H_
6 #define UI_GFX_GEOMETRY_BOX_F_H_
7 
8 #include <iosfwd>
9 #include <string>
10 
11 #include "ui/gfx/geometry/point3_f.h"
12 #include "ui/gfx/geometry/vector3d_f.h"
13 
14 namespace gfx {
15 
16 // A 3d version of gfx::RectF, with the positive z-axis pointed towards
17 // the camera.
18 class GFX_EXPORT BoxF {
19  public:
BoxF()20   constexpr BoxF() : BoxF(0, 0, 0) {}
BoxF(float width,float height,float depth)21   constexpr BoxF(float width, float height, float depth)
22       : BoxF(0, 0, 0, width, height, depth) {}
BoxF(float x,float y,float z,float width,float height,float depth)23   constexpr BoxF(float x,
24                  float y,
25                  float z,
26                  float width,
27                  float height,
28                  float depth)
29       : BoxF(Point3F(x, y, z), width, height, depth) {}
BoxF(const Point3F & origin,float width,float height,float depth)30   constexpr BoxF(const Point3F& origin, float width, float height, float depth)
31       : origin_(origin),
32         width_(width >= 0 ? width : 0),
33         height_(height >= 0 ? height : 0),
34         depth_(depth >= 0 ? depth : 0) {}
35 
36   // Scales all three axes by the given scale.
Scale(float scale)37   void Scale(float scale) {
38     Scale(scale, scale, scale);
39   }
40 
41   // Scales each axis by the corresponding given scale.
Scale(float x_scale,float y_scale,float z_scale)42   void Scale(float x_scale, float y_scale, float z_scale) {
43     origin_.Scale(x_scale, y_scale, z_scale);
44     set_size(width_ * x_scale, height_ * y_scale, depth_ * z_scale);
45   }
46 
47   // Moves the box by the specified distance in each dimension.
48   void operator+=(const Vector3dF& offset) {
49     origin_ += offset;
50   }
51 
52   // Returns true if the box has no interior points.
53   bool IsEmpty() const;
54 
55   // Computes the union of this box with the given box. The union is the
56   // smallest box that contains both boxes.
57   void Union(const BoxF& box);
58 
59   std::string ToString() const;
60 
x()61   constexpr float x() const { return origin_.x(); }
set_x(float x)62   void set_x(float x) { origin_.set_x(x); }
63 
y()64   constexpr float y() const { return origin_.y(); }
set_y(float y)65   void set_y(float y) { origin_.set_y(y); }
66 
z()67   constexpr float z() const { return origin_.z(); }
set_z(float z)68   void set_z(float z) { origin_.set_z(z); }
69 
width()70   constexpr float width() const { return width_; }
set_width(float width)71   void set_width(float width) { width_ = width < 0 ? 0 : width; }
72 
height()73   constexpr float height() const { return height_; }
set_height(float height)74   void set_height(float height) { height_ = height < 0 ? 0 : height; }
75 
depth()76   constexpr float depth() const { return depth_; }
set_depth(float depth)77   void set_depth(float depth) { depth_ = depth < 0 ? 0 : depth; }
78 
right()79   constexpr float right() const { return x() + width(); }
bottom()80   constexpr float bottom() const { return y() + height(); }
front()81   constexpr float front() const { return z() + depth(); }
82 
set_size(float width,float height,float depth)83   void set_size(float width, float height, float depth) {
84     width_ = width < 0 ? 0 : width;
85     height_ = height < 0 ? 0 : height;
86     depth_ = depth < 0 ? 0 : depth;
87   }
88 
origin()89   constexpr const Point3F& origin() const { return origin_; }
set_origin(const Point3F & origin)90   void set_origin(const Point3F& origin) { origin_ = origin; }
91 
92   // Expands |this| to contain the given point, if necessary. Please note, even
93   // if |this| is empty, after the function |this| will continue to contain its
94   // |origin_|.
95   void ExpandTo(const Point3F& point);
96 
97   // Expands |this| to contain the given box, if necessary. Please note, even
98   // if |this| is empty, after the function |this| will continue to contain its
99   // |origin_|.
100   void ExpandTo(const BoxF& box);
101 
102  private:
103   // Expands the box to contain the two given points. It is required that each
104   // component of |min| is less than or equal to the corresponding component in
105   // |max|. Precisely, what this function does is ensure that after the function
106   // completes, |this| contains origin_, min, max, and origin_ + (width_,
107   // height_, depth_), even if the box is empty. Emptiness checks are handled in
108   // the public function Union.
109   void ExpandTo(const Point3F& min, const Point3F& max);
110 
111   Point3F origin_;
112   float width_;
113   float height_;
114   float depth_;
115 };
116 
117 GFX_EXPORT BoxF UnionBoxes(const BoxF& a, const BoxF& b);
118 
ScaleBox(const BoxF & b,float x_scale,float y_scale,float z_scale)119 inline BoxF ScaleBox(const BoxF& b,
120                      float x_scale,
121                      float y_scale,
122                      float z_scale) {
123   return BoxF(b.x() * x_scale,
124               b.y() * y_scale,
125               b.z() * z_scale,
126               b.width() * x_scale,
127               b.height() * y_scale,
128               b.depth() * z_scale);
129 }
130 
ScaleBox(const BoxF & b,float scale)131 inline BoxF ScaleBox(const BoxF& b, float scale) {
132   return ScaleBox(b, scale, scale, scale);
133 }
134 
135 inline bool operator==(const BoxF& a, const BoxF& b) {
136   return a.origin() == b.origin() && a.width() == b.width() &&
137          a.height() == b.height() && a.depth() == b.depth();
138 }
139 
140 inline bool operator!=(const BoxF& a, const BoxF& b) {
141   return !(a == b);
142 }
143 
144 inline BoxF operator+(const BoxF& b, const Vector3dF& v) {
145   return BoxF(b.x() + v.x(),
146               b.y() + v.y(),
147               b.z() + v.z(),
148               b.width(),
149               b.height(),
150               b.depth());
151 }
152 
153 // This is declared here for use in gtest-based unit tests but is defined in
154 // the //ui/gfx:test_support target. Depend on that to use this in your unit
155 // test. This should not be used in production code - call ToString() instead.
156 void PrintTo(const BoxF& box, ::std::ostream* os);
157 
158 }  // namespace gfx
159 
160 #endif  // UI_GFX_GEOMETRY_BOX_F_H_
161