blob: 734fbefda84cbeaabc5e5ce6499329f7b212d1f9 [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#define SQUARE_FLOAT(x) ((x) * (x))
28
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090029namespace latinime {
30
Ken Wakasa0fb95082012-08-13 17:31:46 +090031static inline float getSquaredDistanceFloat(float x1, float y1, float x2, float y2) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090032 const float deltaX = x1 - x2;
33 const float deltaY = y1 - y2;
34 return SQUARE_FLOAT(deltaX) + SQUARE_FLOAT(deltaY);
Ken Wakasa0fb95082012-08-13 17:31:46 +090035}
36
37static inline float getDistanceFloat(float x1, float y1, float x2, float y2) {
38 return hypotf(x1 - x2, y1 - y2);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090039}
40
Ken Wakasabcec82d2012-08-12 11:10:48 +090041static inline int getDistanceInt(int x1, int y1, int x2, int y2) {
Ken Wakasa0fb95082012-08-13 17:31:46 +090042 return static_cast<int>(getDistanceFloat(static_cast<float>(x1), static_cast<float>(y1),
43 static_cast<float>(x2), static_cast<float>(y2)));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090044}
45
46static inline float getAngle(int x1, int y1, int x2, int y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090047 const int dx = x1 - x2;
48 const int dy = y1 - y2;
Ken Wakasa0fb95082012-08-13 17:31:46 +090049 if (dx == 0 && dy == 0) return 0;
50 return atan2f(static_cast<float>(dy), static_cast<float>(dx));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090051}
52
Ken Wakasa0fb95082012-08-13 17:31:46 +090053static inline float getAngleDiff(float a1, float a2) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090054 const float deltaA = fabsf(a1 - a2);
55 const float diff = ROUND_FLOAT_10000(deltaA);
Ken Wakasabcec82d2012-08-12 11:10:48 +090056 if (diff > M_PI_F) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090057 const float normalizedDiff = 2.0f * M_PI_F - diff;
58 return ROUND_FLOAT_10000(normalizedDiff);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090059 }
60 return diff;
61}
62
Ken Wakasa0fb95082012-08-13 17:31:46 +090063static inline float pointToLineSegSquaredDistanceFloat(
Keisuke Kuroyanagi41f12ee2012-09-19 12:03:47 +090064 float x, float y, float x1, float y1, float x2, float y2, bool extend) {
Ken Wakasa507113a2012-08-13 11:36:41 +090065 const float ray1x = x - x1;
66 const float ray1y = y - y1;
67 const float ray2x = x2 - x1;
68 const float ray2y = y2 - y1;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090069
Ken Wakasa507113a2012-08-13 11:36:41 +090070 const float dotProduct = ray1x * ray2x + ray1y * ray2y;
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090071 const float lineLengthSqr = SQUARE_FLOAT(ray2x) + SQUARE_FLOAT(ray2y);
Ken Wakasa507113a2012-08-13 11:36:41 +090072 const float projectionLengthSqr = dotProduct / lineLengthSqr;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090073
Ken Wakasa507113a2012-08-13 11:36:41 +090074 float projectionX;
75 float projectionY;
Keisuke Kuroyanagi41f12ee2012-09-19 12:03:47 +090076 if (!extend && projectionLengthSqr < 0.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090077 projectionX = x1;
78 projectionY = y1;
Keisuke Kuroyanagi41f12ee2012-09-19 12:03:47 +090079 } else if (!extend && projectionLengthSqr > 1.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090080 projectionX = x2;
81 projectionY = y2;
82 } else {
Ken Wakasa507113a2012-08-13 11:36:41 +090083 projectionX = x1 + projectionLengthSqr * ray2x;
84 projectionY = y1 + projectionLengthSqr * ray2y;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090085 }
Ken Wakasa0fb95082012-08-13 17:31:46 +090086 return getSquaredDistanceFloat(x, y, projectionX, projectionY);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090087}
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +090088
89// Normal distribution N(u, sigma^2).
90struct NormalDistribution {
91 NormalDistribution(const float u, const float sigma)
92 : mU(u), mSigma(sigma),
93 mPreComputedNonExpPart(1.0f / sqrtf(2.0f * M_PI_F * sigma * sigma)),
94 mPreComputedExponentPart(-1.0f / (2.0f * sigma * sigma)) {}
95
96 float getProbabilityDensity(const float x) {
97 const float shiftedX = x - mU;
98 return mPreComputedNonExpPart * expf(mPreComputedExponentPart * SQUARE_FLOAT(shiftedX));
99 }
100private:
101 DISALLOW_IMPLICIT_CONSTRUCTORS(NormalDistribution);
102 float mU; // mean value
103 float mSigma; // standard deviation
104 float mPreComputedNonExpPart; // = 1 / sqrt(2 * PI * sigma^2)
105 float mPreComputedExponentPart; // = -1 / (2 * sigma^2)
106};
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900107} // namespace latinime
Ken Wakasa507113a2012-08-13 11:36:41 +0900108#endif // LATINIME_GEOMETRY_UTILS_H