Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/geometry/rect_conversions.h" | 5 #include "ui/gfx/geometry/rect_conversions.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "ui/gfx/geometry/safe_integer_conversions.h" | 11 #include "ui/gfx/geometry/safe_integer_conversions.h" |
| 12 | 12 |
| 13 namespace gfx { | 13 namespace gfx { |
| 14 | 14 |
| 15 Rect ToEnclosingRect(const RectF& rect) { | 15 Rect ToEnclosingRect(const RectF& rect) { |
| 16 int min_x = ToFlooredInt(rect.x()); | 16 int min_x = ToFlooredInt(rect.x()); |
| 17 int min_y = ToFlooredInt(rect.y()); | 17 int min_y = ToFlooredInt(rect.y()); |
| 18 float max_x = rect.right(); | 18 int max_x = ToCeiledInt(rect.right()); |
| 19 float max_y = rect.bottom(); | 19 int max_y = ToCeiledInt(rect.bottom()); |
| 20 int width = | 20 int width = base::SaturatedSubtraction(max_x, min_x); |
| 21 rect.width() == 0 | 21 int height = base::SaturatedSubtraction(max_y, min_y); |
|
Peter Mayo
2017/03/14 18:35:59
move into set from range in header
| |
| 22 ? 0 | 22 |
| 23 : std::max( | 23 // clamping may move one side, choose to move the bigger by recomputing it. |
| 24 ToCeiledInt(static_cast<double>(ToCeiledInt(max_x)) - min_x), | 24 // The computation of right is implicit, so this happens vacuosly. |
| 25 0); | 25 // If Right is small, move left to width away. |
| 26 int height = | 26 // If Left is big(negative), move them both to keep the center. |
| 27 rect.height() == 0 | 27 if (width >= std::numeric_limits<int>::max() / 2) { |
| 28 ? 0 | 28 if (base::SaturatedAbsolute(max_x) < width / 2) |
| 29 : std::max( | 29 min_x = max_x - width; |
| 30 ToCeiledInt(static_cast<double>(ToCeiledInt(max_y)) - min_y), | 30 else if (base::SaturatedAbsolute(min_x) > width / 2) |
| 31 0); | 31 min_x = min_x + width / 4; |
| 32 } | |
| 33 | |
| 34 // Same computation, other axis. | |
| 35 if (height >= std::numeric_limits<int>::max() / 2) { | |
| 36 if (base::SaturatedAbsolute(max_y) < height / 2) | |
| 37 min_y = max_y - height; | |
| 38 else if (base::SaturatedAbsolute(min_y) > height / 2) | |
| 39 min_y = min_y + height / 4; | |
| 40 } | |
| 41 | |
| 32 return Rect(min_x, min_y, width, height); | 42 return Rect(min_x, min_y, width, height); |
| 33 } | 43 } |
| 34 | 44 |
| 35 Rect ToEnclosedRect(const RectF& rect) { | 45 Rect ToEnclosedRect(const RectF& rect) { |
| 36 int min_x = ToCeiledInt(rect.x()); | 46 int min_x = ToCeiledInt(rect.x()); |
| 37 int min_y = ToCeiledInt(rect.y()); | 47 int min_y = ToCeiledInt(rect.y()); |
| 38 float max_x = rect.right(); | 48 int max_x = ToFlooredInt(rect.right()); |
| 39 float max_y = rect.bottom(); | 49 int max_y = ToFlooredInt(rect.bottom()); |
| 40 int width = std::max( | 50 int width = base::SaturatedSubtraction(max_x, min_x); |
| 41 ToFlooredInt(static_cast<float>(ToFlooredInt(max_x)) - min_x), 0); | 51 int height = base::SaturatedSubtraction(max_y, min_y); |
| 42 int height = std::max( | 52 |
| 43 ToFlooredInt(static_cast<float>(ToFlooredInt(max_y)) - min_y), 0); | 53 // clamping may move one side, choose to move the bigger by recomputing it. |
| 54 // The computation of right is implicit, so this happens vacuosly. | |
| 55 // If Right is small, move left to width away. | |
| 56 // If Left is big(negative), move them both to keep the center. | |
| 57 if (width >= std::numeric_limits<int>::max() / 2) { | |
| 58 if (base::SaturatedAbsolute(max_x) < width / 2) | |
| 59 min_x = max_x - width; | |
| 60 else if (base::SaturatedAbsolute(min_x) > width / 2) | |
| 61 min_x = min_x + width / 4; | |
| 62 } | |
| 63 | |
| 64 // Same computation, other axis. | |
| 65 if (height >= std::numeric_limits<int>::max() / 2) { | |
| 66 if (base::SaturatedAbsolute(max_y) < height / 2) | |
| 67 min_y = max_y - height; | |
| 68 else if (base::SaturatedAbsolute(min_y) > height / 2) | |
| 69 min_y = min_y + height / 4; | |
| 70 } | |
| 71 | |
| 44 return Rect(min_x, min_y, width, height); | 72 return Rect(min_x, min_y, width, height); |
| 45 } | 73 } |
| 46 | 74 |
| 47 Rect ToNearestRect(const RectF& rect) { | 75 Rect ToNearestRect(const RectF& rect) { |
| 48 float float_min_x = rect.x(); | 76 float float_min_x = rect.x(); |
| 49 float float_min_y = rect.y(); | 77 float float_min_y = rect.y(); |
| 50 float float_max_x = rect.right(); | 78 float float_max_x = rect.right(); |
| 51 float float_max_y = rect.bottom(); | 79 float float_max_y = rect.bottom(); |
| 52 | 80 |
| 53 int min_x = ToRoundedInt(float_min_x); | 81 int min_x = ToRoundedInt(float_min_x); |
| 54 int min_y = ToRoundedInt(float_min_y); | 82 int min_y = ToRoundedInt(float_min_y); |
| 55 int max_x = ToRoundedInt(float_max_x); | 83 int max_x = ToRoundedInt(float_max_x); |
| 56 int max_y = ToRoundedInt(float_max_y); | 84 int max_y = ToRoundedInt(float_max_y); |
| 57 | 85 |
| 58 // If these DCHECKs fail, you're using the wrong method, consider using | 86 // If these DCHECKs fail, you're using the wrong method, consider using |
| 59 // ToEnclosingRect or ToEnclosedRect instead. | 87 // ToEnclosingRect or ToEnclosedRect instead. |
| 60 DCHECK(std::abs(min_x - float_min_x) < 0.01f); | 88 DCHECK(std::abs(min_x - float_min_x) < 0.01f); |
| 61 DCHECK(std::abs(min_y - float_min_y) < 0.01f); | 89 DCHECK(std::abs(min_y - float_min_y) < 0.01f); |
| 62 DCHECK(std::abs(max_x - float_max_x) < 0.01f); | 90 DCHECK(std::abs(max_x - float_max_x) < 0.01f); |
| 63 DCHECK(std::abs(max_y - float_max_y) < 0.01f); | 91 DCHECK(std::abs(max_y - float_max_y) < 0.01f); |
| 64 | 92 |
| 65 return Rect(min_x, min_y, max_x - min_x, max_y - min_y); | 93 return Rect(min_x, min_y, max_x - min_x, max_y - min_y); |
|
Peter Mayo
2017/03/17 00:30:48
Oops - missed this in the previous CL.
| |
| 66 } | 94 } |
| 67 | 95 |
| 68 bool IsNearestRectWithinDistance(const gfx::RectF& rect, float distance) { | 96 bool IsNearestRectWithinDistance(const gfx::RectF& rect, float distance) { |
| 69 float float_min_x = rect.x(); | 97 float float_min_x = rect.x(); |
| 70 float float_min_y = rect.y(); | 98 float float_min_y = rect.y(); |
| 71 float float_max_x = rect.right(); | 99 float float_max_x = rect.right(); |
| 72 float float_max_y = rect.bottom(); | 100 float float_max_y = rect.bottom(); |
| 73 | 101 |
| 74 int min_x = ToRoundedInt(float_min_x); | 102 int min_x = ToRoundedInt(float_min_x); |
| 75 int min_y = ToRoundedInt(float_min_y); | 103 int min_y = ToRoundedInt(float_min_y); |
| 76 int max_x = ToRoundedInt(float_max_x); | 104 int max_x = ToRoundedInt(float_max_x); |
| 77 int max_y = ToRoundedInt(float_max_y); | 105 int max_y = ToRoundedInt(float_max_y); |
| 78 | 106 |
| 79 return | 107 return |
| 80 (std::abs(min_x - float_min_x) < distance) && | 108 (std::abs(min_x - float_min_x) < distance) && |
| 81 (std::abs(min_y - float_min_y) < distance) && | 109 (std::abs(min_y - float_min_y) < distance) && |
| 82 (std::abs(max_x - float_max_x) < distance) && | 110 (std::abs(max_x - float_max_x) < distance) && |
| 83 (std::abs(max_y - float_max_y) < distance); | 111 (std::abs(max_y - float_max_y) < distance); |
| 84 } | 112 } |
| 85 | 113 |
| 86 Rect ToFlooredRectDeprecated(const RectF& rect) { | 114 Rect ToFlooredRectDeprecated(const RectF& rect) { |
| 87 return Rect(ToFlooredInt(rect.x()), | 115 return Rect(ToFlooredInt(rect.x()), |
| 88 ToFlooredInt(rect.y()), | 116 ToFlooredInt(rect.y()), |
| 89 ToFlooredInt(rect.width()), | 117 ToFlooredInt(rect.width()), |
| 90 ToFlooredInt(rect.height())); | 118 ToFlooredInt(rect.height())); |
| 91 } | 119 } |
| 92 | 120 |
| 93 } // namespace gfx | 121 } // namespace gfx |
| 94 | 122 |
| OLD | NEW |