From 40bf82898f525971b43464b6f488f0e2450a10ff Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 6 May 2019 17:01:14 -0700 Subject: [PATCH] Fabric: Applying a transformation matrix to a point Summary: The diff implements a function that applies given transformation to a point. Reviewed By: JoshuaGross Differential Revision: D15219263 fbshipit-source-id: 4cc0595b0dda38c1ede1f2885b800cbe57f54243 --- ReactCommon/fabric/graphics/Transform.cpp | 26 +++++++++++++++++++++++ ReactCommon/fabric/graphics/Transform.h | 12 +++++++++++ 2 files changed, 38 insertions(+) diff --git a/ReactCommon/fabric/graphics/Transform.cpp b/ReactCommon/fabric/graphics/Transform.cpp index bfcb45f25..9848767b3 100644 --- a/ReactCommon/fabric/graphics/Transform.cpp +++ b/ReactCommon/fabric/graphics/Transform.cpp @@ -149,5 +149,31 @@ Transform Transform::operator*(Transform const &rhs) const { return result; } +Float &Transform::at(int i, int j) { + return matrix[(i * 4) + j]; +} + +Float const &Transform::at(int i, int j) const { + return matrix[(i * 4) + j]; +} + +Point operator*(Point const &point, Transform const &transform) { + if (transform == Transform::Identity()) { + return point; + } + + auto result = Point{}; + result.x = transform.at(3, 0) + point.x * transform.at(0, 0) + point.y * transform.at(1, 0); + result.y = transform.at(3, 1) + point.x * transform.at(0, 1) + point.y * transform.at(1, 1); + auto w = transform.at(3, 3) + point.x * transform.at(0, 3) + point.y * transform.at(1, 3); + + if (w != 1 && w != 0) { + result.x /= w; + result.y /= w; + } + + return result; +} + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/graphics/Transform.h b/ReactCommon/fabric/graphics/Transform.h index 75eff7559..71c9c6b58 100644 --- a/ReactCommon/fabric/graphics/Transform.h +++ b/ReactCommon/fabric/graphics/Transform.h @@ -9,6 +9,7 @@ #include #include +#include namespace facebook { namespace react { @@ -59,12 +60,23 @@ struct Transform { bool operator==(Transform const &rhs) const; bool operator!=(Transform const &rhs) const; + /* + * Matrix subscript. + */ + Float &at(int x, int y); + Float const &at(int x, int y) const; + /* * Concatenates (multiplies) transform matrices. */ Transform operator*(Transform const &rhs) const; }; +/* + * Applies tranformation to the given point. + */ +Point operator*(Point const &point, Transform const &transform); + } // namespace react } // namespace facebook