Esempio n. 1
0
def place_by_sumo(antenna,
                  car_material_id,
                  lane_boundary_dict,
                  margin_dict,
                  cars_with_antenna=None):
    antenna = copy.deepcopy(antenna)
    antenna.clear()

    structure_group = objects.StructureGroup()
    structure_group.name = 'SUMO cars'

    veh_i = None
    c_present = False
    for veh_i, veh in enumerate(traci.vehicle.getIDList()):
        (x, y), angle, lane_id, length, width, height = [
            f(veh) for f in [
                traci.vehicle.getPosition, traci.vehicle.getAngle,
                traci.vehicle.getLaneID, traci.vehicle.getWidth,
                traci.vehicle.getLength, traci.vehicle.getHeight
            ]
        ]

        x, y = coord.convert_distances(lane_id, (x, y),
                                       lane_boundary_dict=lane_boundary_dict,
                                       margin_dict=margin_dict)
        #print((x, y, -angle, veh))

        car = objects.RectangularPrism(length,
                                       width,
                                       height,
                                       material=car_material_id)

        # na posição final do carro a coordenada do SUMO vai ficar levemente deslocada, digo, ele passa no x, y o
        # centro da frente do carro, e eu assumo que essa coordenada é o centro do carro, senão eu teria que ver a
        # direção, acha ok se ficar assim?
        car.translate((-length / 2, -width / 2, 0))
        car.rotate(-angle)
        car.translate((x, y, 0))

        car_structure = objects.Structure(name=veh)
        car_structure.add_sub_structures(car)
        structure_group.add_structures(car_structure)

        #antenna_vertice
        if cars_with_antenna is None or veh in cars_with_antenna:
            c_present = True
            antenna.add_vertice((x, y, height))

    if not c_present:
        return None, None

    if veh_i is None:
        return None, None

    return structure_group, antenna
Esempio n. 2
0
def place_on_line(origin_array,
                  destination_list,
                  dim_list,
                  space,
                  object,
                  antenna=None,
                  antenna_origin=None):
    """ Place object in a line separated by space

    :param origin_array: (x, y, z) or ((x, y, z),)
    :param destination_list: scalar or list the maximum coordinate of the line
    :param dim_list: 0, 1, 2 for x, y or z (one or list of)
    :param space: function that return the space between `object`
    :param object: a RWI Structure with "origin" in (0, 0, 0) (must have a valid dimension)
    :param antanna_origin: (x, y, z) normally "inside" the object
    :param antenna: VerticeList
    :return: a structure group
    """

    origin_array = np.array(origin_array, ndmin=2)
    n_lines = origin_array.shape[0]
    destination_list = np.array(destination_list, ndmin=1)
    dim_list = np.array(dim_list, ndmin=1)
    # if a list of destination and dim is not provided but a list of origin is,
    # assumes the same destination and dim for all origins
    if len(destination_list) == 1:
        destination_list = np.repeat(destination_list, n_lines)
    if len(dim_list) == 1:
        dim_list = np.repeat(dim_list, n_lines)
    if object.dimensions is None:
        raise errors.FormatError('"{}" has no dimensions'.format(object))

    structure_group = objects.StructureGroup()
    structure_group.name = object.name + ' in line'

    if antenna is not None:
        vertice_list = copy.deepcopy(antenna)
        vertice_list.clear()
        if antenna_origin is None:
            vertice_list_origin = np.array(0, 0, 0)
        else:
            vertice_list_origin = np.array(antenna_origin)

    obj_i = 0

    for origin, destination, dim in zip(origin_array, destination_list,
                                        dim_list):
        # position on `dim` accounting for all `object` and `space` placed
        last_obj_loc = origin[dim]
        while True:
            # no more objects fit (last could be the space)
            if last_obj_loc >= destination:
                break
            # check if object fit
            if object.dimensions[dim] + last_obj_loc >= destination:
                break
            # the object to be added
            new_object = copy.deepcopy(object)
            new_object.name += '{:03d}'.format(obj_i)
            # the origin of the new object
            new_object_origin = origin
            new_object_origin[dim] = last_obj_loc
            # move object to the new origin
            new_object.translate(new_object_origin)
            if antenna is not None:
                vertice_list.add_vertice(new_object_origin +
                                         vertice_list_origin)
            # add the new object to the structure group
            structure_group.add_structures(new_object)
            # where new objects should be placed
            last_obj_loc = new_object_origin[dim] + new_object.dimensions[
                dim] + space()
            obj_i += 1
    if antenna is not None:
        return structure_group, vertice_list
    else:
        return structure_group
def place_by_sumo(antenna, car_material_id, lane_boundary_dict, cars_with_antenna, use_fixed_receivers=False, use_pedestrians=False):
    antenna = copy.deepcopy(antenna)
    antenna.clear()

    structure_group = objects.StructureGroup()
    structure_group.name = 'SUMO cars'

    str_vehicles = ''
    veh_i = None
    c_present = False

    if use_pedestrians:
        for ped_i, ped in enumerate(traci.person.getIDList()):
            (x, y), angle, length, width = [f(ped) for f in [
                traci.person.getPosition,
                traci.person.getAngle, #Returns the angle of the named vehicle within the last step [degrees]
                traci.person.getLength,
                traci.person.getWidth
            ]]
            xinsite, yinsite = traci.simulation.convertGeo(x, y)
            pedestrian = objects.RectangularPrism(length, width, 1.72, material=car_material_id)
            pedestrian.translate((-length/2, -width/2, 0))
            pedestrian.rotate(90-angle) #use 90 degrees - angle to convert from y to x-axis the reference

            thisAngleInRad = np.radians(angle) #*np.pi/180
            deltaX = (length/2.0) * np.sin(thisAngleInRad)
            deltaY = (length/2.0) * np.cos(thisAngleInRad)
            pedestrian.translate((xinsite-deltaX, yinsite-deltaY, 0)) #now can translate

            pedestrian_structure = objects.Structure(name=ped)
            pedestrian_structure.add_sub_structures(pedestrian)
            structure_group.add_structures(pedestrian_structure)

            # 1.72 size of a perdestrian
            if c.use_vehicles_template:
                str_vehicles = get_model(str_vehicles,ped,xinsite-deltaX,yinsite-deltaY,0,90-angle,1.72) 

    for veh_i, veh in enumerate(traci.vehicle.getIDList()):
        (x, y), (x3,y3,z3), angle, lane_id, length, width, height = [f(veh) for f in [
            traci.vehicle.getPosition,
            traci.vehicle.getPosition3D, #Returns the 3D-position(three doubles) of the named vehicle (center of the front bumper) within the last step [m,m,m]
            traci.vehicle.getAngle,
            traci.vehicle.getLaneID,
            traci.vehicle.getLength,
            traci.vehicle.getWidth,
            traci.vehicle.getHeight
        ]]

        #x, y = coord.convert_distances(lane_id, (x,y), lane_boundary_dict=lane_boundary_dict)
        x, y = traci.simulation.convertGeo(x, y)
        #x2, y2 = traci.simulation.convertGeo(lon, lat, fromGeo=True)

        #the prism is draw using the first coordinate aligned with x, then y and z. Length is initially along x
        #and later the object will be rotates
        car = objects.RectangularPrism(length, width, height, material=car_material_id)

        #for proper rotation, first centralize the object on plane xy
        car.translate((-length/2, -width/2, 0))
        #now can rotate, but note SUMO assumes y-axis as the reference, and angle increases towards x-axis,
        #while we assume angles start from x-axis in our rotate method (see https://en.wikipedia.org/wiki/Rotation_matrix)
        car.rotate(90-angle) #use 90 degrees - angle to convert from y to x-axis the reference

        #SUMO reports position of middle of front bumper. We need to reposition to the middle of the vehicle
        #for that, use the angle to find to where the vehicle is facing and then translate
        thisAngleInRad = np.radians(angle) #*np.pi/180
        deltaX = (length/2.0) * np.sin(thisAngleInRad)
        deltaY = (length/2.0) * np.cos(thisAngleInRad)
        car.translate((x-deltaX, y-deltaY, z3)) #now can translate

        car_structure = objects.Structure(name=veh)
        car_structure.add_sub_structures(car)
        structure_group.add_structures(car_structure)

        if c.use_vehicles_template:
            str_vehicles = get_model(str_vehicles,veh,x-deltaX,y-deltaY,z3,90-angle,height,length,width) 

        #antenna_vertice
        if veh in cars_with_antenna:
            c_present = True
            #translate the antenna as the vehicle. Note the antenna is not rotated (we are using isotropic anyways)
            #adding Rx 0.1 above car's height, to ensure that it will not be blocked by the vehicle itself
            # if drone
            if ( veh.startswith('dflow') ):
                antenna.add_vertice((x-deltaX, y-deltaY, z3 - 0.1))
            else:
                antenna.add_vertice((x-deltaX, y-deltaY, z3 + height + 0.1))



    if c.use_vehicles_template:
        all_vehicles = str(vt.vehicles_template(searchList=[{'a':str_vehicles,'long':c.longitude,'lat':c.latitude}]))
    else:
        all_vehicles = ''
    if use_fixed_receivers:
        return structure_group, None, all_vehicles

    if not c_present: #there are no vehicles with antennas
        return None, None, None

    if veh_i is None: #there are no vehicles in the scene according to SUMO (traci)
        return None, None, None

    return structure_group, antenna, all_vehicles