Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2013 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_PROXIMITY_INFO_UTILS_H
     18 #define LATINIME_PROXIMITY_INFO_UTILS_H
     19 
     20 #include <cmath>
     21 
     22 #include "additional_proximity_chars.h"
     23 #include "char_utils.h"
     24 #include "defines.h"
     25 #include "geometry_utils.h"
     26 #include "hash_map_compat.h"
     27 
     28 namespace latinime {
     29 class ProximityInfoUtils {
     30  public:
     31     static AK_FORCE_INLINE int getKeyIndexOf(const int keyCount, const int c,
     32             const hash_map_compat<int, int> *const codeToKeyMap) {
     33         if (keyCount == 0) {
     34             // We do not have the coordinate data
     35             return NOT_AN_INDEX;
     36         }
     37         if (c == NOT_A_CODE_POINT) {
     38             return NOT_AN_INDEX;
     39         }
     40         const int lowerCode = toLowerCase(c);
     41         hash_map_compat<int, int>::const_iterator mapPos = codeToKeyMap->find(lowerCode);
     42         if (mapPos != codeToKeyMap->end()) {
     43             return mapPos->second;
     44         }
     45         return NOT_AN_INDEX;
     46     }
     47 
     48     static AK_FORCE_INLINE void initializeProximities(const int *const inputCodes,
     49             const int *const inputXCoordinates, const int *const inputYCoordinates,
     50             const int inputSize, const int *const keyXCoordinates,
     51             const int *const keyYCoordinates, const int *const keyWidths, const int *keyHeights,
     52             const int *const proximityCharsArray, const int cellHeight, const int cellWidth,
     53             const int gridWidth, const int mostCommonKeyWidth, const int keyCount,
     54             const char *const localeStr,
     55             const hash_map_compat<int, int> *const codeToKeyMap, int *inputProximities) {
     56         // Initialize
     57         // - mInputCodes
     58         // - mNormalizedSquaredDistances
     59         // TODO: Merge
     60         for (int i = 0; i < inputSize; ++i) {
     61             const int primaryKey = inputCodes[i];
     62             const int x = inputXCoordinates[i];
     63             const int y = inputYCoordinates[i];
     64             int *proximities = &inputProximities[i * MAX_PROXIMITY_CHARS_SIZE];
     65             calculateProximities(keyXCoordinates, keyYCoordinates, keyWidths, keyHeights,
     66                     proximityCharsArray, cellHeight, cellWidth, gridWidth, mostCommonKeyWidth,
     67                     keyCount, x, y, primaryKey, localeStr, codeToKeyMap, proximities);
     68         }
     69 
     70         if (DEBUG_PROXIMITY_CHARS) {
     71             for (int i = 0; i < inputSize; ++i) {
     72                 AKLOGI("---");
     73                 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE; ++j) {
     74                     int proximityChar =
     75                             inputProximities[i * MAX_PROXIMITY_CHARS_SIZE + j];
     76                     proximityChar += 0;
     77                     AKLOGI("--- (%d)%c", i, proximityChar);
     78                 }
     79             }
     80         }
     81     }
     82 
     83     static AK_FORCE_INLINE int getStartIndexFromCoordinates(const int x, const int y,
     84             const int cellHeight, const int cellWidth, const int gridWidth) {
     85         return ((y / cellHeight) * gridWidth + (x / cellWidth)) * MAX_PROXIMITY_CHARS_SIZE;
     86     }
     87 
     88     static inline float getSquaredDistanceFloat(const float x1, const float y1, const float x2,
     89             const float y2) {
     90         return SQUARE_FLOAT(x1 - x2) + SQUARE_FLOAT(y1 - y2);
     91     }
     92 
     93     static inline float pointToLineSegSquaredDistanceFloat(const float x, const float y,
     94         const float x1, const float y1, const float x2, const float y2, const bool extend) {
     95         const float ray1x = x - x1;
     96         const float ray1y = y - y1;
     97         const float ray2x = x2 - x1;
     98         const float ray2y = y2 - y1;
     99 
    100         const float dotProduct = ray1x * ray2x + ray1y * ray2y;
    101         const float lineLengthSqr = SQUARE_FLOAT(ray2x) + SQUARE_FLOAT(ray2y);
    102         const float projectionLengthSqr = dotProduct / lineLengthSqr;
    103 
    104         float projectionX;
    105         float projectionY;
    106         if (!extend && projectionLengthSqr < 0.0f) {
    107             projectionX = x1;
    108             projectionY = y1;
    109         } else if (!extend && projectionLengthSqr > 1.0f) {
    110             projectionX = x2;
    111             projectionY = y2;
    112         } else {
    113             projectionX = x1 + projectionLengthSqr * ray2x;
    114             projectionY = y1 + projectionLengthSqr * ray2y;
    115         }
    116         return getSquaredDistanceFloat(x, y, projectionX, projectionY);
    117     }
    118 
    119     // Normal distribution N(u, sigma^2).
    120     struct NormalDistribution {
    121      public:
    122         NormalDistribution(const float u, const float sigma)
    123                 : mU(u), mSigma(sigma),
    124                   mPreComputedNonExpPart(1.0f / sqrtf(2.0f * M_PI_F * SQUARE_FLOAT(sigma))),
    125                   mPreComputedExponentPart(-1.0f / (2.0f * SQUARE_FLOAT(sigma))) {}
    126 
    127         float getProbabilityDensity(const float x) const {
    128             const float shiftedX = x - mU;
    129             return mPreComputedNonExpPart * expf(mPreComputedExponentPart * SQUARE_FLOAT(shiftedX));
    130         }
    131 
    132      private:
    133         DISALLOW_IMPLICIT_CONSTRUCTORS(NormalDistribution);
    134         const float mU; // mean value
    135         const float mSigma; // standard deviation
    136         const float mPreComputedNonExpPart; // = 1 / sqrt(2 * PI * sigma^2)
    137         const float mPreComputedExponentPart; // = -1 / (2 * sigma^2)
    138     }; // struct NormalDistribution
    139 
    140  private:
    141     DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfoUtils);
    142 
    143     static bool isOnKey(const int *const keyXCoordinates, const int *const keyYCoordinates,
    144             const int *const keyWidths, const int *keyHeights, const int keyId, const int x,
    145             const int y) {
    146         if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
    147         const int left = keyXCoordinates[keyId];
    148         const int top = keyYCoordinates[keyId];
    149         const int right = left + keyWidths[keyId] + 1;
    150         const int bottom = top + keyHeights[keyId];
    151         return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom;
    152     }
    153 
    154     static AK_FORCE_INLINE void calculateProximities(const int *const keyXCoordinates,
    155             const int *const keyYCoordinates, const int *const keyWidths, const int *keyHeights,
    156             const int *const proximityCharsArray, const int cellHeight, const int cellWidth,
    157             const int gridWidth, const int mostCommonKeyWidth, const int keyCount,
    158             const int x, const int y, const int primaryKey, const char *const localeStr,
    159             const hash_map_compat<int, int> *const codeToKeyMap, int *proximities) {
    160         const int mostCommonKeyWidthSquare = mostCommonKeyWidth * mostCommonKeyWidth;
    161         int insertPos = 0;
    162         proximities[insertPos++] = primaryKey;
    163         const int startIndex = getStartIndexFromCoordinates(x, y, cellHeight, cellWidth, gridWidth);
    164         if (startIndex >= 0) {
    165             for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
    166                 const int c = proximityCharsArray[startIndex + i];
    167                 if (c < KEYCODE_SPACE || c == primaryKey) {
    168                     continue;
    169                 }
    170                 const int keyIndex = getKeyIndexOf(keyCount, c, codeToKeyMap);
    171                 const bool onKey = isOnKey(keyXCoordinates, keyYCoordinates, keyWidths, keyHeights,
    172                         keyIndex, x, y);
    173                 const int distance = squaredLengthToEdge(keyXCoordinates, keyYCoordinates,
    174                         keyWidths, keyHeights, keyIndex, x, y);
    175                 if (onKey || distance < mostCommonKeyWidthSquare) {
    176                     proximities[insertPos++] = c;
    177                     if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
    178                         if (DEBUG_DICT) {
    179                             ASSERT(false);
    180                         }
    181                         return;
    182                     }
    183                 }
    184             }
    185             const int additionalProximitySize =
    186                     AdditionalProximityChars::getAdditionalCharsSize(localeStr, primaryKey);
    187             if (additionalProximitySize > 0) {
    188                 proximities[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
    189                 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
    190                     if (DEBUG_DICT) {
    191                         ASSERT(false);
    192                     }
    193                     return;
    194                 }
    195 
    196                 const int *additionalProximityChars =
    197                         AdditionalProximityChars::getAdditionalChars(localeStr, primaryKey);
    198                 for (int j = 0; j < additionalProximitySize; ++j) {
    199                     const int ac = additionalProximityChars[j];
    200                     int k = 0;
    201                     for (; k < insertPos; ++k) {
    202                         if (ac == proximities[k]) {
    203                             break;
    204                         }
    205                     }
    206                     if (k < insertPos) {
    207                         continue;
    208                     }
    209                     proximities[insertPos++] = ac;
    210                     if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
    211                         if (DEBUG_DICT) {
    212                             ASSERT(false);
    213                         }
    214                         return;
    215                     }
    216                 }
    217             }
    218         }
    219         // Add a delimiter for the proximity characters
    220         for (int i = insertPos; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
    221             proximities[i] = NOT_A_CODE_POINT;
    222         }
    223     }
    224 
    225     static int squaredLengthToEdge(const int *const keyXCoordinates,
    226             const int *const keyYCoordinates, const int *const keyWidths, const int *keyHeights,
    227             const int keyId, const int x, const int y) {
    228         // NOT_A_ID is -1, but return whenever < 0 just in case
    229         if (keyId < 0) return MAX_VALUE_FOR_WEIGHTING;
    230         const int left = keyXCoordinates[keyId];
    231         const int top = keyYCoordinates[keyId];
    232         const int right = left + keyWidths[keyId];
    233         const int bottom = top + keyHeights[keyId];
    234         const int edgeX = x < left ? left : (x > right ? right : x);
    235         const int edgeY = y < top ? top : (y > bottom ? bottom : y);
    236         const int dx = x - edgeX;
    237         const int dy = y - edgeY;
    238         return dx * dx + dy * dy;
    239     }
    240 };
    241 } // namespace latinime
    242 #endif // LATINIME_PROXIMITY_INFO_UTILS_H
    243