Skip to content

Commit 3c84dff

Browse files
filippo-biondindem0
authored andcommitted
Corrected parsing of mesh faces in binary format
1 parent a7fb3b0 commit 3c84dff

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

smithers/io/openfoam/mesh_parser.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,23 @@ def parse_faces_content(cls, content, is_binary, skip=10):
253253
if not is_binary:
254254
data = [[int(s) for s in re.findall(b"\d+", ln)[1:]] for ln in content[n + 2:n + 2 + num]]
255255
else:
256+
char_size = struct.calcsize('c')
257+
int_size = struct.calcsize('i')
258+
256259
buf = b''.join(content[n+1:])
257-
disp = struct.calcsize('c')
258-
idx = struct.unpack('{}i'.format(num), buf[disp:num*struct.calcsize('i') + disp])
259-
disp = 3*struct.calcsize('c') + 2*struct.calcsize('i')
260-
pp = struct.unpack('{}i'.format(idx[-1]),
261-
buf[disp+num*struct.calcsize('i'):
262-
disp+(num+idx[-1])*struct.calcsize('i')])
260+
buf = buf[char_size:] # skip the '(' at the beginning of the line
261+
262+
idx = struct.unpack('{}i'.format(num), buf[:num*int_size])
263+
264+
buf = buf[num*int_size:]
265+
# skip the character corresponding to the length of the second part which is
266+
# already present in idx[-1]
267+
buf = b''.join(buf.splitlines(True)[2:])
268+
buf = buf[char_size:] # skip the '(' at the beginning of the line
269+
270+
pp = list(struct.unpack('{}i'.format(idx[-1]),
271+
buf[:idx[-1]*int_size]))
272+
263273
data = []
264274
for i in range(num - 1):
265275
data.append(pp[idx[i]:idx[i+1]])

0 commit comments

Comments
 (0)