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

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
enne (OOO) 2014/07/28 23:11:34 What are these units in? (both compare and split t
troyhildebrandt 2014/07/28 23:48:45 I believe they're in device pixels, if that's what
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 unsigned int pos_count = 0;
enne (OOO) 2014/07/28 23:11:34 unsigned int => int
troyhildebrandt 2014/07/28 23:48:45 Done.
107 unsigned 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 bool DrawPolygon::Split(const DrawPolygon& splitter,
199 scoped_ptr<DrawPolygon>* front,
200 scoped_ptr<DrawPolygon>* back) {
201 gfx::Point3F intersections[2];
202 std::vector<gfx::Point3F> out_points[2];
203 // vertex_before stores the index of the vertex before its matching
204 // intersection.
205 // i.e. vertex_before[0] stores the vertex we saw before we crossed the plane
206 // which resulted in the line/plane intersection giving us intersections[0].
207 size_t vertex_before[2];
208 size_t points_size = points_.size();
209 size_t current_intersection = 0;
210
211 size_t current_vertex = 0;
212 // We will only have two intersection points because we assume all polygons
213 // are convex.
214 while (current_intersection < 2) {
215 if (LineIntersectPlane(points_[(current_vertex % points_size)],
216 points_[(current_vertex + 1) % points_size],
217 splitter.points_[0],
218 splitter.normal_,
219 &intersections[current_intersection],
220 split_threshold)) {
221 vertex_before[current_intersection] = current_vertex % points_size;
222 current_intersection++;
223 // We found both intersection points so we're done already.
224 if (current_intersection == 2) {
225 break;
226 }
227 }
228 if (current_vertex++ > points_size) {
229 break;
230 }
231 }
232 DCHECK_EQ(current_intersection, static_cast<size_t>(2));
233
234 // Since we found both the intersection points, we can begin building the
235 // vertex set for both our new polygons.
236 size_t start1 = (vertex_before[0] + 1) % points_size;
237 size_t start2 = (vertex_before[1] + 1) % points_size;
238 size_t points_remaining = points_size;
239
240 // First polygon.
241 out_points[0].push_back(intersections[0]);
242 for (size_t i = start1; i <= vertex_before[1]; i++) {
243 out_points[0].push_back(points_[i]);
244 --points_remaining;
245 }
246 out_points[0].push_back(intersections[1]);
247
248 // Second polygon.
249 out_points[1].push_back(intersections[1]);
250 size_t index = start2;
251 for (unsigned int i = 0; i < points_remaining; i++) {
enne (OOO) 2014/07/28 23:11:34 size_t
troyhildebrandt 2014/07/28 23:48:45 Done.
252 out_points[1].push_back(points_[index % points_size]);
253 ++index;
254 }
255 out_points[1].push_back(intersections[0]);
256
257 // Give both polygons the original splitting polygon's ID, so that they'll
258 // still be sorted properly in co-planar instances.
259 scoped_ptr<DrawPolygon> poly1(
260 new DrawPolygon(original_ref_, out_points[0], normal_, order_index_));
261 scoped_ptr<DrawPolygon> poly2(
262 new DrawPolygon(original_ref_, out_points[1], normal_, order_index_));
263
264 if (SideCompare(*poly1, splitter) == BSP_FRONT) {
265 *front = poly1.Pass();
266 *back = poly2.Pass();
267 } else {
268 *front = poly2.Pass();
269 *back = poly1.Pass();
270 }
271 return true;
272 }
273
274 // This algorithm takes the first vertex in the polygon and uses that as a
275 // pivot point to fan out and create quads from the rest of the vertices.
276 // |offset| starts off as the second vertex, and then |op1| and |op2| indicate
277 // offset+1 and offset+2 respectively.
278 // After the first quad is created, the first vertex in the next quad is the
279 // same as all the rest, the pivot point. The second vertex in the next quad is
280 // the old |op2|, the last vertex added to the previous quad. This continues
281 // until all points are exhausted.
282 // The special case here is where there are only 3 points remaining, in which
283 // case we use the same values for vertex 3 and 4 to make a degenerate quad
284 // that represents a triangle.
285 void DrawPolygon::ToQuads2D(std::vector<gfx::QuadF>* quads) const {
286 if (points_.size() <= 2)
287 return;
288
289 gfx::PointF first(points_[0].x(), points_[0].y());
290 size_t offset = 1;
291 while (offset < points_.size() - 1) {
292 size_t op1 = offset + 1;
293 size_t 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 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698