Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(257)

Unified Diff: cc/quads/draw_polygon_unittest.cc

Issue 1497153002: Replace inverse transform with Cross-product computation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved section and rebased again Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cc/quads/draw_polygon.cc ('k') | third_party/WebKit/LayoutTests/TestExpectations » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/quads/draw_polygon_unittest.cc
diff --git a/cc/quads/draw_polygon_unittest.cc b/cc/quads/draw_polygon_unittest.cc
index 0fc25e340721c1c15b7096405e895800096fb116..95eaf6b833e32d0234a56c6c5edabbd157cb0c73 100644
--- a/cc/quads/draw_polygon_unittest.cc
+++ b/cc/quads/draw_polygon_unittest.cc
@@ -2,6 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+// We would like to use M_PI on windows too.
+#ifdef _WIN32
+#define _USE_MATH_DEFINES
+#endif
+
#include <limits>
#include <vector>
@@ -11,11 +16,22 @@
#include "ui/gfx/transform.h"
namespace cc {
+
+#if !defined(OS_WIN)
+void DrawPolygon::RecomputeNormalForTesting() {
+ ConstructNormal();
+}
+#endif
+
namespace {
#define CREATE_NEW_DRAW_POLYGON(name, points_vector, normal, polygon_id) \
DrawPolygon name(NULL, points_vector, normal, polygon_id)
+#define CREATE_TEST_DRAW_POLYGON(name, points_vector, polygon_id) \
+ DrawPolygon name(NULL, points_vector, gfx::Vector3dF(1, 2, 3), polygon_id); \
+ name.RecomputeNormalForTesting()
+
#define EXPECT_FLOAT_WITHIN_EPSILON_OF(a, b) \
EXPECT_TRUE(std::abs(a - b) < std::numeric_limits<float>::epsilon());
@@ -24,6 +40,11 @@ namespace {
EXPECT_FLOAT_EQ(point_a.y(), point_b.y()); \
EXPECT_FLOAT_EQ(point_a.z(), point_b.z());
+#define EXPECT_NORMAL(poly, n_x, n_y, n_z) \
+ EXPECT_FLOAT_WITHIN_EPSILON_OF(poly.normal().x(), n_x); \
+ EXPECT_FLOAT_WITHIN_EPSILON_OF(poly.normal().y(), n_y); \
+ EXPECT_FLOAT_WITHIN_EPSILON_OF(poly.normal().z(), n_z);
+
static void ValidatePoints(const DrawPolygon& polygon,
const std::vector<gfx::Point3F>& points) {
EXPECT_EQ(polygon.points().size(), points.size());
@@ -32,6 +53,101 @@ static void ValidatePoints(const DrawPolygon& polygon,
}
}
+// A simple square in a plane.
+TEST(DrawPolygonConstructionTest, NormalNormal) {
+ gfx::Transform Identity;
+ DrawPolygon polygon(NULL, gfx::RectF(10.0f, 10.0f), Identity, 1);
+ EXPECT_NORMAL(polygon, 0.0f, 0.0f, 1.0f);
+}
+
+// More complicated shapes.
+TEST(DrawPolygonConstructionTest, TestNormal) {
+ std::vector<gfx::Point3F> vertices;
+ vertices.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
+
+ CREATE_TEST_DRAW_POLYGON(polygon, vertices, 1);
+ EXPECT_NORMAL(polygon, 0.0f, 0.0f, 1.0f);
+}
+
+TEST(DrawPolygonConstructionTest, InverseNormal) {
+ std::vector<gfx::Point3F> vertices;
+ vertices.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
+
+ CREATE_TEST_DRAW_POLYGON(polygon, vertices, 1);
+ EXPECT_NORMAL(polygon, 0.0f, 0.0f, -1.0f);
+}
+
+TEST(DrawPolygonConstructionTest, ClippedNormal) {
+ std::vector<gfx::Point3F> vertices;
+ vertices.push_back(gfx::Point3F(0.1f, 10.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(0.0f, 9.9f, 0.0f));
+ vertices.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
+
+ CREATE_TEST_DRAW_POLYGON(polygon, vertices, 1);
+ EXPECT_NORMAL(polygon, 0.0f, 0.0f, 1.0f);
+}
+
+TEST(DrawPolygonConstructionTest, SlimTriangleNormal) {
+ std::vector<gfx::Point3F> vertices;
+ vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(5000.0f, 0.0f, 0.0f));
+ vertices.push_back(gfx::Point3F(10000.0f, 1.0f, 0.0f));
+
+ CREATE_TEST_DRAW_POLYGON(polygon, vertices, 2);
+ EXPECT_NORMAL(polygon, 0.0f, 0.0f, 1.0f);
+}
+
+TEST(DrawPolygonConstructionTest, ManyVertexNormal) {
+ std::vector<gfx::Point3F> vertices_c;
+ std::vector<gfx::Point3F> vertices_d;
+ for (int i = 0; i < 100; i++) {
+ vertices_c.push_back(
+ gfx::Point3F(cos(i * M_PI / 50), sin(i * M_PI / 50), 0.0f));
+ vertices_d.push_back(gfx::Point3F(cos(i * M_PI / 50) + 99.0f,
+ sin(i * M_PI / 50) + 99.0f, 100.0f));
+ }
+ CREATE_TEST_DRAW_POLYGON(polygon_c, vertices_c, 3);
+ EXPECT_NORMAL(polygon_c, 0.0f, 0.0f, 1.0f);
+
+ CREATE_TEST_DRAW_POLYGON(polygon_d, vertices_d, 4);
+ EXPECT_NORMAL(polygon_c, 0.0f, 0.0f, 1.0f);
+}
+
+// A simple rect being transformed.
+TEST(DrawPolygonConstructionTest, DizzyNormal) {
+ gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f);
+
+ gfx::Transform transform_i(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
+ DrawPolygon polygon_i(NULL, src, transform_i, 1);
+
+ EXPECT_NORMAL(polygon_i, 0.0f, 0.0f, 1.0f);
+
+ gfx::Transform tranform_a(0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
+ DrawPolygon polygon_a(NULL, src, tranform_a, 2);
+ EXPECT_NORMAL(polygon_a, 0.0f, 0.0f, -1.0f);
+
+ gfx::Transform tranform_b(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1);
+ DrawPolygon polygon_b(NULL, src, tranform_b, 3);
+ EXPECT_NORMAL(polygon_b, -1.0f, 0.0f, 0.0f);
+
+ gfx::Transform tranform_c(1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1);
+ DrawPolygon polygon_c(NULL, src, tranform_c, 4);
+ EXPECT_NORMAL(polygon_c, 0.0f, -1.0f, 0.0f);
+
+ gfx::Transform tranform_d(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
+ DrawPolygon polygon_d(NULL, src, tranform_d, 5);
+ EXPECT_NORMAL(polygon_d, 0.0f, 0.0f, -1.0f);
+}
+
// Two quads are nearly touching but definitely ordered. Second one should
// compare in front.
TEST(DrawPolygonSplitTest, NearlyTouchingOrder) {
@@ -62,7 +178,7 @@ TEST(DrawPolygonSplitTest, NotClearlyInFront) {
vertices_a.push_back(gfx::Point3F(87.2f, 1196.0f, 0.9f));
gfx::Vector3dF normal_a = gfx::CrossProduct(vertices_a[1] - vertices_a[0],
vertices_a[1] - vertices_a[2]);
- normal_a.Scale(1.0 / normal_a.Length());
+ normal_a.Scale(1.0f / normal_a.Length());
std::vector<gfx::Point3F> vertices_b;
vertices_b.push_back(gfx::Point3F(62.1f, 1034.7f, 1.0f));
@@ -71,7 +187,7 @@ TEST(DrawPolygonSplitTest, NotClearlyInFront) {
vertices_b.push_back(gfx::Point3F(62.1f, 1196.0f, 1.0f));
gfx::Vector3dF normal_b = gfx::CrossProduct(vertices_b[1] - vertices_b[0],
vertices_b[1] - vertices_b[2]);
- normal_b.Scale(1.0 / normal_b.Length());
+ normal_b.Scale(1.0f / normal_b.Length());
CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, normal_a, 0);
CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, normal_b, 1);
@@ -211,14 +327,21 @@ TEST(DrawPolygonSplitTest, AngledSplit) {
}
TEST(DrawPolygonTransformTest, TransformNormal) {
- // We give this polygon no actual vertices because we're not interested
- // in actually transforming any points, just the normal.
std::vector<gfx::Point3F> vertices_a;
+ vertices_a.push_back(gfx::Point3F(1.0f, 0.0f, 1.0f));
+ vertices_a.push_back(gfx::Point3F(-1.0f, 0.0f, -1.0f));
+ vertices_a.push_back(gfx::Point3F(0.0f, 1.0f, 0.0f));
CREATE_NEW_DRAW_POLYGON(
polygon_a, vertices_a, gfx::Vector3dF(0.707107f, 0.0f, -0.707107f), 0);
+ // Check we believe your little white lie.
+ EXPECT_NORMAL(polygon_a, 0.707107f, 0.0f, -0.707107f);
+
+ polygon_a.RecomputeNormalForTesting();
+ // Check that we recompute it more accurately.
+ EXPECT_NORMAL(polygon_a, sqrt(2) / 2, 0.0f, -sqrt(2) / 2);
gfx::Transform transform;
- transform.RotateAboutYAxis(45.0);
+ transform.RotateAboutYAxis(45.0f);
// This would transform the vertices as well, but we are transforming a
// DrawPolygon with 0 vertices just to make sure our normal transformation
// using the inverse tranpose matrix gives us the right result.
@@ -228,9 +351,7 @@ TEST(DrawPolygonTransformTest, TransformNormal) {
// because some architectures (e.g., Arm64) employ a fused multiply-add
// instruction which causes rounding asymmetry and reduces precision.
// http://crbug.com/401117.
- EXPECT_FLOAT_WITHIN_EPSILON_OF(polygon_a.normal().x(), 0);
- EXPECT_FLOAT_WITHIN_EPSILON_OF(polygon_a.normal().y(), 0);
- EXPECT_FLOAT_WITHIN_EPSILON_OF(polygon_a.normal().z(), -1);
+ EXPECT_NORMAL(polygon_a, 0.0f, 0.0f, -1.0f);
}
} // namespace
« no previous file with comments | « cc/quads/draw_polygon.cc ('k') | third_party/WebKit/LayoutTests/TestExpectations » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698