Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: ui/gfx/box_f.cc

Issue 25518002: Add BoxF::ExpandTo (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/box_f.h" 5 #include "ui/gfx/box_f.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 10
11 namespace gfx { 11 namespace gfx {
12 12
13 std::string BoxF::ToString() const { 13 std::string BoxF::ToString() const {
14 return base::StringPrintf("%s %fx%fx%f", 14 return base::StringPrintf("%s %fx%fx%f",
15 origin().ToString().c_str(), 15 origin().ToString().c_str(),
16 width_, 16 width_,
17 height_, 17 height_,
18 depth_); 18 depth_);
19 } 19 }
20 20
21 bool BoxF::IsEmpty() const { 21 bool BoxF::IsEmpty() const {
22 return (width_ == 0 && height_ == 0) || 22 return (width_ == 0 && height_ == 0) ||
23 (width_ == 0 && depth_ == 0) || 23 (width_ == 0 && depth_ == 0) ||
24 (height_ == 0 && depth_ == 0); 24 (height_ == 0 && depth_ == 0);
25 } 25 }
26 26
27 void BoxF::ExpandTo(const Point3F& min, const Point3F& max) {
28 float min_x = std::min(x(), min.x());
danakj 2013/10/01 14:23:44 DCHECK() that min <= max please.
29 float min_y = std::min(y(), min.y());
30 float min_z = std::min(z(), min.z());
31 float max_x = std::max(right(), max.x());
32 float max_y = std::max(bottom(), max.y());
33 float max_z = std::max(front(), max.z());
34
35 origin_.SetPoint(min_x, min_y, min_z);
36 width_ = max_x - min_x;
37 height_ = max_y - min_y;
38 depth_ = max_z - min_z;
39 }
40
27 void BoxF::Union(const BoxF& box) { 41 void BoxF::Union(const BoxF& box) {
28 if (IsEmpty()) { 42 if (IsEmpty()) {
29 *this = box; 43 *this = box;
30 return; 44 return;
31 } 45 }
32 if (box.IsEmpty()) 46 if (box.IsEmpty())
33 return; 47 return;
34 48
35 float min_x = std::min(x(), box.x()); 49 ExpandTo(box.origin(), gfx::Point3F(box.right(), box.bottom(), box.front()));
danakj 2013/10/01 14:23:44 What happens if |box| is entirely left/above |this
36 float min_y = std::min(y(), box.y()); 50 }
37 float min_z = std::min(z(), box.z());
38 float max_x = std::max(right(), box.right());
39 float max_y = std::max(bottom(), box.bottom());
40 float max_z = std::max(front(), box.front());
41 51
42 origin_.SetPoint(min_x, min_y, min_z); 52 void BoxF::ExpandTo(const Point3F& point) {
43 width_ = max_x - min_x; 53 ExpandTo(point, point);
44 height_ = max_y - min_y;
45 depth_ = max_z - min_z;
46 } 54 }
47 55
48 BoxF UnionBoxes(const BoxF& a, const BoxF& b) { 56 BoxF UnionBoxes(const BoxF& a, const BoxF& b) {
49 BoxF result = a; 57 BoxF result = a;
50 result.Union(b); 58 result.Union(b);
51 return result; 59 return result;
52 } 60 }
53 61
54 } // namespace gfx 62 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698