Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 1 | /* |
| 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 Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 17 | #ifndef LATINIME_GEOMETRY_UTILS_H |
| 18 | #define LATINIME_GEOMETRY_UTILS_H |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 19 | |
| 20 | #include <cmath> |
| 21 | |
Ken Wakasa | 6e66349 | 2012-11-03 02:50:47 +0900 | [diff] [blame] | 22 | #include "defines.h" |
| 23 | |
Satoshi Kataoka | f4554d8 | 2012-09-12 20:50:21 +0900 | [diff] [blame] | 24 | #define ROUND_FLOAT_10000(f) ((f) < 1000.0f && (f) > 0.001f) \ |
| 25 | ? (floorf((f) * 10000.0f) / 10000.0f) : (f) |
Satoshi Kataoka | f4554d8 | 2012-09-12 20:50:21 +0900 | [diff] [blame] | 26 | |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 27 | namespace latinime { |
| 28 | |
Ken Wakasa | a323fa6 | 2012-11-02 12:14:34 +0900 | [diff] [blame] | 29 | static inline float SQUARE_FLOAT(const float x) { return x * x; } |
| 30 | |
Ken Wakasa | 6e66349 | 2012-11-03 02:50:47 +0900 | [diff] [blame] | 31 | static AK_FORCE_INLINE float getAngle(const int x1, const int y1, const int x2, const int y2) { |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 32 | const int dx = x1 - x2; |
| 33 | const int dy = y1 - y2; |
Ken Wakasa | 07711c1 | 2012-12-27 16:35:51 +0900 | [diff] [blame] | 34 | if (dx == 0 && dy == 0) return 0.0f; |
Ken Wakasa | 0fb9508 | 2012-08-13 17:31:46 +0900 | [diff] [blame] | 35 | return atan2f(static_cast<float>(dy), static_cast<float>(dx)); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 36 | } |
| 37 | |
Ken Wakasa | 6e66349 | 2012-11-03 02:50:47 +0900 | [diff] [blame] | 38 | static AK_FORCE_INLINE float getAngleDiff(const float a1, const float a2) { |
Satoshi Kataoka | f4554d8 | 2012-09-12 20:50:21 +0900 | [diff] [blame] | 39 | const float deltaA = fabsf(a1 - a2); |
| 40 | const float diff = ROUND_FLOAT_10000(deltaA); |
Ken Wakasa | bcec82d | 2012-08-12 11:10:48 +0900 | [diff] [blame] | 41 | if (diff > M_PI_F) { |
Satoshi Kataoka | f4554d8 | 2012-09-12 20:50:21 +0900 | [diff] [blame] | 42 | const float normalizedDiff = 2.0f * M_PI_F - diff; |
| 43 | return ROUND_FLOAT_10000(normalizedDiff); |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 44 | } |
| 45 | return diff; |
| 46 | } |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 47 | } // namespace latinime |
Ken Wakasa | 507113a | 2012-08-13 11:36:41 +0900 | [diff] [blame] | 48 | #endif // LATINIME_GEOMETRY_UTILS_H |