forked from CRBS/PyIMOD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImodWrite.py
217 lines (202 loc) · 11.7 KB
/
ImodWrite.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import struct
def ImodWrite(imodModel, fname):
with open(fname, mode = "wb") as fid:
writeModelHeader(imodModel, fid)
for iObject in range(0, imodModel.nObjects):
writeObjectHeader(imodModel, iObject, fid)
for iContour in range(0, imodModel.Objects[iObject].nContours):
writeContour(imodModel, iObject, iContour, fid)
for iMesh in range(0, imodModel.Objects[iObject].nMeshes):
writeMesh(imodModel, iObject, iMesh, fid)
writeIMAT(imodModel, iObject, fid)
if imodModel.Objects[iObject].mepa_set:
writeMEPA(imodModel, iObject, fid)
# Handles the case in which there is a 4 byte VIEW chunk before the
# main VIEW chunk. In this case, write the chunk title ('VIEW'), the
# number of bytes (4), and the cview value read from the file.
if imodModel.view_4bytes == 1:
fid.write('VIEW')
fid.write(struct.pack('>i', 4))
fid.write(struct.pack('>i', imodModel.view_4bytes_cview))
# Write the main VIEW chunk model-level header
if imodModel.view_set:
writeViewHeader(imodModel, fid)
# Write each object's VIEW chunk
for iObject in range(imodModel.view_objvsize):
if imodModel.Objects[iObject].Views:
writeView(imodModel, iObject, fid)
# Write MINX data, if it has been created or read.
if imodModel.minx_set:
writeMinx(imodModel, fid)
fid.write('IEOF')
fid.close()
def writeModelHeader(imodModel, fid):
tag = 'IMOD' + imodModel.version
nChar = len(imodModel.name)
nameStr = imodModel.name + struct.pack('>B', 0) * (128 - nChar)
fid.write(tag)
fid.write(nameStr)
fid.write(struct.pack('>i', imodModel.xMax))
fid.write(struct.pack('>i', imodModel.yMax))
fid.write(struct.pack('>i', imodModel.zMax))
fid.write(struct.pack('>i', imodModel.nObjects))
fid.write(struct.pack('>I', imodModel.flags))
fid.write(struct.pack('>i', imodModel.drawMode))
fid.write(struct.pack('>i', imodModel.mouseMode))
fid.write(struct.pack('>i', imodModel.blackLevel))
fid.write(struct.pack('>i', imodModel.whiteLevel))
fid.write(struct.pack('>f', imodModel.xOffset))
fid.write(struct.pack('>f', imodModel.yOffset))
fid.write(struct.pack('>f', imodModel.zOffset))
fid.write(struct.pack('>f', imodModel.xScale))
fid.write(struct.pack('>f', imodModel.yScale))
fid.write(struct.pack('>f', imodModel.zScale))
fid.write(struct.pack('>i', imodModel.object))
fid.write(struct.pack('>i', imodModel.contour))
fid.write(struct.pack('>i', imodModel.point))
fid.write(struct.pack('>i', imodModel.res))
fid.write(struct.pack('>i', imodModel.thresh))
fid.write(struct.pack('>f', imodModel.pixelSizeXY))
fid.write(struct.pack('>i', imodModel.units))
fid.write(struct.pack('>i', imodModel.csum))
fid.write(struct.pack('>f', imodModel.alpha))
fid.write(struct.pack('>f', imodModel.beta))
fid.write(struct.pack('>f', imodModel.gamma))
def writeObjectHeader(imodModel, iObject, fid):
fid.write('OBJT')
name = imodModel.Objects[iObject].name
nChar = len(name)
tagStr = name + struct.pack('>B', 0) * (128 - nChar)
fid.write(tagStr)
fid.write(struct.pack('>i', imodModel.Objects[iObject].nContours))
fid.write(struct.pack('>I', imodModel.Objects[iObject].flags))
fid.write(struct.pack('>i', imodModel.Objects[iObject].axis))
fid.write(struct.pack('>i', imodModel.Objects[iObject].drawMode))
fid.write(struct.pack('>f', imodModel.Objects[iObject].red))
fid.write(struct.pack('>f', imodModel.Objects[iObject].green))
fid.write(struct.pack('>f', imodModel.Objects[iObject].blue))
fid.write(struct.pack('>i', imodModel.Objects[iObject].pdrawsize))
fid.write(struct.pack('>B', imodModel.Objects[iObject].symbol))
fid.write(struct.pack('>B', imodModel.Objects[iObject].symbolSize))
fid.write(struct.pack('>B', imodModel.Objects[iObject].lineWidth2D))
fid.write(struct.pack('>B', imodModel.Objects[iObject].lineWidth3D))
fid.write(struct.pack('>B', imodModel.Objects[iObject].lineStyle))
fid.write(struct.pack('>B', imodModel.Objects[iObject].symbolFlags))
fid.write(struct.pack('>B', imodModel.Objects[iObject].sympad))
fid.write(struct.pack('>B', imodModel.Objects[iObject].transparency))
fid.write(struct.pack('>i', imodModel.Objects[iObject].nMeshes))
fid.write(struct.pack('>i', imodModel.Objects[iObject].nSurfaces))
def writeContour(imodModel, iObject, iContour, fid):
tagStr = 'CONT'
fid.write(tagStr)
fid.write(struct.pack('>i', imodModel.Objects[iObject].Contours[iContour].nPoints))
try:
fid.write(struct.pack('>I', imodModel.Objects[iObject].Contours[iContour].flags))
except:
fid.write(struct.pack('>I', 0))
fid.write(struct.pack('>i', imodModel.Objects[iObject].Contours[iContour].type))
fid.write(struct.pack('>i', imodModel.Objects[iObject].Contours[iContour].iSurface))
fid.write("".join([struct.pack('>f', x) for x in imodModel.Objects[iObject].Contours[iContour].points]))
def writeMesh(imodModel, iObject, iMesh, fid):
tagStr = 'MESH'
fid.write(tagStr)
fid.write(struct.pack('>i', imodModel.Objects[iObject].Meshes[iMesh].nVertices))
fid.write(struct.pack('>i', imodModel.Objects[iObject].Meshes[iMesh].nIndices))
fid.write(struct.pack('>I', imodModel.Objects[iObject].Meshes[iMesh].flag))
fid.write(struct.pack('>h', imodModel.Objects[iObject].Meshes[iMesh].type))
fid.write(struct.pack('>h', imodModel.Objects[iObject].Meshes[iMesh].pad))
fid.write("".join([struct.pack('>f', x) for x in imodModel.Objects[iObject].Meshes[iMesh].vertices]))
fid.write("".join([struct.pack('>i', x) for x in imodModel.Objects[iObject].Meshes[iMesh].indices]))
def writeIMAT(imodModel, iObject, fid):
tagStr = 'IMAT'
fid.write(tagStr)
fid.write(struct.pack('>i', 16))
fid.write(struct.pack('>B', imodModel.Objects[iObject].ambient))
fid.write(struct.pack('>B', imodModel.Objects[iObject].diffuse))
fid.write(struct.pack('>B', imodModel.Objects[iObject].specular))
fid.write(struct.pack('>B', imodModel.Objects[iObject].shininess))
fid.write(struct.pack('>B', imodModel.Objects[iObject].fillred))
fid.write(struct.pack('>B', imodModel.Objects[iObject].fillgreen))
fid.write(struct.pack('>B', imodModel.Objects[iObject].fillblue))
fid.write(struct.pack('>B', imodModel.Objects[iObject].quality))
fid.write(struct.pack('>l', imodModel.Objects[iObject].mat2))
fid.write(struct.pack('>B', imodModel.Objects[iObject].valblack))
fid.write(struct.pack('>B', imodModel.Objects[iObject].valwhite))
fid.write(struct.pack('>B', imodModel.Objects[iObject].matflags2))
fid.write(struct.pack('>B', imodModel.Objects[iObject].mat3b3))
def writeMEPA(imodModel, iObject, fid):
fid.write('MEPA')
fid.write(struct.pack('>i', imodModel.Objects[iObject].mepa_nBytes))
[fid.write(struct.pack('>B', x)) for x in imodModel.Objects[iObject].mepa_byteString]
def writeViewHeader(imodModel, fid):
fid.write('VIEW')
fid.write(struct.pack('>i', 184 + imodModel.nObjects * 187))
fid.write(struct.pack('>f', imodModel.view_fovy))
fid.write(struct.pack('>f', imodModel.view_rad))
fid.write(struct.pack('>f', imodModel.view_aspect))
fid.write(struct.pack('>f', imodModel.view_cnear))
fid.write(struct.pack('>f', imodModel.view_cfar))
fid.write(struct.pack('>f', imodModel.view_rot_x))
fid.write(struct.pack('>f', imodModel.view_rot_y))
fid.write(struct.pack('>f', imodModel.view_rot_z))
fid.write(struct.pack('>f', imodModel.view_trans_x))
fid.write(struct.pack('>f', imodModel.view_trans_y))
fid.write(struct.pack('>f', imodModel.view_trans_z))
fid.write(struct.pack('>f', imodModel.view_scale_x))
fid.write(struct.pack('>f', imodModel.view_scale_y))
fid.write(struct.pack('>f', imodModel.view_scale_z))
[fid.write(struct.pack('>f', x)) for x in imodModel.view_mat]
fid.write(struct.pack('>i', imodModel.view_world))
tag = imodModel.view_label
nChar = len(tag)
fid.write(tag + struct.pack('>B', 0) * (32 - nChar))
fid.write(struct.pack('>f', imodModel.view_dcstart))
fid.write(struct.pack('>f', imodModel.view_dcend))
fid.write(struct.pack('>f', imodModel.view_lightx))
fid.write(struct.pack('>f', imodModel.view_lighty))
fid.write(struct.pack('>f', imodModel.view_plax))
fid.write(struct.pack('>i', imodModel.view_objvsize)) #objvsize
fid.write(struct.pack('>i', imodModel.view_objvsize * 187)) #bytesObjv
def writeView(imodModel, iObject, fid):
fid.write(struct.pack('>I', imodModel.Objects[iObject].Views[0].flags))
fid.write(struct.pack('>f', imodModel.Objects[iObject].Views[0].red))
fid.write(struct.pack('>f', imodModel.Objects[iObject].Views[0].green))
fid.write(struct.pack('>f', imodModel.Objects[iObject].Views[0].blue))
fid.write(struct.pack('>i', imodModel.Objects[iObject].Views[0].pdrawsize))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].linewidth))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].linesty))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].trans))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].clips_count))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].clips_flags))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].clips_trans))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].clips_plane))
fid.write(struct.pack('>f', imodModel.Objects[iObject].Views[0].clips_normal_x))
fid.write(struct.pack('>f', imodModel.Objects[iObject].Views[0].clips_normal_y))
fid.write(struct.pack('>f', imodModel.Objects[iObject].Views[0].clips_normal_z))
fid.write(struct.pack('>f', imodModel.Objects[iObject].Views[0].clips_points_x))
fid.write(struct.pack('>f', imodModel.Objects[iObject].Views[0].clips_points_y))
fid.write(struct.pack('>f', imodModel.Objects[iObject].Views[0].clips_points_z))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].ambient))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].diffuse))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].specular))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].shininess))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].fillred))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].fillgreen))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].fillblue))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].quality))
fid.write(struct.pack('>I', imodModel.Objects[iObject].Views[0].mat2))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].valblack))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].valwhite))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].mat3b2))
fid.write(struct.pack('>B', imodModel.Objects[iObject].Views[0].mat3b3))
[fid.write(struct.pack('>f', x)) for x in imodModel.Objects[iObject].Views[0].clips_normal]
[fid.write(struct.pack('>f', x)) for x in imodModel.Objects[iObject].Views[0].clips_point]
def writeMinx(imodModel, fid):
fid.write('MINX')
fid.write(struct.pack('>i', 72))
[fid.write(struct.pack('>f', x)) for x in imodModel.minx_oscale]
[fid.write(struct.pack('>f', x)) for x in imodModel.minx_otrans]
[fid.write(struct.pack('>f', x)) for x in imodModel.minx_orot]
[fid.write(struct.pack('>f', x)) for x in imodModel.minx_cscale]
[fid.write(struct.pack('>f', x)) for x in imodModel.minx_ctrans]
[fid.write(struct.pack('>f', x)) for x in imodModel.minx_crot]