OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/base/math_util.h" | 5 #include "cc/base/math_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #ifdef __SSE__ | 10 #ifdef __SSE__ |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 const gfx::PointF& rhs) { | 170 const gfx::PointF& rhs) { |
171 return IsNearlyTheSame(lhs.x(), rhs.x()) && IsNearlyTheSame(lhs.y(), rhs.y()); | 171 return IsNearlyTheSame(lhs.x(), rhs.x()) && IsNearlyTheSame(lhs.y(), rhs.y()); |
172 } | 172 } |
173 | 173 |
174 static inline bool IsNearlyTheSame(const gfx::Point3F& lhs, | 174 static inline bool IsNearlyTheSame(const gfx::Point3F& lhs, |
175 const gfx::Point3F& rhs) { | 175 const gfx::Point3F& rhs) { |
176 return IsNearlyTheSame(lhs.x(), rhs.x()) && | 176 return IsNearlyTheSame(lhs.x(), rhs.x()) && |
177 IsNearlyTheSame(lhs.y(), rhs.y()) && IsNearlyTheSame(lhs.z(), rhs.z()); | 177 IsNearlyTheSame(lhs.y(), rhs.y()) && IsNearlyTheSame(lhs.z(), rhs.z()); |
178 } | 178 } |
179 | 179 |
180 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex, | |
181 gfx::PointF clipped_quad[8], | |
182 int* num_vertices_in_clipped_quad) { | |
183 if (*num_vertices_in_clipped_quad > 0 && | |
184 IsNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1], | |
185 new_vertex)) | |
186 return; | |
187 | |
188 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; | |
189 (*num_vertices_in_clipped_quad)++; | |
190 } | |
191 | |
192 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, | 180 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, |
193 gfx::Point3F clipped_quad[8], | 181 gfx::Point3F clipped_quad[8], |
194 int* num_vertices_in_clipped_quad) { | 182 int* num_vertices_in_clipped_quad) { |
195 if (*num_vertices_in_clipped_quad > 0 && | 183 if (*num_vertices_in_clipped_quad > 0 && |
196 IsNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1], | 184 IsNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1], |
197 new_vertex)) | 185 new_vertex)) |
198 return; | 186 return; |
199 | 187 |
200 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; | 188 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; |
201 (*num_vertices_in_clipped_quad)++; | 189 (*num_vertices_in_clipped_quad)++; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]); | 300 HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]); |
313 HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]); | 301 HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]); |
314 DCHECK(!hc0.ShouldBeClipped()); | 302 DCHECK(!hc0.ShouldBeClipped()); |
315 DCHECK(!hc1.ShouldBeClipped()); | 303 DCHECK(!hc1.ShouldBeClipped()); |
316 | 304 |
317 gfx::PointF top_left(hc0.CartesianPoint2d()); | 305 gfx::PointF top_left(hc0.CartesianPoint2d()); |
318 gfx::PointF bottom_right(hc1.CartesianPoint2d()); | 306 gfx::PointF bottom_right(hc1.CartesianPoint2d()); |
319 return gfx::ToEnclosedRect(gfx::BoundingRect(top_left, bottom_right)); | 307 return gfx::ToEnclosedRect(gfx::BoundingRect(top_left, bottom_right)); |
320 } | 308 } |
321 | 309 |
322 void MathUtil::MapClippedQuad(const gfx::Transform& transform, | |
323 const gfx::QuadF& src_quad, | |
324 gfx::PointF clipped_quad[8], | |
325 int* num_vertices_in_clipped_quad) { | |
326 HomogeneousCoordinate h1 = | |
327 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1())); | |
328 HomogeneousCoordinate h2 = | |
329 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p2())); | |
330 HomogeneousCoordinate h3 = | |
331 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p3())); | |
332 HomogeneousCoordinate h4 = | |
333 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p4())); | |
334 | |
335 // The order of adding the vertices to the array is chosen so that | |
336 // clockwise / counter-clockwise orientation is retained. | |
337 | |
338 *num_vertices_in_clipped_quad = 0; | |
339 | |
340 if (!h1.ShouldBeClipped()) { | |
341 AddVertexToClippedQuad( | |
342 h1.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); | |
343 } | |
344 | |
345 if (h1.ShouldBeClipped() ^ h2.ShouldBeClipped()) { | |
346 AddVertexToClippedQuad(ComputeClippedCartesianPoint2dForEdge(h1, h2), | |
347 clipped_quad, num_vertices_in_clipped_quad); | |
348 } | |
349 | |
350 if (!h2.ShouldBeClipped()) { | |
351 AddVertexToClippedQuad( | |
352 h2.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); | |
353 } | |
354 | |
355 if (h2.ShouldBeClipped() ^ h3.ShouldBeClipped()) { | |
356 AddVertexToClippedQuad(ComputeClippedCartesianPoint2dForEdge(h2, h3), | |
357 clipped_quad, num_vertices_in_clipped_quad); | |
358 } | |
359 | |
360 if (!h3.ShouldBeClipped()) { | |
361 AddVertexToClippedQuad( | |
362 h3.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); | |
363 } | |
364 | |
365 if (h3.ShouldBeClipped() ^ h4.ShouldBeClipped()) { | |
366 AddVertexToClippedQuad(ComputeClippedCartesianPoint2dForEdge(h3, h4), | |
367 clipped_quad, num_vertices_in_clipped_quad); | |
368 } | |
369 | |
370 if (!h4.ShouldBeClipped()) { | |
371 AddVertexToClippedQuad( | |
372 h4.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); | |
373 } | |
374 | |
375 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { | |
376 AddVertexToClippedQuad(ComputeClippedCartesianPoint2dForEdge(h4, h1), | |
377 clipped_quad, num_vertices_in_clipped_quad); | |
378 } | |
379 | |
380 if (*num_vertices_in_clipped_quad > 2 && | |
381 IsNearlyTheSame(clipped_quad[0], | |
382 clipped_quad[*num_vertices_in_clipped_quad - 1])) | |
383 *num_vertices_in_clipped_quad -= 1; | |
384 | |
385 DCHECK_LE(*num_vertices_in_clipped_quad, 8); | |
386 } | |
387 | |
388 bool MathUtil::MapClippedQuad3d(const gfx::Transform& transform, | 310 bool MathUtil::MapClippedQuad3d(const gfx::Transform& transform, |
389 const gfx::QuadF& src_quad, | 311 const gfx::QuadF& src_quad, |
390 gfx::Point3F clipped_quad[8], | 312 gfx::Point3F clipped_quad[8], |
391 int* num_vertices_in_clipped_quad) { | 313 int* num_vertices_in_clipped_quad) { |
392 HomogeneousCoordinate h1 = | 314 HomogeneousCoordinate h1 = |
393 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1())); | 315 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1())); |
394 HomogeneousCoordinate h2 = | 316 HomogeneousCoordinate h2 = |
395 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p2())); | 317 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p2())); |
396 HomogeneousCoordinate h3 = | 318 HomogeneousCoordinate h3 = |
397 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p3())); | 319 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p3())); |
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
985 const gfx::PointF& right) { | 907 const gfx::PointF& right) { |
986 return IsNearlyTheSame(left, right); | 908 return IsNearlyTheSame(left, right); |
987 } | 909 } |
988 | 910 |
989 bool MathUtil::IsNearlyTheSameForTesting(const gfx::Point3F& left, | 911 bool MathUtil::IsNearlyTheSameForTesting(const gfx::Point3F& left, |
990 const gfx::Point3F& right) { | 912 const gfx::Point3F& right) { |
991 return IsNearlyTheSame(left, right); | 913 return IsNearlyTheSame(left, right); |
992 } | 914 } |
993 | 915 |
994 } // namespace cc | 916 } // namespace cc |
OLD | NEW |