math_funcs.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import math
  2. from mathutils import Matrix
  3. # file containning math related functions
  4. # function to return a 0 filled matrix 3x3
  5. def get_zero_mat3x3():
  6. return Matrix(([0, 0, 0], [0, 0, 0], [0, 0, 0]))
  7. # function to return a 0 filled matrix 4x4
  8. def get_zero_mat4x4():
  9. return Matrix(([0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]))
  10. # function to return an identity matrix 3x3
  11. def get_id_mat3x3():
  12. return Matrix(([1, 0, 0], [0, 1, 0], [0, 0, 1]))
  13. # function to return an identity matrix 4x4
  14. def get_id_mat4x4():
  15. return Matrix(([1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]))
  16. # calc_scale_matrix function
  17. # function to build the scale matrix
  18. def calc_scale_matrix(x_scale, y_scale, z_scale):
  19. scale_matrix = Matrix(( [x_scale, 0, 0, 0],
  20. [0, y_scale, 0, 0],
  21. [0, 0, z_scale, 0],
  22. [0, 0, 0, 1]
  23. ))
  24. return scale_matrix
  25. # calc_rotation_matrix function
  26. # function to calculate the rotation matrix
  27. # for a Extrinsic Euler XYZ system (radians)
  28. def calc_rotation_matrix(x_angle, y_angle, z_angle):
  29. x_rot_mat = Matrix(([1, 0, 0, 0],
  30. [0, math.cos(x_angle), -math.sin(x_angle), 0],
  31. [0, math.sin(x_angle), math.cos(x_angle), 0],
  32. [0, 0, 0, 1]))
  33. y_rot_mat = Matrix(([math.cos(y_angle), 0, math.sin(y_angle), 0],
  34. [0, 1, 0, 0],
  35. [-math.sin(y_angle), 0, math.cos(y_angle), 0],
  36. [0, 0, 0, 1]))
  37. z_rot_mat = Matrix(([math.cos(z_angle), -math.sin(z_angle), 0, 0],
  38. [math.sin(z_angle), math.cos(z_angle), 0, 0],
  39. [0, 0, 1, 0],
  40. [0, 0, 0, 1]))
  41. return z_rot_mat * y_rot_mat * x_rot_mat
  42. # calc_translation_matrix function
  43. # function to build the translation matrix
  44. def calc_translation_matrix(x_translation, y_translation, z_translation):
  45. translation_matrix = Matrix(( [1, 0, 0, x_translation],
  46. [0, 1, 0, y_translation],
  47. [0, 0, 1, z_translation],
  48. [0, 0, 0, 1]
  49. ))
  50. return translation_matrix
  51. # interpolate function
  52. # used to find a value in an interval with the specified mode.
  53. # So that it is clear that the values are points that have 2
  54. # coordinates I will treat the input as they are (x,y) points
  55. # the function will either return m_x or m_y depending on
  56. # which of the middle point values is provided to the function
  57. # (set None to the variable going to be returned by the
  58. # function). Only linear interpolation is supported for now.
  59. #
  60. # l_x (float) --> left point X axis component
  61. # l_y (float) --> left point Y axis component
  62. # r_x (float) --> right point X axis component
  63. # r_y (float) --> right point Y axis component
  64. # m_x (float) --> middle point X axis component
  65. # m_y (float) --> middle point Y axis component
  66. # interp_type (string) --> "linear" for linear interpolation
  67. def interpolate(l_x, l_y, r_x, r_y, m_x, m_y, interp_type):
  68. # variable to be returned
  69. result = 0
  70. # if right point does not exist (special case)
  71. # return l_y as the interpolation result
  72. if (r_x == None or r_y == None):
  73. result = l_y
  74. return result
  75. # m_x is the one to be returned
  76. if (m_x == None):
  77. # linear interpolation
  78. if (interp_type == "linear"):
  79. m_x = (((r_x - l_x) / (r_y - l_y)) * (m_y - r_y)) + r_x
  80. result = m_x
  81. # m_y is the one to be returned
  82. if (m_y == None):
  83. # linear interpolation
  84. if (interp_type == "linear"):
  85. m_y = (((r_y - l_y) / (r_x - l_x)) * (m_x - r_x)) + r_y
  86. result = m_y
  87. return result
  88. # find_left_right function
  89. # find the values and positions of the elements at the left and the
  90. # right of the element in position pos on the anim_array
  91. # elements will be used in the interpolate() function later
  92. #
  93. # anim_array (array of floats) --> anim property frame array its
  94. # length must the animation length
  95. # pos (int) --> position of the animation property value to be
  96. # later interpolated in the anim_array array
  97. def find_left_right(anim_array, pos):
  98. # create left/right variables
  99. l_val = 0
  100. l_val_pos = 0
  101. r_val = 0
  102. r_val_pos = 0
  103. # find near left value (has to exist)
  104. # read array from right to left
  105. for i in range(len(anim_array), -1, -1):
  106. #
  107. # skip elements at the right of
  108. # pos in anim_array
  109. if (i >= pos):
  110. continue
  111. # left value is found
  112. if (anim_array[i] != None):
  113. l_val = anim_array[i]
  114. l_val_pos = i
  115. break
  116. #
  117. ##############
  118. # special case
  119. # if pos is the last element position on
  120. # the array r_val and r_val_pos do not exist
  121. if (pos == (len(anim_array) - 1)):
  122. return [l_val_pos, l_val, None, None]
  123. # find near right value (might not exist)
  124. # read array from left to right
  125. for i in range(len(anim_array)):
  126. # skip elements at the left of
  127. # pos in anim_array
  128. if (i <= pos):
  129. continue
  130. # right value is found
  131. if (anim_array[i] != None):
  132. r_val = anim_array[i]
  133. r_val_pos = i
  134. break
  135. # if no value is found at the end of
  136. # the anim_array r_val and r_val_pos do not exist
  137. # (value does not change between the left value
  138. # found and the end of the animation)
  139. if (i == (len(anim_array) - 1)):
  140. return [l_val_pos, l_val, None, None]
  141. # if all values are found, return them
  142. return [l_val_pos, l_val, r_val_pos, r_val]
  143. # convert_angle_to_180 function
  144. # function used by the convert_anim_rot_to_180 function
  145. # to convert a single angle in its representation on
  146. # the -180/+180 degree range (angles passed to the
  147. # function that are already in this range will be
  148. # returned without conversion)
  149. #
  150. # angle (float) --> angle to convert to the -180/+180
  151. # degree range (angle is expected to
  152. # be in degrees)
  153. def convert_angle_to_180(angle):
  154. #
  155. # check if the angle really needs to be processed
  156. # i.e. is already inside the -180/+180 degree range
  157. if (angle >= -180 and angle <= 180):
  158. return angle
  159. # convert it otherwise
  160. # check if it is positive or negative
  161. # and set the opposite direction of the angle
  162. # if the angle is > 0 then its mesurement is clockwise (opposite is counter-clockwise)
  163. # if the angle is < 0 then its mesurement is counter-clockwise (opposite is clockwise)
  164. if (angle > 0):
  165. opposite_spin_dir = -1
  166. else: # it is negative
  167. opposite_spin_dir = 1
  168. # decrease the angle by 360 degrees until it
  169. # is in the -180/+180 degree interval
  170. while (abs(angle) > 180):
  171. angle = angle + (opposite_spin_dir * 360)
  172. return angle
  173. # convert_anim_rot_to_180 function
  174. # used to re-calculate a rotation animation on an axis
  175. # so that angles used lay in between -180/180 degrees
  176. # done to avoid rotation animation data loss when extracting
  177. # said angles from a transformation matrix
  178. # this function calls the convert_angle_to_180() function
  179. # at the end of the function csv_keyframe_numbers is updated
  180. # with the new frames to be injected into the animation
  181. #
  182. # example:
  183. #
  184. # Original keyframes:
  185. # Frame 0 Frame 21 (2 keyframes)
  186. # 0º 360º
  187. #
  188. # Processed keyframes:
  189. # Frame 0 Frame 10 Frame 11 Frame 21 (4 keyframes)
  190. # 0º 171.4º -171.5º 0º
  191. #
  192. # rot_array (array of floats) --> bone rotation animation data
  193. # for a single axis
  194. # csv_keyframe_numbers (array of ints) --> original keyframes
  195. # of the animation
  196. #
  197. # Note: function will have problems interpreting keyframes with
  198. # high rotation diferences if the number of frames in
  199. # between said keyframes in lower than 2 times the spins
  200. # done in between those keyframe angles (thinking a fix)
  201. def convert_rot_anim_to_180(rot_array, csv_keyframe_numbers):
  202. # temp rot array to store calculated values and keyframe position
  203. rot_array_cp = [[], []]
  204. # find the frames in which rot_array has values defined
  205. rot_array_kf = []
  206. for i in range(len(rot_array)):
  207. if (rot_array[i] != None):
  208. rot_array_kf.append(i)
  209. # loop through each consecutive pair of keyframes of the rot_array_kf array
  210. for i in range(len(rot_array_kf) - 1):
  211. #
  212. # get left/right keyframe values and positions
  213. l_kf_pos = rot_array_kf[i]
  214. l_kf_val = rot_array[l_kf_pos]
  215. r_kf_pos = rot_array_kf[i + 1]
  216. r_kf_val = rot_array[r_kf_pos]
  217. # append l_kf_val to rot_array_cp (converted)
  218. rot_array_cp[0].append(l_kf_pos)
  219. rot_array_cp[1].append(convert_angle_to_180(l_kf_val))
  220. # get the rotation direction
  221. if (r_kf_val > l_kf_val): # clockwise
  222. rot_direction = 1
  223. else: # counter-clockwise
  224. rot_direction = -1
  225. # advance -180/+180 (depending on the rotation direction)
  226. # and generate the middle keyframe values
  227. # angle is fixed to the l_kf_val's closest 360 degree multiple
  228. angle_val = l_kf_val - convert_angle_to_180(l_kf_val)
  229. while (abs(r_kf_val - angle_val) > 180):
  230. #
  231. angle_val = angle_val + (rot_direction * 180)
  232. # find the frame (float) in which this value exists
  233. angle_pos = interpolate(l_kf_pos, l_kf_val, r_kf_pos, r_kf_val, None, angle_val, "linear")
  234. # find the 2 frames (integer) that are lower
  235. # and upper limits of this angle_pos
  236. lower_frame = int(angle_pos)
  237. upper_frame = int(angle_pos + 1)
  238. # interpolate to find the values on lower_frame and upper_frame
  239. lower_frame_value = convert_angle_to_180(interpolate(l_kf_pos, l_kf_val, r_kf_pos, r_kf_val, lower_frame, None, "linear"))
  240. upper_frame_value = convert_angle_to_180(interpolate(l_kf_pos, l_kf_val, r_kf_pos, r_kf_val, upper_frame, None, "linear"))
  241. # append results to rot_array_cp
  242. # keyframes
  243. rot_array_cp[0].append(lower_frame)
  244. rot_array_cp[0].append(upper_frame)
  245. # values
  246. rot_array_cp[1].append(lower_frame_value)
  247. rot_array_cp[1].append(upper_frame_value)
  248. # add the new keyframe values to rot_array
  249. # on their respective frame position
  250. for i in range(len(rot_array_cp[0])):
  251. rot_array[rot_array_cp[0][i]] = rot_array_cp[1][i]
  252. # update the keyframes on csv_keyframe_numbers to store
  253. # the calculated keyframes numbers from rot_array_cp[0]
  254. # append those at the end of csv_keyframe_numbers
  255. for i in range(len(rot_array_cp[0])):
  256. value_found = False
  257. for j in range(len(csv_keyframe_numbers)):
  258. if (rot_array_cp[0][i] == csv_keyframe_numbers[j]):
  259. value_found = True
  260. break
  261. if (value_found == False):
  262. csv_keyframe_numbers.append(rot_array_cp[0][i])