Add a convex Polygon obstacle into scenario

Hi, I try to add a convex Polygon static obstacle into scenario. But even though I get the vertices in the order of clockwise and confirm the first one also the last one, the obstacle is still not convex.
I don’t know if it’s because of the ‘initial damage state’ parameter by step of defining the static obstacle. I uses the Polygon center as the state position. Even though I also tried the first vertex point, still no difference.

4points broke

Hi,

can you share your code or the vertices you used?

Best,
Sebastian

Hi Sebastian, here is my code. I used convex hull to get the vertices

def give_vertices(points_constrains_x=[0, 15], points_constrains_y=[0, 3.5]):
    vertices = []
    rng = np.random.default_rng()  # set rng as a random generator
    points = rng.random((4, 2))  # xx random points in 2-D
    a = points_constrains_x[0]
    b = points_constrains_x[1]
    c = points_constrains_y[0]
    d = points_constrains_y[1]
    points[:, 0] = (b - a) * points[:, 0] + a
    points[:, 1] = (d - c) * points[:, 1] + c
    hull = ConvexHull(points)
    for i in range(len(hull.vertices)):
        vertices.append(points[hull.vertices[i] - 1])
    vertices.append(vertices[0])
    vertices = np.array(vertices)
    return vertices

Can you also share the code which you use to create the static obstacle?

Yes, of course

def generate_road_damage(scenario, road_damage_type) -> List:
    """Class representing randomly generating a list of road damage"""
    road_damage = []
    for i in range(len(road_damage_type)):
        severity = cal_severity(road_damage_type[i])
        static_obstacle_id = scenario.generate_object_id()
        vertices = give_vertices()
        obstacle_shape = Polygon(vertices)
        init_damage_state = State(position=vertices[0], orientation=0, time_step=0)
        rdm = RoadDamage(severity, static_obstacle_id, init_damage_state, obstacle_shape)
        road_damage.append(rdm)
    return road_damage

and the RoadDamage class is below:

class RoadDamage(StaticObstacle):
    """ Class representing road damage using CommonRoad StaticObstacle.
        Assumption:
        - The polygon is convex polygon
    """

    def __init__(self, severity: int, obstacle_id, initial_state, obstacle_shape: Polygon,
                 initial_center_lanelet_ids=None, initial_shape_lanelet_ids=None,
                 initial_signal_state=None, signal_series=None, ):
        super().__init__(obstacle_id=obstacle_id, obstacle_shape=obstacle_shape, obstacle_type=ObstacleType.UNKNOWN,
                         initial_state=initial_state, initial_center_lanelet_ids=initial_center_lanelet_ids,
                         initial_shape_lanelet_ids=initial_shape_lanelet_ids,
                         initial_signal_state=initial_signal_state, signal_series=signal_series)
        self.severity = severity
        self.obstacle_id = obstacle_id
        self.initial_state = initial_state
        self.obstacle_shape = obstacle_shape

I think the problem is not related to CommonRoad but on how you create the vertices.
I would create the vertices as follows (removing the -1):

    for i in range(len(hull.vertices)):
        vertices.append(points[hull.vertices[i]])

This should result in a convex polygon.

Thanks Sebastian, it works! I think I made a mistake when smoothing the logic of ConvexHull. Thanks for your help.