OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <vector> |
| 6 |
| 7 #include "cc/output/bsp_compare_result.h" |
| 8 #include "cc/quads/draw_polygon.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/gfx/transform.h" |
| 11 |
| 12 namespace cc { |
| 13 namespace { |
| 14 |
| 15 #define CREATE_NEW_DRAW_POLYGON(name, points_vector, normal, polygon_id) \ |
| 16 DrawPolygon name(NULL, points_vector, normal, polygon_id) |
| 17 |
| 18 #define EXPECT_POINT_EQ(point_a, point_b) \ |
| 19 EXPECT_FLOAT_EQ(point_a.x(), point_b.x()); \ |
| 20 EXPECT_FLOAT_EQ(point_a.y(), point_b.y()); \ |
| 21 EXPECT_FLOAT_EQ(point_a.z(), point_b.z()); |
| 22 |
| 23 static void ValidatePoints(const DrawPolygon& polygon, |
| 24 const std::vector<gfx::Point3F>& points) { |
| 25 EXPECT_EQ(polygon.points().size(), points.size()); |
| 26 for (size_t i = 0; i < points.size(); i++) { |
| 27 EXPECT_POINT_EQ(polygon.points()[i], points[i]); |
| 28 } |
| 29 } |
| 30 |
| 31 // Two quads are definitely not touching and so no split should occur. |
| 32 TEST(DrawPolygonSplitTest, NotTouchingNoSplit) { |
| 33 std::vector<gfx::Point3F> vertices_a; |
| 34 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f)); |
| 35 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); |
| 36 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f)); |
| 37 vertices_a.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f)); |
| 38 std::vector<gfx::Point3F> vertices_b; |
| 39 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, 5.0f)); |
| 40 vertices_b.push_back(gfx::Point3F(5.0f, 0.0f, 15.0f)); |
| 41 vertices_b.push_back(gfx::Point3F(5.0f, 0.0f, 15.0f)); |
| 42 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, 5.0f)); |
| 43 |
| 44 CREATE_NEW_DRAW_POLYGON( |
| 45 polygon_a, vertices_a, gfx::Vector3dF(0.0f, 0.0f, 1.0f), 0); |
| 46 CREATE_NEW_DRAW_POLYGON( |
| 47 polygon_b, vertices_b, gfx::Vector3dF(-1.0f, 0.0f, 0.0f), 1); |
| 48 |
| 49 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_FRONT); |
| 50 } |
| 51 |
| 52 // One quad is resting against another, but doesn't cross its plane so no split |
| 53 // should occur. |
| 54 TEST(DrawPolygonSplitTest, BarelyTouchingNoSplit) { |
| 55 std::vector<gfx::Point3F> vertices_a; |
| 56 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f)); |
| 57 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); |
| 58 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f)); |
| 59 vertices_a.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f)); |
| 60 std::vector<gfx::Point3F> vertices_b; |
| 61 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, 0.0f)); |
| 62 vertices_b.push_back(gfx::Point3F(5.0f, 0.0f, -10.0f)); |
| 63 vertices_b.push_back(gfx::Point3F(5.0f, 0.0f, -10.0f)); |
| 64 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, 0.0f)); |
| 65 |
| 66 CREATE_NEW_DRAW_POLYGON( |
| 67 polygon_a, vertices_a, gfx::Vector3dF(0.0f, 0.0f, 1.0f), 0); |
| 68 CREATE_NEW_DRAW_POLYGON( |
| 69 polygon_b, vertices_b, gfx::Vector3dF(-1.0f, 0.0f, 0.0f), 1); |
| 70 |
| 71 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_BACK); |
| 72 } |
| 73 |
| 74 // One quad intersects another and becomes two pieces. |
| 75 TEST(DrawPolygonSplitTest, BasicSplit) { |
| 76 std::vector<gfx::Point3F> vertices_a; |
| 77 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f)); |
| 78 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); |
| 79 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f)); |
| 80 vertices_a.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f)); |
| 81 std::vector<gfx::Point3F> vertices_b; |
| 82 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, -5.0f)); |
| 83 vertices_b.push_back(gfx::Point3F(5.0f, 0.0f, -5.0f)); |
| 84 vertices_b.push_back(gfx::Point3F(5.0f, 0.0f, 5.0f)); |
| 85 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, 5.0f)); |
| 86 |
| 87 CREATE_NEW_DRAW_POLYGON( |
| 88 polygon_a, vertices_a, gfx::Vector3dF(0.0f, 0.0f, 1.0f), 0); |
| 89 CREATE_NEW_DRAW_POLYGON( |
| 90 polygon_b, vertices_b, gfx::Vector3dF(-1.0f, 0.0f, 0.0f), 1); |
| 91 |
| 92 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_SPLIT); |
| 93 |
| 94 scoped_ptr<DrawPolygon> front_polygon; |
| 95 scoped_ptr<DrawPolygon> back_polygon; |
| 96 polygon_b.Split(polygon_a, &front_polygon, &back_polygon); |
| 97 EXPECT_EQ(DrawPolygon::SideCompare(*front_polygon, polygon_a), BSP_FRONT); |
| 98 EXPECT_EQ(DrawPolygon::SideCompare(*back_polygon, polygon_a), BSP_BACK); |
| 99 |
| 100 std::vector<gfx::Point3F> test_points_a; |
| 101 test_points_a.push_back(gfx::Point3F(5.0f, 0.0f, 0.0f)); |
| 102 test_points_a.push_back(gfx::Point3F(5.0f, 0.0f, 5.0f)); |
| 103 test_points_a.push_back(gfx::Point3F(5.0f, 10.0f, 5.0f)); |
| 104 test_points_a.push_back(gfx::Point3F(5.0f, 10.0f, 0.0f)); |
| 105 std::vector<gfx::Point3F> test_points_b; |
| 106 test_points_b.push_back(gfx::Point3F(5.0f, 10.0f, 0.0f)); |
| 107 test_points_b.push_back(gfx::Point3F(5.0f, 10.0f, -5.0f)); |
| 108 test_points_b.push_back(gfx::Point3F(5.0f, 0.0f, -5.0f)); |
| 109 test_points_b.push_back(gfx::Point3F(5.0f, 0.0f, 0.0f)); |
| 110 ValidatePoints(*(front_polygon.get()), test_points_a); |
| 111 ValidatePoints(*(back_polygon.get()), test_points_b); |
| 112 |
| 113 EXPECT_EQ(front_polygon->points().size(), 4u); |
| 114 EXPECT_EQ(back_polygon->points().size(), 4u); |
| 115 } |
| 116 |
| 117 // In this test we cut the corner of a quad so that it creates a triangle and |
| 118 // a pentagon as a result. |
| 119 TEST(DrawPolygonSplitTest, AngledSplit) { |
| 120 std::vector<gfx::Point3F> vertices_a; |
| 121 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); |
| 122 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 10.0f)); |
| 123 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 10.0f)); |
| 124 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f)); |
| 125 std::vector<gfx::Point3F> vertices_b; |
| 126 vertices_b.push_back(gfx::Point3F(2.0f, 5.0f, 1.0f)); |
| 127 vertices_b.push_back(gfx::Point3F(2.0f, -5.0f, 1.0f)); |
| 128 vertices_b.push_back(gfx::Point3F(-1.0f, -5.0f, -2.0f)); |
| 129 vertices_b.push_back(gfx::Point3F(-1.0f, 5.0f, -2.0f)); |
| 130 |
| 131 CREATE_NEW_DRAW_POLYGON( |
| 132 polygon_a, vertices_a, gfx::Vector3dF(0.0f, 1.0f, 0.0f), 0); |
| 133 CREATE_NEW_DRAW_POLYGON( |
| 134 polygon_b, vertices_b, gfx::Vector3dF(0.707107f, 0.0f, -0.707107f), 1); |
| 135 |
| 136 EXPECT_EQ(DrawPolygon::SideCompare(polygon_a, polygon_b), BSP_SPLIT); |
| 137 |
| 138 scoped_ptr<DrawPolygon> front_polygon; |
| 139 scoped_ptr<DrawPolygon> back_polygon; |
| 140 polygon_a.Split(polygon_b, &front_polygon, &back_polygon); |
| 141 EXPECT_EQ(DrawPolygon::SideCompare(*front_polygon, polygon_b), BSP_FRONT); |
| 142 EXPECT_EQ(DrawPolygon::SideCompare(*back_polygon, polygon_b), BSP_BACK); |
| 143 |
| 144 EXPECT_EQ(front_polygon->points().size(), 3u); |
| 145 EXPECT_EQ(back_polygon->points().size(), 5u); |
| 146 |
| 147 std::vector<gfx::Point3F> test_points_a; |
| 148 test_points_a.push_back(gfx::Point3F(10.0f, 0.0f, 9.0f)); |
| 149 test_points_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f)); |
| 150 test_points_a.push_back(gfx::Point3F(1.0f, 0.0f, 0.0f)); |
| 151 std::vector<gfx::Point3F> test_points_b; |
| 152 test_points_b.push_back(gfx::Point3F(1.0f, 0.0f, 0.0f)); |
| 153 test_points_b.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); |
| 154 test_points_b.push_back(gfx::Point3F(0.0f, 0.0f, 10.0f)); |
| 155 test_points_b.push_back(gfx::Point3F(10.0f, 0.0f, 10.0f)); |
| 156 test_points_b.push_back(gfx::Point3F(10.0f, 0.0f, 9.0f)); |
| 157 |
| 158 ValidatePoints(*(front_polygon.get()), test_points_a); |
| 159 ValidatePoints(*(back_polygon.get()), test_points_b); |
| 160 } |
| 161 |
| 162 TEST(DrawPolygonTransformTest, TransformNormal) { |
| 163 // We give this polygon no actual vertices because we're not interested |
| 164 // in actually transforming any points, just the normal. |
| 165 std::vector<gfx::Point3F> vertices_a; |
| 166 CREATE_NEW_DRAW_POLYGON( |
| 167 polygon_a, vertices_a, gfx::Vector3dF(0.707107f, 0.0f, -0.707107f), 0); |
| 168 |
| 169 gfx::Transform transform; |
| 170 transform.RotateAboutYAxis(45.0); |
| 171 // This would transform the vertices as well, but we are transforming a |
| 172 // DrawPolygon with 0 vertices just to make sure our normal transformation |
| 173 // using the inverse tranpose matrix gives us the right result. |
| 174 polygon_a.TransformToScreenSpace(transform); |
| 175 |
| 176 EXPECT_FLOAT_EQ(polygon_a.normal().x(), 0); |
| 177 EXPECT_FLOAT_EQ(polygon_a.normal().y(), 0); |
| 178 EXPECT_FLOAT_EQ(polygon_a.normal().z(), -1); |
| 179 } |
| 180 |
| 181 } // namespace |
| 182 } // namespace cc |
OLD | NEW |