blob: 146eb80550bc824e523761a1cd00fcefb1c0303b [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
22#define MAX_DISTANCE 10000000
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090023#define MAX_PATHS 2
24
25#define DEBUG_DECODER false
26
Ken Wakasabcec82d2012-08-12 11:10:48 +090027#define M_PI_F 3.14159265f
28
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090029namespace latinime {
30
Ken Wakasa0fb95082012-08-13 17:31:46 +090031static inline float squareFloat(float x) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090032 return x * x;
33}
34
Ken Wakasa0fb95082012-08-13 17:31:46 +090035static inline float getSquaredDistanceFloat(float x1, float y1, float x2, float y2) {
36 return squareFloat(x1 - x2) + squareFloat(y1 - y2);
37}
38
39static inline float getDistanceFloat(float x1, float y1, float x2, float y2) {
40 return hypotf(x1 - x2, y1 - y2);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090041}
42
Ken Wakasabcec82d2012-08-12 11:10:48 +090043static inline int getDistanceInt(int x1, int y1, int x2, int y2) {
Ken Wakasa0fb95082012-08-13 17:31:46 +090044 return static_cast<int>(getDistanceFloat(static_cast<float>(x1), static_cast<float>(y1),
45 static_cast<float>(x2), static_cast<float>(y2)));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090046}
47
48static inline float getAngle(int x1, int y1, int x2, int y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090049 const int dx = x1 - x2;
50 const int dy = y1 - y2;
Ken Wakasa0fb95082012-08-13 17:31:46 +090051 if (dx == 0 && dy == 0) return 0;
52 return atan2f(static_cast<float>(dy), static_cast<float>(dx));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090053}
54
Ken Wakasa0fb95082012-08-13 17:31:46 +090055static inline float getAngleDiff(float a1, float a2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090056 const float diff = fabsf(a1 - a2);
Ken Wakasabcec82d2012-08-12 11:10:48 +090057 if (diff > M_PI_F) {
58 return 2.0f * M_PI_F - diff;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090059 }
60 return diff;
61}
62
Ken Wakasa0fb95082012-08-13 17:31:46 +090063// static float pointToLineSegSquaredDistanceFloat(
Ken Wakasabcec82d2012-08-12 11:10:48 +090064// float x, float y, float x1, float y1, float x2, float y2) {
65// float A = x - x1;
66// float B = y - y1;
67// float C = x2 - x1;
68// float D = y2 - y1;
Ken Wakasa507113a2012-08-13 11:36:41 +090069// return fabsf(A * D - C * B) / sqrtf(C * C + D * D);
Ken Wakasabcec82d2012-08-12 11:10:48 +090070// }
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090071
Ken Wakasa0fb95082012-08-13 17:31:46 +090072static inline float pointToLineSegSquaredDistanceFloat(
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090073 float x, float y, float x1, float y1, float x2, float y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090074 const float ray1x = x - x1;
75 const float ray1y = y - y1;
76 const float ray2x = x2 - x1;
77 const float ray2y = y2 - y1;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090078
Ken Wakasa507113a2012-08-13 11:36:41 +090079 const float dotProduct = ray1x * ray2x + ray1y * ray2y;
Ken Wakasa0fb95082012-08-13 17:31:46 +090080 const float lineLengthSqr = squareFloat(ray2x) + squareFloat(ray2y);
Ken Wakasa507113a2012-08-13 11:36:41 +090081 const float projectionLengthSqr = dotProduct / lineLengthSqr;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090082
Ken Wakasa507113a2012-08-13 11:36:41 +090083 float projectionX;
84 float projectionY;
85 if (projectionLengthSqr < 0.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090086 projectionX = x1;
87 projectionY = y1;
Ken Wakasa507113a2012-08-13 11:36:41 +090088 } else if (projectionLengthSqr > 1.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090089 projectionX = x2;
90 projectionY = y2;
91 } else {
Ken Wakasa507113a2012-08-13 11:36:41 +090092 projectionX = x1 + projectionLengthSqr * ray2x;
93 projectionY = y1 + projectionLengthSqr * ray2y;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090094 }
Ken Wakasa0fb95082012-08-13 17:31:46 +090095 return getSquaredDistanceFloat(x, y, projectionX, projectionY);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090096}
97} // namespace latinime
Ken Wakasa507113a2012-08-13 11:36:41 +090098#endif // LATINIME_GEOMETRY_UTILS_H