def test_find_relative_pose_changes_orientation(self): pose = uetrans.UnrealTransform(location=(11, 12, 13), rotation=(45, 30, -30)) trans = uetrans.UnrealTransform(location=(10, 9, 8), rotation=(10, 20, 90)) pose_rel = trans.find_relative(pose) self.assert_close((70.532806, 28.575809, -105.581543), pose_rel.euler)
def test_find_independent_pose_changes_orientation(self): pose = uetrans.UnrealTransform(location=(1, 3, 5), rotation=(45, 30, -30)) trans = uetrans.UnrealTransform(location=(10, 9, 8), rotation=(10, 20, 90)) pose_ind = trans.find_independent(pose) self.assert_close((42.224522, 52.174339, 56.373390), pose_ind.euler)
def test_find_independent_pose_changes_location_coordinates_simple(self): pose = uetrans.UnrealTransform(location=(1, 3, 5), rotation=(45, 30, -30)) trans = uetrans.UnrealTransform(location=(10, 9, 8), rotation=(0, 0, 45)) pose_ind = trans.find_independent(pose) self.assert_close((10 - np.sqrt(2), 9 + 2 * np.sqrt(2), 13), pose_ind.location)
def test_find_independent_pose_changes_location_coordinates_complex(self): pose = uetrans.UnrealTransform(location=(1, 3, 5), rotation=(45, 30, -30)) trans = uetrans.UnrealTransform(location=(10, 9, 8), rotation=(10, 20, 90)) pose_ind = trans.find_independent(pose) self.assert_close((6.177, 8.434, 12.480), pose_ind.location, abs_tol=0.001)
def test_relative_pose_contains_relative_point(self): trans = uetrans.UnrealTransform(location=(-13, 27, -127), rotation=(184, 12, -34)) point = np.array([41, -153, 16]) pose = uetrans.UnrealTransform(location=point, rotation=(0, 0, 45)) pose_rel = trans.find_relative(pose) point_rel = pose_rel.location point_prime = trans.find_independent(point_rel) self.assert_close(point, point_prime)
def test_find_relative_undoes_pose(self): trans = uetrans.UnrealTransform(location=(-13, 27, -127), rotation=(17, -62, -163)) pose = uetrans.UnrealTransform(location=(10, 100, -5), rotation=(-162, 56, 53)) pose_rel = trans.find_relative(pose) pose_prime = trans.find_independent(pose_rel) self.assert_close(pose.location, pose_prime.location) self.assert_close(pose.euler, pose_prime.euler)
def test_find_relative_pose_changes_location_coordinates(self): pose = uetrans.UnrealTransform(location=(11, 12, 13), rotation=(45, 30, -30)) trans = uetrans.UnrealTransform(location=(10, 9, 8), rotation=(10, 20, 90)) pose_rel = trans.find_relative(pose) # Limited by the precision of Unreal prints, I'm not going to worry about errors smaller than this self.assert_close((4.529, -1.623, 3.443), pose_rel.location, abs_tol=0.001)
def test_yaw_from_unreal(self): # This pose is rotated 45 degrees clockwise around up (Z in my coordinate frame) ue_pose = uetrans.UnrealTransform(location=(0, 0, 0), rotation=(0, 0, 45)) pose = uetrans.transform_from_unreal(ue_pose) self.assert_close(tf.quaternions.axangle2quat((0, 0, 1), -np.pi / 4), pose.rotation_quat(w_first=True)) # This pose is rotated 30 degrees anticlockwise around up ue_pose = uetrans.UnrealTransform(location=(0, 0, 0), rotation=(0, 0, -30)) pose = uetrans.transform_from_unreal(ue_pose) self.assert_close(tf.quaternions.axangle2quat((0, 0, 1), np.pi / 6), pose.rotation_quat(w_first=True))
def test_roll_from_unreal(self): # This pose is rotated 45 degrees clockwise around X ue_pose = uetrans.UnrealTransform(location=(0, 0, 0), rotation=(45, 0, 0)) pose = uetrans.transform_from_unreal(ue_pose) self.assert_close(tf.quaternions.axangle2quat((1, 0, 0), np.pi / 4), pose.rotation_quat(w_first=True)) # And this is 30 degrees anticlockwise around X ue_pose = uetrans.UnrealTransform(location=(0, 0, 0), rotation=(-30, 0, 0)) pose = uetrans.transform_from_unreal(ue_pose) self.assert_close(tf.quaternions.axangle2quat((1, 0, 0), -np.pi / 6), pose.rotation_quat(w_first=True))
def test_pitch_from_unreal(self): # This pose is rotated 45 degrees clockwise around right (neg Y) ue_pose = uetrans.UnrealTransform(location=(0, 0, 0), rotation=(0, 45, 0)) pose = uetrans.transform_from_unreal(ue_pose) self.assert_close(tf.quaternions.axangle2quat((0, -1, 0), np.pi / 4), pose.rotation_quat(w_first=True)) # This pose is rotated 30 degrees anticlockwise around right (neg Y) ue_pose = uetrans.UnrealTransform(location=(0, 0, 0), rotation=(0, -30, 0)) pose = uetrans.transform_from_unreal(ue_pose) self.assert_close(tf.quaternions.axangle2quat((0, -1, 0), -np.pi / 6), pose.rotation_quat(w_first=True))
def get_object_pose(self, object_name): """ Get the world pose of an object within the simulation. :param object_name: The name of the object :return: """ if self._client is not None: location = self._client.request( "vget /object/{0}/location".format(object_name)) location = location.split(' ') if len(location) == 3: rotation = self._client.request( "vget /object/{0}/rotation".format(object_name)) rotation = rotation.split(' ') if len(rotation) == 3: ue_trans = uetf.UnrealTransform( location=(float(location[0]), float(location[1]), float(location[2])), # Reorder to roll, pitch, yaw, Unrealcv order is pitch, yaw, roll rotation=(float(rotation[2]), float(rotation[0]), float(location[1]))) ue_trans = self._origin.find_relative( ue_trans) # Express relative to the simulator origin return uetf.transform_from_unreal(ue_trans) return None
def move_camera_to(self, pose): """ Move the camera to a given pose, colliding with objects and stopping if blocked. :param pose: :return: """ if self._client is not None: if isinstance(pose, uetf.UnrealTransform): self._current_pose = uetf.transform_from_unreal(pose) else: self._current_pose = pose pose = uetf.transform_to_unreal(pose) pose = self._origin.find_independent(pose) self._client.request("vset /camera/0/moveto {0} {1} {2}".format( pose.location[0], pose.location[1], pose.location[2])) self._client.request("vset /camera/0/rotation {0} {1} {2}".format( pose.pitch, pose.yaw, pose.roll)) # Re-extract the location from the sim, because we might not have made it to the desired location location = self._client.request("vget /camera/0/location") location = location.split(' ') if len(location) == 3: pose = uetf.UnrealTransform(location=(float(location[0]), float(location[1]), float(location[2])), rotation=pose.euler) self._current_pose = uetf.transform_from_unreal(pose)
def test_find_relative_undoes_point(self): trans = uetrans.UnrealTransform(location=(-13, 27, -127), rotation=(127, -54, -89)) point = (1, 2, 3) point_rel = trans.find_relative(point) point_prime = trans.find_independent(point_rel) self.assert_close(point, point_prime)
def test_pose_from_unreal(self): ue_pose = uetrans.UnrealTransform(location=(37, 113, -97), rotation=(-21.688360, -31.675301, 31.189531)) pose = uetrans.transform_from_unreal(ue_pose) self.assert_array((0.37, -1.13, -0.97), pose.location) self.assert_close(tf.quaternions.axangle2quat((1, -3, 2), -np.pi / 4), pose.rotation_quat(w_first=True))
def test_rotation_from_homogeneous(self): hom = tf.affines.compose(T=(1, 2, 3), R=tf.taitbryan.axangle2mat((1, -3, 2), -np.pi / 9), Z=np.ones(3)) trans = uetrans.UnrealTransform(hom) # These numbers were generated using "Rotator from Axis and Angle" given the same arguments as above self.assert_close(trans.euler, (-6.985809, -15.403244, 11.714415))
def test_location_from_homogeneous(self): hom = tf.affines.compose(T=(1, 2, 3), R=tf.taitbryan.axangle2mat((1, 2, 3), np.pi / 9), Z=np.ones(3)) trans = uetrans.UnrealTransform(hom) # homogenous is in world coordinates self.assert_array((100, -200, 300), trans.location)
def test_find_independent_point_changes_location_coordinates_complex(self): point = (1, 3, 5) trans = uetrans.UnrealTransform(location=(10, 9, 8), rotation=(10, 20, 90)) point_ind = trans.find_independent(point) self.assert_close( (6.177, 8.434, 12.480), point_ind, abs_tol=0.001) # Limited to the accuracy of unreal prints
def test_to_and_from_unreal_interchangable_point_and_pose(self): point = (125, -73, 403) pose = mytf.Transform((-19, -23, 300), (0.6, -0.7, -0.3, 1.4)) ue_point = uetrans.transform_to_unreal(point) ue_pose = uetrans.transform_to_unreal(pose) ue_point_trans = uetrans.UnrealTransform(location=ue_point) ue_pose_point = ue_pose.location point_prime = uetrans.transform_from_unreal(ue_point_trans).location pose_prime = uetrans.transform_from_unreal(ue_pose_point) self.assert_array(point, point_prime) self.assert_array(pose.location, pose_prime)
def parse_transform(location, rotation): """ Construct a transform from a stored location and rotation. Handles changing the coordinate frame from Unreal space to a conventional coordinate frame. :param location: dict containing X, Y, and Z :param rotation: dict containing W, Z, Y, and Z :return: a Transform object, in a sane coordinate frame and scaled to meters """ ue_camera_pose = ue_tf.UnrealTransform(location=(location['X'], location['Y'], location['Z']), rotation=ue_tf.quat2euler( w=rotation['W'], x=rotation['X'], y=rotation['Y'], z=rotation['Z'], )) return ue_tf.transform_from_unreal(ue_camera_pose)
def begin(self): """ Start producing images. This will trigger any necessary startup code, and will allow get_next_image to be called. Return False if there is a problem starting the source. :return: True iff the simulator has started correctly. """ # Start the simulator process if self._host == 'localhost': if self._client is not None: # Stop the client to free up the port self._client.disconnect() self._client = None self._store_config() start_simulator(self._executable) # Wait for the UnrealCV server to start, pulling lines from stdout to check time.sleep( 2) # Wait, we can't capture some of the output right now # while self._simulator_process.poll() is not None: # line = self._simulator_process.stdout.readline() # if 'listening on port {0}'.format(self._port) in line.lower(): # # Server has started, break # break # Create and connect the client if self._client is None: self._client = unrealcv.Client((self._host, self._port)) if not self._client.isconnected(): # Try and connect to the server 3 times, waiting 2s between tries for _ in range(10): self._client.connect() if self._client.isconnected(): break else: time.sleep(2) if not self._client.isconnected(): # Cannot connect to the server, shutdown completely. self.shutdown() # Set camera state from configuration if self._client is not None and self._client.isconnected(): # Set camera properties self.set_field_of_view(self._fov) self.set_fstop(self._aperture) self.set_enable_dof(self._use_dof) if self._use_dof: # Don't bother setting dof properties if dof is disabled if self._focus_distance is None: self.set_autofocus(True) else: self.set_focus_distance(self._focus_distance) # Set quality settings self._client.request( "vset /quality/texture-mipmap-bias {0}".format( self._texture_mipmap_bias)) self._client.request( "vset /quality/normal-maps-enabled {0}".format( int(self._normal_maps_enabled))) self._client.request("vset /quality/roughness-enabled {0}".format( int(self._roughness_enabled))) self._client.request( "vset /quality/geometry-decimation {0}".format( self._geometry_decimation)) # Get the initial camera pose, which will be set by playerstart in the level location = self._client.request("vget /camera/0/location") location = location.split(' ') if len(location) == 3: rotation = self._client.request("vget /camera/0/rotation") rotation = rotation.split(' ') if len(rotation) == 3: ue_trans = uetf.UnrealTransform( location=(float(location[0]), float(location[1]), float(location[2])), # Reorder to roll, pitch, yaw, Unrealcv order is pitch, yaw, roll rotation=(float(rotation[2]), float(rotation[0]), float(location[1]))) self._current_pose = uetf.transform_from_unreal(ue_trans) return self._client is not None and self._client.isconnected()
def test_constructor_clone(self): trans1 = uetrans.UnrealTransform(location=(1, 2, 3), rotation=(4, 5, 6)) trans2 = uetrans.UnrealTransform(trans1) self.assert_array(trans1.location, trans2.location) self.assert_array(trans1.euler, trans2.euler)
def test_location_basic(self): trans = uetrans.UnrealTransform(location=(1, 2, 3)) self.assert_array((1, 2, 3), trans.location)
def test_location_default(self): trans = uetrans.UnrealTransform() self.assert_array(np.zeros(3), trans.location)
def test_rotation_basic(self): # The rotation here is for 45 degrees around axis 1,2,3 trans = uetrans.UnrealTransform(location=(1, 2, 3), rotation=(142, -36, 100)) self.assert_array((142, -36, 100), trans.euler)
def test_rotation_default(self): trans = uetrans.UnrealTransform() self.assert_array(trans.euler, (0, 0, 0))
def test_find_independent_point_moves_origin(self): point = (1, 3, 5) trans = uetrans.UnrealTransform(location=(10, 9, 8)) point_ind = trans.find_independent(point) self.assert_array((11, 12, 13), point_ind)
def test_find_independent_pose_moves_origin(self): pose = uetrans.UnrealTransform(location=(1, 3, 5), rotation=(45, 30, -30)) trans = uetrans.UnrealTransform(location=(10, 9, 8)) pose_ind = trans.find_independent(pose) self.assert_array((11, 12, 13), pose_ind.location)
def test_find_relative_point_moves_origin(self): point = (11, 12, 13) trans = uetrans.UnrealTransform(location=(10, 9, 8)) point_rel = trans.find_relative(point) self.assert_array(point_rel, (1, 3, 5))