Commit c84d167c authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Stop using M_PI in ui/ code.

Use constants from base/numerics/math_constants.h instead. Then there is
no need to add _USE_MATH_DEFINES for MSVC.

Along the way, fix some nits:
- Use the right math function variant for floats.
- Mark more constants as constexpr.
- Do IWYU.
- Fix lint errors.

Change-Id: I9b5efe6f19a6f568aede3ad8c80012a2a7c76ff4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1825893Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarNavid Zolghadr <nzolghadr@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#700039}
parent 0d8deb26
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <cmath> #include <cmath>
#include "base/android/jni_android.h" #include "base/android/jni_android.h"
#include "base/numerics/math_constants.h"
#include "ui/events/base_event_utils.h" #include "ui/events/base_event_utils.h"
#include "ui/events/event_constants.h" #include "ui/events/event_constants.h"
#include "ui/events/event_utils.h" #include "ui/events/event_utils.h"
...@@ -51,7 +52,7 @@ MotionEventAndroid::Action FromAndroidAction(int android_action) { ...@@ -51,7 +52,7 @@ MotionEventAndroid::Action FromAndroidAction(int android_action) {
ACTION_CASE(BUTTON_RELEASE); ACTION_CASE(BUTTON_RELEASE);
default: default:
NOTREACHED() << "Invalid Android MotionEvent action: " << android_action; NOTREACHED() << "Invalid Android MotionEvent action: " << android_action;
}; }
return MotionEventAndroid::Action::CANCEL; return MotionEventAndroid::Action::CANCEL;
} }
...@@ -70,7 +71,7 @@ int ToAndroidAction(MotionEventAndroid::Action action) { ...@@ -70,7 +71,7 @@ int ToAndroidAction(MotionEventAndroid::Action action) {
ACTION_REVERSE_CASE(BUTTON_RELEASE); ACTION_REVERSE_CASE(BUTTON_RELEASE);
default: default:
NOTREACHED() << "Invalid MotionEvent action: " << action; NOTREACHED() << "Invalid MotionEvent action: " << action;
}; }
return JNI_MotionEvent::ACTION_CANCEL; return JNI_MotionEvent::ACTION_CANCEL;
} }
...@@ -84,7 +85,7 @@ MotionEventAndroid::ToolType FromAndroidToolType(int android_tool_type) { ...@@ -84,7 +85,7 @@ MotionEventAndroid::ToolType FromAndroidToolType(int android_tool_type) {
default: default:
NOTREACHED() << "Invalid Android MotionEvent tool type: " NOTREACHED() << "Invalid Android MotionEvent tool type: "
<< android_tool_type; << android_tool_type;
}; }
return MotionEventAndroid::ToolType::UNKNOWN; return MotionEventAndroid::ToolType::UNKNOWN;
} }
...@@ -97,7 +98,7 @@ int ToAndroidToolType(MotionEventAndroid::ToolType tool_type) { ...@@ -97,7 +98,7 @@ int ToAndroidToolType(MotionEventAndroid::ToolType tool_type) {
TOOL_TYPE_REVERSE_CASE(ERASER); TOOL_TYPE_REVERSE_CASE(ERASER);
default: default:
NOTREACHED() << "Invalid MotionEvent tool type: " << tool_type; NOTREACHED() << "Invalid MotionEvent tool type: " << tool_type;
}; }
return JNI_MotionEvent::TOOL_TYPE_UNKNOWN; return JNI_MotionEvent::TOOL_TYPE_UNKNOWN;
} }
...@@ -194,10 +195,10 @@ void ConvertTiltOrientationToTiltXY(float tilt_rad, ...@@ -194,10 +195,10 @@ void ConvertTiltOrientationToTiltXY(float tilt_rad,
float orientation_rad, float orientation_rad,
float* tilt_x, float* tilt_x,
float* tilt_y) { float* tilt_y) {
float r = sin(tilt_rad); float r = sinf(tilt_rad);
float z = cos(tilt_rad); float z = cosf(tilt_rad);
*tilt_x = atan2(sin(-orientation_rad) * r, z) * 180.f / M_PI; *tilt_x = atan2f(sinf(-orientation_rad) * r, z) * 180.f / base::kPiFloat;
*tilt_y = atan2(cos(-orientation_rad) * r, z) * 180.f / M_PI; *tilt_y = atan2f(cosf(-orientation_rad) * r, z) * 180.f / base::kPiFloat;
} }
} // namespace } // namespace
......
...@@ -4,9 +4,12 @@ ...@@ -4,9 +4,12 @@
#include <android/input.h> #include <android/input.h>
#include <stddef.h> #include <stddef.h>
#include <cmath> #include <cmath>
#include <limits>
#include "base/android/jni_android.h" #include "base/android/jni_android.h"
#include "base/numerics/math_constants.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/android/motion_event_android.h" #include "ui/events/android/motion_event_android.h"
#include "ui/events/event_constants.h" #include "ui/events/event_constants.h"
...@@ -14,37 +17,40 @@ ...@@ -14,37 +17,40 @@
#include "ui/events/test/scoped_event_test_tick_clock.h" #include "ui/events/test/scoped_event_test_tick_clock.h"
namespace ui { namespace ui {
class MotionEvent; class MotionEvent;
namespace { namespace {
const float kPixToDip = 0.5f;
int kAndroidActionButton = 0; constexpr float kPixToDip = 0.5f;
int kAndroidActionDown = AMOTION_EVENT_ACTION_DOWN;
int kAndroidActionPointerDown = AMOTION_EVENT_ACTION_POINTER_DOWN; constexpr int kAndroidActionButton = 0;
int kAndroidAltKeyDown = AMETA_ALT_ON; constexpr int kAndroidActionDown = AMOTION_EVENT_ACTION_DOWN;
constexpr int kAndroidActionPointerDown = AMOTION_EVENT_ACTION_POINTER_DOWN;
constexpr int kAndroidAltKeyDown = AMETA_ALT_ON;
// Corresponds to TOOL_TYPE_FINGER, see // Corresponds to TOOL_TYPE_FINGER, see
// developer.android.com/reference/android/view/MotionEvent.html // developer.android.com/reference/android/view/MotionEvent.html
// #TOOL_TYPE_FINGER. // #TOOL_TYPE_FINGER.
int kAndroidToolTypeFinger = 1; constexpr int kAndroidToolTypeFinger = 1;
// Corresponds to BUTTON_PRIMARY, see // Corresponds to BUTTON_PRIMARY, see
// developer.android.com/reference/android/view/MotionEvent.html#BUTTON_PRIMARY. // developer.android.com/reference/android/view/MotionEvent.html#BUTTON_PRIMARY.
int kAndroidButtonPrimary = 1; constexpr int kAndroidButtonPrimary = 1;
// This function convert tilt_x and tilt_y back to tilt_rad. // This function convert tilt_x and tilt_y back to tilt_rad.
float ConvertToTiltRad(float tilt_x, float tilt_y) { float ConvertToTiltRad(float tilt_x, float tilt_y) {
float tilt_x_r = sin(tilt_x * M_PI / 180.f); float tilt_x_r = sinf(tilt_x * base::kPiFloat / 180.f);
float tilt_x_z = cos(tilt_x * M_PI / 180.f); float tilt_x_z = cosf(tilt_x * base::kPiFloat / 180.f);
float tilt_y_r = sin(tilt_y * M_PI / 180.f); float tilt_y_r = sinf(tilt_y * base::kPiFloat / 180.f);
float tilt_y_z = cos(tilt_y * M_PI / 180.f); float tilt_y_z = cosf(tilt_y * base::kPiFloat / 180.f);
float r_x = tilt_x_r * tilt_y_z; float r_x = tilt_x_r * tilt_y_z;
float r_y = tilt_y_r * tilt_x_z; float r_y = tilt_y_r * tilt_x_z;
float r = sqrt(r_x * r_x + r_y * r_y); float r = sqrtf(r_x * r_x + r_y * r_y);
float z = tilt_x_z * tilt_y_z; float z = tilt_x_z * tilt_y_z;
return atan2(r, z); return atan2f(r, z);
} }
} // namespace } // namespace
constexpr float float_error = 0.0001f; constexpr float float_error = 0.0001f;
...@@ -52,9 +58,9 @@ constexpr float float_error = 0.0001f; ...@@ -52,9 +58,9 @@ constexpr float float_error = 0.0001f;
// we're primarily testing caching behavior, and the code necessary to // we're primarily testing caching behavior, and the code necessary to
// construct a Java-backed MotionEvent itself adds unnecessary complexity. // construct a Java-backed MotionEvent itself adds unnecessary complexity.
TEST(MotionEventAndroidTest, Constructor) { TEST(MotionEventAndroidTest, Constructor) {
int event_time_ms = 5; constexpr int kEventTimeMS = 5;
base::TimeTicks event_time = base::TimeTicks event_time =
base::TimeTicks() + base::TimeDelta::FromMilliseconds(event_time_ms); base::TimeTicks() + base::TimeDelta::FromMilliseconds(kEventTimeMS);
ui::test::ScopedEventTestTickClock clock; ui::test::ScopedEventTestTickClock clock;
clock.SetNowTicks(event_time); clock.SetNowTicks(event_time);
...@@ -68,7 +74,7 @@ TEST(MotionEventAndroidTest, Constructor) { ...@@ -68,7 +74,7 @@ TEST(MotionEventAndroidTest, Constructor) {
int action_index = -1; int action_index = -1;
MotionEventAndroid event( MotionEventAndroid event(
base::android::AttachCurrentThread(), nullptr, kPixToDip, 0.f, 0.f, 0.f, base::android::AttachCurrentThread(), nullptr, kPixToDip, 0.f, 0.f, 0.f,
event_time_ms, kAndroidActionDown, pointer_count, history_size, kEventTimeMS, kAndroidActionDown, pointer_count, history_size,
action_index, kAndroidActionButton, kAndroidButtonPrimary, action_index, kAndroidActionButton, kAndroidButtonPrimary,
kAndroidAltKeyDown, raw_offset, -raw_offset, false, &p0, &p1); kAndroidAltKeyDown, raw_offset, -raw_offset, false, &p0, &p1);
...@@ -121,9 +127,9 @@ TEST(MotionEventAndroidTest, Clone) { ...@@ -121,9 +127,9 @@ TEST(MotionEventAndroidTest, Clone) {
} }
TEST(MotionEventAndroidTest, Cancel) { TEST(MotionEventAndroidTest, Cancel) {
const int event_time_ms = 5; constexpr const int kEventTimeMS = 5;
base::TimeTicks event_time = base::TimeTicks event_time =
base::TimeTicks() + base::TimeDelta::FromMilliseconds(event_time_ms); base::TimeTicks() + base::TimeDelta::FromMilliseconds(kEventTimeMS);
ui::test::ScopedEventTestTickClock clock; ui::test::ScopedEventTestTickClock clock;
clock.SetNowTicks(event_time); clock.SetNowTicks(event_time);
...@@ -131,9 +137,9 @@ TEST(MotionEventAndroidTest, Cancel) { ...@@ -131,9 +137,9 @@ TEST(MotionEventAndroidTest, Cancel) {
MotionEventAndroid::Pointer p0( MotionEventAndroid::Pointer p0(
1, 13.7f, -7.13f, 5.3f, 1.2f, 0.1f, 0.2f, kAndroidToolTypeFinger); 1, 13.7f, -7.13f, 5.3f, 1.2f, 0.1f, 0.2f, kAndroidToolTypeFinger);
MotionEventAndroid event(base::android::AttachCurrentThread(), nullptr, MotionEventAndroid event(base::android::AttachCurrentThread(), nullptr,
kPixToDip, 0, 0, 0, event_time_ms, kPixToDip, 0, 0, 0, kEventTimeMS, kAndroidActionDown,
kAndroidActionDown, pointer_count, 0, 0, 0, 0, 0, 0, pointer_count, 0, 0, 0, 0, 0, 0, 0, false, &p0,
0, false, &p0, nullptr); nullptr);
std::unique_ptr<MotionEvent> cancel_event = event.Cancel(); std::unique_ptr<MotionEvent> cancel_event = event.Cancel();
EXPECT_EQ(MotionEvent::Action::CANCEL, cancel_event->GetAction()); EXPECT_EQ(MotionEvent::Action::CANCEL, cancel_event->GetAction());
...@@ -197,4 +203,4 @@ TEST(MotionEventAndroidTest, ActionIndexForPointerDown) { ...@@ -197,4 +203,4 @@ TEST(MotionEventAndroidTest, ActionIndexForPointerDown) {
EXPECT_EQ(action_index, event.GetActionIndex()); EXPECT_EQ(action_index, event.GetActionIndex());
} }
} // namespace content } // namespace ui
...@@ -2,9 +2,12 @@ ...@@ -2,9 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first.
#include "ui/events/blink/prediction/kalman_predictor.h" #include "ui/events/blink/prediction/kalman_predictor.h"
#include <algorithm>
#include <cmath>
#include "base/numerics/math_constants.h"
#include "ui/events/blink/prediction/predictor_factory.h" #include "ui/events/blink/prediction/predictor_factory.h"
namespace { namespace {
...@@ -95,7 +98,7 @@ bool KalmanPredictor::GeneratePrediction(base::TimeTicks predict_time, ...@@ -95,7 +98,7 @@ bool KalmanPredictor::GeneratePrediction(base::TimeTicks predict_time,
atan2(second_dir.x(), second_dir.y()); atan2(second_dir.x(), second_dir.y());
} }
} }
if (abs(points_angle) * 180 / M_PI > 15) { if (fabsf(points_angle) * 180 / base::kPiDouble > 15) {
position += ScaleVector2d(acceleration, position += ScaleVector2d(acceleration,
kAccelerationInfluence * pred_dt * pred_dt); kAccelerationInfluence * pred_dt * pred_dt);
} }
......
...@@ -2,10 +2,9 @@ ...@@ -2,10 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first.
#include <cmath> #include <cmath>
#include "base/numerics/math_constants.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/quaternion.h" #include "ui/gfx/geometry/quaternion.h"
...@@ -39,7 +38,7 @@ TEST(QuatTest, AxisAngleCommon) { ...@@ -39,7 +38,7 @@ TEST(QuatTest, AxisAngleCommon) {
TEST(QuatTest, VectorToVectorRotation) { TEST(QuatTest, VectorToVectorRotation) {
Quaternion q(Vector3dF(1.0f, 0.0f, 0.0f), Vector3dF(0.0f, 1.0f, 0.0f)); Quaternion q(Vector3dF(1.0f, 0.0f, 0.0f), Vector3dF(0.0f, 1.0f, 0.0f));
Quaternion r(Vector3dF(0.0f, 0.0f, 1.0f), M_PI_2); Quaternion r(Vector3dF(0.0f, 0.0f, 1.0f), base::kPiFloat / 2);
EXPECT_FLOAT_EQ(r.x(), q.x()); EXPECT_FLOAT_EQ(r.x(), q.x());
EXPECT_FLOAT_EQ(r.y(), q.y()); EXPECT_FLOAT_EQ(r.y(), q.y());
...@@ -150,8 +149,8 @@ TEST(QuatTest, Slerp) { ...@@ -150,8 +149,8 @@ TEST(QuatTest, Slerp) {
TEST(QuatTest, SlerpOppositeAngles) { TEST(QuatTest, SlerpOppositeAngles) {
Vector3dF axis(1, 1, 1); Vector3dF axis(1, 1, 1);
double start_radians = -M_PI_2; double start_radians = -base::kPiDouble / 2;
double stop_radians = M_PI_2; double stop_radians = base::kPiDouble / 2;
Quaternion start(axis, start_radians); Quaternion start(axis, start_radians);
Quaternion stop(axis, stop_radians); Quaternion stop(axis, stop_radians);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment