blob: bad5eda618bdccb94a30c11dc730277c64b29602 [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 Kataokaf4554d82012-09-12 20:50:21 +090028#define ROUND_FLOAT_10000(f) ((f) < 1000.0f && (f) > 0.001f) \
29 ? (floorf((f) * 10000.0f) / 10000.0f) : (f)
30
31#define SQUARE_FLOAT(x) ((x) * (x))
32
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090033namespace latinime {
34
Ken Wakasa0fb95082012-08-13 17:31:46 +090035static inline float getSquaredDistanceFloat(float x1, float y1, float x2, float y2) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090036 const float deltaX = x1 - x2;
37 const float deltaY = y1 - y2;
38 return SQUARE_FLOAT(deltaX) + SQUARE_FLOAT(deltaY);
Ken Wakasa0fb95082012-08-13 17:31:46 +090039}
40
41static inline float getDistanceFloat(float x1, float y1, float x2, float y2) {
42 return hypotf(x1 - x2, y1 - y2);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090043}
44
Ken Wakasabcec82d2012-08-12 11:10:48 +090045static inline int getDistanceInt(int x1, int y1, int x2, int y2) {
Ken Wakasa0fb95082012-08-13 17:31:46 +090046 return static_cast<int>(getDistanceFloat(static_cast<float>(x1), static_cast<float>(y1),
47 static_cast<float>(x2), static_cast<float>(y2)));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090048}
49
50static inline float getAngle(int x1, int y1, int x2, int y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090051 const int dx = x1 - x2;
52 const int dy = y1 - y2;
Ken Wakasa0fb95082012-08-13 17:31:46 +090053 if (dx == 0 && dy == 0) return 0;
54 return atan2f(static_cast<float>(dy), static_cast<float>(dx));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090055}
56
Ken Wakasa0fb95082012-08-13 17:31:46 +090057static inline float getAngleDiff(float a1, float a2) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090058 const float deltaA = fabsf(a1 - a2);
59 const float diff = ROUND_FLOAT_10000(deltaA);
Ken Wakasabcec82d2012-08-12 11:10:48 +090060 if (diff > M_PI_F) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090061 const float normalizedDiff = 2.0f * M_PI_F - diff;
62 return ROUND_FLOAT_10000(normalizedDiff);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090063 }
64 return diff;
65}
66
Ken Wakasa0fb95082012-08-13 17:31:46 +090067// static float pointToLineSegSquaredDistanceFloat(
Ken Wakasabcec82d2012-08-12 11:10:48 +090068// float x, float y, float x1, float y1, float x2, float y2) {
69// float A = x - x1;
70// float B = y - y1;
71// float C = x2 - x1;
72// float D = y2 - y1;
Ken Wakasa507113a2012-08-13 11:36:41 +090073// return fabsf(A * D - C * B) / sqrtf(C * C + D * D);
Ken Wakasabcec82d2012-08-12 11:10:48 +090074// }
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090075
Ken Wakasa0fb95082012-08-13 17:31:46 +090076static inline float pointToLineSegSquaredDistanceFloat(
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090077 float x, float y, float x1, float y1, float x2, float y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090078 const float ray1x = x - x1;
79 const float ray1y = y - y1;
80 const float ray2x = x2 - x1;
81 const float ray2y = y2 - y1;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090082
Ken Wakasa507113a2012-08-13 11:36:41 +090083 const float dotProduct = ray1x * ray2x + ray1y * ray2y;
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090084 const float lineLengthSqr = SQUARE_FLOAT(ray2x) + SQUARE_FLOAT(ray2y);
Ken Wakasa507113a2012-08-13 11:36:41 +090085 const float projectionLengthSqr = dotProduct / lineLengthSqr;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090086
Ken Wakasa507113a2012-08-13 11:36:41 +090087 float projectionX;
88 float projectionY;
89 if (projectionLengthSqr < 0.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090090 projectionX = x1;
91 projectionY = y1;
Ken Wakasa507113a2012-08-13 11:36:41 +090092 } else if (projectionLengthSqr > 1.0f) {
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090093 projectionX = x2;
94 projectionY = y2;
95 } else {
Ken Wakasa507113a2012-08-13 11:36:41 +090096 projectionX = x1 + projectionLengthSqr * ray2x;
97 projectionY = y1 + projectionLengthSqr * ray2y;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090098 }
Ken Wakasa0fb95082012-08-13 17:31:46 +090099 return getSquaredDistanceFloat(x, y, projectionX, projectionY);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900100}
101} // namespace latinime
Ken Wakasa507113a2012-08-13 11:36:41 +0900102#endif // LATINIME_GEOMETRY_UTILS_H