Chromium Code Reviews| OLD | NEW |
|---|---|
| (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.99f; | |
| 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 static const float split_threshold = 0.5f; | |
| 20 } // namespace | |
| 21 | |
| 22 namespace cc { | |
| 23 | |
| 24 DrawPolygon::DrawPolygon() { | |
| 25 } | |
| 26 | |
| 27 DrawPolygon::DrawPolygon(DrawQuad* original, | |
| 28 const std::vector<gfx::Point3F>& in_points, | |
| 29 int draw_order_index) | |
| 30 : order_index_(draw_order_index), original_ref_(original) { | |
| 31 for (unsigned int i = 0; i < in_points.size(); i++) { | |
| 32 points_.push_back(in_points[i]); | |
| 33 } | |
| 34 normal_ = gfx::Vector3dF(0.0f, 0.0f, 1.0f); | |
| 35 } | |
| 36 | |
| 37 DrawPolygon::~DrawPolygon() { | |
| 38 } | |
| 39 | |
| 40 void DrawPolygon::SetNormal(const gfx::Vector3dF& normal) { | |
| 41 normal_ = normal; | |
| 42 } | |
| 43 | |
| 44 scoped_ptr<DrawPolygon> DrawPolygon::CreateCopy() { | |
| 45 DrawPolygon* new_polygon = new DrawPolygon(); | |
| 46 new_polygon->order_index_ = order_index_; | |
| 47 new_polygon->original_ref_ = original_ref_; | |
| 48 new_polygon->points_.reserve(points_.size()); | |
| 49 new_polygon->points_ = points_; | |
| 50 new_polygon->normal_.set_x(normal_.x()); | |
| 51 new_polygon->normal_.set_y(normal_.y()); | |
| 52 new_polygon->normal_.set_z(normal_.z()); | |
| 53 return scoped_ptr<DrawPolygon>(new_polygon); | |
| 54 } | |
| 55 | |
| 56 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const { | |
| 57 return gfx::DotProduct(point - points_[0], normal_); | |
| 58 } | |
| 59 | |
| 60 // Checks whether or not shape a lies on the front or back side of b, or | |
| 61 // whether they should be considered coplanar. If on the back side, we | |
| 62 // say ABeforeB because it should be drawn in that order. | |
| 63 // Assumes that layers are split and there are no intersecting planes. | |
| 64 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a, | |
| 65 const DrawPolygon& b) { | |
| 66 // Right away let's check if they're coplanar | |
| 67 double dot = gfx::DotProduct(a.normal_, b.normal_); | |
|
Ian Vollick
2014/07/25 15:52:13
Do we need to ensure that apply normal has been ca
troyhildebrandt
2014/07/25 20:37:47
Applying the transformation to the normal happens
Ian Vollick
2014/07/26 01:24:37
K. Thanks for the explanation.
| |
| 68 float sign; | |
| 69 bool normal_match = false; | |
| 70 // This check assumes that the normals are normalized. | |
| 71 if (std::abs(dot) >= coplanar_dot_epsilon) { | |
|
Ian Vollick
2014/07/25 15:52:13
It's a little odd to have epsilon be .99 rather th
troyhildebrandt
2014/07/25 20:37:47
Done.
| |
| 72 normal_match = true; | |
| 73 // The normals are matching enough that we only have to test one point. | |
| 74 sign = gfx::DotProduct(a.points_[0] - b.points_[0], b.normal_); | |
|
Ian Vollick
2014/07/25 15:52:13
What if a.points_[0] == b.points_[0] ?
troyhildebrandt
2014/07/25 20:37:47
We end up with the dot product resulting in 0, whi
Ian Vollick
2014/07/26 01:24:37
I'm not convinced that this is always ok. Two poly
troyhildebrandt
2014/07/28 17:39:37
The std::abs(dot) >= coplanar_dot_epsilon dot prod
Ian Vollick
2014/07/28 19:39:51
Oh right (facepalm). Thanks for catching me up :)
| |
| 75 // Is it on either side of the splitter? | |
| 76 if (sign < -compare_threshold) { | |
| 77 return BSP_BACK; | |
| 78 } | |
| 79 | |
| 80 if (sign > compare_threshold) { | |
| 81 return BSP_FRONT; | |
| 82 } | |
| 83 | |
| 84 // No it wasn't, so the sign of the dot product of the normals | |
| 85 // along with document order determines which side it goes on. | |
| 86 if (dot >= 0.0f) { | |
| 87 if (a.order_index_ < b.order_index_) { | |
| 88 return BSP_COPLANAR_FRONT; | |
| 89 } | |
| 90 return BSP_COPLANAR_BACK; | |
| 91 } | |
| 92 | |
| 93 if (a.order_index_ < b.order_index_) { | |
| 94 return BSP_COPLANAR_BACK; | |
| 95 } | |
| 96 return BSP_COPLANAR_FRONT; | |
| 97 } | |
| 98 | |
| 99 unsigned int pos_count = 0; | |
| 100 unsigned int neg_count = 0; | |
| 101 for (unsigned int i = 0; i < a.points_.size(); i++) { | |
| 102 if (!normal_match || (normal_match && i > 0)) { | |
| 103 sign = gfx::DotProduct(a.points_[i] - b.points_[0], b.normal_); | |
| 104 } | |
| 105 | |
| 106 if (sign < -compare_threshold) { | |
| 107 ++neg_count; | |
| 108 } else if (sign > compare_threshold) { | |
| 109 ++pos_count; | |
| 110 } | |
| 111 | |
| 112 if (pos_count && neg_count) { | |
| 113 return BSP_SPLIT; | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 if (pos_count) { | |
| 118 return BSP_FRONT; | |
| 119 } | |
| 120 return BSP_BACK; | |
| 121 } | |
| 122 | |
| 123 static bool LineIntersectPlane(const gfx::Point3F& line_start, | |
| 124 const gfx::Point3F& line_end, | |
| 125 const gfx::Point3F& plane_origin, | |
| 126 const gfx::Vector3dF& plane_normal, | |
| 127 gfx::Point3F* intersection, | |
| 128 float distance_threshold) { | |
| 129 gfx::Vector3dF start_to_origin_vector = plane_origin - line_start; | |
| 130 gfx::Vector3dF end_to_origin_vector = plane_origin - line_end; | |
| 131 | |
| 132 double start_distance = gfx::DotProduct(start_to_origin_vector, plane_normal); | |
| 133 double end_distance = gfx::DotProduct(end_to_origin_vector, plane_normal); | |
| 134 | |
| 135 // The case where one vertex lies on the thick-plane and the other | |
| 136 // is outside of it. | |
| 137 if (std::abs(start_distance) < distance_threshold && | |
| 138 std::abs(end_distance) > distance_threshold) { | |
| 139 intersection->SetPoint(line_start.x(), line_start.y(), line_start.z()); | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 // This is the case where we clearly cross the thick-plane. | |
| 144 if ((start_distance > distance_threshold && | |
| 145 end_distance < -distance_threshold) || | |
| 146 (start_distance < -distance_threshold && | |
| 147 end_distance > distance_threshold)) { | |
|
Ian Vollick
2014/07/25 15:52:13
Dumb question, if you know start and end distance,
troyhildebrandt
2014/07/25 20:37:47
You're right, much simpler, done.
| |
| 148 // By getting the dot product of the line segment normalized vs. the plane's | |
| 149 // normal, we get a value that approaches zero as the angle of the | |
| 150 // intersecting line becomes parallel with the plane. | |
| 151 // When the line segment vector is equal to the plane's normal, we have the | |
| 152 // most direct path to the plane, and the dot product is 1. In this case, | |
| 153 // the calculation below is just |start_distance| / 1, which is the trivial | |
| 154 // case because the line takes the most direct path to intersect with the | |
| 155 // plane. |start_distance| is already the shortest straight line path | |
| 156 // distance to the plane. | |
| 157 // However, as the vector that represents the direction of the line segment | |
| 158 // indicates that it is becoming more parallel with the surface of the plane | |
| 159 // and the dot product approaches 0, the path to intersection becomes much | |
| 160 // longer, and the division of |start_distance| by < 1 gives us the true | |
| 161 // distance of the start point to the plane following the vector of the line | |
| 162 // segment. | |
| 163 gfx::Vector3dF v = line_end - line_start; | |
| 164 v.Scale(1.f / v.Length()); | |
| 165 double projected_length = gfx::DotProduct(v, plane_normal); | |
| 166 | |
| 167 // The only way this will ever be true is the case where the line runs | |
| 168 // parallel to the surface of the plane and would never contact it, and | |
| 169 // this would result in a divide by zero below. | |
| 170 if (!projected_length) { | |
| 171 return false; | |
| 172 } | |
| 173 | |
| 174 double scale = start_distance / projected_length; | |
| 175 intersection->SetPoint(line_start.x() + (v.x() * scale), | |
| 176 line_start.y() + (v.y() * scale), | |
| 177 line_start.z() + (v.z() * scale)); | |
| 178 | |
| 179 return true; | |
| 180 } | |
| 181 return false; | |
| 182 } | |
| 183 | |
| 184 // This function is separate from ApplyTransform because it is often unnecessary | |
| 185 // to transform the normal with the rest of the polygon. | |
| 186 // When drawing these polygons, it is necessary to move them back into layer | |
| 187 // space before sending them to OpenGL, which requires using ApplyTransform, | |
| 188 // but normal information is no longer needed after sorting. | |
| 189 void DrawPolygon::ApplyTransformToNormal(const gfx::Transform& transform) { | |
| 190 // Now we use the inverse transpose of |transform| to transform the normal. | |
| 191 gfx::Transform inverse_transform; | |
| 192 bool inverted = transform.GetInverse(&inverse_transform); | |
| 193 DCHECK(inverted); | |
| 194 if (!inverted) | |
| 195 return; | |
| 196 inverse_transform.Transpose(); | |
| 197 | |
| 198 gfx::Point3F new_normal(normal_.x(), normal_.y(), normal_.z()); | |
| 199 inverse_transform.TransformPoint(&new_normal); | |
| 200 // Make sure our normal is still normalized. | |
| 201 normal_ = gfx::Vector3dF(new_normal.x(), new_normal.y(), new_normal.z()); | |
| 202 float normal_magnitude = normal_.Length(); | |
| 203 if (normal_magnitude != 0 && normal_magnitude != 1) { | |
| 204 normal_.Scale(1.0f / normal_magnitude); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 void DrawPolygon::ApplyTransform(const gfx::Transform& transform) { | |
| 209 for (unsigned int i = 0; i < points_.size(); i++) { | |
| 210 transform.TransformPoint(&points_[i]); | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 bool DrawPolygon::Split(const DrawPolygon& splitter, | |
| 215 scoped_ptr<DrawPolygon>* front, | |
| 216 scoped_ptr<DrawPolygon>* back) { | |
| 217 gfx::Point3F intersections[2]; | |
| 218 std::vector<gfx::Point3F> out_points[2]; | |
| 219 // vertex_before stores the index of the vertex before its matching | |
| 220 // intersection. | |
| 221 // i.e. vertex_before[0] stores the vertex we saw before we crossed the plane | |
| 222 // which resulted in the line/plane intersection giving us intersections[0]. | |
| 223 unsigned int vertex_before[2]; | |
| 224 unsigned int points_size = points_.size(); | |
| 225 unsigned int current_intersection = 0; | |
| 226 | |
| 227 unsigned int current_vertex = 0; | |
| 228 while (current_intersection < 2) { | |
|
Ian Vollick
2014/07/25 15:52:13
A comment explaining that we can only have 2 point
troyhildebrandt
2014/07/25 20:37:47
If I'm envisioning the case correctly, then it sho
Ian Vollick
2014/07/26 01:24:37
kk.
| |
| 229 if (LineIntersectPlane(points_[(current_vertex % points_size)], | |
| 230 points_[(current_vertex + 1) % points_size], | |
| 231 splitter.points_[0], | |
| 232 splitter.normal_, | |
| 233 &intersections[current_intersection], | |
| 234 split_threshold)) { | |
| 235 vertex_before[current_intersection] = current_vertex % points_size; | |
| 236 current_intersection++; | |
| 237 // We found both intersection points so we're done already. | |
| 238 if (current_intersection == 2) { | |
| 239 break; | |
| 240 } | |
| 241 } | |
| 242 if (current_vertex++ > points_size) { | |
| 243 break; | |
| 244 } | |
| 245 } | |
| 246 if (current_intersection < 2) { | |
| 247 return false; | |
| 248 } | |
| 249 | |
| 250 // Since we found both the intersection points, we can begin building the | |
| 251 // vertex set for both our new polygons. | |
| 252 unsigned int start1 = (vertex_before[0] + 1) % points_size; | |
| 253 unsigned int start2 = (vertex_before[1] + 1) % points_size; | |
| 254 unsigned int points_remaining = points_size; | |
| 255 | |
| 256 // First polygon. | |
| 257 out_points[0].push_back(intersections[0]); | |
| 258 for (unsigned int i = start1; i <= vertex_before[1]; i++) { | |
|
Ian Vollick
2014/07/25 15:52:12
Confused about this. Isn't it possible that we'd h
troyhildebrandt
2014/07/25 20:37:47
If we had to wrap around to reach vertex_before[1]
| |
| 259 out_points[0].push_back(points_[i]); | |
| 260 --points_remaining; | |
| 261 } | |
| 262 out_points[0].push_back(intersections[1]); | |
| 263 | |
| 264 // Second polygon. | |
| 265 out_points[1].push_back(intersections[1]); | |
| 266 unsigned int index = start2; | |
| 267 for (unsigned int i = 0; i < points_remaining; i++) { | |
| 268 out_points[1].push_back(points_[index % points_size]); | |
| 269 ++index; | |
| 270 } | |
| 271 out_points[1].push_back(intersections[0]); | |
| 272 | |
| 273 // Give both polygons the original splitting polygon's ID, so that they'll | |
| 274 // still be sorted properly in co-planar instances. | |
| 275 // Send false as last parameter for is_original because they're split. | |
| 276 scoped_ptr<DrawPolygon> poly1( | |
| 277 new DrawPolygon(original_ref_, out_points[0], order_index_)); | |
| 278 scoped_ptr<DrawPolygon> poly2( | |
| 279 new DrawPolygon(original_ref_, out_points[1], order_index_)); | |
| 280 | |
|
troyhildebrandt
2014/07/25 20:37:47
This is where the normals are set for the splits s
| |
| 281 poly1->SetNormal(normal_); | |
| 282 poly2->SetNormal(normal_); | |
| 283 | |
| 284 if (SideCompare(*poly1, splitter) == BSP_FRONT) { | |
| 285 *front = poly1.Pass(); | |
| 286 *back = poly2.Pass(); | |
| 287 } else { | |
| 288 *front = poly2.Pass(); | |
| 289 *back = poly1.Pass(); | |
| 290 } | |
| 291 return true; | |
| 292 } | |
| 293 | |
| 294 // This algorithm takes the first vertex in the polygon and uses that as a | |
| 295 // pivot point to fan out and create quads from the rest of the vertices. | |
| 296 // |offset| starts off as the second vertex, and then |op1| and |op2| indicate | |
| 297 // offset+1 and offset+2 respectively. | |
| 298 // After the first quad is created, the first vertex in the next quad is the | |
| 299 // same as all the rest, the pivot point. The second vertex in the next quad is | |
| 300 // the old |op2|, the last vertex added to the previous quad. This continues | |
| 301 // until all points are exhausted. | |
| 302 // The special case here is where there are only 3 points remaining, in which | |
| 303 // case we use the same values for vertex 3 and 4 to make a degenerate quad | |
| 304 // that represents a triangle. | |
| 305 void DrawPolygon::ToQuads2D(std::vector<gfx::QuadF>* quads) const { | |
| 306 if (points_.size() <= 2) | |
| 307 return; | |
| 308 | |
| 309 gfx::PointF first(points_[0].x(), points_[0].y()); | |
| 310 unsigned int offset = 1; | |
| 311 while (offset < points_.size() - 1) { | |
| 312 unsigned int op1 = offset + 1; | |
| 313 unsigned int op2 = offset + 2; | |
| 314 if (op2 >= points_.size()) { | |
| 315 // It's going to be a degenerate triangle. | |
| 316 op2 = op1; | |
| 317 } | |
| 318 quads->push_back( | |
| 319 gfx::QuadF(first, | |
| 320 gfx::PointF(points_[offset].x(), points_[offset].y()), | |
| 321 gfx::PointF(points_[op1].x(), points_[op1].y()), | |
| 322 gfx::PointF(points_[op2].x(), points_[op2].y()))); | |
| 323 offset = op2; | |
| 324 } | |
| 325 } | |
| 326 | |
| 327 bool DrawPolygon::GetInverseTransform(gfx::Transform* transform) const { | |
|
Ian Vollick
2014/07/25 15:52:13
Does anyone use this?
troyhildebrandt
2014/07/25 20:37:47
This is used when rendering, since we have to go b
Ian Vollick
2014/07/26 01:24:37
Perhaps you could use it in ApplyTransformToNormal
troyhildebrandt
2014/07/28 17:39:37
I just removed it instead for now.
| |
| 328 return original_ref_->quadTransform().GetInverse(transform); | |
| 329 } | |
| 330 | |
| 331 } // namespace cc | |
| OLD | NEW |