Skip to content

SlopeGroup

roosit edited this page Oct 16, 2026 · 2 revisions

SlopeGroup

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

package com.velitask.plugin.official.slope;

import com.velitask.sdk.data.SlopeSensorAtom;
import java.util.ArrayList;
import java.util.List;

/* A series of consecutive slope atoms of one type; a database row (SlopeGroupAtom) is built from it. */
public class SlopeGroup {

    /* Row identifier in the groups table; assigned after insertion into the database. */
    private long mId;

    /* Slope type of the group: -1 downhill, 0 flat, 1 uphill. */
    private final int mType;
    /* Atoms of the group in time order. */
    private final List<SlopeSensorAtom> mList = new ArrayList<>();

    /* The group is created by its first atom and takes its type. */
    public SlopeGroup(SlopeSensorAtom first) {
        mType = first.slopeType;
        mList.add(first);
    }

    public int getType() {
        return mType;
    }

    public List<SlopeSensorAtom> getList() {
        return mList;
    }

    public long getId() {
        return mId;
    }

    public void setId(long id) {
        mId = id;
    }

    public void add(SlopeSensorAtom atom) {
        mList.add(atom);
    }

    /*
     * Builds a row for the groups table: boundaries from the first and last atom,
     * sums of elevation gain and loss, slope percentage (x100).
     */
    public SlopeGroupAtom buildAtom(long sensorId, int measureIndex) {
        SlopeGroupAtom g = new SlopeGroupAtom();
        g.id = mId;
        g.sensor_id = sensorId;
        g.measureIndex = measureIndex;
        g.slopeType = mType;

        if (mList.isEmpty()) {
            return g;
        }
        /* The group starts at the first atom and ends at the window of the last atom. */
        SlopeSensorAtom first = mList.get(0);
        SlopeSensorAtom last = mList.get(mList.size() - 1);

        g.timeRaw = first.timeRaw;
        g.duration = (last.timeRaw + last.duration) - first.timeRaw;
        g.distance = first.distance;
        g.elevation = first.elevation;

        long endDistance = last.distance + last.distanceDelta;
        int endElevation = last.elevation + last.elevationDelta;
        g.distanceDelta = endDistance - first.distance;
        g.elevationDelta = endElevation - first.elevation;

        /* Elevation gain and loss: sums of positive and negative elevation increments of the atoms (loss is negative). */
        int eleUp = 0;
        int eleDown = 0;
        for (SlopeSensorAtom a : mList) {
            if (a.elevationDelta > 0) {
                eleUp += a.elevationDelta;
            } else if (a.elevationDelta < 0) {
                eleDown += a.elevationDelta;
            }
        }
        g.eleUp = eleUp;
        g.eleDown = eleDown;

        /* Percentage x100: elevation increment * 10000 / length. With zero length it stays 0. */
        if (g.distanceDelta > 0) {
            g.slopePercent = (int) (g.elevationDelta * 10000L / g.distanceDelta);
        }
        return g;
    }
}

Purpose

SlopeGroup is a group of consecutive slope atoms of one type (slopeType). It is created by SlopeDetector and can be turned into a row of the slope_groups table — SlopeGroupAtom.

How it works

  • The group type (-1 downhill, 0 flat, 1 uphill) is fixed by the first atom and does not change.
  • buildAtom(sensorId, measureIndex) assembles the row:
    • timeRaw, distance and elevation come from the first atom; duration runs from the start of the first atom to the end of the last one (last.timeRaw + last.duration - first.timeRaw);
    • distanceDelta and elevationDelta are the differences between the end of the last atom (distance + distanceDelta, elevation + elevationDelta) and the start of the first one;
    • eleUp is the sum of the positive elevationDelta values of the atoms, eleDown is the sum of the negative ones (the value is negative);
    • slopePercent is elevationDelta * 10000 / distanceDelta, that is, the slope percentage multiplied by 100, integer part; when distanceDelta <= 0 it stays 0.
  • The row id is 0 by default; the real identifier is assigned after insertion into the database through setId.
  • All distances and elevations are in millimeters, as in the sensor atoms.

Features and limitations

  • An empty atom list is impossible in the constructor (the first atom is passed immediately); the emptiness check in buildAtom is a safeguard.
  • The slope percentage is calculated for the whole group: it averages all atoms of the group and may differ from the percentages of individual atoms.
  • Integer division truncates the fractional part toward zero: a slope below 0.01% becomes 0.
  • eleUp and eleDown are calculated in the group, but the plugin indicators show the accumulated values of the atom, not these.

Clone this wiki locally