blob: 7d49cde7eae4ce98335a72dbb11229108ffbce14 [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"
Satoshi Kataokad7a8fbf2013-01-22 17:00:43 +090021#include "proximity_info_params.h"
Ken Wakasaf25e7052013-01-16 01:29:43 +090022#include "proximity_info_state.h"
Satoshi Kataokaa9763f92013-01-15 19:16:38 +090023
24namespace latinime {
25class SuggestUtils {
26 public:
Ken Wakasaf25e7052013-01-16 01:29:43 +090027 static float getDistanceScalingFactor(const float normalizedSquaredDistance) {
Satoshi Kataokaa9763f92013-01-15 19:16:38 +090028 if (normalizedSquaredDistance < 0.0f) {
29 return -1.0f;
30 }
31 // Promote or demote the score according to the distance from the sweet spot
32 static const float A = ZERO_DISTANCE_PROMOTION_RATE / 100.0f;
33 static const float B = 1.0f;
34 static const float C = 0.5f;
35 static const float MIN = 0.3f;
36 static const float R1 = NEUTRAL_SCORE_SQUARED_RADIUS;
37 static const float R2 = HALF_SCORE_SQUARED_RADIUS;
Ken Wakasaf25e7052013-01-16 01:29:43 +090038 const float x = normalizedSquaredDistance / static_cast<float>(
Satoshi Kataokad7a8fbf2013-01-22 17:00:43 +090039 ProximityInfoParams::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
Satoshi Kataokaa9763f92013-01-15 19:16:38 +090040 const float factor = max((x < R1)
41 ? (A * (R1 - x) + B * x) / R1
42 : (B * (R2 - x) + C * (x - R1)) / (R2 - R1), MIN);
43 // factor is a piecewise linear function like:
44 // A -_ .
45 // ^-_ .
46 // B \ .
47 // \_ .
48 // C ------------.
49 // .
50 // 0 R1 R2 .
51 return factor;
52 }
Ken Wakasaf25e7052013-01-16 01:29:43 +090053
54 private:
55 DISALLOW_IMPLICIT_CONSTRUCTORS(SuggestUtils);
Satoshi Kataokaa9763f92013-01-15 19:16:38 +090056};
57} // namespace latinime
58#endif // LATINIME_SUGGEST_UTILS_H