OLD | NEW |
---|---|
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 Loading... | |
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 // This makes the code in the test read like the code in the implementation | |
460 // despite the defensively named thunk that exposes the functionality for us. | |
461 #define approx(x, y) IsApproximatelyForUnitTesting(x, y) | |
flackr
2016/12/08 23:31:06
nit: Maybe this should be something like EXPECT_AP
Peter Mayo
2016/12/14 23:13:11
Picked EXPECT_SIMILAR... and EXPECT_DISSIMILAR...
| |
462 | |
463 TEST(MathUtilTest, Approximate) { | |
464 // Same | |
465 EXPECT_TRUE(approx(0.0f, 0.0f)); | |
466 // Chose sensitivity makes hardware sense. | |
467 EXPECT_TRUE(approx(1000000.0f, std::nextafter(1000000.0f, 0.0f))); | |
468 // Arbitrary point close to 1 | |
469 EXPECT_TRUE(approx(1.0f, 1.000001f)); | |
470 // Arbitrary difference close to 1 | |
471 EXPECT_TRUE(approx(10000000.0f, 10000001.0f)); | |
472 | |
473 // Please don't define scales by zero | |
474 EXPECT_FALSE(approx(0.0f, 1.0f)); | |
475 EXPECT_FALSE(approx(1.0f, 0.0f)); | |
476 // Or have visible differences disappear. | |
477 EXPECT_FALSE(approx(10000.0f, 10001.0f)); | |
478 } | |
479 | |
480 TEST(MathUtilTest, ApproximatePointF) { | |
481 // Same same. | |
482 EXPECT_TRUE(approx(gfx::PointF(0.0f, 0.0f), gfx::PointF(0.0f, 0.0f))); | |
Peter Mayo
2016/12/14 23:13:11
We can use a cover to get rid of the constructor n
| |
483 | |
484 // Not over sensitive on each axis. | |
485 EXPECT_TRUE(approx(gfx::PointF(1.000001f, 0.0f), gfx::PointF(1.0f, 0.0f))); | |
486 EXPECT_TRUE(approx(gfx::PointF(0.0f, 1.000001f), gfx::PointF(0.0f, 1.0f))); | |
487 EXPECT_TRUE(approx(gfx::PointF(1.0f, 0.0f), gfx::PointF(1.000001f, 0.0f))); | |
488 EXPECT_TRUE(approx(gfx::PointF(0.0f, 1.0f), gfx::PointF(0.0f, 1.000001f))); | |
489 EXPECT_TRUE( | |
490 approx(gfx::PointF(0.00000000001f, 0.0f), gfx::PointF(0.0f, 0.0f))); | |
491 | |
492 // Still sensitive to each axis. | |
493 EXPECT_FALSE(approx(gfx::PointF(0.0f, 1.0f), gfx::PointF(0.0f, 0.0f))); | |
494 EXPECT_FALSE(approx(gfx::PointF(1.0f, 0.0f), gfx::PointF(0.0f, 0.0f))); | |
495 EXPECT_FALSE(approx(gfx::PointF(0.0f, 0.0f), gfx::PointF(1.0f, 0.0f))); | |
496 EXPECT_FALSE(approx(gfx::PointF(0.0f, 0.0f), gfx::PointF(0.0f, 1.0f))); | |
497 | |
498 // Not crossed over. | |
499 EXPECT_TRUE(approx(gfx::PointF(0.0f, 1.0f), gfx::PointF(0.0f, 1.0f))); | |
500 } | |
501 | |
502 TEST(MathUtilTest, ApproximatePoint3F) { | |
503 // Same same. | |
504 static const float zeroish = 1.0e-11f; | |
505 EXPECT_TRUE( | |
506 approx(gfx::Point3F(0.0f, 0.0f, 0.0f), gfx::Point3F(0.0f, 0.0f, 0.0f))); | |
507 EXPECT_TRUE(approx(gfx::Point3F(zeroish, 0.0f, 0.0f), | |
508 gfx::Point3F(0.0f, 0.0f, 0.0f))); | |
509 EXPECT_TRUE(approx(gfx::Point3F(0.0f, zeroish, 0.0f), | |
510 gfx::Point3F(0.0f, 0.0f, 0.0f))); | |
511 EXPECT_TRUE(approx(gfx::Point3F(0.0f, 0.0f, zeroish), | |
512 gfx::Point3F(0.0f, 0.0f, 0.0f))); | |
513 EXPECT_TRUE(approx(gfx::Point3F(0.0f, 0.0f, 0.0f), | |
514 gfx::Point3F(zeroish, 0.0f, 0.0f))); | |
515 EXPECT_TRUE(approx(gfx::Point3F(0.0f, 0.0f, 0.0f), | |
516 gfx::Point3F(0.0f, zeroish, 0.0f))); | |
517 EXPECT_TRUE(approx(gfx::Point3F(0.0f, 0.0f, 0.0f), | |
518 gfx::Point3F(0.0f, 0.0f, zeroish))); | |
519 | |
520 // Not crossed over, sensitive on each side of each axis. | |
521 EXPECT_TRUE( | |
522 approx(gfx::Point3F(1.0f, 2.0f, 3.0f), gfx::Point3F(1.0f, 2.0f, 3.0f))); | |
523 EXPECT_FALSE( | |
524 approx(gfx::Point3F(4.0f, 2.0f, 3.0f), gfx::Point3F(1.0f, 2.0f, 3.0f))); | |
525 EXPECT_FALSE( | |
526 approx(gfx::Point3F(1.0f, 4.0f, 3.0f), gfx::Point3F(1.0f, 1.0f, 3.0f))); | |
527 EXPECT_FALSE( | |
528 approx(gfx::Point3F(1.0f, 2.0f, 4.0f), gfx::Point3F(1.0f, 2.0f, 1.0f))); | |
529 EXPECT_FALSE( | |
530 approx(gfx::Point3F(1.0f, 2.0f, 3.0f), gfx::Point3F(4.0f, 2.0f, 3.0f))); | |
531 EXPECT_FALSE( | |
532 approx(gfx::Point3F(1.0f, 2.0f, 3.0f), gfx::Point3F(1.0f, 4.0f, 3.0f))); | |
533 EXPECT_FALSE( | |
534 approx(gfx::Point3F(1.0f, 2.0f, 3.0f), gfx::Point3F(1.0f, 2.0f, 4.0f))); | |
535 } | |
536 | |
537 // This takes a quad for which two points, (at x = -99) are behind and below | |
538 // the eypoint and checks to make sure we build a triangle. We used to build | |
539 // a degnerate quad. | |
540 TEST(MathUtilTest, MapClippedQuadDuplicateTriangle) { | |
541 gfx::Transform transform; | |
542 transform.MakeIdentity(); | |
543 transform.ApplyPerspectiveDepth(50.0); | |
544 transform.RotateAboutYAxis(89.0); | |
545 // We are amost looking along the X-Y plane from (-50, almost 0) | |
546 | |
547 gfx::QuadF src_quad(gfx::PointF(0.0f, 100.0f), gfx::PointF(0.0f, -100.0f), | |
548 gfx::PointF(-99.0f, -300.0f), | |
549 gfx::PointF(-99.0f, -100.0f)); | |
550 | |
551 gfx::PointF clipped_quad[8]; | |
552 int num_vertices_in_clipped_quad; | |
553 | |
554 MathUtil::MapClippedQuad(transform, src_quad, clipped_quad, | |
555 &num_vertices_in_clipped_quad); | |
556 | |
557 EXPECT_EQ(num_vertices_in_clipped_quad, 3); | |
558 } | |
559 | |
560 // This takes a quad for which two points, (at x = -99) are behind and below | |
561 // the eypoint and checks to make sure we build a triangle. We used to build | |
562 // a degnerate quad. The quirk here is that the two shared points are first | |
563 // and last, not sequential. | |
564 TEST(MathUtilTest, MapClippedQuadDuplicateTriangleWrapped) { | |
565 gfx::Transform transform; | |
566 transform.MakeIdentity(); | |
567 transform.ApplyPerspectiveDepth(50.0); | |
568 transform.RotateAboutYAxis(89.0); | |
569 | |
570 gfx::QuadF src_quad(gfx::PointF(-99.0f, -100.0f), gfx::PointF(0.0f, 100.0f), | |
571 gfx::PointF(0.0f, -100.0f), gfx::PointF(-99.0f, -300.0f)); | |
572 | |
573 gfx::PointF clipped_quad[8]; | |
574 int num_vertices_in_clipped_quad; | |
575 | |
576 MathUtil::MapClippedQuad(transform, src_quad, clipped_quad, | |
577 &num_vertices_in_clipped_quad); | |
578 | |
579 EXPECT_EQ(num_vertices_in_clipped_quad, 3); | |
580 } | |
581 | |
582 // Here we map and clip a quad with only one point that disappears to infinity | |
583 // behind us. We don't want two verticies at inifinity crossing in and out | |
584 // of w < 0 space. | |
585 TEST(MathUtilTest, MapClippedQuadDuplicateQuad) { | |
586 gfx::Transform transform; | |
587 transform.MakeIdentity(); | |
588 transform.ApplyPerspectiveDepth(50.0); | |
589 transform.RotateAboutYAxis(89.0); | |
590 | |
591 gfx::QuadF src_quad(gfx::PointF(0.0f, 100.0f), gfx::PointF(400.0f, 0.0f), | |
592 gfx::PointF(0.0f, -100.0f), gfx::PointF(-99.0f, -300.0f)); | |
593 | |
594 gfx::PointF clipped_quad[8]; | |
595 int num_vertices_in_clipped_quad; | |
596 | |
597 MathUtil::MapClippedQuad(transform, src_quad, clipped_quad, | |
598 &num_vertices_in_clipped_quad); | |
599 | |
600 EXPECT_EQ(num_vertices_in_clipped_quad, 4); | |
601 } | |
602 | |
458 } // namespace | 603 } // namespace |
459 } // namespace cc | 604 } // namespace cc |
OLD | NEW |