Further Transform2D
This commit is contained in:
@@ -4,61 +4,52 @@
|
||||
#include <J3ML/LinearAlgebra/Matrix3x3.hpp>
|
||||
|
||||
namespace J3ML::LinearAlgebra {
|
||||
|
||||
/// A class that performs and represents translations, rotations, and scaling operations in two dimensions.
|
||||
class Transform2D {
|
||||
protected:
|
||||
// TODO: Verify column-major order (or transpose it) for compatibility with OpenGL.
|
||||
Matrix3x3 transformation;
|
||||
public:
|
||||
const static Transform2D Identity;
|
||||
const static Transform2D FlipX;
|
||||
const static Transform2D FlipY;
|
||||
|
||||
/// Default constructor initializes to an Identity tansformation.
|
||||
Transform2D()
|
||||
{
|
||||
transformation.Row(0) = {1, 0, 0};
|
||||
transformation.Row(1) = {0, 1, 0};
|
||||
transformation.Row(2) = {0, 0, 1};
|
||||
}
|
||||
/// Default constructor initializes to an Identity transformation.
|
||||
Transform2D();
|
||||
|
||||
Transform2D(float rotation, const Vector2& pos);
|
||||
Transform2D(float px, float py, float sx, float sy, float ox, float oy, float kx, float ky, float rotation)
|
||||
{
|
||||
transformation = Matrix3x3(px, py, rotation, sx, sy, ox, oy, kx, ky);
|
||||
}
|
||||
|
||||
static Transform2D FromScale(float sx, float sy)
|
||||
{
|
||||
Transform2D s;
|
||||
s.transformation[0][0] = sx;
|
||||
s.transformation[1][1] = sy;
|
||||
return s;
|
||||
}
|
||||
static Transform2D FromScale(const Vector2& scale)
|
||||
{
|
||||
return FromScale(scale.x, scale.y);
|
||||
}
|
||||
Transform2D(const Vector2& pos, const Vector2& scale, const Vector2& origin, const Vector2& skew, float rotation);
|
||||
Transform2D(const Matrix3x3& transform);
|
||||
explicit Transform2D(const Matrix3x3& transform);
|
||||
|
||||
static Transform2D FromScale(float sx, float sy);
|
||||
static Transform2D FromScale(const Vector2& scale);
|
||||
static Transform2D FromRotation(float radians);
|
||||
static Transform2D FromTranslation(const Vector2& translation);
|
||||
static Transform2D FromTranslation(float tx, float ty);
|
||||
|
||||
/// Returns a Transform2D
|
||||
Transform2D Translate(const Vector2& offset) const;
|
||||
Transform2D Translate(float x, float y) const;
|
||||
Transform2D Scale(float scale); // Perform Uniform Scale
|
||||
Transform2D Scale(float x, float y); // Perform Nonunform Scale
|
||||
Transform2D Scale(const Vector2& scales); // Perform Nonuniform Scale
|
||||
Transform2D Rotate();
|
||||
Vector2 Transform(const Vector2& point) const
|
||||
{
|
||||
Vector2 result;
|
||||
result.x = transformation.At(0,0) * point.x + transformation.At(0,1) * point.y + transformation.At(0,2);
|
||||
result.y = transformation.At(1,0) * point.x + transformation.At(1,1) * point.y + transformation.At(1,2);
|
||||
return result;
|
||||
}
|
||||
Transform2D Scale(float scale);
|
||||
Transform2D Scale(float x, float y);
|
||||
Transform2D Scale(const Vector2& scales);
|
||||
Transform2D Rotate(float radians);
|
||||
|
||||
/// Transforms a given 2D point by this transformation.
|
||||
Vector2 Transform(const Vector2& point) const;
|
||||
Transform2D Inverse() const;
|
||||
Transform2D AffineInverse() const;
|
||||
|
||||
Vector2 ForwardVector() const;
|
||||
Vector2 UpVector() const;
|
||||
|
||||
float Determinant() const;
|
||||
Vector2 GetOrigin() const;
|
||||
float& At(int row, int col);
|
||||
[[nodiscard]] float At(int row, int col) const;
|
||||
|
||||
Vector2 GetTranslation() const;
|
||||
float GetRotation() const;
|
||||
Vector2 GetScale() const;
|
||||
float GetSkew() const;
|
||||
Transform2D OrthoNormalize();
|
||||
};
|
||||
}
|
||||
|
@@ -27,4 +27,71 @@ namespace J3ML::LinearAlgebra {
|
||||
Transform2D Transform2D::Translate(const LinearAlgebra::Vector2 &input) const {
|
||||
return Translate(input.x, input.y);
|
||||
}
|
||||
|
||||
Transform2D::Transform2D() {
|
||||
transformation = Matrix3x3::Identity;
|
||||
}
|
||||
|
||||
Transform2D Transform2D::FromRotation(float radians) {
|
||||
float c = Math::Cos(radians);
|
||||
float s = Math::Sin(radians);
|
||||
return Transform2D(Matrix3x3(
|
||||
c, s, 0.f,
|
||||
-s, c, 0.f,
|
||||
0.f, 0.f, 1.f));
|
||||
}
|
||||
|
||||
Transform2D Transform2D::FromScale(const Vector2 &scale) {
|
||||
return FromScale(scale.x, scale.y);
|
||||
}
|
||||
|
||||
Transform2D Transform2D::FromScale(float sx, float sy) {
|
||||
Transform2D s;
|
||||
s.transformation[0][0] = sx;
|
||||
s.transformation[1][1] = sy;
|
||||
return s;
|
||||
}
|
||||
|
||||
Transform2D Transform2D::FromTranslation(const Vector2 &translation) {
|
||||
return FromTranslation(translation.x, translation.y);
|
||||
}
|
||||
|
||||
Transform2D Transform2D::FromTranslation(float tx, float ty) {
|
||||
return Transform2D(Matrix3x3(
|
||||
1.f, 0.f, 0.f,
|
||||
0.f, 1.f, 0.f,
|
||||
tx, ty, 1.f));
|
||||
}
|
||||
|
||||
Vector2 Transform2D::GetScale() const {
|
||||
return {
|
||||
Math::Sqrt(At(0,0) * At(0,0) + At(0,1) * At(0,1)),
|
||||
Math::Sqrt(At(1,0) * At(1,0) + At(1,1) * At(1,1))
|
||||
};
|
||||
}
|
||||
|
||||
float Transform2D::GetRotation() const {
|
||||
return Math::Atan2(At(1, 0), At(0, 0));
|
||||
}
|
||||
|
||||
Vector2 Transform2D::GetTranslation() const {
|
||||
return {At(2,0), At(2, 1)};
|
||||
}
|
||||
|
||||
float &Transform2D::At(int row, int col) { return transformation.At(row, col); }
|
||||
|
||||
float Transform2D::At(int row, int col) const { return transformation.At(row, col); }
|
||||
|
||||
float Transform2D::Determinant() const { return transformation.Determinant(); }
|
||||
|
||||
Vector2 Transform2D::Transform(const Vector2 &point) const {
|
||||
Vector2 result;
|
||||
result.x = At(0,0) * point.x + At(0,1) * point.y + At(0,2);
|
||||
result.y = At(1,0) * point.x + At(1,1) * point.y + At(1,2);
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector2 Transform2D::ForwardVector() const { return {At(0,0), At(1,0)}; }
|
||||
|
||||
Vector2 Transform2D::UpVector() const { return {At(0,1), At(1,1)}; }
|
||||
}
|
Reference in New Issue
Block a user