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) { | |
|
enne (OOO)
2014/07/23 00:22:42
Thinking about simplifying things or removing comp
troyhildebrandt
2014/07/23 19:47:56
The area gets recalculated when things are split.
enne (OOO)
2014/07/23 20:07:17
Could you test areas of non-quads, then?
| |
| 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 float z_threshold) { | |
| 97 // Right away let's check if they're coplanar | |
| 98 double dot = gfx::DotProduct(a.normal, b.normal); | |
| 99 float sign; | |
| 100 bool normal_match = false; | |
| 101 // This check assumes that the normals are normalized. | |
| 102 if (std::abs(dot) >= coplanar_dot_epsilon) { | |
| 103 normal_match = true; | |
| 104 // The normals are matching enough that we only have to test one point. | |
| 105 sign = gfx::DotProduct(a.points[0] - b.points[0], b.normal); | |
| 106 // Is it on either side of the splitter? | |
| 107 if (sign < -z_threshold) { | |
| 108 return BSP_BACK; | |
| 109 } | |
| 110 | |
| 111 if (sign > z_threshold) { | |
| 112 return BSP_FRONT; | |
| 113 } | |
| 114 | |
| 115 // No it wasn't, so the sign of the dot product of the normals | |
| 116 // along with document order determines which side it goes on. | |
| 117 if (dot >= 0.0f) { | |
| 118 if (a.order_index < b.order_index) { | |
| 119 return BSP_COPLANAR_FRONT; | |
| 120 } | |
| 121 return BSP_COPLANAR_BACK; | |
| 122 } | |
| 123 | |
| 124 if (a.order_index < b.order_index) { | |
| 125 return BSP_COPLANAR_BACK; | |
| 126 } | |
| 127 return BSP_COPLANAR_FRONT; | |
| 128 } | |
| 129 | |
| 130 unsigned int pos_count = 0; | |
| 131 unsigned int neg_count = 0; | |
| 132 for (unsigned int i = 0; i < a.points.size(); i++) { | |
| 133 if (!normal_match || (normal_match && i > 0)) { | |
| 134 sign = gfx::DotProduct(a.points[i] - b.points[0], b.normal); | |
| 135 } | |
| 136 | |
| 137 if (sign < -z_threshold) { | |
| 138 ++neg_count; | |
| 139 } else if (sign > z_threshold) { | |
| 140 ++pos_count; | |
| 141 } | |
| 142 | |
| 143 if (pos_count && neg_count) { | |
| 144 return BSP_SPLIT; | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 if (pos_count) { | |
| 149 return BSP_FRONT; | |
| 150 } | |
| 151 return BSP_BACK; | |
| 152 } | |
| 153 | |
| 154 static bool LineIntersectPlane(const gfx::Point3F& line_start, | |
| 155 const gfx::Point3F& line_end, | |
| 156 const gfx::Point3F& plane_origin, | |
| 157 const gfx::Vector3dF& plane_normal, | |
| 158 gfx::Point3F* intersection, | |
| 159 float distance_threshold) { | |
| 160 gfx::Vector3dF vec1 = plane_origin - line_start; | |
| 161 gfx::Vector3dF vec2 = plane_origin - line_end; | |
| 162 | |
| 163 double start_distance = gfx::DotProduct(vec1, plane_normal); | |
| 164 double end_distance = gfx::DotProduct(vec2, plane_normal); | |
| 165 | |
| 166 // The case where one vertex lies on the thick-plane and the other | |
| 167 // is outside of it. | |
| 168 if (std::abs(start_distance) < distance_threshold && | |
| 169 std::abs(end_distance) > distance_threshold) { | |
| 170 intersection->SetPoint(line_start.x(), line_start.y(), line_start.z()); | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 // This is the case where we clearly cross the thick-plane. | |
| 175 if ((start_distance > distance_threshold && | |
| 176 end_distance < -distance_threshold) || | |
| 177 (start_distance < -distance_threshold && | |
| 178 end_distance > distance_threshold)) { | |
| 179 gfx::Vector3dF v = line_end - line_start; | |
| 180 | |
| 181 v.Scale(1.f / v.Length()); | |
| 182 double projected_length = gfx::DotProduct(v, plane_normal); | |
| 183 if (!projected_length) | |
| 184 return false; | |
| 185 | |
| 186 double scale = start_distance / projected_length; | |
| 187 intersection->SetPoint(line_start.x() + (v.x() * scale), | |
| 188 line_start.y() + (v.y() * scale), | |
| 189 line_start.z() + (v.z() * scale)); | |
| 190 | |
| 191 return true; | |
| 192 } | |
| 193 return false; | |
| 194 } | |
| 195 | |
| 196 bool DrawPolygon::ApplyTransform(const gfx::Transform& transform) { | |
| 197 bool clipped = false; | |
| 198 for (unsigned int i = 0; i < points.size(); i++) { | |
| 199 points[i] = MathUtil::MapPoint(transform, points[i], &clipped); | |
|
enne (OOO)
2014/07/23 00:22:42
Will this clobber |clipped| every time it is calle
troyhildebrandt
2014/07/23 19:47:56
It definitely clobbers clipped every single time,
| |
| 200 } | |
| 201 return !clipped; | |
| 202 } | |
| 203 | |
| 204 bool DrawPolygon::Split(const DrawPolygon& splitter, | |
| 205 double plane_threshold, | |
| 206 scoped_ptr<DrawPolygon>* front, | |
| 207 scoped_ptr<DrawPolygon>* back) { | |
| 208 gfx::Point3F intersections[2]; | |
| 209 std::vector<gfx::Point3F> out_points[2]; | |
| 210 int vertex_before[2]; | |
| 211 int points_size = points.size(); | |
| 212 int current_intersection = 0; | |
| 213 | |
| 214 int current_vertex = 0; | |
| 215 while (current_intersection < 2) { | |
| 216 if (current_vertex++ > points_size) { | |
| 217 break; | |
| 218 } | |
| 219 if (current_intersection > 0 && | |
| 220 vertex_before[0] == (current_vertex % points_size)) { | |
| 221 continue; | |
| 222 } | |
| 223 | |
| 224 if (LineIntersectPlane(points[(current_vertex % points_size)], | |
| 225 points[(current_vertex + 1) % points_size], | |
| 226 splitter.points[0], | |
| 227 splitter.normal, | |
| 228 &intersections[current_intersection], | |
| 229 plane_threshold)) { | |
| 230 vertex_before[current_intersection] = current_vertex % points_size; | |
| 231 current_intersection++; | |
| 232 // We found both intersection points so we're done already. | |
| 233 if (current_intersection == 2) { | |
| 234 break; | |
| 235 } | |
| 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 int start1 = (vertex_before[0] + 1) % points_size; | |
| 245 int start2 = (vertex_before[1] + 1) % points_size; | |
| 246 int points_remaining = points_size; | |
| 247 | |
| 248 // First polygon. | |
| 249 out_points[0].push_back(intersections[0]); | |
| 250 for (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 int index = start2; | |
| 259 for (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(new DrawPolygon(original_ref, | |
| 269 &(out_points[0][0]), | |
| 270 out_points[0].size(), | |
| 271 this->order_index)); | |
| 272 scoped_ptr<DrawPolygon> poly2(new DrawPolygon(original_ref, | |
| 273 &(out_points[1][0]), | |
| 274 out_points[1].size(), | |
| 275 this->order_index)); | |
| 276 | |
| 277 if (SideCompare(*poly1, splitter, plane_threshold) == BSP_FRONT) { | |
| 278 *front = poly1.Pass(); | |
| 279 *back = poly2.Pass(); | |
| 280 } else { | |
| 281 *front = poly2.Pass(); | |
| 282 *back = poly1.Pass(); | |
| 283 } | |
| 284 return true; | |
| 285 } | |
| 286 | |
| 287 void DrawPolygon::ToQuads2D(std::vector<gfx::QuadF>* quads) const { | |
| 288 if (points.size() == 0) | |
| 289 return; | |
| 290 | |
| 291 // op1 = offset plus 1, op2 = offset plus 2. | |
| 292 gfx::PointF first(points[0].x(), points[0].y()); | |
| 293 unsigned int offset = 1; | |
| 294 while (offset < points.size() - 1) { | |
| 295 unsigned int op1 = offset + 1; | |
| 296 unsigned int op2 = offset + 2; | |
| 297 if (op2 >= points.size()) { | |
| 298 // It's going to be a degenerate triangle. | |
| 299 op2 = op1; | |
| 300 } | |
| 301 quads->push_back( | |
| 302 gfx::QuadF(first, | |
| 303 gfx::PointF(points[offset].x(), points[offset].y()), | |
| 304 gfx::PointF(points[op1].x(), points[op1].y()), | |
| 305 gfx::PointF(points[op2].x(), points[op2].y()))); | |
| 306 offset = op2; | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 bool DrawPolygon::GetInverseTransform(gfx::Transform* transform) const { | |
| 311 return original_ref->quadTransform().GetInverse(transform); | |
| 312 } | |
| 313 | |
| 314 } // namespace cc | |
| OLD | NEW |