-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Added Line Iterator #3197
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
Merged
Merged
Added Line Iterator #3197
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a824708
Added Line Iterator
Afif-Swaidan 5db702e
Merge branch 'ros-planning:main' into main
afifswaidan a1767f2
Updated Line Iterator to a new iteration method
afifswaidan a9c6d6d
Added the resolution as a parameter/ fixed linting
afifswaidan 9b79757
Added the resolution as a parameter/ fixed linting
afifswaidan 94934dd
Merge branch 'ros-planning:main' into main
afifswaidan 9ca4db6
Merge branch 'ros-planning:main' into main
afifswaidan c050299
Added unittests for the line iterator
afifswaidan 02f726d
Merge branch 'ros-planning:main' into main
afifswaidan 9df1b62
Added unittests based on "unittest" package
afifswaidan 6731c6b
Fixed __init__.py and rephrased some docstrings
afifswaidan d35d26d
Fixed linting errors
afifswaidan b8d0858
Fixed Linting Errors
afifswaidan dc438c4
Merge branch 'ros-planning:main' into main
afifswaidan 9d3992b
Merge branch 'ros-planning:main' into main
afifswaidan f9d48a2
Added some unittests and removed some methods
afifswaidan a571cde
Dummy commit for CircleCI Issue
afifswaidan 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
201 changes: 201 additions & 0 deletions
201
nav2_simple_commander/nav2_simple_commander/line_iterator.py
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,201 @@ | ||
| #! /usr/bin/env python3 | ||
| # Copyright 2021 Samsung Research America | ||
| # Copyright 2022 Afif Swaidan | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| This is a Python3 API for a line iterator. | ||
|
|
||
| It provides the ability to iterate through the points of a line. | ||
| """ | ||
|
|
||
|
|
||
| from cmath import sqrt | ||
|
|
||
|
|
||
| class LineIterator(): | ||
| def __init__(self, x0, y0, x1, y1, step_size=1.0): | ||
| """Initializer for LineIterator. | ||
|
|
||
| Args: | ||
| x0 (Float): Abscissa of the initial point | ||
| y0 (Float): Ordinate of the initial point | ||
| x1 (Float): Abscissa of the final point | ||
| y1 (Float): Ordinate of the final point | ||
| step_size (Float, optional): Resolution of increments. Defaults to 1. | ||
| """ | ||
| self.x0_ = x0 | ||
| self.y0_ = x0 | ||
| self.x1_ = x1 | ||
| self.y1_ = y1 | ||
| self.x_ = x0 | ||
| self.y_ = y0 | ||
| self.step_size_ = step_size | ||
|
|
||
| if x1 != x0 and y1 != y0: | ||
| self.valid_ = True | ||
| self.m_ = (y1-y0)/(x1-x0) | ||
| self.b_ = y1 - (self.m_*x1) | ||
| if (self.b_ < 0): | ||
| self.equation_ = "y = " + str(self.m_) + "*x " + str(self.b_) | ||
| else: | ||
| self.equation_ = "y = " + str(self.m_) + "*x + " + str(self.b_) | ||
| elif x1 == x0 and y1 != y0: | ||
| self.valid_ = True | ||
| self.equation_ = "x = " + str(x1) | ||
| elif y1 == y1 and x1 != x0: | ||
| self.valid_ = True | ||
| self.m_ = (y1-y0)/(x1-x0) | ||
| self.b_ = y1 - (self.m_*x1) | ||
| self.equation_ = "y = " + str(y1) | ||
| else: | ||
| self.valid_ = False | ||
| self.equation_ = "Invalid" | ||
|
|
||
| def isValid(self): | ||
| """Returns True if the line is valid | ||
| Returns: | ||
| Bool: Flag if live is valid (true) or not (false) | ||
| """ | ||
| return self.valid_ | ||
|
|
||
| def advance(self): | ||
SteveMacenski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Advances to the next point in the line.""" | ||
| if self.x1_ > self.x0_: | ||
| if self.x_ < self.x1_: | ||
| self.x_ = round(self.clamp( | ||
| self.x_ + self.step_size_, self.x0_, self.x1_), 5) | ||
| self.y_ = round(self.m_ * self.x_ + self.b_, 5) | ||
| else: | ||
| self.valid_ = False | ||
| elif self.x1_ < self.x0_: | ||
| if self.x_ > self.x1_: | ||
| self.x_ = round(self.clamp( | ||
| self.x_ - self.step_size_, self.x1_, self.x0_), 5) | ||
| self.y_ = round(self.m_ * self.x_ + self.b_, 5) | ||
| else: | ||
| self.valid_ = False | ||
| else: | ||
| if self.y1_ > self.y0_: | ||
| if self.y_ < self.y1_: | ||
| self.y_ = round(self.clamp( | ||
SteveMacenski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.y_ + self.step_size_, self.y0_, self.y1_), 5) | ||
| else: | ||
| self.valid_ = False | ||
| elif self.y1_ < self.y0_: | ||
| if self.y_ > self.y1_: | ||
| self.y_ = round(self.clamp( | ||
| self.y_ - self.step_size_, self.y1_, self.y0_), 5) | ||
| else: | ||
| self.valid_ = False | ||
| else: | ||
| self.valid_ = False | ||
|
|
||
| def getX(self): | ||
| """Returns the abscissa of the current point. | ||
|
|
||
| Returns: | ||
| Float: abscissa of current point | ||
| """ | ||
| return self.x_ | ||
|
|
||
| def getY(self): | ||
| """Returns the ordinate of the current point. | ||
|
|
||
| Returns: | ||
| Float: ordinate of current point | ||
| """ | ||
| return self.y_ | ||
|
|
||
| def getX0(self): | ||
| """Returns the abscissa of the initial point. | ||
|
|
||
| Returns: | ||
| Float: abscissa of initial point | ||
| """ | ||
| return self.x0_ | ||
|
|
||
| def getY0(self): | ||
| """Returns the ordinate of the intial point. | ||
|
|
||
| Returns: | ||
| Float: ordinate of intial point | ||
| """ | ||
| return self.y0_ | ||
|
|
||
| def getX1(self): | ||
| """Returns the abscissa of the final point. | ||
|
|
||
| Returns: | ||
| Float: abscissa of final point | ||
| """ | ||
| return self.x1_ | ||
|
|
||
| def getY1(self): | ||
| """Returns the ordinate of the final point. | ||
|
|
||
| Returns: | ||
| Float: ordinate of final point | ||
| """ | ||
| return self.y1_ | ||
|
|
||
| def get_line_length(self): | ||
afifswaidan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Returns the length of the line. | ||
|
|
||
| Returns: | ||
| Float: Line Length | ||
| """ | ||
| return sqrt(pow(self.x1_ - self.x0_, 2) + pow(self.y1_ - self.y0_, 2)) | ||
|
|
||
| def get_line_equation(self): | ||
afifswaidan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """Returns the equation of the line as a string. | ||
|
|
||
| Returns: | ||
| String: Line's Equation | ||
| """ | ||
| return self.equation_ | ||
|
|
||
| def get_curr_point_str(self): | ||
| """Returns the coordinates of the current point as string. | ||
|
|
||
| Returns: | ||
| String: Current Coordinates | ||
| """ | ||
| return "X: " + str(self.x_) + " Y: " + str(self.y_) | ||
afifswaidan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def get_curr_point(self): | ||
| """Returns the coordinates of the current point as list [X,Y]. | ||
|
|
||
| Returns: | ||
| List: Current Coordinates as float [X,Y] | ||
| """ | ||
| return [self.x_, self.y_] | ||
|
|
||
| def clamp(self, n, min_n, max_n): | ||
| """Class Helper Function: Clamps n to be between min_n and max_n | ||
|
|
||
| Args: | ||
| n (Float): input value | ||
| min_n (Float): minimum value | ||
| max_n (Float): maximum value | ||
|
|
||
| Returns: | ||
| Float: input value clamped between given min and max | ||
| """ | ||
| if n < min_n: | ||
| return min_n | ||
| elif n > max_n: | ||
| return max_n | ||
| else: | ||
| return n | ||
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.
Uh oh!
There was an error while loading. Please reload this page.