aboutsummaryrefslogtreecommitdiffstats
path: root/native/jni/src/proximity_info_state.h
blob: 97281af1c3dfddf714cd0b409f5bc5a43fc9f103 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LATINIME_PROXIMITY_INFO_STATE_H
#define LATINIME_PROXIMITY_INFO_STATE_H

#include <bitset>
#include <cstring> // for memset()
#include <stdint.h>
#include <string>
#include <vector>

#include "char_utils.h"
#include "defines.h"
#include "geometry_utils.h"
#include "hash_map_compat.h"

namespace latinime {

class ProximityInfo;

class ProximityInfoState {
 public:
    typedef std::bitset<MAX_KEY_COUNT_IN_A_KEYBOARD> NearKeycodesSet;
    static const int NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
    static const int NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
    static const float NOT_A_DISTANCE_FLOAT;
    static const int NOT_A_CODE;

    /////////////////////////////////////////
    // Defined in proximity_info_state.cpp //
    /////////////////////////////////////////
    void initInputParams(const int pointerId, const float maxPointToKeyLength,
            const ProximityInfo *proximityInfo, const int *const inputCodes,
            const int inputSize, const int *xCoordinates, const int *yCoordinates,
            const int *const times, const int *const pointerIds, const bool isGeometric);

    /////////////////////////////////////////
    // Defined here                        //
    /////////////////////////////////////////
    AK_FORCE_INLINE ProximityInfoState()
            : mProximityInfo(0), mMaxPointToKeyLength(0.0f), mAverageSpeed(0.0f),
              mHasTouchPositionCorrectionData(false), mMostCommonKeyWidthSquare(0), mLocaleStr(),
              mKeyCount(0), mCellHeight(0), mCellWidth(0), mGridHeight(0), mGridWidth(0),
              mIsContinuationPossible(false), mSampledInputXs(), mSampledInputYs(), mTimes(),
              mInputIndice(), mLengthCache(), mDistanceCache(), mSpeedRates(),
              mDirections(), mBeelineSpeedRates(), mCharProbabilities(), mNearKeysVector(),
              mSearchKeysVector(), mTouchPositionCorrectionEnabled(false), mSampledInputSize(0) {
        memset(mInputCodes, 0, sizeof(mInputCodes));
        memset(mNormalizedSquaredDistances, 0, sizeof(mNormalizedSquaredDistances));
        memset(mPrimaryInputWord, 0, sizeof(mPrimaryInputWord));
    }

    // Non virtual inline destructor -- never inherit this class
    AK_FORCE_INLINE ~ProximityInfoState() {}

    inline int getPrimaryCodePointAt(const int index) const {
        return getProximityCodePointsAt(index)[0];
    }

    AK_FORCE_INLINE bool existsCodePointInProximityAt(const int index, const int c) const {
        const int *codePoints = getProximityCodePointsAt(index);
        int i = 0;
        while (codePoints[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE_INTERNAL) {
            if (codePoints[i++] == c) {
                return true;
            }
        }
        return false;
    }

    inline bool existsAdjacentProximityChars(const int index) const {
        if (index < 0 || index >= mSampledInputSize) return false;
        const int currentCodePoint = getPrimaryCodePointAt(index);
        const int leftIndex = index - 1;
        if (leftIndex >= 0 && existsCodePointInProximityAt(leftIndex, currentCodePoint)) {
            return true;
        }
        const int rightIndex = index + 1;
        if (rightIndex < mSampledInputSize
                && existsCodePointInProximityAt(rightIndex, currentCodePoint)) {
            return true;
        }
        return false;
    }

    inline int getNormalizedSquaredDistance(
            const int inputIndex, const int proximityIndex) const {
        return mNormalizedSquaredDistances[
                inputIndex * MAX_PROXIMITY_CHARS_SIZE_INTERNAL + proximityIndex];
    }

    inline const int *getPrimaryInputWord() const {
        return mPrimaryInputWord;
    }

    inline bool touchPositionCorrectionEnabled() const {
        return mTouchPositionCorrectionEnabled;
    }

    inline bool sameAsTyped(const int *word, int length) const {
        if (length != mSampledInputSize) {
            return false;
        }
        const int *inputCodes = mInputCodes;
        while (length--) {
            if (*inputCodes != *word) {
                return false;
            }
            inputCodes += MAX_PROXIMITY_CHARS_SIZE_INTERNAL;
            word++;
        }
        return true;
    }

    int getDuration(const int index) const;

    bool isUsed() const {
        return mSampledInputSize > 0;
    }

    uint32_t size() const {
        return mSampledInputSize;
    }

    int getInputX(const int index) const {
        return mSampledInputXs[index];
    }

    int getInputY(const int index) const {
        return mSampledInputYs[index];
    }

    int getLengthCache(const int index) const {
        return mLengthCache[index];
    }

    bool isContinuationPossible() const {
        return mIsContinuationPossible;
    }

    float getPointToKeyLength(const int inputIndex, const int charCode) const;
    float getPointToKeyByIdLength(const int inputIndex, const int keyId) const;

    ProximityType getMatchedProximityId(const int index, const int c,
            const bool checkProximityChars, int *proximityIndex = 0) const;

    int getSpaceY() const;

    int32_t getAllPossibleChars(
            const size_t startIndex, int32_t *const filter, const int32_t filterSize) const;

    float getSpeedRate(const int index) const {
        return mSpeedRates[index];
    }

    AK_FORCE_INLINE float getBeelineSpeedRate(const int id) const {
        return mBeelineSpeedRates[id];
    }

    float getDirection(const int index) const {
        return mDirections[index];
    }
    // get xy direction
    float getDirection(const int x, const int y) const;

    float getPointAngle(const int index) const;
    // Returns angle of three points. x, y, and z are indices.
    float getPointsAngle(const int index0, const int index1, const int index2) const;

    float getHighestProbabilitySequence(int *const codePointBuf) const;

    float getProbability(const int index, const int charCode) const;

    float getLineToKeyDistance(
            const int from, const int to, const int keyId, const bool extend) const;

    bool isKeyInSerchKeysAfterIndex(const int index, const int keyId) const;
 private:
    DISALLOW_COPY_AND_ASSIGN(ProximityInfoState);
    typedef hash_map_compat<int, float> NearKeysDistanceMap;
    /////////////////////////////////////////
    // Defined in proximity_info_state.cpp //
    /////////////////////////////////////////
    float calculateNormalizedSquaredDistance(const int keyIndex, const int inputIndex) const;

    float calculateSquaredDistanceFromSweetSpotCenter(
            const int keyIndex, const int inputIndex) const;

    bool pushTouchPoint(const int inputIndex, const int nodeCodePoint, int x, int y, const int time,
            const bool sample, const bool isLastPoint, const float sumAngle,
            NearKeysDistanceMap *const currentNearKeysDistances,
            const NearKeysDistanceMap *const prevNearKeysDistances,
            const NearKeysDistanceMap *const prevPrevNearKeysDistances);
    /////////////////////////////////////////
    // Defined here                        //
    /////////////////////////////////////////
    inline float square(const float x) const { return x * x; }

    bool hasInputCoordinates() const {
        return mSampledInputXs.size() > 0 && mSampledInputYs.size() > 0;
    }

    inline const int *getProximityCodePointsAt(const int index) const {
        return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE_INTERNAL);
    }

    float updateNearKeysDistances(const int x, const int y,
            NearKeysDistanceMap *const currentNearKeysDistances);
    bool isPrevLocalMin(const NearKeysDistanceMap *const currentNearKeysDistances,
            const NearKeysDistanceMap *const prevNearKeysDistances,
            const NearKeysDistanceMap *const prevPrevNearKeysDistances) const;
    float getPointScore(
            const int x, const int y, const int time, const bool last, const float nearest,
            const float sumAngle, const NearKeysDistanceMap *const currentNearKeysDistances,
            const NearKeysDistanceMap *const prevNearKeysDistances,
            const NearKeysDistanceMap *const prevPrevNearKeysDistances) const;
    bool checkAndReturnIsContinuationPossible(const int inputSize, const int *const xCoordinates,
            const int *const yCoordinates, const int *const times);
    void popInputData();
    void updateAlignPointProbabilities(const int start);
    bool suppressCharProbabilities(const int index1, const int index2);
    void refreshSpeedRates(const int inputSize, const int *const xCoordinates,
            const int *const yCoordinates, const int *const times, const int lastSavedInputSize);
    void refreshBeelineSpeedRates(const int inputSize,
            const int *const xCoordinates, const int *const yCoordinates, const int * times);
    float calculateBeelineSpeedRate(const int id, const int inputSize,
            const int *const xCoordinates, const int *const yCoordinates, const int * times) const;

    // const
    const ProximityInfo *mProximityInfo;
    float mMaxPointToKeyLength;
    float mAverageSpeed;
    bool mHasTouchPositionCorrectionData;
    int mMostCommonKeyWidthSquare;
    std::string mLocaleStr;
    int mKeyCount;
    int mCellHeight;
    int mCellWidth;
    int mGridHeight;
    int mGridWidth;
    bool mIsContinuationPossible;

    std::vector<int> mSampledInputXs;
    std::vector<int> mSampledInputYs;
    std::vector<int> mTimes;
    std::vector<int> mInputIndice;
    std::vector<int> mLengthCache;
    std::vector<float> mDistanceCache;
    std::vector<float> mSpeedRates;
    std::vector<float> mDirections;
    std::vector<float> mBeelineSpeedRates;
    // probabilities of skipping or mapping to a key for each point.
    std::vector<hash_map_compat<int, float> > mCharProbabilities;
    // The vector for the key code set which holds nearby keys for each sampled input point
    // 1. Used to calculate the probability of the key
    // 2. Used to calculate mSearchKeysVector
    std::vector<NearKeycodesSet> mNearKeysVector;
    // The vector for the key code set which holds nearby keys of some trailing sampled input points
    // for each sampled input point. These nearby keys contain the next characters which can be in
    // the dictionary. Specifically, currently we are looking for keys nearby trailing sampled
    // inputs including the current input point.
    std::vector<NearKeycodesSet> mSearchKeysVector;
    bool mTouchPositionCorrectionEnabled;
    int mInputCodes[MAX_PROXIMITY_CHARS_SIZE_INTERNAL * MAX_WORD_LENGTH_INTERNAL];
    int mNormalizedSquaredDistances[MAX_PROXIMITY_CHARS_SIZE_INTERNAL * MAX_WORD_LENGTH_INTERNAL];
    int mSampledInputSize;
    int mPrimaryInputWord[MAX_WORD_LENGTH_INTERNAL];
};
} // namespace latinime
#endif // LATINIME_PROXIMITY_INFO_STATE_H