Port Geometry tests.
This commit is contained in:
@@ -34,7 +34,10 @@ impeller_component("geometry") {
|
||||
|
||||
impeller_component("geometry_unittests") {
|
||||
testonly = true
|
||||
sources = [ "geometry_unittests.cc" ]
|
||||
sources = [
|
||||
"geometry_unittests.cc",
|
||||
"geometry_unittests.h",
|
||||
]
|
||||
deps = [
|
||||
":geometry",
|
||||
"//flutter/testing",
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "impeller/geometry/geometry_unittests.h"
|
||||
#include "flutter/testing/testing.h"
|
||||
#include "impeller/geometry/path.h"
|
||||
#include "impeller/geometry/path_builder.h"
|
||||
#include "impeller/geometry/point.h"
|
||||
#include "impeller/geometry/rect.h"
|
||||
#include "impeller/geometry/size.h"
|
||||
@@ -10,6 +13,222 @@
|
||||
namespace impeller {
|
||||
namespace testing {
|
||||
|
||||
TEST(GeometryTest, RotationMatrix) {
|
||||
auto rotation = Matrix::MakeRotationZ(M_PI_4);
|
||||
auto expect = Matrix{0.707, 0.707, 0, 0, //
|
||||
-0.707, 0.707, 0, 0, //
|
||||
0, 0, 1, 0, //
|
||||
0, 0, 0, 1};
|
||||
ASSERT_MATRIX_NEAR(rotation, expect);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, InvertMultMatrix) {
|
||||
auto rotation = Matrix::MakeRotationZ(M_PI_4);
|
||||
auto invert = rotation.Invert();
|
||||
auto expect = Matrix{0.707, -0.707, 0, 0, //
|
||||
0.707, 0.707, 0, 0, //
|
||||
0, 0, 1, 0, //
|
||||
0, 0, 0, 1};
|
||||
ASSERT_MATRIX_NEAR(invert, expect);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, MutliplicationMatrix) {
|
||||
auto rotation = Matrix::MakeRotationZ(M_PI_4);
|
||||
auto invert = rotation.Invert();
|
||||
ASSERT_MATRIX_NEAR(rotation * invert, Matrix{});
|
||||
}
|
||||
|
||||
TEST(GeometryTest, DeterminantTest) {
|
||||
auto matrix = Matrix{3, 4, 14, 155, 2, 1, 3, 4, 2, 3, 2, 1, 1, 2, 4, 2};
|
||||
ASSERT_EQ(matrix.GetDeterminant(), -1889);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, InvertMatrix) {
|
||||
auto inverted = Matrix{10, -9, -12, 8, //
|
||||
7, -12, 11, 22, //
|
||||
-10, 10, 3, 6, //
|
||||
-2, 22, 2, 1}
|
||||
.Invert();
|
||||
|
||||
auto result = Matrix{
|
||||
438.0 / 85123.0, 1751.0 / 85123.0, -7783.0 / 85123.0, 4672.0 / 85123.0,
|
||||
393.0 / 85123.0, -178.0 / 85123.0, -570.0 / 85123.0, 4192 / 85123.0,
|
||||
-5230.0 / 85123.0, 2802.0 / 85123.0, -3461.0 / 85123.0, 962.0 / 85123.0,
|
||||
2690.0 / 85123.0, 1814.0 / 85123.0, 3896.0 / 85123.0, 319.0 / 85123.0};
|
||||
|
||||
ASSERT_MATRIX_NEAR(inverted, result);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, TestDecomposition) {
|
||||
auto rotated = Matrix::MakeRotationZ(M_PI_4);
|
||||
|
||||
auto result = rotated.Decompose();
|
||||
|
||||
ASSERT_TRUE(result.first);
|
||||
|
||||
Matrix::Decomposition res = result.second;
|
||||
|
||||
auto quaternion = Quaternion{{0.0, 0.0, 1.0}, M_PI_4};
|
||||
ASSERT_QUATERNION_NEAR(res.rotation, quaternion);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, TestDecomposition2) {
|
||||
auto rotated = Matrix::MakeRotationZ(M_PI_4);
|
||||
auto scaled = Matrix::MakeScale({2.0, 3.0, 1.0});
|
||||
auto translated = Matrix::MakeTranslation({-200, 750, 20});
|
||||
|
||||
auto result = (translated * rotated * scaled).Decompose();
|
||||
|
||||
ASSERT_TRUE(result.first);
|
||||
|
||||
Matrix::Decomposition res = result.second;
|
||||
|
||||
auto quaternion = Quaternion{{0.0, 0.0, 1.0}, M_PI_4};
|
||||
|
||||
ASSERT_QUATERNION_NEAR(res.rotation, quaternion);
|
||||
|
||||
ASSERT_FLOAT_EQ(res.translation.x, -200);
|
||||
ASSERT_FLOAT_EQ(res.translation.y, 750);
|
||||
ASSERT_FLOAT_EQ(res.translation.z, 20);
|
||||
|
||||
ASSERT_FLOAT_EQ(res.scale.x, 2);
|
||||
ASSERT_FLOAT_EQ(res.scale.y, 3);
|
||||
ASSERT_FLOAT_EQ(res.scale.z, 1);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, TestRecomposition) {
|
||||
/*
|
||||
* Decomposition.
|
||||
*/
|
||||
auto rotated = Matrix::MakeRotationZ(M_PI_4);
|
||||
|
||||
auto result = rotated.Decompose();
|
||||
|
||||
ASSERT_TRUE(result.first);
|
||||
|
||||
Matrix::Decomposition res = result.second;
|
||||
|
||||
auto quaternion = Quaternion{{0.0, 0.0, 1.0}, M_PI_4};
|
||||
|
||||
ASSERT_QUATERNION_NEAR(res.rotation, quaternion);
|
||||
|
||||
/*
|
||||
* Recomposition.
|
||||
*/
|
||||
ASSERT_MATRIX_NEAR(rotated, Matrix{res});
|
||||
}
|
||||
|
||||
TEST(GeometryTest, TestRecomposition2) {
|
||||
auto matrix = Matrix::MakeTranslation({100, 100, 100}) *
|
||||
Matrix::MakeRotationZ(M_PI_4) *
|
||||
Matrix::MakeScale({2.0, 2.0, 2.0});
|
||||
|
||||
auto result = matrix.Decompose();
|
||||
|
||||
ASSERT_TRUE(result.first);
|
||||
|
||||
ASSERT_MATRIX_NEAR(matrix, Matrix{result.second});
|
||||
}
|
||||
|
||||
TEST(GeometryTest, QuaternionLerp) {
|
||||
auto q1 = Quaternion{{0.0, 0.0, 1.0}, 0.0};
|
||||
auto q2 = Quaternion{{0.0, 0.0, 1.0}, M_PI_4};
|
||||
|
||||
auto q3 = q1.Slerp(q2, 0.5);
|
||||
|
||||
auto expected = Quaternion{{0.0, 0.0, 1.0}, M_PI_4 / 2.0};
|
||||
|
||||
ASSERT_QUATERNION_NEAR(q3, expected);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, RectWithPoint) {
|
||||
auto rect = Rect{};
|
||||
|
||||
auto expected = Rect{};
|
||||
|
||||
ASSERT_RECT_NEAR(rect, expected);
|
||||
|
||||
rect = rect.WithPoint({100, 100});
|
||||
expected = Rect{0, 0, 100, 100};
|
||||
ASSERT_RECT_NEAR(rect, expected);
|
||||
|
||||
rect = rect.WithPoint({-11, -12});
|
||||
expected = Rect{-11, -12, 111, 112};
|
||||
ASSERT_RECT_NEAR(rect, expected);
|
||||
|
||||
rect = rect.WithPoint({55, 65});
|
||||
expected = Rect{-11, -12, 111, 112};
|
||||
ASSERT_RECT_NEAR(rect, expected);
|
||||
|
||||
rect = rect.WithPoint({-25, 0});
|
||||
expected = Rect{-25, -12, 125, 112};
|
||||
ASSERT_RECT_NEAR(rect, expected);
|
||||
|
||||
rect = rect.WithPoint({0, -25});
|
||||
expected = Rect{-25, -25, 125, 125};
|
||||
ASSERT_RECT_NEAR(rect, expected);
|
||||
|
||||
rect = rect.WithPoint({125, 135});
|
||||
expected = Rect{-25, -25, 150, 160};
|
||||
ASSERT_RECT_NEAR(rect, expected);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, SimplePath) {
|
||||
Path path;
|
||||
|
||||
path.AddLinearComponent({0, 0}, {100, 100})
|
||||
.AddQuadraticComponent({100, 100}, {200, 200}, {300, 300})
|
||||
.AddCubicComponent({300, 300}, {400, 400}, {500, 500}, {600, 600});
|
||||
|
||||
ASSERT_EQ(path.GetComponentCount(), 3u);
|
||||
|
||||
path.EnumerateComponents(
|
||||
[](size_t index, const LinearPathComponent& linear) {
|
||||
Point p1(0, 0);
|
||||
Point p2(100, 100);
|
||||
ASSERT_EQ(index, 0u);
|
||||
ASSERT_EQ(linear.p1, p1);
|
||||
ASSERT_EQ(linear.p2, p2);
|
||||
},
|
||||
[](size_t index, const QuadraticPathComponent& quad) {
|
||||
Point p1(100, 100);
|
||||
Point cp(200, 200);
|
||||
Point p2(300, 300);
|
||||
ASSERT_EQ(index, 1u);
|
||||
ASSERT_EQ(quad.p1, p1);
|
||||
ASSERT_EQ(quad.cp, cp);
|
||||
ASSERT_EQ(quad.p2, p2);
|
||||
},
|
||||
[](size_t index, const CubicPathComponent& cubic) {
|
||||
Point p1(300, 300);
|
||||
Point cp1(400, 400);
|
||||
Point cp2(500, 500);
|
||||
Point p2(600, 600);
|
||||
ASSERT_EQ(index, 2u);
|
||||
ASSERT_EQ(cubic.p1, p1);
|
||||
ASSERT_EQ(cubic.cp1, cp1);
|
||||
ASSERT_EQ(cubic.cp2, cp2);
|
||||
ASSERT_EQ(cubic.p2, p2);
|
||||
});
|
||||
}
|
||||
|
||||
TEST(GeometryTest, BoundingBoxCubic) {
|
||||
Path path;
|
||||
path.AddCubicComponent({120, 160}, {25, 200}, {220, 260}, {220, 40});
|
||||
auto box = path.GetBoundingBox();
|
||||
Rect expected(0, 0, 220, 198.862);
|
||||
ASSERT_RECT_NEAR(box, expected);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, BoundingBoxOfCompositePathIsCorrect) {
|
||||
PathBuilder builder;
|
||||
builder.AddRoundedRect({{10, 10}, {300, 300}}, {50, 50, 50, 50});
|
||||
auto path = builder.CreatePath();
|
||||
auto actual = path.GetBoundingBox();
|
||||
Rect expected(0, 0, 310, 310);
|
||||
ASSERT_RECT_NEAR(actual, expected);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, CanGenerateMipCounts) {
|
||||
ASSERT_EQ((Size{128, 128}.MipCount()), 7u);
|
||||
ASSERT_EQ((Size{128, 256}.MipCount()), 8u);
|
||||
|
||||
123
engine/src/flutter/impeller/geometry/geometry_unittests.h
Normal file
123
engine/src/flutter/impeller/geometry/geometry_unittests.h
Normal file
@@ -0,0 +1,123 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "impeller/geometry/matrix.h"
|
||||
#include "impeller/geometry/point.h"
|
||||
#include "impeller/geometry/rect.h"
|
||||
#include "impeller/geometry/size.h"
|
||||
#include "impeller/geometry/vector.h"
|
||||
|
||||
namespace std {
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out, const impeller::Matrix& m) {
|
||||
out << "(";
|
||||
for (size_t i = 0; i < 4u; i++) {
|
||||
for (size_t j = 0; j < 4u; j++) {
|
||||
out << m.e[i][j] << ",";
|
||||
}
|
||||
out << std::endl;
|
||||
}
|
||||
out << ")";
|
||||
return out;
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out,
|
||||
const impeller::Quaternion& q) {
|
||||
out << "(" << q.x << ", " << q.y << ", " << q.z << ", " << q.w << ")";
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline std::ostream& operator<<(std::ostream& out,
|
||||
const impeller::TSize<T>& s) {
|
||||
out << "(" << s.width << ", " << s.height << ")";
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline std::ostream& operator<<(std::ostream& out,
|
||||
const impeller::TPoint<T>& p) {
|
||||
out << "(" << p.x << ", " << p.y << ")";
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline std::ostream& operator<<(std::ostream& out,
|
||||
const impeller::TRect<T>& r) {
|
||||
out << "(" << r.origin << ", " << r.size << ")";
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace std
|
||||
|
||||
inline bool NumberNear(double a, double b) {
|
||||
static const double epsilon = 1e-3;
|
||||
return (a > (b - epsilon)) && (a < (b + epsilon));
|
||||
}
|
||||
|
||||
inline ::testing::AssertionResult MatrixNear(impeller::Matrix a,
|
||||
impeller::Matrix b) {
|
||||
auto equal = NumberNear(a.m[0], b.m[0]) //
|
||||
&& NumberNear(a.m[1], b.m[1]) //
|
||||
&& NumberNear(a.m[2], b.m[2]) //
|
||||
&& NumberNear(a.m[3], b.m[3]) //
|
||||
&& NumberNear(a.m[4], b.m[4]) //
|
||||
&& NumberNear(a.m[5], b.m[5]) //
|
||||
&& NumberNear(a.m[6], b.m[6]) //
|
||||
&& NumberNear(a.m[7], b.m[7]) //
|
||||
&& NumberNear(a.m[8], b.m[8]) //
|
||||
&& NumberNear(a.m[9], b.m[9]) //
|
||||
&& NumberNear(a.m[10], b.m[10]) //
|
||||
&& NumberNear(a.m[11], b.m[11]) //
|
||||
&& NumberNear(a.m[12], b.m[12]) //
|
||||
&& NumberNear(a.m[13], b.m[13]) //
|
||||
&& NumberNear(a.m[14], b.m[14]) //
|
||||
&& NumberNear(a.m[15], b.m[15]);
|
||||
|
||||
return equal ? ::testing::AssertionSuccess()
|
||||
: ::testing::AssertionFailure() << "Matrixes are not equal.";
|
||||
}
|
||||
|
||||
inline ::testing::AssertionResult QuaternionNear(impeller::Quaternion a,
|
||||
impeller::Quaternion b) {
|
||||
auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y) &&
|
||||
NumberNear(a.z, b.z) && NumberNear(a.w, b.w);
|
||||
|
||||
return equal ? ::testing::AssertionSuccess()
|
||||
: ::testing::AssertionFailure() << "Quaternions are not equal.";
|
||||
}
|
||||
|
||||
inline ::testing::AssertionResult RectNear(impeller::Rect a, impeller::Rect b) {
|
||||
auto equal = NumberNear(a.origin.x, b.origin.x) &&
|
||||
NumberNear(a.origin.y, b.origin.y) &&
|
||||
NumberNear(a.size.width, b.size.width) &&
|
||||
NumberNear(a.size.height, b.size.height);
|
||||
|
||||
return equal ? ::testing::AssertionSuccess()
|
||||
: ::testing::AssertionFailure() << "Rects are not equal.";
|
||||
}
|
||||
|
||||
inline ::testing::AssertionResult PointNear(impeller::Point a,
|
||||
impeller::Point b) {
|
||||
auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y);
|
||||
|
||||
return equal ? ::testing::AssertionSuccess()
|
||||
: ::testing::AssertionFailure() << "Points are not equal.";
|
||||
}
|
||||
|
||||
inline ::testing::AssertionResult SizeNear(impeller::Size a, impeller::Size b) {
|
||||
auto equal = NumberNear(a.width, b.width) && NumberNear(a.height, b.height);
|
||||
|
||||
return equal ? ::testing::AssertionSuccess()
|
||||
: ::testing::AssertionFailure() << "Sizes are not equal.";
|
||||
}
|
||||
|
||||
#define ASSERT_MATRIX_NEAR(a, b) ASSERT_PRED2(&::MatrixNear, a, b)
|
||||
#define ASSERT_QUATERNION_NEAR(a, b) ASSERT_PRED2(&::QuaternionNear, a, b)
|
||||
#define ASSERT_RECT_NEAR(a, b) ASSERT_PRED2(&::RectNear, a, b)
|
||||
#define ASSERT_POINT_NEAR(a, b) ASSERT_PRED2(&::PointNear, a, b)
|
||||
#define ASSERT_SIZE_NEAR(a, b) ASSERT_PRED2(&::SizeNear, a, b)
|
||||
@@ -8,16 +8,15 @@ uniform FrameInfo {
|
||||
vec2 window_size;
|
||||
} frame;
|
||||
|
||||
in vec4 color;
|
||||
in vec2 interporlated_texture_coordinates;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
uniform sampler2D contents1_texture;
|
||||
uniform sampler2D contents2_texture;
|
||||
uniform sampler2D contents1;
|
||||
uniform sampler2D contents2;
|
||||
|
||||
void main() {
|
||||
vec4 tex1 = texture(contents1_texture, interporlated_texture_coordinates);
|
||||
vec4 tex2 = texture(contents2_texture, interporlated_texture_coordinates);
|
||||
vec4 tex1 = texture(contents1, interporlated_texture_coordinates);
|
||||
vec4 tex2 = texture(contents2, interporlated_texture_coordinates);
|
||||
frag_color = mix(tex1, tex2, clamp(frame.cursor_position.x / frame.window_size.x, 0.0, 1.0));
|
||||
}
|
||||
|
||||
@@ -7,14 +7,11 @@ uniform UniformBuffer {
|
||||
} uniforms;
|
||||
|
||||
in vec3 vertex_position;
|
||||
in vec4 vertex_color;
|
||||
in vec2 texture_coordinates;
|
||||
|
||||
out vec4 color;
|
||||
out vec2 interporlated_texture_coordinates;
|
||||
|
||||
void main() {
|
||||
gl_Position = uniforms.mvp * vec4(vertex_position, 1.0);
|
||||
color = vertex_color;
|
||||
interporlated_texture_coordinates = texture_coordinates;
|
||||
}
|
||||
|
||||
@@ -35,13 +35,12 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
|
||||
VertexBufferBuilder<BoxVertexShader::PerVertexData> vertex_builder;
|
||||
vertex_builder.SetLabel("Box");
|
||||
vertex_builder.AddVertices({
|
||||
{{100, 100, 0.0}, {Color::Red()}, {0.0, 0.0}}, // 1
|
||||
{{800, 100, 0.0}, {Color::Green()}, {1.0, 0.0}}, // 2
|
||||
{{800, 800, 0.0}, {Color::Blue()}, {1.0, 1.0}}, // 3
|
||||
|
||||
{{100, 100, 0.0}, {Color::Cyan()}, {0.0, 0.0}}, // 1
|
||||
{{800, 800, 0.0}, {Color::White()}, {1.0, 1.0}}, // 3
|
||||
{{100, 800, 0.0}, {Color::Purple()}, {0.0, 1.0}}, // 4
|
||||
{{100, 100, 0.0}, {0.0, 0.0}}, // 1
|
||||
{{800, 100, 0.0}, {1.0, 0.0}}, // 2
|
||||
{{800, 800, 0.0}, {1.0, 1.0}}, // 3
|
||||
{{100, 100, 0.0}, {0.0, 0.0}}, // 1
|
||||
{{800, 800, 0.0}, {1.0, 1.0}}, // 3
|
||||
{{100, 800, 0.0}, {0.0, 1.0}}, // 4
|
||||
});
|
||||
auto vertex_buffer =
|
||||
vertex_builder.CreateVertexBuffer(*context->GetPermanentsAllocator());
|
||||
@@ -74,8 +73,8 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) {
|
||||
|
||||
BoxFragmentShader::BindFrameInfo(
|
||||
cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
|
||||
BoxFragmentShader::BindContents1Texture(cmd, boston, sampler);
|
||||
BoxFragmentShader::BindContents2Texture(cmd, bridge, sampler);
|
||||
BoxFragmentShader::BindContents1(cmd, boston, sampler);
|
||||
BoxFragmentShader::BindContents2(cmd, bridge, sampler);
|
||||
|
||||
cmd.primitive_type = PrimitiveType::kTriangle;
|
||||
if (!pass.RecordCommand(std::move(cmd))) {
|
||||
|
||||
Reference in New Issue
Block a user