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

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

Issue 411793002: DrawPolygon class with Unit Tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added non quad area test 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
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 "cc/quads/draw_polygon.h"
6
7 #include <vector>
8
9 #include "cc/output/bsp_compare_result.h"
10
11 namespace {
12 // This allows for some imperfection in the normal comparison when checking if
13 // two pieces of geometry are coplanar.
14 const float coplanar_dot_epsilon = 0.99f;
15 } // namespace
16
17 namespace cc {
18
19 float DrawPolygon::compare_threshold = 1.0f;
20 float DrawPolygon::split_threshold = 0.5f;
21
22 DrawPolygon::DrawPolygon() {
23 }
24
25 static float SignedArea(const DrawPolygon& polygon) {
26 gfx::Vector3dF total;
27 for (unsigned int i = 0; i < polygon.points.size(); i++) {
28 unsigned int j = (i + 1) % polygon.points.size();
29 gfx::Vector3dF cross_prod =
30 gfx::CrossProduct(gfx::Vector3dF(polygon.points[i].x(),
31 polygon.points[i].y(),
32 polygon.points[i].z()),
33 gfx::Vector3dF(polygon.points[j].x(),
34 polygon.points[j].y(),
35 polygon.points[j].z()));
36 total = total + cross_prod;
37 }
38 return 0.5f * std::abs(gfx::DotProduct(total, polygon.normal));
39 }
40
41 float Area(const DrawPolygon& polygon) {
42 return std::abs(SignedArea(polygon));
43 }
44
45 DrawPolygon::DrawPolygon(DrawQuad* original,
46 gfx::Point3F* in_points,
47 int num_vertices_in_polygon,
48 int draw_order_index)
49 : order_index(draw_order_index), original_ref(original) {
50 for (int i = 0; i < num_vertices_in_polygon; i++) {
51 points.push_back(in_points[i]);
52 }
53
54 if (num_vertices_in_polygon > 2) {
55 gfx::Vector3dF c12 = in_points[1] - in_points[0];
56 gfx::Vector3dF c13 = in_points[2] - in_points[0];
57 normal = gfx::CrossProduct(c12, c13);
58 normal.Scale(1.0f / normal.Length());
59 }
60 area = Area(*this);
61 }
62
63 DrawPolygon::DrawPolygon(const DrawPolygon& other) {
64 CopyFrom(other);
65 }
66
67 DrawPolygon::~DrawPolygon() {
68 }
69
70 DrawPolygon& DrawPolygon::operator=(const DrawPolygon& rhs) {
71 CopyFrom(rhs);
72 return *this;
73 }
74
75 void DrawPolygon::CopyFrom(const DrawPolygon& other) {
76 order_index = other.order_index;
77 original_ref = other.original_ref;
78 points.reserve(other.points.size());
79 points = other.points;
80 normal.set_x(other.normal.x());
81 normal.set_y(other.normal.y());
82 normal.set_z(other.normal.z());
83 area = other.area;
84 }
85
86 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const {
87 return gfx::DotProduct(point - points[0], normal);
88 }
89
90 // Checks whether or not shape a lies on the front or back side of b, or
91 // whether they should be considered coplanar. If on the back side, we
92 // say ABeforeB because it should be drawn in that order.
93 // Assumes that layers are split and there are no intersecting planes.
94 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a,
95 const DrawPolygon& b) {
96 // Right away let's check if they're coplanar
97 double dot = gfx::DotProduct(a.normal, b.normal);
98 float sign;
99 bool normal_match = false;
100 // This check assumes that the normals are normalized.
101 if (std::abs(dot) >= coplanar_dot_epsilon) {
102 normal_match = true;
103 // The normals are matching enough that we only have to test one point.
104 sign = gfx::DotProduct(a.points[0] - b.points[0], b.normal);
105 // Is it on either side of the splitter?
106 if (sign < -compare_threshold) {
107 return BSP_BACK;
108 }
109
110 if (sign > compare_threshold) {
111 return BSP_FRONT;
112 }
113
114 // No it wasn't, so the sign of the dot product of the normals
115 // along with document order determines which side it goes on.
116 if (dot >= 0.0f) {
117 if (a.order_index < b.order_index) {
118 return BSP_COPLANAR_FRONT;
119 }
120 return BSP_COPLANAR_BACK;
121 }
122
123 if (a.order_index < b.order_index) {
124 return BSP_COPLANAR_BACK;
125 }
126 return BSP_COPLANAR_FRONT;
127 }
128
129 unsigned int pos_count = 0;
130 unsigned int neg_count = 0;
131 for (unsigned int i = 0; i < a.points.size(); i++) {
132 if (!normal_match || (normal_match && i > 0)) {
133 sign = gfx::DotProduct(a.points[i] - b.points[0], b.normal);
134 }
135
136 if (sign < -compare_threshold) {
137 ++neg_count;
138 } else if (sign > compare_threshold) {
139 ++pos_count;
140 }
141
142 if (pos_count && neg_count) {
143 return BSP_SPLIT;
144 }
145 }
146
147 if (pos_count) {
148 return BSP_FRONT;
149 }
150 return BSP_BACK;
151 }
152
153 static bool LineIntersectPlane(const gfx::Point3F& line_start,
154 const gfx::Point3F& line_end,
155 const gfx::Point3F& plane_origin,
156 const gfx::Vector3dF& plane_normal,
157 gfx::Point3F* intersection,
158 float distance_threshold) {
159 gfx::Vector3dF vec1 = plane_origin - line_start;
enne (OOO) 2014/07/23 21:19:31 Can you name these better than vec1 and vec2?
troyhildebrandt 2014/07/24 00:43:20 Done.
160 gfx::Vector3dF vec2 = plane_origin - line_end;
161
162 double start_distance = gfx::DotProduct(vec1, plane_normal);
163 double end_distance = gfx::DotProduct(vec2, plane_normal);
164
165 // The case where one vertex lies on the thick-plane and the other
166 // is outside of it.
167 if (std::abs(start_distance) < distance_threshold &&
168 std::abs(end_distance) > distance_threshold) {
169 intersection->SetPoint(line_start.x(), line_start.y(), line_start.z());
170 return true;
171 }
172
173 // This is the case where we clearly cross the thick-plane.
174 if ((start_distance > distance_threshold &&
175 end_distance < -distance_threshold) ||
176 (start_distance < -distance_threshold &&
177 end_distance > distance_threshold)) {
178 gfx::Vector3dF v = line_end - line_start;
179
180 v.Scale(1.f / v.Length());
enne (OOO) 2014/07/23 21:19:32 Can you leave a comment about what this is all doi
troyhildebrandt 2014/07/24 00:43:21 Done.
181 double projected_length = gfx::DotProduct(v, plane_normal);
182 if (!projected_length)
183 return false;
184
185 double scale = start_distance / projected_length;
186 intersection->SetPoint(line_start.x() + (v.x() * scale),
187 line_start.y() + (v.y() * scale),
188 line_start.z() + (v.z() * scale));
189
190 return true;
191 }
192 return false;
193 }
194
195 void DrawPolygon::ApplyTransform(const gfx::Transform& transform) {
196 bool clipped = false;
197 for (unsigned int i = 0; i < points.size(); i++) {
198 points[i] = MathUtil::MapPoint(transform, points[i], &clipped);
199 }
200 }
201
202 bool DrawPolygon::Split(const DrawPolygon& splitter,
203 scoped_ptr<DrawPolygon>* front,
204 scoped_ptr<DrawPolygon>* back) {
205 gfx::Point3F intersections[2];
206 std::vector<gfx::Point3F> out_points[2];
207 int vertex_before[2];
208 int points_size = points.size();
209 int current_intersection = 0;
210
211 int current_vertex = 0;
212 while (current_intersection < 2) {
213 if (current_vertex++ > points_size) {
214 break;
215 }
216 if (current_intersection > 0 &&
217 vertex_before[0] == (current_vertex % points_size)) {
218 continue;
219 }
220
221 if (LineIntersectPlane(points[(current_vertex % points_size)],
222 points[(current_vertex + 1) % points_size],
223 splitter.points[0],
224 splitter.normal,
225 &intersections[current_intersection],
226 split_threshold)) {
227 vertex_before[current_intersection] = current_vertex % points_size;
228 current_intersection++;
229 // We found both intersection points so we're done already.
230 if (current_intersection == 2) {
231 break;
232 }
233 }
234 }
235 if (current_intersection < 2) {
236 return false;
237 }
238
239 // Since we found both the intersection points, we can begin building the
240 // vertex set for both our new polygons.
241 int start1 = (vertex_before[0] + 1) % points_size;
242 int start2 = (vertex_before[1] + 1) % points_size;
243 int points_remaining = points_size;
244
245 // First polygon.
246 out_points[0].push_back(intersections[0]);
247 for (int i = start1; i <= vertex_before[1]; i++) {
248 out_points[0].push_back(points[i]);
249 --points_remaining;
250 }
251 out_points[0].push_back(intersections[1]);
252
253 // Second polygon.
254 out_points[1].push_back(intersections[1]);
255 int index = start2;
256 for (int i = 0; i < points_remaining; i++) {
257 out_points[1].push_back(points[index % points_size]);
258 ++index;
259 }
260 out_points[1].push_back(intersections[0]);
261
262 // Give both polygons the original splitting polygon's ID, so that they'll
263 // still be sorted properly in co-planar instances.
264 // Send false as last parameter for is_original because they're split.
265 scoped_ptr<DrawPolygon> poly1(new DrawPolygon(original_ref,
266 &(out_points[0][0]),
267 out_points[0].size(),
268 this->order_index));
269 scoped_ptr<DrawPolygon> poly2(new DrawPolygon(original_ref,
270 &(out_points[1][0]),
271 out_points[1].size(),
272 this->order_index));
273
274 if (SideCompare(*poly1, splitter) == BSP_FRONT) {
275 *front = poly1.Pass();
276 *back = poly2.Pass();
277 } else {
278 *front = poly2.Pass();
279 *back = poly1.Pass();
280 }
281 return true;
282 }
283
284 void DrawPolygon::ToQuads2D(std::vector<gfx::QuadF>* quads) const {
285 if (points.size() == 0)
enne (OOO) 2014/07/23 21:19:31 Should this be points.size() <= 2?
troyhildebrandt 2014/07/24 00:43:21 Done.
286 return;
287
288 // op1 = offset plus 1, op2 = offset plus 2.
enne (OOO) 2014/07/23 21:19:31 This comment isn't particularly helpful. Maybe in
troyhildebrandt 2014/07/24 00:43:20 Done.
289 gfx::PointF first(points[0].x(), points[0].y());
290 unsigned int offset = 1;
291 while (offset < points.size() - 1) {
292 unsigned int op1 = offset + 1;
293 unsigned int op2 = offset + 2;
294 if (op2 >= points.size()) {
295 // It's going to be a degenerate triangle.
296 op2 = op1;
297 }
298 quads->push_back(
299 gfx::QuadF(first,
300 gfx::PointF(points[offset].x(), points[offset].y()),
301 gfx::PointF(points[op1].x(), points[op1].y()),
302 gfx::PointF(points[op2].x(), points[op2].y())));
303 offset = op2;
304 }
305 }
306
307 bool DrawPolygon::GetInverseTransform(gfx::Transform* transform) const {
308 return original_ref->quadTransform().GetInverse(transform);
309 }
310
311 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698