# This file was auto generated by Styx.
# Do not edit this file directly.

import typing
import pathlib
from styxdefs import *

VOXEL2MESH_METADATA = Metadata(
    id="85ff2c9f04efcbe2d5484d2e2ccd18c1577521ed.boutiques",
    name="voxel2mesh",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_Voxel2meshConfigParamsDictNoTag = typing.TypedDict('_Voxel2meshConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
Voxel2meshConfigParamsDictTagged = typing.TypedDict('Voxel2meshConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
Voxel2meshConfigParamsDict = _Voxel2meshConfigParamsDictNoTag | Voxel2meshConfigParamsDictTagged


_Voxel2meshParamsDictNoTag = typing.TypedDict('_Voxel2meshParamsDictNoTag', {
    "blocky": bool,
    "threshold": typing.NotRequired[float | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Voxel2meshConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "input": InputPathType,
    "output": str,
})
Voxel2meshParamsDictTagged = typing.TypedDict('Voxel2meshParamsDictTagged', {
    "@type": typing.Literal["mrtrix/voxel2mesh"],
    "blocky": bool,
    "threshold": typing.NotRequired[float | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Voxel2meshConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "input": InputPathType,
    "output": str,
})
Voxel2meshParamsDict = _Voxel2meshParamsDictNoTag | Voxel2meshParamsDictTagged


def voxel2mesh_config(
    key: str,
    value: str,
) -> Voxel2meshConfigParamsDictTagged:
    """
    Build parameters.
    
    Args:
        key: temporarily set the value of an MRtrix config file entry.
        value: temporarily set the value of an MRtrix config file entry.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "config",
        "key": key,
        "value": value,
    }
    return params


def voxel2mesh_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Voxel2meshConfigParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("key", None) is None:
        raise StyxValidationError("`key` must not be None")
    if not isinstance(params["key"], str):
        raise StyxValidationError(f'`key` has the wrong type: Received `{type(params.get("key", None))}` expected `str`')
    if params.get("value", None) is None:
        raise StyxValidationError("`value` must not be None")
    if not isinstance(params["value"], str):
        raise StyxValidationError(f'`value` has the wrong type: Received `{type(params.get("value", None))}` expected `str`')


def voxel2mesh_config_cargs(
    params: Voxel2meshConfigParamsDict,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.append("-config")
    cargs.append(params.get("key", None))
    cargs.append(params.get("value", None))
    return cargs


class Voxel2meshOutputs(typing.NamedTuple):
    """
    Output object returned when calling `Voxel2meshParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    output: OutputPathType
    """the output mesh file."""


def voxel2mesh_params(
    input_: InputPathType,
    output: str,
    blocky: bool = False,
    threshold: float | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Voxel2meshConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> Voxel2meshParamsDictTagged:
    """
    Build parameters.
    
    Args:
        input_: the input image.
        output: the output mesh file.
        blocky: generate a 'blocky' mesh that precisely represents the voxel\
            edges.
        threshold: manually set the intensity threshold for the Marching Cubes\
            algorithm.
        info: display information messages.
        quiet: do not display information messages or progress status;\
            alternatively, this can be achieved by setting the MRTRIX_QUIET\
            environment variable to a non-empty string.
        debug: display debugging messages.
        force: force overwrite of output files (caution: using the same file as\
            input and output might cause unexpected behaviour).
        nthreads: use this number of threads in multi-threaded applications\
            (set to 0 to disable multi-threading).
        config: temporarily set the value of an MRtrix config file entry.
        help_: display this information page and exit.
        version: display version information and exit.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "mrtrix/voxel2mesh",
        "blocky": blocky,
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "input": input_,
        "output": output,
    }
    if threshold is not None:
        params["threshold"] = threshold
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def voxel2mesh_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Voxel2meshParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("blocky", False) is None:
        raise StyxValidationError("`blocky` must not be None")
    if not isinstance(params["blocky"], bool):
        raise StyxValidationError(f'`blocky` has the wrong type: Received `{type(params.get("blocky", False))}` expected `bool`')
    if params.get("threshold", None) is not None:
        if not isinstance(params["threshold"], (float, int)):
            raise StyxValidationError(f'`threshold` has the wrong type: Received `{type(params.get("threshold", None))}` expected `float | None`')
    if params.get("info", False) is None:
        raise StyxValidationError("`info` must not be None")
    if not isinstance(params["info"], bool):
        raise StyxValidationError(f'`info` has the wrong type: Received `{type(params.get("info", False))}` expected `bool`')
    if params.get("quiet", False) is None:
        raise StyxValidationError("`quiet` must not be None")
    if not isinstance(params["quiet"], bool):
        raise StyxValidationError(f'`quiet` has the wrong type: Received `{type(params.get("quiet", False))}` expected `bool`')
    if params.get("debug", False) is None:
        raise StyxValidationError("`debug` must not be None")
    if not isinstance(params["debug"], bool):
        raise StyxValidationError(f'`debug` has the wrong type: Received `{type(params.get("debug", False))}` expected `bool`')
    if params.get("force", False) is None:
        raise StyxValidationError("`force` must not be None")
    if not isinstance(params["force"], bool):
        raise StyxValidationError(f'`force` has the wrong type: Received `{type(params.get("force", False))}` expected `bool`')
    if params.get("nthreads", None) is not None:
        if not isinstance(params["nthreads"], int):
            raise StyxValidationError(f'`nthreads` has the wrong type: Received `{type(params.get("nthreads", None))}` expected `int | None`')
    if params.get("config", None) is not None:
        if not isinstance(params["config"], list):
            raise StyxValidationError(f'`config` has the wrong type: Received `{type(params.get("config", None))}` expected `list[Voxel2meshConfigParamsDict] | None`')
        for e in params["config"]:
            voxel2mesh_config_validate(e)
    if params.get("help", False) is None:
        raise StyxValidationError("`help` must not be None")
    if not isinstance(params["help"], bool):
        raise StyxValidationError(f'`help` has the wrong type: Received `{type(params.get("help", False))}` expected `bool`')
    if params.get("version", False) is None:
        raise StyxValidationError("`version` must not be None")
    if not isinstance(params["version"], bool):
        raise StyxValidationError(f'`version` has the wrong type: Received `{type(params.get("version", False))}` expected `bool`')
    if params.get("input", None) is None:
        raise StyxValidationError("`input` must not be None")
    if not isinstance(params["input"], (pathlib.Path, str)):
        raise StyxValidationError(f'`input` has the wrong type: Received `{type(params.get("input", None))}` expected `InputPathType`')
    if params.get("output", None) is None:
        raise StyxValidationError("`output` must not be None")
    if not isinstance(params["output"], str):
        raise StyxValidationError(f'`output` has the wrong type: Received `{type(params.get("output", None))}` expected `str`')


def voxel2mesh_cargs(
    params: Voxel2meshParamsDict,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.append("voxel2mesh")
    if params.get("blocky", False):
        cargs.append("-blocky")
    if params.get("threshold", None) is not None:
        cargs.extend([
            "-threshold",
            str(params.get("threshold", None))
        ])
    if params.get("info", False):
        cargs.append("-info")
    if params.get("quiet", False):
        cargs.append("-quiet")
    if params.get("debug", False):
        cargs.append("-debug")
    if params.get("force", False):
        cargs.append("-force")
    if params.get("nthreads", None) is not None:
        cargs.extend([
            "-nthreads",
            str(params.get("nthreads", None))
        ])
    if params.get("config", None) is not None:
        cargs.extend([a for c in [voxel2mesh_config_cargs(s, execution) for s in params.get("config", None)] for a in c])
    if params.get("help", False):
        cargs.append("-help")
    if params.get("version", False):
        cargs.append("-version")
    cargs.append(execution.input_file(params.get("input", None)))
    cargs.append(params.get("output", None))
    return cargs


def voxel2mesh_outputs(
    params: Voxel2meshParamsDict,
    execution: Execution,
) -> Voxel2meshOutputs:
    """
    Build outputs object containing output file paths and possibly stdout/stderr.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Outputs object.
    """
    ret = Voxel2meshOutputs(
        root=execution.output_file("."),
        output=execution.output_file(params.get("output", None)),
    )
    return ret


def voxel2mesh_execute(
    params: Voxel2meshParamsDict,
    runner: Runner | None = None,
) -> Voxel2meshOutputs:
    """
    voxel2mesh
    
    Generate a surface mesh representation from a voxel image.
    
    This command utilises the Marching Cubes algorithm to generate a polygonal
    surface that represents the isocontour(s) of the input image at a particular
    intensity. By default, an appropriate threshold will be determined
    automatically from the input image, however the intensity value of the
    isocontour(s) can instead be set manually using the -threhsold option.
    
    If the -blocky option is used, then the Marching Cubes algorithm will not be
    used. Instead, the input image will be interpreted as a binary mask image,
    and polygonal surfaces will be generated at the outer faces of the voxel
    clusters within the mask.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `Voxel2meshOutputs`).
    """
    voxel2mesh_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOXEL2MESH_METADATA)
    params = execution.params(params)
    cargs = voxel2mesh_cargs(params, execution)
    ret = voxel2mesh_outputs(params, execution)
    execution.run(cargs)
    return ret


def voxel2mesh(
    input_: InputPathType,
    output: str,
    blocky: bool = False,
    threshold: float | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Voxel2meshConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> Voxel2meshOutputs:
    """
    voxel2mesh
    
    Generate a surface mesh representation from a voxel image.
    
    This command utilises the Marching Cubes algorithm to generate a polygonal
    surface that represents the isocontour(s) of the input image at a particular
    intensity. By default, an appropriate threshold will be determined
    automatically from the input image, however the intensity value of the
    isocontour(s) can instead be set manually using the -threhsold option.
    
    If the -blocky option is used, then the Marching Cubes algorithm will not be
    used. Instead, the input image will be interpreted as a binary mask image,
    and polygonal surfaces will be generated at the outer faces of the voxel
    clusters within the mask.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        input_: the input image.
        output: the output mesh file.
        blocky: generate a 'blocky' mesh that precisely represents the voxel\
            edges.
        threshold: manually set the intensity threshold for the Marching Cubes\
            algorithm.
        info: display information messages.
        quiet: do not display information messages or progress status;\
            alternatively, this can be achieved by setting the MRTRIX_QUIET\
            environment variable to a non-empty string.
        debug: display debugging messages.
        force: force overwrite of output files (caution: using the same file as\
            input and output might cause unexpected behaviour).
        nthreads: use this number of threads in multi-threaded applications\
            (set to 0 to disable multi-threading).
        config: temporarily set the value of an MRtrix config file entry.
        help_: display this information page and exit.
        version: display version information and exit.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `Voxel2meshOutputs`).
    """
    params = voxel2mesh_params(
        blocky=blocky,
        threshold=threshold,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        input_=input_,
        output=output,
    )
    return voxel2mesh_execute(params, runner)


__all__ = [
    "VOXEL2MESH_METADATA",
    "Voxel2meshConfigParamsDict",
    "Voxel2meshConfigParamsDictTagged",
    "Voxel2meshOutputs",
    "Voxel2meshParamsDict",
    "Voxel2meshParamsDictTagged",
    "voxel2mesh",
    "voxel2mesh_config",
    "voxel2mesh_execute",
    "voxel2mesh_params",
]
