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

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

Issue 2714243002: Correct polygon splitting in an almost-coplanar case. (Closed)
Patch Set: Make constants float, for Windows. Created 3 years, 9 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') | no next file » | 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>
11 11
12 #include <limits> 12 #include <limits>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/stl_util.h"
16 #include "cc/output/bsp_compare_result.h" 17 #include "cc/output/bsp_compare_result.h"
17 #include "cc/quads/draw_polygon.h" 18 #include "cc/quads/draw_polygon.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/gfx/transform.h" 20 #include "ui/gfx/transform.h"
20 21
21 namespace cc { 22 namespace cc {
22 23
23 #if !defined(OS_WIN) 24 #if !defined(OS_WIN)
24 void DrawPolygon::RecomputeNormalForTesting() { 25 void DrawPolygon::RecomputeNormalForTesting() {
25 ConstructNormal(); 26 ConstructNormal();
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 test_points_b.push_back(gfx::Point3F(1.0f, 0.0f, 0.0f)); 480 test_points_b.push_back(gfx::Point3F(1.0f, 0.0f, 0.0f));
480 test_points_b.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); 481 test_points_b.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
481 test_points_b.push_back(gfx::Point3F(0.0f, 0.0f, 10.0f)); 482 test_points_b.push_back(gfx::Point3F(0.0f, 0.0f, 10.0f));
482 test_points_b.push_back(gfx::Point3F(10.0f, 0.0f, 10.0f)); 483 test_points_b.push_back(gfx::Point3F(10.0f, 0.0f, 10.0f));
483 test_points_b.push_back(gfx::Point3F(10.0f, 0.0f, 9.0f)); 484 test_points_b.push_back(gfx::Point3F(10.0f, 0.0f, 9.0f));
484 485
485 ValidatePointsWithinDeltaOf(*front_polygon, test_points_a, 1e-6f); 486 ValidatePointsWithinDeltaOf(*front_polygon, test_points_a, 1e-6f);
486 ValidatePointsWithinDeltaOf(*back_polygon, test_points_b, 1e-6f); 487 ValidatePointsWithinDeltaOf(*back_polygon, test_points_b, 1e-6f);
487 } 488 }
488 489
490 // This test was derived from crbug.com/693826. An almost coplanar
491 // pair of polygons are used for splitting. In this case, the
492 // splitting plane distance signs are [ 0 0 + - ]. This configuration
493 // represents a case where snapping to the splitting plane causes the
494 // polygon to become twisted. Splitting should still give a valid
495 // result, indicated by all four of the input split polygon vertices
496 // being present in the output polygons.
497 TEST(DrawPolygonSplitTest, AlmostCoplanarSplit) {
498 std::vector<gfx::Point3F> vertices_a;
499 vertices_a.push_back(gfx::Point3F(723.814758300781250f, 552.810119628906250f,
500 -206.656036376953125f));
501 vertices_a.push_back(gfx::Point3F(797.634155273437500f, 549.095703125000000f,
502 -209.802902221679688f));
503 vertices_a.push_back(gfx::Point3F(799.264648437500000f, 490.325805664062500f,
504 -172.261627197265625f));
505 vertices_a.push_back(gfx::Point3F(720.732421875000000f, 493.944458007812500f,
506 -168.700469970703125f));
507 std::vector<gfx::Point3F> vertices_b;
508 vertices_b.push_back(gfx::Point3F(720.631286621093750f, 487.595977783203125f,
509 -164.681198120117188f));
510 vertices_b.push_back(gfx::Point3F(799.672851562500000f, 484.059020996093750f,
511 -168.219161987304688f));
512 vertices_b.push_back(gfx::Point3F(801.565490722656250f, 416.416809082031250f,
513 -125.007690429687500f));
514 vertices_b.push_back(gfx::Point3F(717.096801757812500f, 419.792327880859375f,
515 -120.967689514160156f));
516
517 CREATE_NEW_DRAW_POLYGON_PTR(
518 splitting_polygon, vertices_a,
519 gfx::Vector3dF(-0.062916249036789f, -0.538499474525452f,
520 -0.840273618698120f),
521 0);
522 CREATE_NEW_DRAW_POLYGON_PTR(
523 split_polygon, vertices_b,
524 gfx::Vector3dF(-0.061713f, -0.538550f, -0.840330f), 1);
525
526 std::unique_ptr<DrawPolygon> front_polygon;
527 std::unique_ptr<DrawPolygon> back_polygon;
528 bool is_coplanar;
529
530 splitting_polygon->SplitPolygon(std::move(split_polygon), &front_polygon,
531 &back_polygon, &is_coplanar);
532
533 EXPECT_FALSE(is_coplanar);
534 EXPECT_TRUE(front_polygon != nullptr);
535 EXPECT_TRUE(back_polygon != nullptr);
536
537 for (auto vertex : vertices_b) {
538 EXPECT_TRUE(base::ContainsValue(front_polygon->points(), vertex) ||
539 base::ContainsValue(back_polygon->points(), vertex));
540 }
541 }
542
489 // In this test we cut the corner of a quad so that it creates a triangle and 543 // In this test we cut the corner of a quad so that it creates a triangle and
490 // a pentagon as a result, and then cut the pentagon. 544 // a pentagon as a result, and then cut the pentagon.
491 TEST(DrawPolygonSplitTest, DoubleSplit) { 545 TEST(DrawPolygonSplitTest, DoubleSplit) {
492 std::vector<gfx::Point3F> vertices_a; 546 std::vector<gfx::Point3F> vertices_a;
493 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); 547 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
494 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 10.0f)); 548 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 10.0f));
495 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 10.0f)); 549 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 10.0f));
496 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f)); 550 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
497 std::vector<gfx::Point3F> vertices_b; 551 std::vector<gfx::Point3F> vertices_b;
498 vertices_b.push_back(gfx::Point3F(2.0f, 5.0f, 1.0f)); 552 vertices_b.push_back(gfx::Point3F(2.0f, 5.0f, 1.0f));
(...skipping 12 matching lines...) Expand all
511 bool is_coplanar; 565 bool is_coplanar;
512 566
513 polygon_b->SplitPolygon(std::move(polygon_a), &front_polygon, &back_polygon, 567 polygon_b->SplitPolygon(std::move(polygon_a), &front_polygon, &back_polygon,
514 &is_coplanar); 568 &is_coplanar);
515 EXPECT_FALSE(is_coplanar); 569 EXPECT_FALSE(is_coplanar);
516 EXPECT_TRUE(front_polygon != nullptr); 570 EXPECT_TRUE(front_polygon != nullptr);
517 EXPECT_TRUE(back_polygon != nullptr); 571 EXPECT_TRUE(back_polygon != nullptr);
518 572
519 EXPECT_EQ(3u, front_polygon->points().size()); 573 EXPECT_EQ(3u, front_polygon->points().size());
520 EXPECT_EQ(5u, back_polygon->points().size()); 574 EXPECT_EQ(5u, back_polygon->points().size());
575 std::vector<gfx::Point3F> saved_back_polygon_vertices =
576 back_polygon->points();
521 577
522 std::vector<gfx::Point3F> vertices_c; 578 std::vector<gfx::Point3F> vertices_c;
523 vertices_c.push_back(gfx::Point3F(0.0f, 0.0f, 10.0f)); 579 vertices_c.push_back(gfx::Point3F(0.0f, 0.0f, 10.0f));
524 vertices_c.push_back(gfx::Point3F(1.0f, -0.05f, 0.0f)); 580 vertices_c.push_back(gfx::Point3F(1.0f, -0.05f, 0.0f));
525 vertices_c.push_back(gfx::Point3F(10.0f, 0.05f, 9.0f)); 581 vertices_c.push_back(gfx::Point3F(10.0f, 0.05f, 9.0f));
526 582
527 CREATE_NEW_DRAW_POLYGON_PTR(polygon_c, vertices_c, 583 CREATE_NEW_DRAW_POLYGON_PTR(polygon_c, vertices_c,
528 gfx::Vector3dF(0.005555f, -0.99997f, 0.005555f), 584 gfx::Vector3dF(0.005555f, -0.99997f, 0.005555f),
529 0); 585 0);
530 polygon_c->RecomputeNormalForTesting(); 586 polygon_c->RecomputeNormalForTesting();
531 587
532 std::unique_ptr<DrawPolygon> second_front_polygon; 588 std::unique_ptr<DrawPolygon> second_front_polygon;
533 std::unique_ptr<DrawPolygon> second_back_polygon; 589 std::unique_ptr<DrawPolygon> second_back_polygon;
534 590
535 polygon_c->SplitPolygon(std::move(back_polygon), &second_front_polygon, 591 polygon_c->SplitPolygon(std::move(back_polygon), &second_front_polygon,
536 &second_back_polygon, &is_coplanar); 592 &second_back_polygon, &is_coplanar);
537 EXPECT_FALSE(is_coplanar); 593 EXPECT_FALSE(is_coplanar);
538 EXPECT_TRUE(second_front_polygon != nullptr); 594 EXPECT_TRUE(second_front_polygon != nullptr);
539 EXPECT_TRUE(second_back_polygon != nullptr); 595 EXPECT_TRUE(second_back_polygon != nullptr);
540 596
541 EXPECT_EQ(3u, second_front_polygon->points().size()); 597 EXPECT_EQ(4u, second_front_polygon->points().size());
542 EXPECT_EQ(3u, second_back_polygon->points().size()); 598 EXPECT_EQ(3u, second_back_polygon->points().size());
599
600 for (auto vertex : saved_back_polygon_vertices) {
601 EXPECT_TRUE(base::ContainsValue(second_front_polygon->points(), vertex) ||
602 base::ContainsValue(second_back_polygon->points(), vertex));
603 }
543 } 604 }
544 605
545 TEST(DrawPolygonTransformTest, TransformNormal) { 606 TEST(DrawPolygonTransformTest, TransformNormal) {
546 std::vector<gfx::Point3F> vertices_a; 607 std::vector<gfx::Point3F> vertices_a;
547 vertices_a.push_back(gfx::Point3F(1.0f, 0.0f, 1.0f)); 608 vertices_a.push_back(gfx::Point3F(1.0f, 0.0f, 1.0f));
548 vertices_a.push_back(gfx::Point3F(-1.0f, 0.0f, -1.0f)); 609 vertices_a.push_back(gfx::Point3F(-1.0f, 0.0f, -1.0f));
549 vertices_a.push_back(gfx::Point3F(0.0f, 1.0f, 0.0f)); 610 vertices_a.push_back(gfx::Point3F(0.0f, 1.0f, 0.0f));
550 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 611 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a,
551 gfx::Vector3dF(sqrt(2) / 2, 0.0f, -sqrt(2) / 2), 0); 612 gfx::Vector3dF(sqrt(2) / 2, 0.0f, -sqrt(2) / 2), 0);
552 EXPECT_NORMAL(polygon_a, sqrt(2) / 2, 0.0f, -sqrt(2) / 2); 613 EXPECT_NORMAL(polygon_a, sqrt(2) / 2, 0.0f, -sqrt(2) / 2);
553 614
554 gfx::Transform transform; 615 gfx::Transform transform;
555 transform.RotateAboutYAxis(45.0f); 616 transform.RotateAboutYAxis(45.0f);
556 // This would transform the vertices as well, but we are transforming a 617 // This would transform the vertices as well, but we are transforming a
557 // DrawPolygon with 0 vertices just to make sure our normal transformation 618 // DrawPolygon with 0 vertices just to make sure our normal transformation
558 // using the inverse tranpose matrix gives us the right result. 619 // using the inverse tranpose matrix gives us the right result.
559 polygon_a.TransformToScreenSpace(transform); 620 polygon_a.TransformToScreenSpace(transform);
560 621
561 // Note: We use EXPECT_FLOAT_WITHIN_EPSILON instead of EXPECT_FLOAT_EQUAL here 622 // Note: We use EXPECT_FLOAT_WITHIN_EPSILON instead of EXPECT_FLOAT_EQUAL here
562 // because some architectures (e.g., Arm64) employ a fused multiply-add 623 // because some architectures (e.g., Arm64) employ a fused multiply-add
563 // instruction which causes rounding asymmetry and reduces precision. 624 // instruction which causes rounding asymmetry and reduces precision.
564 // http://crbug.com/401117. 625 // http://crbug.com/401117.
565 EXPECT_NORMAL(polygon_a, 0.0f, 0.0f, -1.0f); 626 EXPECT_NORMAL(polygon_a, 0.0f, 0.0f, -1.0f);
566 } 627 }
567 628
568 } // namespace 629 } // namespace
569 } // namespace cc 630 } // namespace cc
OLDNEW
« no previous file with comments | « cc/quads/draw_polygon.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698