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