blob: 64bbbd4b8c6efe6435f31b47faa85403656f2135 [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
Ken Wakasa6e663492012-11-03 02:50:47 +090022#include "defines.h"
23
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090024#define ROUND_FLOAT_10000(f) ((f) < 1000.0f && (f) > 0.001f) \
25 ? (floorf((f) * 10000.0f) / 10000.0f) : (f)
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090026
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090027namespace latinime {
28
Ken Wakasaa323fa62012-11-02 12:14:34 +090029static inline float SQUARE_FLOAT(const float x) { return x * x; }
30
Ken Wakasa6e663492012-11-03 02:50:47 +090031static AK_FORCE_INLINE float getAngle(const int x1, const int y1, const int x2, const int y2) {
Ken Wakasa507113a2012-08-13 11:36:41 +090032 const int dx = x1 - x2;
33 const int dy = y1 - y2;
Ken Wakasa07711c12012-12-27 16:35:51 +090034 if (dx == 0 && dy == 0) return 0.0f;
Ken Wakasa0fb95082012-08-13 17:31:46 +090035 return atan2f(static_cast<float>(dy), static_cast<float>(dx));
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090036}
37
Ken Wakasa6e663492012-11-03 02:50:47 +090038static AK_FORCE_INLINE float getAngleDiff(const float a1, const float a2) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090039 const float deltaA = fabsf(a1 - a2);
40 const float diff = ROUND_FLOAT_10000(deltaA);
Ken Wakasabcec82d2012-08-12 11:10:48 +090041 if (diff > M_PI_F) {
Satoshi Kataokaf4554d82012-09-12 20:50:21 +090042 const float normalizedDiff = 2.0f * M_PI_F - diff;
43 return ROUND_FLOAT_10000(normalizedDiff);
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090044 }
45 return diff;
46}
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090047} // namespace latinime
Ken Wakasa507113a2012-08-13 11:36:41 +090048#endif // LATINIME_GEOMETRY_UTILS_H