Skip to content

Commit

Permalink
refactor: makes factory for PathElement a classmethod
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Becker committed Jul 29, 2021
1 parent 83af430 commit c494057
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 59 deletions.
105 changes: 52 additions & 53 deletions reeds_shepp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,19 @@ class Gear(Enum):
BACKWARD = -1


def PathElement(param: float, steering: Steering, gear: Gear):
element = _PathElement(param, steering, gear)
if param >= 0:
return element
else:
return replace(element, param=-param).reverse_gear()


@dataclass(eq=True)
class _PathElement:
class PathElement:
param: float
steering: Steering
gear: Gear

@classmethod
def create(cls, param: float, steering: Steering, gear: Gear):
if param >= 0:
return cls(param, steering, gear)
else:
return cls(-param, steering, gear).reverse_gear()

def __repr__(self):
s = "{ Steering: " + self.steering.name + "\tGear: " + self.gear.name \
+ "\tdistance: " + str(round(self.param, 2)) + " }"
Expand Down Expand Up @@ -132,9 +131,9 @@ def path1(x, y, phi):
v = M(phi - t)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(u, Steering.STRAIGHT, Gear.FORWARD))
path.append(PathElement(v, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(u, Steering.STRAIGHT, Gear.FORWARD))
path.append(PathElement.create(v, Steering.LEFT, Gear.FORWARD))

return path

Expand All @@ -155,9 +154,9 @@ def path2(x, y, phi):
v = M(t - phi)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(u, Steering.STRAIGHT, Gear.FORWARD))
path.append(PathElement(v, Steering.RIGHT, Gear.FORWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(u, Steering.STRAIGHT, Gear.FORWARD))
path.append(PathElement.create(v, Steering.RIGHT, Gear.FORWARD))

return path

Expand All @@ -181,9 +180,9 @@ def path3(x, y, phi):
v = M(phi - t - u)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(u, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement(v, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(u, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement.create(v, Steering.LEFT, Gear.FORWARD))

return path

Expand All @@ -207,9 +206,9 @@ def path4(x, y, phi):
v = M(t + u - phi)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(u, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement(v, Steering.LEFT, Gear.BACKWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(u, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement.create(v, Steering.LEFT, Gear.BACKWARD))

return path

Expand All @@ -233,9 +232,9 @@ def path5(x, y, phi):
v = M(t - u - phi)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(u, Steering.RIGHT, Gear.FORWARD))
path.append(PathElement(v, Steering.LEFT, Gear.BACKWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(u, Steering.RIGHT, Gear.FORWARD))
path.append(PathElement.create(v, Steering.LEFT, Gear.BACKWARD))

return path

Expand Down Expand Up @@ -265,10 +264,10 @@ def path6(x, y, phi):
v = M(phi - t + 2*u)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(u, Steering.RIGHT, Gear.FORWARD))
path.append(PathElement(u, Steering.LEFT, Gear.BACKWARD))
path.append(PathElement(v, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(u, Steering.RIGHT, Gear.FORWARD))
path.append(PathElement.create(u, Steering.LEFT, Gear.BACKWARD))
path.append(PathElement.create(v, Steering.RIGHT, Gear.BACKWARD))

return path

Expand All @@ -293,10 +292,10 @@ def path7(x, y, phi):
v = M(t - phi)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(u, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement(u, Steering.LEFT, Gear.BACKWARD))
path.append(PathElement(v, Steering.RIGHT, Gear.FORWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(u, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement.create(u, Steering.LEFT, Gear.BACKWARD))
path.append(PathElement.create(v, Steering.RIGHT, Gear.FORWARD))

return path

Expand All @@ -320,10 +319,10 @@ def path8(x, y, phi):
v = M(t - phi + math.pi/2)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(math.pi/2, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement(u, Steering.STRAIGHT, Gear.BACKWARD))
path.append(PathElement(v, Steering.LEFT, Gear.BACKWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(math.pi/2, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement.create(u, Steering.STRAIGHT, Gear.BACKWARD))
path.append(PathElement.create(v, Steering.LEFT, Gear.BACKWARD))

return path

Expand All @@ -347,10 +346,10 @@ def path9(x, y, phi):
v = M(t - phi - math.pi/2)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(u, Steering.STRAIGHT, Gear.FORWARD))
path.append(PathElement(math.pi/2, Steering.RIGHT, Gear.FORWARD))
path.append(PathElement(v, Steering.LEFT, Gear.BACKWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(u, Steering.STRAIGHT, Gear.FORWARD))
path.append(PathElement.create(math.pi/2, Steering.RIGHT, Gear.FORWARD))
path.append(PathElement.create(v, Steering.LEFT, Gear.BACKWARD))

return path

Expand All @@ -373,10 +372,10 @@ def path10(x, y, phi):
v = M(phi - t - math.pi/2)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(math.pi/2, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement(u, Steering.STRAIGHT, Gear.BACKWARD))
path.append(PathElement(v, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(math.pi/2, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement.create(u, Steering.STRAIGHT, Gear.BACKWARD))
path.append(PathElement.create(v, Steering.RIGHT, Gear.BACKWARD))

return path

Expand All @@ -399,10 +398,10 @@ def path11(x, y, phi):
v = M(phi - t - math.pi/2)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(u, Steering.STRAIGHT, Gear.FORWARD))
path.append(PathElement(math.pi/2, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(v, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(u, Steering.STRAIGHT, Gear.FORWARD))
path.append(PathElement.create(math.pi/2, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(v, Steering.RIGHT, Gear.BACKWARD))

return path

Expand All @@ -426,10 +425,10 @@ def path12(x, y, phi):
v = M(t - phi)

if t >= 0 and u >= 0 and v >= 0:
path.append(PathElement(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement(math.pi/2, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement(u, Steering.STRAIGHT, Gear.BACKWARD))
path.append(PathElement(math.pi/2, Steering.LEFT, Gear.BACKWARD))
path.append(PathElement(v, Steering.RIGHT, Gear.FORWARD))
path.append(PathElement.create(t, Steering.LEFT, Gear.FORWARD))
path.append(PathElement.create(math.pi/2, Steering.RIGHT, Gear.BACKWARD))
path.append(PathElement.create(u, Steering.STRAIGHT, Gear.BACKWARD))
path.append(PathElement.create(math.pi/2, Steering.LEFT, Gear.BACKWARD))
path.append(PathElement.create(v, Steering.RIGHT, Gear.FORWARD))

return path
12 changes: 6 additions & 6 deletions test_reeds_shepp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class TestPathElement(unittest.TestCase):
def setUp(self) -> None:
self.element = PathElement(13, Steering.LEFT, Gear.FORWARD)
self.element = PathElement.create(13, Steering.LEFT, Gear.FORWARD)

def test_repr(self):
self.assertEqual(
Expand All @@ -25,16 +25,16 @@ def test_reverse_steering(self):
)

def test_with_negative_parameter(self):
element = PathElement(-1, Steering.LEFT, Gear.FORWARD)
element = PathElement.create(-1, Steering.LEFT, Gear.FORWARD)
self.assertEqual(
element,
PathElement(1, Steering.LEFT, Gear.BACKWARD)
PathElement.create(1, Steering.LEFT, Gear.BACKWARD)
)


class TestPathLength(unittest.TestCase):
def test_with_positive_path_elements(self):
path = [PathElement(1, Steering.LEFT, Gear.FORWARD) for _ in range(2)]
path = [PathElement.create(1, Steering.LEFT, Gear.FORWARD) for _ in range(2)]
self.assertEqual(
path_length(path),
2
Expand All @@ -43,7 +43,7 @@ def test_with_positive_path_elements(self):

class TestTimeflip(unittest.TestCase):
def setUp(self) -> None:
self.path = [PathElement(1, Steering.LEFT, g) for g in (Gear.FORWARD, Gear.BACKWARD)]
self.path = [PathElement.create(1, Steering.LEFT, g) for g in (Gear.FORWARD, Gear.BACKWARD)]
self.timeflipped = timeflip(self.path)

def test_it_flips_forward_backward(self):
Expand All @@ -65,7 +65,7 @@ def test_it_does_not_mutate_original_path(self):

class TestReflect(unittest.TestCase):
def setUp(self) -> None:
self.path = [PathElement(1, s, Gear.FORWARD) for s in (Steering.LEFT, Steering.STRAIGHT, Steering.RIGHT)]
self.path = [PathElement.create(1, s, Gear.FORWARD) for s in (Steering.LEFT, Steering.STRAIGHT, Steering.RIGHT)]
self.reflected = reflect(self.path)

def test_it_reflects_steering(self):
Expand Down

0 comments on commit c494057

Please sign in to comment.