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

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, 4 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, 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 (unsigned int i = 0; i < points.size(); i++) {
enne (OOO) 2014/07/28 23:11:34 size_t
troyhildebrandt 2014/07/28 23:48:45 Done.
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(), static_cast<unsigned int>(4));
enne (OOO) 2014/07/28 23:11:34 4u, here and elsewhere :( If it's helpful, this p
114 EXPECT_EQ(back_polygon->points().size(), static_cast<unsigned int>(4));
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.707107, 0, -0.707107), 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(), static_cast<unsigned int>(3));
145 EXPECT_EQ(back_polygon->points().size(), static_cast<unsigned int>(5));
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 std::vector<gfx::Point3F> vertices_a;
164 vertices_a.push_back(gfx::Point3F(2.0f, -5.0f, 1.0f));
165 vertices_a.push_back(gfx::Point3F(2.0f, 5.0f, 1.0f));
166 vertices_a.push_back(gfx::Point3F(-1.0f, 5.0f, -2.0f));
167 vertices_a.push_back(gfx::Point3F(-1.0f, -5.0f, -2.0f));
168
169 CREATE_NEW_DRAW_POLYGON(
170 polygon_a, vertices_a, gfx::Vector3dF(0.707107, 0, -0.707107), 0);
171
172 gfx::Transform transform;
173 transform.RotateAboutYAxis(45.0);
174 polygon_a.ApplyTransformToNormal(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
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