blob: 4060a7bd30235cead04544499758bd657093f935 [file] [log] [blame]
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +09001/*
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 Wakasa507113a2012-08-13 11:36:41 +090017#ifndef LATINIME_GEOMETRY_UTILS_H
18#define LATINIME_GEOMETRY_UTILS_H
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090019
20#include <cmath>
21
Ken Wakasa6e663492012-11-03 02:50:47 +090022#include "defines.h"
23
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090024#define DEBUG_DECODER false
25
Ken Wakasabcec82d2012-08-12 11:10:48 +090026#define M_PI_F 3.14159265f
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090027#define ROUND_FLOAT_10000(f) ((f) < 1000.0f && (f) > 0.001f) \
28 ? (floorf((f) * 10000.0f) / 10000.0f) : (f)
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090029
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090030namespace latinime {
31
Ken Wakasaa323fa62012-11-02 12:14:34 +090032static inline float SQUARE_FLOAT(const float x) { return x * x; }
33
34static inline float getSquaredDistanceFloat(const float x1, const float y1, const float x2,
35 const float y2) {
36 return SQUARE_FLOAT(x1 - x2) + SQUARE_FLOAT(y1 - y2);
Ken Wakasa0fb95082012-08-13 17:31:46 +090037}
38
Ken Wakasaa323fa62012-11-02 12:14:34 +090039static inline float getNormalizedSquaredDistanceFloat(const float x1, const float y1,
40 const float x2, const float y2, const float scale) {
41 return getSquaredDistanceFloat(x1, y1, x2, y2) / SQUARE_FLOAT(scale);
42}
43
44static inline float getDistanceFloat(const float x1, const float y1, const float x2,
45 const float y2) {
Ken Wakasa0fb95082012-08-13 17:31:46 +090046 return hypotf(x1 - x2, y1 - y2);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090047}
48
Ken Wakasa6e663492012-11-03 02:50:47 +090049static AK_FORCE_INLINE int getDistanceInt(const int x1, const int y1, const int x2, const int y2) {
Ken Wakasa0fb95082012-08-13 17:31:46 +090050 return static_cast<int>(getDistanceFloat(static_cast<float>(x1), static_cast<float>(y1),
51 static_cast<float>(x2), static_cast<float>(y2)));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090052}
53
Ken Wakasa6e663492012-11-03 02:50:47 +090054static AK_FORCE_INLINE float getAngle(const int x1, const int y1, const int x2, const int y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090055 const int dx = x1 - x2;
56 const int dy = y1 - y2;
Ken Wakasa07711c12012-12-27 16:35:51 +090057 if (dx == 0 && dy == 0) return 0.0f;
Ken Wakasa0fb95082012-08-13 17:31:46 +090058 return atan2f(static_cast<float>(dy), static_cast<float>(dx));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090059}
60
Ken Wakasa6e663492012-11-03 02:50:47 +090061static AK_FORCE_INLINE float getAngleDiff(const float a1, const float a2) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090062 const float deltaA = fabsf(a1 - a2);
63 const float diff = ROUND_FLOAT_10000(deltaA);
Ken Wakasabcec82d2012-08-12 11:10:48 +090064 if (diff > M_PI_F) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090065 const float normalizedDiff = 2.0f * M_PI_F - diff;
66 return ROUND_FLOAT_10000(normalizedDiff);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090067 }
68 return diff;
69}
70
Ken Wakasaa323fa62012-11-02 12:14:34 +090071static inline float pointToLineSegSquaredDistanceFloat(const float x, const float y, const float x1,
72 const float y1, const float x2, const float y2, const bool extend) {
Ken Wakasa507113a2012-08-13 11:36:41 +090073 const float ray1x = x - x1;
74 const float ray1y = y - y1;
75 const float ray2x = x2 - x1;
76 const float ray2y = y2 - y1;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090077
Ken Wakasa507113a2012-08-13 11:36:41 +090078 const float dotProduct = ray1x * ray2x + ray1y * ray2y;
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090079 const float lineLengthSqr = SQUARE_FLOAT(ray2x) + SQUARE_FLOAT(ray2y);
Ken Wakasa507113a2012-08-13 11:36:41 +090080 const float projectionLengthSqr = dotProduct / lineLengthSqr;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090081
Ken Wakasa507113a2012-08-13 11:36:41 +090082 float projectionX;
83 float projectionY;
Keisuke Kuroyanagi41f12ee2012-09-19 12:03:47 +090084 if (!extend && projectionLengthSqr < 0.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090085 projectionX = x1;
86 projectionY = y1;
Keisuke Kuroyanagi41f12ee2012-09-19 12:03:47 +090087 } else if (!extend && projectionLengthSqr > 1.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090088 projectionX = x2;
89 projectionY = y2;
90 } else {
Ken Wakasa507113a2012-08-13 11:36:41 +090091 projectionX = x1 + projectionLengthSqr * ray2x;
92 projectionY = y1 + projectionLengthSqr * ray2y;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090093 }
Ken Wakasa0fb95082012-08-13 17:31:46 +090094 return getSquaredDistanceFloat(x, y, projectionX, projectionY);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090095}
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +090096
97// Normal distribution N(u, sigma^2).
98struct NormalDistribution {
Ken Wakasa07711c12012-12-27 16:35:51 +090099 public:
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +0900100 NormalDistribution(const float u, const float sigma)
101 : mU(u), mSigma(sigma),
Keisuke Kuroyanagiff74cc32012-10-11 13:08:06 +0900102 mPreComputedNonExpPart(1.0f / sqrtf(2.0f * M_PI_F * SQUARE_FLOAT(sigma))),
103 mPreComputedExponentPart(-1.0f / (2.0f * SQUARE_FLOAT(sigma))) {}
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +0900104
Ken Wakasaa323fa62012-11-02 12:14:34 +0900105 float getProbabilityDensity(const float x) const {
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +0900106 const float shiftedX = x - mU;
107 return mPreComputedNonExpPart * expf(mPreComputedExponentPart * SQUARE_FLOAT(shiftedX));
108 }
Ken Wakasaa323fa62012-11-02 12:14:34 +0900109
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +0900110private:
111 DISALLOW_IMPLICIT_CONSTRUCTORS(NormalDistribution);
Ken Wakasa07711c12012-12-27 16:35:51 +0900112 const float mU; // mean value
113 const float mSigma; // standard deviation
114 const float mPreComputedNonExpPart; // = 1 / sqrt(2 * PI * sigma^2)
115 const float mPreComputedExponentPart; // = -1 / (2 * sigma^2)
Ken Wakasaa323fa62012-11-02 12:14:34 +0900116}; // struct NormalDistribution
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900117} // namespace latinime
Ken Wakasa507113a2012-08-13 11:36:41 +0900118#endif // LATINIME_GEOMETRY_UTILS_H