Add BoxF::ExpandTo
[chromium-blink-merge.git] / ui / gfx / box_f.cc
blobb4191f36bb32b71b806fe1043f13bba2fef13d22
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.
5 #include "ui/gfx/box_f.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
12 namespace gfx {
14 std::string BoxF::ToString() const {
15 return base::StringPrintf("%s %fx%fx%f",
16 origin().ToString().c_str(),
17 width_,
18 height_,
19 depth_);
22 bool BoxF::IsEmpty() const {
23 return (width_ == 0 && height_ == 0) ||
24 (width_ == 0 && depth_ == 0) ||
25 (height_ == 0 && depth_ == 0);
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());
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());
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;
46 void BoxF::Union(const BoxF& box) {
47 if (IsEmpty()) {
48 *this = box;
49 return;
51 if (box.IsEmpty())
52 return;
54 ExpandTo(box.origin(), gfx::Point3F(box.right(), box.bottom(), box.front()));
57 void BoxF::ExpandTo(const Point3F& point) {
58 ExpandTo(point, point);
61 BoxF UnionBoxes(const BoxF& a, const BoxF& b) {
62 BoxF result = a;
63 result.Union(b);
64 return result;
67 } // namespace gfx