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

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

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

Powered by Google App Engine
This is Rietveld 408576698