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 #include "ui/gfx/geometry/box_f.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11
12 namespace gfx {
13
ToString() const14 std::string BoxF::ToString() const {
15 return base::StringPrintf("%s %fx%fx%f",
16 origin().ToString().c_str(),
17 width_,
18 height_,
19 depth_);
20 }
21
IsEmpty() const22 bool BoxF::IsEmpty() const {
23 return (width_ == 0 && height_ == 0) ||
24 (width_ == 0 && depth_ == 0) ||
25 (height_ == 0 && depth_ == 0);
26 }
27
ExpandTo(const Point3F & min,const Point3F & max)28 void BoxF::ExpandTo(const Point3F& min, const Point3F& max) {
29 DCHECK_LE(min.x(), max.x());
30 DCHECK_LE(min.y(), max.y());
31 DCHECK_LE(min.z(), max.z());
32
33 float min_x = std::min(x(), min.x());
34 float min_y = std::min(y(), min.y());
35 float min_z = std::min(z(), min.z());
36 float max_x = std::max(right(), max.x());
37 float max_y = std::max(bottom(), max.y());
38 float max_z = std::max(front(), max.z());
39
40 origin_.SetPoint(min_x, min_y, min_z);
41 width_ = max_x - min_x;
42 height_ = max_y - min_y;
43 depth_ = max_z - min_z;
44 }
45
Union(const BoxF & box)46 void BoxF::Union(const BoxF& box) {
47 if (IsEmpty()) {
48 *this = box;
49 return;
50 }
51 if (box.IsEmpty())
52 return;
53 ExpandTo(box);
54 }
55
ExpandTo(const Point3F & point)56 void BoxF::ExpandTo(const Point3F& point) {
57 ExpandTo(point, point);
58 }
59
ExpandTo(const BoxF & box)60 void BoxF::ExpandTo(const BoxF& box) {
61 ExpandTo(box.origin(), gfx::Point3F(box.right(), box.bottom(), box.front()));
62 }
63
UnionBoxes(const BoxF & a,const BoxF & b)64 BoxF UnionBoxes(const BoxF& a, const BoxF& b) {
65 BoxF result = a;
66 result.Union(b);
67 return result;
68 }
69
70 } // namespace gfx
71