blob: 927b44f965b738a5f07c76d13a4fe45757fb4f86 [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
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090022#define DEBUG_DECODER false
23
Ken Wakasabcec82d2012-08-12 11:10:48 +090024#define M_PI_F 3.14159265f
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090025#define ROUND_FLOAT_10000(f) ((f) < 1000.0f && (f) > 0.001f) \
26 ? (floorf((f) * 10000.0f) / 10000.0f) : (f)
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090027
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090028namespace latinime {
29
Ken Wakasaa323fa62012-11-02 12:14:34 +090030static inline float SQUARE_FLOAT(const float x) { return x * x; }
31
32static 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 Wakasa0fb95082012-08-13 17:31:46 +090035}
36
Ken Wakasaa323fa62012-11-02 12:14:34 +090037static inline float getNormalizedSquaredDistanceFloat(const float x1, const float y1,
38 const float x2, const float y2, const float scale) {
39 return getSquaredDistanceFloat(x1, y1, x2, y2) / SQUARE_FLOAT(scale);
40}
41
42static inline float getDistanceFloat(const float x1, const float y1, const float x2,
43 const float y2) {
Ken Wakasa0fb95082012-08-13 17:31:46 +090044 return hypotf(x1 - x2, y1 - y2);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090045}
46
Ken Wakasaa323fa62012-11-02 12:14:34 +090047static inline int getDistanceInt(const int x1, const int y1, const int x2, const int y2) {
Ken Wakasa0fb95082012-08-13 17:31:46 +090048 return static_cast<int>(getDistanceFloat(static_cast<float>(x1), static_cast<float>(y1),
49 static_cast<float>(x2), static_cast<float>(y2)));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090050}
51
Ken Wakasaa323fa62012-11-02 12:14:34 +090052static inline float getAngle(const int x1, const int y1, const int x2, const int y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090053 const int dx = x1 - x2;
54 const int dy = y1 - y2;
Ken Wakasa0fb95082012-08-13 17:31:46 +090055 if (dx == 0 && dy == 0) return 0;
56 return atan2f(static_cast<float>(dy), static_cast<float>(dx));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090057}
58
Ken Wakasaa323fa62012-11-02 12:14:34 +090059static inline float getAngleDiff(const float a1, const float a2) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090060 const float deltaA = fabsf(a1 - a2);
61 const float diff = ROUND_FLOAT_10000(deltaA);
Ken Wakasabcec82d2012-08-12 11:10:48 +090062 if (diff > M_PI_F) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090063 const float normalizedDiff = 2.0f * M_PI_F - diff;
64 return ROUND_FLOAT_10000(normalizedDiff);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090065 }
66 return diff;
67}
68
Ken Wakasaa323fa62012-11-02 12:14:34 +090069static inline float pointToLineSegSquaredDistanceFloat(const float x, const float y, const float x1,
70 const float y1, const float x2, const float y2, const bool extend) {
Ken Wakasa507113a2012-08-13 11:36:41 +090071 const float ray1x = x - x1;
72 const float ray1y = y - y1;
73 const float ray2x = x2 - x1;
74 const float ray2y = y2 - y1;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090075
Ken Wakasa507113a2012-08-13 11:36:41 +090076 const float dotProduct = ray1x * ray2x + ray1y * ray2y;
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090077 const float lineLengthSqr = SQUARE_FLOAT(ray2x) + SQUARE_FLOAT(ray2y);
Ken Wakasa507113a2012-08-13 11:36:41 +090078 const float projectionLengthSqr = dotProduct / lineLengthSqr;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090079
Ken Wakasa507113a2012-08-13 11:36:41 +090080 float projectionX;
81 float projectionY;
Keisuke Kuroyanagi41f12ee2012-09-19 12:03:47 +090082 if (!extend && projectionLengthSqr < 0.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090083 projectionX = x1;
84 projectionY = y1;
Keisuke Kuroyanagi41f12ee2012-09-19 12:03:47 +090085 } else if (!extend && projectionLengthSqr > 1.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090086 projectionX = x2;
87 projectionY = y2;
88 } else {
Ken Wakasa507113a2012-08-13 11:36:41 +090089 projectionX = x1 + projectionLengthSqr * ray2x;
90 projectionY = y1 + projectionLengthSqr * ray2y;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090091 }
Ken Wakasa0fb95082012-08-13 17:31:46 +090092 return getSquaredDistanceFloat(x, y, projectionX, projectionY);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090093}
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +090094
95// Normal distribution N(u, sigma^2).
96struct NormalDistribution {
97 NormalDistribution(const float u, const float sigma)
98 : mU(u), mSigma(sigma),
Keisuke Kuroyanagiff74cc32012-10-11 13:08:06 +090099 mPreComputedNonExpPart(1.0f / sqrtf(2.0f * M_PI_F * SQUARE_FLOAT(sigma))),
100 mPreComputedExponentPart(-1.0f / (2.0f * SQUARE_FLOAT(sigma))) {}
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +0900101
Ken Wakasaa323fa62012-11-02 12:14:34 +0900102 float getProbabilityDensity(const float x) const {
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +0900103 const float shiftedX = x - mU;
104 return mPreComputedNonExpPart * expf(mPreComputedExponentPart * SQUARE_FLOAT(shiftedX));
105 }
Ken Wakasaa323fa62012-11-02 12:14:34 +0900106
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +0900107private:
108 DISALLOW_IMPLICIT_CONSTRUCTORS(NormalDistribution);
109 float mU; // mean value
110 float mSigma; // standard deviation
111 float mPreComputedNonExpPart; // = 1 / sqrt(2 * PI * sigma^2)
112 float mPreComputedExponentPart; // = -1 / (2 * sigma^2)
Ken Wakasaa323fa62012-11-02 12:14:34 +0900113}; // struct NormalDistribution
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900114} // namespace latinime
Ken Wakasa507113a2012-08-13 11:36:41 +0900115#endif // LATINIME_GEOMETRY_UTILS_H