Blame mono/Assistance/Guideline.cs

cfceb0
using System;
cfceb0
using System.Collections.Generic;
b82ef4
using Assistance.Drawing;
cfceb0
cfceb0
namespace Assistance {
cfceb0
	public class Guideline {
b82ef4
		public static readonly Pen pen = new Pen("Light Gray");
b82ef4
		public static readonly Pen penActive = new Pen("Deep Sky Blue");
cfceb0
		public static readonly double snapLenght = 20.0;
cfceb0
		public static readonly double snapScale = 1.0;
cfceb0
	
cfceb0
		public virtual Point transformPoint(Point p) {
cfceb0
			return p;
cfceb0
		}
cfceb0
		
b82ef4
		public virtual void draw(Cairo.Context context, bool active) { }
cfceb0
b82ef4
		public void draw(Cairo.Context context) {
b82ef4
			draw(context, false);
cfceb0
		}
cfceb0
		
cfceb0
		public double calcTrackWeight(Track track) {
d13872
			if (track.points.Count < 1)
cfceb0
				return double.PositiveInfinity;
cfceb0
			double sumWeight = 0.0;
cfceb0
			double sumLength = 0.0;
cfceb0
			double sumDeviation = 0.0;
cfceb0
			
b82ef4
			Point prev = track.points[0].point;
b82ef4
			foreach(TrackPoint tp in track.points) {
b82ef4
				Point p = tp.point;
d13872
				double length = (p - prev).len();
cfceb0
				sumLength += length;
cfceb0
				
d13872
				double midStepLength = sumLength - 0.5*length;
d13872
				if (midStepLength > Geometry.precision) {
d13872
					double weight = length*Geometry.logNormalDistribuitionUnscaled(midStepLength, snapLenght, snapScale);
d13872
					sumWeight += weight;
cfceb0
				
d13872
					double deviation = (transformPoint(p) - p).len();
d13872
					sumDeviation += weight*deviation;
d13872
				}
d13872
				prev = p;
cfceb0
			}
d13872
			if (sumWeight < Geometry.precision)
d13872
				return double.PositiveInfinity;
cfceb0
			return sumDeviation/sumWeight;
cfceb0
		}
cfceb0
cfceb0
		public static Guideline findBest(List<guideline> guidelines, Track track) {</guideline>
cfceb0
			double bestWeight = double.PositiveInfinity;
cfceb0
			Guideline best = null;
cfceb0
			foreach(Guideline guideline in guidelines) {
cfceb0
				double weight = guideline.calcTrackWeight(track);
cfceb0
				if (weight < bestWeight) {
cfceb0
					bestWeight = weight;
cfceb0
					best = guideline;
cfceb0
				}
cfceb0
			}
cfceb0
			return best;
cfceb0
		}
cfceb0
	}
cfceb0
}
cfceb0