0

ui: Add non-member Vector2dScale() and Vector3dScale() methods to create scaled vectors

These behave like similar methods for Rect/Point/Size.

Tests:
ui_unittests:Vector2dTest.Scale
ui_unittests:Vector3dTest.Scale

R=sky
BUG=160158

Review URL: https://codereview.chromium.org/11293193

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@166993 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
danakj@chromium.org
2012-11-09 22:15:30 +00:00
parent d43bf845ce
commit b0c369a9b8
9 changed files with 78 additions and 29 deletions

@ -65,19 +65,6 @@ inline gfx::PointF BottomRight(gfx::RectF rect) {
return gfx::PointF(rect.right(), rect.bottom());
}
// Return a vector that is |v| scaled by the given scale factors along each
// axis.
inline gfx::Vector2dF ScaleVector2d(gfx::Vector2dF v, float x_scale, float y_scale) {
gfx::Vector2dF scaled = v;
scaled.Scale(x_scale, y_scale);
return scaled;
}
// Return a vector that is |v| scaled by the given scale factor.
inline gfx::Vector2dF ScaleVector2d(gfx::Vector2dF v, float scale) {
return ScaleVector2d(v, scale, scale);
}
} // namespace cc
#endif // CC_GEOMETRY_H_

@ -1174,7 +1174,7 @@ TEST_P(LayerTreeHostImplTest, scrollRootAndChangePageScaleOnMainThread)
if (!Settings::pageScalePinchZoomEnabled()) {
// The scale should apply to the scroll delta.
expectedScrollDelta = gfx::ToFlooredVector2d(cc::ScaleVector2d(expectedScrollDelta, pageScale));
expectedScrollDelta = gfx::ToFlooredVector2d(gfx::ScaleVector2d(expectedScrollDelta, pageScale));
}
scoped_ptr<ScrollAndScaleSet> scrollInfo = m_hostImpl->processScrollDeltas();
expectContains(*scrollInfo.get(), m_hostImpl->rootLayer()->id(), expectedScrollDelta);
@ -1297,7 +1297,7 @@ TEST_P(LayerTreeHostImplTest, scrollChildAndChangePageScaleOnMainThread)
if (!Settings::pageScalePinchZoomEnabled()) {
// The scale should apply to the scroll delta.
expectedScrollDelta = gfx::ToFlooredVector2d(cc::ScaleVector2d(expectedScrollDelta, pageScale));
expectedScrollDelta = gfx::ToFlooredVector2d(gfx::ScaleVector2d(expectedScrollDelta, pageScale));
}
scoped_ptr<ScrollAndScaleSet> scrollInfo = m_hostImpl->processScrollDeltas();
expectContains(*scrollInfo.get(), scrollLayerId, expectedScrollDelta);

@ -22,16 +22,12 @@ gfx::PointF toPointF(const gfx::Vector2dF& vector)
// between 0 and 1, representing the percentage position within the viewport.
gfx::Vector2dF normalizeFromViewport(const gfx::Vector2dF& denormalized, const gfx::SizeF& viewportSize)
{
gfx::Vector2dF normalized(denormalized);
normalized.Scale(1 / viewportSize.width(), 1 / viewportSize.height());
return normalized;
return gfx::ScaleVector2d(denormalized, 1 / viewportSize.width(), 1 / viewportSize.height());
}
gfx::Vector2dF denormalizeToViewport(const gfx::Vector2dF& normalized, const gfx::SizeF& viewportSize)
{
gfx::Vector2dF denormalized(normalized);
denormalized.Scale(viewportSize.width(), viewportSize.height());
return denormalized;
return gfx::ScaleVector2d(normalized, viewportSize.width(), viewportSize.height());
}
gfx::Vector2dF interpolateBetween(const gfx::Vector2dF& start, const gfx::Vector2dF end, float interp)
@ -183,8 +179,7 @@ gfx::Vector2dF PageScaleAnimation::scrollOffsetAt(float interp) const
gfx::Vector2dF PageScaleAnimation::anchorAt(float interp) const
{
// Interpolate from start to target anchor in absolute space.
gfx::Vector2dF delta = m_targetAnchor - m_startAnchor;
delta.Scale(interp);
gfx::Vector2dF delta = gfx::ScaleVector2d(m_targetAnchor - m_startAnchor, interp);
return m_startAnchor + delta;
}

@ -61,4 +61,10 @@ double DotProduct(const Vector2dF& lhs, const Vector2dF& rhs) {
static_cast<double>(lhs.y()) * rhs.y();
}
Vector2dF ScaleVector2d(const Vector2dF& v, float x_scale, float y_scale) {
Vector2dF scaled_v(v);
scaled_v.Scale(x_scale, y_scale);
return scaled_v;
}
} // namespace gfx

@ -82,6 +82,17 @@ UI_EXPORT double CrossProduct(const Vector2dF& lhs, const Vector2dF& rhs);
// Return the dot product of two vectors.
UI_EXPORT double DotProduct(const Vector2dF& lhs, const Vector2dF& rhs);
// Return a vector that is |v| scaled by the given scale factors along each
// axis.
UI_EXPORT Vector2dF ScaleVector2d(const Vector2dF& v,
float x_scale,
float y_scale);
// Return a vector that is |v| scaled by the given scale factor.
inline Vector2dF ScaleVector2d(const Vector2dF& v, float scale) {
return ScaleVector2d(v, scale, scale);
}
} // namespace gfx
#endif // UI_GFX_VECTOR2D_F_H_

@ -115,6 +115,12 @@ TEST(Vector2dTest, Scale) {
v.Scale(double_values[i][2], double_values[i][3]);
EXPECT_EQ(v.x(), double_values[i][0] * double_values[i][2]);
EXPECT_EQ(v.y(), double_values[i][1] * double_values[i][3]);
Vector2dF v2 = ScaleVector2d(
gfx::Vector2dF(double_values[i][0], double_values[i][1]),
double_values[i][2], double_values[i][3]);
EXPECT_EQ(double_values[i][0] * double_values[i][2], v2.x());
EXPECT_EQ(double_values[i][1] * double_values[i][3], v2.y());
}
float single_values[][3] = {
@ -134,6 +140,12 @@ TEST(Vector2dTest, Scale) {
v.Scale(single_values[i][2]);
EXPECT_EQ(v.x(), single_values[i][0] * single_values[i][2]);
EXPECT_EQ(v.y(), single_values[i][1] * single_values[i][2]);
Vector2dF v2 = ScaleVector2d(
gfx::Vector2dF(double_values[i][0], double_values[i][1]),
double_values[i][2]);
EXPECT_EQ(single_values[i][0] * single_values[i][2], v2.x());
EXPECT_EQ(single_values[i][1] * single_values[i][2], v2.y());
}
}

@ -76,4 +76,13 @@ float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs) {
return lhs.x() * rhs.x() + lhs.y() * rhs.y() + lhs.z() * rhs.z();
}
Vector3dF ScaleVector3d(const Vector3dF& v,
float x_scale,
float y_scale,
float z_scale) {
Vector3dF scaled_v(v);
scaled_v.Scale(x_scale, y_scale, z_scale);
return scaled_v;
}
} // namespace gfx

@ -95,6 +95,17 @@ inline Vector3dF CrossProduct(const Vector3dF& lhs, const Vector3dF& rhs) {
// Return the dot product of two vectors.
UI_EXPORT float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs);
// Return a vector that is |v| scaled by the given scale factors along each
// axis.
UI_EXPORT Vector3dF ScaleVector3d(const Vector3dF& v,
float x_scale,
float y_scale,
float z_scale);
// Return a vector that is |v| scaled by the given scale factor.
inline Vector3dF ScaleVector3d(const Vector3dF& v, float scale) {
return ScaleVector3d(v, scale, scale, scale);
}
} // namespace gfx

@ -88,9 +88,18 @@ TEST(Vector3dTest, Scale) {
triple_values[i][1],
triple_values[i][2]);
v.Scale(triple_values[i][3], triple_values[i][4], triple_values[i][5]);
EXPECT_EQ(v.x(), triple_values[i][0] * triple_values[i][3]);
EXPECT_EQ(v.y(), triple_values[i][1] * triple_values[i][4]);
EXPECT_EQ(v.z(), triple_values[i][2] * triple_values[i][5]);
EXPECT_EQ(triple_values[i][0] * triple_values[i][3], v.x());
EXPECT_EQ(triple_values[i][1] * triple_values[i][4], v.y());
EXPECT_EQ(triple_values[i][2] * triple_values[i][5], v.z());
Vector3dF v2 = ScaleVector3d(
gfx::Vector3dF(triple_values[i][0],
triple_values[i][1],
triple_values[i][2]),
triple_values[i][3], triple_values[i][4], triple_values[i][5]);
EXPECT_EQ(triple_values[i][0] * triple_values[i][3], v2.x());
EXPECT_EQ(triple_values[i][1] * triple_values[i][4], v2.y());
EXPECT_EQ(triple_values[i][2] * triple_values[i][5], v2.z());
}
float single_values[][4] = {
@ -115,9 +124,18 @@ TEST(Vector3dTest, Scale) {
single_values[i][1],
single_values[i][2]);
v.Scale(single_values[i][3]);
EXPECT_EQ(v.x(), single_values[i][0] * single_values[i][3]);
EXPECT_EQ(v.y(), single_values[i][1] * single_values[i][3]);
EXPECT_EQ(v.z(), single_values[i][2] * single_values[i][3]);
EXPECT_EQ(single_values[i][0] * single_values[i][3], v.x());
EXPECT_EQ(single_values[i][1] * single_values[i][3], v.y());
EXPECT_EQ(single_values[i][2] * single_values[i][3], v.z());
Vector3dF v2 = ScaleVector3d(
gfx::Vector3dF(single_values[i][0],
single_values[i][1],
single_values[i][2]),
single_values[i][3]);
EXPECT_EQ(single_values[i][0] * single_values[i][3], v2.x());
EXPECT_EQ(single_values[i][1] * single_values[i][3], v2.y());
EXPECT_EQ(single_values[i][2] * single_values[i][3], v2.z());
}
}