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

Side by Side Diff: cc/base/math_util.cc

Issue 2551263002: Don't add duplicate points when clipping (Closed)
Patch Set: flackr comments #10 Created 3 years, 11 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
« no previous file with comments | « cc/base/math_util.h ('k') | cc/base/math_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 float* xmax, 148 float* xmax,
149 float* ymin, 149 float* ymin,
150 float* ymax, 150 float* ymax,
151 const gfx::PointF& p) { 151 const gfx::PointF& p) {
152 *xmin = std::min(p.x(), *xmin); 152 *xmin = std::min(p.x(), *xmin);
153 *xmax = std::max(p.x(), *xmax); 153 *xmax = std::max(p.x(), *xmax);
154 *ymin = std::min(p.y(), *ymin); 154 *ymin = std::min(p.y(), *ymin);
155 *ymax = std::max(p.y(), *ymax); 155 *ymax = std::max(p.y(), *ymax);
156 } 156 }
157 157
158 static inline bool isNearlyTheSame(const float f, const float g) {
danakj 2017/01/11 23:12:06 fix name style
Peter Mayo 2017/01/11 23:47:04 Just the capital, or do you have a specific sugges
danakj 2017/01/11 23:51:54 just the capital. but also consting a float param
159 // The idea behind this is to use this fraction of the larger of the
160 // tow numbers as the limit of the difference. This breaks down near
161 // zero, so we reuse this ase the minimum asolute size we will use
162 // for the base of the scale too.
flackr 2017/01/11 22:48:28 spelling nit: s/tow/two s/ase/as s/asolute/absolut
Peter Mayo 2017/01/11 23:47:04 enoD
163 static const float epsilon_scale = 0.00001f;
164 return std::abs(f - g) <
165 epsilon_scale *
166 std::max(std::max(std::abs(f), std::abs(g)), epsilon_scale);
167 }
168
169 static inline bool isNearlyTheSame(const gfx::PointF& lhs,
170 const gfx::PointF& rhs) {
171 return isNearlyTheSame(lhs.x(), rhs.x()) && isNearlyTheSame(lhs.y(), rhs.y());
172 }
173
174 static inline bool isNearlyTheSame(const gfx::Point3F& lhs,
175 const gfx::Point3F& rhs) {
176 return isNearlyTheSame(lhs.x(), rhs.x()) &&
177 isNearlyTheSame(lhs.y(), rhs.y()) && isNearlyTheSame(lhs.z(), rhs.z());
178 }
179
158 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex, 180 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex,
159 gfx::PointF clipped_quad[8], 181 gfx::PointF clipped_quad[8],
160 int* num_vertices_in_clipped_quad) { 182 int* num_vertices_in_clipped_quad) {
183 if (*num_vertices_in_clipped_quad > 0) {
184 if (isNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1],
185 new_vertex))
186 return;
187 }
161 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; 188 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex;
162 (*num_vertices_in_clipped_quad)++; 189 (*num_vertices_in_clipped_quad)++;
163 } 190 }
164 191
165 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, 192 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex,
166 gfx::Point3F clipped_quad[8], 193 gfx::Point3F clipped_quad[8],
167 int* num_vertices_in_clipped_quad) { 194 int* num_vertices_in_clipped_quad) {
195 if (*num_vertices_in_clipped_quad > 0) {
196 if (isNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1],
197 new_vertex))
198 return;
199 }
168 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; 200 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex;
169 (*num_vertices_in_clipped_quad)++; 201 (*num_vertices_in_clipped_quad)++;
170 } 202 }
171 203
172 gfx::Rect MathUtil::MapEnclosingClippedRect(const gfx::Transform& transform, 204 gfx::Rect MathUtil::MapEnclosingClippedRect(const gfx::Transform& transform,
173 const gfx::Rect& src_rect) { 205 const gfx::Rect& src_rect) {
174 if (transform.IsIdentityOrIntegerTranslation()) { 206 if (transform.IsIdentityOrIntegerTranslation()) {
175 gfx::Vector2d offset(static_cast<int>(transform.matrix().getFloat(0, 3)), 207 gfx::Vector2d offset(static_cast<int>(transform.matrix().getFloat(0, 3)),
176 static_cast<int>(transform.matrix().getFloat(1, 3))); 208 static_cast<int>(transform.matrix().getFloat(1, 3)));
177 return src_rect + offset; 209 return src_rect + offset;
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 if (!h4.ShouldBeClipped()) { 370 if (!h4.ShouldBeClipped()) {
339 AddVertexToClippedQuad( 371 AddVertexToClippedQuad(
340 h4.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); 372 h4.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad);
341 } 373 }
342 374
343 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { 375 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) {
344 AddVertexToClippedQuad(ComputeClippedCartesianPoint2dForEdge(h4, h1), 376 AddVertexToClippedQuad(ComputeClippedCartesianPoint2dForEdge(h4, h1),
345 clipped_quad, num_vertices_in_clipped_quad); 377 clipped_quad, num_vertices_in_clipped_quad);
346 } 378 }
347 379
380 if (*num_vertices_in_clipped_quad > 2) {
381 if (isNearlyTheSame(clipped_quad[0],
flackr 2017/01/11 22:48:28 I think canonical style is to use && rather than n
danakj 2017/01/11 23:12:06 yes, this throughout.
Peter Mayo 2017/01/11 23:47:04 Throughout the CL, done.
382 clipped_quad[*num_vertices_in_clipped_quad - 1])
383 {
384 *num_vertices_in_clipped_quad -= 1;
385 }
386 }
387
348 DCHECK_LE(*num_vertices_in_clipped_quad, 8); 388 DCHECK_LE(*num_vertices_in_clipped_quad, 8);
349 } 389 }
350 390
351 bool MathUtil::MapClippedQuad3d(const gfx::Transform& transform, 391 bool MathUtil::MapClippedQuad3d(const gfx::Transform& transform,
352 const gfx::QuadF& src_quad, 392 const gfx::QuadF& src_quad,
353 gfx::Point3F clipped_quad[8], 393 gfx::Point3F clipped_quad[8],
354 int* num_vertices_in_clipped_quad) { 394 int* num_vertices_in_clipped_quad) {
355 HomogeneousCoordinate h1 = 395 HomogeneousCoordinate h1 =
356 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1())); 396 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1()));
357 HomogeneousCoordinate h2 = 397 HomogeneousCoordinate h2 =
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 if (!h4.ShouldBeClipped()) { 439 if (!h4.ShouldBeClipped()) {
400 AddVertexToClippedQuad3d( 440 AddVertexToClippedQuad3d(
401 h4.CartesianPoint3d(), clipped_quad, num_vertices_in_clipped_quad); 441 h4.CartesianPoint3d(), clipped_quad, num_vertices_in_clipped_quad);
402 } 442 }
403 443
404 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { 444 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) {
405 AddVertexToClippedQuad3d(ComputeClippedCartesianPoint3dForEdge(h4, h1), 445 AddVertexToClippedQuad3d(ComputeClippedCartesianPoint3dForEdge(h4, h1),
406 clipped_quad, num_vertices_in_clipped_quad); 446 clipped_quad, num_vertices_in_clipped_quad);
407 } 447 }
408 448
449 if (*num_vertices_in_clipped_quad > 2) {
450 if (isNearlyTheSame(clipped_quad[0],
451 clipped_quad[*num_vertices_in_clipped_quad - 1])
452 {
453 *num_vertices_in_clipped_quad -= 1;
454 }
455 }
456
409 DCHECK_LE(*num_vertices_in_clipped_quad, 8); 457 DCHECK_LE(*num_vertices_in_clipped_quad, 8);
410 return (*num_vertices_in_clipped_quad >= 4); 458 return (*num_vertices_in_clipped_quad >= 4);
411 } 459 }
412 460
413 gfx::RectF MathUtil::ComputeEnclosingRectOfVertices( 461 gfx::RectF MathUtil::ComputeEnclosingRectOfVertices(
414 const gfx::PointF vertices[], 462 const gfx::PointF vertices[],
415 int num_vertices) { 463 int num_vertices) {
416 if (num_vertices < 2) 464 if (num_vertices < 2)
417 return gfx::RectF(); 465 return gfx::RectF();
418 466
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after
928 _mm_setcsr(orig_state_ | 0x8040); 976 _mm_setcsr(orig_state_ | 0x8040);
929 #endif 977 #endif
930 } 978 }
931 979
932 ScopedSubnormalFloatDisabler::~ScopedSubnormalFloatDisabler() { 980 ScopedSubnormalFloatDisabler::~ScopedSubnormalFloatDisabler() {
933 #ifdef __SSE__ 981 #ifdef __SSE__
934 _mm_setcsr(orig_state_); 982 _mm_setcsr(orig_state_);
935 #endif 983 #endif
936 } 984 }
937 985
986 bool IsNearlyTheSameForUnitTesting(const float left, const float right) {
danakj 2017/01/11 23:12:06 ForTesting is the suffix that presubmits look for.
Peter Mayo 2017/01/11 23:47:04 Yes, because it would stress the definitions. Did
danakj 2017/01/11 23:53:36 They look for non-test code calling the function a
987 return isNearlyTheSame(left, right);
988 }
989
990 bool IsNearlyTheSameForUnitTesting(const gfx::PointF& left,
991 const gfx::PointF& right) {
992 return isNearlyTheSame(left, right);
993 }
994
995 bool IsNearlyTheSameForUnitTesting(const gfx::Point3F& left,
996 const gfx::Point3F& right) {
997 return isNearlyTheSame(left, right);
998 }
999
938 } // namespace cc 1000 } // namespace cc
OLDNEW
« no previous file with comments | « cc/base/math_util.h ('k') | cc/base/math_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698