11import math
22import os
33import tempfile
4+ from typing import Tuple
45
56import meshio
67import numpy
@@ -382,13 +383,13 @@ def remesh_surface(
382383 return mesh
383384
384385
385- def save_inr (vol , h , fname : str ):
386+ def save_inr (vol , voxel_size : Tuple [ float , float , float ] , fname : str ):
386387 """
387388 Save a volume (described as a numpy array) to INR format.
388389 Code inspired by iso2mesh (http://iso2mesh.sf.net) by Q. Fang
389390 INPUTS:
390- - vol: volume as numpy array
391- - h: voxel sizes as list or numpy array
391+ - vol: volume as a 3D numpy array
392+ - h: voxel sizes
392393 - fname: filename for saving the inr file
393394 """
394395 fid = open (fname , "wb" )
@@ -400,10 +401,23 @@ def save_inr(vol, h, fname: str):
400401 "float64" : ("float" , 64 ),
401402 }[vol .dtype .name ]
402403
403- header = (
404- "#INRIMAGE-4#{8:s}\n XDIM={0:d}\n YDIM={1:d}\n ZDIM={2:d}\n VDIM=1\n TYPE={3:s}\n "
405- + "PIXSIZE={4:d} bits\n CPU=decm\n VX={5:f}\n VY={6:f}\n VZ={7:f}\n "
406- ).format (* vol .shape , btype , bitlen , h [0 ], h [1 ], h [2 ], "{" )
404+ xdim , ydim , zdim = vol .shape
405+ header = "\n " .join (
406+ [
407+ "#INRIMAGE-4#{" ,
408+ f"XDIM={ xdim } " ,
409+ f"YDIM={ ydim } " ,
410+ f"ZDIM={ zdim } " ,
411+ "VDIM=1" ,
412+ f"TYPE={ btype } " ,
413+ f"PIXSIZE={ bitlen } bits" ,
414+ "CPU=decm" ,
415+ f"VX={ voxel_size [0 ]:f} " ,
416+ f"VY={ voxel_size [1 ]:f} " ,
417+ f"VZ={ voxel_size [2 ]:f} " ,
418+ ]
419+ )
420+ header += "\n "
407421
408422 header = header + "\n " * (256 - 4 - len (header )) + "##}\n "
409423
@@ -413,7 +427,7 @@ def save_inr(vol, h, fname: str):
413427
414428def generate_from_array (
415429 vol ,
416- h ,
430+ voxel_size : Tuple [ float , float , float ] ,
417431 lloyd : bool = False ,
418432 odt : bool = False ,
419433 perturb : bool = True ,
@@ -430,7 +444,7 @@ def generate_from_array(
430444 assert vol .dtype in ["uint8" , "uint16" ]
431445 fh , inr_filename = tempfile .mkstemp (suffix = ".inr" )
432446 os .close (fh )
433- save_inr (vol , h , inr_filename )
447+ save_inr (vol , voxel_size , inr_filename )
434448 mesh = generate_from_inr (
435449 inr_filename ,
436450 lloyd ,
0 commit comments