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 17 #ifndef LATINIME_GEOMETRY_UTILS_H 18 #define LATINIME_GEOMETRY_UTILS_H 19 20 #include <cmath> 21 22 #include "defines.h" 23 24 #define ROUND_FLOAT_10000(f) ((f) < 1000.0f && (f) > 0.001f) \ 25 ? (floorf((f) * 10000.0f) / 10000.0f) : (f) 26 27 namespace latinime { 28 29 static inline float SQUARE_FLOAT(const float x) { return x * x; } 30 31 static AK_FORCE_INLINE float getAngle(const int x1, const int y1, const int x2, const int y2) { 32 const int dx = x1 - x2; 33 const int dy = y1 - y2; 34 if (dx == 0 && dy == 0) return 0.0f; 35 return atan2f(static_cast<float>(dy), static_cast<float>(dx)); 36 } 37 38 static AK_FORCE_INLINE float getAngleDiff(const float a1, const float a2) { 39 const float deltaA = fabsf(a1 - a2); 40 const float diff = ROUND_FLOAT_10000(deltaA); 41 if (diff > M_PI_F) { 42 const float normalizedDiff = 2.0f * M_PI_F - diff; 43 return ROUND_FLOAT_10000(normalizedDiff); 44 } 45 return diff; 46 } 47 48 static AK_FORCE_INLINE int getDistanceInt(const int x1, const int y1, const int x2, 49 const int y2) { 50 return static_cast<int>(hypotf(static_cast<float>(x1 - x2), static_cast<float>(y1 - y2))); 51 } 52 } // namespace latinime 53 #endif // LATINIME_GEOMETRY_UTILS_H 54