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 | |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 17 | #ifndef LATINIME_GEOMETRY_UTILS_H |
| 18 | #define LATINIME_GEOMETRY_UTILS_H |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 19 | |
| 20 | #include <cmath> |
| 21 | |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 22 | #define MAX_PATHS 2 |
| 23 | |
| 24 | #define DEBUG_DECODER false |
| 25 | |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame] | 26 | #define M_PI_F 3.14159265f |
| 27 | |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 28 | namespace latinime { |
| 29 | |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 30 | static inline float squareFloat(float x) { |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 31 | return x * x; |
| 32 | } |
| 33 | |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 34 | static inline float getSquaredDistanceFloat(float x1, float y1, float x2, float y2) { |
| 35 | return squareFloat(x1 - x2) + squareFloat(y1 - y2); |
| 36 | } |
| 37 | |
| 38 | static inline float getDistanceFloat(float x1, float y1, float x2, float y2) { |
| 39 | return hypotf(x1 - x2, y1 - y2); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 40 | } |
| 41 | |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame] | 42 | static inline int getDistanceInt(int x1, int y1, int x2, int y2) { |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 43 | return static_cast<int>(getDistanceFloat(static_cast<float>(x1), static_cast<float>(y1), |
| 44 | static_cast<float>(x2), static_cast<float>(y2))); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static inline float getAngle(int x1, int y1, int x2, int y2) { |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 48 | const int dx = x1 - x2; |
| 49 | const int dy = y1 - y2; |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 50 | if (dx == 0 && dy == 0) return 0; |
| 51 | return atan2f(static_cast<float>(dy), static_cast<float>(dx)); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 52 | } |
| 53 | |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 54 | static inline float getAngleDiff(float a1, float a2) { |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 55 | const float diff = fabsf(a1 - a2); |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame] | 56 | if (diff > M_PI_F) { |
| 57 | return 2.0f * M_PI_F - diff; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 58 | } |
| 59 | return diff; |
| 60 | } |
| 61 | |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 62 | // static float pointToLineSegSquaredDistanceFloat( |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame] | 63 | // float x, float y, float x1, float y1, float x2, float y2) { |
| 64 | // float A = x - x1; |
| 65 | // float B = y - y1; |
| 66 | // float C = x2 - x1; |
| 67 | // float D = y2 - y1; |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 68 | // return fabsf(A * D - C * B) / sqrtf(C * C + D * D); |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame] | 69 | // } |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 70 | |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 71 | static inline float pointToLineSegSquaredDistanceFloat( |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 72 | float x, float y, float x1, float y1, float x2, float y2) { |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 73 | const float ray1x = x - x1; |
| 74 | const float ray1y = y - y1; |
| 75 | const float ray2x = x2 - x1; |
| 76 | const float ray2y = y2 - y1; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 77 | |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 78 | const float dotProduct = ray1x * ray2x + ray1y * ray2y; |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 79 | const float lineLengthSqr = squareFloat(ray2x) + squareFloat(ray2y); |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 80 | const float projectionLengthSqr = dotProduct / lineLengthSqr; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 81 | |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 82 | float projectionX; |
| 83 | float projectionY; |
| 84 | if (projectionLengthSqr < 0.0f) { |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 85 | projectionX = x1; |
| 86 | projectionY = y1; |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 87 | } else if (projectionLengthSqr > 1.0f) { |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 88 | projectionX = x2; |
| 89 | projectionY = y2; |
| 90 | } else { |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 91 | projectionX = x1 + projectionLengthSqr * ray2x; |
| 92 | projectionY = y1 + projectionLengthSqr * ray2y; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 93 | } |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 94 | return getSquaredDistanceFloat(x, y, projectionX, projectionY); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 95 | } |
| 96 | } // namespace latinime |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 97 | #endif // LATINIME_GEOMETRY_UTILS_H |