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 class GeometryUtils {
30  public:
SQUARE_FLOAT(const float x)31     static inline float SQUARE_FLOAT(const float x) { return x * x; }
32 
getAngle(const int x1,const int y1,const int x2,const int y2)33     static AK_FORCE_INLINE float getAngle(const int x1, const int y1, const int x2, const int y2) {
34         const int dx = x1 - x2;
35         const int dy = y1 - y2;
36         if (dx == 0 && dy == 0) return 0.0f;
37         return atan2f(static_cast<float>(dy), static_cast<float>(dx));
38     }
39 
getAngleDiff(const float a1,const float a2)40     static AK_FORCE_INLINE float getAngleDiff(const float a1, const float a2) {
41         static const float M_2PI_F = M_PI * 2.0f;
42         float delta = fabsf(a1 - a2);
43         if (delta > M_2PI_F) {
44             delta -= (M_2PI_F * static_cast<int>(delta / M_2PI_F));
45         }
46         if (delta > M_PI_F) {
47             delta = M_2PI_F - delta;
48         }
49         return ROUND_FLOAT_10000(delta);
50     }
51 
getDistanceInt(const int x1,const int y1,const int x2,const int y2)52     static AK_FORCE_INLINE int getDistanceInt(const int x1, const int y1, const int x2,
53             const int y2) {
54         return static_cast<int>(hypotf(static_cast<float>(x1 - x2), static_cast<float>(y1 - y2)));
55     }
56 
57  private:
58     DISALLOW_IMPLICIT_CONSTRUCTORS(GeometryUtils);
59 };
60 } // namespace latinime
61 #endif // LATINIME_GEOMETRY_UTILS_H
62