

package de.biomedical_imaging.traJ.features;

import de.biomedical_imaging.traJ.Trajectory;

/**
 * Implements the mean curvilinear speed (MCS) and the mean straight-line speed (MSS) according to
 * 
 *$MCS=\sum_{n=1}^{N}\frac{p_{n}-p_{n-1}}{\triangle t$
 * http://doi.org/10.1016/B978-0-12-391857-4.00009-4
 * 
 * @author Thorsten Wagner
 *
 */
public class MeanSpeedFeature extends AbstractTrajectoryFeature {

	private Trajectory t;
	private double timelag;
	
	/**
	 * 
	 * @param t Trajectory for which the speed is to be calculated.
	 * @param timelag Timelag between two steps.
	 */
	public MeanSpeedFeature(Trajectory t,double timelag) {
		this.t = t;
		this.timelag = timelag;
	}
	
	@Override
	/**
	 * Calculates the mean curvlinear speed and the mean straight-line speed.
	 * @return An double array where the first element is the mean curvilinear speed and the second the mean straight-line speed.
	 */
	public double[] evaluate() {
		double sum = 0;
		for(int i = 1; i < t.size(); i++){
			sum += t.get(i-1).distance(t.get(i))/timelag;
		}
		
		double meanspeed = sum/(t.size()-1);
		
		double netDistance = t.get(0).distance(t.get(t.size()-1));
		double straightLineSpeed = netDistance/((t.size()-1)*timelag);
		result = new double[]{meanspeed,straightLineSpeed};
		return result;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "Mean Speed Feature";
	}

	@Override
	public String getShortName() {
		// TODO Auto-generated method stub
		return "MEANSPEED";
	}

	@Override
	public void setTrajectory(Trajectory t) {
		this.t = t;
		result = null;
	}

}
