Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

from __future__ import print_function 

 

import numbers 

 

import numpy 

from director.thirdparty import transformations 

from geometry_msgs_mock import Pose 

 

 

 

def rounded(val): 

  if isinstance(val, str): 

    return rounded(float(val)) 

  elif isinstance(val, numbers.Number): 

    return int(round(val,6) * 1e5) / 1.0e5 

  else: 

    return numpy.array([rounded(v) for v in val]) 

 

 

def homogeneous2translation_quaternion(homogeneous): 

  """ 

  Translation: [x, y, z] 

  Quaternion: [x, y, z, w] 

  """ 

  translation = transformations.translation_from_matrix(homogeneous) 

  quaternion = transformations.quaternion_from_matrix(homogeneous) 

  return translation, quaternion 

 

 

def homogeneous2translation_rpy(homogeneous): 

  """ 

  Translation: [x, y, z] 

  RPY: [sx, sy, sz] 

  """ 

  translation = transformations.translation_from_matrix(homogeneous) 

  rpy = transformations.euler_from_matrix(homogeneous) 

  return translation, rpy 

 

 

def homogeneous2pose_msg(homogeneous): 

  pose = Pose() 

  translation, quaternion = homogeneous2translation_quaternion(homogeneous) 

  pose.position.x = translation[0] 

  pose.position.y = translation[1] 

  pose.position.z = translation[2] 

  pose.orientation.x = quaternion[0] 

  pose.orientation.y = quaternion[1] 

  pose.orientation.z = quaternion[2] 

  pose.orientation.w = quaternion[3] 

  return pose 

 

 

def pose_msg2homogeneous(pose): 

  trans = transformations.translation_matrix((pose.position.x, pose.position.y, pose.position.z)) 

  rot = transformations.quaternion_matrix((pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w)) 

  return transformations.concatenate_matrices(trans, rot) 

 

 

def array2string(array): 

  return numpy.array_str(array).strip('[]. ').replace('. ', ' ') 

 

 

def homogeneous2tq_string(homogeneous): 

  return 't=%s q=%s' % homogeneous2translation_quaternion(homogeneous) 

 

 

def homogeneous2tq_string_rounded(homogeneous): 

  return 't=%s q=%s' % tuple(rounded(o) for o in homogeneous2translation_quaternion(homogeneous)) 

 

 

def string2float_list(s): 

  return [float(i) for i in s.split()] 

 

 

def pose_string2homogeneous(pose): 

  pose_float = string2float_list(pose) 

  translate = pose_float[:3] 

  angles = pose_float[3:] 

  homogeneous = transformations.compose_matrix(None, None, angles, translate) 

  #print('pose_string=%s; translate=%s angles=%s homogeneous:\n%s' % (pose, translate, angles, homogeneous)) 

  return homogeneous 

 

 

def rotation_only(homogeneous): 

  euler = transformations.euler_from_matrix(homogeneous) 

  return transformations.euler_matrix(euler[0], euler[1], euler[2])