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

Unified Diff: cc/base/math_util.cc

Issue 2551263002: Don't add duplicate points when clipping (Closed)
Patch Set: danakj comments #15 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 side-by-side diff with in-line comments
Download patch
Index: cc/base/math_util.cc
diff --git a/cc/base/math_util.cc b/cc/base/math_util.cc
index d28c195971f6cd14eb753b6d9eddd51d1dd082fb..ff9906c064a69150d86984a852e7dfd8f681a567 100644
--- a/cc/base/math_util.cc
+++ b/cc/base/math_util.cc
@@ -155,9 +155,36 @@ static inline void ExpandBoundsToIncludePoint(float* xmin,
*ymax = std::max(p.y(), *ymax);
}
+static inline bool IsNearlyTheSame(const float f, const float g) {
+ // The idea behind this is to use this fraction of the larger of the
+ // two numbers as the limit of the difference. This breaks down near
+ // zero, so we reuse this as the minimum absolute size we will use
+ // for the base of the scale too.
+ static const float epsilon_scale = 0.00001f;
+ return std::abs(f - g) <
+ epsilon_scale *
+ std::max(std::max(std::abs(f), std::abs(g)), epsilon_scale);
+}
+
+static inline bool IsNearlyTheSame(const gfx::PointF& lhs,
+ const gfx::PointF& rhs) {
+ return IsNearlyTheSame(lhs.x(), rhs.x()) && IsNearlyTheSame(lhs.y(), rhs.y());
+}
+
+static inline bool IsNearlyTheSame(const gfx::Point3F& lhs,
+ const gfx::Point3F& rhs) {
+ return IsNearlyTheSame(lhs.x(), rhs.x()) &&
+ IsNearlyTheSame(lhs.y(), rhs.y()) && IsNearlyTheSame(lhs.z(), rhs.z());
+}
+
static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex,
gfx::PointF clipped_quad[8],
int* num_vertices_in_clipped_quad) {
+ if (*num_vertices_in_clipped_quad > 0 &&
+ IsNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1],
+ new_vertex))
+ return;
+
clipped_quad[*num_vertices_in_clipped_quad] = new_vertex;
(*num_vertices_in_clipped_quad)++;
}
@@ -165,6 +192,11 @@ static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex,
static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex,
gfx::Point3F clipped_quad[8],
int* num_vertices_in_clipped_quad) {
+ if (*num_vertices_in_clipped_quad > 0 &&
+ IsNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1],
+ new_vertex))
+ return;
+
clipped_quad[*num_vertices_in_clipped_quad] = new_vertex;
(*num_vertices_in_clipped_quad)++;
}
@@ -345,6 +377,11 @@ void MathUtil::MapClippedQuad(const gfx::Transform& transform,
clipped_quad, num_vertices_in_clipped_quad);
}
+ if (*num_vertices_in_clipped_quad > 2 &&
+ IsNearlyTheSame(clipped_quad[0],
+ clipped_quad[*num_vertices_in_clipped_quad - 1]))
+ *num_vertices_in_clipped_quad -= 1;
+
DCHECK_LE(*num_vertices_in_clipped_quad, 8);
}
@@ -406,6 +443,11 @@ bool MathUtil::MapClippedQuad3d(const gfx::Transform& transform,
clipped_quad, num_vertices_in_clipped_quad);
}
+ if (*num_vertices_in_clipped_quad > 2 &&
+ IsNearlyTheSame(clipped_quad[0],
+ clipped_quad[*num_vertices_in_clipped_quad - 1]))
+ *num_vertices_in_clipped_quad -= 1;
+
DCHECK_LE(*num_vertices_in_clipped_quad, 8);
return (*num_vertices_in_clipped_quad >= 4);
}
@@ -935,4 +977,18 @@ ScopedSubnormalFloatDisabler::~ScopedSubnormalFloatDisabler() {
#endif
}
+bool IsNearlyTheSameForTesting(float left, float right) {
+ return IsNearlyTheSame(left, right);
+}
+
+bool IsNearlyTheSameForTesting(const gfx::PointF& left,
+ const gfx::PointF& right) {
+ return IsNearlyTheSame(left, right);
+}
+
+bool IsNearlyTheSameForTesting(const gfx::Point3F& left,
+ const gfx::Point3F& right) {
+ return IsNearlyTheSame(left, right);
+}
+
} // namespace cc
« cc/base/math_util.h ('K') | « 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