box_f.cc 1.7 KB

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