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

Side by Side Diff: cc/base/math_util_unittest.cc

Issue 2551263002: Don't add duplicate points when clipping (Closed)
Patch Set: danakj comments #15 Created 3 years, 11 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
« cc/base/math_util.h ('K') | « cc/base/math_util.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 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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 #include "cc/base/math_util.h" 5 #include "cc/base/math_util.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <cmath> 9 #include <cmath>
10 10
11 #include "cc/test/geometry_test_utils.h" 11 #include "cc/test/geometry_test_utils.h"
12 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/geometry/quad_f.h"
14 #include "ui/gfx/geometry/rect.h" 15 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/geometry/rect_f.h" 16 #include "ui/gfx/geometry/rect_f.h"
16 #include "ui/gfx/transform.h" 17 #include "ui/gfx/transform.h"
17 18
18 namespace cc { 19 namespace cc {
19 namespace { 20 namespace {
20 21
21 TEST(MathUtilTest, ProjectionOfPerpendicularPlane) { 22 TEST(MathUtilTest, ProjectionOfPerpendicularPlane) {
22 // In this case, the m33() element of the transform becomes zero, which could 23 // In this case, the m33() element of the transform becomes zero, which could
23 // cause a divide-by-zero when projecting points/quads. 24 // cause a divide-by-zero when projecting points/quads.
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 } 449 }
449 } 450 }
450 451
451 TEST(MathUtilTest, RoundDownUnderflow) { 452 TEST(MathUtilTest, RoundDownUnderflow) {
452 // Rounding down -123 by 50 is -150, which underflows int8_t, but fits in 453 // Rounding down -123 by 50 is -150, which underflows int8_t, but fits in
453 // int16_t. 454 // int16_t.
454 EXPECT_FALSE(MathUtil::VerifyRoundDown<int8_t>(-123, 50)); 455 EXPECT_FALSE(MathUtil::VerifyRoundDown<int8_t>(-123, 50));
455 EXPECT_TRUE(MathUtil::VerifyRoundDown<int16_t>(-123, 50)); 456 EXPECT_TRUE(MathUtil::VerifyRoundDown<int16_t>(-123, 50));
456 } 457 }
457 458
459 #define EXPECT_SIMILAR_VALUE(x, y) EXPECT_TRUE(IsNearlyTheSameForTesting(x, y))
460 #define EXPECT_DISSIMILAR_VALUE(x, y) \
461 EXPECT_FALSE(IsNearlyTheSameForTesting(x, y))
462
463 // Arbitrary point that shouldn't be different from zero.
464 static const float zeroish = 1.0e-11f;
465
466 TEST(MathUtilTest, Approximate) {
467 // Same should be similar.
468 EXPECT_SIMILAR_VALUE(1.0f, 1.0f);
469
470 // Zero should not cause similarity issues.
471 EXPECT_SIMILAR_VALUE(0.0f, 0.0f);
472
473 // Chosen sensitivity makes hardware sense, whether small or large.
474 EXPECT_SIMILAR_VALUE(0.0f, std::nextafter(0.0f, 1.0f));
475 EXPECT_SIMILAR_VALUE(1000000.0f, std::nextafter(1000000.0f, 0.0f));
476
477 // Make sure that neither the side you approach, nor the order of
478 // parameters matter at the borderline case.
479 EXPECT_SIMILAR_VALUE(std::nextafter(0.0f, 1.0f), 0.0f);
480 EXPECT_SIMILAR_VALUE(std::nextafter(1000000.0f, 0.0f), 1000000.0f);
481 EXPECT_SIMILAR_VALUE(0.0f, std::nextafter(0.0f, -1.0f));
482 EXPECT_SIMILAR_VALUE(1000000.0f, std::nextafter(1000000.0f, 1e9f));
483 EXPECT_SIMILAR_VALUE(std::nextafter(0.0f, -1.0f), 0.0f);
484 EXPECT_SIMILAR_VALUE(std::nextafter(1000000.0f, 1e9f), 1000000.0f);
485
486 // Double check our arbitrary constant. Mostly this is for the
487 // following Point tests.
488 EXPECT_SIMILAR_VALUE(0.0f, zeroish);
489
490 // Arbitrary point that is different from one for Approximate tests.
491 EXPECT_SIMILAR_VALUE(1.0f, 1.000001f);
492
493 // Arbitrary (large) difference close to 1.
494 EXPECT_SIMILAR_VALUE(10000000.0f, 10000001.0f);
495
496 // Make sure one side being zero doesn't hide real differences.
497 EXPECT_DISSIMILAR_VALUE(0.0f, 1.0f);
498 EXPECT_DISSIMILAR_VALUE(1.0f, 0.0f);
499
500 // Make sure visible differences don't disappear.
501 EXPECT_DISSIMILAR_VALUE(1.0f, 2.0f);
502 EXPECT_DISSIMILAR_VALUE(10000.0f, 10001.0f);
503 }
504
505 #define EXPECT_SIMILAR_POINT_F(x, y) \
506 EXPECT_TRUE(IsNearlyTheSameForTesting(gfx::PointF x, gfx::PointF y))
507 #define EXPECT_DISSIMILAR_POINT_F(x, y) \
508 EXPECT_FALSE(IsNearlyTheSameForTesting(gfx::PointF x, gfx::PointF y))
509
510 TEST(MathUtilTest, ApproximatePointF) {
511 // Same is similar.
512 EXPECT_SIMILAR_POINT_F((0.0f, 0.0f), (0.0f, 0.0f));
513
514 // Not over sensitive on each axis.
515 EXPECT_SIMILAR_POINT_F((zeroish, 0.0f), (0.0f, 0.0f));
516 EXPECT_SIMILAR_POINT_F((0.0f, zeroish), (0.0f, 0.0f));
517 EXPECT_SIMILAR_POINT_F((0.0f, 0.0f), (zeroish, 0.0f));
518 EXPECT_SIMILAR_POINT_F((0.0f, 0.0f), (0.0f, zeroish));
519
520 // Still sensitive to any axis.
521 EXPECT_DISSIMILAR_POINT_F((1.0f, 0.0f), (0.0f, 0.0f));
522 EXPECT_DISSIMILAR_POINT_F((0.0f, 1.0f), (0.0f, 0.0f));
523 EXPECT_DISSIMILAR_POINT_F((0.0f, 0.0f), (1.0f, 0.0f));
524 EXPECT_DISSIMILAR_POINT_F((0.0f, 0.0f), (0.0f, 1.0f));
525
526 // Not crossed over, sensitive on each side of each axis.
527 EXPECT_SIMILAR_POINT_F((0.0f, 1.0f), (0.0f, 1.0f));
528 EXPECT_SIMILAR_POINT_F((1.0f, 2.0f), (1.0f, 2.0f));
529 EXPECT_DISSIMILAR_POINT_F((3.0f, 2.0f), (1.0f, 2.0f));
530 EXPECT_DISSIMILAR_POINT_F((1.0f, 3.0f), (1.0f, 1.0f));
531 EXPECT_DISSIMILAR_POINT_F((1.0f, 2.0f), (3.0f, 2.0f));
532 EXPECT_DISSIMILAR_POINT_F((1.0f, 2.0f), (1.0f, 3.0f));
533 }
534
535 #define EXPECT_SIMILAR_POINT_3F(x, y) \
536 EXPECT_TRUE(IsNearlyTheSameForTesting(gfx::Point3F x, gfx::Point3F y))
537 #define EXPECT_DISSIMILAR_POINT_3F(x, y) \
538 EXPECT_FALSE(IsNearlyTheSameForTesting(gfx::Point3F x, gfx::Point3F y))
539
540 TEST(MathUtilTest, ApproximatePoint3F) {
541 // Same same.
542 EXPECT_SIMILAR_POINT_3F((0.0f, 0.0f, 0.0f), (0.0f, 0.0f, 0.0f));
543 EXPECT_SIMILAR_POINT_3F((zeroish, 0.0f, 0.0f), (0.0f, 0.0f, 0.0f));
544 EXPECT_SIMILAR_POINT_3F((0.0f, zeroish, 0.0f), (0.0f, 0.0f, 0.0f));
545 EXPECT_SIMILAR_POINT_3F((0.0f, 0.0f, zeroish), (0.0f, 0.0f, 0.0f));
546 EXPECT_SIMILAR_POINT_3F((0.0f, 0.0f, 0.0f), (zeroish, 0.0f, 0.0f));
547 EXPECT_SIMILAR_POINT_3F((0.0f, 0.0f, 0.0f), (0.0f, zeroish, 0.0f));
548 EXPECT_SIMILAR_POINT_3F((0.0f, 0.0f, 0.0f), (0.0f, 0.0f, zeroish));
549
550 // Not crossed over, sensitive on each side of each axis.
551 EXPECT_SIMILAR_POINT_3F((1.0f, 2.0f, 3.0f), (1.0f, 2.0f, 3.0f));
552 EXPECT_DISSIMILAR_POINT_3F((4.0f, 2.0f, 3.0f), (1.0f, 2.0f, 3.0f));
553 EXPECT_DISSIMILAR_POINT_3F((1.0f, 4.0f, 3.0f), (1.0f, 1.0f, 3.0f));
554 EXPECT_DISSIMILAR_POINT_3F((1.0f, 2.0f, 4.0f), (1.0f, 2.0f, 1.0f));
555 EXPECT_DISSIMILAR_POINT_3F((1.0f, 2.0f, 3.0f), (4.0f, 2.0f, 3.0f));
556 EXPECT_DISSIMILAR_POINT_3F((1.0f, 2.0f, 3.0f), (1.0f, 4.0f, 3.0f));
557 EXPECT_DISSIMILAR_POINT_3F((1.0f, 2.0f, 3.0f), (1.0f, 2.0f, 4.0f));
558 }
559
560 // This takes a quad for which two points, (at x = -99) are behind and below
561 // the eyepoint and checks to make sure we build a triangle. We used to build
562 // a degenerate quad.
563 TEST(MathUtilTest, MapClippedQuadDuplicateTriangle) {
564 gfx::Transform transform;
565 transform.MakeIdentity();
566 transform.ApplyPerspectiveDepth(50.0);
567 transform.RotateAboutYAxis(89.0);
568 // We are amost looking along the X-Y plane from (-50, almost 0)
569
570 gfx::QuadF src_quad(gfx::PointF(0.0f, 100.0f), gfx::PointF(0.0f, -100.0f),
571 gfx::PointF(-99.0f, -300.0f),
572 gfx::PointF(-99.0f, -100.0f));
573
574 gfx::PointF clipped_quad[8];
575 int num_vertices_in_clipped_quad;
576
577 MathUtil::MapClippedQuad(transform, src_quad, clipped_quad,
578 &num_vertices_in_clipped_quad);
579
580 EXPECT_EQ(num_vertices_in_clipped_quad, 3);
581 }
582
583 // This takes a quad for which two points, (at x = -99) are behind and below
584 // the eyepoint and checks to make sure we build a triangle. We used to build
585 // a degenerate quad. The quirk here is that the two shared points are first
586 // and last, not sequential.
587 TEST(MathUtilTest, MapClippedQuadDuplicateTriangleWrapped) {
588 gfx::Transform transform;
589 transform.MakeIdentity();
590 transform.ApplyPerspectiveDepth(50.0);
591 transform.RotateAboutYAxis(89.0);
592
593 gfx::QuadF src_quad(gfx::PointF(-99.0f, -100.0f), gfx::PointF(0.0f, 100.0f),
594 gfx::PointF(0.0f, -100.0f), gfx::PointF(-99.0f, -300.0f));
595
596 gfx::PointF clipped_quad[8];
597 int num_vertices_in_clipped_quad;
598
599 MathUtil::MapClippedQuad(transform, src_quad, clipped_quad,
600 &num_vertices_in_clipped_quad);
601
602 EXPECT_EQ(num_vertices_in_clipped_quad, 3);
603 }
604
605 // Here we map and clip a quad with only one point that disappears to infinity
606 // behind us. We don't want two vertices at infinity crossing in and out
607 // of w < 0 space.
608 TEST(MathUtilTest, MapClippedQuadDuplicateQuad) {
609 gfx::Transform transform;
610 transform.MakeIdentity();
611 transform.ApplyPerspectiveDepth(50.0);
612 transform.RotateAboutYAxis(89.0);
613
614 gfx::QuadF src_quad(gfx::PointF(0.0f, 100.0f), gfx::PointF(400.0f, 0.0f),
615 gfx::PointF(0.0f, -100.0f), gfx::PointF(-99.0f, -300.0f));
616
617 gfx::PointF clipped_quad[8];
618 int num_vertices_in_clipped_quad;
619
620 MathUtil::MapClippedQuad(transform, src_quad, clipped_quad,
621 &num_vertices_in_clipped_quad);
622
623 EXPECT_EQ(num_vertices_in_clipped_quad, 4);
624 }
625
458 } // namespace 626 } // namespace
459 } // namespace cc 627 } // namespace cc
OLDNEW
« cc/base/math_util.h ('K') | « cc/base/math_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698