1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
package tim.prune.function.compress.methods;
import tim.prune.data.DataPoint;
import tim.prune.data.MarkingData;
import tim.prune.data.NumberUtils;
import tim.prune.data.Track;
import tim.prune.function.compress.CompressionMethodType;
import tim.prune.function.compress.TrackDetails;
public class SingletonsMethod extends CompressionMethod
{
private final double _factor;
public SingletonsMethod(double factor) {
_factor = factor;
}
public SingletonsMethod(String inString) {
_factor = NumberUtils.getDoubleOrZero(recogniseString(inString) ? inString.substring(4) : inString);
}
public CompressionMethodType getType() {
return CompressionMethodType.SINGLETONS;
}
public String getParam() {
return "" + Math.abs(_factor);
}
public int compress(Track inTrack, TrackDetails inDetails, MarkingData inMarkings)
{
final int numPoints = inTrack.getNumPoints();
int numDeleted = 0;
double threshold = tweakFactor(_factor) * inDetails.getMeanRadians();
DataPoint currPoint = null, prevPoint = null;
final TrackDetails modifiedDetails = inDetails.modifyUsingMarkings(inMarkings);
// Loop over all points looking for points far away from neighbours
for (int i=0; i<numPoints; i++)
{
if (inMarkings.isPointMarkedForDeletion(i)) {
continue;
}
currPoint = inTrack.getPoint(i);
// Don't delete any waypoints or photo points
// Only interested in start and end of segments
if (!currPoint.isWaypoint() && !currPoint.hasMedia()
&& modifiedDetails.isSegmentStart(i) && modifiedDetails.isSegmentEnd(i))
{
// Measure distance from previous track point
if (DataPoint.calculateRadiansBetween(prevPoint, currPoint) > threshold)
{
// Now need to find next track point, and measure distances
DataPoint nextPoint = getNextTrackPoint(inTrack, i+1, inMarkings);
if (nextPoint == null || DataPoint.calculateRadiansBetween(currPoint, nextPoint) > threshold)
{
// Found a point to delete
inMarkings.markPointForDeletion(i);
numDeleted++;
}
}
}
// Remember last (not-deleted) track point
if (!currPoint.isWaypoint() && !inMarkings.isPointMarkedForDeletion(i)) {
prevPoint = currPoint;
}
}
return numDeleted;
}
/**
* If the parameter is greater than 1, take the inverse
* so we can multiply it by the distance to get a threshold
*/
private double tweakFactor(double inParam)
{
if (inParam <= 0.0) {
return 1.0;
}
return inParam <= 1.0 ? inParam : (1.0 / inParam);
}
public String getSettingsString() {
return getType().getKey() + _factor;
}
static boolean recogniseString(String inString) {
return recogniseString(inString, CompressionMethodType.SINGLETONS);
}
}
|