def _build_intermediate_structure(self) -> None: # append scales and colors and names for (record, data) in self.middle_objects: self.model_names.append(record.name) self.scales.append(copy.deepcopy(data['scale'])) self.colors = np.concatenate( [self.colors, np.array(data['color']).reshape((1, 3))], axis=0) # aim camera camera_y_aim = max(0.5, self.ramp_base_height) self.camera_aim = arr_to_xyz([0., camera_y_aim, 0.]) return []
def _build_intermediate_structure(self) -> List[dict]: print("middle color", self.middle_color) if self.randomize_colors_across_trials: self.middle_color = self.random_color( exclude=self.target_color) if self.monochrome else None self.cap_color = self.target_color commands = [] commands.extend(self._build_stack()) commands.extend(self._add_cap()) # set camera params camera_y_aim = 0.5 * self.tower_height self.camera_aim = arr_to_xyz([0., camera_y_aim, 0.]) return commands
def _build_intermediate_structure(self) -> List[dict]: if self.randomize_colors_across_trials: self.middle_color = self.random_color(exclude=self.target_color) if self.monochrome else None commands = [] # Build a stand for the linker object commands.extend(self._build_base(height=self.tower_height, as_cap=False)) # Add the attacment object (i.e. what the links will be partly attached to) commands.extend(self._place_attachment()) # Add the links commands.extend(self._add_links()) # # set camera params camera_y_aim = 0.5 * self.tower_height self.camera_aim = arr_to_xyz([0.,camera_y_aim,0.]) return commands
def _build_intermediate_structure(self) -> List[dict]: ramp_pos = TDWUtils.VECTOR3_ZERO ramp_rot = TDWUtils.VECTOR3_ZERO self.middle = random.choice(self._middle_types) self.middle_type = self.middle.name self.middle_id = self._get_next_object_id() self.middle_scale = get_random_xyz_transform(self.middle_scale_range) commands = self.add_ramp(record=self.middle, position=ramp_pos, rotation=ramp_rot, scale=self.middle_scale, o_id=self.middle_id, add_data=True) # give the ramp a texture and color commands.extend( self.get_object_material_commands( self.middle, self.middle_id, self.get_material_name(self.middle_material))) rgb = self.middle_color or self.random_color(exclude=self.target_color) commands.append({ "$type": "set_color", "color": { "r": rgb[0], "g": rgb[1], "b": rgb[2], "a": 1. }, "id": self.middle_id }) self.colors = np.concatenate( [self.colors, np.array(rgb).reshape((1, 3))], axis=0) camera_y_aim = self.ramp_base_height self.camera_aim = arr_to_xyz([0., camera_y_aim, 0.]) return commands
def _place_and_push_target_object(self) -> List[dict]: """ Place a probe object at the other end of the collision axis, then apply a force to push it. Using probe mass and location here to allow for ramps. There is no dedicated probe in this controller. """ # create a target object record, data = self.random_primitive(self._target_types, scale=self.target_scale_range, color=self.target_color, add_data=False) # add_data=(not self.remove_target) o_id, scale, rgb = [data[k] for k in ["id", "scale", "color"]] self.target = record self.target_type = data["name"] self.target_color = rgb self.target_scale = self.middle_scale = scale self.target_id = o_id # Where to put the target if self.target_rotation is None: self.target_rotation = self.get_rotation(self.target_rotation_range) # Add the object with random physics values commands = [] ### TODO: better sampling of random physics values self.probe_mass = random.uniform(self.probe_mass_range[0], self.probe_mass_range[1]) self.probe_initial_position = {"x": -0.5*self.collision_axis_length, "y": self.target_lift, "z": 0.} rot = self.get_rotation(self.target_rotation_range) print(rot) if self.use_ramp: commands.extend(self._place_ramp_under_probe()) self.probe_initial_position['x'] += self.target_lift #HACK rotation might've led to the object falling of the back of the ramp, so we're moving it forward # commands.extend( # self.add_physics_object( # record=record, # position=self.probe_initial_position, # rotation=rot, # mass=self.probe_mass, # # dynamic_friction=0.5, # # static_friction=0.5, # # bounciness=0.1, # dynamic_friction=0.4, # static_friction=0.4, # bounciness=0, # o_id=o_id)) commands.extend( self.add_primitive( record=record, position=self.probe_initial_position, rotation=rot, mass=self.probe_mass, scale_mass=False, material=self.target_material, color=rgb, scale=scale, # dynamic_friction=0.5, # static_friction=0.5, # bounciness=0.1, dynamic_friction=0.4, static_friction=0.4, bounciness=0, o_id=o_id, add_data=True )) # Set the target material # commands.extend( # self.get_object_material_commands( # record, o_id, self.get_material_name(self.target_material))) # the target is the probe self.target_position = self.probe_initial_position # Scale the object and set its color. # commands.extend([ # {"$type": "set_color", # "color": {"r": rgb[0], "g": rgb[1], "b": rgb[2], "a": 1.}, # "id": o_id}, # {"$type": "scale_object", # "scale_factor": scale, # "id": o_id}]) # Set its collision mode commands.extend([ # {"$type": "set_object_collision_detection_mode", # "mode": "continuous_speculative", # "id": o_id}, {"$type": "set_object_drag", "id": o_id, "drag": 0, "angular_drag": 0}]) # Apply a force to the target object self.push_force = self.get_push_force( scale_range=self.probe_mass * np.array(self.force_scale_range), angle_range=self.force_angle_range, yforce=self.fupforce) self.push_force = self.rotate_vector_parallel_to_floor( self.push_force, -rot['y'], degrees=True) self.push_position = self.probe_initial_position if self.use_ramp: self.push_cmd = { "$type": "apply_force_to_object", "force": self.push_force, "id": int(o_id) } else: self.push_position = { k:v+self.force_offset[k]*self.rotate_vector_parallel_to_floor( self.target_scale, rot['y'])[k] for k,v in self.push_position.items()} self.push_position = { k:v+random.uniform(-self.force_offset_jitter, self.force_offset_jitter) for k,v in self.push_position.items()} self.push_cmd = { "$type": "apply_force_at_position", "force": self.push_force, "position": self.push_position, "id": int(o_id) } # decide when to apply the force self.force_wait = int(random.uniform(*get_range(self.force_wait_range))) print("force wait", self.force_wait) if self.force_wait == 0: commands.append(self.push_cmd) return commands # def _place_target_object(self) -> List[dict]: """ Place a primitive object at one end of the collision axis. """ # create a target object record, data = self.random_primitive(self._target_types, scale=self.target_scale_range, color=self.target_color, add_data=(not self.remove_target) ) o_id, scale, rgb = [data[k] for k in ["id", "scale", "color"]] self.target = record self.target_type = data["name"] self.target_color = rgb self.target_scale = self.middle_scale = scale self.target_id = o_id if any((s <= 0 for s in scale.values())): self.remove_target = True # Where to put the target if self.target_rotation is None: self.target_rotation = self.get_rotation(self.target_rotation_range) if self.target_position is None: self.target_position = { "x": 0.5 * self.collision_axis_length, "y": 0. if not self.remove_target else 10.0, "z": 0. if not self.remove_target else 10.0 } # Commands for adding hte object commands = [] commands.extend( self.add_physics_object( record=record, position=self.target_position, rotation=self.target_rotation, mass=2.0, dynamic_friction=0.5, static_friction=0.5, bounciness=0.0, o_id=o_id, add_data=(not self.remove_target) )) # Set the object material commands.extend( self.get_object_material_commands( record, o_id, self.get_material_name(self.target_material))) # Scale the object and set its color. commands.extend([ {"$type": "set_color", "color": {"r": rgb[0], "g": rgb[1], "b": rgb[2], "a": 1.}, "id": o_id}, {"$type": "scale_object", "scale_factor": scale if not self.remove_target else TDWUtils.VECTOR3_ZERO, "id": o_id}]) # If this scene won't have a target if self.remove_target: commands.append( {"$type": self._get_destroy_object_command_name(o_id), "id": int(o_id)}) self.object_ids = self.object_ids[:-1] return commands cmds = [] # ramp params self.ramp = random.choice(self.DEFAULT_RAMPS) rgb = self.ramp_color or np.array([0.75,0.75,1.0]) ramp_pos = copy.deepcopy(self.probe_initial_position) ramp_pos['y'] += self.zone_scale['y'] if not self.remove_zone else 0.0 # don't intersect w zone ramp_rot = self.get_y_rotation([180,180]) ramp_id = self._get_next_object_id() # figure out scale r_len, r_height, r_dep = self.get_record_dimensions(self.ramp) scale_x = (0.75 * self.collision_axis_length) / r_len if self.ramp_scale is None: self.ramp_scale = arr_to_xyz([scale_x, self.scale_to(r_height, 1.5), 0.75 * scale_x]) # optionally add base self.ramp_base_height = random.uniform(*get_range(self.ramp_base_height_range)) if self.ramp_base_height > 0.01: self.ramp_base = self.CUBE self.ramp_base_scale = arr_to_xyz([ float(scale_x * r_len), float(self.ramp_base_height), float(0.75 * scale_x * r_dep)]) self.ramp_base_id = self._get_next_object_id() cmds.extend( self.add_physics_object( record=self.ramp_base, position=copy.deepcopy(ramp_pos), rotation=TDWUtils.VECTOR3_ZERO, mass=500, dynamic_friction=0.01, static_friction=0.01, bounciness=0.0, o_id=self.ramp_base_id, add_data=True)) _,rb_height,_ = self.get_record_dimensions(self.ramp_base) ramp_pos['y'] += self.ramp_base_scale['y'] # scale it, color it, fix it cmds.extend( self.get_object_material_commands( self.ramp_base, self.ramp_base_id, self.get_material_name(self.zone_material))) cmds.extend([ {"$type": "scale_object", "scale_factor": self.ramp_base_scale, "id": self.ramp_base_id}, {"$type": "set_color", "color": {"r": rgb[0], "g": rgb[1], "b": rgb[2], "a": 1.}, "id": self.ramp_base_id}, {"$type": "set_object_collision_detection_mode", "mode": "continuous_speculative", "id": self.ramp_base_id}, {"$type": "set_kinematic_state", "id": self.ramp_base_id, "is_kinematic": True, "use_gravity": True}]) cmds.extend( self.add_ramp( record = self.ramp, position=ramp_pos, rotation=ramp_rot, scale=self.ramp_scale, o_id=ramp_id, add_data=True)) # give the ramp a texture and color cmds.extend( self.get_object_material_commands( self.ramp, ramp_id, self.get_material_name(self.zone_material))) cmds.append( {"$type": "set_color", "color": {"r": rgb[0], "g": rgb[1], "b": rgb[2], "a": 1.}, "id": ramp_id}) print("ramp commands") print(cmds) # need to adjust probe height as a result of ramp placement self.probe_initial_position['x'] -= 0.5 * self.ramp_scale['x'] * r_len - 0.15 self.probe_initial_position['y'] = self.ramp_scale['y'] * r_height + self.ramp_base_height return cmds
def final_target_displacement(d): try: disp = arr_to_xyz(get_labels(d, 'target_delta_position')[-1]) return {k: round(float(v), 3) for k, v in disp.items()} except TypeError: return None
def _build_intermediate_structure(self) -> List[dict]: commands = [] print("THIS IS A PIT!") print(self.num_middle_objects) # get the scale of the total pit object scale = get_random_xyz_transform(self.middle_scale_range) self.pit_mass = random.uniform(*get_range(self.middle_mass_range)) # get color and texture self.pit_color = self.middle_color or self.random_color( exclude=self.target_color) self.pit_material = self.middle_material # how wide are the pits? self.pit_widths = [ random.uniform(0.0, self.spacing_jitter) * scale['x'] for _ in range(self.num_middle_objects - 1) ] # make M cubes and scale in x accordingly x_remaining = scale['x'] - self.pit_widths[0] x_filled = 0.0 print("PIT WIDTHS", self.pit_widths) for m in range(self.num_middle_objects): print("x_filled, remaining", x_filled, x_remaining) m_rec = random.choice(self._middle_types) x_scale = random.uniform(0.0, x_remaining) x_len, _, _ = self.get_record_dimensions(m_rec) x_len *= x_scale x_pos = self.ramp_end_x + x_filled + (0.5 * x_len) z_pos = random.uniform(-self.lateral_jitter, self.lateral_jitter) print(m) print("ramp_end", self.ramp_end_x) print("x_len", x_len) print("x_scale", x_scale) print("x_pos", x_pos) print("z_pos", z_pos) m_scale = arr_to_xyz([x_scale, scale['y'], scale['z']]) commands.extend( self.add_primitive(record=m_rec, position=arr_to_xyz([x_pos, 0., z_pos]), rotation=TDWUtils.VECTOR3_ZERO, scale=m_scale, color=self.pit_color, exclude_color=self.target_color, material=self.pit_material, mass=self.pit_mass, dynamic_friction=self.middle_friction, static_friction=self.middle_friction, scale_mass=True, make_kinematic=True, add_data=True, obj_list=self.middle_objects)) if m < len(self.pit_widths): x_filled += self.pit_widths[m] + x_len x_remaining -= (self.pit_widths[m] + x_len) commands.extend(Gravity._build_intermediate_structure(self)) print("INTERMEDIATE") print(commands) return commands