|
|
cfceb0 |
using System;
|
|
|
cfceb0 |
using System.Collections.Generic;
|
|
|
b82ef4 |
using Assistance.Drawing;
|
|
|
cfceb0 |
|
|
|
cfceb0 |
namespace Assistance {
|
|
|
cfceb0 |
public class Track {
|
|
|
589f9a |
public interface IOwner { }
|
|
|
61bedf |
|
|
|
a62e15 |
public class Handler {
|
|
|
589f9a |
public readonly IOwner owner;
|
|
|
a62e15 |
public readonly Track original;
|
|
|
a62e15 |
public readonly List<track> tracks = new List<track>();
|
|
|
589f9a |
public Handler(IOwner owner, Track original) {
|
|
|
a62e15 |
this.owner = owner;
|
|
|
a62e15 |
this.original = original;
|
|
|
a62e15 |
}
|
|
|
a62e15 |
}
|
|
|
a62e15 |
|
|
|
a62e15 |
public class Modifier {
|
|
|
a62e15 |
public readonly Handler handler;
|
|
|
a62e15 |
public readonly double timeOffset;
|
|
|
a62e15 |
|
|
|
a62e15 |
public Modifier(Handler handler, double timeOffset = 0.0)
|
|
|
a62e15 |
{ this.handler = handler; this.timeOffset = timeOffset; }
|
|
|
589f9a |
public IOwner owner
|
|
|
a62e15 |
{ get { return handler.owner; } }
|
|
|
a62e15 |
public Track original
|
|
|
a62e15 |
{ get { return handler.original; } }
|
|
|
702257 |
public virtual Point calcPoint(double originalIndex) {
|
|
|
702257 |
Point p = original.calcPoint(originalIndex);
|
|
|
7bbf70 |
p.originalIndex = originalIndex;
|
|
|
7bbf70 |
return p;
|
|
|
7bbf70 |
}
|
|
|
a62e15 |
}
|
|
|
a62e15 |
|
|
|
a62e15 |
public struct Point {
|
|
|
a62e15 |
public Assistance.Point position;
|
|
|
a62e15 |
public double pressure;
|
|
|
589f9a |
public Assistance.Point tilt;
|
|
|
a62e15 |
|
|
|
a62e15 |
public double originalIndex;
|
|
|
a62e15 |
public double time;
|
|
|
a62e15 |
public double length;
|
|
|
a62e15 |
|
|
|
a62e15 |
public bool final;
|
|
|
a62e15 |
|
|
|
702257 |
public Point(
|
|
|
702257 |
Assistance.Point position,
|
|
|
702257 |
double pressure = 0.5,
|
|
|
702257 |
Assistance.Point tilt = new Assistance.Point(),
|
|
|
a62e15 |
double originalIndex = 0.0,
|
|
|
a62e15 |
double time = 0.0,
|
|
|
a62e15 |
double length = 0.0,
|
|
|
a62e15 |
bool final = false
|
|
|
a62e15 |
) {
|
|
|
702257 |
this.position = position;
|
|
|
702257 |
this.pressure = pressure;
|
|
|
702257 |
this.tilt = tilt;
|
|
|
a62e15 |
this.originalIndex = originalIndex;
|
|
|
a62e15 |
this.time = time;
|
|
|
a62e15 |
this.length = length;
|
|
|
a62e15 |
this.final = final;
|
|
|
a62e15 |
}
|
|
|
a62e15 |
}
|
|
|
a62e15 |
|
|
|
77e178 |
|
|
|
61bedf |
private static long lastId;
|
|
|
77e178 |
|
|
|
61bedf |
public readonly long id;
|
|
|
c66393 |
public readonly Gdk.Device device;
|
|
|
61bedf |
public readonly long touchId;
|
|
|
702257 |
public readonly List<point> points = new List<point>();</point></point>
|
|
|
a62e15 |
public readonly KeyHistory<gdk.key>.Holder keyHistory;</gdk.key>
|
|
|
a62e15 |
public readonly KeyHistory<uint>.Holder buttonHistory;</uint>
|
|
|
cfceb0 |
|
|
|
a62e15 |
public readonly Modifier modifier;
|
|
|
a62e15 |
|
|
|
a62e15 |
public Handler handler;
|
|
|
a62e15 |
public int wayPointsRemoved;
|
|
|
a62e15 |
public int wayPointsAdded;
|
|
|
77c1c7 |
|
|
|
a62e15 |
public Track(
|
|
|
61bedf |
Gdk.Device device = null,
|
|
|
61bedf |
long touchId = 0,
|
|
|
61bedf |
KeyHistory<gdk.key>.Holder keyHistory = null,</gdk.key>
|
|
|
61bedf |
KeyHistory<uint>.Holder buttonHistory = null</uint>
|
|
|
a62e15 |
) {
|
|
|
61bedf |
this.id = ++lastId;
|
|
|
c66393 |
this.device = device;
|
|
|
61bedf |
this.touchId = touchId;
|
|
|
a62e15 |
this.keyHistory = keyHistory;
|
|
|
a62e15 |
this.buttonHistory = buttonHistory;
|
|
|
c66393 |
}
|
|
|
a62e15 |
|
|
|
61bedf |
public Track(Modifier modifier):
|
|
|
a62e15 |
this( modifier.original.device,
|
|
|
589f9a |
modifier.original.touchId,
|
|
|
a62e15 |
modifier.original.keyHistory.offset( modifier.timeOffset ),
|
|
|
61bedf |
modifier.original.buttonHistory.offset( modifier.timeOffset ) )
|
|
|
a62e15 |
{ this.modifier = modifier; }
|
|
|
77c1c7 |
|
|
|
a62e15 |
public Track original
|
|
|
a62e15 |
{ get { return modifier != null ? modifier.original : null; } }
|
|
|
a62e15 |
public double timeOffset
|
|
|
a62e15 |
{ get { return modifier != null ? modifier.timeOffset : 0.0; } }
|
|
|
720280 |
public long ticks
|
|
|
720280 |
{ get { return keyHistory.ticks; } }
|
|
|
c3ebff |
|
|
|
c3ebff |
public bool isChanged
|
|
|
c3ebff |
{ get { return wayPointsAdded != 0 || wayPointsRemoved != 0; } }
|
|
|
77c1c7 |
|
|
|
a62e15 |
public Track getRoot()
|
|
|
a62e15 |
{ return original == null ? this : original.getRoot(); }
|
|
|
a62e15 |
public int getLevel()
|
|
|
a62e15 |
{ return original == null ? 0 : original.getLevel() + 1; }
|
|
|
c66393 |
|
|
|
702257 |
public Point getFirst()
|
|
|
702257 |
{ return getPoint(0); }
|
|
|
702257 |
public Point getLast()
|
|
|
702257 |
{ return getPoint(points.Count - 1); }
|
|
|
a62e15 |
public bool isFinished()
|
|
|
0f2bf8 |
{ return points.Count > 0 && getLast().final; }
|
|
|
a62e15 |
|
|
|
a62e15 |
public int clampIndex(int index)
|
|
|
a62e15 |
{ return Math.Min(Math.Max(index, 0), points.Count - 1); }
|
|
|
a62e15 |
public int floorIndex(double index, out double frac) {
|
|
|
a62e15 |
int i = (int)Math.Floor(index + Geometry.precision);
|
|
|
a62e15 |
if (i > points.Count - 1)
|
|
|
a62e15 |
{ frac = 0.0; return points.Count - 1; }
|
|
|
a62e15 |
if (i < 0)
|
|
|
a62e15 |
{ frac = 0.0; return 0; }
|
|
|
a62e15 |
frac = Math.Max(0.0, index - (double)i);
|
|
|
a62e15 |
return i;
|
|
|
72b17c |
}
|
|
|
1cdf17 |
public int floorIndexNoClamp(double index)
|
|
|
1cdf17 |
{ return (int)Math.Floor(index + Geometry.precision); }
|
|
|
a62e15 |
public int floorIndex(double index)
|
|
|
1cdf17 |
{ return clampIndex(floorIndexNoClamp(index)); }
|
|
|
1cdf17 |
public int ceilIndexNoClamp(double index)
|
|
|
1cdf17 |
{ return (int)Math.Ceiling(index - Geometry.precision); }
|
|
|
a62e15 |
public int ceilIndex(double index)
|
|
|
1cdf17 |
{ return clampIndex(ceilIndexNoClamp(index)); }
|
|
|
a62e15 |
|
|
|
702257 |
public Point getPoint(int index) {
|
|
|
a62e15 |
index = clampIndex(index);
|
|
|
702257 |
return index < 0 ? new Point() : points[index];
|
|
|
77c1c7 |
}
|
|
|
702257 |
public Point floorPoint(double index, out double frac)
|
|
|
702257 |
{ return getPoint(floorIndex(index, out frac)); }
|
|
|
702257 |
public Point floorPoint(double index)
|
|
|
702257 |
{ return getPoint(floorIndex(index)); }
|
|
|
702257 |
public Point ceilPoint(double index)
|
|
|
702257 |
{ return getPoint(ceilIndex(index)); }
|
|
|
77c1c7 |
|
|
|
702257 |
private delegate double PointFieldGetter(Point p);
|
|
|
702257 |
private double binarySearch(double value, PointFieldGetter getter) {
|
|
|
a62e15 |
// points[a].value <= value < points[b].value
|
|
|
77c1c7 |
|
|
|
a62e15 |
if (points.Count <= 0) return 0.0;
|
|
|
a62e15 |
int a = 0;
|
|
|
a62e15 |
double aa = getter(points[a]);
|
|
|
0f2bf8 |
if (Geometry.isLess(value, aa)) return 0.0;
|
|
|
a62e15 |
|
|
|
a62e15 |
int b = points.Count - 1;
|
|
|
a62e15 |
double bb = getter(points[b]);
|
|
|
a62e15 |
if (Geometry.isGreaterOrEqual(value, bb)) return (double)b;
|
|
|
77c1c7 |
|
|
|
a62e15 |
while(true) {
|
|
|
a62e15 |
int c = (a + b)/2;
|
|
|
a62e15 |
if (a == c) break;
|
|
|
a62e15 |
double cc = getter(points[c]);
|
|
|
a62e15 |
if (Geometry.isLess(value, cc))
|
|
|
a62e15 |
{ b = c; bb = cc; } else { a = c; aa = cc; }
|
|
|
77c1c7 |
}
|
|
|
a62e15 |
|
|
|
1cdf17 |
return Geometry.isLess(aa, bb) ? (double)a + (value - aa)/(bb - aa) : (double)a;
|
|
|
a62e15 |
}
|
|
|
77c1c7 |
|
|
|
a62e15 |
public double indexByOriginalIndex(double originalIndex)
|
|
|
702257 |
{ return binarySearch(originalIndex, delegate(Point p) { return p.originalIndex; }); }
|
|
|
a62e15 |
public double indexByTime(double time)
|
|
|
702257 |
{ return binarySearch(time, delegate(Point p) { return p.time; }); }
|
|
|
a62e15 |
public double indexByLength(double length)
|
|
|
702257 |
{ return binarySearch(length, delegate(Point p) { return p.length; }); }
|
|
|
a62e15 |
|
|
|
a62e15 |
public double originalIndexByIndex(double index) {
|
|
|
a62e15 |
double frac;
|
|
|
702257 |
Point p0 = floorPoint(index, out frac), p1 = ceilPoint(index);
|
|
|
a62e15 |
return Geometry.interpolationLinear(p0.originalIndex, p1.originalIndex, frac);
|
|
|
a62e15 |
}
|
|
|
a62e15 |
public double timeByIndex(double index) {
|
|
|
a62e15 |
double frac;
|
|
|
702257 |
Point p0 = floorPoint(index, out frac), p1 = ceilPoint(index);
|
|
|
a62e15 |
return Geometry.interpolationLinear(p0.time, p1.time, frac);
|
|
|
a62e15 |
}
|
|
|
a62e15 |
public double lengthByIndex(double index) {
|
|
|
a62e15 |
double frac;
|
|
|
702257 |
Point p0 = floorPoint(index, out frac), p1 = ceilPoint(index);
|
|
|
a62e15 |
return Geometry.interpolationLinear(p0.length, p1.length, frac);
|
|
|
77c1c7 |
}
|
|
|
cfceb0 |
|
|
|
702257 |
public Point calcPoint(double index) {
|
|
|
a62e15 |
return modifier == null
|
|
|
702257 |
? interpolateLinear(index)
|
|
|
702257 |
: modifier.calcPoint( originalIndexByIndex(index) );
|
|
|
a62e15 |
}
|
|
|
a62e15 |
|
|
|
702257 |
public Point interpolateLinear(double index) {
|
|
|
a62e15 |
double frac;
|
|
|
702257 |
Point p0 = floorPoint(index, out frac);
|
|
|
702257 |
Point p1 = ceilPoint(index);
|
|
|
702257 |
return interpolateLinear(p0, p1, frac);
|
|
|
a62e15 |
}
|
|
|
a62e15 |
|
|
|
702257 |
public static Point interpolateLinear(Point p0, Point p1, double l) {
|
|
|
a62e15 |
if (l <= Geometry.precision) return p0;
|
|
|
a62e15 |
if (l >= 1.0 - Geometry.precision) return p1;
|
|
|
702257 |
return new Point(
|
|
|
702257 |
Geometry.interpolationLinear(p0.position, p1.position, l),
|
|
|
702257 |
Geometry.interpolationLinear(p0.pressure, p1.pressure, l),
|
|
|
702257 |
Geometry.interpolationLinear(p0.tilt, p1.tilt, l),
|
|
|
a62e15 |
Geometry.interpolationLinear(p0.originalIndex, p1.originalIndex, l),
|
|
|
a62e15 |
Geometry.interpolationLinear(p0.time, p1.time, l),
|
|
|
702257 |
Geometry.interpolationLinear(p0.length, p1.length, l) );
|
|
|
cfceb0 |
}
|
|
|
7bbf70 |
|
|
|
7bbf70 |
public void print() {
|
|
|
702257 |
foreach(Point wp in points)
|
|
|
7bbf70 |
Console.Write(
|
|
|
7bbf70 |
"{2:f1}, ",
|
|
|
702257 |
wp.position.x,
|
|
|
702257 |
wp.position.y,
|
|
|
7bbf70 |
wp.originalIndex );
|
|
|
7bbf70 |
Console.WriteLine();
|
|
|
7bbf70 |
}
|
|
|
cfceb0 |
}
|
|
|
cfceb0 |
}
|
|
|
cfceb0 |
|