Skip to content

SameValueCompress

roosit edited this page Nov 15, 2026 · 1 revision

SameValueCompress

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 SameValueCompress.java: the production code stays clean and the explanations live in the wiki.

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

import com.velitask.sdk.data.SensorAtom;
import com.velitask.sdk.scenario.CompressStep;
import com.velitask.sdk.scenario.IStepControl;
import com.velitask.sdk.scenario.SegmentView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* Data compression: merges consecutive identical points of a recording segment. Works with the atoms of any sensor. */
public final class SameValueCompress extends CompressStep<SensorAtom> {

    /* Stable step name in the import plans and in the saved settings. */
    public static final String NAME = "sameValueCompress";

    /* Step logic version is 1; the common atom type SensorAtom, so the step suits any sensor. */
    public SameValueCompress() {
        super(NAME, 1, SensorAtom.class);
    }

    /* Called by the base CompressStep for each recording segment separately. */
    @Override
    protected SensorAtom[] compress(SegmentView<SensorAtom> segment, IStepControl control) {
        SensorAtom[] atoms = segment.atoms();
        /* kept - the atoms that remain; last - the last kept one, the following ones are "merged" into it. */
        List<SensorAtom> kept = new ArrayList<>();
        SensorAtom last = null;
        for (SensorAtom atom : atoms) {
            /* The next atom fits - extend last by its duration; otherwise it stays separate. */
            if (last != null && canMerge(last, atom)) {
                last.add(atom);
            } else {
                kept.add(atom);
                last = atom;
            }
        }
        /* An array of the same type as the input (a typed empty sample array). */
        return kept.toArray(Arrays.copyOf(atoms, 0));
    }

    /* Merging is possible if the next atom has non-zero duration, neither has increments and the values are equal. */
    private boolean canMerge(SensorAtom last, SensorAtom next) {
        return next.duration != 0
                && last.hasZeroDelta()
                && next.hasZeroDelta()
                && next.sameValueAs(last);
    }
}

Purpose

The "Data compression" step merges consecutive identical points so that the data takes less space; the values do not change (the Plugin.ImportStep.sameValueCompress.description string). The step has no parameters.

How it works

Data. The base CompressStep cuts the input into recording segments (by segmentStart), calls compress for every segment and checks the outcome: the first and the last atom of the segment are kept, the atom before a pause has zero duration, the total duration of the segment has not changed. A violation is a StepException with the reason compress.segment-broken. The step requires a pauses stage before it in the scenario (requiresBefore).

Calculation. compress goes over the atoms of the segment in order. The first atom always remains. Every next atom atom is merged into the last kept one last (last.add(atom) extends the window of last by the duration of atom) if all four canMerge conditions hold:

  1. last != null — there is something to merge with;
  2. next.duration != 0 — the atom has non-zero duration (an atom of zero duration that closes a segment before a pause always remains);
  3. last.hasZeroDelta() and next.hasZeroDelta() — neither has increments, that is, the value does not change inside the atom;
  4. next.sameValueAs(last) — the values are exactly equal, without a tolerance.

Otherwise the atom is added to kept and becomes the new last.

Settings. None. The step title "Data compression" is set by the string Plugin.ImportStep.sameValueCompress.title.

Where it is used. The step is registered in BuiltinScenarios.steps() and is the last stage of almost every sensor in the GPX, FIT, TCX, TMG, GoPro, DJI and DJI SRT import plans, and for the computed sensors (distance, g-force, slope) it is the only compression stage. Sensors with several components (acceleration, rotation, orientation, angles) are compressed with a tolerance: ToleranceCompress.

Features and limitations

  • Only exactly equal values are merged: sensor noise that makes the values slightly different gives no compression — for that there is tolerance compression.
  • Atoms with a non-zero increment (the value changes linearly) are not merged, even if the neighbouring values are equal.
  • Event atoms (lap, gear changes, subtitles) are by the SDK rules never equal by value, so the step merges nothing for them.
  • The class works with SensorAtom; Arrays.copyOf(atoms, 0) is used for a typed result.

Clone this wiki locally