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

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: 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
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 static const float coplanar_dot_epsilon = 0.01f;
15 // This threshold controls how "thick" a plane is. If a point's distance is
16 // <= |compare_threshold|, then it is considered on the plane. Only when this
17 // boundary is crossed do we consider doing splitting.
18 static const float compare_threshold = 1.0f;
19 // |split_threshold| is lower in this case because we want the points created
20 // during splitting to be well within the range of |compare_threshold| for
21 // comparison purposes. The splitting operation will produce intersection points
22 // that fit within a tighter distance to the splitting plane as a result of this
23 // value. By using a value >= |compare_threshold| we run the risk of creating
24 // points that SHOULD be intersecting the "thick plane", but actually fail to
25 // test positively for it because |split_threshold| allowed them to be outside
26 // this range.
27 static const float split_threshold = 0.5f;
28 } // namespace
29
30 namespace cc {
31
32 gfx::Vector3dF DrawPolygon::default_normal = gfx::Vector3dF(0.0f, 0.0f, -1.0f);
33
34 DrawPolygon::DrawPolygon() {
35 }
36
37 DrawPolygon::DrawPolygon(DrawQuad* original,
38 const std::vector<gfx::Point3F>& in_points,
39 const gfx::Vector3dF& normal,
40 int draw_order_index)
41 : order_index_(draw_order_index), original_ref_(original) {
42 for (size_t i = 0; i < in_points.size(); i++) {
43 points_.push_back(in_points[i]);
44 }
45 normal_ = normal;
46 }
47
48 DrawPolygon::~DrawPolygon() {
49 }
50
51 scoped_ptr<DrawPolygon> DrawPolygon::CreateCopy() {
52 DrawPolygon* new_polygon = new DrawPolygon();
53 new_polygon->order_index_ = order_index_;
54 new_polygon->original_ref_ = original_ref_;
55 new_polygon->points_.reserve(points_.size());
56 new_polygon->points_ = points_;
57 new_polygon->normal_.set_x(normal_.x());
58 new_polygon->normal_.set_y(normal_.y());
59 new_polygon->normal_.set_z(normal_.z());
60 return scoped_ptr<DrawPolygon>(new_polygon);
61 }
62
63 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const {
64 return gfx::DotProduct(point - points_[0], normal_);
65 }
66
67 // Checks whether or not shape a lies on the front or back side of b, or
68 // whether they should be considered coplanar. If on the back side, we
69 // say ABeforeB because it should be drawn in that order.
70 // Assumes that layers are split and there are no intersecting planes.
71 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a,
72 const DrawPolygon& b) {
73 // Right away let's check if they're coplanar
74 double dot = gfx::DotProduct(a.normal_, b.normal_);
75 float sign;
76 bool normal_match = false;
77 // This check assumes that the normals are normalized.
78 if (std::abs(dot) >= 1.0f - coplanar_dot_epsilon) {
79 normal_match = true;
80 // The normals are matching enough that we only have to test one point.
81 sign = gfx::DotProduct(a.points_[0] - b.points_[0], b.normal_);
82 // Is it on either side of the splitter?
83 if (sign < -compare_threshold) {
84 return BSP_BACK;
85 }
86
87 if (sign > compare_threshold) {
88 return BSP_FRONT;
89 }
90
91 // No it wasn't, so the sign of the dot product of the normals
92 // along with document order determines which side it goes on.
93 if (dot >= 0.0f) {
94 if (a.order_index_ < b.order_index_) {
95 return BSP_COPLANAR_FRONT;
96 }
97 return BSP_COPLANAR_BACK;
98 }
99
100 if (a.order_index_ < b.order_index_) {
101 return BSP_COPLANAR_BACK;
102 }
103 return BSP_COPLANAR_FRONT;
104 }
105
106 int pos_count = 0;
107 int neg_count = 0;
108 for (size_t i = 0; i < a.points_.size(); i++) {
109 if (!normal_match || (normal_match && i > 0)) {
110 sign = gfx::DotProduct(a.points_[i] - b.points_[0], b.normal_);
111 }
112
113 if (sign < -compare_threshold) {
114 ++neg_count;
115 } else if (sign > compare_threshold) {
116 ++pos_count;
117 }
118
119 if (pos_count && neg_count) {
120 return BSP_SPLIT;
121 }
122 }
123
124 if (pos_count) {
125 return BSP_FRONT;
126 }
127 return BSP_BACK;
128 }
129
130 static bool LineIntersectPlane(const gfx::Point3F& line_start,
131 const gfx::Point3F& line_end,
132 const gfx::Point3F& plane_origin,
133 const gfx::Vector3dF& plane_normal,
134 gfx::Point3F* intersection,
135 float distance_threshold) {
136 gfx::Vector3dF start_to_origin_vector = plane_origin - line_start;
137 gfx::Vector3dF end_to_origin_vector = plane_origin - line_end;
138
139 double start_distance = gfx::DotProduct(start_to_origin_vector, plane_normal);
140 double end_distance = gfx::DotProduct(end_to_origin_vector, plane_normal);
141
142 // The case where one vertex lies on the thick-plane and the other
143 // is outside of it.
144 if (std::abs(start_distance) < distance_threshold &&
145 std::abs(end_distance) > distance_threshold) {
146 intersection->SetPoint(line_start.x(), line_start.y(), line_start.z());
147 return true;
148 }
149
150 // This is the case where we clearly cross the thick-plane.
151 if ((start_distance > distance_threshold &&
152 end_distance < -distance_threshold) ||
153 (start_distance < -distance_threshold &&
154 end_distance > distance_threshold)) {
155 gfx::Vector3dF v = line_end - line_start;
156 float total_distance = std::abs(start_distance) + std::abs(end_distance);
157 float lerp_factor = std::abs(start_distance) / total_distance;
158
159 intersection->SetPoint(line_start.x() + (v.x() * lerp_factor),
160 line_start.y() + (v.y() * lerp_factor),
161 line_start.z() + (v.z() * lerp_factor));
162
163 return true;
164 }
165 return false;
166 }
167
168 // This function is separate from ApplyTransform because it is often unnecessary
169 // to transform the normal with the rest of the polygon.
170 // When drawing these polygons, it is necessary to move them back into layer
171 // space before sending them to OpenGL, which requires using ApplyTransform,
172 // but normal information is no longer needed after sorting.
173 void DrawPolygon::ApplyTransformToNormal(const gfx::Transform& transform) {
174 // Now we use the inverse transpose of |transform| to transform the normal.
175 gfx::Transform inverse_transform;
176 bool inverted = transform.GetInverse(&inverse_transform);
177 DCHECK(inverted);
178 if (!inverted)
179 return;
180 inverse_transform.Transpose();
181
182 gfx::Point3F new_normal(normal_.x(), normal_.y(), normal_.z());
183 inverse_transform.TransformPoint(&new_normal);
184 // Make sure our normal is still normalized.
185 normal_ = gfx::Vector3dF(new_normal.x(), new_normal.y(), new_normal.z());
186 float normal_magnitude = normal_.Length();
187 if (normal_magnitude != 0 && normal_magnitude != 1) {
188 normal_.Scale(1.0f / normal_magnitude);
189 }
190 }
191
192 void DrawPolygon::ApplyTransform(const gfx::Transform& transform) {
193 for (size_t i = 0; i < points_.size(); i++) {
194 transform.TransformPoint(&points_[i]);
195 }
196 }
197
198 // TransformToScreenSpace assumes we're moving a layer from its layer space
199 // into 3D screen space, which for sorting purposes requires the normal to
200 // be transformed along with the vertices.
201 void DrawPolygon::TransformToScreenSpace(const gfx::Transform& transform) {
202 ApplyTransform(transform);
203 ApplyTransformToNormal(transform);
204 }
205
206 // In the case of TransformToLayerSpace, we assume that we are giving the
207 // inverse transformation back to the polygon to move it back into layer space
208 // but we can ignore the costly process of applying the inverse to the normal
209 // since we know the normal will just reset to its original state.
210 void DrawPolygon::TransformToLayerSpace(
211 const gfx::Transform& inverse_transform) {
212 ApplyTransform(inverse_transform);
213 normal_ = gfx::Vector3dF(0.0f, 0.0f, -1.0f);
214 }
215
216 bool DrawPolygon::Split(const DrawPolygon& splitter,
217 scoped_ptr<DrawPolygon>* front,
218 scoped_ptr<DrawPolygon>* back) {
219 gfx::Point3F intersections[2];
220 std::vector<gfx::Point3F> out_points[2];
221 // vertex_before stores the index of the vertex before its matching
222 // intersection.
223 // i.e. vertex_before[0] stores the vertex we saw before we crossed the plane
224 // which resulted in the line/plane intersection giving us intersections[0].
225 size_t vertex_before[2];
226 size_t points_size = points_.size();
227 size_t current_intersection = 0;
228
229 size_t current_vertex = 0;
230 // We will only have two intersection points because we assume all polygons
231 // are convex.
232 while (current_intersection < 2) {
233 if (LineIntersectPlane(points_[(current_vertex % points_size)],
234 points_[(current_vertex + 1) % points_size],
235 splitter.points_[0],
236 splitter.normal_,
237 &intersections[current_intersection],
238 split_threshold)) {
239 vertex_before[current_intersection] = current_vertex % points_size;
240 current_intersection++;
241 // We found both intersection points so we're done already.
242 if (current_intersection == 2) {
243 break;
244 }
245 }
246 if (current_vertex++ > points_size) {
247 break;
248 }
249 }
250 DCHECK_EQ(current_intersection, static_cast<size_t>(2));
251
252 // Since we found both the intersection points, we can begin building the
253 // vertex set for both our new polygons.
254 size_t start1 = (vertex_before[0] + 1) % points_size;
255 size_t start2 = (vertex_before[1] + 1) % points_size;
256 size_t points_remaining = points_size;
257
258 // First polygon.
259 out_points[0].push_back(intersections[0]);
260 for (size_t i = start1; i <= vertex_before[1]; i++) {
261 out_points[0].push_back(points_[i]);
262 --points_remaining;
263 }
264 out_points[0].push_back(intersections[1]);
265
266 // Second polygon.
267 out_points[1].push_back(intersections[1]);
268 size_t index = start2;
269 for (size_t i = 0; i < points_remaining; i++) {
270 out_points[1].push_back(points_[index % points_size]);
271 ++index;
272 }
273 out_points[1].push_back(intersections[0]);
274
275 // Give both polygons the original splitting polygon's ID, so that they'll
276 // still be sorted properly in co-planar instances.
277 scoped_ptr<DrawPolygon> poly1(
278 new DrawPolygon(original_ref_, out_points[0], normal_, order_index_));
279 scoped_ptr<DrawPolygon> poly2(
280 new DrawPolygon(original_ref_, out_points[1], normal_, order_index_));
281
282 if (SideCompare(*poly1, splitter) == BSP_FRONT) {
283 *front = poly1.Pass();
284 *back = poly2.Pass();
285 } else {
286 *front = poly2.Pass();
287 *back = poly1.Pass();
288 }
289 return true;
290 }
291
292 // This algorithm takes the first vertex in the polygon and uses that as a
293 // pivot point to fan out and create quads from the rest of the vertices.
294 // |offset| starts off as the second vertex, and then |op1| and |op2| indicate
295 // offset+1 and offset+2 respectively.
296 // After the first quad is created, the first vertex in the next quad is the
297 // same as all the rest, the pivot point. The second vertex in the next quad is
298 // the old |op2|, the last vertex added to the previous quad. This continues
299 // until all points are exhausted.
300 // The special case here is where there are only 3 points remaining, in which
301 // case we use the same values for vertex 3 and 4 to make a degenerate quad
302 // that represents a triangle.
303 void DrawPolygon::ToQuads2D(std::vector<gfx::QuadF>* quads) const {
304 if (points_.size() <= 2)
305 return;
306
307 gfx::PointF first(points_[0].x(), points_[0].y());
308 size_t offset = 1;
309 while (offset < points_.size() - 1) {
310 size_t op1 = offset + 1;
311 size_t op2 = offset + 2;
312 if (op2 >= points_.size()) {
313 // It's going to be a degenerate triangle.
314 op2 = op1;
315 }
316 quads->push_back(
317 gfx::QuadF(first,
318 gfx::PointF(points_[offset].x(), points_[offset].y()),
319 gfx::PointF(points_[op1].x(), points_[op1].y()),
320 gfx::PointF(points_[op2].x(), points_[op2].y())));
321 offset = op2;
322 }
323 }
324
325 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698