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

Side by Side Diff: cc/quads/draw_polygon_unittest.cc

Issue 1883623002: Correct normal computation for inverted polygons. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months 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 unified diff | Download patch
« no previous file with comments | « cc/quads/draw_polygon.cc ('k') | ui/gfx/transform.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // We would like to use M_PI on windows too. 5 // We would like to use M_PI on windows too.
6 #ifdef _WIN32 6 #ifdef _WIN32
7 #define _USE_MATH_DEFINES 7 #define _USE_MATH_DEFINES
8 #endif 8 #endif
9 9
10 #include <stddef.h> 10 #include <stddef.h>
(...skipping 12 matching lines...) Expand all
23 void DrawPolygon::RecomputeNormalForTesting() { 23 void DrawPolygon::RecomputeNormalForTesting() {
24 ConstructNormal(); 24 ConstructNormal();
25 } 25 }
26 #endif 26 #endif
27 27
28 namespace { 28 namespace {
29 29
30 #define CREATE_NEW_DRAW_POLYGON(name, points_vector, normal, polygon_id) \ 30 #define CREATE_NEW_DRAW_POLYGON(name, points_vector, normal, polygon_id) \
31 DrawPolygon name(NULL, points_vector, normal, polygon_id) 31 DrawPolygon name(NULL, points_vector, normal, polygon_id)
32 32
33 #define CREATE_TEST_DRAW_POLYGON(name, points_vector, polygon_id) \ 33 #define CREATE_TEST_DRAW_FORWARD_POLYGON(name, points_vector, id) \
34 DrawPolygon name(NULL, points_vector, gfx::Vector3dF(1, 2, 3), polygon_id); \ 34 DrawPolygon name(NULL, points_vector, gfx::Vector3dF(0, 0, 1.0f), id); \
35 name.RecomputeNormalForTesting()
36
37 #define CREATE_TEST_DRAW_REVERSE_POLYGON(name, points_vector, id) \
38 DrawPolygon name(NULL, points_vector, gfx::Vector3dF(0, 0, -1.0f), id); \
35 name.RecomputeNormalForTesting() 39 name.RecomputeNormalForTesting()
36 40
37 #define EXPECT_FLOAT_WITHIN_EPSILON_OF(a, b) \ 41 #define EXPECT_FLOAT_WITHIN_EPSILON_OF(a, b) \
38 EXPECT_TRUE(std::abs(a - b) < std::numeric_limits<float>::epsilon()); 42 EXPECT_TRUE(std::abs(a - b) < std::numeric_limits<float>::epsilon());
39 43
40 #define EXPECT_POINT_EQ(point_a, point_b) \ 44 #define EXPECT_POINT_EQ(point_a, point_b) \
41 EXPECT_FLOAT_EQ(point_a.x(), point_b.x()); \ 45 EXPECT_FLOAT_EQ(point_a.x(), point_b.x()); \
42 EXPECT_FLOAT_EQ(point_a.y(), point_b.y()); \ 46 EXPECT_FLOAT_EQ(point_a.y(), point_b.y()); \
43 EXPECT_FLOAT_EQ(point_a.z(), point_b.z()); 47 EXPECT_FLOAT_EQ(point_a.z(), point_b.z());
44 48
(...skipping 18 matching lines...) Expand all
63 } 67 }
64 68
65 // More complicated shapes. 69 // More complicated shapes.
66 TEST(DrawPolygonConstructionTest, TestNormal) { 70 TEST(DrawPolygonConstructionTest, TestNormal) {
67 std::vector<gfx::Point3F> vertices; 71 std::vector<gfx::Point3F> vertices;
68 vertices.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f)); 72 vertices.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
69 vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); 73 vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
70 vertices.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f)); 74 vertices.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
71 vertices.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f)); 75 vertices.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
72 76
73 CREATE_TEST_DRAW_POLYGON(polygon, vertices, 1); 77 CREATE_TEST_DRAW_FORWARD_POLYGON(polygon, vertices, 1);
74 EXPECT_NORMAL(polygon, 0.0f, 0.0f, 1.0f); 78 EXPECT_NORMAL(polygon, 0.0f, 0.0f, 1.0f);
75 } 79 }
76 80
77 TEST(DrawPolygonConstructionTest, ClippedNormal) { 81 TEST(DrawPolygonConstructionTest, ClippedNormal) {
78 std::vector<gfx::Point3F> vertices; 82 std::vector<gfx::Point3F> vertices;
79 vertices.push_back(gfx::Point3F(0.1f, 10.0f, 0.0f)); 83 vertices.push_back(gfx::Point3F(0.1f, 10.0f, 0.0f));
80 vertices.push_back(gfx::Point3F(0.0f, 9.9f, 0.0f)); 84 vertices.push_back(gfx::Point3F(0.0f, 9.9f, 0.0f));
81 vertices.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f)); 85 vertices.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
82 vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); 86 vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
83 vertices.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f)); 87 vertices.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
84 vertices.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f)); 88 vertices.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
85 89
86 CREATE_TEST_DRAW_POLYGON(polygon, vertices, 1); 90 CREATE_TEST_DRAW_FORWARD_POLYGON(polygon, vertices, 1);
87 EXPECT_NORMAL(polygon, 0.0f, 0.0f, 1.0f); 91 EXPECT_NORMAL(polygon, 0.0f, 0.0f, 1.0f);
88 } 92 }
89 93
90 TEST(DrawPolygonConstructionTest, SlimTriangleNormal) { 94 TEST(DrawPolygonConstructionTest, SlimTriangleNormal) {
91 std::vector<gfx::Point3F> vertices; 95 std::vector<gfx::Point3F> vertices;
92 vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); 96 vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
93 vertices.push_back(gfx::Point3F(5000.0f, 0.0f, 0.0f)); 97 vertices.push_back(gfx::Point3F(5000.0f, 0.0f, 0.0f));
94 vertices.push_back(gfx::Point3F(10000.0f, 1.0f, 0.0f)); 98 vertices.push_back(gfx::Point3F(10000.0f, 1.0f, 0.0f));
95 99
96 CREATE_TEST_DRAW_POLYGON(polygon, vertices, 2); 100 CREATE_TEST_DRAW_FORWARD_POLYGON(polygon, vertices, 2);
97 EXPECT_NORMAL(polygon, 0.0f, 0.0f, 1.0f); 101 EXPECT_NORMAL(polygon, 0.0f, 0.0f, 1.0f);
98 } 102 }
99 103
100 TEST(DrawPolygonConstructionTest, ManyVertexNormal) { 104 TEST(DrawPolygonConstructionTest, ManyVertexNormal) {
101 std::vector<gfx::Point3F> vertices_c; 105 std::vector<gfx::Point3F> vertices_c;
102 std::vector<gfx::Point3F> vertices_d; 106 std::vector<gfx::Point3F> vertices_d;
103 for (int i = 0; i < 100; i++) { 107 for (int i = 0; i < 100; i++) {
104 vertices_c.push_back( 108 vertices_c.push_back(
105 gfx::Point3F(cos(i * M_PI / 50), sin(i * M_PI / 50), 0.0f)); 109 gfx::Point3F(cos(i * M_PI / 50), sin(i * M_PI / 50), 0.0f));
106 vertices_d.push_back(gfx::Point3F(cos(i * M_PI / 50) + 99.0f, 110 vertices_d.push_back(gfx::Point3F(cos(i * M_PI / 50) + 99.0f,
107 sin(i * M_PI / 50) + 99.0f, 100.0f)); 111 sin(i * M_PI / 50) + 99.0f, 100.0f));
108 } 112 }
109 CREATE_TEST_DRAW_POLYGON(polygon_c, vertices_c, 3); 113 CREATE_TEST_DRAW_FORWARD_POLYGON(polygon_c, vertices_c, 3);
110 EXPECT_NORMAL(polygon_c, 0.0f, 0.0f, 1.0f); 114 EXPECT_NORMAL(polygon_c, 0.0f, 0.0f, 1.0f);
111 115
112 CREATE_TEST_DRAW_POLYGON(polygon_d, vertices_d, 4); 116 CREATE_TEST_DRAW_FORWARD_POLYGON(polygon_d, vertices_d, 4);
113 EXPECT_NORMAL(polygon_c, 0.0f, 0.0f, 1.0f); 117 EXPECT_NORMAL(polygon_c, 0.0f, 0.0f, 1.0f);
114 } 118 }
115 119
116 // A simple rect being transformed. 120 // A simple rect being transformed.
117 TEST(DrawPolygonConstructionTest, SimpleNormal) { 121 TEST(DrawPolygonConstructionTest, SimpleNormal) {
118 gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f); 122 gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f);
119 123
120 gfx::Transform transform_i(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); 124 gfx::Transform transform_i(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
121 DrawPolygon polygon_i(NULL, src, transform_i, 1); 125 DrawPolygon polygon_i(NULL, src, transform_i, 1);
122 126
(...skipping 29 matching lines...) Expand all
152 156
153 TEST(DrawPolygonConstructionTest, NormalRotate90) { 157 TEST(DrawPolygonConstructionTest, NormalRotate90) {
154 gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f); 158 gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f);
155 159
156 gfx::Transform transform(0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1); 160 gfx::Transform transform(0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1);
157 DrawPolygon polygon_b(NULL, src, transform, 3); 161 DrawPolygon polygon_b(NULL, src, transform, 3);
158 162
159 EXPECT_NORMAL(polygon_b, 0.0f, 0.0f, 1.0f); 163 EXPECT_NORMAL(polygon_b, 0.0f, 0.0f, 1.0f);
160 } 164 }
161 165
162 // Bug https://bugs.chromium.org/p/chromium/issues/detail?id=595820 166 TEST(DrawPolygonConstructionTest, InvertXNormal) {
163 TEST(DrawPolygonConstructionTest, DISABLED_InvertXNormal) {
164 gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f); 167 gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f);
165 168
166 gfx::Transform transform(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); 169 gfx::Transform transform(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
167 DrawPolygon polygon_d(NULL, src, transform, 5); 170 DrawPolygon polygon_d(NULL, src, transform, 5);
168 171
169 EXPECT_NORMAL(polygon_d, 0.0f, 0.0f, 1.0f); 172 EXPECT_NORMAL(polygon_d, 0.0f, 0.0f, 1.0f);
170 } 173 }
171 174
172 TEST(DrawPolygonConstructionTest, DISABLED_InvertYNormal) { 175 TEST(DrawPolygonConstructionTest, InvertYNormal) {
173 gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f); 176 gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f);
174 177
175 gfx::Transform transform(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); 178 gfx::Transform transform(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
176 DrawPolygon polygon_d(NULL, src, transform, 5); 179 DrawPolygon polygon_d(NULL, src, transform, 5);
177 180
178 EXPECT_NORMAL(polygon_d, 0.0f, 0.0f, 1.0f); 181 EXPECT_NORMAL(polygon_d, 0.0f, 0.0f, 1.0f);
179 } 182 }
180 183
181 TEST(DrawPolygonConstructionTest, DISABLED_InvertZNormal) { 184 TEST(DrawPolygonConstructionTest, InvertZNormal) {
182 gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f); 185 gfx::RectF src(-0.1f, -10.0f, 0.2f, 20.0f);
183 186
184 gfx::Transform transform(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1); 187 gfx::Transform transform(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1);
185 DrawPolygon polygon_d(NULL, src, transform, 5); 188 DrawPolygon polygon_d(NULL, src, transform, 5);
186 189
187 EXPECT_NORMAL(polygon_d, 0.0f, 0.0f, -1.0f); 190 EXPECT_NORMAL(polygon_d, 0.0f, 0.0f, -1.0f);
188 } 191 }
189 192
190 // Two quads are nearly touching but definitely ordered. Second one should 193 // Two quads are nearly touching but definitely ordered. Second one should
191 // compare in front. 194 // compare in front.
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 366
364 ValidatePoints(*(front_polygon.get()), test_points_a); 367 ValidatePoints(*(front_polygon.get()), test_points_a);
365 ValidatePoints(*(back_polygon.get()), test_points_b); 368 ValidatePoints(*(back_polygon.get()), test_points_b);
366 } 369 }
367 370
368 TEST(DrawPolygonTransformTest, TransformNormal) { 371 TEST(DrawPolygonTransformTest, TransformNormal) {
369 std::vector<gfx::Point3F> vertices_a; 372 std::vector<gfx::Point3F> vertices_a;
370 vertices_a.push_back(gfx::Point3F(1.0f, 0.0f, 1.0f)); 373 vertices_a.push_back(gfx::Point3F(1.0f, 0.0f, 1.0f));
371 vertices_a.push_back(gfx::Point3F(-1.0f, 0.0f, -1.0f)); 374 vertices_a.push_back(gfx::Point3F(-1.0f, 0.0f, -1.0f));
372 vertices_a.push_back(gfx::Point3F(0.0f, 1.0f, 0.0f)); 375 vertices_a.push_back(gfx::Point3F(0.0f, 1.0f, 0.0f));
373 CREATE_NEW_DRAW_POLYGON( 376 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a,
374 polygon_a, vertices_a, gfx::Vector3dF(0.707107f, 0.0f, -0.707107f), 0); 377 gfx::Vector3dF(sqrt(2) / 2, 0.0f, -sqrt(2) / 2), 0);
375 // Check we believe your little white lie.
376 EXPECT_NORMAL(polygon_a, 0.707107f, 0.0f, -0.707107f);
377
378 polygon_a.RecomputeNormalForTesting();
379 // Check that we recompute it more accurately.
380 EXPECT_NORMAL(polygon_a, sqrt(2) / 2, 0.0f, -sqrt(2) / 2); 378 EXPECT_NORMAL(polygon_a, sqrt(2) / 2, 0.0f, -sqrt(2) / 2);
381 379
382 gfx::Transform transform; 380 gfx::Transform transform;
383 transform.RotateAboutYAxis(45.0f); 381 transform.RotateAboutYAxis(45.0f);
384 // This would transform the vertices as well, but we are transforming a 382 // This would transform the vertices as well, but we are transforming a
385 // DrawPolygon with 0 vertices just to make sure our normal transformation 383 // DrawPolygon with 0 vertices just to make sure our normal transformation
386 // using the inverse tranpose matrix gives us the right result. 384 // using the inverse tranpose matrix gives us the right result.
387 polygon_a.TransformToScreenSpace(transform); 385 polygon_a.TransformToScreenSpace(transform);
388 386
389 // Note: We use EXPECT_FLOAT_WITHIN_EPSILON instead of EXPECT_FLOAT_EQUAL here 387 // Note: We use EXPECT_FLOAT_WITHIN_EPSILON instead of EXPECT_FLOAT_EQUAL here
390 // because some architectures (e.g., Arm64) employ a fused multiply-add 388 // because some architectures (e.g., Arm64) employ a fused multiply-add
391 // instruction which causes rounding asymmetry and reduces precision. 389 // instruction which causes rounding asymmetry and reduces precision.
392 // http://crbug.com/401117. 390 // http://crbug.com/401117.
393 EXPECT_NORMAL(polygon_a, 0.0f, 0.0f, -1.0f); 391 EXPECT_NORMAL(polygon_a, 0.0f, 0.0f, -1.0f);
394 } 392 }
395 393
396 } // namespace 394 } // namespace
397 } // namespace cc 395 } // namespace cc
OLDNEW
« no previous file with comments | « cc/quads/draw_polygon.cc ('k') | ui/gfx/transform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698