blob: f30e9fcc08259273695544ac25edaeb568503e25 [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 MAX_PATHS 2
23
24#define DEBUG_DECODER false
25
Ken Wakasabcec82d2012-08-12 11:10:48 +090026#define M_PI_F 3.14159265f
27
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090028namespace latinime {
29
Ken Wakasa0fb95082012-08-13 17:31:46 +090030static inline float squareFloat(float x) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090031 return x * x;
32}
33
Ken Wakasa0fb95082012-08-13 17:31:46 +090034static inline float getSquaredDistanceFloat(float x1, float y1, float x2, float y2) {
35 return squareFloat(x1 - x2) + squareFloat(y1 - y2);
36}
37
38static inline float getDistanceFloat(float x1, float y1, float x2, float y2) {
39 return hypotf(x1 - x2, y1 - y2);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090040}
41
Ken Wakasabcec82d2012-08-12 11:10:48 +090042static inline int getDistanceInt(int x1, int y1, int x2, int y2) {
Ken Wakasa0fb95082012-08-13 17:31:46 +090043 return static_cast<int>(getDistanceFloat(static_cast<float>(x1), static_cast<float>(y1),
44 static_cast<float>(x2), static_cast<float>(y2)));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090045}
46
47static inline float getAngle(int x1, int y1, int x2, int y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090048 const int dx = x1 - x2;
49 const int dy = y1 - y2;
Ken Wakasa0fb95082012-08-13 17:31:46 +090050 if (dx == 0 && dy == 0) return 0;
51 return atan2f(static_cast<float>(dy), static_cast<float>(dx));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090052}
53
Ken Wakasa0fb95082012-08-13 17:31:46 +090054static inline float getAngleDiff(float a1, float a2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090055 const float diff = fabsf(a1 - a2);
Ken Wakasabcec82d2012-08-12 11:10:48 +090056 if (diff > M_PI_F) {
57 return 2.0f * M_PI_F - diff;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090058 }
59 return diff;
60}
61
Ken Wakasa0fb95082012-08-13 17:31:46 +090062// static float pointToLineSegSquaredDistanceFloat(
Ken Wakasabcec82d2012-08-12 11:10:48 +090063// float x, float y, float x1, float y1, float x2, float y2) {
64// float A = x - x1;
65// float B = y - y1;
66// float C = x2 - x1;
67// float D = y2 - y1;
Ken Wakasa507113a2012-08-13 11:36:41 +090068// return fabsf(A * D - C * B) / sqrtf(C * C + D * D);
Ken Wakasabcec82d2012-08-12 11:10:48 +090069// }
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090070
Ken Wakasa0fb95082012-08-13 17:31:46 +090071static inline float pointToLineSegSquaredDistanceFloat(
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090072 float x, float y, float x1, float y1, float x2, float y2) {
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;
Ken Wakasa0fb95082012-08-13 17:31:46 +090079 const float lineLengthSqr = squareFloat(ray2x) + squareFloat(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;
84 if (projectionLengthSqr < 0.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090085 projectionX = x1;
86 projectionY = y1;
Ken Wakasa507113a2012-08-13 11:36:41 +090087 } else if (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}
96} // namespace latinime
Ken Wakasa507113a2012-08-13 11:36:41 +090097#endif // LATINIME_GEOMETRY_UTILS_H