( torch.ones( 1, faces_gt.shape[1], 2, 1, dtype=torch.float32, device=device), torch.zeros( 1, faces_gt.shape[1], 2, 1, dtype=torch.float32, device=device), torch.zeros( 1, faces_gt.shape[1], 2, 1, dtype=torch.float32, device=device), ), dim=-1, ) # Initialize the renderer. renderer = SoftRenderer(camera_mode="look_at", device=device) camera_distance = 8.0 elevation = 0.0 azimuth = 0.0 renderer.set_eye_from_angles(camera_distance, elevation, azimuth) pendulum_gt = SimplePendulum(args.mass, args.radius, args.gravity, args.length, args.damping).to(device) theta_init = torch.tensor([0.0, 3.0], device=device) times = torch.arange( args.starttime, args.starttime + args.simsteps * args.dtime, args.dtime, device=device, ) numsteps = times.numel()
# # writer = imageio.get_writer("cache/a.gif", mode="I") # for i in trange(60): # img = seqs[0, i] # # writer.append_data(img.astype(np.uint8)) # imgs_gt.append(img.astype(np.uint8)) # # writer.close() # Initialize the renderer image_size = 256 camera_mode = "look_at" camera_distance = 8.0 elevation = 30.0 azimuth = 0.0 # Initialize the renderer. renderer = SoftRenderer(image_size=image_size, camera_mode=camera_mode, device=device) renderer.set_eye_from_angles(camera_distance, elevation, azimuth) sim_duration = 2.0 # seconds fps = 30 # frames per second sim_steps = int((sim_duration * fps) / 2) # obj = get_primitive_obj(shape[0]) obj = get_primitive_obj(INT_TO_PRIMITIVE[shape[0]]) # print(obj) mesh = TriangleMesh.from_obj(obj) vertices = (( meshutils.normalize_vertices(mesh.vertices) # + \ # torch.from_numpy(init_pos[i]).float().unsqueeze(0) ).to(device).unsqueeze(0))
model.contact_ke = 1.0e4 model.contact_kd = 1000.0 model.contact_kf = 1000.0 model.contact_mu = 0.5 model.particle_radius = 0.01 model.ground = False target = torch.tensor((3.5, 0.0, 0.0)) initial_velocity = torch.tensor((1.0, 0.0, 0.0), requires_grad=True) integrator = df.sim.SemiImplicitIntegrator() # Setup SoftRasterizer device = "cuda:0" renderer = SoftRenderer(camera_mode="look_at", device=device) camera_distance = 15.0 elevation = 0.0 azimuth = 0.0 renderer.set_eye_from_angles(camera_distance, elevation, azimuth) faces = model.tri_indices textures = torch.cat( ( torch.zeros(1, faces.shape[-2], 2, 1, dtype=torch.float32, device=device), torch.ones(1, faces.shape[-2], 2, 1, dtype=torch.float32, device=device), torch.zeros(1, faces.shape[-2], 2, 1, dtype=torch.float32, device=device), ), dim=-1, )
# copy_scripts( # os.path.abspath( # os.path.join(os.path.dirname(os.path.abspath(__file__)), "../gradsim") # ), # os.path.join(args.out_dir, "gradsim"), # ) shutil.copy(os.path.abspath(__file__), args.out_dir) os.makedirs(os.path.join(args.out_dir, "obj"), exist_ok=True) for obj in glob.glob(os.path.join(args.data_dir, "*.obj")): shutil.copy(obj, os.path.join(args.out_dir, "obj")) with open(os.path.join(args.out_dir, "args.yaml"), "w") as f: yaml.dump(vars(args), f, default_flow_style=False) # Initialize the renderer. renderer = SoftRenderer(image_size=args.image_size, camera_mode=args.camera_mode, device=device) renderer.set_eye_from_angles(args.camera_distance, args.elevation, args.azimuth) # Initialize sampler prim_gen = PrimitiveGenerator(mass_scaling=args.mass_scaling, mass_offset=args.mass_offset, data_type=args.data_type, data_dir=args.data_dir) objects = sorted(glob.glob(os.path.join(args.data_dir, "*.obj"))) pos, ornt, linvel, angvel = None, None, None, None try:
mesh=mesh, scale=scale_gt, density=density_gt, ke=ke_gt, kd=kd_gt, kf=kf_gt, mu=mu_gt, ) model_gt = builder_gt.finalize("cpu") model_gt.ground = True integrator = df.sim.SemiImplicitIntegrator() # Setup SoftRasterizer device = "cuda:0" renderer = SoftRenderer(camera_mode="look_at", device=device) camera_distance = 10.0 elevation = 30.0 azimuth = 0.0 renderer.set_eye_from_angles(camera_distance, elevation, azimuth) render_every = 60 * 4 vertices = torch.from_numpy(np.array(points).astype(np.float32)) faces = torch.from_numpy(np.asarray(indices).reshape((-1, 3))) textures = torch.cat( ( torch.ones( 1, faces.shape[-2], 2, 1, dtype=torch.float32, device=device), torch.zeros( 1, faces.shape[-2], 2, 1, dtype=torch.float32, 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,) # Initialize the renderer. renderer = SoftRenderer(camera_mode="look_at", device=device) camera_distance = 10.0 elevation = 30.0 azimuth = 0.0 renderer.set_eye_from_angles(camera_distance, elevation, azimuth) # Run the simulation. writer = imageio.get_writer(outfile, mode="I") for i in trange(sim_steps): sim.step() # print("Body is at:", body.position) if i % render_every == 0: rgba = renderer.forward( body.get_world_vertices().unsqueeze(0), faces, textures ) img = rgba[0].permute(1, 2, 0).detach().cpu().numpy()
if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--iters", type=int, default=2000, help="Number of iterations to run optimization for.", ) parser.add_argument( "--no-viz", action="store_true", help="Skip visualization steps." ) args = parser.parse_args() # Initialize the soft rasterizer. renderer = SoftRenderer(camera_mode="look_at", device="cuda:0") # Camera settings. camera_distance = ( 2.0 # Distance of the camera from the origin (i.e., center of the object) ) elevation = 30.0 # Angle of elevation azimuth = 0.0 # Azimuth angle # Directory in which sample data is located. DATA_DIR = Path(__file__).parent / "sampledata" # Read in the input mesh. TODO: Add filepath as argument. mesh = TriangleMesh.from_obj(DATA_DIR / "sphere.obj") # Output filename to write out a rendered .gif to, showing the progress of optimization.
# Seed RNG for repeatability torch.manual_seed(args.seed) device = "cuda:0" # # Hyperparams # dtime = 0.1 # 1 / 30 # simsteps = 50 # seed = 123 # numepochs = 100 # compare_every = 10 # log = True # logdir = Path("cache/bounce2d") # Initialize the differentiable renderer. renderer = SoftRenderer(camera_mode="look_at", device=device) camera_distance = 15.0 elevation = 0.0 azimuth = 0.0 renderer.set_eye_from_angles(camera_distance, elevation, azimuth) # GT parameters position_initial_gt = torch.tensor([-7.5, 0.0], device=device) radius_gt = 0.75 speed_gt = 1.0 height_gt = 5.0 gravity_gt = -9.0 ball2d_gt = BouncingBall2D( pos=position_initial_gt, radius=radius_gt,