示例#1
0
def test_stage_attributes_managers():
    cfg_settings = examples.settings.default_sim_settings.copy()
    cfg_settings[
        "scene"] = "data/scene_datasets/habitat-test-scenes/van-gogh-room.glb"
    cfg_settings["enable_physics"] = True
    hab_cfg = examples.settings.make_cfg(cfg_settings)
    with habitat_sim.Simulator(hab_cfg) as sim:
        stage_name = cfg_settings["scene"]

        # get attribute managers
        stage_mgr = sim.get_stage_template_manager()

        # perform general tests for this attributes manager
        template0, _ = perform_general_tests(stage_mgr, stage_name)

        # verify gravity in template is as expected
        assert template0.gravity == mn.Vector3(0.0, -9.8, 0.0)

        # verify creating new template
        perform_add_blank_template_test(stage_mgr,
                                        template0.render_asset_handle)
示例#2
0
    def __init__(self, config: Config) -> None:
        self.config = config
        agent_config = self._get_agent_config()

        sim_sensors = []
        for sensor_name in agent_config.SENSORS:
            sensor_cfg = getattr(self.config, sensor_name)
            sensor_type = registry.get_sensor(sensor_cfg.TYPE)

            assert sensor_type is not None, "invalid sensor type {}".format(
                sensor_cfg.TYPE)
            sim_sensors.append(sensor_type(sensor_cfg))

        self._sensor_suite = SensorSuite(sim_sensors)
        self.sim_config = self.create_sim_config(self._sensor_suite)
        self._current_scene = self.sim_config.sim_cfg.scene.id
        self._sim = habitat_sim.Simulator(self.sim_config)
        self._action_space = spaces.Discrete(
            len(self.sim_config.agents[0].action_space))

        self._is_episode_active = False
示例#3
0
def test_no_navmesh_smoke():
    sim_cfg = habitat_sim.SimulatorConfiguration()
    agent_config = habitat_sim.AgentConfiguration()
    # No sensors as we are only testing to see if things work
    # with no navmesh and the navmesh isn't used for any exisitng sensors
    agent_config.sensor_specifications = []

    sim_cfg.scene.id = "data/scene_datasets/habitat-test-scenes/van-gogh-room.glb"
    # Make it try to load a navmesh that doesn't exists
    sim_cfg.scene.filepaths["navmesh"] = "/tmp/dne.navmesh"

    sim = habitat_sim.Simulator(
        habitat_sim.Configuration(sim_cfg, [agent_config]))

    sim.initialize_agent(0)

    random.seed(0)
    for _ in range(50):
        obs = sim.step(random.choice(list(agent_config.action_space.keys())))
        # Can't collide with no navmesh
        assert not obs["collided"]
示例#4
0
def test_empty_scene():
    cfg_settings = examples.settings.default_sim_settings.copy()

    # keyword "NONE" initializes a scene with no scene mesh
    cfg_settings["scene"] = "NONE"
    # test that depth sensor doesn't mind an empty scene
    cfg_settings["depth_sensor"] = True

    hab_cfg = examples.settings.make_cfg(cfg_settings)
    hab_cfg_mm = examples.settings.make_cfg(cfg_settings)
    mm = habitat_sim.metadata.MetadataMediator(hab_cfg.sim_cfg)
    hab_cfg_mm.metadata_mediator = mm

    test_list = [hab_cfg, hab_cfg_mm]
    for ctor_arg in test_list:
        with habitat_sim.Simulator(ctor_arg) as sim:
            assert sim.get_stage_initialization_template() == None

            # test that empty frames can be rendered without a scene mesh
            for _ in range(2):
                sim.step(random.choice(list(hab_cfg.agents[0].action_space.keys())))
示例#5
0
    def __init__(
        self,
        scene_filepath,
        labels=[0.0],
        img_size=(512, 512),
        output=["rgba"],
        sim=None,
    ):
        self.scene_filepath = scene_filepath
        self.labels = set(labels)
        self.cfg = self._config_sim(self.scene_filepath, img_size)

        if sim is None:
            sim = habitat_sim.Simulator(self.cfg)
        else:
            # If a sim is provided we have to make a new cfg
            self.cfg = self._config_sim(sim.config.sim_cfg.scene.id, img_size)
            sim.reconfigure(self.cfg)

        self.sim = sim
        self.pixels_per_meter = 0.1
        ref_point = self._get_pathfinder_reference_point(self.sim.pathfinder)
        self.tdv = TopdownView(self.sim, ref_point[1], self.pixels_per_meter)
        self.topdown_view = self.tdv.topdown_view

        self.pose_extractor = PoseExtractor(
            self.topdown_view, self.sim.pathfinder, self.pixels_per_meter
        )
        self.poses = self.pose_extractor.extract_poses(
            labels=self.labels
        )  # list of poses
        self.label_map = {0.0: "unnavigable", 1.0: "navigable"}

        # Configure the output each data sample
        self.out_name_to_sensor_name = {
            "rgba": "color_sensor",
            "depth": "depth_sensor",
            "semantic": "semantic_sensor",
        }
        self.output = output
示例#6
0
    def __init__(self):
        test_scene = Config().scene

        sim_settings = {
            "width": 640,  # Spatial resolution of the observations
            "height": 480,
            "scene": test_scene,  # Scene path
            "default_agent": 0,
            "sensor_height": 1.5,  # Height of sensors in meters
            "color_sensor": True,  # RGB sensor
            "semantic_sensor": True,  # Semantic sensor
            "depth_sensor": True,  # Depth sensor
            "seed": 1,
        }

        cfg = self.make_cfg(sim_settings)
        self.sim = habitat_sim.Simulator(cfg)

        self.action_names = list(
            cfg.agents[sim_settings["default_agent"]].action_space.keys())

        self.total_frames = 0
示例#7
0
def init_sim(sim_settings, start_pos, start_rot):
    cfg = make_cfg(sim_settings)
    sim = habitat_sim.Simulator(cfg)

    # Actions should change the position of the agent as well.
    action = habitat_sim.registry.get_move_fn("move_up")
    action.body_action = True
    action = habitat_sim.registry.get_move_fn("move_down")
    action.body_action = True

    random.seed(sim_settings["seed"])
    sim.seed(sim_settings["seed"])

    # Set agent state
    agent = sim.initialize_agent(sim_settings["default_agent"])
    agent_state = habitat_sim.AgentState()
    agent_state.position = np.array(start_pos)  # Agent start position set
    agent_state.rotation = quaternion.quaternion(
        *start_rot)  # Agent start orientation set
    agent.set_state(agent_state)

    return sim, agent, cfg
示例#8
0
def test_unproject():
    cfg_settings = examples.settings.default_sim_settings.copy()

    # configure some settings in case defaults change
    cfg_settings[
        "scene"] = "data/scene_datasets/habitat-test-scenes/apartment_1.glb"
    cfg_settings["width"] = 101
    cfg_settings["height"] = 101
    cfg_settings["sensor_height"] = 0
    cfg_settings["color_sensor"] = True

    # loading the scene
    hab_cfg = examples.settings.make_cfg(cfg_settings)
    with habitat_sim.Simulator(hab_cfg) as sim:
        # position agent
        sim.agents[0].scene_node.rotation = mn.Quaternion()
        sim.agents[0].scene_node.translation = mn.Vector3(0.5, 0, 0)

        # setup camera
        visual_sensor = sim._sensors["color_sensor"]
        scene_graph = sim.get_active_scene_graph()
        scene_graph.set_default_render_camera_parameters(
            visual_sensor._sensor_object)
        render_camera = scene_graph.get_default_render_camera()

        # test unproject
        center_ray = render_camera.unproject(mn.Vector2i(
            50, 50))  # middle of the viewport
        assert np.allclose(center_ray.origin, np.array([0.5, 0, 0]), atol=0.07)
        assert np.allclose(center_ray.direction,
                           np.array([0, 0, -1.0]),
                           atol=0.02)

        test_ray_2 = render_camera.unproject(mn.Vector2i(
            100, 100))  # bottom right of the viewport
        assert np.allclose(test_ray_2.direction,
                           np.array([0.569653, -0.581161, -0.581161]),
                           atol=0.07)
示例#9
0
def test_no_navmesh_smoke():
    sim_cfg = habitat_sim.SimulatorConfiguration()
    agent_config = habitat_sim.AgentConfiguration()
    # No sensors as we are only testing to see if things work
    # with no navmesh and the navmesh isn't used for any exisitng sensors
    agent_config.sensor_specifications = []

    sim_cfg.scene_id = "data/test_assets/scenes/stage_floor1.glb"
    hab_cfg = habitat_sim.Configuration(sim_cfg, [agent_config])

    mm = habitat_sim.metadata.MetadataMediator(sim_cfg)
    hab_cfg_mm = habitat_sim.Configuration(sim_cfg, [agent_config], mm)

    test_list = [hab_cfg, hab_cfg_mm]
    for ctor_arg in test_list:
        with habitat_sim.Simulator(ctor_arg) as sim:
            sim.initialize_agent(0)

            random.seed(0)
            for _ in range(50):
                obs = sim.step(random.choice(list(agent_config.action_space.keys())))
                # Can't collide with no navmesh
                assert not obs["collided"]
示例#10
0
    def init_common(self):
        self._cfg = make_cfg(self._sim_settings)
        scene_file = self._sim_settings["scene"]

        if (not os.path.exists(scene_file)
                and scene_file == default_sim_settings["test_scene"]):
            print(
                "Test scenes not downloaded locally, downloading and extracting now..."
            )
            utils.download_and_unzip(
                default_sim_settings["test_scene_data_url"], ".")
            print("Downloaded and extracted test scenes data.")

        self._sim = habitat_sim.Simulator(self._cfg)

        random.seed(self._sim_settings["seed"])
        self._sim.seed(self._sim_settings["seed"])

        # initialize the agent at a random start state
        start_state = self.init_agent_state(
            self._sim_settings["default_agent"])

        return start_state
示例#11
0
def setup_sim_and_sensors():
    left_rgb_sensor = create_sensor(position=[eye_seperation / 2, 0, 0], sensor_uuid="left_rgb_sensor")
    right_rgb_sensor = create_sensor(position=[-eye_seperation / 2, 0, 0], sensor_uuid="right_rgb_sensor")
    depth_sensor = create_sensor(sensor_uuid="depth_sensor", camera_type="D")

    # agent configuration has the sensor_specifications objects as a list
    new_agent_config = habitat_sim.AgentConfiguration()
    new_agent_config.sensor_specifications = [left_rgb_sensor, right_rgb_sensor, depth_sensor]
    # Configuration of the backend of the simulator includes default_agent_id set to 0
    backend_cfg = habitat_sim.SimulatorConfiguration()
    default_agent_id = backend_cfg.default_agent_id
    backend_cfg.foveation_distortion = True
    backend_cfg.scene.id = (
        "../multi_agent/data_files/skokloster-castle.glb"
    )
    # backend_cfg.scene.id = (
    #    "/Users/rajan/My_Replica/replica_v1/room_2/mesh.ply"
    # )
    # Tie the backend of the simulator and a list of agent configurations
    new_Configuration = habitat_sim.Configuration(backend_cfg, [new_agent_config])
    # When Simulator is called the agent configuration becomes Agents (only one agent in our case]
    new_sim = habitat_sim.Simulator(new_Configuration)
    return new_sim, default_agent_id
示例#12
0
def test_random_seed():
    # reconfigure to ensure pathfinder exists
    cfg_settings = examples.settings.default_sim_settings.copy()
    hab_cfg = examples.settings.make_cfg(cfg_settings)
    with habitat_sim.Simulator(hab_cfg) as sim:
        # Test that the same seed gives the same point
        sim.seed(1)
        point1 = sim.pathfinder.get_random_navigable_point()
        sim.seed(1)
        point2 = sim.pathfinder.get_random_navigable_point()
        assert all(point1 == point2)

        # Test that different seeds give different points
        sim.seed(2)
        point1 = sim.pathfinder.get_random_navigable_point()
        sim.seed(3)
        point2 = sim.pathfinder.get_random_navigable_point()
        assert any(point1 != point2)

        # Test that the same seed gives different points when sampled twice
        sim.seed(4)
        point1 = sim.pathfinder.get_random_navigable_point()
        point2 = sim.pathfinder.get_random_navigable_point()
        assert any(point1 != point2)
示例#13
0
def test_sim_reset(make_cfg_settings):
    hab_cfg = examples.settings.make_cfg(make_cfg_settings)
    hab_cfg_mm = examples.settings.make_cfg(make_cfg_settings)
    mm = habitat_sim.metadata.MetadataMediator(hab_cfg.sim_cfg)
    hab_cfg_mm.metadata_mediator = mm

    test_list = [hab_cfg, hab_cfg_mm]
    for ctor_arg in test_list:
        with habitat_sim.Simulator(ctor_arg) as sim:
            agent_config = sim.config.agents[0]
            sim.initialize_agent(0)
            initial_state = sim.agents[0].initial_state
            # Take random steps in the environment
            for _ in range(10):
                action = random.choice(list(agent_config.action_space.keys()))
                sim.step(action)

            sim.reset()
            new_state = sim.agents[0].get_state()
            same_position = all(initial_state.position == new_state.position)
            same_rotation = np.isclose(
                initial_state.rotation, new_state.rotation,
                rtol=1e-4)  # Numerical error can cause slight deviations
            assert same_position and same_rotation
示例#14
0
def make_simulator_from_settings(sim_settings):
    cfg = make_cfg(sim_settings)
    # clean-up the current simulator instance if it exists
    global sim
    global obj_attr_mgr
    global prim_attr_mgr
    global stage_attr_mgr
    global rigid_obj_mgr
    global metadata_mediator

    if sim != None:
        sim.close()
    # initialize the simulator
    sim = habitat_sim.Simulator(cfg)
    # Managers of various Attributes templates
    obj_attr_mgr = sim.get_object_template_manager()
    obj_attr_mgr.load_configs(
        str(os.path.join(data_path, "objects/example_objects")))
    prim_attr_mgr = sim.get_asset_template_manager()
    stage_attr_mgr = sim.get_stage_template_manager()
    # Manager providing access to rigid objects
    rigid_obj_mgr = sim.get_rigid_object_manager()
    # get metadata_mediator
    metadata_mediator = sim.metadata_mediator

    # UI-populated handles used in various cells.  Need to initialize to valid
    # value in case IPyWidgets are not available.
    # Holds the user's desired file-based object template handle
    global sel_file_obj_handle
    sel_file_obj_handle = obj_attr_mgr.get_file_template_handles()[0]
    # Holds the user's desired primitive-based object template handle
    global sel_prim_obj_handle
    sel_prim_obj_handle = obj_attr_mgr.get_synth_template_handles()[0]
    # Holds the user's desired primitive asset template handle
    global sel_asset_handle
    sel_asset_handle = prim_attr_mgr.get_template_handles()[0]
示例#15
0
def test_smoke_redwood_noise(scene, gpu2gpu, make_cfg_settings):
    if not osp.exists(scene):
        pytest.skip("Skipping {}".format(scene))

    if not habitat_sim.cuda_enabled and gpu2gpu:
        pytest.skip("Skipping GPU->GPU test")

    make_cfg_settings["depth_sensor"] = True
    make_cfg_settings["color_sensor"] = False
    make_cfg_settings["semantic_sensor"] = False
    make_cfg_settings["scene"] = scene
    hsim_cfg = make_cfg(make_cfg_settings)
    hsim_cfg.agents[0].sensor_specifications[0].noise_model = "RedwoodDepthNoiseModel"
    for sensor_spec in hsim_cfg.agents[0].sensor_specifications:
        sensor_spec.gpu2gpu_transfer = gpu2gpu

    with habitat_sim.Simulator(hsim_cfg) as sim:
        obs, gt = _render_and_load_gt(sim, scene, "depth_sensor", gpu2gpu)

        assert np.linalg.norm(
            obs["depth_sensor"].astype(float) - gt.astype(float)
        ) > 1.5e-2 * np.linalg.norm(gt.astype(float)), "Incorrect depth_sensor output"

    sim.close()
示例#16
0
def main(show_imgs=True, save_imgs=False):
    if save_imgs and not os.path.exists(output_path):
        os.mkdir(output_path)

    # [default scene lighting]

    # create the simulator and render flat shaded scene
    cfg = make_configuration()
    sim = habitat_sim.Simulator(cfg)
    agent_transform = place_agent(sim)
    get_obs(sim, show_imgs, save_imgs)

    # [scene swap shader]

    # close the simulator and re-initialize with DEFAULT_LIGHTING_KEY:
    sim.close()
    cfg = make_configuration()
    cfg.sim_cfg.scene_light_setup = habitat_sim.gfx.DEFAULT_LIGHTING_KEY
    sim = habitat_sim.Simulator(cfg)
    agent_transform = place_agent(sim)
    get_obs(sim, show_imgs, save_imgs)

    # create and register new light setup:
    my_scene_lighting_setup = [
        LightInfo(vector=[0.0, 2.0, 0.6, 0.0], model=LightPositionModel.GLOBAL)
    ]
    sim.set_light_setup(my_scene_lighting_setup, "my_scene_lighting")

    # reconfigure with custom key:
    new_cfg = make_configuration()
    new_cfg.sim_cfg.scene_light_setup = "my_scene_lighting"
    sim.reconfigure(new_cfg)
    agent_transform = place_agent(sim)
    get_obs(sim, show_imgs, save_imgs)

    # [/scene]

    # reset to default scene shading
    sim.close()
    cfg = make_configuration()
    sim = habitat_sim.Simulator(cfg)
    agent_transform = place_agent(sim)  # noqa: F841

    # [example 2]

    # get the physics object attributes manager
    obj_templates_mgr = sim.get_object_template_manager()

    # load some object templates from configuration files
    sphere_template_id = obj_templates_mgr.load_configs(
        str(os.path.join(data_path, "test_assets/objects/sphere")))[0]
    chair_template_id = obj_templates_mgr.load_configs(
        str(os.path.join(data_path, "test_assets/objects/chair")))[0]

    id_1 = sim.add_object(sphere_template_id)
    sim.set_translation([3.2, 0.23, 0.03], id_1)

    get_obs(sim, show_imgs, save_imgs)

    # [/example 2]

    # [example 3]

    # create a custom light setup
    my_default_lighting = [
        LightInfo(vector=[2.0, 2.0, 1.0, 0.0], model=LightPositionModel.CAMERA)
    ]
    # overwrite the default DEFAULT_LIGHTING_KEY light setup
    sim.set_light_setup(my_default_lighting)

    get_obs(sim, show_imgs, save_imgs)

    # [/example 3]

    # [example 4]
    id_2 = sim.add_object(chair_template_id)
    sim.set_rotation(mn.Quaternion.rotation(mn.Deg(-115), mn.Vector3.y_axis()),
                     id_2)
    sim.set_translation([3.06, 0.47, 1.15], id_2)

    get_obs(sim, show_imgs, save_imgs)

    # [/example 4]

    # [example 5]
    light_setup_2 = [
        LightInfo(
            vector=[2.0, 1.5, 5.0, 1.0],
            color=[0.0, 100.0, 100.0],
            model=LightPositionModel.GLOBAL,
        )
    ]
    sim.set_light_setup(light_setup_2, "my_custom_lighting")

    # [/example 5]

    remove_all_objects(sim)

    # [example 6]

    id_1 = sim.add_object(chair_template_id,
                          light_setup_key="my_custom_lighting")
    sim.set_rotation(mn.Quaternion.rotation(mn.Deg(-115), mn.Vector3.y_axis()),
                     id_1)
    sim.set_translation([3.06, 0.47, 1.15], id_1)

    id_2 = sim.add_object(chair_template_id,
                          light_setup_key="my_custom_lighting")
    sim.set_rotation(mn.Quaternion.rotation(mn.Deg(50), mn.Vector3.y_axis()),
                     id_2)
    sim.set_translation([3.45927, 0.47, -0.624958], id_2)

    get_obs(sim, show_imgs, save_imgs)

    # [/example 6]

    # [example 7]
    existing_light_setup = sim.get_light_setup("my_custom_lighting")

    # create a new setup with an additional light
    new_light_setup = existing_light_setup + [
        LightInfo(
            vector=[0.0, 0.0, 1.0, 0.0],
            color=[1.6, 1.6, 1.4],
            model=LightPositionModel.CAMERA,
        )
    ]

    # register the old setup under a new name
    sim.set_light_setup(existing_light_setup, "my_old_custom_lighting")

    # [/example 7]

    # [example 8]

    # register the new setup overwriting the old one
    sim.set_light_setup(new_light_setup, "my_custom_lighting")

    get_obs(sim, show_imgs, save_imgs)

    # [/example 8]

    # [example 9]
    sim.set_object_light_setup(id_1, habitat_sim.gfx.DEFAULT_LIGHTING_KEY)

    get_obs(sim, show_imgs, save_imgs)
示例#17
0
def main(show_imgs=True, save_imgs=False):
    if save_imgs:
        if not os.path.exists(output_path):
            os.mkdir(output_path)

    # [semantic id]

    # create the simulator and render flat shaded scene
    cfg = make_configuration(scene_file="NONE")
    sim = habitat_sim.Simulator(cfg)

    test_scenes = [
        "data/scene_datasets/habitat-test-scenes/apartment_1.glb",
        "data/scene_datasets/habitat-test-scenes/van-gogh-room.glb",
    ]

    for scene in test_scenes:
        # reconfigure the simulator with a new scene asset
        cfg = make_configuration(scene_file=scene)
        sim.reconfigure(cfg)
        agent_transform = place_agent(sim)  # noqa: F841
        get_obs(sim, show_imgs, save_imgs)

        # get the physics object attributes manager
        obj_templates_mgr = sim.get_object_template_manager()

        # load some chair object template from configuration file
        chair_template_id = obj_templates_mgr.load_object_configs(
            str(os.path.join(data_path, "test_assets/objects/chair")))[0]

        # add 2 chairs with default semanticId == 0 and arrange them
        chair_ids = []
        chair_ids.append(sim.add_object(chair_template_id))
        chair_ids.append(sim.add_object(chair_template_id))

        sim.set_rotation(
            mn.Quaternion.rotation(mn.Deg(-115), mn.Vector3.y_axis()),
            chair_ids[0])
        sim.set_translation([2.0, 0.47, 0.9], chair_ids[0])

        sim.set_translation([2.9, 0.47, 0.0], chair_ids[1])
        get_obs(sim, show_imgs, save_imgs)

        # set the semanticId for both chairs
        sim.set_object_semantic_id(2, chair_ids[0])
        sim.set_object_semantic_id(2, chair_ids[1])
        get_obs(sim, show_imgs, save_imgs)

        # set the semanticId for one chair
        sim.set_object_semantic_id(1, chair_ids[1])
        get_obs(sim, show_imgs, save_imgs)

        # add a box with default semanticId configured in the template
        box_template = habitat_sim.attributes.PhysicsObjectAttributes()
        box_template.render_asset_handle = str(
            os.path.join(data_path, "test_assets/objects/transform_box.glb"))

        box_template.scale = np.array([0.2, 0.2, 0.2])
        # set the default semantic id for this object template
        box_template.semantic_id = 10
        obj_templates_mgr = sim.get_object_template_manager()
        box_template_id = obj_templates_mgr.register_template(
            box_template, "box")
        box_id = sim.add_object(box_template_id)
        sim.set_translation([3.5, 0.47, 0.9], box_id)
        sim.set_rotation(
            mn.Quaternion.rotation(mn.Deg(-30), mn.Vector3.y_axis()), box_id)

        get_obs(sim, show_imgs, save_imgs)

        # set semantic id for specific SceneNode components of the box object
        box_visual_nodes = sim.get_object_visual_scene_nodes(box_id)
        box_visual_nodes[6].semantic_id = 3
        box_visual_nodes[7].semantic_id = 4
        get_obs(sim, show_imgs, save_imgs)
示例#18
0
    my_agent_config.sensor_specifications = [
        left_rgb_sensor, right_rgb_sensor, depth_sensor
    ]

    # Configuration of the backend of the simulator includes default_agent_id set to 0
    backend_cfg = habitat_sim.SimulatorConfiguration()
    default_agent_id = backend_cfg.default_agent_id
    backend_cfg.foveation_distortion = True
    #backend_cfg.foveation_distortion = False
    backend_cfg.scene.id = (
        "/Users/rajan/My_Replica/replica_v1/room_2/mesh.ply")

    # Tie the backend of the simulator and a list of agent configurations
    my_Configuration = habitat_sim.Configuration(backend_cfg,
                                                 [my_agent_config])
    my_sim = habitat_sim.Simulator(my_Configuration)
    print("Initial Left sensor orientation = {}".format(
        left_rgb_sensor.orientation))
    my_agent = my_sim.get_agent(default_agent_id)
    my_agent_state = my_agent.get_state()
    print("Agent orientation = {}".format(my_agent_state.rotation))
    print("Agent-Left RGB Sensor orientation = {}".format(
        my_agent_state.sensor_states['left_rgb_sensor'].rotation))

    my_agent_state.sensor_states[
        'right_rgb_sensor'].rotation = quaternion.from_rotation_vector(
            [0.0, 0.0, 0.0])
    my_agent_state.sensor_states[
        'left_rgb_sensor'].rotation = quaternion.from_rotation_vector(
            [0.0, 0.0, 0.0])
    '''
示例#19
0
    def __init__(self,
                 scene,
                 result_folder,
                 depth_camera=False,
                 loc_depth_cam='c',
                 foveation=False):

        self.agent_config = habitat_sim.AgentConfiguration()
        # Left sensor - # oreo perspective - staring at -ive z
        self.left_sensor = habitat_sim.SensorSpec()
        self.left_sensor.sensor_type = habitat_sim.SensorType.COLOR
        self.left_sensor.resolution = sensor_resolution
        self.left_sensor.uuid = "left_rgb_sensor"
        self.left_sensor.position = [-eye_separation / 2, 0.0, 0.0]
        self.left_sensor.orientation = np.array([0.0, 0.0, 0.0], dtype=float)
        self.left_sensor_hfov = math.radians(
            int(self.left_sensor.parameters['hfov']))
        self.focal_distance = sensor_resolution[0] / (
            2 * math.tan(self.left_sensor_hfov / 2))

        # Right sensor - # oreo perspective - staring at -ive z
        self.right_sensor = habitat_sim.SensorSpec()
        self.right_sensor.sensor_type = habitat_sim.SensorType.COLOR
        self.right_sensor.resolution = sensor_resolution
        self.right_sensor.uuid = "right_rgb_sensor"
        self.right_sensor.position = [eye_separation / 2, 0.0, 0.0]
        self.right_sensor.orientation = np.array([0.0, 0.0, 0.0], dtype=float)
        self.right_sensor_hfov = math.radians(
            int(self.right_sensor.parameters['hfov']))
        if self.right_sensor_hfov != self.left_sensor_hfov:
            print("Warning - Right and Left Sensor widths are not identical!")

        # Depth camera - At the origin of the reference coordinate axes (habitat frame)
        if depth_camera:
            self.num_sensors = 3
            self.depth_sensor = habitat_sim.SensorSpec()
            self.depth_sensor.sensor_type = habitat_sim.SensorType.DEPTH
            self.depth_sensor.resolution = sensor_resolution
            self.depth_sensor.uuid = "depth_sensor"
            if loc_depth_cam == 'l':
                self.depth_sensor.position = self.left_sensor.position
            elif loc_depth_cam == 'r':
                self.depth_sensor.position = self.right_sensor.position
            else:
                self.depth_sensor.position = [0.0, 0.0, 0.0]

            self.depth_sensor.orientation = np.array([0.0, 0.0, 0.0],
                                                     dtype=float)
            self.agent_config.sensor_specifications = [
                self.right_sensor, self.left_sensor, self.depth_sensor
            ]
        else:
            self.num_sensors = 2
            self.agent_config.sensor_specifications = [
                self.right_sensor, self.left_sensor
            ]

        self.backend_cfg = habitat_sim.SimulatorConfiguration()

        if foveation:
            self.backend_cfg.foveation_distortion = True

        self.backend_cfg.scene.id = scene  #This works in older habitat versions
        # self.backend_cfg.scene_id = scene #newer versions like the colab install

        self.destination = os.path.realpath(result_folder)
        if not os.path.isdir(self.destination):
            os.makedirs(self.destination)

        # Tie the backend of the simulator and the list of agent configurations (only one)
        self.sim_configuration = habitat_sim.Configuration(
            self.backend_cfg, [self.agent_config])
        self.sim = habitat_sim.Simulator(self.sim_configuration)
        self.agent_id = self.backend_cfg.default_agent_id
        self.agent = self.sim.get_agent(self.agent_id)
        self.initial_agent_state = self.agent.get_state()
        print(
            f"Agent rotation {self.initial_agent_state.rotation} Agent position {self.initial_agent_state.position}"
        )
        # agent_head_neck_rotation (not a part of habitat api to keep track of head/neck rotation wrt to the agent.
        # HabitatAI api agent rotation is not rotation of agent wrt to WCS followed by rotation of head/neck
        self.agent_head_neck_rotation = np.quaternion(1, 0, 0, 0)

        self.counter = 0  # counter for saccade file numbering
        self.filename = self.create_unique_filename(scene)
        self.my_images = self.get_sensor_observations()
        self.current_saved_image = "empty"
        return
示例#20
0
def test_dynamics():
    # This test assumes that default.phys_scene_config.json contains "physics simulator": "bullet".
    # TODO: enable dynamic override of this setting in simulation config structure

    cfg_settings = examples.settings.default_sim_settings.copy()

    cfg_settings[
        "scene"] = "data/scene_datasets/habitat-test-scenes/skokloster-castle.glb"
    # enable the physics simulator: also clears available actions to no-op
    cfg_settings["enable_physics"] = True
    cfg_settings["depth_sensor"] = True

    # test loading the physical scene
    hab_cfg = examples.settings.make_cfg(cfg_settings)
    with habitat_sim.Simulator(hab_cfg) as sim:
        obj_mgr = sim.get_object_template_manager()
        # make the simulation deterministic (C++ seed is set in reconfigure)
        np.random.seed(cfg_settings["seed"])
        assert obj_mgr.get_num_templates() > 0

        # test adding an object to the world
        obj_handle_list = obj_mgr.get_template_handles("cheezit")
        object_id = sim.add_object_by_handle(obj_handle_list[0])
        object2_id = sim.add_object_by_handle(obj_handle_list[0])
        # object_id = sim.add_object(1)
        # object2_id = sim.add_object(1)
        assert len(sim.get_existing_object_ids()) > 0

        # place the objects over the table in room
        sim.set_translation(np.array([-0.569043, 2.04804, 13.6156]), object_id)
        sim.set_translation(np.array([-0.569043, 2.04804, 12.6156]),
                            object2_id)

        # get object MotionType and continue testing if MotionType::DYNAMIC (implies a physics implementation is active)
        if (sim.get_object_motion_type(object_id) ==
                habitat_sim.physics.MotionType.DYNAMIC):
            object1_init_template = sim.get_object_initialization_template(
                object_id)
            object1_mass = object1_init_template.mass
            grav = sim.get_gravity()
            previous_object_states = [
                [sim.get_translation(object_id),
                 sim.get_rotation(object_id)],
                [
                    sim.get_translation(object2_id),
                    sim.get_rotation(object2_id)
                ],
            ]
            prev_time = sim.get_world_time()
            for _ in range(50):
                # force application at a location other than the origin should always cause angular and linear motion
                sim.apply_force(np.random.rand(3), np.random.rand(3),
                                object2_id)

                # TODO: expose object properties (such as mass) to python
                # Counter the force of gravity on the object (it should not translate)
                sim.apply_force(-grav * object1_mass, np.zeros(3), object_id)

                # apply torque to the "floating" object. It should rotate, but not translate
                sim.apply_torque(np.random.rand(3), object_id)

                # TODO: test other physics functions

                # test getting observation
                sim.step(
                    random.choice(list(hab_cfg.agents[0].action_space.keys())))

                # check that time is increasing in the world
                assert sim.get_world_time() > prev_time
                prev_time = sim.get_world_time()

                # check the object states
                # 1st object should rotate, but not translate
                assert np.allclose(previous_object_states[0][0],
                                   sim.get_translation(object_id))
                assert previous_object_states[0][1] != sim.get_rotation(
                    object_id)

                # 2nd object should rotate and translate
                assert not np.allclose(previous_object_states[1][0],
                                       sim.get_translation(object2_id))
                assert previous_object_states[1][1] != sim.get_rotation(
                    object2_id)

                previous_object_states = [
                    [
                        sim.get_translation(object_id),
                        sim.get_rotation(object_id)
                    ],
                    [
                        sim.get_translation(object2_id),
                        sim.get_rotation(object2_id)
                    ],
                ]

            # test setting DYNAMIC object to KINEMATIC
            assert sim.set_object_motion_type(
                habitat_sim.physics.MotionType.KINEMATIC, object2_id)
            assert (sim.get_object_motion_type(object2_id) ==
                    habitat_sim.physics.MotionType.KINEMATIC)

            sim.step(random.choice(list(
                hab_cfg.agents[0].action_space.keys())))

            # 2nd object should no longer rotate or translate
            assert np.allclose(previous_object_states[1][0],
                               sim.get_translation(object2_id))
            assert previous_object_states[1][1] == sim.get_rotation(object2_id)

            sim.step_physics(0.1)

            # test velocity get/set
            test_lin_vel = np.array([1.0, 0.0, 0.0])
            test_ang_vel = np.array([0.0, 1.0, 0.0])

            # no velocity setting for KINEMATIC objects
            sim.set_linear_velocity(test_lin_vel, object2_id)
            assert sim.get_linear_velocity(object2_id) == np.array(
                [0.0, 0.0, 0.0])

            sim.set_object_motion_type(habitat_sim.physics.MotionType.DYNAMIC,
                                       object2_id)
            sim.set_linear_velocity(test_lin_vel, object2_id)
            sim.set_angular_velocity(test_ang_vel, object2_id)
            assert sim.get_linear_velocity(object2_id) == test_lin_vel
            assert sim.get_angular_velocity(object2_id) == test_ang_vel

            # test modifying gravity
            new_object_start = np.array([100.0, 0, 0])
            sim.set_translation(new_object_start, object_id)
            new_grav = np.array([10.0, 0, 0])
            sim.set_gravity(new_grav)
            assert np.allclose(sim.get_gravity(), new_grav)
            assert np.allclose(sim.get_translation(object_id),
                               new_object_start)
            sim.step_physics(0.1)
            assert sim.get_translation(object_id)[0] > new_object_start[0]
示例#21
0
    def reconfigure(self, config: Config) -> None:
        self.config = config
        is_same_sound = config.AGENT_0.SOUND == self._current_sound
        if not is_same_sound:
            self._current_sound = self.config.AGENT_0.SOUND

        is_same_scene = config.SCENE == self._current_scene
        if not is_same_scene:
            self._current_scene = config.SCENE
            logging.debug('Current scene: {} and sound: {}'.format(
                self.current_scene_name, self._current_sound))

            if not self.config.USE_RENDERED_OBSERVATIONS:
                self._sim.close()
                del self._sim
                self.sim_config = self.create_sim_config(self._sensor_suite)
                self._sim = habitat_sim.Simulator(self.sim_config)
                self._update_agents_state()
                self._frame_cache = dict()
            else:
                with open(self.current_scene_observation_file, 'rb') as fo:
                    self._frame_cache = pickle.load(fo)
            logging.debug('Loaded scene {}'.format(self.current_scene_name))

            self.points, self.graph = load_metadata(self.metadata_dir)
            for node in self.graph.nodes():
                self._position_to_index_mapping[self.position_encoding(
                    self.graph.nodes()[node]['point'])] = node

        if not is_same_scene or not is_same_sound:
            self._audiogoal_cache = dict()
            self._spectrogram_cache = dict()

        self._episode_step_count = 0

        # set agent positions
        self._receiver_position_index = self._position_to_index(
            self.config.AGENT_0.START_POSITION)
        self._source_position_index = self._position_to_index(
            self.config.AGENT_0.GOAL_POSITION)
        # the agent rotates about +Y starting from -Z counterclockwise,
        # so rotation angle 90 means the agent rotate about +Y 90 degrees
        self._rotation_angle = int(
            np.around(
                np.rad2deg(
                    quat_to_angle_axis(
                        quat_from_coeffs(
                            self.config.AGENT_0.START_ROTATION))[0]))) % 360
        if not self.config.USE_RENDERED_OBSERVATIONS:
            self.set_agent_state(
                list(self.graph.nodes[self._receiver_position_index]['point']),
                self.config.AGENT_0.START_ROTATION)
        else:
            self._sim.set_agent_state(
                list(self.graph.nodes[self._receiver_position_index]['point']),
                quat_from_coeffs(self.config.AGENT_0.START_ROTATION))

        logging.debug(
            "Initial source, agent at: {}, {}, orientation: {}".format(
                self._source_position_index, self._receiver_position_index,
                self.get_orientation()))
示例#22
0
    args, _ = parser.parse_known_args()
    show_video = args.show_video
    make_video = args.make_video
else:
    show_video = False
    make_video = False

if make_video and not os.path.exists(output_path):
    os.mkdir(output_path)

cfg = make_configuration()
sim = None
replay_filepath = output_path + "replay.json"

if not sim:
    sim = habitat_sim.Simulator(cfg)
else:
    sim.reconfigure(cfg)

agent_state = habitat_sim.AgentState()
agent = sim.initialize_agent(0, agent_state)

# %% [markdown]
# ## Initial placement for agent and sensor
# %%

agent_node = sim.get_agent(0).body.object
sensor_node = sim._sensors["rgba_camera"]._sensor_object.object

# initial agent transform
agent_node.translation = [-0.15, -1.5, 1.0]
示例#23
0
def main():
    # We will define an action that moves the agent and turns it by some amount

    # First, define a class to keep the parameters of the control
    # @attr.s is just syntatic sugar for creating these data-classes
    @attr.s(auto_attribs=True, slots=True)
    class MoveAndSpinSpec:
        forward_amount: float
        spin_amount: float

    print(MoveAndSpinSpec(1.0, 45.0))

    # Register the control functor
    # This action will be an action that effects the body, so body_action=True
    @habitat_sim.register_move_fn(body_action=True)
    class MoveForwardAndSpin(habitat_sim.SceneNodeControl):
        def __call__(self, scene_node: habitat_sim.SceneNode,
                     actuation_spec: MoveAndSpinSpec):
            forward_ax = (np.array(
                scene_node.absolute_transformation().rotation_scaling())
                          @ habitat_sim.geo.FRONT)
            scene_node.translate_local(forward_ax *
                                       actuation_spec.forward_amount)

            # Rotate about the +y (up) axis
            rotation_ax = habitat_sim.geo.UP
            scene_node.rotate_local(mn.Deg(actuation_spec.spin_amount),
                                    rotation_ax)
            # Calling normalize is needed after rotating to deal with machine precision errors
            scene_node.rotation = scene_node.rotation.normalized()

    # We can also register the function with a custom name
    habitat_sim.register_move_fn(MoveForwardAndSpin,
                                 name="my_custom_name",
                                 body_action=True)

    # We can also re-register this function such that it effects just the sensors
    habitat_sim.register_move_fn(MoveForwardAndSpin,
                                 name="move_forward_and_spin_sensors",
                                 body_action=False)

    # Now we need to add this action to the agent's action space in the configuration!
    agent_config = habitat_sim.AgentConfiguration()
    _pprint(agent_config.action_space)

    # We can add the control function in a bunch of ways
    # Note that the name of the action does not need to match the name the control function
    # was registered under.

    # The habitat_sim.ActionSpec defines an action.  The first arguement is the regsitered name
    # of the control spec, the second is the parameter spec
    agent_config.action_space["fwd_and_spin"] = habitat_sim.ActionSpec(
        "move_forward_and_spin", MoveAndSpinSpec(1.0, 45.0))

    # Add the sensor version
    agent_config.action_space["fwd_and_spin_sensors"] = habitat_sim.ActionSpec(
        "move_forward_and_spin_sensors", MoveAndSpinSpec(1.0, 45.0))

    # Add the same control with different parameters
    agent_config.action_space["fwd_and_spin_double"] = habitat_sim.ActionSpec(
        "move_forward_and_spin", MoveAndSpinSpec(2.0, 90.0))

    # Use the custom name with an integer name for the action
    agent_config.action_space[100] = habitat_sim.ActionSpec(
        "my_custom_name", MoveAndSpinSpec(0.1, 1.0))

    _pprint(agent_config.action_space)

    # Spin up the simulator
    backend_cfg = habitat_sim.SimulatorConfiguration()
    backend_cfg.scene.id = "data/scene_datasets/habitat-test-scenes/van-gogh-room.glb"
    sim = habitat_sim.Simulator(
        habitat_sim.Configuration(backend_cfg, [agent_config]))
    print(sim.get_agent(0).state)

    # Take the new actions!
    sim.step("fwd_and_spin")
    print(sim.get_agent(0).state)

    # Take the new actions!
    sim.step("fwd_and_spin_sensors")
    print(sim.get_agent(0).state)

    sim.step("fwd_and_spin_double")
    print(sim.get_agent(0).state)

    sim.step(100)
    print(sim.get_agent(0).state)

    sim.close()
    del sim

    # Let's define a strafe action!
    @attr.s(auto_attribs=True, slots=True)
    class StrafeActuationSpec:
        forward_amount: float
        # Classic strafing is to move perpendicular (90 deg) to the forward direction
        strafe_angle: float = 90.0

    def _strafe_impl(scene_node: habitat_sim.SceneNode, forward_amount: float,
                     strafe_angle: float):
        forward_ax = (
            np.array(scene_node.absolute_transformation().rotation_scaling())
            @ habitat_sim.geo.FRONT)
        rotation = habitat_sim.utils.quat_from_angle_axis(
            np.deg2rad(strafe_angle), habitat_sim.geo.UP)
        move_ax = habitat_sim.utils.quat_rotate_vector(rotation, forward_ax)

        scene_node.translate_local(move_ax * forward_amount)

    @habitat_sim.register_move_fn(body_action=True)
    class StrafeLeft(habitat_sim.SceneNodeControl):
        def __call__(self, scene_node: habitat_sim.SceneNode,
                     actuation_spec: StrafeActuationSpec):
            _strafe_impl(scene_node, actuation_spec.forward_amount,
                         actuation_spec.strafe_angle)

    @habitat_sim.register_move_fn(body_action=True)
    class StrafeRight(habitat_sim.SceneNodeControl):
        def __call__(self, scene_node: habitat_sim.SceneNode,
                     actuation_spec: StrafeActuationSpec):
            _strafe_impl(scene_node, actuation_spec.forward_amount,
                         -actuation_spec.strafe_angle)

    agent_config = habitat_sim.AgentConfiguration()
    agent_config.action_space["strafe_left"] = habitat_sim.ActionSpec(
        "strafe_left", StrafeActuationSpec(0.25))
    agent_config.action_space["strafe_right"] = habitat_sim.ActionSpec(
        "strafe_right", StrafeActuationSpec(0.25))

    sim = habitat_sim.Simulator(
        habitat_sim.Configuration(backend_cfg, [agent_config]))
    print(sim.get_agent(0).state)

    sim.step("strafe_left")
    print(sim.get_agent(0).state)

    sim.step("strafe_right")
    print(sim.get_agent(0).state)
示例#24
0
def test_velocity_control():
    cfg_settings = examples.settings.default_sim_settings.copy()
    cfg_settings["scene"] = "NONE"
    cfg_settings["enable_physics"] = True
    hab_cfg = examples.settings.make_cfg(cfg_settings)
    with habitat_sim.Simulator(hab_cfg) as sim:
        sim.set_gravity(np.array([0, 0, 0.0]))
        obj_mgr = sim.get_object_template_manager()

        template_path = osp.abspath("data/test_assets/objects/nested_box")
        template_ids = obj_mgr.load_object_configs(template_path)
        object_template = obj_mgr.get_template_by_ID(template_ids[0])
        object_template.linear_damping = 0.0
        object_template.angular_damping = 0.0
        obj_mgr.register_template(object_template)

        obj_handle = obj_mgr.get_template_handle_by_ID(template_ids[0])

        for iteration in range(2):
            sim.reset()
            object_id = sim.add_object_by_handle(obj_handle)
            vel_control = sim.get_object_velocity_control(object_id)

            if iteration == 0:
                if (sim.get_object_motion_type(object_id) !=
                        habitat_sim.physics.MotionType.DYNAMIC):
                    # Non-dynamic simulator in use. Skip 1st pass.
                    sim.remove_object(object_id)
                    continue
            elif iteration == 1:
                # test KINEMATIC
                sim.set_object_motion_type(
                    habitat_sim.physics.MotionType.KINEMATIC, object_id)

            # test global velocities
            vel_control.linear_velocity = np.array([1.0, 0, 0])
            vel_control.angular_velocity = np.array([0, 1.0, 0])
            vel_control.controlling_lin_vel = True
            vel_control.controlling_ang_vel = True

            while sim.get_world_time() < 1.0:
                # NOTE: stepping close to default timestep to get near-constant velocity control of DYNAMIC bodies.
                sim.step_physics(0.00416)

            ground_truth_pos = sim.get_world_time(
            ) * vel_control.linear_velocity
            assert np.allclose(sim.get_translation(object_id),
                               ground_truth_pos,
                               atol=0.01)
            ground_truth_q = mn.Quaternion([[0, 0.480551, 0], 0.876967])
            angle_error = mn.math.angle(ground_truth_q,
                                        sim.get_rotation(object_id))
            assert angle_error < mn.Rad(0.005)

            sim.reset()

            # test local velocities (turn in a half circle)
            vel_control.lin_vel_is_local = True
            vel_control.ang_vel_is_local = True
            vel_control.linear_velocity = np.array([0, 0, -math.pi])
            vel_control.angular_velocity = np.array([math.pi * 2.0, 0, 0])

            sim.set_translation(np.array([0, 0, 0.0]), object_id)
            sim.set_rotation(mn.Quaternion(), object_id)

            while sim.get_world_time() < 0.5:
                # NOTE: stepping close to default timestep to get near-constant velocity control of DYNAMIC bodies.
                sim.step_physics(0.008)

            print(sim.get_world_time())

            # NOTE: explicit integration, so expect some error
            ground_truth_q = mn.Quaternion([[1.0, 0, 0], 0])
            print(sim.get_translation(object_id))
            assert np.allclose(sim.get_translation(object_id),
                               np.array([0, 1.0, 0.0]),
                               atol=0.07)
            angle_error = mn.math.angle(ground_truth_q,
                                        sim.get_rotation(object_id))
            assert angle_error < mn.Rad(0.05)

            sim.remove_object(object_id)
示例#25
0
    def __init__(
        self,
        scene_filepath: Union[str, List[str]],
        labels: List[float] = None,
        img_size: tuple = (512, 512),
        output: List[str] = None,
        pose_extractor_name: str = "closest_point_extractor",
        sim=None,
        shuffle: bool = True,
        split: tuple = (70, 30),
        use_caching: bool = True,
        meters_per_pixel: float = 0.1,
    ):
        if labels is None:
            labels = [0.0]
        if output is None:
            output = ["rgba"]
        if sum(split) != 100:
            raise Exception("Train/test split must sum to 100.")

        self.scene_filepaths = None
        self.cur_fp = None
        if isinstance(scene_filepath, list):
            self.scene_filepaths = scene_filepath
        else:
            self.scene_filepaths = [scene_filepath]
            self.cur_fp = scene_filepath

        self.labels = set(labels)
        self.img_size = img_size
        self.cfg = self._config_sim(self.scene_filepaths[0], self.img_size)

        sim_provided = sim is not None
        if not sim_provided:
            sim = habitat_sim.Simulator(self.cfg)
        else:
            # If a sim is provided we have to make a new cfg
            self.cfg = self._config_sim(sim.config.sim_cfg.scene_id, img_size)
            sim.reconfigure(self.cfg)

        self.sim = sim
        self.meters_per_pixel = meters_per_pixel
        if not sim_provided:
            self.tdv_fp_ref_triples = self._preprocessing(
                self.sim, self.scene_filepaths, self.meters_per_pixel)
        else:
            ref_point = self._get_pathfinder_reference_point(
                self.sim.pathfinder)
            self.tdv_fp_ref_triples = [(
                TopdownView(self.sim, ref_point[1], meters_per_pixel),
                self.sim.config.sim_cfg.scene_id,
                ref_point,
            )]

        args = (self.tdv_fp_ref_triples, self.meters_per_pixel)
        self.pose_extractor = make_pose_extractor(pose_extractor_name)(*args)
        self.poses = self.pose_extractor.extract_all_poses()

        if shuffle:
            np.random.shuffle(self.poses)

        self.train, self.test = self._handle_split(split, self.poses)
        self.mode = "full"
        self.mode_to_data = {
            "full": self.poses,
            "train": self.train,
            "test": self.test,
            None: self.poses,
        }
        self.instance_id_to_name = self._generate_label_map(
            self.sim.semantic_scene)
        self.out_name_to_sensor_name = {
            "rgba": "color_sensor",
            "depth": "depth_sensor",
            "semantic": "semantic_sensor",
        }
        self.output = output
        self.use_caching = use_caching
        if self.use_caching:
            self.cache = ExtractorLRUCache()
示例#26
0
def test_kinematics():
    cfg_settings = examples.settings.default_sim_settings.copy()

    cfg_settings[
        "scene"] = "data/scene_datasets/habitat-test-scenes/skokloster-castle.glb"
    # enable the physics simulator: also clears available actions to no-op
    cfg_settings["enable_physics"] = True
    cfg_settings["depth_sensor"] = True

    # test loading the physical scene
    hab_cfg = examples.settings.make_cfg(cfg_settings)
    with habitat_sim.Simulator(hab_cfg) as sim:
        obj_mgr = sim.get_object_template_manager()

        assert obj_mgr.get_num_templates() > 0

        # test adding an object to the world
        # get handle for object 0, used to test
        obj_handle_list = obj_mgr.get_template_handles("cheezit")
        object_id = sim.add_object_by_handle(obj_handle_list[0])
        assert len(sim.get_existing_object_ids()) > 0

        # test setting the motion type

        assert sim.set_object_motion_type(
            habitat_sim.physics.MotionType.STATIC, object_id)
        assert (sim.get_object_motion_type(object_id) ==
                habitat_sim.physics.MotionType.STATIC)
        assert sim.set_object_motion_type(
            habitat_sim.physics.MotionType.KINEMATIC, object_id)
        assert (sim.get_object_motion_type(object_id) ==
                habitat_sim.physics.MotionType.KINEMATIC)

        # test kinematics
        I = np.identity(4)

        # test get and set translation
        sim.set_translation(np.array([0, 1.0, 0]), object_id)
        assert np.allclose(sim.get_translation(object_id),
                           np.array([0, 1.0, 0]))

        # test object SceneNode
        object_node = sim.get_object_scene_node(object_id)
        assert np.allclose(sim.get_translation(object_id),
                           object_node.translation)

        # test get and set transform
        sim.set_transformation(I, object_id)
        assert np.allclose(sim.get_transformation(object_id), I)

        # test get and set rotation
        Q = quat_from_angle_axis(np.pi, np.array([0, 1.0, 0]))
        expected = np.eye(4)
        expected[0:3, 0:3] = quaternion.as_rotation_matrix(Q)
        sim.set_rotation(quat_to_magnum(Q), object_id)
        assert np.allclose(sim.get_transformation(object_id), expected)
        assert np.allclose(quat_from_magnum(sim.get_rotation(object_id)), Q)

        # test object removal
        sim.remove_object(object_id)
        assert len(sim.get_existing_object_ids()) == 0

        obj_handle_list = obj_mgr.get_template_handles("cheezit")
        object_id = sim.add_object_by_handle(obj_handle_list[0])

        prev_time = 0.0
        for _ in range(2):
            # do some kinematics here (todo: translating or rotating instead of absolute)
            sim.set_translation(np.random.rand(3), object_id)
            T = sim.get_transformation(object_id)  # noqa : F841

            # test getting observation
            sim.step(random.choice(list(
                hab_cfg.agents[0].action_space.keys())))

            # check that time is increasing in the world
            assert sim.get_world_time() > prev_time
            prev_time = sim.get_world_time()

        sim.remove_object(object_id)

        # test attaching/dettaching an Agent to/from physics simulation
        agent_node = sim.agents[0].scene_node
        obj_handle_list = obj_mgr.get_template_handles("cheezit")
        object_id = sim.add_object_by_handle(obj_handle_list[0], agent_node)
        sim.set_translation(np.random.rand(3), object_id)
        assert np.allclose(agent_node.translation,
                           sim.get_translation(object_id))
        sim.remove_object(object_id, False)  # don't delete the agent's node
        assert agent_node.translation

        # test get/set RigidState
        object_id = sim.add_object_by_handle(obj_handle_list[0])
        targetRigidState = habitat_sim.bindings.RigidState(
            mn.Quaternion(), np.array([1.0, 2.0, 3.0]))
        sim.set_rigid_state(targetRigidState, object_id)
        objectRigidState = sim.get_rigid_state(object_id)
        assert np.allclose(objectRigidState.translation,
                           targetRigidState.translation)
        assert objectRigidState.rotation == targetRigidState.rotation
示例#27
0
def runVHACDSimulation(obj_path):
    # data will store data from simulation
    data = {
        "observations": [],
    }

    cfg = make_configuration()
    with habitat_sim.Simulator(cfg) as sim:
        sim.initialize_agent(
            0,
            habitat_sim.AgentState(
                [5.45, 2.4, 1.2],
                np.quaternion(1, 0, 0, 0),
            ),
        )

        # get the physics object attributes manager
        obj_templates_mgr = sim.get_object_template_manager()

        # create a list that will store the object ids
        obj_ids = []

        # get object template & render asset handle
        obj_id_1 = obj_templates_mgr.load_configs(
            str(os.path.join(data_path, obj_path)))[0]
        obj_template = obj_templates_mgr.get_template_by_ID(obj_id_1)
        obj_handle = obj_template.render_asset_handle
        obj_templates_mgr.register_template(obj_template,
                                            force_registration=True)
        obj_ids += [obj_id_1]

        # == VHACD ==
        # Now, create a new object template based on the recently created obj_template
        # specify parameters for running CHD

        # high resolution
        params = habitat_sim._ext.habitat_sim_bindings.VHACDParameters()
        params.resolution = 200000
        params.max_convex_hulls = 32

        # run VHACD, with params passed in and render
        new_handle_1 = sim.apply_convex_hull_decomposition(
            obj_handle, params, True, True)
        new_obj_template_1 = obj_templates_mgr.get_template_by_handle(
            new_handle_1)

        obj_templates_mgr.register_template(new_obj_template_1,
                                            force_registration=True)
        obj_ids += [new_obj_template_1.ID]

        # Medium resolution
        params = habitat_sim.VHACDParameters()
        params.resolution = 100000
        params.max_convex_hulls = 4

        # run VHACD, with params passed in and render
        new_handle_2 = sim.apply_convex_hull_decomposition(
            obj_handle, params, True, True)
        new_obj_template_2 = obj_templates_mgr.get_template_by_handle(
            new_handle_2)

        obj_templates_mgr.register_template(new_obj_template_2,
                                            force_registration=True)
        obj_ids += [new_obj_template_2.ID]

        # Low resolution
        params = habitat_sim.VHACDParameters()
        params.resolution = 10000
        params.max_convex_hulls = 1

        # run VHACD, with params passed in and render
        new_handle_3 = sim.apply_convex_hull_decomposition(
            obj_handle, params, True, True)
        new_obj_template_3 = obj_templates_mgr.get_template_by_handle(
            new_handle_3)

        obj_templates_mgr.register_template(new_obj_template_3,
                                            force_registration=True)
        obj_ids += [new_obj_template_3.ID]

        # now display objects
        cur_ids = []
        for i in range(len(obj_ids)):
            cur_id = sim.add_object(obj_ids[i])
            cur_ids.append(cur_id)
            # get length
            obj_node = sim.get_object_scene_node(cur_id)
            obj_bb = obj_node.cumulative_bb
            length = obj_bb.size().length() * 0.8

            total_length = length * len(obj_ids)
            dist = math.sqrt(3) * total_length / 2

            set_object_state_from_agent(
                sim,
                cur_id,
                offset=np.array([
                    -total_length / 2 + total_length * i / (len(obj_ids) - 1),
                    1.4,
                    -1 * dist,
                ]),
            )
            sim.set_object_motion_type(
                habitat_sim.physics.MotionType.KINEMATIC, cur_id)
            vel_control = sim.get_object_velocity_control(cur_id)
            vel_control.controlling_ang_vel = True
            vel_control.angular_velocity = np.array([0, -1.56, 0])

        # simulate for 4 seconds
        simulate(sim, dt=4, get_frames=True, data=data)

        for cur_id in cur_ids:
            vel_control = sim.get_object_velocity_control(cur_id)
            vel_control.controlling_ang_vel = True
            vel_control.angular_velocity = np.array([-1.56, 0, 0])

        # simulate for 4 seconds
        simulate(sim, dt=4, get_frames=True, data=data)
    return data
示例#28
0
def test_get_no_light_setup(make_cfg_settings):
    with habitat_sim.Simulator(make_cfg(make_cfg_settings)) as sim:
        assert len(sim.get_light_setup(NO_LIGHT_KEY)) == 0
    args, _ = parser.parse_known_args()
    show_video = args.show_video
    make_video = args.make_video
    if make_video and not os.path.exists(output_path):
        os.mkdir(output_path)

    # %%
    # [initialize]
    # create the simulators AND resets the simulator

    cfg = make_configuration()
    try:  # Got to make initialization idiot proof
        sim.close()
    except NameError:
        pass
    sim = habitat_sim.Simulator(cfg)
    agent_transform = place_agent(sim)

    # get the primitive assets attributes manager
    prim_templates_mgr = sim.get_asset_template_manager()

    # get the physics object attributes manager
    obj_templates_mgr = sim.get_object_template_manager()
    # [/initialize]

    # %%
    # [basics]

    # load some object templates from configuration files
    sphere_template_id = obj_templates_mgr.load_configs(
        str(os.path.join(data_path, "test_assets/objects/sphere")))[0]
示例#30
0
def box_drop_test(
    args,
    objects,
    num_objects=30,
    box_size=2,
    object_speed=2,
    post_throw_settling_time=5,
    useVHACD=False,
    VHACDParams=def_params,
):  # take in parameters here
    """Drops a specified number of objects into a box and returns metrics including the time to simulate each frame, render each frame, and the number of collisions each frame."""

    data = {
        "observations": [],
        "physics_step_times": [],
        "graphics_render_times": [],
        "collisions": [],
    }

    cfg = make_configuration()
    with habitat_sim.Simulator(cfg) as sim:
        sim.initialize_agent(
            0,
            habitat_sim.AgentState(
                [5.45 * box_size, 2.4 * box_size, 1.2 * box_size],
                np.quaternion(-0.8000822, 0.1924500, -0.5345973, -0.1924500),
            ),
        )

        # get the physics object attributes manager
        obj_templates_mgr = sim.get_object_template_manager()

        # build box
        cube_handle = obj_templates_mgr.get_template_handles("cube")[0]
        box_parts = []
        boxIDs = []
        for _i in range(5):
            box_parts += [
                obj_templates_mgr.get_template_by_handle(cube_handle)
            ]
        box_parts[0].scale = np.array([1.0, 0.1, 1.0]) * box_size
        box_parts[1].scale = np.array([1.0, 0.6, 0.1]) * box_size
        box_parts[2].scale = np.array([1.0, 0.6, 0.1]) * box_size
        box_parts[3].scale = np.array([0.1, 0.6, 1.0]) * box_size
        box_parts[4].scale = np.array([0.1, 0.6, 1.0]) * box_size

        for i in range(5):
            part_name = "box_part_" + str(i)
            obj_templates_mgr.register_template(box_parts[i], part_name)
            boxIDs += [sim.add_object_by_handle(part_name)]
            sim.set_object_motion_type(
                habitat_sim.physics.MotionType.KINEMATIC, boxIDs[i])
        sim.set_translation(np.array([2.50, 0.05, 0.4]) * box_size, boxIDs[0])
        sim.set_translation(np.array([2.50, 0.35, 1.30]) * box_size, boxIDs[1])
        sim.set_translation(
            np.array([2.50, 0.35, -0.50]) * box_size, boxIDs[2])
        sim.set_translation(np.array([3.40, 0.35, 0.4]) * box_size, boxIDs[3])
        sim.set_translation(np.array([1.60, 0.35, 0.4]) * box_size, boxIDs[4])
        for i in range(5):
            sim.set_object_motion_type(habitat_sim.physics.MotionType.STATIC,
                                       boxIDs[i])

        # load some object templates from configuration files
        object_ids = []
        for obj in objects:
            obj_path = obj["path"]
            object_ids += [
                obj_templates_mgr.load_configs(
                    str(os.path.join(data_path, obj_path)))[0]
            ]
            obj_template = obj_templates_mgr.get_template_by_ID(object_ids[-1])

            if "scale" in obj:
                obj_template.scale *= obj["scale"]
            obj_handle = obj_template.render_asset_handle

            if useVHACD:
                new_handle = sim.apply_convex_hull_decomposition(
                    obj_handle, VHACDParams, True, False)

                new_obj_template = obj_templates_mgr.get_template_by_handle(
                    new_handle)

                if "scale" in obj:
                    new_obj_template.scale *= obj["scale"]

                obj_templates_mgr.register_template(new_obj_template,
                                                    force_registration=True)
                object_ids[-1] = new_obj_template.ID
                print("Template Registered")
            else:
                obj_templates_mgr.register_template(obj_template,
                                                    force_registration=True)

        # throw objects into box
        for i in range(num_objects):
            cur_id = sim.add_object(object_ids[i % len(object_ids)])

            obj_node = sim.get_object_scene_node(cur_id)
            obj_bb = obj_node.cumulative_bb
            diagonal_length = obj_bb.size().length()
            time_til_next_obj = diagonal_length / (object_speed * box_size) / 2

            # set object position and velocity
            sim.set_translation(
                np.multiply(np.array([1.50, 2, 1.2]), box_size), cur_id)
            sim.set_linear_velocity(
                mn.Vector3(1, 0, -1).normalized() * object_speed * box_size,
                cur_id)

            # simulate a short amount of time, then add next object
            simulate(sim,
                     dt=time_til_next_obj,
                     get_frames=args.make_video,
                     data=data)

        simulate(sim,
                 dt=post_throw_settling_time,
                 get_frames=args.make_video,
                 data=data)

        # [/basics]
        # return total time to run, time to load, time to simulate physics, time for rendering
        remove_all_objects(sim)
    return data