Пример #1
0
def test_constantforce():
    direction = torch.randn(3)
    magnitude = 10.0
    force = ConstantForce(direction, magnitude, starttime=0.5, endtime=1.0)
    assert torch.allclose(force.apply(0.1), torch.zeros(3))
    assert torch.allclose(force.apply(1.1), torch.zeros(3))
    assert torch.allclose(force.apply(0.5), direction * magnitude)
    assert torch.allclose(force.apply(0.9), direction * magnitude)
Пример #2
0
            friction_coefficient=float(fric[0]),
            restitution=float(elas[0]),
            # linear_velocity=torch.tensor(linear_velocity[i], device=device),
            # angular_velocity=torch.tensor(angular_velocity[i], device=device),
        )

        # inds = body_gt.vertices.argmin(1)
        # application_points = list(inds.view(-1).detach().cpu().numpy())
        # print("Est app points:", application_points)
        application_points = [force_application_points[0]]
        # print("True app points:", application_points)
        # Add a force
        force = ConstantForce(
            magnitude=force_magnitude[0],
            direction=torch.from_numpy(force_direction[0]).float().to(device),
            starttime=0.0,
            endtime=0.1,
            device=device,
        )
        body_gt.add_external_force(force,
                                   application_points=application_points)

        # Add gravity
        gravity = ConstantForce(
            magnitude=10.0,
            direction=torch.tensor([0, -1, 0]),
            device=device,
        )
        body_gt.add_external_force(gravity)

        sim_gt = Simulator([body_gt])
Пример #3
0
            # application_points = [
            #     int(v)
            #     for v in torch.randint(
            #         vertices.shape[1], (max(1, torch.randint(vertices.shape[1], (1,))),)
            #     )
            # ]
            inds = vertices.argmin(1)
            application_points = list(inds.view(-1).detach().cpu().numpy())
            application_points = [application_points[1]]

            # Add an impulse
            if impulse_magnitude > 0:
                impulse = ConstantForce(
                    magnitude=impulse_magnitude,
                    direction=torch.from_numpy(impulse_direction),
                    starttime=0.0,
                    endtime=0.1,
                    device=device,
                )
                body.add_external_force(impulse,
                                        application_points=application_points)

            # Add gravity
            if args.gravity:
                gravity = ConstantForce(
                    magnitude=args.gravity_magnitude / len(vertices[0]),
                    direction=torch.tensor([0, -1, 0]),
                    device=device,
                )
                body.add_external_force(gravity)
Пример #4
0
    position = torch.tensor([0.0, 4.0, 0.0], dtype=torch.float32, device=device)
    orientation = torch.tensor([1.0, 0.0, 0.0, 0.0], dtype=torch.float32, device=device)
    body = RigidBody(
        vertices[0],
        position=position,
        orientation=orientation,
        masses=masses,
        restitution=0.5,
    )

    # Create a force that applies gravity (g = 10 metres / second^2).
    # gravity = Gravity(device=device)
    force_magnitude = 10.0  # / vertices.shape[-2]
    gravity = ConstantForce(
        magnitude=force_magnitude,
        direction=torch.tensor([0.0, -1.0, 0.0]),
        device=device,
    )

    # Add this force to the body.
    body.add_external_force(gravity)

    sim_duration = 2.0
    fps = 30
    sim_substeps = 32
    dtime = (1 / 30) / sim_substeps
    sim_steps = int(sim_duration / dtime)
    render_every = sim_substeps

    # Initialize the simulator with the body at the origin.
    sim = Simulator(bodies=[body], engine=SemiImplicitEulerWithContacts(), dtime=dtime,)
Пример #5
0
        [1.0, -1.0, 1.0],
        [1.0, -1.0, -1.0],
        [1.0, 1.0, -1.0],
        [-1.0, 1.0, 1.0],
        [-1.0, -1.0, 1.0],
        [-1.0, -1.0, -1.0],
        [-1.0, 1.0, -1.0],
    ])
    position = torch.tensor([2.0, 2.0, 2.0])
    orientation = torch.tensor([1.0, 0.0, 0.0, 0.0])
    cube = RigidBody(cube_verts + 1,
                     position=position,
                     orientation=orientation)
    force_magnitude = 10.0  # / cube_verts.shape[0]
    force_direction = torch.tensor([0.0, -1.0, 0.0])
    gravity = ConstantForce(magnitude=force_magnitude,
                            direction=force_direction)
    cube.add_external_force(gravity)

    # sim = Simulator([cube], engine=EulerIntegratorWithContacts())

    sim_substeps = 32
    dtime = (1 / 30) / sim_substeps
    sim = Simulator([cube],
                    engine=SemiImplicitEulerWithContacts(),
                    dtime=dtime)

    # import numpy as np
    # from mpl_toolkits.mplot3d import Axes3D
    # import matplotlib.pyplot as plt
    # fig = plt.figure()
    # ax = fig.add_subplot(111, projection='3d')
Пример #6
0
                1, faces.shape[1], 2, 1, dtype=torch.float32, device=device),
            torch.zeros(
                1, faces.shape[1], 2, 1, dtype=torch.float32, device=device),
        ),
        dim=-1,
    )
    masses = torch.nn.Parameter(
        0.1 *
        torch.ones(vertices.shape[1], dtype=vertices.dtype, device=device),
        requires_grad=True,
    )
    body = RigidBody(vertices[0], masses=masses)

    # Create a force that applies gravity (g = 10 metres / second^2).
    # gravity = Gravity(device=device)
    gravity = ConstantForce(direction=torch.tensor([0.0, -1.0, 0.0]),
                            device=device)

    # Add this force to the body.
    body.add_external_force(gravity, application_points=[0, 1])

    # Initialize the simulator with the body at the origin.
    sim = Simulator([body])

    # Initialize the renderer.
    renderer = SoftRenderer(camera_mode="look_at", device=device)
    camera_distance = 8.0
    elevation = 30.0
    azimuth = 0.0
    renderer.set_eye_from_angles(camera_distance, elevation, azimuth)

    # Run the simulation.
Пример #7
0
        # application_points = [force_application_points[0]]
        # # Add a force
        # force = ConstantForce(
        #     magnitude=force_magnitude[0],
        #     direction=torch.from_numpy(force_direction[0]).float().to(device),
        #     starttime=0.0,
        #     endtime=0.1,
        #     device=device,
        # )
        # body_gt.add_external_force(force, application_points=application_points)

        # Add gravity
        gravity = ConstantForce(
            magnitude=10.0,
            direction=torch.tensor([0, -1, 0]),
            device=device,
        )
        body_gt.add_external_force(gravity)

        sim_gt = Simulator([body_gt],
                           dtime=sim_dtime,
                           engine=SemiImplicitEulerWithContacts())

        # 2 seconds; 30 fps
        imgs_gt = []
        with torch.no_grad():
            for t in range(sim_steps):
                sim_gt.step()
                if t % render_every == 0:
                    rgba = renderer.forward(
Пример #8
0
        mass_per_vertex * torch.ones(
            vertices_gt.shape[1], dtype=vertices_gt.dtype, device=device),
        requires_grad=False,
    )
    body_gt = RigidBody(
        vertices_gt[0],
        masses=masses_gt,
        position=position_gt,
        orientation=orientation_gt,
    )

    # Create a force that applies gravity (g = 10 metres / second^2).
    # normalized_magnitude_gt = args.force_magnitude / vertices_gt.shape[-2]
    gravity_gt = ConstantForce(
        direction=torch.tensor([0.0, -1.0, 0.0]),
        magnitude=args.force_magnitude,
        device=device,
    )

    # Add this force to the body.
    inds = vertices_gt.argmin(-2)[0]
    application_points = list(inds.view(-1).detach().cpu().numpy())
    body_gt.add_external_force(gravity_gt,
                               application_points=application_points)

    # Initialize the simulator with the body at the origin.
    sim_gt = Simulator([body_gt])

    # Initialize the renderer.
    renderer = DIBRenderer(128, 128, mode="VertexColor")
    camera_distance = 8.0