blob: 168542e458176ef78f5352a3acc702b3f9f22df7 [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 Wakasabcec82d2012-08-12 11:10:48 +090031static inline float sqrf(float x) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090032 return x * x;
33}
34
Ken Wakasabcec82d2012-08-12 11:10:48 +090035static inline float getNormalizedSqrDistanceFloat(int x1, int y1, int x2, int y2, int scale) {
36 return sqrf(static_cast<float>(x1 - x2) / static_cast<float>(scale))
37 + sqrf(static_cast<float>(y1 - y2) / static_cast<float>(scale));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090038}
39
Ken Wakasabcec82d2012-08-12 11:10:48 +090040static inline float getDistanceSqrFloat(float x1, float y1, float x2, float y2) {
41 return sqrf(x2 - x1) + sqrf(y2 - y1);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090042}
43
Ken Wakasabcec82d2012-08-12 11:10:48 +090044static inline int getDistanceInt(int x1, int y1, int x2, int y2) {
45 return static_cast<int>(
46 sqrtf(getDistanceSqrFloat(
47 static_cast<float>(x1), static_cast<float>(y1),
48 static_cast<float>(x2), static_cast<float>(y2))));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090049}
50
51static inline float getAngle(int x1, int y1, int x2, int y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090052 const int dx = x1 - x2;
53 const int dy = y1 - y2;
Ken Wakasabcec82d2012-08-12 11:10:48 +090054 if (dx == 0 && dy == 0) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090055 return 0;
Ken Wakasabcec82d2012-08-12 11:10:48 +090056 }
Ken Wakasa507113a2012-08-13 11:36:41 +090057 const float dxf = static_cast<float>(dx);
58 const float dyf = static_cast<float>(dy);
Ken Wakasabcec82d2012-08-12 11:10:48 +090059 return atan2f(dyf, dxf);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090060}
61
62static inline float angleDiff(float a1, float a2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090063 const float diff = fabsf(a1 - a2);
Ken Wakasabcec82d2012-08-12 11:10:48 +090064 if (diff > M_PI_F) {
65 return 2.0f * M_PI_F - diff;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090066 }
67 return diff;
68}
69
Ken Wakasabcec82d2012-08-12 11:10:48 +090070// static float pointToLineDistanceSqrFloat(
71// float x, float y, float x1, float y1, float x2, float y2) {
72// float A = x - x1;
73// float B = y - y1;
74// float C = x2 - x1;
75// float D = y2 - y1;
Ken Wakasa507113a2012-08-13 11:36:41 +090076// return fabsf(A * D - C * B) / sqrtf(C * C + D * D);
Ken Wakasabcec82d2012-08-12 11:10:48 +090077// }
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090078
Ken Wakasabcec82d2012-08-12 11:10:48 +090079static inline float pointToLineSegDistanceSqrFloat(
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090080 float x, float y, float x1, float y1, float x2, float y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090081 const float ray1x = x - x1;
82 const float ray1y = y - y1;
83 const float ray2x = x2 - x1;
84 const float ray2y = y2 - y1;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090085
Ken Wakasa507113a2012-08-13 11:36:41 +090086 const float dotProduct = ray1x * ray2x + ray1y * ray2y;
87 const float lineLengthSqr = sqrf(ray2x) + sqrf(ray2y);
88 const float projectionLengthSqr = dotProduct / lineLengthSqr;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090089
Ken Wakasa507113a2012-08-13 11:36:41 +090090 float projectionX;
91 float projectionY;
92 if (projectionLengthSqr < 0.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090093 projectionX = x1;
94 projectionY = y1;
Ken Wakasa507113a2012-08-13 11:36:41 +090095 } else if (projectionLengthSqr > 1.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090096 projectionX = x2;
97 projectionY = y2;
98 } else {
Ken Wakasa507113a2012-08-13 11:36:41 +090099 projectionX = x1 + projectionLengthSqr * ray2x;
100 projectionY = y1 + projectionLengthSqr * ray2y;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900101 }
Ken Wakasabcec82d2012-08-12 11:10:48 +0900102 return getDistanceSqrFloat(x, y, projectionX, projectionY);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900103}
104} // namespace latinime
Ken Wakasa507113a2012-08-13 11:36:41 +0900105#endif // LATINIME_GEOMETRY_UTILS_H