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

Unified Diff: cc/base/math_util.cc

Issue 2551263002: Don't add duplicate points when clipping (Closed)
Patch Set: Readability and review changes Created 4 years 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..b34eb5ef03f13c148bc3862b3aa83fbb44fd7330 100644
--- a/cc/base/math_util.cc
+++ b/cc/base/math_util.cc
@@ -155,9 +155,34 @@ static inline void ExpandBoundsToIncludePoint(float* xmin,
*ymax = std::max(p.y(), *ymax);
}
+static inline bool isNearlyTheSame(const float f, const float g) {
+ static const float epsilon_scale = 0.00001f;
flackr 2017/01/11 18:23:57 nit: Can you add a short comment as to why / how w
Peter Mayo 2017/01/11 22:23:31 Done.
+ 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) {
+ if (isNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1],
+ new_vertex))
+ return;
+ if (isNearlyTheSame(clipped_quad[0], new_vertex))
flackr 2017/01/11 18:23:57 Instead of comparing every added vertex with verte
Peter Mayo 2017/01/11 22:23:31 This expands the scope in which the "isNearlyTheSa
+ return;
+ }
clipped_quad[*num_vertices_in_clipped_quad] = new_vertex;
(*num_vertices_in_clipped_quad)++;
}
@@ -165,6 +190,13 @@ 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) {
+ if (isNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1],
+ new_vertex))
+ return;
+ if (isNearlyTheSame(clipped_quad[0], new_vertex))
+ return;
+ }
clipped_quad[*num_vertices_in_clipped_quad] = new_vertex;
(*num_vertices_in_clipped_quad)++;
}
@@ -935,4 +967,18 @@ ScopedSubnormalFloatDisabler::~ScopedSubnormalFloatDisabler() {
#endif
}
+bool IsNearlyTheSameForUnitTesting(const float left, const float right) {
+ return isNearlyTheSame(left, right);
+}
+
+bool IsNearlyTheSameForUnitTesting(const gfx::PointF& left,
+ const gfx::PointF& right) {
+ return isNearlyTheSame(left, right);
+}
+
+bool IsNearlyTheSameForUnitTesting(const gfx::Point3F& left,
+ const gfx::Point3F& right) {
+ return isNearlyTheSame(left, right);
+}
+
} // namespace cc

Powered by Google App Engine
This is Rietveld 408576698