Merge "Cleanups in geometry_utils.h" into jb-mr1-dev

main
Ken Wakasa 2012-08-13 02:11:52 -07:00 committed by Android (Google) Code Review
commit 40a6d4af2a
2 changed files with 28 additions and 29 deletions

View File

@ -142,20 +142,21 @@ public class GestureStroke {
mLastIncrementalBatchSize = size; mLastIncrementalBatchSize = size;
} }
private static float getDistance(final int p1x, final int p1y, private static float getDistance(final int x1, final int y1, final int x2, final int y2) {
final int p2x, final int p2y) { final float dx = x1 - x2;
final float dx = p1x - p2x; final float dy = y1 - y2;
final float dy = p1y - p2y;
// Note that, in recent versions of Android, FloatMath is actually slower than // Note that, in recent versions of Android, FloatMath is actually slower than
// java.lang.Math due to the way the JIT optimizes java.lang.Math. // java.lang.Math due to the way the JIT optimizes java.lang.Math.
return (float)Math.sqrt(dx * dx + dy * dy); return (float)Math.sqrt(dx * dx + dy * dy);
} }
private static float getAngle(final int p1x, final int p1y, final int p2x, final int p2y) { private static float getAngle(final int x1, final int y1, final int x2, final int y2) {
final int dx = p1x - p2x; final int dx = x1 - x2;
final int dy = p1y - p2y; final int dy = y1 - y2;
if (dx == 0 && dy == 0) return 0; if (dx == 0 && dy == 0) return 0;
return (float)Math.atan2(dy, dx); // Would it be faster to call atan2f() directly via JNI? Not sure about what the JIT
// does with Math.atan2().
return (float)Math.atan2((double)dy, (double)dx);
} }
private static float getAngleDiff(final float a1, final float a2) { private static float getAngleDiff(final float a1, final float a2) {

View File

@ -28,38 +28,36 @@
namespace latinime { namespace latinime {
static inline float sqrf(float x) { static inline float squareFloat(float x) {
return x * x; return x * x;
} }
static inline float getNormalizedSqrDistanceFloat(int x1, int y1, int x2, int y2, int scale) { static inline float getNormalizedSquaredDistanceFloat(float x1, float y1, float x2, float y2,
return sqrf(static_cast<float>(x1 - x2) / static_cast<float>(scale)) float scale) {
+ sqrf(static_cast<float>(y1 - y2) / static_cast<float>(scale)); return squareFloat((x1 - x2) / scale) + squareFloat((y1 - y2) / scale);
} }
static inline float getDistanceSqrFloat(float x1, float y1, float x2, float y2) { static inline float getSquaredDistanceFloat(float x1, float y1, float x2, float y2) {
return sqrf(x2 - x1) + sqrf(y2 - y1); return squareFloat(x1 - x2) + squareFloat(y1 - y2);
}
static inline float getDistanceFloat(float x1, float y1, float x2, float y2) {
return hypotf(x1 - x2, y1 - y2);
} }
static inline int getDistanceInt(int x1, int y1, int x2, int y2) { static inline int getDistanceInt(int x1, int y1, int x2, int y2) {
return static_cast<int>( return static_cast<int>(getDistanceFloat(static_cast<float>(x1), static_cast<float>(y1),
sqrtf(getDistanceSqrFloat( static_cast<float>(x2), static_cast<float>(y2)));
static_cast<float>(x1), static_cast<float>(y1),
static_cast<float>(x2), static_cast<float>(y2))));
} }
static inline float getAngle(int x1, int y1, int x2, int y2) { static inline float getAngle(int x1, int y1, int x2, int y2) {
const int dx = x1 - x2; const int dx = x1 - x2;
const int dy = y1 - y2; const int dy = y1 - y2;
if (dx == 0 && dy == 0) { if (dx == 0 && dy == 0) return 0;
return 0; return atan2f(static_cast<float>(dy), static_cast<float>(dx));
}
const float dxf = static_cast<float>(dx);
const float dyf = static_cast<float>(dy);
return atan2f(dyf, dxf);
} }
static inline float angleDiff(float a1, float a2) { static inline float getAngleDiff(float a1, float a2) {
const float diff = fabsf(a1 - a2); const float diff = fabsf(a1 - a2);
if (diff > M_PI_F) { if (diff > M_PI_F) {
return 2.0f * M_PI_F - diff; return 2.0f * M_PI_F - diff;
@ -67,7 +65,7 @@ static inline float angleDiff(float a1, float a2) {
return diff; return diff;
} }
// static float pointToLineDistanceSqrFloat( // static float pointToLineSegSquaredDistanceFloat(
// float x, float y, float x1, float y1, float x2, float y2) { // float x, float y, float x1, float y1, float x2, float y2) {
// float A = x - x1; // float A = x - x1;
// float B = y - y1; // float B = y - y1;
@ -76,7 +74,7 @@ static inline float angleDiff(float a1, float a2) {
// return fabsf(A * D - C * B) / sqrtf(C * C + D * D); // return fabsf(A * D - C * B) / sqrtf(C * C + D * D);
// } // }
static inline float pointToLineSegDistanceSqrFloat( static inline float pointToLineSegSquaredDistanceFloat(
float x, float y, float x1, float y1, float x2, float y2) { float x, float y, float x1, float y1, float x2, float y2) {
const float ray1x = x - x1; const float ray1x = x - x1;
const float ray1y = y - y1; const float ray1y = y - y1;
@ -84,7 +82,7 @@ static inline float pointToLineSegDistanceSqrFloat(
const float ray2y = y2 - y1; const float ray2y = y2 - y1;
const float dotProduct = ray1x * ray2x + ray1y * ray2y; const float dotProduct = ray1x * ray2x + ray1y * ray2y;
const float lineLengthSqr = sqrf(ray2x) + sqrf(ray2y); const float lineLengthSqr = squareFloat(ray2x) + squareFloat(ray2y);
const float projectionLengthSqr = dotProduct / lineLengthSqr; const float projectionLengthSqr = dotProduct / lineLengthSqr;
float projectionX; float projectionX;
@ -99,7 +97,7 @@ static inline float pointToLineSegDistanceSqrFloat(
projectionX = x1 + projectionLengthSqr * ray2x; projectionX = x1 + projectionLengthSqr * ray2x;
projectionY = y1 + projectionLengthSqr * ray2y; projectionY = y1 + projectionLengthSqr * ray2y;
} }
return getDistanceSqrFloat(x, y, projectionX, projectionY); return getSquaredDistanceFloat(x, y, projectionX, projectionY);
} }
} // namespace latinime } // namespace latinime
#endif // LATINIME_GEOMETRY_UTILS_H #endif // LATINIME_GEOMETRY_UTILS_H