Component utils

scadpy.core.component.utils.blend_component_colors(components, get_component_color, get_component_magnitude)

Compute the weighted average (blend) of component colors, using each component’s magnitude as the weight.

This function is fully generic and uses dependency injection for all domain-specific operations, making it suitable for a wide range of applications (2D, 3D, CAD, etc.). Colors are expected to be RGBA sequences (length 4). If the total magnitude is zero, a default color is returned.

Parameters:
componentsSequence[C]

List of components whose colors will be blended.

get_component_colorCallable[[C], Color]

Function to extract the color from a component (as a list or tuple of 4 floats: RGBA).

get_component_magnitudeCallable[[C], float]

Function to extract a magnitude (e.g., area, volume) from a component for weighting.

Returns:
Color

The blended color as a list of 4 floats (RGBA).

Examples

>>> from scadpy import blend_component_colors, DEFAULT_COLOR
>>> components = [
...     {'color': [1, 0, 0, 1], 'magnitude': 1},
...     {'color': [0, 1, 0, 1], 'magnitude': 2}
... ]
...
>>> blend_component_colors(
...     components,
...     get_component_color=lambda c: c['color'],
...     get_component_magnitude=lambda c: c['magnitude']
... )
[0.3333333333333333, 0.6666666666666666, 0.0, 1.0]
>>> blend_component_colors(
...     [],
...     get_component_color=lambda c: c['color'],
...     get_component_magnitude=lambda c: c['magnitude']
... ) == DEFAULT_COLOR
True
scadpy.core.component.utils.get_intersecting_component_index_groups(components, get_component_bounds, are_components_intersecting)

Find groups of mutually intersecting components by their indices.

This function uses an R-tree for efficient spatial indexing and a graph traversal to group components that are directly or indirectly intersecting. It is fully generic and uses dependency injection for all domain-specific operations.

Parameters:
componentsSequence[C]

Sequence of components to group.

get_component_boundsCallable[[C], NDArray[np.float64]]

Function to extract the bounding box of a component (as [minx, miny, maxx, maxy]).

are_components_intersectingCallable[[C, C], bool]

Function to determine if two components intersect.

Returns:
list[list[int]]

A list of groups, each group being a list of indices into the original components sequence, where all components in a group are mutually intersecting (directly or indirectly).

Examples

>>> from scadpy import get_intersecting_component_index_groups
>>> components = [
...     {'bounds': [0, 0, 2, 2]},
...     {'bounds': [1, 1, 3, 3]},
...     {'bounds': [5, 5, 6, 6]}
... ]
...
>>> def are_intersecting(c1, c2):
...     b1, b2 = c1['bounds'], c2['bounds']
...     return not (b1[2] <= b2[0] or b2[2] <= b1[0] or
...                 b1[3] <= b2[1] or b2[3] <= b1[1])
...
>>> get_intersecting_component_index_groups(
...     components,
...     get_component_bounds=lambda c: c['bounds'],
...     are_components_intersecting=are_intersecting
... )
[[0, 1], [2]]
>>> get_intersecting_component_index_groups(
...     [],
...     get_component_bounds=lambda c: c['bounds'],
...     are_components_intersecting=are_intersecting
... )
[]