Chromium Code Reviews| 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 "cc/output/bsp_compare_result.h" | |
| 6 #include "cc/quads/draw_polygon.h" | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 #include "ui/gfx/transform.h" | |
| 9 | |
| 10 namespace cc { | |
| 11 namespace { | |
| 12 | |
| 13 #define CREATE_NEW_DRAW_POLYGON(name, vertex_array, num_vertices, polygon_id) \ | |
| 14 DrawPolygon name(NULL, vertex_array, num_vertices, polygon_id) | |
| 15 | |
| 16 // Two quads are definitely not touching and so no split should occur. | |
| 17 TEST(DrawPolygonSplitTest, NotTouchingNoSplit) { | |
| 18 gfx::Point3F vertices_a[4]; | |
| 19 vertices_a[0] = gfx::Point3F(0.0f, 10.0f, 0.0f); | |
| 20 vertices_a[1] = gfx::Point3F(0.0f, 0.0f, 0.0f); | |
| 21 vertices_a[2] = gfx::Point3F(10.0f, 0.0f, 0.0f); | |
| 22 vertices_a[3] = gfx::Point3F(10.0f, 10.0f, 0.0f); | |
| 23 gfx::Point3F vertices_b[4]; | |
| 24 vertices_b[0] = gfx::Point3F(5.0f, 10.0f, 5.0f); | |
| 25 vertices_b[1] = gfx::Point3F(5.0f, 0.0f, 15.0f); | |
| 26 vertices_b[2] = gfx::Point3F(5.0f, 0.0f, 15.0f); | |
| 27 vertices_b[3] = gfx::Point3F(5.0f, 10.0f, 5.0f); | |
| 28 | |
| 29 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 4, 0); | |
| 30 CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, 4, 1); | |
| 31 | |
| 32 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_FRONT); | |
| 33 } | |
| 34 | |
| 35 // One quad is resting against another, but doesn't cross its plane so no split | |
| 36 // should occur. | |
| 37 TEST(DrawPolygonSplitTest, BarelyTouchingNoSplit) { | |
| 38 gfx::Point3F vertices_a[4]; | |
| 39 vertices_a[0] = gfx::Point3F(0.0f, 10.0f, 0.0f); | |
| 40 vertices_a[1] = gfx::Point3F(0.0f, 0.0f, 0.0f); | |
| 41 vertices_a[2] = gfx::Point3F(10.0f, 0.0f, 0.0f); | |
| 42 vertices_a[3] = gfx::Point3F(10.0f, 10.0f, 0.0f); | |
| 43 gfx::Point3F vertices_b[4]; | |
| 44 vertices_b[0] = gfx::Point3F(5.0f, 10.0f, 0.0f); | |
| 45 vertices_b[1] = gfx::Point3F(5.0f, 0.0f, -10.0f); | |
| 46 vertices_b[2] = gfx::Point3F(5.0f, 0.0f, -10.0f); | |
| 47 vertices_b[3] = gfx::Point3F(5.0f, 10.0f, 0.0f); | |
| 48 | |
| 49 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 4, 0); | |
| 50 CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, 4, 1); | |
| 51 | |
| 52 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_BACK); | |
| 53 } | |
| 54 | |
| 55 // One quad intersects another and becomes two pieces. | |
| 56 TEST(DrawPolygonSplitTest, BasicSplit) { | |
| 57 gfx::Point3F vertices_a[4]; | |
| 58 vertices_a[0] = gfx::Point3F(0.0f, 10.0f, 0.0f); | |
| 59 vertices_a[1] = gfx::Point3F(0.0f, 0.0f, 0.0f); | |
| 60 vertices_a[2] = gfx::Point3F(10.0f, 0.0f, 0.0f); | |
| 61 vertices_a[3] = gfx::Point3F(10.0f, 10.0f, 0.0f); | |
| 62 gfx::Point3F vertices_b[4]; | |
| 63 vertices_b[0] = gfx::Point3F(5.0f, 10.0f, -5.0f); | |
| 64 vertices_b[1] = gfx::Point3F(5.0f, 0.0f, -5.0f); | |
| 65 vertices_b[2] = gfx::Point3F(5.0f, 0.0f, 5.0f); | |
| 66 vertices_b[3] = gfx::Point3F(5.0f, 10.0f, 5.0f); | |
| 67 | |
| 68 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 4, 0); | |
| 69 CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, 4, 1); | |
| 70 | |
| 71 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_SPLIT); | |
|
enne (OOO)
2014/07/23 21:19:32
Can you test the values of the points too?
troyhildebrandt
2014/07/24 00:43:21
Done.
| |
| 72 | |
| 73 scoped_ptr<DrawPolygon> front_polygon; | |
| 74 scoped_ptr<DrawPolygon> back_polygon; | |
| 75 polygon_b.Split(polygon_a, &front_polygon, &back_polygon); | |
| 76 EXPECT_EQ(DrawPolygon::SideCompare(*front_polygon, polygon_a), BSP_FRONT); | |
| 77 EXPECT_EQ(DrawPolygon::SideCompare(*back_polygon, polygon_a), BSP_BACK); | |
| 78 } | |
| 79 | |
| 80 // A "wave" shape of points that do not make up those of a convex polygon | |
| 81 // crossing the boundaries of another polygon. This should not cause a problem | |
| 82 // and should still result in two different pieces of geometry on either side. | |
| 83 TEST(DrawPolygonSplitTest, NonConvexSplit) { | |
| 84 gfx::Point3F vertices_a[4]; | |
| 85 vertices_a[0] = gfx::Point3F(0.0f, 10.0f, 0.0f); | |
| 86 vertices_a[1] = gfx::Point3F(0.0f, 0.0f, 0.0f); | |
| 87 vertices_a[2] = gfx::Point3F(10.0f, 0.0f, 0.0f); | |
| 88 vertices_a[3] = gfx::Point3F(10.0f, 10.0f, 0.0f); | |
| 89 gfx::Point3F vertices_b[4]; | |
| 90 vertices_b[0] = gfx::Point3F(5.0f, 10.0f, -10.0f); | |
| 91 vertices_b[1] = gfx::Point3F(5.0f, 7.5f, -5.0f); | |
| 92 vertices_b[2] = gfx::Point3F(5.0f, 5.0f, 0.0f); | |
| 93 vertices_b[3] = gfx::Point3F(5.0f, 7.5f, 5.0f); | |
| 94 vertices_b[4] = gfx::Point3F(5.0f, 10.0f, 10.0f); | |
| 95 | |
| 96 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 4, 0); | |
| 97 CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, 5, 1); | |
| 98 | |
| 99 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_SPLIT); | |
| 100 | |
| 101 scoped_ptr<DrawPolygon> front_polygon; | |
| 102 scoped_ptr<DrawPolygon> back_polygon; | |
| 103 polygon_b.Split(polygon_a, &front_polygon, &back_polygon); | |
| 104 EXPECT_EQ(DrawPolygon::SideCompare(*front_polygon, polygon_a), BSP_FRONT); | |
| 105 EXPECT_EQ(DrawPolygon::SideCompare(*back_polygon, polygon_a), BSP_BACK); | |
| 106 } | |
| 107 | |
| 108 // In this test we cut the corner of a quad so that it creates a triangle and | |
| 109 // a pentagon as a result. | |
| 110 TEST(DrawPolygonSplitTest, AngledSplit) { | |
| 111 gfx::Point3F vertices_a[4]; | |
| 112 vertices_a[0] = gfx::Point3F(0.0f, 0.0f, 0.0f); | |
| 113 vertices_a[1] = gfx::Point3F(0.0f, 0.0f, 10.0f); | |
| 114 vertices_a[2] = gfx::Point3F(10.0f, 0.0f, 10.0f); | |
| 115 vertices_a[3] = gfx::Point3F(10.0f, 0.0f, 0.0f); | |
| 116 gfx::Point3F vertices_b[4]; | |
| 117 vertices_b[0] = gfx::Point3F(2.0f, 5.0f, 1.0f); | |
| 118 vertices_b[1] = gfx::Point3F(2.0f, -5.0f, 1.0f); | |
| 119 vertices_b[2] = gfx::Point3F(-1.0f, -5.0f, -2.0f); | |
| 120 vertices_b[3] = gfx::Point3F(-1.0f, 5.0f, -2.0f); | |
| 121 | |
| 122 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 4, 0); | |
| 123 CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, 4, 1); | |
| 124 | |
| 125 EXPECT_EQ(DrawPolygon::SideCompare(polygon_a, polygon_b), BSP_SPLIT); | |
| 126 | |
| 127 scoped_ptr<DrawPolygon> front_polygon; | |
| 128 scoped_ptr<DrawPolygon> back_polygon; | |
| 129 polygon_a.Split(polygon_b, &front_polygon, &back_polygon); | |
| 130 EXPECT_EQ(DrawPolygon::SideCompare(*front_polygon, polygon_b), BSP_FRONT); | |
| 131 EXPECT_EQ(DrawPolygon::SideCompare(*back_polygon, polygon_b), BSP_BACK); | |
| 132 | |
| 133 EXPECT_EQ(front_polygon->points.size(), static_cast<unsigned int>(3)); | |
| 134 EXPECT_EQ(back_polygon->points.size(), static_cast<unsigned int>(5)); | |
| 135 } | |
| 136 | |
| 137 // Testing the area calculation on a basic flat 10x10 quad. | |
| 138 TEST(DrawPolygonAreaTest, Basic) { | |
| 139 gfx::Point3F vertices_a[4]; | |
| 140 vertices_a[0] = gfx::Point3F(0.0f, 10.0f, 0.0f); | |
| 141 vertices_a[1] = gfx::Point3F(0.0f, 0.0f, 0.0f); | |
| 142 vertices_a[2] = gfx::Point3F(10.0f, 0.0f, 0.0f); | |
| 143 vertices_a[3] = gfx::Point3F(10.0f, 10.0f, 0.0f); | |
| 144 | |
| 145 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 4, 0); | |
| 146 | |
| 147 EXPECT_EQ(polygon_a.area, 100.0f); | |
| 148 } | |
| 149 | |
| 150 // Test the area of an octagon. It can be seen as a quad with the the corners | |
| 151 // all chopped off, removing 0.5 of the area at each corner from the previous | |
| 152 // test. | |
| 153 TEST(DrawPolygonAreaTest, OctagonArea) { | |
| 154 gfx::Point3F vertices_a[8]; | |
| 155 vertices_a[0] = gfx::Point3F(1.0f, 0.0f, 0.0f); | |
| 156 vertices_a[1] = gfx::Point3F(9.0f, 0.0f, 0.0f); | |
| 157 vertices_a[2] = gfx::Point3F(10.0f, 0.0f, 1.0f); | |
| 158 vertices_a[3] = gfx::Point3F(10.0f, 0.0f, 9.0f); | |
| 159 vertices_a[4] = gfx::Point3F(9.0f, 0.0f, 10.0f); | |
| 160 vertices_a[5] = gfx::Point3F(1.0f, 0.0f, 10.0f); | |
| 161 vertices_a[6] = gfx::Point3F(0.0f, 0.0f, 9.0f); | |
| 162 vertices_a[7] = gfx::Point3F(0.0f, 0.0f, 1.0f); | |
| 163 | |
| 164 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 8, 0); | |
| 165 | |
| 166 EXPECT_EQ(polygon_a.area, 98.0f); | |
| 167 } | |
| 168 | |
| 169 // Same quad as before, but with transformations applied to it. This shows that | |
| 170 // the area calculation works on arbitrarily transformed geometry. | |
| 171 TEST(DrawPolygonAreaTest, TransformedPolygon) { | |
| 172 gfx::Point3F vertices_a[4]; | |
| 173 vertices_a[0] = gfx::Point3F(0.0f, 10.0f, 0.0f); | |
| 174 vertices_a[1] = gfx::Point3F(0.0f, 0.0f, 0.0f); | |
| 175 vertices_a[2] = gfx::Point3F(10.0f, 0.0f, 0.0f); | |
| 176 vertices_a[3] = gfx::Point3F(10.0f, 10.0f, 0.0f); | |
| 177 | |
| 178 gfx::Transform transform; | |
| 179 transform.RotateAboutXAxis(30.0); | |
| 180 transform.RotateAboutYAxis(40.0); | |
| 181 transform.RotateAboutZAxis(50.0); | |
| 182 for (int i = 0; i < 4; i++) { | |
| 183 transform.TransformPoint(&vertices_a[i]); | |
| 184 } | |
| 185 | |
| 186 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 4, 0); | |
| 187 | |
| 188 EXPECT_EQ(polygon_a.area, 100.0f); | |
| 189 } | |
| 190 | |
| 191 } // namespace | |
| 192 } // namespace cc | |
| OLD | NEW |