Home | History | Annotate | Download | only in layout
      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:
     31     static inline float SQUARE_FLOAT(const float x) { return x * x; }
     32 
     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 
     40     static AK_FORCE_INLINE float getAngleDiff(const float a1, const float a2) {
     41         const float deltaA = fabsf(a1 - a2);
     42         const float diff = ROUND_FLOAT_10000(deltaA);
     43         if (diff > M_PI_F) {
     44             const float normalizedDiff = 2.0f * M_PI_F - diff;
     45             return ROUND_FLOAT_10000(normalizedDiff);
     46         }
     47         return diff;
     48     }
     49 
     50     static AK_FORCE_INLINE int getDistanceInt(const int x1, const int y1, const int x2,
     51             const int y2) {
     52         return static_cast<int>(hypotf(static_cast<float>(x1 - x2), static_cast<float>(y1 - y2)));
     53     }
     54 
     55  private:
     56     DISALLOW_IMPLICIT_CONSTRUCTORS(GeometryUtils);
     57 };
     58 } // namespace latinime
     59 #endif // LATINIME_GEOMETRY_UTILS_H
     60