Chromium Code Reviews| Index: ui/gfx/geometry/axis_transform2d.h |
| diff --git a/ui/gfx/geometry/axis_transform2d.h b/ui/gfx/geometry/axis_transform2d.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c67041089958b07441655723a21439987e5b1622 |
| --- /dev/null |
| +++ b/ui/gfx/geometry/axis_transform2d.h |
| @@ -0,0 +1,133 @@ |
| +// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef UI_GFX_GEOMETRY_AXIS_TRANSFORM2D_H_ |
| +#define UI_GFX_GEOMETRY_AXIS_TRANSFORM2D_H_ |
| + |
| +#include "ui/gfx/geometry/rect_f.h" |
| +#include "ui/gfx/geometry/vector2d_f.h" |
| +#include "ui/gfx/gfx_export.h" |
| + |
| +namespace gfx { |
| + |
| +// This class implements the subset of 2D linear transforms that only |
| +// translation and uniform scaling are allowed. |
| +// Internally this is stored as a scalar pre-scale factor, and a vector |
| +// for post-translation. The class constructor and member accessor follows |
| +// the same convention. |
| +class GFX_EXPORT AxisTransform2d { |
| + public: |
| + constexpr AxisTransform2d() = default; |
| + constexpr AxisTransform2d(float scale, const Vector2dF& translation) |
| + : scale_(scale), translation_(translation) {} |
| + |
| + bool operator==(const AxisTransform2d& other) const { |
| + return scale_ == other.scale_ && translation_ == other.translation_; |
| + } |
| + bool operator!=(const AxisTransform2d& other) const { |
| + return !(*this == other); |
| + } |
| + |
| + void PreScale(float scale) { scale_ *= scale; } |
| + void PostScale(float scale) { |
| + scale_ *= scale; |
| + translation_.Scale(scale); |
| + } |
| + void PreTranslate(const Vector2dF& translation) { |
| + translation_ += ScaleVector2d(translation, scale_); |
| + } |
| + void PostTranslate(const Vector2dF& translation) { |
| + translation_ += translation; |
| + } |
| + |
| + void PreConcat(const AxisTransform2d& pre) { |
| + PreTranslate(pre.translation_); |
| + PreScale(pre.scale_); |
| + } |
| + void PostConcat(const AxisTransform2d& post) { |
| + PostScale(post.scale_); |
| + PostTranslate(post.translation_); |
| + } |
| + |
| + void Invert() { |
| + DCHECK(scale_); |
| + scale_ = 1.f / scale_; |
| + translation_.Scale(-scale_); |
| + } |
| + |
| + PointF MapPoint(const PointF& p) const { |
| + return ScalePoint(p, scale_) + translation_; |
| + } |
| + PointF InverseMapPoint(const PointF& p) const { |
| + return ScalePoint(p - translation_, 1.f / scale_); |
| + } |
| + |
| + RectF MapRect(const RectF& r) const { |
| + DCHECK(scale_ >= 0.f); |
| + return ScaleRect(r, scale_) + translation_; |
| + } |
| + RectF InverseMapRect(const RectF& r) const { |
| + DCHECK(scale_ > 0.f); |
| + return ScaleRect(r - translation_, 1.f / scale_); |
| + } |
| + |
| + float scale() const { return scale_; } |
| + Vector2dF translation() const { return translation_; } |
|
danakj
2017/03/31 15:09:49
nit: return const& as this is returning a member
trchen
2017/03/31 20:56:51
Done.
|
| + |
| + std::string ToString() const; |
| + |
| + private: |
| + // Scale is applied before translation, i.e. |
| + // this->Transform(p) == scale_ * p + translation_ |
| + float scale_ = 1.f; |
| + Vector2dF translation_; |
| +}; |
| + |
| +static inline AxisTransform2d PreScaleAxisTransform2d(const AxisTransform2d& t, |
| + float scale) { |
| + AxisTransform2d result(t); |
| + result.PreScale(scale); |
| + return result; |
| +} |
| + |
| +static inline AxisTransform2d PostScaleAxisTransform2d(const AxisTransform2d& t, |
| + float scale) { |
| + AxisTransform2d result(t); |
| + result.PostScale(scale); |
| + return result; |
| +} |
| + |
| +static inline AxisTransform2d PreTranslateAxisTransform2d( |
| + const AxisTransform2d& t, |
| + const Vector2dF& translation) { |
| + AxisTransform2d result(t); |
| + result.PreTranslate(translation); |
| + return result; |
| +} |
| + |
| +static inline AxisTransform2d PostTranslateAxisTransform2d( |
| + const AxisTransform2d& t, |
| + const Vector2dF& translation) { |
| + AxisTransform2d result(t); |
| + result.PostTranslate(translation); |
| + return result; |
| +} |
| + |
| +static inline AxisTransform2d ConcatAxisTransform2d( |
| + const AxisTransform2d& post, |
| + const AxisTransform2d& pre) { |
| + AxisTransform2d result(post); |
| + result.PreConcat(pre); |
| + return result; |
| +} |
| + |
| +static inline AxisTransform2d InverseAxisTransform2d(const AxisTransform2d& t) { |
|
danakj
2017/03/31 15:09:49
Invert
trchen
2017/03/31 20:56:51
Done.
|
| + AxisTransform2d result = t; |
| + result.Invert(); |
| + return result; |
| +} |
| + |
| +} // namespace gfx |
|
danakj
2017/03/31 15:09:49
I was going to ask for PrintTo for this for unit t
trchen
2017/03/31 20:56:51
PrintTo added.
|
| + |
| +#endif // UI_GFX_GEOMETRY_AXIS_TRANSFORM2D_H_ |