5 #include "gtest/gtest.h"
15 TEST(MatrixTest, Multiply) {
25 11.0, 21.0, 0.0, 1.0)));
31 EXPECT_TRUE(
x.Equals(y));
34 TEST(MatrixTest, NotEquals) {
36 Matrix y =
x.Translate({1, 0, 0});
37 EXPECT_FALSE(
x.Equals(y));
40 TEST(MatrixTest, HasPerspective2D) {
41 EXPECT_FALSE(
Matrix().HasPerspective2D());
43 auto test = [](
int index,
bool expect) {
46 matrix.
m[index] = 0.5f;
51 test( 0,
false); test( 1,
false); test( 2,
false); test( 3,
true);
52 test( 4,
false); test( 5,
false); test( 6,
false); test( 7,
true);
53 test( 8,
false); test( 9,
false); test(10,
false); test(11,
false);
54 test(12,
false); test(13,
false); test(14,
false); test(15,
true);
58 TEST(MatrixTest, HasPerspective) {
59 EXPECT_FALSE(
Matrix().HasPerspective());
61 auto test = [](
int index,
bool expect) {
64 matrix.
m[index] = 0.5f;
69 test( 0,
false); test( 1,
false); test( 2,
false); test( 3,
true);
70 test( 4,
false); test( 5,
false); test( 6,
false); test( 7,
true);
71 test( 8,
false); test( 9,
false); test(10,
false); test(11,
true);
72 test(12,
false); test(13,
false); test(14,
false); test(15,
true);
76 TEST(MatrixTest, HasTranslation) {
80 EXPECT_FALSE(
Matrix().HasTranslation());
83 TEST(MatrixTest, IsTranslationOnly) {
90 EXPECT_TRUE(
Matrix().IsTranslationOnly());
91 EXPECT_TRUE(
Matrix().IsTranslationScaleOnly());
94 TEST(MatrixTest, IsTranslationScaleOnly) {
101 EXPECT_TRUE(
Matrix().IsTranslationOnly());
102 EXPECT_TRUE(
Matrix().IsTranslationScaleOnly());
105 TEST(MatrixTest, IsInvertibleGetDeterminant) {
106 EXPECT_TRUE(
Matrix().IsInvertible());
107 EXPECT_NE(
Matrix().GetDeterminant(), 0.0f);
133 EXPECT_TRUE(
Matrix().IsFinite());
146 for (
int i = 0; i < 16; i++) {
150 matrix.
m[i] = std::numeric_limits<Scalar>::infinity();
157 matrix.
m[i] = -std::numeric_limits<Scalar>::infinity();
164 matrix.
m[i] = -std::numeric_limits<Scalar>::quiet_NaN();
170 TEST(MatrixTest, IsAligned2D) {
171 EXPECT_TRUE(
Matrix().IsAligned2D());
174 auto test = [](
int index,
bool expect) {
177 matrix.
m[index] = 0.5f;
178 EXPECT_EQ(matrix.
IsAligned2D(), expect) <<
"index: " << index;
182 test( 0,
true); test( 1,
false); test( 2,
true); test( 3,
false);
183 test( 4,
false); test( 5,
true); test( 6,
true); test( 7,
false);
184 test( 8,
true); test( 9,
true); test(10,
true); test(11,
true);
185 test(12,
true); test(13,
true); test(14,
true); test(15,
false);
189 for (
int i = -1000; i < 1000; i++) {
196 for (
int i = -1000; i < 1000; i++) {
204 EXPECT_TRUE(
Matrix().IsAligned());
217 ASSERT_FALSE(result);
221 auto test = [](
int index,
bool expect) {
224 matrix.
m[index] = 0.5f;
225 EXPECT_EQ(matrix.
IsAligned(), expect) <<
"index: " << index;
229 test( 0,
true); test( 1,
false); test( 2,
false); test( 3,
false);
230 test( 4,
false); test( 5,
true); test( 6,
false); test( 7,
false);
231 test( 8,
false); test( 9,
false); test(10,
true); test(11,
false);
232 test(12,
true); test(13,
true); test(14,
true); test(15,
false);
236 for (
int i = -1000; i < 1000; i++) {
243 for (
int i = -1000; i < 1000; i++) {
250 TEST(MatrixTest, TransformHomogenous) {
253 2.0f, 3.0f, 5.0f, 7.0f,
254 11.0f, 13.0f, 17.0f, 19.0f,
255 23.0f, 29.0f, 31.0f, 37.0f,
256 41.0f, 43.0f, 47.0f, 53.0f
263 TEST(MatrixTest, GetMaxBasisXYNegativeScale) {
274 TEST(MatrixTest, GetMaxBasisXYWithLargeAndSmallScalingFactor) {
282 TEST(MatrixTest, GetMaxBasisXYWithLargeAndSmallScalingFactorNonScaleTranslate) {
289 TEST(MatrixTest, TranslateWithPerspective) {
293 0.0, 2.0, 0.0, 30.0);
296 0.0, 1.0, 0.0, 220.0,
298 0.0, 2.0, 0.0, 430.0)));
301 TEST(MatrixTest, MakeScaleTranslate) {
319 6.0, 5.0, 111.0, 7.0,
325 TEST(MatrixTest, MinMaxScales2D) {
331 EXPECT_TRUE(pair.has_value())
332 <<
"Scales: " << scale1 <<
", " << scale2 <<
", " << matrix;
334 EXPECT_FLOAT_EQ(pair->first, scale1) << matrix;
335 EXPECT_FLOAT_EQ(pair->second, scale2) << matrix;
337 EXPECT_FLOAT_EQ(pair->first, scale2) << matrix;
338 EXPECT_FLOAT_EQ(pair->second, scale1) << matrix;
342 for (
int i = 1; i < 10; i++) {
344 for (
int j = 1; j < 10; j++) {
346 Scalar minScale = std::min(xScale, yScale);
347 Scalar maxScale = std::max(xScale, yScale);
354 EXPECT_FLOAT_EQ(matrix.
GetMinScale2D().value_or(-1.0f), minScale);
355 EXPECT_FLOAT_EQ(matrix.
GetMaxScale2D().value_or(-1.0f), maxScale);
356 check_pair(matrix, xScale, yScale);
364 EXPECT_FLOAT_EQ(matrix.
GetMinScale2D().value_or(-1.0f), minScale);
365 EXPECT_FLOAT_EQ(matrix.
GetMaxScale2D().value_or(-1.0f), maxScale);
366 check_pair(matrix, xScale, yScale);
372 {10.0f, 15.0f, 2.0f});
375 EXPECT_FLOAT_EQ(matrix.
GetMinScale2D().value_or(-1.0f), minScale);
376 EXPECT_FLOAT_EQ(matrix.
GetMaxScale2D().value_or(-1.0f), maxScale);
377 check_pair(matrix, xScale, yScale);
380 for (
int d = 45; d < 360; d += 45) {
387 EXPECT_FLOAT_EQ(matrix.
GetMinScale2D().value_or(-1.0f), minScale);
388 EXPECT_FLOAT_EQ(matrix.
GetMaxScale2D().value_or(-1.0f), maxScale);
389 check_pair(matrix, xScale, yScale);
398 EXPECT_FLOAT_EQ(matrix.
GetMinScale2D().value_or(-1.0f), minScale);
399 EXPECT_FLOAT_EQ(matrix.
GetMaxScale2D().value_or(-1.0f), maxScale);
400 check_pair(matrix, xScale, yScale);
428 EXPECT_FLOAT_EQ(matrix.
GetMinScale2D().value_or(-1.0f), minScale);
429 EXPECT_FLOAT_EQ(matrix.
GetMaxScale2D().value_or(-1.0f), maxScale);
430 check_pair(matrix, xScale, yScale);
inline ::testing::AssertionResult MatrixNear(impeller::Matrix a, impeller::Matrix b)
TEST(AllocationSizeTest, CanCreateTypedAllocations)
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
A 4x4 matrix using column-major storage.
static constexpr Matrix MakeTranslation(const Vector3 &t)
constexpr Matrix Translate(const Vector3 &t) const
std::optional< Scalar > GetMinScale2D() const
Return the smaller of the two non-negative scales that will be applied to 2D coordinates by this matr...
constexpr bool IsAligned(Scalar tolerance=0) const
static constexpr Matrix MakeColumn(Scalar m0, Scalar m1, Scalar m2, Scalar m3, Scalar m4, Scalar m5, Scalar m6, Scalar m7, Scalar m8, Scalar m9, Scalar m10, Scalar m11, Scalar m12, Scalar m13, Scalar m14, Scalar m15)
static Matrix MakeRotationY(Radians r)
constexpr bool HasPerspective2D() const
static constexpr Matrix MakeRow(Scalar m0, Scalar m1, Scalar m2, Scalar m3, Scalar m4, Scalar m5, Scalar m6, Scalar m7, Scalar m8, Scalar m9, Scalar m10, Scalar m11, Scalar m12, Scalar m13, Scalar m14, Scalar m15)
constexpr Vector3 TransformHomogenous(const Point &v) const
static constexpr Matrix MakeTranslateScale(const Vector3 &s, const Vector3 &t)
static Matrix MakeRotationZ(Radians r)
std::optional< std::pair< Scalar, Scalar > > GetScales2D() const
Compute the two non-negative scales applied by this matrix to 2D coordinates and return them as an op...
std::optional< Scalar > GetMaxScale2D() const
Return the smaller of the two non-negative scales that will be applied to 2D coordinates by this matr...
constexpr bool HasPerspective() const
static constexpr Matrix MakeScale(const Vector3 &s)
constexpr bool IsAligned2D(Scalar tolerance=0) const
Scalar GetMaxBasisLengthXY() const
Return the maximum scale applied specifically to either the X axis or Y axis unit vectors (the bases)...
static Matrix MakeRotationX(Radians r)