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

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

Issue 2551263002: Don't add duplicate points when clipping (Closed)
Patch Set: flackr comments #10 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.cc ('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) \
460 EXPECT_TRUE(IsNearlyTheSameForUnitTesting(x, y))
461 #define EXPECT_DISSIMILAR_VALUE(x, y) \
462 EXPECT_FALSE(IsNearlyTheSameForUnitTesting(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(IsNearlyTheSameForUnitTesting(gfx::PointF x, gfx::PointF y))
508 #define EXPECT_DISSIMILAR_POINT_F(x, y) \
509 EXPECT_FALSE(IsNearlyTheSameForUnitTesting(gfx::PointF x, gfx::PointF y))
510
511 TEST(MathUtilTest, ApproximatePointF) {
512 // Same is similar.
513 EXPECT_SIMILAR_POINT_F((0.0f, 0.0f), (0.0f, 0.0f));
514
515 // Not over sensitive on each axis.
516 EXPECT_SIMILAR_POINT_F((zeroish, 0.0f), (0.0f, 0.0f));
517 EXPECT_SIMILAR_POINT_F((0.0f, zeroish), (0.0f, 0.0f));
518 EXPECT_SIMILAR_POINT_F((0.0f, 0.0f), (zeroish, 0.0f));
519 EXPECT_SIMILAR_POINT_F((0.0f, 0.0f), (0.0f, zeroish));
520
521 // Still sensitive to any axis.
522 EXPECT_DISSIMILAR_POINT_F((1.0f, 0.0f), (0.0f, 0.0f));
523 EXPECT_DISSIMILAR_POINT_F((0.0f, 1.0f), (0.0f, 0.0f));
524 EXPECT_DISSIMILAR_POINT_F((0.0f, 0.0f), (1.0f, 0.0f));
525 EXPECT_DISSIMILAR_POINT_F((0.0f, 0.0f), (0.0f, 1.0f));
526
527 // Not crossed over, sensitive on each side of each axis.
528 EXPECT_SIMILAR_POINT_F((0.0f, 1.0f), (0.0f, 1.0f));
529 EXPECT_SIMILAR_POINT_F((1.0f, 2.0f), (1.0f, 2.0f));
530 EXPECT_DISSIMILAR_POINT_F((3.0f, 2.0f), (1.0f, 2.0f));
531 EXPECT_DISSIMILAR_POINT_F((1.0f, 3.0f), (1.0f, 1.0f));
532 EXPECT_DISSIMILAR_POINT_F((1.0f, 2.0f), (3.0f, 2.0f));
533 EXPECT_DISSIMILAR_POINT_F((1.0f, 2.0f), (1.0f, 3.0f));
534 }
535
536 #define EXPECT_SIMILAR_POINT_3F(x, y) \
537 EXPECT_TRUE(IsNearlyTheSameForUnitTesting(gfx::Point3F x, gfx::Point3F y))
538 #define EXPECT_DISSIMILAR_POINT_3F(x, y) \
539 EXPECT_FALSE(IsNearlyTheSameForUnitTesting(gfx::Point3F x, gfx::Point3F y))
540
541 TEST(MathUtilTest, ApproximatePoint3F) {
542 // Same same.
543 EXPECT_SIMILAR_POINT_3F((0.0f, 0.0f, 0.0f), (0.0f, 0.0f, 0.0f));
544 EXPECT_SIMILAR_POINT_3F((zeroish, 0.0f, 0.0f), (0.0f, 0.0f, 0.0f));
545 EXPECT_SIMILAR_POINT_3F((0.0f, zeroish, 0.0f), (0.0f, 0.0f, 0.0f));
546 EXPECT_SIMILAR_POINT_3F((0.0f, 0.0f, zeroish), (0.0f, 0.0f, 0.0f));
547 EXPECT_SIMILAR_POINT_3F((0.0f, 0.0f, 0.0f), (zeroish, 0.0f, 0.0f));
548 EXPECT_SIMILAR_POINT_3F((0.0f, 0.0f, 0.0f), (0.0f, zeroish, 0.0f));
549 EXPECT_SIMILAR_POINT_3F((0.0f, 0.0f, 0.0f), (0.0f, 0.0f, zeroish));
550
551 // Not crossed over, sensitive on each side of each axis.
552 EXPECT_SIMILAR_POINT_3F((1.0f, 2.0f, 3.0f), (1.0f, 2.0f, 3.0f));
553 EXPECT_DISSIMILAR_POINT_3F((4.0f, 2.0f, 3.0f), (1.0f, 2.0f, 3.0f));
554 EXPECT_DISSIMILAR_POINT_3F((1.0f, 4.0f, 3.0f), (1.0f, 1.0f, 3.0f));
555 EXPECT_DISSIMILAR_POINT_3F((1.0f, 2.0f, 4.0f), (1.0f, 2.0f, 1.0f));
556 EXPECT_DISSIMILAR_POINT_3F((1.0f, 2.0f, 3.0f), (4.0f, 2.0f, 3.0f));
557 EXPECT_DISSIMILAR_POINT_3F((1.0f, 2.0f, 3.0f), (1.0f, 4.0f, 3.0f));
558 EXPECT_DISSIMILAR_POINT_3F((1.0f, 2.0f, 3.0f), (1.0f, 2.0f, 4.0f));
559 }
560
561 // This takes a quad for which two points, (at x = -99) are behind and below
562 // the eyepoint and checks to make sure we build a triangle. We used to build
563 // a degenerate quad.
564 TEST(MathUtilTest, MapClippedQuadDuplicateTriangle) {
565 gfx::Transform transform;
566 transform.MakeIdentity();
567 transform.ApplyPerspectiveDepth(50.0);
568 transform.RotateAboutYAxis(89.0);
569 // We are amost looking along the X-Y plane from (-50, almost 0)
570
571 gfx::QuadF src_quad(gfx::PointF(0.0f, 100.0f), gfx::PointF(0.0f, -100.0f),
572 gfx::PointF(-99.0f, -300.0f),
573 gfx::PointF(-99.0f, -100.0f));
574
575 gfx::PointF clipped_quad[8];
576 int num_vertices_in_clipped_quad;
577
578 MathUtil::MapClippedQuad(transform, src_quad, clipped_quad,
579 &num_vertices_in_clipped_quad);
580
581 EXPECT_EQ(num_vertices_in_clipped_quad, 3);
582 }
583
584 // This takes a quad for which two points, (at x = -99) are behind and below
585 // the eyepoint and checks to make sure we build a triangle. We used to build
586 // a degenerate quad. The quirk here is that the two shared points are first
587 // and last, not sequential.
588 TEST(MathUtilTest, MapClippedQuadDuplicateTriangleWrapped) {
589 gfx::Transform transform;
590 transform.MakeIdentity();
591 transform.ApplyPerspectiveDepth(50.0);
592 transform.RotateAboutYAxis(89.0);
593
594 gfx::QuadF src_quad(gfx::PointF(-99.0f, -100.0f), gfx::PointF(0.0f, 100.0f),
595 gfx::PointF(0.0f, -100.0f), gfx::PointF(-99.0f, -300.0f));
596
597 gfx::PointF clipped_quad[8];
598 int num_vertices_in_clipped_quad;
599
600 MathUtil::MapClippedQuad(transform, src_quad, clipped_quad,
601 &num_vertices_in_clipped_quad);
602
603 EXPECT_EQ(num_vertices_in_clipped_quad, 3);
604 }
605
606 // Here we map and clip a quad with only one point that disappears to infinity
607 // behind us. We don't want two vertices at infinity crossing in and out
608 // of w < 0 space.
609 TEST(MathUtilTest, MapClippedQuadDuplicateQuad) {
610 gfx::Transform transform;
611 transform.MakeIdentity();
612 transform.ApplyPerspectiveDepth(50.0);
613 transform.RotateAboutYAxis(89.0);
614
615 gfx::QuadF src_quad(gfx::PointF(0.0f, 100.0f), gfx::PointF(400.0f, 0.0f),
616 gfx::PointF(0.0f, -100.0f), gfx::PointF(-99.0f, -300.0f));
617
618 gfx::PointF clipped_quad[8];
619 int num_vertices_in_clipped_quad;
620
621 MathUtil::MapClippedQuad(transform, src_quad, clipped_quad,
622 &num_vertices_in_clipped_quad);
623
624 EXPECT_EQ(num_vertices_in_clipped_quad, 4);
625 }
626
458 } // namespace 627 } // namespace
459 } // namespace cc 628 } // namespace cc
OLDNEW
« cc/base/math_util.cc ('K') | « cc/base/math_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698