Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify sketch paths using Douglas-Peuker algorithm #150

Merged
merged 5 commits into from
Jun 12, 2021

Conversation

danielworkman
Copy link
Contributor

  • Added an implementation of the Douglas-Peuker algorithm for simplifying polylines/paths
  • This gets run on a sketch path a) each time a user lifts their finger after making a new sketch and b) when loading a saved survey. This means that older surveys saved before the addition of this enhancement can get the same performance gains.
  • The calculated epsilon value is quite crude but can be improved after additional testing if necessary
  • Added a "debug mode" option to the "Dev" menu to toggle the drawing of path points which is handy when analysing epsilon values
  • Using the current epsilon calculation there was a reduction of 54% of the total sketch coordinates after loading a large test project

…path and b) on loading of JSON so that older survey files can also make use of the performance improvement. Also added debug drawing of path points which makes deriving sensible epsilon values easier
@@ -62,11 +62,12 @@
public abstract class SexyTopoActivity extends AppCompatActivity {


protected static SurveyManager dataManager;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android studio claimed this was a memory leak

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep, it does whinge about that. It's because it's holding a reference to a Context. I don't think it's an issue - the only context we're setting is an ApplicationContext, which is (apparently) fine.

Looks like not a problem making this non-static though.

double distMax = 0;

for (int i = 1; i != pathSize; ++i) {
double dist = getDistanceFromLine(path.get(i), path.get(0), path.get(pathSize - 1));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering how much this function gets called we should really implement a version that does not perform the square root and compare against the square of the epsilon but we can optimise when and where necessary


return DouglasPeukerIteration(path, epsilon);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods may need moving to more appropriate homes but I'll leave that decision to you :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough - not a terrible place for them to live for now. Although they should have lowercase first letters because the initial caps looks a bit weird in Java.


double epsilon = Space2DUtils.SketchEpsilon(pathDetail);
List<Coord2D> simplifiedPath = Space2DUtils.Simplify(path, epsilon);
pathDetail.setPath(simplifiedPath);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether we should set this behind a version guard clause. That is, only do this if the file was saved with a SexyTopo earlier than the current version. Although that would be a pain in the bum to do that the way the file is currently written. Guess this Douggie-Puker algo is idempotent? May well not be worth it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming you use the same epsilon then yes, you'll get exactly the same result each time

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably understood what I meant, but just checking: we don't just need the result to be the same for the same input, but we also need the result to be the same for feeding the output of the first pass back into the algorithm. That is, we don't want the paths to get progressively simpler every time the file gets loaded.

@@ -88,6 +88,8 @@
android:orderInCategory="100"
app:showAsAction="never">
<menu>
<item android:id="@+id/action_debug_mode"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to hide this dev menu behind a settings flag as it looks a bit messy.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Just an FYI - you don't need to do anything. I've been fiddling with the menus in my Bric4 branch to add custom entries for instrument ops).

Copy link
Owner

@richsmith richsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Just needs those methods uncapitalised ;)

@danielworkman
Copy link
Contributor Author

danielworkman commented Jun 10, 2021 via email

@richsmith
Copy link
Owner

If you look at the implementation of the algorithm you can see that that will not be the case. You could run the algo 1000 times on a geometry simplified just once and nothing would happen

I guess I should stop being lazy and understand what the algo does!

[...]

OK, think I understand it now. Quite elegant. Yep, should be fine.

@@ -985,6 +983,15 @@ public void onClick(DialogInterface dialog, int id) {
}


private void toggleDebugMode() {
debugMode = !debugMode;
dataManager.broadcastSurveyUpdated();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really just want to call invalidate on the canvas so that you instantly see the path points but this will call the sync method hence how I noticed how long it would take to refresh. It's fine as long as the table has never been loaded though so I just don't do that. Worth broadcasting an invalidate style message instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahah - are you saying that the table view gets updated even if it's not currently active? That does make sense - it would explain a couple of bug reports about things hanging when updating data. And since I rarely use it that might explain why I didn't see the issue.

I assumed it wouldn't do anything f it's not currently running. (Well actually I suppose I hadn't thought about it.)

I don't think it's worth a separate broadcast option. There's probably a command to tell the activity to redraw itself.

However, it's a bit useless having the table update itself even when you can't see it. I think we need to add a check at the start of the sync methods to not bother if the activity is not Resumed and then sync in onResume().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep to repro:

  • Load that survey you sent me
  • Load table view
  • Load plan view
  • Pan away from the main survey (to discount draw time)
  • Draw a small sketch
  • Toggle debug mode

@richsmith richsmith merged commit 67cfd4a into richsmith:master Jun 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants