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 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; | |
|
Ian Vollick
2014/07/24 18:11:27
How does this do with large quads? Seems like ther
troyhildebrandt
2014/07/24 21:05:06
All of this removed.
| |
| 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 const std::vector<gfx::Point3F>& in_points, | |
| 47 int draw_order_index) | |
| 48 : order_index(draw_order_index), original_ref(original) { | |
| 49 for (unsigned int i = 0; i < in_points.size(); i++) { | |
| 50 points.push_back(in_points[i]); | |
| 51 } | |
| 52 | |
| 53 if (points.size() > 2) { | |
| 54 gfx::Vector3dF c12 = in_points[1] - in_points[0]; | |
| 55 gfx::Vector3dF c13 = in_points[2] - in_points[0]; | |
|
Ian Vollick
2014/07/24 18:11:27
Couldn't these two vectors be collinear? I think t
| |
| 56 normal = gfx::CrossProduct(c12, c13); | |
| 57 normal.Scale(1.0f / normal.Length()); | |
|
Ian Vollick
2014/07/24 18:11:28
Please check for div-by-zero. If |in_points| is fi
| |
| 58 } | |
| 59 area = Area(*this); | |
| 60 } | |
| 61 | |
| 62 DrawPolygon::~DrawPolygon() { | |
| 63 } | |
| 64 | |
| 65 scoped_ptr<DrawPolygon> DrawPolygon::CreateCopy() { | |
| 66 DrawPolygon* new_polygon = new DrawPolygon(); | |
| 67 new_polygon->order_index = order_index; | |
| 68 new_polygon->original_ref = original_ref; | |
| 69 new_polygon->points.reserve(points.size()); | |
| 70 new_polygon->points = points; | |
| 71 new_polygon->normal.set_x(normal.x()); | |
| 72 new_polygon->normal.set_y(normal.y()); | |
| 73 new_polygon->normal.set_z(normal.z()); | |
| 74 new_polygon->area = area; | |
| 75 return scoped_ptr<DrawPolygon>(new_polygon); | |
| 76 } | |
| 77 | |
| 78 float DrawPolygon::SignedPointDistance(const gfx::Point3F& point) const { | |
| 79 return gfx::DotProduct(point - points[0], normal); | |
| 80 } | |
| 81 | |
| 82 // Checks whether or not shape a lies on the front or back side of b, or | |
| 83 // whether they should be considered coplanar. If on the back side, we | |
| 84 // say ABeforeB because it should be drawn in that order. | |
| 85 // Assumes that layers are split and there are no intersecting planes. | |
| 86 BspCompareResult DrawPolygon::SideCompare(const DrawPolygon& a, | |
| 87 const DrawPolygon& b) { | |
| 88 // Right away let's check if they're coplanar | |
| 89 double dot = gfx::DotProduct(a.normal, b.normal); | |
| 90 float sign; | |
| 91 bool normal_match = false; | |
| 92 // This check assumes that the normals are normalized. | |
| 93 if (std::abs(dot) >= coplanar_dot_epsilon) { | |
| 94 normal_match = true; | |
| 95 // The normals are matching enough that we only have to test one point. | |
| 96 sign = gfx::DotProduct(a.points[0] - b.points[0], b.normal); | |
| 97 // Is it on either side of the splitter? | |
| 98 if (sign < -compare_threshold) { | |
| 99 return BSP_BACK; | |
| 100 } | |
| 101 | |
| 102 if (sign > compare_threshold) { | |
| 103 return BSP_FRONT; | |
| 104 } | |
| 105 | |
| 106 // No it wasn't, so the sign of the dot product of the normals | |
| 107 // along with document order determines which side it goes on. | |
| 108 if (dot >= 0.0f) { | |
| 109 if (a.order_index < b.order_index) { | |
| 110 return BSP_COPLANAR_FRONT; | |
| 111 } | |
| 112 return BSP_COPLANAR_BACK; | |
| 113 } | |
| 114 | |
| 115 if (a.order_index < b.order_index) { | |
| 116 return BSP_COPLANAR_BACK; | |
| 117 } | |
| 118 return BSP_COPLANAR_FRONT; | |
| 119 } | |
| 120 | |
| 121 unsigned int pos_count = 0; | |
| 122 unsigned int neg_count = 0; | |
| 123 for (unsigned int i = 0; i < a.points.size(); i++) { | |
| 124 if (!normal_match || (normal_match && i > 0)) { | |
| 125 sign = gfx::DotProduct(a.points[i] - b.points[0], b.normal); | |
| 126 } | |
| 127 | |
| 128 if (sign < -compare_threshold) { | |
| 129 ++neg_count; | |
| 130 } else if (sign > compare_threshold) { | |
| 131 ++pos_count; | |
| 132 } | |
| 133 | |
| 134 if (pos_count && neg_count) { | |
| 135 return BSP_SPLIT; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 if (pos_count) { | |
| 140 return BSP_FRONT; | |
| 141 } | |
| 142 return BSP_BACK; | |
| 143 } | |
| 144 | |
| 145 static bool LineIntersectPlane(const gfx::Point3F& line_start, | |
| 146 const gfx::Point3F& line_end, | |
| 147 const gfx::Point3F& plane_origin, | |
| 148 const gfx::Vector3dF& plane_normal, | |
| 149 gfx::Point3F* intersection, | |
| 150 float distance_threshold) { | |
| 151 gfx::Vector3dF start_to_origin_vector = plane_origin - line_start; | |
| 152 gfx::Vector3dF end_to_origin_vector = plane_origin - line_end; | |
| 153 | |
| 154 double start_distance = gfx::DotProduct(start_to_origin_vector, plane_normal); | |
| 155 double end_distance = gfx::DotProduct(end_to_origin_vector, plane_normal); | |
| 156 | |
| 157 // The case where one vertex lies on the thick-plane and the other | |
| 158 // is outside of it. | |
| 159 if (std::abs(start_distance) < distance_threshold && | |
| 160 std::abs(end_distance) > distance_threshold) { | |
| 161 intersection->SetPoint(line_start.x(), line_start.y(), line_start.z()); | |
| 162 return true; | |
| 163 } | |
| 164 | |
| 165 // This is the case where we clearly cross the thick-plane. | |
| 166 if ((start_distance > distance_threshold && | |
| 167 end_distance < -distance_threshold) || | |
| 168 (start_distance < -distance_threshold && | |
| 169 end_distance > distance_threshold)) { | |
| 170 // By getting the dot product of the line segment normalized vs. the plane's | |
| 171 // normal, we get a value that approaches zero as the angle of the | |
| 172 // intersecting line becomes parallel with the plane. | |
| 173 // When the line segment vector is equal to the plane's normal, we have the | |
| 174 // most direct path to the plane, and the dot product is 1. In this case, | |
| 175 // the calculation below is just |start_distance| / 1, which is the trivial | |
| 176 // case because the line takes the most direct path to intersect with the | |
| 177 // plane. |start_distance| is already the shortest straight line path | |
| 178 // distance to the plane. | |
| 179 // However, as the vector that represents the direction of the line segment | |
| 180 // indicates that it is becoming more parallel with the surface of the plane | |
| 181 // and the dot product approaches 0, the path to intersection becomes much | |
| 182 // longer, and the division of |start_distance| by < 1 gives us the true | |
| 183 // distance of the start point to the plane following the vector of the line | |
| 184 // segment. | |
| 185 gfx::Vector3dF v = line_end - line_start; | |
| 186 v.Scale(1.f / v.Length()); | |
| 187 double projected_length = gfx::DotProduct(v, plane_normal); | |
| 188 | |
| 189 // The only way this will ever be true is the case where the line runs | |
| 190 // parallel to the surface of the plane and would never contact it, and | |
| 191 // this would result in a divide by zero below. | |
| 192 if (!projected_length) { | |
| 193 return false; | |
| 194 } | |
| 195 | |
| 196 double scale = start_distance / projected_length; | |
| 197 intersection->SetPoint(line_start.x() + (v.x() * scale), | |
| 198 line_start.y() + (v.y() * scale), | |
| 199 line_start.z() + (v.z() * scale)); | |
| 200 | |
| 201 return true; | |
| 202 } | |
| 203 return false; | |
| 204 } | |
| 205 | |
| 206 bool DrawPolygon::Split(const DrawPolygon& splitter, | |
| 207 scoped_ptr<DrawPolygon>* front, | |
| 208 scoped_ptr<DrawPolygon>* back) { | |
| 209 gfx::Point3F intersections[2]; | |
| 210 std::vector<gfx::Point3F> out_points[2]; | |
| 211 // vertex_before stores the index of the vertex before its matching | |
| 212 // intersection. | |
| 213 // i.e. vertex_before[0] stores the vertex we saw before we crossed the plane | |
| 214 // which resulted in the line/plane intersection giving us intersections[0]. | |
| 215 unsigned int vertex_before[2]; | |
| 216 unsigned int points_size = points.size(); | |
| 217 unsigned int current_intersection = 0; | |
| 218 | |
| 219 unsigned int current_vertex = 0; | |
| 220 while (current_intersection < 2) { | |
| 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 if (current_vertex++ > points_size) { | |
| 235 break; | |
| 236 } | |
| 237 } | |
| 238 if (current_intersection < 2) { | |
| 239 return false; | |
| 240 } | |
| 241 | |
| 242 // Since we found both the intersection points, we can begin building the | |
| 243 // vertex set for both our new polygons. | |
| 244 unsigned int start1 = (vertex_before[0] + 1) % points_size; | |
| 245 unsigned int start2 = (vertex_before[1] + 1) % points_size; | |
| 246 unsigned int points_remaining = points_size; | |
| 247 | |
| 248 // First polygon. | |
| 249 out_points[0].push_back(intersections[0]); | |
| 250 for (unsigned int i = start1; i <= vertex_before[1]; i++) { | |
| 251 out_points[0].push_back(points[i]); | |
| 252 --points_remaining; | |
| 253 } | |
| 254 out_points[0].push_back(intersections[1]); | |
| 255 | |
| 256 // Second polygon. | |
| 257 out_points[1].push_back(intersections[1]); | |
| 258 unsigned int index = start2; | |
| 259 for (unsigned int i = 0; i < points_remaining; i++) { | |
| 260 out_points[1].push_back(points[index % points_size]); | |
| 261 ++index; | |
| 262 } | |
| 263 out_points[1].push_back(intersections[0]); | |
| 264 | |
| 265 // Give both polygons the original splitting polygon's ID, so that they'll | |
| 266 // still be sorted properly in co-planar instances. | |
| 267 // Send false as last parameter for is_original because they're split. | |
| 268 scoped_ptr<DrawPolygon> poly1( | |
| 269 new DrawPolygon(original_ref, out_points[0], this->order_index)); | |
| 270 scoped_ptr<DrawPolygon> poly2( | |
| 271 new DrawPolygon(original_ref, out_points[1], this->order_index)); | |
| 272 | |
| 273 if (SideCompare(*poly1, splitter) == BSP_FRONT) { | |
| 274 *front = poly1.Pass(); | |
| 275 *back = poly2.Pass(); | |
| 276 } else { | |
| 277 *front = poly2.Pass(); | |
| 278 *back = poly1.Pass(); | |
| 279 } | |
| 280 return true; | |
| 281 } | |
| 282 | |
| 283 // This algorithm takes the first vertex in the polygon and uses that as a | |
| 284 // pivot point to fan out and create quads from the rest of the vertices. | |
| 285 // |offset| starts off as the second vertex, and then |op1| and |op2| indicate | |
| 286 // offset+1 and offset+2 respectively. | |
| 287 // After the first quad is created, the first vertex in the next quad is the | |
| 288 // same as all the rest, the pivot point. The second vertex in the next quad is | |
| 289 // the old |op2|, the last vertex added to the previous quad. This continues | |
| 290 // until all points are exhausted. | |
| 291 // The special case here is where there are only 3 points remaining, in which | |
| 292 // case we use the same values for vertex 3 and 4 to make a degenerate quad | |
| 293 // that represents a triangle. | |
| 294 void DrawPolygon::ToQuads2D(std::vector<gfx::QuadF>* quads) const { | |
| 295 if (points.size() <= 2) | |
| 296 return; | |
| 297 | |
| 298 gfx::PointF first(points[0].x(), points[0].y()); | |
| 299 unsigned int offset = 1; | |
| 300 while (offset < points.size() - 1) { | |
| 301 unsigned int op1 = offset + 1; | |
| 302 unsigned int op2 = offset + 2; | |
| 303 if (op2 >= points.size()) { | |
| 304 // It's going to be a degenerate triangle. | |
| 305 op2 = op1; | |
| 306 } | |
| 307 quads->push_back( | |
| 308 gfx::QuadF(first, | |
| 309 gfx::PointF(points[offset].x(), points[offset].y()), | |
| 310 gfx::PointF(points[op1].x(), points[op1].y()), | |
| 311 gfx::PointF(points[op2].x(), points[op2].y()))); | |
| 312 offset = op2; | |
| 313 } | |
| 314 } | |
| 315 | |
| 316 bool DrawPolygon::GetInverseTransform(gfx::Transform* transform) const { | |
| 317 return original_ref->quadTransform().GetInverse(transform); | |
| 318 } | |
| 319 | |
| 320 } // namespace cc | |
| OLD | NEW |