11import numpy as np
2- import vtk
32import pyvista as pv
4- from vtk .util import numpy_support
53from skimage .measure import marching_cubes
64
5+ # Import only the required VTK classes from vtkmodules to avoid
6+ # pulling in optional IO backends (e.g., NetCDF) via umbrella import.
7+ from vtkmodules .vtkFiltersSources import vtkPlaneSource
8+ from vtkmodules .vtkCommonTransforms import vtkTransform
9+ from vtkmodules .vtkFiltersCore import (
10+ vtkAppendFilter ,
11+ vtkThreshold ,
12+ vtkMarchingSquares ,
13+ )
14+ from vtkmodules .vtkFiltersGeometry import vtkGeometryFilter
15+ from vtkmodules .vtkFiltersGeneral import vtkTransformFilter , vtkTransformPolyDataFilter
16+ from vtkmodules .vtkCommonDataModel import (
17+ vtkUnstructuredGrid ,
18+ vtkDataObject ,
19+ vtkUniformGrid ,
20+ vtkImageData ,
21+ )
22+ from vtkmodules .util import numpy_support
23+
724def descritize (domain , ** kwargs ):
825 """
926 This function evaluates the implicit representation
@@ -85,16 +102,16 @@ def threshold_2d(function, resolution, origin, scale, upper, lower):
85102
86103 """
87104 # Build a plane source to evaluate the function
88- plane = vtk . vtkPlaneSource ()
105+ plane = vtkPlaneSource ()
89106 plane .SetXResolution (resolution )
90107 plane .SetYResolution (resolution )
91108 plane .Update ()
92109 # Define the transform to scale the plane
93- transform = vtk . vtkTransform ()
110+ transform = vtkTransform ()
94111 transform .Scale (scale [0 ], scale [1 ], 1 )
95112 transform .Translate (origin [0 ], origin [1 ], origin [2 ])
96113 # Apply the transform to the plane
97- transform_filter = vtk . vtkTransformPolyDataFilter ()
114+ transform_filter = vtkTransformPolyDataFilter ()
98115 transform_filter .SetInputData (plane .GetOutput ())
99116 transform_filter .SetTransform (transform )
100117 transform_filter .Update ()
@@ -107,19 +124,19 @@ def threshold_2d(function, resolution, origin, scale, upper, lower):
107124 # Set implicit values to the plane
108125 plane .GetPointData ().SetScalars (values )
109126 # Create an UnstructuredGrid from the PolyData
110- polydata_to_unstructured_grid = vtk . vtkAppendFilter ()
127+ polydata_to_unstructured_grid = vtkAppendFilter ()
111128 polydata_to_unstructured_grid .AddInputData (plane )
112129 polydata_to_unstructured_grid .Update ()
113- unstructured_grid = vtk . vtkUnstructuredGrid ()
130+ unstructured_grid = vtkUnstructuredGrid ()
114131 unstructured_grid .ShallowCopy (polydata_to_unstructured_grid .GetOutput ())
115132 # Threshold the plane
116- threshold = vtk . vtkThreshold ()
133+ threshold = vtkThreshold ()
117134 threshold .SetInputData (unstructured_grid )
118135 threshold .ThresholdBetween (lower , upper )
119- threshold .SetInputArrayToProcess (0 , 0 , 0 , vtk . vtkDataObject .FIELD_ASSOCIATION_POINTS , "ImplicitFunctionValue" )
136+ threshold .SetInputArrayToProcess (0 , 0 , 0 , vtkDataObject .FIELD_ASSOCIATION_POINTS , "ImplicitFunctionValue" )
120137 threshold .Update ()
121138 # Obtain the mesh
122- geometry_filter = vtk . vtkGeometryFilter ()
139+ geometry_filter = vtkGeometryFilter ()
123140 geometry_filter .SetInputData (threshold .GetOutput ())
124141 geometry_filter .Update ()
125142 mesh = geometry_filter .GetOutput ()
@@ -163,14 +180,14 @@ def threshold_3d(function, resolution, origin, scale, upper, lower):
163180
164181 """
165182 # Build the structured grid for thresholding
166- uniform_grid = vtk . vtkUniformGrid ()
183+ uniform_grid = vtkUniformGrid ()
167184 uniform_grid .SetDimensions (resolution , resolution , resolution )
168185 # Define the transform to scale the grid
169- transform = vtk . vtkTransform ()
186+ transform = vtkTransform ()
170187 transform .Scale (scale [0 ], scale [1 ], scale [2 ])
171188 transform .Translate (origin [0 ], origin [1 ], origin [2 ])
172189 # Apply the transform to the grid
173- transform_filter = vtk . vtkTransformFilter ()
190+ transform_filter = vtkTransformFilter ()
174191 transform_filter .SetInputData (uniform_grid )
175192 transform_filter .SetTransform (transform )
176193 transform_filter .Update ()
@@ -183,16 +200,16 @@ def threshold_3d(function, resolution, origin, scale, upper, lower):
183200 # Set implicit values to the grid
184201 uniform_grid .GetPointData ().SetScalars (values )
185202 # Transform the grid to an unstructured grid
186- append_filter = vtk . vtkAppendFilter ()
203+ append_filter = vtkAppendFilter ()
187204 append_filter .AddInputData (uniform_grid )
188205 append_filter .Update ()
189- unstructured_grid = vtk . vtkUnstructuredGrid ()
206+ unstructured_grid = vtkUnstructuredGrid ()
190207 unstructured_grid .ShallowCopy (append_filter .GetOutput ())
191208 # Threshold the grid
192- threshold = vtk . vtkThreshold ()
209+ threshold = vtkThreshold ()
193210 threshold .SetInputData (unstructured_grid )
194211 threshold .ThresholdBetween (lower , upper )
195- threshold .SetInputArrayToProcess (0 , 0 , 0 , vtk . vtkDataObject .FIELD_ASSOCIATION_POINTS , "ImplicitFunctionValue" )
212+ threshold .SetInputArrayToProcess (0 , 0 , 0 , vtkDataObject .FIELD_ASSOCIATION_POINTS , "ImplicitFunctionValue" )
196213 threshold .Update ()
197214 # Obtain the mesh
198215 mesh = threshold .GetOutput ()
@@ -225,15 +242,15 @@ def marching_squares(function, grid, origin, value=0.0):
225242 dimension_1 = grid [1 ].flatten ()
226243 dimensions = np .vstack ((dimension_0 , dimension_1 )).T
227244 values = function (dimensions ).flatten ()
228- image_data = vtk . vtkImageData ()
245+ image_data = vtkImageData ()
229246 image_data .SetDimensions (grid [0 ].shape [0 ], grid [0 ].shape [1 ], 1 )
230247 image_data .SetSpacing (dimension_0_spacing , dimension_1_spacing , 1 )
231248 image_data .SetOrigin (origin [0 ], origin [1 ], 0 )
232- image_data . AllocateScalars ( vtk . VTK_DOUBLE , 1 )
233- vtk_values = numpy_support .numpy_to_vtk (values , deep = True , array_type = vtk . VTK_DOUBLE )
249+ # Let numpy_support infer array type to avoid referencing VTK_* constants
250+ vtk_values = numpy_support .numpy_to_vtk (values , deep = True )
234251 image_data .GetPointData ().SetScalars (vtk_values )
235252 # Create the 2D filter
236- marching_squares_filter = vtk . vtkMarchingSquares ()
253+ marching_squares_filter = vtkMarchingSquares ()
237254 marching_squares_filter .SetInputData (image_data )
238255 marching_squares_filter .SetValue (0 , value )
239256 # Obtain the mesh
@@ -299,5 +316,3 @@ def contour(function, points, resolution, value=0.0, buffer=1.5):
299316 boundary = None
300317 mesh = None
301318 return boundary , mesh
302-
303-
0 commit comments