Mesh 3D¶
3D surface mesh generation from segmentation volumes and snapshot rendering.
Snapshot 3D¶
TPTBox.mesh3D.snapshot3D
¶
make_snapshot3D
¶
make_snapshot3D(img: Image_Reference, output_path: str | Path | None, view: VIEW | list[VIEW] = 'A', ids_list: list[Sequence[int]] | None = None, smoothing: int = 20, resolution: float | None = None, width_factor: float = 1.0, scale_factor: int = 1, verbose: bool = True, crop: bool = True, png_magnify: int = 1) -> Image.Image
Generate a 3D surface-rendered snapshot from a segmentation image.
Renders each label in the segmentation with its ITK color, arranges the specified views side-by-side, and saves the composite image as a PNG.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Image_Reference
|
Source segmentation image reference (NIfTI path, NII, etc.). |
required |
output_path
|
str | Path | None
|
Destination PNG path. If None, a temporary file is used and the resulting image is returned without being saved permanently. |
required |
view
|
VIEW | list[VIEW]
|
Camera direction(s) for the render. Accepts a single direction
string or a list. Valid values: |
'A'
|
ids_list
|
list[Sequence[int]] | None
|
Per-view lists of label IDs to render. If None, all unique non-zero labels are used for every view. |
None
|
smoothing
|
int
|
Number of VTK smoothing iterations applied to each surface. |
20
|
resolution
|
float | None
|
Isotropic voxel size (mm) to resample to before rendering. Defaults to the minimum zoom of the image. |
None
|
width_factor
|
float
|
Multiplier applied to the per-view pixel width. |
1.0
|
scale_factor
|
int
|
PNG magnification factor passed to fury's record function. |
1
|
verbose
|
bool
|
If True, logs the output path after saving. |
True
|
crop
|
bool
|
If True, crops the image to its bounding box before rendering. |
True
|
png_magnify
|
int
|
Window pixel density multiplier for the fury renderer. |
1
|
Returns:
| Type | Description |
|---|---|
Image
|
The rendered snapshot as a PIL Image object. |
Source code in TPTBox/mesh3D/snapshot3D.py
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 | |
make_snapshot3D_parallel
¶
make_snapshot3D_parallel(imgs: list[Image_Reference], output_paths: list[Path | str], view: VIEW | list[VIEW] = 'A', ids_list: list[Sequence[int]] | None = None, smoothing: int = 20, resolution: float = 1, cpus: int = 10, width_factor: float = 1.0, png_magnify: int = 1, scale_factor: int = 1, override: bool = True, crop: bool = True) -> None
Run :func:make_snapshot3D in parallel across multiple images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
imgs
|
list[Image_Reference]
|
List of segmentation image references to render. |
required |
output_paths
|
list[Path | str]
|
Destination PNG paths, one per image in |
required |
view
|
VIEW | list[VIEW]
|
Camera direction(s) forwarded to :func: |
'A'
|
ids_list
|
list[Sequence[int]] | None
|
Per-view label ID lists forwarded to :func: |
None
|
smoothing
|
int
|
VTK smoothing iterations forwarded to :func: |
20
|
resolution
|
float
|
Isotropic voxel size (mm) for resampling. |
1
|
cpus
|
int
|
Number of worker processes in the multiprocessing pool. |
10
|
width_factor
|
float
|
Per-view width multiplier. |
1.0
|
png_magnify
|
int
|
Window pixel density multiplier. |
1
|
scale_factor
|
int
|
PNG magnification factor. |
1
|
override
|
bool
|
If False, skips images whose output file already exists. |
True
|
crop
|
bool
|
If True, crops each image to its bounding box before rendering. |
True
|
Source code in TPTBox/mesh3D/snapshot3D.py
Mesh¶
TPTBox.mesh3D.mesh
¶
MeshOutputType
¶
Mesh3D
¶
Wrapper around a pyvista PolyData mesh providing save, load, and display utilities.
Source code in TPTBox/mesh3D/mesh.py
save
¶
save(filepath: str | Path, mode: MeshOutputType = MeshOutputType.PLY, verbose: logging = True) -> None
Save the mesh to disk in the specified format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Destination file path. The appropriate extension is appended if absent. |
required |
mode
|
MeshOutputType
|
Output format. Currently only PLY is supported. |
PLY
|
verbose
|
logging
|
If True, prints a confirmation message after saving. |
True
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the parent directory of |
NotImplementedError
|
If |
Source code in TPTBox/mesh3D/mesh.py
load
classmethod
¶
Load a mesh from a file supported by pyvista.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Path to the mesh file (e.g. PLY, OBJ, VTK). |
required |
Returns:
| Type | Description |
|---|---|
Mesh3D
|
A new |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in TPTBox/mesh3D/mesh.py
show
¶
Display the mesh interactively in a pyvista window with a black background.
Source code in TPTBox/mesh3D/mesh.py
save_to_html
¶
Export the mesh as an interactive HTML file via pyvista.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_output
|
str | Path
|
Destination path for the HTML file. |
required |
Source code in TPTBox/mesh3D/mesh.py
SegmentationMesh
¶
Bases: Mesh3D
Mesh generated from a segmentation volume using the marching-cubes algorithm.
Source code in TPTBox/mesh3D/mesh.py
get_mesh_with_offset
¶
Return a copy of the mesh with all vertices shifted by the given offset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
tuple[float, float, float]
|
A (x, y, z) translation vector applied to every vertex. |
required |
Returns:
| Type | Description |
|---|---|
PolyData
|
A new pyvista PolyData mesh with shifted vertex positions. |
Source code in TPTBox/mesh3D/mesh.py
from_segmentation_nii
classmethod
¶
Construct a SegmentationMesh from a NIfTI segmentation image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seg_nii
|
NII
|
A NIfTI segmentation image. Must have |
required |
rescale_to_iso
|
bool
|
If True, resamples the image to isotropic voxel spacing before extracting the surface mesh. |
True
|
Returns:
| Type | Description |
|---|---|
SegmentationMesh
|
A new |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in TPTBox/mesh3D/mesh.py
POIMesh
¶
Bases: Mesh3D
Mesh constructed from a set of POI (point-of-interest) coordinates rendered as spheres.
Source code in TPTBox/mesh3D/mesh.py
get_mesh_with_offset
¶
Return a copy of the glyph mesh with all POI positions shifted by the given offset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
tuple[float, float, float]
|
A (x, y, z) translation vector applied to every point-of-interest coordinate. |
required |
Returns:
| Type | Description |
|---|---|
PolyData
|
A new pyvista PolyData glyph mesh with shifted sphere positions. |
Source code in TPTBox/mesh3D/mesh.py
Mesh Colors¶
TPTBox.mesh3D.mesh_colors
¶
RGB_Color
¶
An RGB color stored as a NumPy integer array with helpers for normalized access.
Source code in TPTBox/mesh3D/mesh_colors.py
init_separate
classmethod
¶
Construct an RGB_Color from three separate channel values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
int
|
Red channel (0–255). |
required |
g
|
int
|
Green channel (0–255). |
required |
b
|
int
|
Blue channel (0–255). |
required |
Returns:
| Type | Description |
|---|---|
RGB_Color
|
A new |
Source code in TPTBox/mesh3D/mesh_colors.py
init_list
classmethod
¶
Construct an RGB_Color from a list or NumPy array of three integers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rgb
|
list[int] | ndarray
|
Sequence of three integers (R, G, B) each in the range 0–255. |
required |
Returns:
| Type | Description |
|---|---|
RGB_Color
|
A new |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in TPTBox/mesh3D/mesh_colors.py
Mesh_Color_List
¶
Catalog of named RGB colors used for anatomical mesh visualization.
Contains general-purpose color constants and 201 ITK-style colors (ITK_1
through ITK_201) matching the ITK-SNAP color table convention.
Source code in TPTBox/mesh3D/mesh_colors.py
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
get_color_by_label
¶
Return the RGB_Color assigned to a given integer label.
Labels 1–149 have a fixed ITK-style color. Labels outside that range are wrapped modulo 50 to stay within the defined palette.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
int
|
Integer segmentation label (must be >= 1). |
required |
Returns:
| Type | Description |
|---|---|
RGB_Color
|
The |
Source code in TPTBox/mesh3D/mesh_colors.py
write_ctbl
¶
Write the ITK color table to a 3D Slicer-compatible .ctbl file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Destination file path. Defaults to |
'ITK_ColorTable.ctbl'
|
Source code in TPTBox/mesh3D/mesh_colors.py
HTML Preview¶
TPTBox.mesh3D.html_preview
¶
Preview_Settings
dataclass
¶
Configuration for visualizing a NII or POI object as a mesh.
Attributes:
| Name | Type | Description |
|---|---|---|
obj |
NII | POI
|
The image or point-of-interest object to visualize. |
offset |
tuple[float, float, float] | None
|
Optional (PIR) spatial offset for rendering. |
opacity |
float
|
Mesh opacity value between 0 and 1. |
color |
Literal['auto'] | str | None
|
Desired mesh color. Defaults to "auto". |
binary |
bool
|
Whether to render the object as a binary segmentation. |
Source code in TPTBox/mesh3D/html_preview.py
make_html_preview
¶
make_html_preview(images: list[NII | POI | Preview_Settings], html_out: str | Path | None, background='black', rescale_to_iso=False, poi_size=1.7, logger=l, show=False, default_color_nii='bisque', default_poi_nii='red', ref_spacing: Has_Grid | None = None, auto_rescale_to_ref=False) -> None
Render NII or POI objects as meshes in an interactive 3D HTML viewer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
list[NII | POI | Preview_Settings]
|
List of images or wrapped settings to visualize. |
required |
html_out
|
str | Path | None
|
Output file path for HTML export. Must end in |
required |
background
|
str
|
Background color of the 3D viewer. |
'black'
|
rescale_to_iso
|
bool
|
Whether to rescale NII images to isotropic voxel spacing. |
False
|
poi_size
|
float
|
Size factor for point-of-interest rendering. |
1.7
|
logger
|
Print_Logger
|
Logger for output messages. |
l
|
show
|
bool
|
If True, shows the viewer after rendering. |
False
|
default_color_nii
|
str
|
Default color for NII-based visualizations. |
'bisque'
|
default_poi_nii
|
str
|
Default color for POI-based visualizations. |
'red'
|
ref_spacing
|
Has_Grid | None
|
Optional reference object to resample all images to a common spacing. |
None
|
auto_rescale_to_ref
|
bool
|
Whether to resample all objects to the reference spacing automatically. |
False
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |