3
3
from __future__ import annotations
4
4
5
5
from math import isclose
6
- from typing import Tuple , Union
6
+ from typing import Optional , Tuple , Union
7
7
8
8
import numpy as np
9
9
24
24
]
25
25
26
26
27
- def flatten_groups (* geometries : GeometryType , flatten_nonunion_type : bool = False ) -> GeometryType :
27
+ def flatten_groups (
28
+ * geometries : GeometryType ,
29
+ flatten_nonunion_type : bool = False ,
30
+ flatten_transformed : bool = False ,
31
+ transform : Optional [MatrixReal4x4 ] = None ,
32
+ ) -> GeometryType :
28
33
"""Iterates over all geometries, flattening groups and unions.
29
34
30
35
Parameters
31
36
----------
32
37
*geometries : GeometryType
33
38
Geometries to flatten.
34
-
35
39
flatten_nonunion_type : bool = False
36
40
If ``False``, only flatten geometry unions (and ``GeometryGroup``). If ``True``, flatten
37
41
all clip operations.
42
+ flatten_transformed : bool = False
43
+ If ``True``, ``Transformed`` groups are flattened into individual transformed geometries.
44
+ transform : Optional[MatrixReal4x4]
45
+ Accumulated transform from parents. Only used when ``flatten_transformed`` is ``True``.
38
46
39
47
Yields
40
48
------
@@ -44,7 +52,10 @@ def flatten_groups(*geometries: GeometryType, flatten_nonunion_type: bool = Fals
44
52
for geometry in geometries :
45
53
if isinstance (geometry , base .GeometryGroup ):
46
54
yield from flatten_groups (
47
- * geometry .geometries , flatten_nonunion_type = flatten_nonunion_type
55
+ * geometry .geometries ,
56
+ flatten_nonunion_type = flatten_nonunion_type ,
57
+ flatten_transformed = flatten_transformed ,
58
+ transform = transform ,
48
59
)
49
60
elif isinstance (geometry , base .ClipOperation ) and (
50
61
flatten_nonunion_type or geometry .operation == "union"
@@ -53,7 +64,21 @@ def flatten_groups(*geometries: GeometryType, flatten_nonunion_type: bool = Fals
53
64
geometry .geometry_a ,
54
65
geometry .geometry_b ,
55
66
flatten_nonunion_type = flatten_nonunion_type ,
67
+ flatten_transformed = flatten_transformed ,
68
+ transform = transform ,
69
+ )
70
+ elif flatten_transformed and isinstance (geometry , base .Transformed ):
71
+ new_transform = geometry .transform
72
+ if transform is not None :
73
+ new_transform = np .matmul (transform , new_transform )
74
+ yield from flatten_groups (
75
+ geometry .geometry ,
76
+ flatten_nonunion_type = flatten_nonunion_type ,
77
+ flatten_transformed = flatten_transformed ,
78
+ transform = new_transform ,
56
79
)
80
+ elif flatten_transformed and transform is not None :
81
+ yield base .Transformed (geometry = geometry , transform = transform )
57
82
else :
58
83
yield geometry
59
84
0 commit comments