Commit b63a661d authored by jdduke@chromium.org's avatar jdduke@chromium.org

Optimize QuadF's PointIsInTriangle function

The original version required 34 float ops (16mul + 11sub + 5add + 2div).
This version removes some of the intermediate dot products, reducing the
max number of operations to 19 (6mul + 2div + 11sub).  It also avoids using
doubles, warranted by the removal of several intermediate operations that might
otherwise incur significance loss.

In local (release build) benchmarks, this reduced the typical CPU cost of a call to
QuadF::Contains(...) by 2/3, both with GCC and Clang on an ARM v7 device.

Review URL: https://codereview.chromium.org/291493003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271088 0039d316-1c4b-4281-b951-d872f2087c98
parent 95434360
...@@ -62,28 +62,24 @@ static inline bool PointIsInTriangle(const PointF& point, ...@@ -62,28 +62,24 @@ static inline bool PointIsInTriangle(const PointF& point,
const PointF& r1, const PointF& r1,
const PointF& r2, const PointF& r2,
const PointF& r3) { const PointF& r3) {
// Compute the barycentric coordinates of |point| relative to the triangle // Compute the barycentric coordinates (u, v, w) of |point| relative to the
// (r1, r2, r3). This algorithm comes from Christer Ericson's Real-Time // triangle (r1, r2, r3) by the solving the system of equations:
// Collision Detection. // 1) point = u * r1 + v * r2 + w * r3
Vector2dF v0 = r2 - r1; // 2) u + v + w = 1
Vector2dF v1 = r3 - r1; // This algorithm comes from Christer Ericson's Real-Time Collision Detection.
Vector2dF v2 = point - r1;
double dot00 = DotProduct(v0, v0); Vector2dF r31 = r1 - r3;
double dot01 = DotProduct(v0, v1); Vector2dF r32 = r2 - r3;
double dot11 = DotProduct(v1, v1); Vector2dF r3p = point - r3;
double dot20 = DotProduct(v2, v0);
double dot21 = DotProduct(v2, v1);
double denom = dot00 * dot11 - dot01 * dot01; float denom = r32.y() * r31.x() - r32.x() * r31.y();
float u = (r32.y() * r3p.x() - r32.x() * r3p.y()) / denom;
double v = (dot11 * dot20 - dot01 * dot21) / denom; float v = (r31.x() * r3p.y() - r31.y() * r3p.x()) / denom;
double w = (dot00 * dot21 - dot01 * dot20) / denom; float w = 1.f - u - v;
double u = 1 - v - w;
// Use the barycentric coordinates to test if |point| is inside the // Use the barycentric coordinates to test if |point| is inside the
// triangle (r1, r2, r2). // triangle (r1, r2, r2).
return (v >= 0) && (w >= 0) && (u >= 0); return (u >= 0) && (v >= 0) && (w >= 0);
} }
bool QuadF::Contains(const PointF& point) const { bool QuadF::Contains(const PointF& point) const {
......
...@@ -52,7 +52,7 @@ class GFX_EXPORT QuadF { ...@@ -52,7 +52,7 @@ class GFX_EXPORT QuadF {
bool IsCounterClockwise() const; bool IsCounterClockwise() const;
// Returns true if the |point| is contained within the quad, or lies on on // Returns true if the |point| is contained within the quad, or lies on on
// edge of the quad. // edge of the quad. This assumes that the quad is convex.
bool Contains(const gfx::PointF& point) const; bool Contains(const gfx::PointF& point) const;
// Returns a rectangle that bounds the four points of the quad. The points of // Returns a rectangle that bounds the four points of the quad. The points of
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment