- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gfx / box_f.h
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:
17   BoxF()
18       : width_(0.f),
19         height_(0.f),
20         depth_(0.f) {}
21
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
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
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
39   ~BoxF() {}
40
41   // Scales all three axes by the given scale.
42   void Scale(float scale) {
43     Scale(scale, scale, scale);
44   }
45
46   // Scales each axis by the corresponding given 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
66   float x() const { return origin_.x(); }
67   void set_x(float x) { origin_.set_x(x); }
68
69   float y() const { return origin_.y(); }
70   void set_y(float y) { origin_.set_y(y); }
71
72   float z() const { return origin_.z(); }
73   void set_z(float z) { origin_.set_z(z); }
74
75   float width() const { return width_; }
76   void set_width(float width) { width_ = width < 0 ? 0 : width; }
77
78   float height() const { return height_; }
79   void set_height(float height) { height_ = height < 0 ? 0 : height; }
80
81   float depth() const { return depth_; }
82   void set_depth(float depth) { depth_ = depth < 0 ? 0 : depth; }
83
84   float right() const { return x() + width(); }
85   float bottom() const { return y() + height(); }
86   float front() const { return z() + depth(); }
87
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
94   const Point3F& origin() const { return 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  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
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
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 }  // namespace gfx
154
155 #endif  // UI_GFX_BOX_F_H_