Skip to content

Commit

Permalink
Refactoring of point generation for the track.
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueAndi committed Mar 23, 2024
1 parent b96daa7 commit 5af5340
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 114 deletions.
177 changes: 101 additions & 76 deletions src/pyLineFollowerTrackGenerator/cmd_etrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# Variables
################################################################################
_CMD_NAME = "etrack"
_NUM_OF_POINTS_MIN = 14
_NUM_OF_POINTS_MIN = 30
_BASIC_TIME_STEP = 8 # [ms]

################################################################################
Expand All @@ -54,7 +54,45 @@
# Functions
################################################################################

# pylint: disable=too-many-locals, line-too-long
# pylint: disable=line-too-long, too-many-arguments
def _generate_points_along_x(num_points, distance, x_base, x_tolerance, y_base, y_tolerance, positive) -> list[list[int, int]]:
points = []

for idx in range(num_points):
if positive is False:
x = x_base - idx * distance
else:
x = x_base + idx * distance

y = np.random.uniform(y_base - y_tolerance / 2, y_base + y_tolerance)

x += x_tolerance
y += y_tolerance

points.append([x, y])

return points

# pylint: disable=line-too-long, too-many-arguments
def _generate_points_along_y(num_points, distance, x_base, x_tolerance, y_base, y_tolerance, positive) -> list[list[int, int]]:
points = []

for idx in range(num_points):
x = np.random.uniform(x_base - x_tolerance / 2, x_base + x_tolerance)

if positive is False:
y = y_base - idx * distance
else:
y = y_base + idx * distance

x += x_tolerance
y += y_tolerance

points.append([x, y])

return points

# pylint: disable=line-too-long, too-many-statements, too-many-locals
def _generate_points_along_e(num_points, rect_width, rect_height) -> list[list[int, int]]:
"""Generate a number of points along a virtual E inside a rectangle with the given
width/height.
Expand All @@ -68,117 +106,100 @@ def _generate_points_along_e(num_points, rect_width, rect_height) -> list[list[i
list[list[int, int]]: Point coordinates (x, y)
"""

# 4
# long
# *---------*
# | 2 |
# | *------*
# | | 2
# | short| small
# | *------*
# 4 | 2 |
# l | | short small
# o | *------*
# n | short| small
# g | *------*
# | | short small
# | *------*
# | | 2
# | *------*
# | 4 |
# | long | small
# *---------*
#
# -----------------------> x
#
num_points_on_short_side = num_points // 7
num_points_on_long_side = 2 * num_points_on_short_side
ratio_long_side = 1
ratio_short_side = 2/3 * ratio_long_side
ratio_small_side = 1/5 * ratio_long_side
num_long_sides = 3
num_short_sides = 4
num_small_sides = 5
num_points_long_side = int(ratio_long_side * num_points / (num_long_sides * ratio_long_side + num_short_sides * ratio_short_side + num_small_sides * ratio_small_side))
num_points_short_side = int(num_points_long_side * ratio_short_side)
num_points_small_side = int(num_points_long_side * ratio_small_side)
tolerance = 10 # [%]
width = rect_width * (100 - 2 * tolerance) // 100
height = rect_height * (100 - 2 * tolerance) // 100
x_tolerance = width * tolerance // 100
y_tolerance = height * tolerance // 100
long_ratio = 1
short_ratio = 2/3
distance_long_x = int(long_ratio * width / num_points_on_long_side)
distance_long_y = int(long_ratio * height / num_points_on_long_side)
distance_short_x = int(short_ratio * width / num_points_on_short_side)
distance_finger = height // 5
distance_long_x = int(ratio_long_side * width / num_points_long_side)
distance_long_y = int(ratio_long_side * height / num_points_long_side)
distance_short_x = int(ratio_short_side * width / num_points_short_side)
distance_small_y = int(ratio_small_side * height / num_points_small_side)
points = []

# Walk along x-axis in positive direction
x_base = 0
y_base = 0
for idx in range(num_points_on_long_side):
x = idx * distance_long_x
y = np.random.uniform(y_base - y_tolerance / 2, y_base + y_tolerance)

x += x_tolerance
y += y_tolerance
points += _generate_points_along_x(num_points_long_side, distance_long_x, x_base, x_tolerance, y_base, y_tolerance, True)

points.append([x, y])
# Walk along y-axis in positive direction
x_base = width - 1
y_base = 0
points += _generate_points_along_y(num_points_small_side, distance_small_y, x_base, x_tolerance, y_base, y_tolerance, True)

# Walk along x-axis in negative direction
x_base = width - 1
y_base = (1 * distance_finger) - 1
for idx in range(num_points_on_short_side):
x = x_base - idx * distance_short_x
y = np.random.uniform(y_base - y_tolerance / 2, y_base + y_tolerance)
y_base = (1 * distance_small_y) - 1
points += _generate_points_along_x(num_points_short_side, distance_short_x, x_base, x_tolerance, y_base, y_tolerance, False)

x += x_tolerance
y += y_tolerance

points.append([x, y])
# Walk along y-axis in positive direction
x_base = int((1 - ratio_short_side) * width - 1)
y_base = (1 * distance_small_y) - 1
points += _generate_points_along_y(num_points_small_side, distance_small_y, x_base, x_tolerance, y_base, y_tolerance, True)

# Walk along x-axis in positive direction
x_base = (1 - short_ratio) * width - 1
y_base = (2 * distance_finger) - 1
for idx in range(num_points_on_short_side):
x = x_base + idx * distance_short_x
y = np.random.uniform(y_base - y_tolerance / 2, y_base + y_tolerance)
x_base = int((1 - ratio_short_side) * width - 1)
y_base = (2 * distance_small_y) - 1
points += _generate_points_along_x(num_points_short_side, distance_short_x, x_base, x_tolerance, y_base, y_tolerance, True)

x += x_tolerance
y += y_tolerance

points.append([x, y])
# Walk along y-axis in positive direction
x_base = width - 1
y_base = (2 * distance_small_y) - 1
points += _generate_points_along_y(num_points_small_side, distance_small_y, x_base, x_tolerance, y_base, y_tolerance, True)

# Walk along x-axis in negative direction
x_base = width - 1
y_base = (3 * distance_finger) - 1
for idx in range(num_points_on_short_side):
x = x_base - idx * distance_short_x
y = np.random.uniform(y_base - y_tolerance / 2, y_base + y_tolerance)

x += x_tolerance
y += y_tolerance
y_base = (3 * distance_small_y) - 1
points += _generate_points_along_x(num_points_short_side, distance_short_x, x_base, x_tolerance, y_base, y_tolerance, False)

points.append([x, y])
# Walk along y-axis in positive direction
x_base = int((1 - ratio_short_side) * width - 1)
y_base = (3 * distance_small_y) - 1
points += _generate_points_along_y(num_points_small_side, distance_small_y, x_base, x_tolerance, y_base, y_tolerance, True)

# Walk along x-axis in positive direction
x_base = (1 - short_ratio) * width - 1
y_base = (4 * distance_finger) - 1
for idx in range(num_points_on_short_side):
x = x_base + idx * distance_short_x
y = np.random.uniform(y_base - y_tolerance / 2, y_base + y_tolerance)

x += x_tolerance
y += y_tolerance
x_base = int((1 - ratio_short_side) * width - 1)
y_base = (4 * distance_small_y) - 1
points += _generate_points_along_x(num_points_short_side, distance_short_x, x_base, x_tolerance, y_base, y_tolerance, True)

points.append([x, y])
# Walk along y-axis in positive direction
x_base = width - 1
y_base = (4 * distance_small_y) - 1
points += _generate_points_along_y(num_points_small_side, distance_small_y, x_base, x_tolerance, y_base, y_tolerance, True)

# Walk along x-axis in negative direction
x_base = width - 1
y_base = (5 * distance_finger) - 1
for idx in range(num_points_on_long_side):
x = x_base - idx * distance_long_x
y = np.random.uniform(y_base - y_tolerance / 2, y_base + y_tolerance)

x += x_tolerance
y += y_tolerance

points.append([x, y])
y_base = (5 * distance_small_y) - 1
points += _generate_points_along_x(num_points_long_side, distance_long_x, x_base, x_tolerance, y_base, y_tolerance, False)

# Walk along y-axis in negative direction
x_base = 0
for idx in range(num_points_on_long_side):
x = np.random.uniform(x_base - x_tolerance / 2, x_base + x_tolerance)
y = y_base - idx * distance_long_y

x += x_tolerance
y += y_tolerance

points.append([x, y])
y_base = (5 * distance_small_y) - 1
points += _generate_points_along_y(num_points_long_side, distance_long_y, x_base, x_tolerance, y_base, y_tolerance, False)

return points

Expand Down Expand Up @@ -250,11 +271,15 @@ def _exec(args):

points = _generate_points_along_e(num_points, image_width, image_height)

# 5 % after the first point
start_stop_line_location = 0.05

fig = generate_track_image( points,
image_width,
image_height,
image_line_width,
pixel_per_m,
start_stop_line_location,
is_debug_mode)

if is_debug_mode is True:
Expand Down
95 changes: 61 additions & 34 deletions src/pyLineFollowerTrackGenerator/cmd_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,44 @@
# Functions
################################################################################

# pylint: disable=line-too-long, too-many-arguments
def _generate_points_along_x(num_points, distance, x_base, x_tolerance, y_base, y_tolerance, positive) -> list[list[int, int]]:
points = []

for idx in range(num_points):
if positive is False:
x = x_base - idx * distance
else:
x = x_base + idx * distance

y = np.random.uniform(y_base - y_tolerance / 2, y_base + y_tolerance)

x += x_tolerance
y += y_tolerance

points.append([x, y])

return points

# pylint: disable=line-too-long, too-many-arguments
def _generate_points_along_y(num_points, distance, x_base, x_tolerance, y_base, y_tolerance, positive) -> list[list[int, int]]:
points = []

for idx in range(num_points):
x = np.random.uniform(x_base - x_tolerance / 2, x_base + x_tolerance)

if positive is False:
y = y_base - idx * distance
else:
y = y_base + idx * distance

x += x_tolerance
y += y_tolerance

points.append([x, y])

return points

# pylint: disable=too-many-locals, line-too-long
def _generate_points_along_rectangle(num_points, rect_width, rect_height) -> list[list[int, int]]:
"""Generate a number of points along a virtual rectangle with the given
Expand All @@ -76,53 +114,38 @@ def _generate_points_along_rectangle(num_points, rect_width, rect_height) -> lis
y_tolerance = height * tolerance // 100
distance_x = width // num_points_on_x_axis
distance_y = height // num_points_on_y_axis
points = []

#
# *------*
# | |
# | |
# | |
# *------*
#
# -----------------------> x
#

# Walk along x-axis in positive direction
x_base = 0
y_base = 0
for idx in range(num_points_on_x_axis):
x = idx * distance_x
y = np.random.uniform(y_base - y_tolerance / 2, y_base + y_tolerance)

x += x_tolerance
y += y_tolerance

points.append([x, y])
points1 = _generate_points_along_x(num_points_on_x_axis, distance_x, x_base, x_tolerance, y_base, y_tolerance, True)

# Walk along y-axis in positive direction
x_base = width - 1
for idx in range(num_points_on_y_axis):
x = np.random.uniform(x_base - x_tolerance / 2, x_base + x_tolerance)
y = idx * distance_y

x += x_tolerance
y += y_tolerance

points.append([x, y])
y_base = 0
points2 = _generate_points_along_y(num_points_on_y_axis, distance_y, x_base, x_tolerance, y_base, y_tolerance, True)

# Walk along x-axis in negative direction
x_base = width - 1
y_base = height - 1
for idx in range(num_points_on_x_axis):
x = x_base - idx * distance_x
y = np.random.uniform(y_base - y_tolerance / 2, y_base + y_tolerance)

x += x_tolerance
y += y_tolerance

points.append([x, y])
points3 = _generate_points_along_x(num_points_on_x_axis, distance_x, x_base, x_tolerance, y_base, y_tolerance, False)

# Walk along y-axis in negative direction
x_base = 0
for idx in range(num_points_on_y_axis):
x = np.random.uniform(x_base - x_tolerance / 2, x_base + x_tolerance)
y = y_base - idx * distance_y

x += x_tolerance
y += y_tolerance

points.append([x, y])
y_base = height - 1
points4 = _generate_points_along_y(num_points_on_y_axis, distance_y, x_base, x_tolerance, y_base, y_tolerance, False)

return points
return points1 + points2 + points3 + points4

# pylint: disable=too-many-locals
def _exec(args):
Expand Down Expand Up @@ -192,11 +215,15 @@ def _exec(args):

points = _generate_points_along_rectangle(num_points, image_width, image_height)

# 12.5 % after the first point, means in the middle of the lower rectangle part.
start_stop_line_location = 0.125

fig = generate_track_image( points,
image_width,
image_height,
image_line_width,
pixel_per_m,
start_stop_line_location,
is_debug_mode)

if is_debug_mode is True:
Expand Down
Loading

0 comments on commit 5af5340

Please sign in to comment.