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 |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 23 | #define MAX_PATHS 2 |
| 24 | |
| 25 | #define DEBUG_DECODER false |
| 26 | |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 27 | #define M_PI_F 3.14159265f |
| 28 | |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 29 | namespace latinime { |
| 30 | |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 31 | static inline float sqrf(float x) { |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 32 | return x * x; |
| 33 | } |
| 34 | |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 35 | static inline float getNormalizedSqrDistanceFloat(int x1, int y1, int x2, int y2, int scale) { |
| 36 | return sqrf(static_cast<float>(x1 - x2) / static_cast<float>(scale)) |
| 37 | + sqrf(static_cast<float>(y1 - y2) / static_cast<float>(scale)); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 38 | } |
| 39 | |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 40 | static inline float getDistanceSqrFloat(float x1, float y1, float x2, float y2) { |
| 41 | return sqrf(x2 - x1) + sqrf(y2 - y1); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 42 | } |
| 43 | |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 44 | static inline int getDistanceInt(int x1, int y1, int x2, int y2) { |
| 45 | return static_cast<int>( |
| 46 | sqrtf(getDistanceSqrFloat( |
| 47 | static_cast<float>(x1), static_cast<float>(y1), |
| 48 | static_cast<float>(x2), static_cast<float>(y2)))); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static inline float getAngle(int x1, int y1, int x2, int y2) { |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 52 | int dx = x1 - x2; |
| 53 | int dy = y1 - y2; |
| 54 | if (dx == 0 && dy == 0) { |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 55 | return 0; |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 56 | } |
| 57 | float dxf = static_cast<float>(dx); |
| 58 | float dyf = static_cast<float>(dy); |
| 59 | return atan2f(dyf, dxf); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static inline float angleDiff(float a1, float a2) { |
| 63 | float diff = a1 - a2; |
| 64 | if (diff < 0) { |
| 65 | diff = -diff; |
| 66 | } |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 67 | if (diff > M_PI_F) { |
| 68 | return 2.0f * M_PI_F - diff; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 69 | } |
| 70 | return diff; |
| 71 | } |
| 72 | |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 73 | // static float pointToLineDistanceSqrFloat( |
| 74 | // float x, float y, float x1, float y1, float x2, float y2) { |
| 75 | // float A = x - x1; |
| 76 | // float B = y - y1; |
| 77 | // float C = x2 - x1; |
| 78 | // float D = y2 - y1; |
| 79 | // return abs(A * D - C * B) / sqrt(C * C + D * D); |
| 80 | // } |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 81 | |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 82 | static inline float pointToLineSegDistanceSqrFloat( |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 83 | float x, float y, float x1, float y1, float x2, float y2) { |
| 84 | float ray1x = x - x1; |
| 85 | float ray1y = y - y1; |
| 86 | float ray2x = x2 - x1; |
| 87 | float ray2y = y2 - y1; |
| 88 | |
| 89 | float dotProduct = ray1x * ray2x + ray1y * ray2y; |
| 90 | float lineLengthSq = ray2x * ray2x + ray2y * ray2y; |
| 91 | float projectionLengthSq = dotProduct / lineLengthSq; |
| 92 | |
| 93 | float projectionX, projectionY; |
| 94 | if (projectionLengthSq < 0) { |
| 95 | projectionX = x1; |
| 96 | projectionY = y1; |
| 97 | } else if (projectionLengthSq > 1) { |
| 98 | projectionX = x2; |
| 99 | projectionY = y2; |
| 100 | } else { |
| 101 | projectionX = x1 + projectionLengthSq * ray2x; |
| 102 | projectionY = y1 + projectionLengthSq * ray2y; |
| 103 | } |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame^] | 104 | return getDistanceSqrFloat(x, y, projectionX, projectionY); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 105 | } |
| 106 | } // namespace latinime |
| 107 | #endif // LATINIME_INCREMENTAL_GEOMETRY_UTILS_H |