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 | |
Ken Wakasa | 6e66349 | 2012-11-03 02:50:47 +0900 | [diff] [blame] | 22 | #include "defines.h" |
| 23 | |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame] | 24 | #define M_PI_F 3.14159265f |
Satoshi Kataoka | f4554d8 | 2012-09-12 20:50:21 +0900 | [diff] [blame] | 25 | #define ROUND_FLOAT_10000(f) ((f) < 1000.0f && (f) > 0.001f) \ |
| 26 | ? (floorf((f) * 10000.0f) / 10000.0f) : (f) |
Satoshi Kataoka | f4554d8 | 2012-09-12 20:50:21 +0900 | [diff] [blame] | 27 | |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 28 | namespace latinime { |
| 29 | |
Ken Wakasa | a323fa6 | 2012-11-02 12:14:34 +0900 | [diff] [blame] | 30 | static inline float SQUARE_FLOAT(const float x) { return x * x; } |
| 31 | |
| 32 | static inline float getSquaredDistanceFloat(const float x1, const float y1, const float x2, |
| 33 | const float y2) { |
| 34 | return SQUARE_FLOAT(x1 - x2) + SQUARE_FLOAT(y1 - y2); |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 35 | } |
| 36 | |
Ken Wakasa | 6e66349 | 2012-11-03 02:50:47 +0900 | [diff] [blame] | 37 | static AK_FORCE_INLINE int getDistanceInt(const int x1, const int y1, const int x2, const int y2) { |
Ken Wakasa | 6cee61d | 2013-01-15 16:15:48 +0900 | [diff] [blame] | 38 | return static_cast<int>(hypotf(static_cast<float>(x1 - x2), static_cast<float>(y1 - y2))); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 39 | } |
| 40 | |
Ken Wakasa | 6e66349 | 2012-11-03 02:50:47 +0900 | [diff] [blame] | 41 | static AK_FORCE_INLINE float getAngle(const int x1, const int y1, const int x2, const int y2) { |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 42 | const int dx = x1 - x2; |
| 43 | const int dy = y1 - y2; |
Ken Wakasa | 07711c1 | 2012-12-27 16:35:51 +0900 | [diff] [blame] | 44 | if (dx == 0 && dy == 0) return 0.0f; |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 45 | return atan2f(static_cast<float>(dy), static_cast<float>(dx)); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 46 | } |
| 47 | |
Ken Wakasa | 6e66349 | 2012-11-03 02:50:47 +0900 | [diff] [blame] | 48 | static AK_FORCE_INLINE float getAngleDiff(const float a1, const float a2) { |
Satoshi Kataoka | f4554d8 | 2012-09-12 20:50:21 +0900 | [diff] [blame] | 49 | const float deltaA = fabsf(a1 - a2); |
| 50 | const float diff = ROUND_FLOAT_10000(deltaA); |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame] | 51 | if (diff > M_PI_F) { |
Satoshi Kataoka | f4554d8 | 2012-09-12 20:50:21 +0900 | [diff] [blame] | 52 | const float normalizedDiff = 2.0f * M_PI_F - diff; |
| 53 | return ROUND_FLOAT_10000(normalizedDiff); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 54 | } |
| 55 | return diff; |
| 56 | } |
| 57 | |
Ken Wakasa | a323fa6 | 2012-11-02 12:14:34 +0900 | [diff] [blame] | 58 | static inline float pointToLineSegSquaredDistanceFloat(const float x, const float y, const float x1, |
| 59 | const float y1, const float x2, const float y2, const bool extend) { |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 60 | const float ray1x = x - x1; |
| 61 | const float ray1y = y - y1; |
| 62 | const float ray2x = x2 - x1; |
| 63 | const float ray2y = y2 - y1; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 64 | |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 65 | const float dotProduct = ray1x * ray2x + ray1y * ray2y; |
Satoshi Kataoka | f4554d8 | 2012-09-12 20:50:21 +0900 | [diff] [blame] | 66 | const float lineLengthSqr = SQUARE_FLOAT(ray2x) + SQUARE_FLOAT(ray2y); |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 67 | const float projectionLengthSqr = dotProduct / lineLengthSqr; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 68 | |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 69 | float projectionX; |
| 70 | float projectionY; |
Keisuke Kuroyanagi | 41f12ee | 2012-09-19 12:03:47 +0900 | [diff] [blame] | 71 | if (!extend && projectionLengthSqr < 0.0f) { |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 72 | projectionX = x1; |
| 73 | projectionY = y1; |
Keisuke Kuroyanagi | 41f12ee | 2012-09-19 12:03:47 +0900 | [diff] [blame] | 74 | } else if (!extend && projectionLengthSqr > 1.0f) { |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 75 | projectionX = x2; |
| 76 | projectionY = y2; |
| 77 | } else { |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 78 | projectionX = x1 + projectionLengthSqr * ray2x; |
| 79 | projectionY = y1 + projectionLengthSqr * ray2y; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 80 | } |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 81 | return getSquaredDistanceFloat(x, y, projectionX, projectionY); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 82 | } |
Keisuke Kuroyanagi | 806eba4 | 2012-10-09 19:57:08 +0900 | [diff] [blame] | 83 | |
| 84 | // Normal distribution N(u, sigma^2). |
| 85 | struct NormalDistribution { |
Ken Wakasa | 07711c1 | 2012-12-27 16:35:51 +0900 | [diff] [blame] | 86 | public: |
Keisuke Kuroyanagi | 806eba4 | 2012-10-09 19:57:08 +0900 | [diff] [blame] | 87 | NormalDistribution(const float u, const float sigma) |
| 88 | : mU(u), mSigma(sigma), |
Keisuke Kuroyanagi | ff74cc3 | 2012-10-11 13:08:06 +0900 | [diff] [blame] | 89 | mPreComputedNonExpPart(1.0f / sqrtf(2.0f * M_PI_F * SQUARE_FLOAT(sigma))), |
| 90 | mPreComputedExponentPart(-1.0f / (2.0f * SQUARE_FLOAT(sigma))) {} |
Keisuke Kuroyanagi | 806eba4 | 2012-10-09 19:57:08 +0900 | [diff] [blame] | 91 | |
Ken Wakasa | a323fa6 | 2012-11-02 12:14:34 +0900 | [diff] [blame] | 92 | float getProbabilityDensity(const float x) const { |
Keisuke Kuroyanagi | 806eba4 | 2012-10-09 19:57:08 +0900 | [diff] [blame] | 93 | const float shiftedX = x - mU; |
| 94 | return mPreComputedNonExpPart * expf(mPreComputedExponentPart * SQUARE_FLOAT(shiftedX)); |
| 95 | } |
Ken Wakasa | a323fa6 | 2012-11-02 12:14:34 +0900 | [diff] [blame] | 96 | |
Keisuke Kuroyanagi | 806eba4 | 2012-10-09 19:57:08 +0900 | [diff] [blame] | 97 | private: |
| 98 | DISALLOW_IMPLICIT_CONSTRUCTORS(NormalDistribution); |
Ken Wakasa | 07711c1 | 2012-12-27 16:35:51 +0900 | [diff] [blame] | 99 | const float mU; // mean value |
| 100 | const float mSigma; // standard deviation |
| 101 | const float mPreComputedNonExpPart; // = 1 / sqrt(2 * PI * sigma^2) |
| 102 | const float mPreComputedExponentPart; // = -1 / (2 * sigma^2) |
Ken Wakasa | a323fa6 | 2012-11-02 12:14:34 +0900 | [diff] [blame] | 103 | }; // struct NormalDistribution |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 104 | } // namespace latinime |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 105 | #endif // LATINIME_GEOMETRY_UTILS_H |