-
Notifications
You must be signed in to change notification settings - Fork 6
1122 test path smoothing with splines #1139
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
Open
mFragaBA
wants to merge
12
commits into
main
Choose a base branch
from
1122-test-path-smoothing-with-splines
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4229308
feat: add spline post processing to astar path
mFragaBA db7b5c3
feat: add path simplification
mFragaBA c913692
fix twitching during path following and path recalculation
mFragaBA b102161
change splines params
mFragaBA 52b0f2a
fix: handle edge case of path of length 1
mFragaBA 37cab25
put path smoothing with splines under env var flag
mFragaBA 1eb64a0
fix: restore obstacle setup
mFragaBA c4cfe43
format: underscore unused vars
mFragaBA 5150caf
remove commented lines and add mention to source article
mFragaBA 3d8be76
run formatter
mFragaBA f0c1206
restore old decision time values
mFragaBA 529f6d0
Merge branch 'main' into 1122-test-path-smoothing-with-splines
mFragaBA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
defmodule SplinePath do | ||
@moduledoc """ | ||
This module defines methods to generate a spline path out of a waypoint path | ||
|
||
Based on https://qroph.github.io/2018/07/30/smooth-paths-using-catmull-rom-splines.html | ||
""" | ||
alias BotManager.Math.Vector | ||
|
||
@segment_point_amount 5 | ||
@tension 0.2 | ||
@alpha 0.5 | ||
Comment on lines
+9
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can also play with these values. I used the values suggested in the article, and played around with the |
||
|
||
def smooth_path(waypoints) when length(waypoints) < 3 do | ||
waypoints | ||
end | ||
|
||
def smooth_path(waypoints) do | ||
first_point = Enum.at(waypoints, 0) | ||
second_point = Enum.at(waypoints, 1) | ||
last_point = Enum.at(waypoints, -1) | ||
second_to_last_point = Enum.at(waypoints, -2) | ||
|
||
first_control_point = Vector.add(first_point, Vector.sub(first_point, second_point) |> Vector.normalize()) | ||
last_control_point = Vector.add(last_point, Vector.sub(last_point, second_to_last_point) |> Vector.normalize()) | ||
control_points = [first_control_point] ++ waypoints ++ [last_control_point] | ||
|
||
generate_spline_from_control_points(control_points) ++ [last_point] | ||
end | ||
|
||
defp generate_spline_from_control_points(control_points) do | ||
Enum.chunk_every(control_points, 4, 1, :discard) | ||
|> Enum.map(fn cps -> build_points_for_spline(cps) end) | ||
|> List.flatten() | ||
end | ||
|
||
defp build_points_for_spline([p0, p1, p2, p3]) do | ||
t01 = :math.pow(Vector.distance(p0, p1), @alpha) | ||
t12 = :math.pow(Vector.distance(p1, p2), @alpha) | ||
t23 = :math.pow(Vector.distance(p2, p3), @alpha) | ||
|
||
m1 = | ||
Vector.sub( | ||
Vector.mult(Vector.sub(p1, p0), 1 / t01), | ||
Vector.mult(Vector.sub(p1, p0), 1 / (t01 + t12)) | ||
) | ||
|> Vector.mult(t12) | ||
|> Vector.add(p2) | ||
|> Vector.sub(p1) | ||
|> Vector.mult(1.0 - @tension) | ||
|
||
m2 = | ||
Vector.sub( | ||
Vector.mult(Vector.sub(p3, p2), 1 / t23), | ||
Vector.mult(Vector.sub(p3, p1), 1 / (t12 + t23)) | ||
) | ||
|> Vector.mult(t12) | ||
|> Vector.add(p2) | ||
|> Vector.sub(p1) | ||
|> Vector.mult(1.0 - @tension) | ||
|
||
a = | ||
Vector.sub(p1, p2) | ||
|> Vector.mult(2.0) | ||
|> Vector.add(m1) | ||
|> Vector.add(m2) | ||
|
||
b = | ||
Vector.sub(p1, p2) | ||
|> Vector.mult(-3.0) | ||
|> Vector.sub(m1) | ||
|> Vector.sub(m1) | ||
|> Vector.sub(m2) | ||
|
||
c = m1 | ||
d = p1 | ||
|
||
# last point will be the next part start so do not add it | ||
Enum.map(0..(@segment_point_amount - 1), fn segment_num -> | ||
t = segment_num / @segment_point_amount | ||
|
||
d | ||
|> Vector.add(Vector.mult(c, t)) | ||
|> Vector.add(Vector.mult(b, t * t)) | ||
|> Vector.add(Vector.mult(a, t * t * t)) | ||
end) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to store the obstacles in order to perform collision checks when simplifying the path