Home | History | Annotate | Download | only in gfx
      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 UI_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  private:
     98   Point3F origin_;
     99   float width_;
    100   float height_;
    101   float depth_;
    102 };
    103 
    104 UI_EXPORT BoxF UnionBoxes(const BoxF& a, const BoxF& b);
    105 
    106 inline BoxF ScaleBox(const BoxF& b,
    107                      float x_scale,
    108                      float y_scale,
    109                      float z_scale) {
    110   return BoxF(b.x() * x_scale,
    111               b.y() * y_scale,
    112               b.z() * z_scale,
    113               b.width() * x_scale,
    114               b.height() * y_scale,
    115               b.depth() * z_scale);
    116 }
    117 
    118 inline BoxF ScaleBox(const BoxF& b, float scale) {
    119   return ScaleBox(b, scale, scale, scale);
    120 }
    121 
    122 inline bool operator==(const BoxF& a, const BoxF& b) {
    123   return a.origin() == b.origin() && a.width() == b.width() &&
    124          a.height() == b.height() && a.depth() == b.depth();
    125 }
    126 
    127 inline bool operator!=(const BoxF& a, const BoxF& b) {
    128   return !(a == b);
    129 }
    130 
    131 inline BoxF operator+(const BoxF& b, const Vector3dF& v) {
    132   return BoxF(b.x() + v.x(),
    133               b.y() + v.y(),
    134               b.z() + v.z(),
    135               b.width(),
    136               b.height(),
    137               b.depth());
    138 }
    139 
    140 }  // namespace gfx
    141 
    142 #endif  // UI_GFX_BOX_F_H_
    143