Skip to content

Commit

Permalink
[native][cpp to rust][#8] start porting proximity info state
Browse files Browse the repository at this point in the history
this one is significantly bigger and more complex than i anticipated.
  • Loading branch information
nyancrimew committed Aug 3, 2022
1 parent c354650 commit bf28196
Show file tree
Hide file tree
Showing 8 changed files with 672 additions and 30 deletions.
10 changes: 6 additions & 4 deletions native/jni/src/suggest/core/layout/proximity_info_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ void ProximityInfoState::initInputParams(const int pointerId, const float maxPoi
const int *const xCoordinates, const int *const yCoordinates, const int *const times,
const int *const pointerIds, const bool isGeometric, const std::vector<int> *locale) {
ASSERT(isGeometric || (inputSize < MAX_WORD_LENGTH));
mIsContinuousSuggestionPossible = (mHasBeenUpdatedByGeometricInput != isGeometric) ?
false : ProximityInfoStateUtils::checkAndReturnIsContinuousSuggestionPossible(
inputSize, xCoordinates, yCoordinates, times, mSampledInputSize,
&mSampledInputXs, &mSampledInputYs, &mSampledTimes, &mSampledInputIndice);
mIsContinuousSuggestionPossible = mHasBeenUpdatedByGeometricInput == isGeometric &&
ProximityInfoStateUtils::checkAndReturnIsContinuousSuggestionPossible(
inputSize, xCoordinates, yCoordinates, times,
mSampledInputSize,
&mSampledInputXs, &mSampledInputYs, &mSampledTimes,
&mSampledInputIndice);
if (DEBUG_DICT) {
AKLOGI("isContinuousSuggestionPossible = %s",
(mIsContinuousSuggestionPossible ? "true" : "false"));
Expand Down
1 change: 1 addition & 0 deletions native/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
crate_type = ["staticlib", "dylib"]

[dependencies]
indexmap = "1.9.1"
jni = "0.19.0"
lazy_static = "1.4.0"
levenshtein = "1.0.5"
Expand Down
6 changes: 6 additions & 0 deletions native/rust/src/defines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ pub const MAX_KEY_COUNT_IN_A_KEYBOARD: usize = 64;
pub const KEYCODE_SPACE: char = ' ';
pub const KEYCODE_SINGLE_QUOTE: char = '\'';
pub const KEYCODE_HYPHEN_MINUS: char = '-';

pub enum DoubleLetterLevel {
None,
DoubleLetter,
Strong,
}
35 changes: 35 additions & 0 deletions native/rust/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::f32::consts::PI;

#[inline]
pub fn square_float(x: f32) -> f32 {
x * x
Expand All @@ -12,3 +14,36 @@ pub fn get_distance_int(x1: i32, y1: i32, x2: i32, y2: i32) -> i32 {
pub fn get_distance(x1: f32, y1: f32, x2: f32, y2: f32) -> i32 {
(x1 - x2).hypot(y1 - y2) as i32
}

#[inline]
pub fn get_angle(x1: i32, y1: i32, x2: i32, y2: i32) -> f32 {
let dx = x1 - x2;
let dy = y1 - y2;
if dx == 0 && dy == 0 {
return 0.0;
}
(dy as f32).atan2(dx as f32)
}

#[inline]
pub fn get_angle_diff(a1: f32, a2: f32) -> f32 {
const TWO_PI: f32 = PI * 2.0;
let mut delta = (a1 - a2).abs();
if delta > TWO_PI {
// TODO: i am simply assuming the weird int coercion in the cpp impl is there for flooring
delta -= TWO_PI * (delta / TWO_PI).floor() as f32
}
if delta > PI {
delta = TWO_PI - delta;
}
round_float_10000(delta)
}

#[inline]
fn round_float_10000(f: f32) -> f32 {
if f < 1000.0 && f > 0.001 {
(f * 10000.0).floor() / 10000.0
} else {
f
}
}
1 change: 1 addition & 0 deletions native/rust/src/suggest/core/layout.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod proximity_info;
mod proximity_info_params;
mod proximity_info_state;
49 changes: 23 additions & 26 deletions native/rust/src/suggest/core/layout/proximity_info.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::cmp::{min, Ordering};
use std::collections::HashMap;

use jni::JNIEnv;
use jni::sys::{jfloat, jfloatArray, jint, jintArray};
use jni::JNIEnv;

use crate::defines::{KEYCODE_SPACE, MAX_KEY_COUNT_IN_A_KEYBOARD};
use crate::expect_droid::ResultExt;
Expand Down Expand Up @@ -235,43 +235,40 @@ impl ProximityInfo {

pub fn initialize_proximities(
&self,
input_codes: Vec<char>,
input_x_coordinates: Vec<i32>,
input_y_coordinates: Vec<i32>,
input_size: i32,
// alias: all_input_codes
// TODO: this feels like a very cpp way of doing things, we probably wanna do this in another way
mut input_proximities: Vec<char>,
locale: &str,
) {
input_codes: &Vec<char>,
input_x_coordinates: &Vec<i32>,
input_y_coordinates: &Vec<i32>,
input_size: &usize,
locale: &String,
) -> Vec<Vec<char>> {
// Initialize
// - mInputCodes
// - mNormalizedSquaredDistances
// TODO: Merge
for i in 0..input_size as usize {
let primary_key = input_codes[i];
let x = input_x_coordinates[i];
let y = input_y_coordinates[i];
let proximities = &mut input_proximities[..i * MAX_PROXIMITY_CHARS_SIZE];
self.calculate_proximities(x, y, primary_key, locale, proximities);
}
(0..*input_size)
.map(|i| {
let primary_key = input_codes[i];
let x = input_x_coordinates[i];
let y = input_y_coordinates[i];
self.calculate_proximities(x, y, primary_key, locale)
})
.collect()
}

fn calculate_proximities(
&self,
x: i32,
y: i32,
primary_key: char,
locale: &str,
proximities: &mut [char],
) {
locale: &String,
) -> Vec<char> {
let mut insert_pos = 0;
proximities[insert_pos] = primary_key;
let mut proximities = vec![primary_key];
insert_pos += 1;

if x < 0 || y < 0 {
// TODO: panic or handle properly
return;
return proximities;
}

let start_index = get_start_index_from_coordinates(
Expand All @@ -290,16 +287,16 @@ impl ProximityInfo {
let on_key = self.is_on_key(key_id, x, y);
let distance = self.squared_length_to_edge(key_id, x, y);
if on_key || distance < self.most_common_key_width_square {
proximities[insert_pos] = c;
insert_pos += 1;
if insert_pos >= MAX_PROXIMITY_CHARS_SIZE {
proximities.push(c);
if proximities.len() >= MAX_PROXIMITY_CHARS_SIZE {
// TODO: panic
return;
return proximities;
}
}
}
// TODO: additional proximities
// TODO: do we also need delimiters??
proximities
}

fn squared_length_to_edge(&self, key_id: usize, x: i32, y: i32) -> i32 {
Expand Down
17 changes: 17 additions & 0 deletions native/rust/src/suggest/core/layout/proximity_info_params.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
use std::f32::consts::PI;

pub const NOT_A_DISTANCE_FLOAT: f32 = -1.0;
pub const MIN_DOUBLE_LETTER_BEELINE_SPEED_PERCENTILE: i32 = 5;
pub const VERTICAL_SWEET_SPOT_SCALE: f32 = 1.0;
pub const VERTICAL_SWEET_SPOT_SCALE_G: f32 = 0.5;

pub const NEAR_KEY_THRESHOLD_FOR_DISTANCE: f32 = 2.0;

pub const MARGIN_FOR_PREV_LOCAL_MIN: f32 = 0.01;

pub const DISTANCE_BASE_SCALE: i32 = 100;
pub const NEAR_KEY_THRESHOLD_FOR_POINT_SCORE: f32 = 0.6;
pub const CORNER_CHECK_DISTANCE_THRESHOLD_SCALE: i32 = 25;
pub const NOT_LOCALMIN_DISTANCE_SCORE: f32 = -1.0;
pub const LOCALMIN_DISTANCE_AND_NEAR_TO_KEY_SCORE: f32 = 1.0;
pub const CORNER_ANGLE_THRESHOLD_FOR_POINT_SCORE: f32 = PI * 2.0 / 3.0;
pub const CORNER_SUM_ANGLE_THRESHOLD: f32 = PI / 4.0;
pub const CORNER_SCORE: f32 = 1.0;

pub const LAST_POINT_SKIP_DISTANCE_SCALE: i32 = 4;

// TODO: add the rest when we get to them
Loading

0 comments on commit bf28196

Please sign in to comment.