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

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

Issue 2551263002: Don't add duplicate points when clipping (Closed)
Patch Set: Add approximate truncation, with tests. 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 unified diff | Download patch
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 approx(const float f, const float g) {
flackr 2016/12/08 23:31:06 Maybe approximatelyEqual to be verbose?
Peter Mayo 2016/12/14 23:13:11 I think nearlyTheSame covers all of the overloaded
159 static const float epsilon_scale = 0.00001;
flackr 2016/12/08 23:31:06 nit: 0.00001f
Peter Mayo 2016/12/14 23:13:11 Acknowledged.
160 return std::abs(f - g) <
flackr 2016/12/08 23:31:06 nit: I think <= might be safer in case of precisio
Peter Mayo 2016/12/14 23:13:11 I worry that it would worsely cover over inappropr
161 epsilon_scale *
162 std::max(std::max(std::abs(f), std::abs(g)), epsilon_scale);
163 }
164
165 static inline bool approx(const gfx::PointF& lhs, const gfx::PointF& rhs) {
166 return approx(lhs.x(), rhs.x()) && approx(lhs.y(), rhs.y());
167 }
168
169 static inline bool approx(const gfx::Point3F& lhs, const gfx::Point3F& rhs) {
170 return approx(lhs.x(), rhs.x()) && approx(lhs.y(), rhs.y()) &&
171 approx(lhs.z(), rhs.z());
172 }
173
158 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex, 174 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex,
159 gfx::PointF clipped_quad[8], 175 gfx::PointF clipped_quad[8],
160 int* num_vertices_in_clipped_quad) { 176 int* num_vertices_in_clipped_quad) {
177 if (*num_vertices_in_clipped_quad > 0) {
178 if (approx(clipped_quad[*num_vertices_in_clipped_quad - 1], new_vertex))
179 return;
180 if (approx(clipped_quad[0], new_vertex))
181 return;
182 }
161 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; 183 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex;
162 (*num_vertices_in_clipped_quad)++; 184 (*num_vertices_in_clipped_quad)++;
163 } 185 }
164 186
165 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, 187 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex,
166 gfx::Point3F clipped_quad[8], 188 gfx::Point3F clipped_quad[8],
167 int* num_vertices_in_clipped_quad) { 189 int* num_vertices_in_clipped_quad) {
190 if (*num_vertices_in_clipped_quad > 0) {
191 if (approx(clipped_quad[*num_vertices_in_clipped_quad - 1], new_vertex))
192 return;
193 if (approx(clipped_quad[0], new_vertex))
194 return;
195 }
168 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; 196 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex;
169 (*num_vertices_in_clipped_quad)++; 197 (*num_vertices_in_clipped_quad)++;
170 } 198 }
171 199
172 gfx::Rect MathUtil::MapEnclosingClippedRect(const gfx::Transform& transform, 200 gfx::Rect MathUtil::MapEnclosingClippedRect(const gfx::Transform& transform,
173 const gfx::Rect& src_rect) { 201 const gfx::Rect& src_rect) {
174 if (transform.IsIdentityOrIntegerTranslation()) { 202 if (transform.IsIdentityOrIntegerTranslation()) {
175 gfx::Vector2d offset(static_cast<int>(transform.matrix().getFloat(0, 3)), 203 gfx::Vector2d offset(static_cast<int>(transform.matrix().getFloat(0, 3)),
176 static_cast<int>(transform.matrix().getFloat(1, 3))); 204 static_cast<int>(transform.matrix().getFloat(1, 3)));
177 return src_rect + offset; 205 return src_rect + offset;
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
928 _mm_setcsr(orig_state_ | 0x8040); 956 _mm_setcsr(orig_state_ | 0x8040);
929 #endif 957 #endif
930 } 958 }
931 959
932 ScopedSubnormalFloatDisabler::~ScopedSubnormalFloatDisabler() { 960 ScopedSubnormalFloatDisabler::~ScopedSubnormalFloatDisabler() {
933 #ifdef __SSE__ 961 #ifdef __SSE__
934 _mm_setcsr(orig_state_); 962 _mm_setcsr(orig_state_);
935 #endif 963 #endif
936 } 964 }
937 965
966 bool IsApproximatelyForUnitTesting(const float left, const float right) {
967 return approx(left, right);
968 }
969
970 bool IsApproximatelyForUnitTesting(const gfx::PointF& left,
971 const gfx::PointF& right) {
972 return approx(left, right);
973 }
974
975 bool IsApproximatelyForUnitTesting(const gfx::Point3F& left,
976 const gfx::Point3F& right) {
977 return approx(left, right);
978 }
979
938 } // namespace cc 980 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698