blob: 4bff80f156737fe1e0e8a196a98581e3f7e538e0 [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
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 Wakasa6e663492012-11-03 02:50:47 +090037static AK_FORCE_INLINE int getDistanceInt(const int x1, const int y1, const int x2, const int y2) {
Ken Wakasa6cee61d2013-01-15 16:15:48 +090038 return static_cast<int>(hypotf(static_cast<float>(x1 - x2), static_cast<float>(y1 - y2)));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090039}
40
Ken Wakasa6e663492012-11-03 02:50:47 +090041static AK_FORCE_INLINE float getAngle(const int x1, const int y1, const int x2, const int y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090042 const int dx = x1 - x2;
43 const int dy = y1 - y2;
Ken Wakasa07711c12012-12-27 16:35:51 +090044 if (dx == 0 && dy == 0) return 0.0f;
Ken Wakasa0fb95082012-08-13 17:31:46 +090045 return atan2f(static_cast<float>(dy), static_cast<float>(dx));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090046}
47
Ken Wakasa6e663492012-11-03 02:50:47 +090048static AK_FORCE_INLINE float getAngleDiff(const float a1, const float a2) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090049 const float deltaA = fabsf(a1 - a2);
50 const float diff = ROUND_FLOAT_10000(deltaA);
Ken Wakasabcec82d2012-08-12 11:10:48 +090051 if (diff > M_PI_F) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090052 const float normalizedDiff = 2.0f * M_PI_F - diff;
53 return ROUND_FLOAT_10000(normalizedDiff);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090054 }
55 return diff;
56}
57
Ken Wakasaa323fa62012-11-02 12:14:34 +090058static 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 Wakasa507113a2012-08-13 11:36:41 +090060 const float ray1x = x - x1;
61 const float ray1y = y - y1;
62 const float ray2x = x2 - x1;
63 const float ray2y = y2 - y1;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090064
Ken Wakasa507113a2012-08-13 11:36:41 +090065 const float dotProduct = ray1x * ray2x + ray1y * ray2y;
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090066 const float lineLengthSqr = SQUARE_FLOAT(ray2x) + SQUARE_FLOAT(ray2y);
Ken Wakasa507113a2012-08-13 11:36:41 +090067 const float projectionLengthSqr = dotProduct / lineLengthSqr;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090068
Ken Wakasa507113a2012-08-13 11:36:41 +090069 float projectionX;
70 float projectionY;
Keisuke Kuroyanagi41f12ee2012-09-19 12:03:47 +090071 if (!extend && projectionLengthSqr < 0.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090072 projectionX = x1;
73 projectionY = y1;
Keisuke Kuroyanagi41f12ee2012-09-19 12:03:47 +090074 } else if (!extend && projectionLengthSqr > 1.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090075 projectionX = x2;
76 projectionY = y2;
77 } else {
Ken Wakasa507113a2012-08-13 11:36:41 +090078 projectionX = x1 + projectionLengthSqr * ray2x;
79 projectionY = y1 + projectionLengthSqr * ray2y;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090080 }
Ken Wakasa0fb95082012-08-13 17:31:46 +090081 return getSquaredDistanceFloat(x, y, projectionX, projectionY);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090082}
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +090083
84// Normal distribution N(u, sigma^2).
85struct NormalDistribution {
Ken Wakasa07711c12012-12-27 16:35:51 +090086 public:
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +090087 NormalDistribution(const float u, const float sigma)
88 : mU(u), mSigma(sigma),
Keisuke Kuroyanagiff74cc32012-10-11 13:08:06 +090089 mPreComputedNonExpPart(1.0f / sqrtf(2.0f * M_PI_F * SQUARE_FLOAT(sigma))),
90 mPreComputedExponentPart(-1.0f / (2.0f * SQUARE_FLOAT(sigma))) {}
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +090091
Ken Wakasaa323fa62012-11-02 12:14:34 +090092 float getProbabilityDensity(const float x) const {
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +090093 const float shiftedX = x - mU;
94 return mPreComputedNonExpPart * expf(mPreComputedExponentPart * SQUARE_FLOAT(shiftedX));
95 }
Ken Wakasaa323fa62012-11-02 12:14:34 +090096
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +090097private:
98 DISALLOW_IMPLICIT_CONSTRUCTORS(NormalDistribution);
Ken Wakasa07711c12012-12-27 16:35:51 +090099 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 Wakasaa323fa62012-11-02 12:14:34 +0900103}; // struct NormalDistribution
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900104} // namespace latinime
Ken Wakasa507113a2012-08-13 11:36:41 +0900105#endif // LATINIME_GEOMETRY_UTILS_H