Skip to content

Commit

Permalink
Merge pull request #871 from gerhardol/dratasich/_simplify_track
Browse files Browse the repository at this point in the history
simplify track
  • Loading branch information
gerhardol authored Jan 19, 2020
2 parents 33011e3 + 2c4daee commit f7d53bb
Show file tree
Hide file tree
Showing 17 changed files with 1,703 additions and 956 deletions.
21 changes: 13 additions & 8 deletions app/res/menu/detail_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,28 @@
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/menu_share_activity"
android:orderInCategory="100"
android:title="@string/Share_activity"/>

<item
android:id="@+id/menu_edit_activity"
android:orderInCategory="100"
android:title="@string/Edit_activity"/>

<item
android:id="@+id/menu_share_activity"
android:id="@+id/menu_recompute_activity"
android:orderInCategory="100"
android:title="@string/Share_activity"/>
android:title="@string/Recompute_activity"/>

<item
android:id="@+id/menu_delete_activity"
android:id="@+id/menu_simplify_path"
android:orderInCategory="100"
android:title="@string/Delete_activity"/>
android:title="@string/path_simplification_menu"/>

<item
android:id="@+id/menu_recompute_activity"
android:id="@+id/menu_delete_activity"
android:orderInCategory="100"
android:title="@string/Recompute_activity"
android:enabled="false"/>
android:title="@string/Delete_activity"/>
</menu>
5 changes: 5 additions & 0 deletions app/res/values/pref_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
<string name="pref_pace_graph_smoothing">pref_pace_graph_smoothing</string>
<string name="pref_pace_graph_smoothing_filters">pref_pace_graph_smoothing_filters</string>

<string name="pref_path_simplification_save">pref_path_simplification_save</string>
<string name="pref_path_simplification_export_gpx">pref_path_simplification_export_gpx</string>
<string name="pref_path_simplification_tolerance">pref_path_simplification_tolerance</string>
<string name="pref_path_simplification_algorithm">pref_path_simplification_algorithm</string>

<string name="pref_mapbox_default_style">pref_mapbox_default_style2</string>
<string name="pref_runneruplive_active">pref_runneruplive_active</string>
<string name="pref_runneruplive_serveradress">pref_runneruplive_serveradress</string>
Expand Down
39 changes: 39 additions & 0 deletions app/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,45 @@
android:title="@string/log_extended_gps_title"
android:summary="@string/log_extended_gps_summary" />

<PreferenceCategory
android:key="@string/path_simplification"
android:title="@string/path_simplification">

<Preference
android:summary="@string/path_simplification_info" />

<CheckBoxPreference
android:key="@string/pref_path_simplification_save"
android:defaultValue="false"
android:persistent="true"
android:title="@string/path_simplification_save"
android:summary="@string/path_simplification_save_info" />

<CheckBoxPreference
android:key="@string/pref_path_simplification_export_gpx"
android:defaultValue="false"
android:persistent="true"
android:title="@string/path_simplification_export_gpx"
android:summary="@string/path_simplification_export_gpx_info" />

<org.runnerup.widget.TextPreference
android:key="@string/pref_path_simplification_tolerance"
android:defaultValue="3"
android:persistent="true"
android:inputType="number"
android:title="@string/path_simplification_tolerance"
android:summary="the higher the tolerance, the smoother the path; however, this might reduce the length of the path (curves instead of sharp edges)"/>

<ListPreference
android:key="@string/pref_path_simplification_algorithm"
android:defaultValue="ramer_douglas_peucker"
android:persistent="true"
android:title="@string/path_simplification_algorithm"
android:entries="@array/path_simplification_algorithm_titles"
android:entryValues="@array/path_simplification_algorithm_values"
android:summary="@string/path_simplification_algorithm_info" />
</PreferenceCategory>

</PreferenceScreen>

<PreferenceScreen
Expand Down
156 changes: 156 additions & 0 deletions app/src/main/com/goebl/simplify/AbstractSimplify.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
The MIT License (MIT)
Copyright (c) 2013 Heinrich Göbl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/*
* Source retrieved from: https://github.com/hgoebl/simplify-java
* No changes made.
*/

package com.goebl.simplify;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

/**
* Abstract base class for simplification of a polyline.
*
* @author hgoebl
* @since 06.07.13
*/
abstract class AbstractSimplify<T> {

private T[] sampleArray;

protected AbstractSimplify(T[] sampleArray) {
this.sampleArray = sampleArray;
}

/**
* Simplifies a list of points to a shorter list of points.
* @param points original list of points
* @param tolerance tolerance in the same measurement as the point coordinates
* @param highestQuality <tt>true</tt> for using Douglas-Peucker only,
* <tt>false</tt> for using Radial-Distance algorithm before
* applying Douglas-Peucker (should be a bit faster)
* @return simplified list of points
*/
public T[] simplify(T[] points,
double tolerance,
boolean highestQuality) {

if (points == null || points.length <= 2) {
return points;
}

double sqTolerance = tolerance * tolerance;

if (!highestQuality) {
points = simplifyRadialDistance(points, sqTolerance);
}

points = simplifyDouglasPeucker(points, sqTolerance);

return points;
}

T[] simplifyRadialDistance(T[] points, double sqTolerance) {
T point = null;
T prevPoint = points[0];

List<T> newPoints = new ArrayList<T>();
newPoints.add(prevPoint);

for (int i = 1; i < points.length; ++i) {
point = points[i];

if (getSquareDistance(point, prevPoint) > sqTolerance) {
newPoints.add(point);
prevPoint = point;
}
}

if (prevPoint != point) {
newPoints.add(point);
}

return newPoints.toArray(sampleArray);
}

private static class Range {
private Range(int first, int last) {
this.first = first;
this.last = last;
}

int first;
int last;
}

T[] simplifyDouglasPeucker(T[] points, double sqTolerance) {

BitSet bitSet = new BitSet(points.length);
bitSet.set(0);
bitSet.set(points.length - 1);

List<Range> stack = new ArrayList<Range>();
stack.add(new Range(0, points.length - 1));

while (!stack.isEmpty()) {
Range range = stack.remove(stack.size() - 1);

int index = -1;
double maxSqDist = 0f;

// find index of point with maximum square distance from first and last point
for (int i = range.first + 1; i < range.last; ++i) {
double sqDist = getSquareSegmentDistance(points[i], points[range.first], points[range.last]);

if (sqDist > maxSqDist) {
index = i;
maxSqDist = sqDist;
}
}

if (maxSqDist > sqTolerance) {
bitSet.set(index);

stack.add(new Range(range.first, index));
stack.add(new Range(index, range.last));
}
}

List<T> newPoints = new ArrayList<T>(bitSet.cardinality());
for (int index = bitSet.nextSetBit(0); index >= 0; index = bitSet.nextSetBit(index + 1)) {
newPoints.add(points[index]);
}

return newPoints.toArray(sampleArray);
}


public abstract double getSquareDistance(T p1, T p2);

public abstract double getSquareSegmentDistance(T p0, T p1, T p2);
}
41 changes: 41 additions & 0 deletions app/src/main/com/goebl/simplify/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
The MIT License (MIT)
Copyright (c) 2013 Heinrich Göbl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/*
* Source retrieved from: https://github.com/hgoebl/simplify-java
* No changes made.
*/

package com.goebl.simplify;

/**
* Access to X and Y coordinates (2D-Point).
*
* @author hgoebl
* @since 06.07.13
*/
public interface Point {
double getX();
double getY();
}
41 changes: 41 additions & 0 deletions app/src/main/com/goebl/simplify/PointExtractor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
The MIT License (MIT)
Copyright (c) 2013 Heinrich Göbl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/*
* Source retrieved from: https://github.com/hgoebl/simplify-java
* No changes made.
*/

package com.goebl.simplify;

/**
* Helper to get X and Y coordinates from a foreign class T.
*
* @author hgoebl
* @since 06.07.13
*/
public interface PointExtractor<T> {
double getX(T point);
double getY(T point);
}
Loading

0 comments on commit f7d53bb

Please sign in to comment.