Skip to content

StayFilter

roosit edited this page Nov 15, 2026 · 1 revision

StayFilter

Up | Previous | Next | Русский

Full source with short explanations

Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not stored in the working file StayFilter.java: the production code stays clean and the explanations live in the wiki.

package com.velitask.plugin.official.processing.filters;

import static com.velitask.plugin.official.processing.FormulaParameters.decimal;

import com.velitask.sdk.data.SensorAtom;
import com.velitask.sdk.properties.DoubleSpinnerProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.scenario.FilterStep;
import com.velitask.sdk.scenario.IStepControl;
import com.velitask.sdk.scenario.StepException;
import com.velitask.sdk.scenario.StepInput;

/* Stop straightening: the points that stayed near the first one are moved to it. */
public abstract class StayFilter<A extends SensorAtom> extends FilterStep<A> {

    /* Parameter names: keys in the step settings. */
    public static final String RADIUS_METERS = "radiusMeters";
    public static final String MIN_STAY_SEC = "minStaySec";

    /* Localization key prefix: Plugin.ImportStep.stayFilter. */
    private static final String KEY = _KEY + ".stayFilter";
    private static final int CANCEL_CHECK_MASK = 0xFFF;

    private DoubleSpinnerProperty mRadiusMeters;
    private DoubleSpinnerProperty mMinStaySec;

    protected StayFilter(String name, int version, Class<A> atomClass) {
        super(name, version, atomClass);
    }

    /* Distance between two atoms in metres; computed by the subclass. */
    protected abstract double distanceMeters(A from, A to);

    /* Copies the value of the anchor atom (from) to the stop atom (to). */
    protected abstract void copyValue(A from, A to);

    @Override
    /* Two parameters; both default to 0. */
    public IProperty[] defineParameters() {
        mRadiusMeters = decimal(RADIUS_METERS, localized(KEY + "." + RADIUS_METERS + ".title"),
                localizedOrEmpty(KEY + "." + RADIUS_METERS + ".description"), 0);
        mMinStaySec = decimal(MIN_STAY_SEC, localized(KEY + "." + MIN_STAY_SEC + ".title"),
                localizedOrEmpty(KEY + "." + MIN_STAY_SEC + ".description"), 0);
        return new IProperty[]{mRadiusMeters, mMinStaySec};
    }

    @Override
    /* Walk over the atoms with the anchor point anchor and look for a stop that starts at it. */
    protected A[] filter(StepInput<A> input, IStepControl control) throws StepException {
        parameters();
        A[] atoms = input.atoms();
        double radius = mRadiusMeters.get();
        long minStayMs = (long) (mMinStaySec.get() * 1000);
        int anchor = 0;
        while (anchor < atoms.length) {
            if ((anchor & CANCEL_CHECK_MASK) == 0) {
                control.throwIfCancelled();
            }
            /* end - the first atom after the stop: extend while the atoms belong to the same segment and are within the radius. */
            int end = anchor + 1;
            while (end < atoms.length && atoms[end - 1].continuesWith(atoms[end])
                    && distanceMeters(atoms[anchor], atoms[end]) <= radius) {
                end++;
            }
            /* The stop counts: at least two atoms and it lasts longer than the minimum. */
            if (end - 1 > anchor && atoms[end - 1].timeRaw - atoms[anchor].timeRaw > minStayMs) {
                for (int i = anchor + 1; i < end; i++) {
                    /* All atoms of the stop get the value of the anchor. */
                    copyValue(atoms[anchor], atoms[i]);
                }
                anchor = end;
            } else {
                anchor++;
            }
        }
        /* The values changed: recalculate the increments. */
        ValueFilter.relink(atoms);
        return atoms;
    }
}

Purpose

StayFilter<A> is the base of the "Stop at a single point" filters. While the object stands still, the values "wander" and draw a tangle in place. The filter finds a stop — a group of consecutive atoms within a radius of the first one — and moves the value of the first atom to all the others. The subclass GeoStayFilter works with coordinates. The class does not remove atoms; identical values are merged afterwards by the "Data compression" step.

Unlike GeoStayGaps, which sets a pause at a stop, this filter only straightens the values and does not create pauses.

How it works

Parameters. Both are decimal(...) (a number not less than zero), default 0:

Parameter (key) UI name Meaning
radiusMeters Stay radius, m Points within the radius from the first point of the stop are moved to it. 0 — only points with the same coordinates
minStaySec Minimal stay time, s A shorter stop is not straightened. 0 — any stop is straightened

Algorithm. The anchor index anchor starts at 0. For it:

  1. end grows from anchor + 1 while the atom end continues the segment of the previous one (continuesWith) and distanceMeters(anchor, end) <= radius. The distance is always counted from the anchor atom.
  2. If at least one more atom joined the stop (end - 1 > anchor) and the timeRaw of the last atom is greater than the timeRaw of the anchor by more than minStayMs (minStaySec * 1000), all atoms from anchor + 1 to end - 1 get the value of the anchor through copyValue, and anchor moves to end.
  3. Otherwise anchor moves one atom forward.

At the end ValueFilter.relink(atoms) is called, which recalculates the increments. The same array is returned. Cancellation is checked once per 4096 anchor indexes.

Features and limitations

  • The step is not part of any built-in import plan: it is only registered in BuiltinScenarios (a sample of "stop straightening" for plugin authors). The user can add it to a scenario manually.
  • The step requires a pauses step before it (the default for FilterStep); a stop does not cross a pause, because continuesWith breaks it at the segment boundary.
  • The stop time is counted by the timeRaw of the first and the last atoms, not by the atom durations; the comparison is strict (>): a stop of exactly minStaySec seconds is not straightened.
  • If a stop does not count (too few points or it is shorter than the minimum), the anchor moves one atom forward and the search starts again; on long "almost stops" with a small step this increases the number of distance checks.
  • The atoms stay in the data: only the value changes. The number of points will be reduced by the subsequent compression.

Clone this wiki locally