Skip to content

Commit

Permalink
Move cc::MathUtil::ApproximatelyEqual() from cc/base/math_util.h to b…
Browse files Browse the repository at this point in the history
…ase/numerics/ranges.h

This CL moves the utility function cc::MathUtil::ApproximatelyEqual() from
//cc/base/math_util.h to //base/numerics/ranges.h, so that it can be used
from folders that don't need/ can't to depend on //cc.

It also adds an static_assert for std::is_arithmetic<>, and removes the
DCHECK internal (because we don't depend on base/logging.h inside
base/numerics).

Bug: 771345
Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel
Change-Id: I2617abe630774c3148df1507afcb57257ed394bc
Reviewed-on: https://chromium-review.googlesource.com/704208
Commit-Queue: Miguel Casas <mcasas@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Reviewed-by: Scott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#507807}
  • Loading branch information
yellowdoge authored and Commit Bot committed Oct 10, 2017
1 parent 93f2601 commit 92f328e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 58 deletions.
7 changes: 7 additions & 0 deletions base/numerics/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BASE_NUMERICS_RANGES_H_

#include <algorithm>
#include <cmath>

namespace base {

Expand All @@ -14,6 +15,12 @@ T ClampToRange(T value, T min, T max) {
return std::min(std::max(value, min), max);
}

template <typename T>
constexpr bool IsApproximatelyEqual(T lhs, T rhs, T tolerance) {
static_assert(std::is_arithmetic<T>::value, "Argument must be arithmetic");
return std::abs(rhs - lhs) <= tolerance;
}

} // namespace base

#endif // BASE_NUMERICS_RANGES_H_
44 changes: 22 additions & 22 deletions cc/animation/transform_operation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include "base/logging.h"
#include "base/numerics/math_constants.h"
#include "base/numerics/ranges.h"
#include "cc/animation/transform_operation.h"
#include "cc/animation/transform_operations.h"
#include "cc/base/math_util.h"
#include "ui/gfx/geometry/angle_conversions.h"
#include "ui/gfx/geometry/box_f.h"
#include "ui/gfx/geometry/vector3d_f.h"
Expand Down Expand Up @@ -120,31 +120,31 @@ bool TransformOperation::ApproximatelyEqual(const TransformOperation& other,
return false;
switch (type) {
case TransformOperation::TRANSFORM_OPERATION_TRANSLATE:
return MathUtil::ApproximatelyEqual(translate.x, other.translate.x,
tolerance) &&
MathUtil::ApproximatelyEqual(translate.y, other.translate.y,
tolerance) &&
MathUtil::ApproximatelyEqual(translate.z, other.translate.z,
tolerance);
return base::IsApproximatelyEqual(translate.x, other.translate.x,
tolerance) &&
base::IsApproximatelyEqual(translate.y, other.translate.y,
tolerance) &&
base::IsApproximatelyEqual(translate.z, other.translate.z,
tolerance);
case TransformOperation::TRANSFORM_OPERATION_ROTATE:
return MathUtil::ApproximatelyEqual(rotate.axis.x, other.rotate.axis.x,
tolerance) &&
MathUtil::ApproximatelyEqual(rotate.axis.y, other.rotate.axis.y,
tolerance) &&
MathUtil::ApproximatelyEqual(rotate.axis.z, other.rotate.axis.z,
tolerance) &&
MathUtil::ApproximatelyEqual(rotate.angle, other.rotate.angle,
tolerance);
return base::IsApproximatelyEqual(rotate.axis.x, other.rotate.axis.x,
tolerance) &&
base::IsApproximatelyEqual(rotate.axis.y, other.rotate.axis.y,
tolerance) &&
base::IsApproximatelyEqual(rotate.axis.z, other.rotate.axis.z,
tolerance) &&
base::IsApproximatelyEqual(rotate.angle, other.rotate.angle,
tolerance);
case TransformOperation::TRANSFORM_OPERATION_SCALE:
return MathUtil::ApproximatelyEqual(scale.x, other.scale.x, tolerance) &&
MathUtil::ApproximatelyEqual(scale.y, other.scale.y, tolerance) &&
MathUtil::ApproximatelyEqual(scale.z, other.scale.z, tolerance);
return base::IsApproximatelyEqual(scale.x, other.scale.x, tolerance) &&
base::IsApproximatelyEqual(scale.y, other.scale.y, tolerance) &&
base::IsApproximatelyEqual(scale.z, other.scale.z, tolerance);
case TransformOperation::TRANSFORM_OPERATION_SKEW:
return MathUtil::ApproximatelyEqual(skew.x, other.skew.x, tolerance) &&
MathUtil::ApproximatelyEqual(skew.y, other.skew.y, tolerance);
return base::IsApproximatelyEqual(skew.x, other.skew.x, tolerance) &&
base::IsApproximatelyEqual(skew.y, other.skew.y, tolerance);
case TransformOperation::TRANSFORM_OPERATION_PERSPECTIVE:
return MathUtil::ApproximatelyEqual(perspective_depth,
other.perspective_depth, tolerance);
return base::IsApproximatelyEqual(perspective_depth,
other.perspective_depth, tolerance);
case TransformOperation::TRANSFORM_OPERATION_MATRIX:
// TODO(vollick): we could expose a tolerance on gfx::Transform, but it's
// complex since we need a different tolerance per component. Driving this
Expand Down
6 changes: 0 additions & 6 deletions cc/base/math_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,6 @@ class CC_BASE_EXPORT MathUtil {
return RoundDownInternal(n, mul);
}

template <typename T>
static bool ApproximatelyEqual(T lhs, T rhs, T tolerance) {
DCHECK_LE(0, tolerance);
return std::abs(rhs - lhs) <= tolerance;
}

template <typename T>
static bool IsWithinEpsilon(T a, T b) {
return std::abs(a - b) < std::numeric_limits<T>::epsilon();
Expand Down
10 changes: 4 additions & 6 deletions chrome/browser/vr/animation_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@

#include <algorithm>

#include "base/numerics/ranges.h"
#include "base/stl_util.h"
#include "cc/animation/animation_curve.h"
#include "cc/animation/animation_target.h"
#include "cc/animation/animation_ticker.h"
#include "cc/animation/keyframed_animation_curve.h"
#include "cc/base/math_util.h"
#include "chrome/browser/vr/elements/ui_element.h"

using cc::MathUtil;

namespace vr {

namespace {
Expand Down Expand Up @@ -91,7 +89,7 @@ base::TimeDelta GetEndTime(cc::Animation* animation) {
}

bool SufficientlyEqual(float lhs, float rhs) {
return MathUtil::ApproximatelyEqual(lhs, rhs, kTolerance);
return base::IsApproximatelyEqual(lhs, rhs, kTolerance);
}

bool SufficientlyEqual(const cc::TransformOperations& lhs,
Expand All @@ -100,8 +98,8 @@ bool SufficientlyEqual(const cc::TransformOperations& lhs,
}

bool SufficientlyEqual(const gfx::SizeF& lhs, const gfx::SizeF& rhs) {
return MathUtil::ApproximatelyEqual(lhs.width(), rhs.width(), kTolerance) &&
MathUtil::ApproximatelyEqual(lhs.height(), rhs.height(), kTolerance);
return base::IsApproximatelyEqual(lhs.width(), rhs.width(), kTolerance) &&
base::IsApproximatelyEqual(lhs.height(), rhs.height(), kTolerance);
}

bool SufficientlyEqual(SkColor lhs, SkColor rhs) {
Expand Down
15 changes: 6 additions & 9 deletions chrome/browser/vr/elements/viewport_aware_root_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cmath>

#include "base/memory/ptr_util.h"
#include "base/numerics/ranges.h"
#include "chrome/browser/vr/elements/draw_phase.h"
#include "chrome/browser/vr/test/animation_utils.h"
#include "chrome/browser/vr/test/constants.h"
Expand All @@ -22,22 +23,18 @@ namespace {
const float kThreshold = ViewportAwareRoot::kViewportRotationTriggerDegrees;
const int kFramesPerSecond = 60;

bool FloatNearlyEqual(float a, float b) {
return std::abs(a - b) < kEpsilon;
}

bool Point3FAreNearlyEqual(const gfx::Point3F& lhs, const gfx::Point3F& rhs) {
return FloatNearlyEqual(lhs.x(), rhs.x()) &&
FloatNearlyEqual(lhs.y(), rhs.y()) &&
FloatNearlyEqual(lhs.z(), rhs.z());
return base::IsApproximatelyEqual(lhs.x(), rhs.x(), kEpsilon) &&
base::IsApproximatelyEqual(lhs.y(), rhs.y(), kEpsilon) &&
base::IsApproximatelyEqual(lhs.z(), rhs.z(), kEpsilon);
}

bool MatricesAreNearlyEqual(const gfx::Transform& lhs,
const gfx::Transform& rhs) {
for (int row = 0; row < 4; ++row) {
for (int col = 0; col < 4; ++col) {
if (!FloatNearlyEqual(lhs.matrix().get(row, col),
rhs.matrix().get(row, col))) {
if (!base::IsApproximatelyEqual(lhs.matrix().get(row, col),
rhs.matrix().get(row, col), kEpsilon)) {
return false;
}
}
Expand Down
8 changes: 3 additions & 5 deletions chrome/browser/vr/ui_scene_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "chrome/browser/vr/ui_scene_manager.h"

#include "base/macros.h"
#include "cc/base/math_util.h"
#include "base/numerics/ranges.h"
#include "chrome/browser/vr/color_scheme.h"
#include "chrome/browser/vr/elements/content_element.h"
#include "chrome/browser/vr/elements/ui_element.h"
Expand Down Expand Up @@ -64,10 +64,8 @@ static constexpr float kTolerance = 1e-5;
static constexpr float kSmallDelaySeconds = 0.1;

MATCHER_P2(SizeFsAreApproximatelyEqual, other, tolerance, "") {
return cc::MathUtil::ApproximatelyEqual(arg.width(), other.width(),
tolerance) &&
cc::MathUtil::ApproximatelyEqual(arg.height(), other.height(),
tolerance);
return base::IsApproximatelyEqual(arg.width(), other.width(), tolerance) &&
base::IsApproximatelyEqual(arg.height(), other.height(), tolerance);
}

void CheckHitTestableRecursive(UiElement* element) {
Expand Down
19 changes: 9 additions & 10 deletions ui/display/util/edid_parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>

#include "base/macros.h"
#include "base/numerics/ranges.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "ui/gfx/geometry/size.h"
Expand Down Expand Up @@ -155,23 +156,21 @@ ::testing::AssertionResult SkColorSpacePrimariesEquals(
const char* rhs_expr,
const SkColorSpacePrimaries& lhs,
const SkColorSpacePrimaries& rhs) {
// TODO(mcasas): consider using MathUtil::ApproximatelyEqual() when and if
// this is available in //base, https://crbug.com/771345
if (std::fabs(lhs.fRX - rhs.fRX) > kPrimariesPrecision)
if (!base::IsApproximatelyEqual(lhs.fRX, rhs.fRX, kPrimariesPrecision))
return AssertionFailure() << "fRX: " << lhs.fRX << " != " << rhs.fRX;
if (std::fabs(lhs.fRY - rhs.fRY) > kPrimariesPrecision)
if (!base::IsApproximatelyEqual(lhs.fRY, rhs.fRY, kPrimariesPrecision))
return AssertionFailure() << "fRY: " << lhs.fRY << " != " << rhs.fRY;
if (std::fabs(lhs.fGX - rhs.fGX) > kPrimariesPrecision)
if (!base::IsApproximatelyEqual(lhs.fGX, rhs.fGX, kPrimariesPrecision))
return AssertionFailure() << "fGX: " << lhs.fGX << " != " << rhs.fGX;
if (std::fabs(lhs.fGY - rhs.fGY) > kPrimariesPrecision)
if (!base::IsApproximatelyEqual(lhs.fGY, rhs.fGY, kPrimariesPrecision))
return AssertionFailure() << "fGY: " << lhs.fGY << " != " << rhs.fGY;
if (std::fabs(lhs.fBX - rhs.fBX) > kPrimariesPrecision)
if (!base::IsApproximatelyEqual(lhs.fBX, rhs.fBX, kPrimariesPrecision))
return AssertionFailure() << "fBX: " << lhs.fBX << " != " << rhs.fBX;
if (std::fabs(lhs.fBY - rhs.fBY) > kPrimariesPrecision)
if (!base::IsApproximatelyEqual(lhs.fBY, rhs.fBY, kPrimariesPrecision))
return AssertionFailure() << "fBY: " << lhs.fBY << " != " << rhs.fBY;
if (std::fabs(lhs.fWX - rhs.fWX) > kPrimariesPrecision)
if (!base::IsApproximatelyEqual(lhs.fWX, rhs.fWX, kPrimariesPrecision))
return AssertionFailure() << "fWX: " << lhs.fWX << " != " << rhs.fWX;
if (std::fabs(lhs.fWY - rhs.fWY) > kPrimariesPrecision)
if (!base::IsApproximatelyEqual(lhs.fWY, rhs.fWY, kPrimariesPrecision))
return AssertionFailure() << "fWY: " << lhs.fWY << " != " << rhs.fWY;
return AssertionSuccess();
}
Expand Down

0 comments on commit 92f328e

Please sign in to comment.