tiny-skia-cpp
A C++20 2D rendering library (port of tiny-skia)
Loading...
Searching...
No Matches
Transform.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <optional>
7#include <span>
8
9namespace tiny_skia {
10
11struct Point;
12
17class Transform {
18 public:
19 float sx = 1.0f;
20 float kx = 0.0f;
21 float ky = 0.0f;
22 float sy = 1.0f;
23 float tx = 0.0f;
24 float ty = 0.0f;
25
26 constexpr Transform() = default;
27
28 [[nodiscard]] static constexpr Transform identity() { return Transform{}; }
29
31 [[nodiscard]] static constexpr Transform fromRow(float sx, float ky, float kx, float sy, float tx,
32 float ty) {
33 Transform t;
34 t.sx = sx;
35 t.ky = ky;
36 t.kx = kx;
37 t.sy = sy;
38 t.tx = tx;
39 t.ty = ty;
40 return t;
41 }
42
43 [[nodiscard]] static constexpr Transform fromTranslate(float tx, float ty) {
44 return fromRow(1.0f, 0.0f, 0.0f, 1.0f, tx, ty);
45 }
46
47 [[nodiscard]] static constexpr Transform fromScale(float sx, float sy) {
48 return fromRow(sx, 0.0f, 0.0f, sy, 0.0f, 0.0f);
49 }
50
51 [[nodiscard]] bool isFinite() const;
52 [[nodiscard]] bool isIdentity() const;
53 [[nodiscard]] bool isTranslate() const;
54 [[nodiscard]] bool isScaleTranslate() const;
55 [[nodiscard]] bool hasScale() const;
56 [[nodiscard]] bool hasSkew() const;
57 [[nodiscard]] bool hasTranslate() const;
58
60 [[nodiscard]] std::optional<Transform> invert() const;
61
63 [[nodiscard]] Transform preConcat(const Transform& other) const;
65 [[nodiscard]] Transform postConcat(const Transform& other) const;
66 [[nodiscard]] Transform preScale(float sx, float sy) const;
67 [[nodiscard]] Transform postScale(float sx, float sy) const;
68 [[nodiscard]] Transform preTranslate(float tx, float ty) const;
69 [[nodiscard]] Transform postTranslate(float tx, float ty) const;
70
72 void mapPoints(std::span<Point> points) const;
73
74 constexpr bool operator==(const Transform&) const = default;
75};
76
77} // namespace tiny_skia
2D affine transformation: [sx kx tx; ky sy ty; 0 0 1].
Definition Transform.h:17
Transform postConcat(const Transform &other) const
Returns other * this (this applied first).
std::optional< Transform > invert() const
Returns the inverse, or nullopt if singular.
float kx
Horizontal skew.
Definition Transform.h:20
float ky
Vertical skew.
Definition Transform.h:21
static constexpr Transform fromRow(float sx, float ky, float kx, float sy, float tx, float ty)
Creates from all 6 matrix entries (row-major: sx, ky, kx, sy, tx, ty).
Definition Transform.h:31
float ty
Vertical translation.
Definition Transform.h:24
Transform preConcat(const Transform &other) const
Returns this * other (other applied first).
void mapPoints(std::span< Point > points) const
Transforms an array of points in-place.
float sx
Horizontal scale.
Definition Transform.h:19
float tx
Horizontal translation.
Definition Transform.h:23
float sy
Vertical scale.
Definition Transform.h:22