Edge cases handled gracefully for low point-count arrays

Sitations with low point-counts may have been problematic. Now, 0, 1 and 2 point arrays are explicitly handled.
Added a copy constructor to WptPt to allow copies to be made, rather than returning original array.
Cases where no data is returned always returns a zero-sized array rather than a null array.
This commit is contained in:
Andrew Davie 2016-03-30 15:14:24 +11:00
parent 7b4daba1eb
commit 5d4ab73620
3 changed files with 39 additions and 8 deletions

View file

@ -109,6 +109,27 @@ public class GPXUtilities {
public WptPt() {
}
public WptPt(WptPt toCopy) {
this.lat = toCopy.lat;
this.lon = toCopy.lon;
if (toCopy.name != null) {
this.name = new String(toCopy.name);
}
if (toCopy.link != null) {
this.link = new String(toCopy.link);
}
if (toCopy.category != null) {
this.category = new String(toCopy.category);
}
this.time = toCopy.time;
this.ele = toCopy.ele;
this.speed = toCopy.speed;
this.hdop = toCopy.hdop;
this.deleted = toCopy.deleted;
this.colourARGB = toCopy.colourARGB;
this.distance = toCopy.distance;
}
public void setDistance(double dist) {
distance = dist;
}

View file

@ -16,7 +16,7 @@ public abstract class AsynchronousResampler extends AsyncTask<String,Integer,Str
AsynchronousResampler(Renderable.RenderableSegment rs) {
this.rs = rs;
culled = null;
culled = new ArrayList<>(); // so we NEVER return a null list
}
@Override protected void onPostExecute(String result) {
@ -35,10 +35,10 @@ public abstract class AsynchronousResampler extends AsyncTask<String,Integer,Str
protected List<GPXUtilities.WptPt> resampleTrack(List<GPXUtilities.WptPt> pts, double dist) {
ArrayList<GPXUtilities.WptPt> newPts = new ArrayList<>();
List<GPXUtilities.WptPt> newPts = new ArrayList<>();
int ptCt = pts.size();
if (ptCt > 0) {
if (ptCt > 1) {
GPXUtilities.WptPt lastPt = pts.get(0);
double segSub = 0;
@ -76,6 +76,11 @@ public abstract class AsynchronousResampler extends AsyncTask<String,Integer,Str
GPXUtilities.WptPt newPoint = new GPXUtilities.WptPt( lastPt.getLatitude(), lastPt.getLongitude(), lastPt.time, lastPt.ele, lastPt.speed, lastPt. hdop);
newPoint.setDistance(cumDist);
newPts.add(newPts.size(), newPoint);
} else { // 0 and 1 point lines are just copied
for (GPXUtilities.WptPt pt : pts) {
newPts.add(new GPXUtilities.WptPt(pt));
}
}
return newPts;
}
@ -98,7 +103,7 @@ public abstract class AsynchronousResampler extends AsyncTask<String,Integer,Str
// Resample track, then analyse altitudes and set colours for each point
culled = resampleTrack(rs.getPoints(), segmentSize);
if (!isCancelled()) {
if (!isCancelled() && !culled.isEmpty()) {
int halfC = Algorithms.getRainbowColor(0.5);
@ -138,7 +143,7 @@ public abstract class AsynchronousResampler extends AsyncTask<String,Integer,Str
List<GPXUtilities.WptPt> points = rs.getPoints();
culled = resampleTrack(points, segmentSize);
if (!isCancelled()) {
if (!isCancelled() && !culled.isEmpty()) {
GPXUtilities.WptPt lastPt = points.get(0);
lastPt.speed = 0;
@ -211,10 +216,9 @@ public abstract class AsynchronousResampler extends AsyncTask<String,Integer,Str
// Reduce the point-count of the GPX track using Ramer-Douglas-Peucker algorithm.
points = rs.getPoints();
culled = new ArrayList<>();
int nsize = points.size();
if (nsize > 0) {
if (nsize > 2) {
culled = new ArrayList<>();
survivor = new boolean[nsize];
cullRamerDouglasPeucer(0, nsize - 1);
if (!isCancelled()) {
@ -224,6 +228,11 @@ public abstract class AsynchronousResampler extends AsyncTask<String,Integer,Str
culled.add(points.get(i));
}
}
} else { // make a copy of 0-1-2 point arrays
culled = new ArrayList<>();
for (GPXUtilities.WptPt pt : points) {
culled.add(new GPXUtilities.WptPt(pt));
}
}
return isCancelled() ? "" : "OK";
}

View file

@ -78,6 +78,7 @@ public class Renderable {
// When the asynchronous task has finished, it calls this function to set the 'culled' list
public void setRDP(List<GPXUtilities.WptPt> cull) {
assert cull!=null;
culled = cull;
// Find the segment's bounding box, to allow quick draw rejection later