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