Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef LATINIME_INCREMENTAL_GEOMETRY_UTILS_H |
| 18 | #define LATINIME_INCREMENTAL_GEOMETRY_UTILS_H |
| 19 | |
| 20 | #include <cmath> |
| 21 | |
| 22 | #define MAX_DISTANCE 10000000 |
| 23 | #define KEY_NUM 27 |
| 24 | #define SPACE_KEY 26 |
| 25 | #define MAX_PATHS 2 |
| 26 | |
| 27 | #define DEBUG_DECODER false |
| 28 | |
| 29 | namespace latinime { |
| 30 | |
| 31 | static inline float sqr(float x) { |
| 32 | return x * x; |
| 33 | } |
| 34 | |
| 35 | static inline float getNormalizedSqrDistance(int x1, int y1, int x2, int y2, int scale) { |
| 36 | return sqr((x1 - x2) * 1.0 / scale) + sqr((y1 - y2) * 1.0 / scale); |
| 37 | } |
| 38 | |
| 39 | static inline int getDistance(int x1, int y1, int x2, int y2) { |
| 40 | return (int) sqrt(sqr(x2 - x1) + sqr(y2 - y1)); |
| 41 | } |
| 42 | |
| 43 | static inline float getDistanceSq(float x1, float y1, float x2, float y2) { |
| 44 | return sqr(x2 - x1) + sqr(y2 - y1); |
| 45 | } |
| 46 | |
| 47 | static inline float getAngle(int x1, int y1, int x2, int y2) { |
| 48 | float dx = x1 - x2; |
| 49 | float dy = y1 - y2; |
| 50 | if (dx == 0 && dy == 0) |
| 51 | return 0; |
| 52 | return atan2(dy, dx); |
| 53 | } |
| 54 | |
| 55 | static inline float angleDiff(float a1, float a2) { |
| 56 | float diff = a1 - a2; |
| 57 | if (diff < 0) { |
| 58 | diff = -diff; |
| 59 | } |
| 60 | if (diff > M_PI) { |
| 61 | return 2 * M_PI - diff; |
| 62 | } |
| 63 | return diff; |
| 64 | } |
| 65 | |
| 66 | //static float pointToLineDistanceSq(float x, float y, float x1, float y1, float x2, float y2) { |
| 67 | // float A = x - x1; |
| 68 | // float B = y - y1; |
| 69 | // float C = x2 - x1; |
| 70 | // float D = y2 - y1; |
| 71 | // return abs(A * D - C * B) / sqrt(C * C + D * D); |
| 72 | //} |
| 73 | |
| 74 | static inline float pointToLineSegDistanceSq( |
| 75 | float x, float y, float x1, float y1, float x2, float y2) { |
| 76 | float ray1x = x - x1; |
| 77 | float ray1y = y - y1; |
| 78 | float ray2x = x2 - x1; |
| 79 | float ray2y = y2 - y1; |
| 80 | |
| 81 | float dotProduct = ray1x * ray2x + ray1y * ray2y; |
| 82 | float lineLengthSq = ray2x * ray2x + ray2y * ray2y; |
| 83 | float projectionLengthSq = dotProduct / lineLengthSq; |
| 84 | |
| 85 | float projectionX, projectionY; |
| 86 | if (projectionLengthSq < 0) { |
| 87 | projectionX = x1; |
| 88 | projectionY = y1; |
| 89 | } else if (projectionLengthSq > 1) { |
| 90 | projectionX = x2; |
| 91 | projectionY = y2; |
| 92 | } else { |
| 93 | projectionX = x1 + projectionLengthSq * ray2x; |
| 94 | projectionY = y1 + projectionLengthSq * ray2y; |
| 95 | } |
| 96 | |
| 97 | float dist = getDistanceSq(x, y, projectionX, projectionY); |
| 98 | return dist; |
| 99 | } |
| 100 | } // namespace latinime |
| 101 | #endif // LATINIME_INCREMENTAL_GEOMETRY_UTILS_H |