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

Side by Side Diff: cc/quads/draw_polygon_unittest.cc

Issue 411793002: DrawPolygon class with Unit Tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 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/quads/draw_polygon.cc ('K') | « cc/quads/draw_polygon.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
(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, polygon_id) \
16 DrawPolygon name(NULL, points_vector, 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 (unsigned int 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(polygon_a, vertices_a, 0);
45 CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, 1);
46
47 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_FRONT);
48 }
49
50 // One quad is resting against another, but doesn't cross its plane so no split
51 // should occur.
52 TEST(DrawPolygonSplitTest, BarelyTouchingNoSplit) {
53 std::vector<gfx::Point3F> vertices_a;
54 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
55 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
56 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
57 vertices_a.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
58 std::vector<gfx::Point3F> vertices_b;
59 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, 0.0f));
60 vertices_b.push_back(gfx::Point3F(5.0f, 0.0f, -10.0f));
61 vertices_b.push_back(gfx::Point3F(5.0f, 0.0f, -10.0f));
62 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, 0.0f));
63
64 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 0);
65 CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, 1);
66
67 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_BACK);
68 }
69
70 // One quad intersects another and becomes two pieces.
71 TEST(DrawPolygonSplitTest, BasicSplit) {
72 std::vector<gfx::Point3F> vertices_a;
73 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
74 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
75 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
76 vertices_a.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
77 std::vector<gfx::Point3F> vertices_b;
78 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, -5.0f));
79 vertices_b.push_back(gfx::Point3F(5.0f, 0.0f, -5.0f));
80 vertices_b.push_back(gfx::Point3F(5.0f, 0.0f, 5.0f));
81 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, 5.0f));
82
83 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 0);
84 CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, 1);
85
86 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_SPLIT);
87
88 scoped_ptr<DrawPolygon> front_polygon;
89 scoped_ptr<DrawPolygon> back_polygon;
90 polygon_b.Split(polygon_a, &front_polygon, &back_polygon);
91 EXPECT_EQ(DrawPolygon::SideCompare(*front_polygon, polygon_a), BSP_FRONT);
92 EXPECT_EQ(DrawPolygon::SideCompare(*back_polygon, polygon_a), BSP_BACK);
93
94 std::vector<gfx::Point3F> test_points_a;
95 test_points_a.push_back(gfx::Point3F(5.0f, 0.0f, 0.0f));
96 test_points_a.push_back(gfx::Point3F(5.0f, 0.0f, 5.0f));
97 test_points_a.push_back(gfx::Point3F(5.0f, 10.0f, 5.0f));
98 test_points_a.push_back(gfx::Point3F(5.0f, 10.0f, 0.0f));
99 std::vector<gfx::Point3F> test_points_b;
100 test_points_b.push_back(gfx::Point3F(5.0f, 10.0f, 0.0f));
101 test_points_b.push_back(gfx::Point3F(5.0f, 10.0f, -5.0f));
102 test_points_b.push_back(gfx::Point3F(5.0f, 0.0f, -5.0f));
103 test_points_b.push_back(gfx::Point3F(5.0f, 0.0f, 0.0f));
104 ValidatePoints(*(front_polygon.get()), test_points_a);
105 ValidatePoints(*(back_polygon.get()), test_points_b);
106
107 EXPECT_EQ(front_polygon->points.size(), static_cast<unsigned int>(4));
108 EXPECT_EQ(back_polygon->points.size(), static_cast<unsigned int>(4));
109 }
110
111 // A "wave" shape of points that do not make up those of a convex polygon
112 // crossing the boundaries of another polygon. This should not cause a problem
113 // and should still result in two different pieces of geometry on either side.
114 TEST(DrawPolygonSplitTest, NonConvexSplit) {
115 std::vector<gfx::Point3F> vertices_a;
116 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, -5.0f));
117 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, -5.0f));
118 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 5.0f));
119 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, 5.0f));
120 std::vector<gfx::Point3F> vertices_b;
121 vertices_b.push_back(gfx::Point3F(-3.0f, 10.0f, 0.0f));
122 vertices_b.push_back(gfx::Point3F(-1.0f, 7.5f, 0.0f));
123 vertices_b.push_back(gfx::Point3F(1.0f, 5.0f, 0.0f));
124 vertices_b.push_back(gfx::Point3F(3.0f, 7.5f, 0.0f));
125 vertices_b.push_back(gfx::Point3F(5.0f, 10.0f, 0.0f));
126 vertices_b.push_back(gfx::Point3F(7.0f, 7.5f, 0.0f));
127
128 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 0);
129 CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, 1);
130
131 EXPECT_EQ(DrawPolygon::SideCompare(polygon_b, polygon_a), BSP_SPLIT);
132
133 scoped_ptr<DrawPolygon> front_polygon;
134 scoped_ptr<DrawPolygon> back_polygon;
135 polygon_b.Split(polygon_a, &front_polygon, &back_polygon);
136 EXPECT_EQ(DrawPolygon::SideCompare(*front_polygon, polygon_a), BSP_FRONT);
137 EXPECT_EQ(DrawPolygon::SideCompare(*back_polygon, polygon_a), BSP_BACK);
138
139 std::vector<gfx::Point3F> test_points_a;
140 test_points_a.push_back(gfx::Point3F(0.0f, 9.25f, 0.0f));
141 test_points_a.push_back(gfx::Point3F(-3.0f, 10.0f, 0.0f));
142 test_points_a.push_back(gfx::Point3F(-1.0f, 7.5f, 0.0f));
143 test_points_a.push_back(gfx::Point3F(0.0f, 6.25f, 0.0f));
144 std::vector<gfx::Point3F> test_points_b;
145 test_points_b.push_back(gfx::Point3F(0.0f, 6.25f, 0.0f));
146 test_points_b.push_back(gfx::Point3F(1.0f, 5.0f, 0.0f));
147 test_points_b.push_back(gfx::Point3F(3.0f, 7.5f, 0.0f));
148 test_points_b.push_back(gfx::Point3F(5.0f, 10.0f, 0.0f));
149 test_points_b.push_back(gfx::Point3F(7.0f, 7.5f, 0.0f));
150 test_points_b.push_back(gfx::Point3F(0.0f, 9.25f, 0.0f));
151
152 ValidatePoints(*(front_polygon.get()), test_points_a);
153 ValidatePoints(*(back_polygon.get()), test_points_b);
154 }
155
156 // In this test we cut the corner of a quad so that it creates a triangle and
157 // a pentagon as a result.
158 TEST(DrawPolygonSplitTest, AngledSplit) {
159 std::vector<gfx::Point3F> vertices_a;
160 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
161 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 10.0f));
162 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 10.0f));
163 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
164 std::vector<gfx::Point3F> vertices_b;
165 vertices_b.push_back(gfx::Point3F(2.0f, 5.0f, 1.0f));
166 vertices_b.push_back(gfx::Point3F(2.0f, -5.0f, 1.0f));
167 vertices_b.push_back(gfx::Point3F(-1.0f, -5.0f, -2.0f));
168 vertices_b.push_back(gfx::Point3F(-1.0f, 5.0f, -2.0f));
169
170 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 0);
171 CREATE_NEW_DRAW_POLYGON(polygon_b, vertices_b, 1);
172
173 EXPECT_EQ(DrawPolygon::SideCompare(polygon_a, polygon_b), BSP_SPLIT);
174
175 scoped_ptr<DrawPolygon> front_polygon;
176 scoped_ptr<DrawPolygon> back_polygon;
177 polygon_a.Split(polygon_b, &front_polygon, &back_polygon);
178 EXPECT_EQ(DrawPolygon::SideCompare(*front_polygon, polygon_b), BSP_FRONT);
179 EXPECT_EQ(DrawPolygon::SideCompare(*back_polygon, polygon_b), BSP_BACK);
180
181 EXPECT_EQ(front_polygon->points.size(), static_cast<unsigned int>(3));
182 EXPECT_EQ(back_polygon->points.size(), static_cast<unsigned int>(5));
183
184 std::vector<gfx::Point3F> test_points_a;
185 test_points_a.push_back(gfx::Point3F(10.0f, 0.0f, 9.0f));
186 test_points_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
187 test_points_a.push_back(gfx::Point3F(1.0f, 0.0f, 0.0f));
188 std::vector<gfx::Point3F> test_points_b;
189 test_points_b.push_back(gfx::Point3F(1.0f, 0.0f, 0.0f));
190 test_points_b.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
191 test_points_b.push_back(gfx::Point3F(0.0f, 0.0f, 10.0f));
192 test_points_b.push_back(gfx::Point3F(10.0f, 0.0f, 10.0f));
193 test_points_b.push_back(gfx::Point3F(10.0f, 0.0f, 9.0f));
194
195 ValidatePoints(*(front_polygon.get()), test_points_a);
196 ValidatePoints(*(back_polygon.get()), test_points_b);
197 }
198
199 // Testing the area calculation on a basic flat 10x10 quad.
200 TEST(DrawPolygonAreaTest, Basic) {
201 std::vector<gfx::Point3F> vertices_a;
202 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
203 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
204 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
205 vertices_a.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
206
207 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 0);
208
209 EXPECT_EQ(polygon_a.area, 100.0f);
210 }
211
212 // Test the area of an octagon. It can be seen as a quad with the the corners
213 // all chopped off, removing 0.5 of the area at each corner from the previous
214 // test.
215 TEST(DrawPolygonAreaTest, OctagonArea) {
216 std::vector<gfx::Point3F> vertices_a;
217 vertices_a.push_back(gfx::Point3F(1.0f, 0.0f, 0.0f));
218 vertices_a.push_back(gfx::Point3F(9.0f, 0.0f, 0.0f));
219 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 1.0f));
220 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 9.0f));
221 vertices_a.push_back(gfx::Point3F(9.0f, 0.0f, 10.0f));
222 vertices_a.push_back(gfx::Point3F(1.0f, 0.0f, 10.0f));
223 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 9.0f));
224 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 1.0f));
225
226 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 0);
227
228 EXPECT_EQ(polygon_a.area, 98.0f);
229 }
230
231 // Same quad as before, but with transformations applied to it. This shows that
232 // the area calculation works on arbitrarily transformed geometry.
233 TEST(DrawPolygonAreaTest, TransformedPolygon) {
234 std::vector<gfx::Point3F> vertices_a;
235 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
236 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
237 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
238 vertices_a.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
239
240 gfx::Transform transform;
241 transform.RotateAboutXAxis(30.0);
242 transform.RotateAboutYAxis(40.0);
243 transform.RotateAboutZAxis(50.0);
244 for (int i = 0; i < 4; i++) {
245 transform.TransformPoint(&vertices_a[i]);
246 }
247
248 CREATE_NEW_DRAW_POLYGON(polygon_a, vertices_a, 0);
249
250 EXPECT_EQ(polygon_a.area, 100.0f);
251 }
252
253 } // namespace
254 } // namespace cc
OLDNEW
« cc/quads/draw_polygon.cc ('K') | « cc/quads/draw_polygon.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698