-
Notifications
You must be signed in to change notification settings - Fork 439
/
composite.py
200 lines (155 loc) · 6.3 KB
/
composite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
"""Filters module with a class to manage filters/algorithms for composite datasets."""
import numpy as np
import pyvista
from pyvista import _vtk, abstract_class, wrap
from pyvista.core.filters import _get_output, _update_alg
from pyvista.core.filters.data_set import DataSetFilters
@abstract_class
class CompositeFilters:
"""An internal class to manage filters/algorithms for composite datasets."""
def extract_geometry(self):
"""Extract the surface the geometry of all blocks.
Place this filter at the end of a pipeline before a polydata
consumer such as a polydata mapper to extract geometry from
all blocks and append them to one polydata object.
Returns
-------
pyvista.PolyData
Surface of the composite dataset.
"""
gf = _vtk.vtkCompositeDataGeometryFilter()
gf.SetInputData(self)
gf.Update()
return wrap(gf.GetOutputDataObject(0))
def combine(self, merge_points=False, tolerance=0.0):
"""Combine all blocks into a single unstructured grid.
Parameters
----------
merge_points : bool, default: False
Merge coincidental points.
tolerance : float, default: 0.0
The absolute tolerance to use to find coincident points when
``merge_points=True``.
Returns
-------
pyvista.UnstructuredGrid
Combined blocks.
Examples
--------
Combine blocks within a multiblock without merging points.
>>> import pyvista
>>> block = pyvista.MultiBlock(
... [
... pyvista.Cube(clean=False),
... pyvista.Cube(center=(1, 0, 0), clean=False),
... ]
... )
>>> merged = block.combine()
>>> merged.n_points
48
Combine blocks and merge points
>>> merged = block.combine(merge_points=True)
>>> merged.n_points
12
"""
alg = _vtk.vtkAppendFilter()
for block in self:
if isinstance(block, _vtk.vtkMultiBlockDataSet):
block = CompositeFilters.combine(
block, merge_points=merge_points, tolerance=tolerance
)
alg.AddInputData(block)
alg.SetMergePoints(merge_points)
alg.SetTolerance(tolerance)
alg.Update()
return wrap(alg.GetOutputDataObject(0))
clip = DataSetFilters.clip
clip_box = DataSetFilters.clip_box
slice = DataSetFilters.slice
slice_orthogonal = DataSetFilters.slice_orthogonal
slice_along_axis = DataSetFilters.slice_along_axis
slice_along_line = DataSetFilters.slice_along_line
extract_all_edges = DataSetFilters.extract_all_edges
elevation = DataSetFilters.elevation
compute_cell_sizes = DataSetFilters.compute_cell_sizes
cell_centers = DataSetFilters.cell_centers
cell_data_to_point_data = DataSetFilters.cell_data_to_point_data
point_data_to_cell_data = DataSetFilters.point_data_to_cell_data
triangulate = DataSetFilters.triangulate
def outline(self, generate_faces=False, nested=False, progress_bar=False):
"""Produce an outline of the full extent for the all blocks in this composite dataset.
Parameters
----------
generate_faces : bool, default: False
Generate solid faces for the box.
nested : bool, default: False
If ``True``, these creates individual outlines for each nested dataset.
progress_bar : bool, default: False
Display a progress bar to indicate progress.
Returns
-------
pyvista.PolyData
Mesh containing the outline.
"""
if nested:
return DataSetFilters.outline(
self, generate_faces=generate_faces, progress_bar=progress_bar
)
box = pyvista.Box(bounds=self.bounds)
return box.outline(generate_faces=generate_faces, progress_bar=progress_bar)
def outline_corners(self, factor=0.2, nested=False, progress_bar=False):
"""Produce an outline of the corners for the all blocks in this composite dataset.
Parameters
----------
factor : float, default: 0.2
Controls the relative size of the corners to the length of
the corresponding bounds.
nested : bool, default: False
If ``True``, these creates individual outlines for each nested dataset.
progress_bar : bool, default: False
Display a progress bar to indicate progress.
Returns
-------
pyvista.PolyData
Mesh containing outlined corners.
"""
if nested:
return DataSetFilters.outline_corners(self, factor=factor, progress_bar=progress_bar)
box = pyvista.Box(bounds=self.bounds)
return box.outline_corners(factor=factor, progress_bar=progress_bar)
def _compute_normals(
self,
cell_normals=True,
point_normals=True,
split_vertices=False,
flip_normals=False,
consistent_normals=True,
auto_orient_normals=False,
non_manifold_traversal=True,
feature_angle=30.0,
track_vertices=False,
progress_bar=False,
):
"""Compute point and/or cell normals for a multi-block dataset."""
if not self.is_all_polydata:
raise RuntimeError(
'This multiblock contains non-PolyData datasets. Convert all the '
'datasets to PolyData with `as_polydata`'
)
# track original point indices
if split_vertices and track_vertices:
for block in self:
ids = np.arange(block.n_points, dtype=pyvista.ID_TYPE)
block.point_data.set_array(ids, 'pyvistaOriginalPointIds')
alg = _vtk.vtkPolyDataNormals()
alg.SetComputeCellNormals(cell_normals)
alg.SetComputePointNormals(point_normals)
alg.SetSplitting(split_vertices)
alg.SetFlipNormals(flip_normals)
alg.SetConsistency(consistent_normals)
alg.SetAutoOrientNormals(auto_orient_normals)
alg.SetNonManifoldTraversal(non_manifold_traversal)
alg.SetFeatureAngle(feature_angle)
alg.SetInputData(self)
_update_alg(alg, progress_bar, 'Computing Normals')
return _get_output(alg)