def project_to_2d(point_on_plane, plane_center, plane_width, plane_height, plane_quaternion): if not isinstance(point_on_plane, list) and not isinstance(point_on_plane, tuple) \ and not isinstance(point_on_plane, np.ndarray): raise GenericRolloutException("point_on_plane must be a type of list, tuple, or numpy.ndarray") if not isinstance(plane_center, list) and not isinstance(plane_center, tuple) \ and not isinstance(plane_center, np.ndarray): raise GenericRolloutException("plane_center must be a type of list, tuple, or numpy.ndarray") if not isinstance(plane_quaternion, list) and not isinstance(plane_quaternion, tuple) \ and not isinstance(plane_quaternion, np.ndarray): raise GenericRolloutException("plane_quaternion must be a type of list, tuple, or numpy.ndarray") point_on_plane = np.array(point_on_plane) plane_center = np.array(plane_center) plane_quaternion = np.array(plane_quaternion) # Transpose the center back to origin point_on_plane_from_origin = point_on_plane - plane_center # Reverse the rotation so plane can align back to y-axis inverse_cam_quaternion = inverse_quaternion(plane_quaternion) point_on_y_axis = apply_orientation(inverse_cam_quaternion, point_on_plane_from_origin) # Rotate pitch 90 degree and yaw 90 degree, so plane will align to x and y axis # Remember rotation order is roll, pitch, yaw in euler_to_quaternion method project_2d_quaternion = euler_to_quaternion(pitch=np.pi / 2.0, yaw=np.pi / 2.0) point_on_2d_plane = apply_orientation(np.array(project_2d_quaternion), point_on_y_axis) # Align plane to origin at x, y = (0, 0) point_on_2d_plane = point_on_2d_plane + np.array([plane_width / 2.0, plane_height / 2.0, 0.0]) # Re-scale x and y space between 0 and 1 return (point_on_2d_plane[0] / plane_width), (point_on_2d_plane[1] / plane_height)
def get_object_bounding_rect(object_pose, object_dims): """ Returns a list of points (numpy.ndarray) of bounding rectangle on the floor. object_pose - Object pose object. object_dims - Tuple representing the dimension of object (width, height) """ half_width = 0.5 * (object_dims.value[0]) half_length = 0.5 * (object_dims.value[1]) local_verts = np.array([ [+half_length, +half_width, 0.0], [+half_length, -half_width, 0.0], [-half_length, -half_width, 0.0], [-half_length, +half_width, 0.0], ]) object_position = np.array([ object_pose.position.x, object_pose.position.y, object_pose.position.z ]) object_orientation = np.array([ object_pose.orientation.x, object_pose.orientation.y, object_pose.orientation.z, object_pose.orientation.w, ]) return [ object_position + apply_orientation(object_orientation, p) for p in local_verts ]
def update(self, car_pose): """ Update the frustums Args: car_pose (Pose): Gazebo Pose of the agent """ with self.lock: self.frustums = [] self.cam_poses = [] self.near_plane_infos = [] # Retrieve car pose to calculate camera pose. car_pos = np.array([car_pose.position.x, car_pose.position.y, car_pose.position.z]) car_quaternion = [car_pose.orientation.x, car_pose.orientation.y, car_pose.orientation.z, car_pose.orientation.w] for camera_offset in self.camera_offsets: # Get camera position by applying position offset from the car position. cam_pos = car_pos + apply_orientation(car_quaternion, camera_offset) # Get camera rotation by applying car rotation and pitch angle of camera. _, _, yaw = quaternion_to_euler(x=car_quaternion[0], y=car_quaternion[1], z=car_quaternion[2], w=car_quaternion[3]) cam_quaternion = np.array(euler_to_quaternion(pitch=self.camera_pitch, yaw=yaw)) # Calculate frustum with camera position and rotation. planes, cam_pose, near_plane_info = self._calculate_frustum_planes(cam_pos=cam_pos, cam_quaternion=cam_quaternion) self.frustums.append(planes) self.cam_poses.append(cam_pose) self.near_plane_infos.append(near_plane_info)
def _get_agent_pos(self, car_pose, car_link_points, relative_pos): '''Returns a dictionary with the keys defined in AgentPos which contains the position of the agent on the track, the location of the desired links, and the orientation of the agent. car_pose - Gazebo Pose of the agent car_link_points (Point[]) - List of car's links' Points. relative_pos - List containing the x-y relative position of the front of the agent ''' try: # Compute the model's orientation model_orientation = np.array([ car_pose.orientation.x, car_pose.orientation.y, car_pose.orientation.z, car_pose.orientation.w ]) # Compute the model's location relative to the front of the agent model_location = np.array([car_pose.position.x, car_pose.position.y, car_pose.position.z]) + \ apply_orientation(model_orientation, np.array(relative_pos)) model_point = Point(model_location[0], model_location[1]) return { AgentPos.ORIENTATION.value: model_orientation, AgentPos.POINT.value: model_point, AgentPos.LINK_POINTS.value: car_link_points } except Exception as ex: raise GenericRolloutException( "Unable to get position: {}".format(ex))
def _update_temp(self): with self.lock: self.frustums = [] self.cam_poses = [] self.near_plane_infos = [] car_model_state = self.model_state_client('racecar', '') car_pos = np.array([ car_model_state.pose.position.x, car_model_state.pose.position.y, car_model_state.pose.position.z ]) car_quaternion = [ car_model_state.pose.orientation.x, car_model_state.pose.orientation.y, car_model_state.pose.orientation.z, car_model_state.pose.orientation.w ] for camera_offset in self.camera_offsets: cam_pos = car_pos + apply_orientation(car_quaternion, camera_offset) _, _, yaw = quaternion_to_euler(x=car_quaternion[0], y=car_quaternion[1], z=car_quaternion[2], w=car_quaternion[3]) cam_quaternion = np.array( euler_to_quaternion(pitch=self.camera_pitch, yaw=yaw)) planes, cam_pose, near_plane_info = self._calculate_frustum_planes( cam_pos=cam_pos, cam_quaternion=cam_quaternion) self.frustums.append(planes) self.cam_poses.append(cam_pose) self.near_plane_infos.append(near_plane_info)
def _update(self, car_model_state, delta_time): # Calculate target Camera position based on nearest center track from the car. near_dist = self.track_data._center_line_.project( Point(car_model_state.pose.position.x, car_model_state.pose.position.y)) near_pnt_ctr = self.track_data._center_line_.interpolate(near_dist) yaw = self.track_data._center_line_.interpolate_yaw( distance=near_dist, normalized=False, reverse_dir=False, position=near_pnt_ctr) yaw = utils.lerp_angle_rad(self.last_yaw, yaw, delta_time * self.damping) quaternion = np.array( euler_to_quaternion(pitch=self.look_down_angle_rad, yaw=yaw)) target_camera_location = np.array([near_pnt_ctr.x, near_pnt_ctr.y, 0.0]) + \ apply_orientation(quaternion, np.array([-self.cam_dist_offset, 0, 0])) target_camera_location_2d = target_camera_location[0:2] # Linear interpolate Camera position to target position cur_camera_2d_pos = np.array([ self.last_camera_state.pose.position.x, self.last_camera_state.pose.position.y ]) new_cam_pos_2d = utils.lerp(cur_camera_2d_pos, target_camera_location_2d, delta_time * self.damping) # Calculate camera rotation quaternion based on lookAt yaw look_at_yaw = utils.get_angle_between_two_points_2d_rad( self.last_camera_state.pose.position, car_model_state.pose.position) cam_quaternion = euler_to_quaternion(pitch=self.look_down_angle_rad, yaw=look_at_yaw) # Configure Camera Model State camera_model_state = ModelState() camera_model_state.model_name = self.topic_name camera_model_state.pose.position.x = new_cam_pos_2d[0] camera_model_state.pose.position.y = new_cam_pos_2d[1] camera_model_state.pose.position.z = self.cam_fixed_height camera_model_state.pose.orientation.x = cam_quaternion[0] camera_model_state.pose.orientation.y = cam_quaternion[1] camera_model_state.pose.orientation.z = cam_quaternion[2] camera_model_state.pose.orientation.w = cam_quaternion[3] self.model_state_client(camera_model_state) self.last_camera_state = camera_model_state self.last_yaw = yaw
def _update(self, car_model_state, delta_time): # Calculate target Camera position based on car position _, _, car_yaw = quaternion_to_euler( x=car_model_state.pose.orientation.x, y=car_model_state.pose.orientation.y, z=car_model_state.pose.orientation.z, w=car_model_state.pose.orientation.w) # Linear Interpolate Yaw angle car_yaw = utils.lerp_angle_rad(self.last_yaw, car_yaw, delta_time * self.damping) target_cam_quaternion = np.array( euler_to_quaternion(pitch=self.look_down_angle_rad, yaw=car_yaw)) target_camera_location = np.array([car_model_state.pose.position.x, car_model_state.pose.position.y, 0.0]) + \ apply_orientation(target_cam_quaternion, np.array([-self.cam_dist_offset, 0, 0])) target_camera_location_2d = target_camera_location[0:2] # Linear interpolate Camera position to target position cur_camera_2d_pos = np.array([ self.last_camera_state.pose.position.x, self.last_camera_state.pose.position.y ]) new_cam_pos_2d = utils.lerp(cur_camera_2d_pos, target_camera_location_2d, delta_time * self.damping) # Calculate camera rotation quaternion based on lookAt yaw look_at_yaw = utils.get_angle_between_two_points_2d_rad( self.last_camera_state.pose.position, car_model_state.pose.position) cam_quaternion = euler_to_quaternion(pitch=self.look_down_angle_rad, yaw=look_at_yaw) # Configure Camera Model State camera_model_state = ModelState() camera_model_state.model_name = self.topic_name camera_model_state.pose.position.x = new_cam_pos_2d[0] camera_model_state.pose.position.y = new_cam_pos_2d[1] camera_model_state.pose.position.z = self.cam_fixed_height camera_model_state.pose.orientation.x = cam_quaternion[0] camera_model_state.pose.orientation.y = cam_quaternion[1] camera_model_state.pose.orientation.z = cam_quaternion[2] camera_model_state.pose.orientation.w = cam_quaternion[3] self.model_state_client(camera_model_state) self.last_camera_state = camera_model_state self.last_yaw = car_yaw
def _reset(self, car_model_state): camera_model_state = ModelState() camera_model_state.model_name = self.topic_name # Calculate target Camera position based on nearest center track from the car. # 1. Project the car position to 1-d global distance of track # 2. Minus camera offset from the point of center track near_dist = self.track_data._center_line_.project( Point(car_model_state.pose.position.x, car_model_state.pose.position.y)) near_pnt_ctr = self.track_data._center_line_.interpolate(near_dist) yaw = self.track_data._center_line_.interpolate_yaw( distance=near_dist, normalized=False, reverse_dir=False, position=near_pnt_ctr) quaternion = np.array( euler_to_quaternion(pitch=self.look_down_angle_rad, yaw=yaw)) target_camera_location = np.array([near_pnt_ctr.x, near_pnt_ctr.y, 0.0]) + \ apply_orientation(quaternion, np.array([-self.cam_dist_offset, 0, 0])) # Calculate camera rotation quaternion based on lookAt yaw with respect to # current camera position and car position look_at_yaw = utils.get_angle_between_two_points_2d_rad( Point(target_camera_location[0], target_camera_location[1]), car_model_state.pose.position) cam_quaternion = euler_to_quaternion(pitch=self.look_down_angle_rad, yaw=look_at_yaw) camera_model_state.pose.position.x = target_camera_location[0] camera_model_state.pose.position.y = target_camera_location[1] camera_model_state.pose.position.z = self.cam_fixed_height camera_model_state.pose.orientation.x = cam_quaternion[0] camera_model_state.pose.orientation.y = cam_quaternion[1] camera_model_state.pose.orientation.z = cam_quaternion[2] camera_model_state.pose.orientation.w = cam_quaternion[3] self.model_state_client(camera_model_state) self.last_camera_state = camera_model_state self.last_yaw = yaw
def get_agent_pos(self, agent_name, link_name_list, relative_pos): '''Returns a dictionary with the keys defined in AgentPos which contains the position of the agent on the track, the location of the desired links, and the orientation of the agent. agent_name - String with the name of the agent link_name_list - List of strings containing the name of the links whose positions are to be retrieved. relative_pos - List containing the x-y relative position of the front of the agent ''' try: #Request the model state from gazebo model_state = self._get_model_state_(agent_name, '') #Compute the model's orientation model_orientation = np.array([ model_state.pose.orientation.x, model_state.pose.orientation.y, model_state.pose.orientation.z, model_state.pose.orientation.w ]) #Compute the model's location relative to the front of the agent model_location = np.array([model_state.pose.position.x, model_state.pose.position.y, model_state.pose.position.z]) + \ apply_orientation(model_orientation, np.array(relative_pos)) model_point = Point(model_location[0], model_location[1]) #Grab the location of the links make_link_points = lambda link: Point( link.link_state.pose.position.x, link.link_state.pose.position. y) link_points = [ make_link_points(self._get_link_state_(name, '')) for name in link_name_list ] return { AgentPos.ORIENTATION.value: model_orientation, AgentPos.POINT.value: model_point, AgentPos.LINK_POINTS.value: link_points } except Exception as ex: raise GenericRolloutException( "Unable to get position: {}".format(ex))
def _get_initial_camera_pose(self, car_model_state): _, _, car_yaw = quaternion_to_euler(x=car_model_state.pose.orientation.x, y=car_model_state.pose.orientation.y, z=car_model_state.pose.orientation.z, w=car_model_state.pose.orientation.w) target_cam_quaternion = np.array(euler_to_quaternion(pitch=self.look_down_angle_rad, yaw=car_yaw)) target_camera_location = np.array([car_model_state.pose.position.x, car_model_state.pose.position.y, 0.0]) + \ apply_orientation(target_cam_quaternion, np.array([-self.cam_dist_offset, 0, 0])) camera_model_pose = Pose() camera_model_pose.position.x = target_camera_location[0] camera_model_pose.position.y = target_camera_location[1] camera_model_pose.position.z = self.cam_fixed_height camera_model_pose.orientation.x = target_cam_quaternion[0] camera_model_pose.orientation.y = target_cam_quaternion[1] camera_model_pose.orientation.z = target_cam_quaternion[2] camera_model_pose.orientation.w = target_cam_quaternion[3] self.last_yaw = car_yaw return camera_model_pose
def get_relative_pos(origin, translation, rotation): ''' Get the relative offset position to the center of car Args: origin(list): list of object origin in [x,y,z] in Cartision coordinate translation(list): list of object translation [x,y,z] in Cartision coordinate rotation(list): list of object rotation [x,y,z,w] in Quaternion Return: Tuple: (x, y) ''' try: # Compute the model's location relative to the front of the car model_location = np.array(origin) + \ apply_orientation(np.array(rotation), np.array(translation)) model_point = (model_location[0], model_location[1]) return model_point except Exception as ex: raise GenericRolloutException("Unable to get position: {}".format(ex))
def get_right_vec(quaternion): return apply_orientation(quaternion, GazeboWorld.right)
def get_up_vec(quaternion): return apply_orientation(quaternion, GazeboWorld.up)
def get_forward_vec(quaternion): return apply_orientation(quaternion, GazeboWorld.forward)