@@ -33,8 +33,11 @@ def _fread3(fobj):
33
33
n : int
34
34
A 3 byte int
35
35
"""
36
- b1 , b2 , b3 = np .fromfile (fobj , ">u1" , 3 )
37
- return (b1 << 16 ) + (b2 << 8 ) + b3
36
+ b = np .fromfile (fobj , ">u1" , 3 )
37
+ if len (b ) != 3 :
38
+ raise IOError ('Unexpected end of file when reading magic number.' )
39
+ return (int (b [0 ]) << 16 ) + (int (b [1 ]) << 8 ) + int (b [2 ])
40
+
38
41
39
42
40
43
def _fread3_many (fobj , n ):
@@ -48,9 +51,11 @@ def _fread3_many(fobj, n):
48
51
out : 1D array
49
52
An array of 3 byte int
50
53
"""
51
- b1 , b2 , b3 = np .fromfile (fobj , ">u1" , 3 * n ).reshape (- 1 , 3 ).astype (np .int64 ).T
52
- return (b1 << 16 ) + (b2 << 8 ) + b3
53
-
54
+ b = np .fromfile (fobj , ">u1" , 3 * n )
55
+ if len (b ) != 3 * n :
56
+ raise IOError ('Unexpected end of file when reading multiple 3-byte integers.' )
57
+ b = b .reshape (- 1 , 3 )
58
+ return (int (b [:, 0 ]) << 16 ) + (int (b [:, 1 ]) << 8 ) + int (b [:, 2 ])
54
59
55
60
def _read_geometry_fs (ipth , is_ascii = False ):
56
61
"""Adapted from nibabel. Add ascii support."""
@@ -60,17 +65,17 @@ def _read_geometry_fs(ipth, is_ascii=False):
60
65
re_header = re .compile ('^#!ascii version (.*)$' )
61
66
fname_header = re_header .match (fh .readline ()).group (1 )
62
67
63
- re_npoints_cells = re .compile ('[\s]*(\d+)[\s]*(\d+)[\s]*$' )
68
+ re_npoints_cells = re .compile (r '[\s]*(\d+)[\s]*(\d+)[\s]*$' )
64
69
re_n = re_npoints_cells .match (fh .readline ())
65
70
n_points , n_cells = int (re_n .group (1 )), int (re_n .group (2 ))
66
71
67
72
x_points = np .zeros ((n_points , 3 ))
68
73
for i in range (n_points ):
69
74
x_points [i , :] = [float (v ) for v in fh .readline ().split ()[:3 ]]
70
75
71
- x_cells = np .zeros ((n_cells , 3 ), dtype = np .uintp )
76
+ x_cells = np .zeros ((n_cells , 3 ), dtype = np .int32 )
72
77
for i in range (n_cells ):
73
- x_cells [i ] = [np .uintp (v ) for v in fh .readline ().split ()[:3 ]]
78
+ x_cells [i ] = [np .int32 (v ) for v in fh .readline ().split ()[:3 ]]
74
79
75
80
else :
76
81
with open (ipth , 'rb' ) as fh :
@@ -90,7 +95,7 @@ def _read_geometry_fs(ipth, is_ascii=False):
90
95
quads = _fread3_many (fh , n_quad * 4 )
91
96
quads = quads .reshape (n_quad , 4 )
92
97
n_cells = 2 * n_quad
93
- x_cells = np .zeros ((n_cells , 3 ), dtype = np .uintp )
98
+ x_cells = np .zeros ((n_cells , 3 ), dtype = np .int32 )
94
99
95
100
# Face splitting follows (Remove loop in nib) -> Not tested!
96
101
m0 = (quads [:, 0 ] % 2 ) == 0
@@ -107,7 +112,7 @@ def _read_geometry_fs(ipth, is_ascii=False):
107
112
x_points = np .fromfile (fh , '>f4' , n_points * 3 )
108
113
x_points = x_points .reshape (n_points , 3 ).astype (np .float64 )
109
114
110
- x_cells = np .zeros ((n_cells , 3 ), dtype = np .uintp )
115
+ x_cells = np .zeros ((n_cells , 3 ), dtype = np .int32 )
111
116
x_cells .flat [:] = np .fromfile (fh , '>i4' , n_cells * 3 )
112
117
113
118
return build_polydata (x_points , cells = x_cells ).VTKObject
@@ -123,7 +128,7 @@ def _write_geometry_fs(pd, opth, fname_header=None, is_ascii=False):
123
128
n_points , n_cells = pd .GetNumberOfPoints (), pd .GetNumberOfCells ()
124
129
x_points = np .zeros ((n_points , 4 ), dtype = np .float32 )
125
130
x_points [:, :3 ] = pd .GetPoints ()
126
- x_cells = np .zeros ((n_cells , 4 ), dtype = np .uintp )
131
+ x_cells = np .zeros ((n_cells , 4 ), dtype = np .int32 )
127
132
x_cells [:, :3 ] = pd .GetPolygons ().reshape (- 1 , 4 )[:, 1 :]
128
133
129
134
if is_ascii :
@@ -146,7 +151,9 @@ def _write_geometry_fs(pd, opth, fname_header=None, is_ascii=False):
146
151
147
152
with open (opth , 'wb' ) as fobj :
148
153
magic_bytes .tofile (fobj )
149
- fobj .write ('{0}%s\n \n ' .format (create_stamp ).encode ('utf-8' ))
154
+ # fobj.write('{0}%s\n\n'.format(create_stamp).encode('utf-8'))
155
+ fobj .write ((create_stamp + '\n ' ).encode ('utf-8' ))
156
+ fobj .write (b'\n ' )
150
157
151
158
np .array ([n_points , n_cells ], dtype = '>i4' ).tofile (fobj )
152
159
0 commit comments