def motionStitching(M1, M2, slice, funcType): A_endPos = M1.postures[M1.frames] B_startPos = M2.postures[1] pos_dif = Posture.postureDifference_v2(A_endPos, B_startPos) # project to y-axis new_R = np.identity(4) new_R[:-1, :-1] = uti.projection_y(pos_dif.Rmatrix[0][:-1, :-1]) pos_dif.Rmatrix[0] = new_R newB_postures = [] for i in range(1, M2.frames + 1): newB_postures.append( Posture(np.array(M2.postures[i].origin), list(M2.postures[i].Rmatrix))) # Alignment for posture in newB_postures: posture.origin = A_endPos.origin + pos_dif.Rmatrix[0][:-1, :-1] @ ( posture.origin - B_startPos.origin) posture.Rmatrix[0][:-1, :-1] = pos_dif.Rmatrix[ 0][:-1, :-1] @ posture.Rmatrix[0][:-1, :-1] posture.Rmatrix[0][:-1, 3] = posture.origin # Motion Warping pos_dif = Posture.postureDifference(newB_postures[0], A_endPos) func = None arg = None if funcType == 0: func = uti.linearFunc2_t arg = 1. / slice elif funcType == 1: func = uti.cosFunc_t arg = slice for i in range(1, slice + 1): t = func(arg, i) b = np.zeros(3) for j in range(3): b[j] = t * pos_dif.origin[j] new_origin = newB_postures[i].origin + b new_Rmatrix = [] for R1, R2 in zip(newB_postures[i].Rmatrix, pos_dif.Rmatrix): new_R = np.identity(4) new_R[:-1, :-1] = uti.addByT(R1[:-1, :-1], R2[:-1, :-1], t) # new_R[:-1,:-1] = uti.addByT(np.identity(3),R2[:-1,:-1],t) new_Rmatrix.append(new_R) new_Rmatrix[0][:-1, 3] = new_origin newB_postures[i] = Posture(new_origin, new_Rmatrix) # Concatenate for i in range(1, M2.frames): newB_postures[i].make_framebuffer(M1.skeleton.root) M1.postures.append(newB_postures[i]) M1.frames = M1.frames + (M2.frames - 1) return M1
def motionWarping(self, frame, new_posture, startF, endF, funcType): posture_dif = Posture.postureDifference(self.postures[frame], new_posture) new_postures = [] for posture in self.postures: new_postures.append( Posture(np.array(posture.origin), list(posture.Rmatrix))) first_slice = frame - startF second_slice = endF - frame first_func = None second_func = None firstArg = None secondArg = None if funcType == 0: first_func = uti.linearFunc_t second_func = uti.linearFunc2_t firstArg = 1. / first_slice secondArg = 1. / second_slice elif funcType == 1: first_func = uti.sinFunc_t second_func = uti.cosFunc_t firstArg = first_slice secondArg = second_slice for i in range(startF, frame + 1): t = first_func(firstArg, i - startF) b = np.zeros(3) for j in range(3): b[j] = t * posture_dif.origin[j] new_origin = self.postures[i].origin + b new_Rmatrix = [] for R1, R2 in zip(self.postures[i].Rmatrix, posture_dif.Rmatrix): new_R = np.identity(4) new_R[:-1, :-1] = uti.addByT(R1[:-1, :-1], R2[:-1, :-1], t) new_Rmatrix.append(new_R) new_postures[i] = Posture(new_origin, new_Rmatrix) for i in range(frame + 1, endF + 1): t = second_func(secondArg, i - frame) b = np.zeros(3) for j in range(3): b[j] = t * posture_dif.origin[j] new_origin = self.postures[i].origin + b new_Rmatrix = [] for R1, R2 in zip(self.postures[i].Rmatrix, posture_dif.Rmatrix): new_R = np.identity(4) new_R[:-1, :-1] = uti.addByT(R1[:-1, :-1], R2[:-1, :-1], t) new_Rmatrix.append(new_R) new_postures[i] = Posture(new_origin, new_Rmatrix) for posture in new_postures: posture.Rmatrix[0][:-1, 3] = posture.origin posture.make_framebuffer(self.skeleton.root) return Motion(self.skeleton, new_postures, self.frames, self.frame_rate)