blob: a3b3c12ddf8344df65225df5c266ab6bb73cfdf8 [file] [log] [blame]
Satoshi Kataokaa9763f92013-01-15 19:16:38 +09001/*
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_SUGGEST_UTILS_H
18#define LATINIME_SUGGEST_UTILS_H
19
20#include "defines.h"
21
22namespace latinime {
23class SuggestUtils {
24 public:
25 static float getDistanceScalingFactor(float normalizedSquaredDistance) {
26 if (normalizedSquaredDistance < 0.0f) {
27 return -1.0f;
28 }
29 // Promote or demote the score according to the distance from the sweet spot
30 static const float A = ZERO_DISTANCE_PROMOTION_RATE / 100.0f;
31 static const float B = 1.0f;
32 static const float C = 0.5f;
33 static const float MIN = 0.3f;
34 static const float R1 = NEUTRAL_SCORE_SQUARED_RADIUS;
35 static const float R2 = HALF_SCORE_SQUARED_RADIUS;
36 const float x = static_cast<float>(normalizedSquaredDistance)
37 / ProximityInfoState::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
38 const float factor = max((x < R1)
39 ? (A * (R1 - x) + B * x) / R1
40 : (B * (R2 - x) + C * (x - R1)) / (R2 - R1), MIN);
41 // factor is a piecewise linear function like:
42 // A -_ .
43 // ^-_ .
44 // B \ .
45 // \_ .
46 // C ------------.
47 // .
48 // 0 R1 R2 .
49 return factor;
50 }
51};
52} // namespace latinime
53#endif // LATINIME_SUGGEST_UTILS_H