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

Unified Diff: ui/gfx/geometry/quad_f.cc

Issue 458173002: Optimize QuadF's PointIsInTriangle function (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 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
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/geometry/quad_f.cc
diff --git a/ui/gfx/geometry/quad_f.cc b/ui/gfx/geometry/quad_f.cc
index dbc50458b3fa1e04aedfdee3c51d5ae00351b46a..0c8473c3e3eb262c98cd53fe0aa184e8b670534b 100644
--- a/ui/gfx/geometry/quad_f.cc
+++ b/ui/gfx/geometry/quad_f.cc
@@ -62,24 +62,31 @@ static inline bool PointIsInTriangle(const PointF& point,
const PointF& r1,
const PointF& r2,
const PointF& r3) {
- // Compute the barycentric coordinates (u, v, w) of |point| relative to the
- // triangle (r1, r2, r3) by the solving the system of equations:
- // 1) point = u * r1 + v * r2 + w * r3
- // 2) u + v + w = 1
- // This algorithm comes from Christer Ericson's Real-Time Collision Detection.
-
- Vector2dF r31 = r1 - r3;
- Vector2dF r32 = r2 - r3;
- Vector2dF r3p = point - r3;
-
- float denom = r32.y() * r31.x() - r32.x() * r31.y();
- float u = (r32.y() * r3p.x() - r32.x() * r3p.y()) / denom;
- float v = (r31.x() * r3p.y() - r31.y() * r3p.x()) / denom;
- float w = 1.f - u - v;
-
- // Use the barycentric coordinates to test if |point| is inside the
- // triangle (r1, r2, r2).
- return (u >= 0) && (v >= 0) && (w >= 0);
+ // Translate point and triangle so that point lies at origin.
+ // Then checking if the origin is contained in the translated triangle.
+ // The origin O lies inside ABC if and only if the triangles OAB, OBC,
+ // and OCA are all either clockwise or counterclockwise.
+ // This algorithm is from Christer Ericson's Real-Time Collision Detection.
+
+ Vector2dF a = r1 - point;
+ Vector2dF b = r2 - point;
+ Vector2dF c = r3 - point;
+ // Quick test doubles have same sign
danakj 2014/08/11 14:09:26 style nit: comments should be full sentences inclu
kui.zheng 2014/08/14 06:44:17 Remove comments due to algorithm is updated
+ #define DOUBLE_SIGN_MASK UINT64_C(0x8000000000000000)
danakj 2014/08/11 14:09:26 Can you use "static const uint64 kDoubleSignMask"
kui.zheng 2014/08/14 06:44:16 Remove "Mask" due to algorithm is updated
+ #define DOUBLE_SAME_SIGN(d1, d2) \
danakj 2014/08/11 14:09:26 Is the performance changed if you use a static met
kui.zheng 2014/08/14 06:44:16 Compiler should help inlining *short* static metho
+ (((reinterpret_cast<__uint64_t&>(d1)) ^ \
danakj 2014/08/11 14:09:26 i think the extra () around the reinterpret_cast m
kui.zheng 2014/08/14 06:44:17 Same as above.
+ (reinterpret_cast<__uint64_t&>(d2))) & \
danakj 2014/08/11 14:09:26 Is doing XOR and AND really all that better than d
kui.zheng 2014/08/14 06:44:16 The micro-bench(on armv7) result shows manual "XOR
+ DOUBLE_SIGN_MASK)
+
+ // Normal vectors of triangles obc and oca
+ double u = CrossProduct(b, c);
+ double v = CrossProduct(c, a);
+ if(DOUBLE_SAME_SIGN(u, v)) return false;
danakj 2014/08/11 14:09:26 This is checking if they have different signs not
kui.zheng 2014/08/14 06:44:16 Oh yes, it's really a bad name. The Macro would be
+ // Normal vector of triangle oab
+ double w = CrossProduct(a, b);
+ if(DOUBLE_SAME_SIGN(u, w)) return false;
+
+ return true;
}
bool QuadF::Contains(const PointF& point) const {
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698