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
This commit is contained in:
Valentin Shergin
2019-05-06 17:01:14 -07:00
committed by Facebook Github Bot
parent 0e35406e4e
commit 40bf82898f
2 changed files with 38 additions and 0 deletions

View File

@@ -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

View File

@@ -9,6 +9,7 @@
#include <folly/Hash.h>
#include <react/graphics/Float.h>
#include <react/graphics/Geometry.h>
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