/
hair_interpolation.py
159 lines (132 loc) · 4.81 KB
/
hair_interpolation.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from weight_estimate import npar
from struct import pack
import coordinates as cd
import numpy as np
import array
class HairInterpolation:
def __init__(self, guideData, weights, reference, fileName):
self.guideData = guideData
self.weights = weights
self.refFrame = reference
self.fileName = fileName
self.nFrame = len(guideData)
self.nStrand = len(weights)
print "frame %d, strand %d" % (self.nFrame, self.nStrand)
def interpolate(self):
f = open(self.fileName, 'wb')
f.write(pack('i', self.nFrame))
f.write(pack('i', self.nStrand*npar))
bar = ProgressBar().start()
for i in range(self.nFrame):
bar.update(i*100/self.nFrame)
f.write(pack('i', i))
for j in range(self.nStrand):
s = self.strandInterpolation(j, i)
for k in range(npar*3):
f.write(pack('f', s[k]))
bar.finish()
f.close()
def strandInterpolation(self, s, fn):
if self.weights[s][0] == None:
Ci = [s]
w = 1.0
else:
Ci = self.weights[s][1]
w = self.weights[s][0]
t0 = self.refFrame.data[s*npar:(s+1)*npar]
guide = self.guideData[fn]
tref = cd.rigid_trans_batch_no_dir(guide.rigid_motion, t0)
A = []
for g in Ci:
Bg = guide.particle_motions[g]
state = np.array(cd.point_trans_batch_no_dir(Bg, tref))
import ipdb; ipdb.set_trace()
state.resize(3*npar)
A.append(state)
A = np.matrix(A)
return (A.T * np.matrix(w).T).A1
class HairInterpolation20:
def __init__(self, guideData, weights, reference, fileName):
self.guideData = guideData
self.weights = weights
self.refFrame = reference
self.fileName = fileName
self.nFrame = len(guideData)
self.nStrand = len(weights)
print "frame %d, strand %d" % (self.nFrame, self.nStrand)
# @profile
def interpolate(self):
f = open(self.fileName, 'wb')
f.write(pack('i', self.nFrame))
f.write(pack('i', self.nStrand*npar))
bar = ProgressBar().start()
for i in range(self.nFrame):
bar.update(i*100/self.nFrame)
f.write(pack('i', i))
for i1 in range(4):
for j in range(4):
f.write(pack('f', self.guideData[i].rigidMotionMatrix[i1, j]))
poss = np.empty(self.nStrand*npar*3)
dirs = np.empty(self.nStrand*npar*3)
for j in range(self.nStrand):
s = self.strandInterpolation(j, i)
poss[j*npar*3:(j+1)*npar*3] = s[:npar*3]
dirs[j*npar*3:(j+1)*npar*3] = s[npar*3:]
# import ipdb; ipdb.set_trace()
array.array('f', poss).tofile(f)
array.array('f', dirs).tofile(f)
bar.finish()
f.close()
# @profile
def strandInterpolation(self, s, fn):
if self.weights[s][0] == None:
Ci = [s]
w = 1.0
else:
Ci = self.weights[s][1]
w = self.weights[s][0]
s0 = (self.refFrame.data[s*npar:(s+1)*npar], self.refFrame.particle_direction[s*npar:(s+1)*npar])
guide = self.guideData[fn]
tref = cd.rigid_trans_batch(guide.rigid_motion, s0)
A = []
for g in Ci:
Bg = guide.particle_motions[g]
state = np.array(cd.point_trans_batch(Bg, tref))
state.resize(6*npar)
A.append(state)
A = np.matrix(A)
return (A.T * np.matrix(w).T).A1
if __name__ == "__main__":
import cPickle as pkl
import nCacheHooker as ch
import nCache
from progressbar import*
import crash_on_ipy
from local_para import *
import os
os.chdir(dumpFilePath)
nFrame = 200
# fileName = "../../maya cache/03074/hair_nRigidShape1.xml"
fileName = mxcFile
nStrand, nParticle, factor, refFrame, radius, frameFilter = pkl.load(file(dumpFilePath+'\info.dump', 'r'))
guide, weights = pkl.load(file(wFile, 'rb'))
factor = 5
n = len(weights)
# print len(weights)
# if n != 4245:
# nStrand = n/factor
# for i in range(n/factor):
# for j in range(1, factor):
# if weights[j*nStrand+i][0] != None:
# weights[i] = weights[j*nStrand+i]
# weights = weights[:nStrand]
# else:
# nStrand = n
print len(weights)
guideImporter = ch.GuideHairHooker(guide, refFrame)
guideImporter.startLoop("Import guide hair data with %d frames:" % nFrame)
nCache.loop(fileName, guideImporter, nFrame)
guideImporter.endLoop()
guideData = guideImporter.getResult()
cache = HairInterpolation20(guideData, weights, refFrame, cacheFile)
cache.interpolate()