Utilities¶
Helper functions for I/O, geometry operations, and more.
utilities
¶
Helper utilities for I/O, geometry operations, and more.
TerminalCommand
¶
Creates a new command container. Note that this immediately executes the command synchronously and writes the return values to the corresponding members.
Attributes:
| Name | Type | Description |
|---|---|---|
cmd |
The command to execute.
|
|
cwd |
The working directory to run the command in.
|
|
env |
The environment the command is run in.
|
|
Source code in src/compas_slicer/utilities/terminal_command.py
transfer_mesh_attributes_to_printpoints
¶
Transfers face and vertex attributes from the mesh to the printpoints. Each printpoint is projected to the closest mesh face. It takes directly all the face attributes. It takes the averaged vertex attributes of the face vertices using barycentric coordinates.
Face attributes can be anything. Vertex attributes can only be entities that support multiplication with a scalar. They have been tested to work with scalars and np.arrays.
The reserved attribute names (see 'is_reserved_attribute(attr)') are not passed on to the printpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh to transfer attributes from. |
required |
printpoints
|
PrintPointsCollection
|
The collection of printpoints to transfer attributes to. |
required |
Source code in src/compas_slicer/utilities/attributes_transfer.py
remap
¶
Bounded remap from source domain to target domain.
Source code in src/compas_slicer/utilities/utils.py
remap_unbound
¶
Remap input_val from source domain to target domain (no clamping).
Source code in src/compas_slicer/utilities/utils.py
get_output_directory
¶
Get or create 'output' directory in the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The path where the 'output' directory will be created. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The path to the 'output' directory. |
Source code in src/compas_slicer/utilities/utils.py
get_closest_pt_index
¶
Find the index of the closest point to pt in pts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pt
|
Point | NDArray
|
Query point. |
required |
pts
|
list[Point] | NDArray
|
Point cloud to search. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Index of the closest point. |
Source code in src/compas_slicer/utilities/utils.py
get_closest_pt
¶
Find the closest point to pt in pts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pt
|
Point | NDArray
|
Query point. |
required |
pts
|
list[Point]
|
Point cloud to search. |
required |
Returns:
| Type | Description |
|---|---|
Point
|
The closest point. |
Source code in src/compas_slicer/utilities/utils.py
pull_pts_to_mesh_faces
¶
Project points to mesh and find their closest face keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh to project onto. |
required |
points
|
list[Point]
|
Points to project. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[int], list[Point]]
|
Closest face keys and projected points. |
Source code in src/compas_slicer/utilities/utils.py
smooth_vectors
¶
Smooth vectors iteratively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vectors
|
list[Vector]
|
Vectors to smooth. |
required |
strength
|
float
|
Smoothing strength (0-1). |
required |
iterations
|
int
|
Number of smoothing iterations. |
required |
Returns:
| Type | Description |
|---|---|
list[Vector]
|
Smoothed vectors. |
Source code in src/compas_slicer/utilities/utils.py
save_to_json
¶
Save data to JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict | list
|
Data to save. |
required |
filepath
|
str | Path
|
Directory path. |
required |
name
|
str
|
Filename. |
required |
Source code in src/compas_slicer/utilities/utils.py
load_from_json
¶
Load data from JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Directory path. |
required |
name
|
str
|
Filename. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Loaded data. |
Source code in src/compas_slicer/utilities/utils.py
is_jsonable
¶
get_jsonable_attributes
¶
Convert attributes dict to JSON-serializable form.
Source code in src/compas_slicer/utilities/utils.py
save_to_text_file
¶
Save text to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
str
|
Text to save. |
required |
filepath
|
str | Path
|
Directory path. |
required |
name
|
str
|
Filename. |
required |
Source code in src/compas_slicer/utilities/utils.py
get_closest_mesh_vkey_to_pt
¶
Find the vertex key closest to the point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh. |
required |
pt
|
Point
|
Query point. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Closest vertex key. |
Source code in src/compas_slicer/utilities/utils.py
get_closest_mesh_normal_to_pt
¶
Find the closest vertex normal to the point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh. |
required |
pt
|
Point
|
Query point. |
required |
Returns:
| Type | Description |
|---|---|
Vector
|
Normal at closest vertex. |
Source code in src/compas_slicer/utilities/utils.py
get_mesh_vertex_coords_with_attribute
¶
Get coordinates of vertices where attribute equals value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh. |
required |
attr
|
str
|
Attribute name. |
required |
value
|
Any
|
Value to match. |
required |
Returns:
| Type | Description |
|---|---|
list[Point]
|
Points of matching vertices. |
Source code in src/compas_slicer/utilities/utils.py
get_normal_of_path_on_xy_plane
¶
Find the normal of the curve on xy plane at point with index k.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Index of the point. |
required |
point
|
Point
|
The point. |
required |
path
|
Path
|
The path containing the point. |
required |
mesh
|
Mesh
|
The mesh (fallback for degenerate cases). |
required |
Returns:
| Type | Description |
|---|---|
Vector
|
Normal vector. |
Source code in src/compas_slicer/utilities/utils.py
get_mesh_cotmatrix
¶
Get the cotangent Laplacian matrix of the mesh.
Computes L_ij = (cot α_ij + cot β_ij) / 2 for adjacent vertices, with L_ii = -sum_j L_ij (row sum = 0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh (must be triangulated). |
required |
fix_boundaries
|
bool
|
If True, zero out rows for boundary vertices. |
True
|
Returns:
| Type | Description |
|---|---|
csr_matrix
|
Sparse matrix (V x V), cotangent Laplacian. |
Source code in src/compas_slicer/utilities/utils.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
get_mesh_cotans
¶
Get the cotangent entries of the mesh.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh (must be triangulated). |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
F x 3 array of ½*cotangents for corresponding angles. Column i contains cotangent of angle at vertex i of each face. |
Source code in src/compas_slicer/utilities/utils.py
get_mesh_massmatrix
¶
Get the mass matrix of the mesh (Voronoi area weights).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh (must be triangulated). |
required |
Returns:
| Type | Description |
|---|---|
csr_matrix
|
Sparse diagonal matrix (V x V), vertex areas. |
Source code in src/compas_slicer/utilities/utils.py
point_list_to_dict
¶
Convert list of points/vectors to dict for JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pts_list
|
list[Point | Vector]
|
List of points or vectors. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, list[float]]
|
Dict mapping index to [x, y, z]. |
Source code in src/compas_slicer/utilities/utils.py
point_list_from_dict
¶
Convert dict of points to list of [x, y, z].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[Any, list[float]]
|
Dict mapping keys to [x, y, z]. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
List of [x, y, z] coordinates. |
Source code in src/compas_slicer/utilities/utils.py
flattened_list_of_dictionary
¶
Flatten dictionary values into a single list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dictionary
|
dict[Any, list[Any]]
|
Dictionary with list values. |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
Flattened list. |
Source code in src/compas_slicer/utilities/utils.py
get_dict_key_from_value
¶
Return the key of a dictionary that stores the value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dictionary
|
dict
|
The dictionary to search. |
required |
val
|
Any
|
Value to find. |
required |
Returns:
| Type | Description |
|---|---|
Any | None
|
The key, or None if not found. |
Source code in src/compas_slicer/utilities/utils.py
find_next_printpoint
¶
Returns the next printpoint from the current printpoint if it exists, otherwise returns None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
printpoints
|
PrintPointsCollection
|
The collection of printpoints. |
required |
i
|
int
|
Layer index. |
required |
j
|
int
|
Path index. |
required |
k
|
int
|
Printpoint index within the path. |
required |
Returns:
| Type | Description |
|---|---|
PrintPoint | None
|
The next printpoint or None if at the end. |
Source code in src/compas_slicer/utilities/utils.py
find_previous_printpoint
¶
Returns the previous printpoint from the current printpoint if it exists, otherwise returns None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
printpoints
|
PrintPointsCollection
|
The collection of printpoints. |
required |
i
|
int
|
Layer index. |
required |
j
|
int
|
Path index. |
required |
k
|
int
|
Printpoint index within the path. |
required |
Returns:
| Type | Description |
|---|---|
PrintPoint | None
|
The previous printpoint or None if at the start. |
Source code in src/compas_slicer/utilities/utils.py
interrupt
¶
Interrupts the flow of the code while it is running. It asks for the user to press a enter to continue or abort.
Source code in src/compas_slicer/utilities/utils.py
get_all_files_with_name
¶
Finds all the filenames in the DATA_PATH that start and end with the provided strings
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
startswith
|
str
|
|
required |
endswith
|
str
|
|
required |
DATA_PATH
|
str | Path
|
|
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
All the filenames |
Source code in src/compas_slicer/utilities/utils.py
check_package_is_installed
¶
Throws an error if igl python bindings are not installed in the current environment.
Source code in src/compas_slicer/utilities/utils.py
attributes_transfer
¶
transfer_mesh_attributes_to_printpoints
¶
Transfers face and vertex attributes from the mesh to the printpoints. Each printpoint is projected to the closest mesh face. It takes directly all the face attributes. It takes the averaged vertex attributes of the face vertices using barycentric coordinates.
Face attributes can be anything. Vertex attributes can only be entities that support multiplication with a scalar. They have been tested to work with scalars and np.arrays.
The reserved attribute names (see 'is_reserved_attribute(attr)') are not passed on to the printpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh to transfer attributes from. |
required |
printpoints
|
PrintPointsCollection
|
The collection of printpoints to transfer attributes to. |
required |
Source code in src/compas_slicer/utilities/attributes_transfer.py
is_reserved_attribute
¶
Returns True if the attribute name is a reserved, false otherwise.
transfer_mesh_attributes_to_point
¶
It projects the point on the closest face of the mesh. Then if finds all the vertex and face attributes of the face and its attributes and transfers them to the point. The vertex attributes are transferred using barycentric coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
|
required |
fkey
|
int
|
|
required |
proj_pt
|
list[float]
|
|
required |
Returns:
| Type | Description |
|---|---|
dict that contains all the attributes that correspond to the printpoints position
|
|
Source code in src/compas_slicer/utilities/attributes_transfer.py
terminal_command
¶
TerminalCommand class is used to run commands from python as if we are in a shell/cmd
TerminalCommand
¶
Creates a new command container. Note that this immediately executes the command synchronously and writes the return values to the corresponding members.
Attributes:
| Name | Type | Description |
|---|---|---|
cmd |
The command to execute.
|
|
cwd |
The working directory to run the command in.
|
|
env |
The environment the command is run in.
|
|
Source code in src/compas_slicer/utilities/terminal_command.py
utils
¶
remap
¶
Bounded remap from source domain to target domain.
Source code in src/compas_slicer/utilities/utils.py
remap_unbound
¶
Remap input_val from source domain to target domain (no clamping).
Source code in src/compas_slicer/utilities/utils.py
get_output_directory
¶
Get or create 'output' directory in the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The path where the 'output' directory will be created. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The path to the 'output' directory. |
Source code in src/compas_slicer/utilities/utils.py
get_closest_pt_index
¶
Find the index of the closest point to pt in pts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pt
|
Point | NDArray
|
Query point. |
required |
pts
|
list[Point] | NDArray
|
Point cloud to search. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Index of the closest point. |
Source code in src/compas_slicer/utilities/utils.py
get_closest_pt
¶
Find the closest point to pt in pts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pt
|
Point | NDArray
|
Query point. |
required |
pts
|
list[Point]
|
Point cloud to search. |
required |
Returns:
| Type | Description |
|---|---|
Point
|
The closest point. |
Source code in src/compas_slicer/utilities/utils.py
pull_pts_to_mesh_faces
¶
Project points to mesh and find their closest face keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh to project onto. |
required |
points
|
list[Point]
|
Points to project. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[int], list[Point]]
|
Closest face keys and projected points. |
Source code in src/compas_slicer/utilities/utils.py
smooth_vectors
¶
Smooth vectors iteratively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vectors
|
list[Vector]
|
Vectors to smooth. |
required |
strength
|
float
|
Smoothing strength (0-1). |
required |
iterations
|
int
|
Number of smoothing iterations. |
required |
Returns:
| Type | Description |
|---|---|
list[Vector]
|
Smoothed vectors. |
Source code in src/compas_slicer/utilities/utils.py
save_to_json
¶
Save data to JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict | list
|
Data to save. |
required |
filepath
|
str | Path
|
Directory path. |
required |
name
|
str
|
Filename. |
required |
Source code in src/compas_slicer/utilities/utils.py
load_from_json
¶
Load data from JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Directory path. |
required |
name
|
str
|
Filename. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Loaded data. |
Source code in src/compas_slicer/utilities/utils.py
is_jsonable
¶
get_jsonable_attributes
¶
Convert attributes dict to JSON-serializable form.
Source code in src/compas_slicer/utilities/utils.py
save_to_text_file
¶
Save text to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
str
|
Text to save. |
required |
filepath
|
str | Path
|
Directory path. |
required |
name
|
str
|
Filename. |
required |
Source code in src/compas_slicer/utilities/utils.py
check_triangular_mesh
¶
Check if mesh is triangular, raise TypeError if not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh to check. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any face is not a triangle. |
Source code in src/compas_slicer/utilities/utils.py
get_closest_mesh_vkey_to_pt
¶
Find the vertex key closest to the point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh. |
required |
pt
|
Point
|
Query point. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Closest vertex key. |
Source code in src/compas_slicer/utilities/utils.py
get_closest_mesh_normal_to_pt
¶
Find the closest vertex normal to the point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh. |
required |
pt
|
Point
|
Query point. |
required |
Returns:
| Type | Description |
|---|---|
Vector
|
Normal at closest vertex. |
Source code in src/compas_slicer/utilities/utils.py
get_mesh_vertex_coords_with_attribute
¶
Get coordinates of vertices where attribute equals value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh. |
required |
attr
|
str
|
Attribute name. |
required |
value
|
Any
|
Value to match. |
required |
Returns:
| Type | Description |
|---|---|
list[Point]
|
Points of matching vertices. |
Source code in src/compas_slicer/utilities/utils.py
get_normal_of_path_on_xy_plane
¶
Find the normal of the curve on xy plane at point with index k.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Index of the point. |
required |
point
|
Point
|
The point. |
required |
path
|
Path
|
The path containing the point. |
required |
mesh
|
Mesh
|
The mesh (fallback for degenerate cases). |
required |
Returns:
| Type | Description |
|---|---|
Vector
|
Normal vector. |
Source code in src/compas_slicer/utilities/utils.py
get_mesh_cotmatrix
¶
Get the cotangent Laplacian matrix of the mesh.
Computes L_ij = (cot α_ij + cot β_ij) / 2 for adjacent vertices, with L_ii = -sum_j L_ij (row sum = 0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh (must be triangulated). |
required |
fix_boundaries
|
bool
|
If True, zero out rows for boundary vertices. |
True
|
Returns:
| Type | Description |
|---|---|
csr_matrix
|
Sparse matrix (V x V), cotangent Laplacian. |
Source code in src/compas_slicer/utilities/utils.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
get_mesh_cotans
¶
Get the cotangent entries of the mesh.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh (must be triangulated). |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
F x 3 array of ½*cotangents for corresponding angles. Column i contains cotangent of angle at vertex i of each face. |
Source code in src/compas_slicer/utilities/utils.py
get_mesh_massmatrix
¶
Get the mass matrix of the mesh (Voronoi area weights).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh
|
The mesh (must be triangulated). |
required |
Returns:
| Type | Description |
|---|---|
csr_matrix
|
Sparse diagonal matrix (V x V), vertex areas. |
Source code in src/compas_slicer/utilities/utils.py
point_list_to_dict
¶
Convert list of points/vectors to dict for JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pts_list
|
list[Point | Vector]
|
List of points or vectors. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, list[float]]
|
Dict mapping index to [x, y, z]. |
Source code in src/compas_slicer/utilities/utils.py
point_list_from_dict
¶
Convert dict of points to list of [x, y, z].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[Any, list[float]]
|
Dict mapping keys to [x, y, z]. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
List of [x, y, z] coordinates. |
Source code in src/compas_slicer/utilities/utils.py
flattened_list_of_dictionary
¶
Flatten dictionary values into a single list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dictionary
|
dict[Any, list[Any]]
|
Dictionary with list values. |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
Flattened list. |
Source code in src/compas_slicer/utilities/utils.py
get_dict_key_from_value
¶
Return the key of a dictionary that stores the value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dictionary
|
dict
|
The dictionary to search. |
required |
val
|
Any
|
Value to find. |
required |
Returns:
| Type | Description |
|---|---|
Any | None
|
The key, or None if not found. |
Source code in src/compas_slicer/utilities/utils.py
find_next_printpoint
¶
Returns the next printpoint from the current printpoint if it exists, otherwise returns None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
printpoints
|
PrintPointsCollection
|
The collection of printpoints. |
required |
i
|
int
|
Layer index. |
required |
j
|
int
|
Path index. |
required |
k
|
int
|
Printpoint index within the path. |
required |
Returns:
| Type | Description |
|---|---|
PrintPoint | None
|
The next printpoint or None if at the end. |
Source code in src/compas_slicer/utilities/utils.py
find_previous_printpoint
¶
Returns the previous printpoint from the current printpoint if it exists, otherwise returns None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
printpoints
|
PrintPointsCollection
|
The collection of printpoints. |
required |
i
|
int
|
Layer index. |
required |
j
|
int
|
Path index. |
required |
k
|
int
|
Printpoint index within the path. |
required |
Returns:
| Type | Description |
|---|---|
PrintPoint | None
|
The previous printpoint or None if at the start. |
Source code in src/compas_slicer/utilities/utils.py
interrupt
¶
Interrupts the flow of the code while it is running. It asks for the user to press a enter to continue or abort.
Source code in src/compas_slicer/utilities/utils.py
get_all_files_with_name
¶
Finds all the filenames in the DATA_PATH that start and end with the provided strings
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
startswith
|
str
|
|
required |
endswith
|
str
|
|
required |
DATA_PATH
|
str | Path
|
|
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
All the filenames |
Source code in src/compas_slicer/utilities/utils.py
check_package_is_installed
¶
Throws an error if igl python bindings are not installed in the current environment.