Skip to content

Commit

Permalink
refactor obstacle class
Browse files Browse the repository at this point in the history
  • Loading branch information
ll7 committed Mar 25, 2024
1 parent b121981 commit 663552b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 55 deletions.
58 changes: 3 additions & 55 deletions robot_sf/nav/map_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,14 @@
import os
import json
import random

from math import sqrt
from typing import List, Union, Dict
from dataclasses import dataclass, field

import numpy as np

from robot_sf.nav.gloabal_route import GlobalRoute
from robot_sf.nav.nav_types import Vec2D, Line2D, Rect


@dataclass
class Obstacle:
"""
A class to represent an obstacle.
Attributes
----------
vertices : List[Vec2D]
The vertices of the obstacle.
lines : List[Line2D]
The lines that make up the obstacle. This is calculated in the post-init method.
vertices_np : np.ndarray
The vertices of the obstacle as a numpy array. This is calculated in the post-init method.
Methods
-------
__post_init__():
Validates and processes the vertices to create the lines and vertices_np attributes.
"""

vertices: List[Vec2D]
lines: List[Line2D] = field(init=False)
vertices_np: np.ndarray = field(init=False)

def __post_init__(self):
"""
Validates and processes the vertices to create the lines and vertices_np
attributes.
Raises a ValueError if no vertices are specified.
"""

if not self.vertices:
raise ValueError('No vertices specified for obstacle!')

# Convert vertices to numpy array
self.vertices_np = np.array(self.vertices)

# Create edges from vertices
edges = list(zip(self.vertices[:-1], self.vertices[1:])) \
+ [(self.vertices[-1], self.vertices[0])]

# Remove fake lines that are just points
edges = list(filter(lambda l: l[0] != l[1], edges))

# Create lines from edges
lines = [(p1[0], p2[0], p1[1], p2[1]) for p1, p2 in edges]
self.lines = lines

if not self.vertices:
print('WARNING: obstacle is just a single point that cannot collide!')
from robot_sf.nav.gloabal_route import GlobalRoute
from robot_sf.nav.obstacle import Obstacle


@dataclass
Expand Down
56 changes: 56 additions & 0 deletions robot_sf/nav/obstacle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

from dataclasses import dataclass, field
from typing import List
import numpy as np
from robot_sf.nav.nav_types import Vec2D, Line2D

@dataclass
class Obstacle:
"""
A class to represent an obstacle.
Attributes
----------
vertices : List[Vec2D]
The vertices of the obstacle.
lines : List[Line2D]
The lines that make up the obstacle. This is calculated in the post-init method.
vertices_np : np.ndarray
The vertices of the obstacle as a numpy array. This is calculated in the post-init method.
Methods
-------
__post_init__():
Validates and processes the vertices to create the lines and vertices_np attributes.
"""

vertices: List[Vec2D]
lines: List[Line2D] = field(init=False)
vertices_np: np.ndarray = field(init=False)

def __post_init__(self):
"""
Validates and processes the vertices to create the lines and vertices_np
attributes.
Raises a ValueError if no vertices are specified.
"""

if not self.vertices:
raise ValueError('No vertices specified for obstacle!')

# Convert vertices to numpy array
self.vertices_np = np.array(self.vertices)

# Create edges from vertices
edges = list(zip(self.vertices[:-1], self.vertices[1:])) \
+ [(self.vertices[-1], self.vertices[0])]

# Remove fake lines that are just points
edges = list(filter(lambda l: l[0] != l[1], edges))

# Create lines from edges
lines = [(p1[0], p2[0], p1[1], p2[1]) for p1, p2 in edges]
self.lines = lines

if not self.vertices:
print('WARNING: obstacle is just a single point that cannot collide!')

0 comments on commit 663552b

Please sign in to comment.