diff options
author | Siarhei Vishniakou <svv@google.com> | 2022-09-07 15:11:57 +0000 |
---|---|---|
committer | Siarhei Vishniakou <svv@google.com> | 2022-09-09 20:42:16 +0000 |
commit | cf709f207d4af4c471ea39a0dff18733d35aa86f (patch) | |
tree | 40aa418887f3eb8c3ad0b5f149e5ef1b46f5fb8e | |
parent | bc17b161dfa8d983a816188cc9f9fa827de55abf (diff) | |
download | libpalmrejection-cf709f207d4af4c471ea39a0dff18733d35aa86f.tar.gz |
Separate stroke-length code into static functions
These functions will allow us to handle resampling in the future,
because the logic will be moved out of the main Filter function and into
the small pieces of code that are easy to modify.
Bug: b/240168494
Test: autoninja -C out/Default events_unittests
Change-Id: I639634df001ad30977a2d07e48348af99e11e655
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3857624
Auto-Submit: Siarhei Vishniakou <svv@google.com>
Commit-Queue: Siarhei Vishniakou <svv@google.com>
Reviewed-by: Rob Schonberger <robsc@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1043971}
-rw-r--r-- | ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.cc | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.cc b/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.cc index 6b91d1b..0b301e4 100644 --- a/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.cc +++ b/ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.cc @@ -34,6 +34,35 @@ float EuclideanDistance(const gfx::PointF& a, const gfx::PointF& b) { return (a - b).Length(); } +bool IsEarlyStageSample( + const PalmFilterStroke& stroke, + const NeuralStylusPalmDetectionFilterModelConfig& config) { + return config.early_stage_sample_counts.find(stroke.samples_seen()) != + config.early_stage_sample_counts.end(); +} + +bool HasDecidedStroke( + const PalmFilterStroke& stroke, + const NeuralStylusPalmDetectionFilterModelConfig& config) { + return stroke.samples_seen() >= config.max_sample_count; +} + +bool IsVeryShortStroke( + const PalmFilterStroke& stroke, + const NeuralStylusPalmDetectionFilterModelConfig& config) { + return stroke.samples_seen() < config.min_sample_count; +} + +/** + * The provided stroke must be a neighbor stroke rather than a stroke currently + * being evaluated. The parameter 'neighbor_min_sample_count' might be different + * from the config, depending on the specific usage in the caller. + */ +bool HasInsufficientDataAsNeighbor(const PalmFilterStroke& neighbor_stroke, + size_t neighbor_min_sample_count) { + return neighbor_stroke.samples().size() < neighbor_min_sample_count; +} + } // namespace NeuralStylusPalmDetectionFilter::NeuralStylusPalmDetectionFilter( @@ -76,7 +105,7 @@ void NeuralStylusPalmDetectionFilter::FindBiggestNeighborsWithin( if (neighbor.tracking_id() == stroke.tracking_id()) { continue; } - if (neighbor.samples().size() < neighbor_min_sample_count) { + if (HasInsufficientDataAsNeighbor(neighbor, neighbor_min_sample_count)) { continue; } float distance = @@ -116,7 +145,7 @@ void NeuralStylusPalmDetectionFilter::FindNearestNeighborsWithin( if (neighbor.tracking_id() == stroke.tracking_id()) { continue; } - if (neighbor.samples().size() < neighbor_min_sample_count) { + if (HasInsufficientDataAsNeighbor(neighbor, neighbor_min_sample_count)) { continue; } float distance = @@ -195,7 +224,7 @@ void NeuralStylusPalmDetectionFilter::Filter( PalmFilterStroke& stroke = stroke_it->second; if (end_of_stroke) { // This is a stroke that hasn't had a decision yet, so we force decide. - if (stroke.samples_seen() < config.max_sample_count) { + if (!HasDecidedStroke(stroke, config)) { slots_to_decide.insert(slot); } @@ -215,8 +244,7 @@ void NeuralStylusPalmDetectionFilter::Filter( // Heuristic delay detection. if (config.heuristic_delay_start_if_palm && !end_of_stroke && - stroke.samples_seen() < config.max_sample_count && - IsHeuristicPalmStroke(stroke)) { + !HasDecidedStroke(stroke, config) && IsHeuristicPalmStroke(stroke)) { // A stroke that we _think_ may be a palm, but is too short to decide // yet. So we mark for delay for now. is_delay_.set(slot, true); @@ -224,8 +252,7 @@ void NeuralStylusPalmDetectionFilter::Filter( // Early stage delay detection that marks suspicious palms for delay. if (!is_delay_.test(slot) && config.nn_delay_start_if_palm && - config.early_stage_sample_counts.find(stroke.samples_seen()) != - config.early_stage_sample_counts.end()) { + IsEarlyStageSample(stroke, config)) { VLOG(1) << "About to run a early_stage prediction."; if (DetectSpuriousStroke(ExtractFeatures(tracking_id), model_->config().output_threshold)) { @@ -245,7 +272,7 @@ void NeuralStylusPalmDetectionFilter::Filter( continue; } const auto& stroke = lookup->second; - if (stroke.samples_seen() < model_->config().min_sample_count) { + if (IsVeryShortStroke(stroke, model_->config())) { // in very short strokes: we use a heuristic. is_palm_.set(slot, IsHeuristicPalmStroke(stroke)); continue; @@ -278,11 +305,12 @@ bool NeuralStylusPalmDetectionFilter::ShouldDecideStroke( bool NeuralStylusPalmDetectionFilter::IsHeuristicPalmStroke( const PalmFilterStroke& stroke) const { - if (stroke.samples().size() >= model_->config().max_sample_count) { + const auto& config = model_->config(); + if (stroke.samples().size() >= config.max_sample_count) { LOG(DFATAL) << "Should not call this method on long strokes."; return false; } - const auto& config = model_->config(); + if (config.heuristic_palm_touch_limit > 0.0) { if (stroke.MaxMajorRadius() >= config.heuristic_palm_touch_limit) { VLOG(1) << "IsHeuristicPalm: Yes major radius."; @@ -297,7 +325,7 @@ bool NeuralStylusPalmDetectionFilter::IsHeuristicPalmStroke( std::vector<std::pair<float, int>> biggest_strokes; FindBiggestNeighborsWithin( 1 /* neighbors */, 1 /* neighbor min sample count */, - model_->config().max_neighbor_distance_in_mm, stroke, &biggest_strokes); + config.max_neighbor_distance_in_mm, stroke, &biggest_strokes); if (!biggest_strokes.empty() && strokes_.find(biggest_strokes[0].second)->second.BiggestSize() >= config.heuristic_palm_area_limit) { |