21
21
__copyright__ = "Thomas Guillod - Dartmouth College"
22
22
__license__ = "Mozilla Public License Version 2.0"
23
23
24
- import vtk
25
24
import warnings
26
25
import scilogger
27
26
import numpy as np
28
- import pyvista as pv
29
27
import shapely as sha
30
28
import rasterio .features as raf
31
29
import rasterio .transform as rat
32
30
33
- # prevent VTK to mess up the output
34
- vtk .vtkObject .GlobalWarningDisplayOff ()
35
-
36
31
# prevent problematic linear transform to trigger warnings
37
32
warnings .filterwarnings ("ignore" , module = "shapely" )
38
33
warnings .filterwarnings ("ignore" , module = "rasterio.features" )
44
39
45
40
def _get_boundary_polygon (bnd , z_min ):
46
41
"""
47
- Convert a Shapely boundary into a PyVista polygon .
42
+ Convert a Shapely boundary into a line mesh .
48
43
"""
49
44
50
45
# check that the boundary is closed
@@ -58,24 +53,29 @@ def _get_boundary_polygon(bnd, z_min):
58
53
# get the 3D boundary
59
54
z = np .full (len (xy ), z_min , dtype = np .float64 )
60
55
z = np .expand_dims (z , axis = 1 )
61
- xyz = np .hstack ((xy , z ))
56
+ points = np .hstack ((xy , z ))
62
57
63
58
# remove the duplicated points
64
- xyz = xyz [:- 1 ]
59
+ points = points [:- 1 ]
65
60
66
- # get the face indices
67
- lines = np .arange (len (xyz ))
68
- lines = np .concatenate (([len (xyz )+ 1 ], lines , [0 ]))
61
+ # get the indices
62
+ faces = np .empty (0 , dtype = np .int64 )
63
+ lines = np .arange (len (points ), dtype = np .int64 )
64
+ lines = np .concatenate (([len (points )+ 1 ], lines , [0 ]))
69
65
70
66
# create the polygon
71
- mesh = pv .PolyData (xyz , lines = lines )
67
+ mesh = {
68
+ "faces" : np .array (faces , dtype = np .int64 ),
69
+ "lines" : np .array (lines , dtype = np .int64 ),
70
+ "points" : np .array (points , dtype = np .float64 ),
71
+ }
72
72
73
73
return mesh
74
74
75
75
76
- def _get_shape_mesh (z_min , z_max , obj ):
76
+ def _get_shape_mesh (obj , z_min , z_max ):
77
77
"""
78
- Extrude a Shapely polygon into a PyVista mesh .
78
+ Extrude a Shapely polygon into line meshes .
79
79
"""
80
80
81
81
# ensure that the polygon has a positive orientation
@@ -85,19 +85,19 @@ def _get_shape_mesh(z_min, z_max, obj):
85
85
bnd = obj .exterior
86
86
holes = obj .interiors
87
87
88
- # create an empty mesh
89
- mesh = pv . PolyData ()
88
+ # init mesh list
89
+ geom_def = []
90
90
91
91
# polygon for the external boundaries
92
- mesh += _get_boundary_polygon (bnd , z_min )
93
- mesh += _get_boundary_polygon (bnd , z_max )
92
+ geom_def . append ( _get_boundary_polygon (bnd , z_min ) )
93
+ geom_def . append ( _get_boundary_polygon (bnd , z_max ) )
94
94
95
95
# polygon for the holes
96
96
for bnd in holes :
97
- mesh += _get_boundary_polygon (bnd , z_min )
98
- mesh += _get_boundary_polygon (bnd , z_max )
97
+ geom_def . append ( _get_boundary_polygon (bnd , z_min ) )
98
+ geom_def . append ( _get_boundary_polygon (bnd , z_max ) )
99
99
100
- return mesh
100
+ return geom_def
101
101
102
102
103
103
def _get_shape_single (tag , shape_type , shape_data ):
@@ -426,13 +426,11 @@ def _get_voxel_size(dx, dy, dz, stack_pos, xy_max_obj, xy_min_obj, xy_max, xy_mi
426
426
def _get_merge_shape (stack_pos , shape_obj ):
427
427
"""
428
428
Transform all the 2D vector shapes into 3D meshes.
429
- Merge all the meshes into a single mesh.
430
- The resulting mesh represent the original geometry.
431
- This mesh can be used to assess the quality of the voxelization.
429
+ The resulting meshes represent the original geometry.
432
430
"""
433
431
434
- # create an empty mesh
435
- reference = pv . PolyData ()
432
+ # init mesh list
433
+ geom_def = []
436
434
437
435
# merge all the shapes
438
436
for shape_obj_tmp in shape_obj :
@@ -446,14 +444,14 @@ def _get_merge_shape(stack_pos, shape_obj):
446
444
447
445
# transform the shapes into meshes
448
446
if isinstance (obj , sha .Polygon ):
449
- reference += _get_shape_mesh (z_min , z_max , obj )
447
+ geom_def += _get_shape_mesh (obj , z_min , z_max )
450
448
elif isinstance (obj , sha .MultiPolygon ):
451
449
for obj_tmp in obj .geoms :
452
- reference += _get_shape_mesh (z_min , z_max , obj_tmp )
450
+ geom_def += _get_shape_mesh (obj_tmp , z_min , z_max )
453
451
else :
454
452
raise ValueError ("invalid shape type" )
455
453
456
- return reference
454
+ return geom_def
457
455
458
456
459
457
def get_mesh (param , layer_stack , geometry_shape ):
@@ -496,7 +494,7 @@ def get_mesh(param, layer_stack, geometry_shape):
496
494
497
495
# merge the shapes representing the original geometry
498
496
LOGGER .debug ("merge the shapes" )
499
- reference = _get_merge_shape (stack_pos , shape_obj )
497
+ geom_def = _get_merge_shape (stack_pos , shape_obj )
500
498
501
499
# voxelize the shapes
502
500
LOGGER .debug ("voxelize the shapes" )
@@ -508,11 +506,4 @@ def get_mesh(param, layer_stack, geometry_shape):
508
506
d = d .tolist ()
509
507
c = c .tolist ()
510
508
511
- # cast reference mesh
512
- reference = {
513
- "faces" : np .array (reference .faces , dtype = np .int64 ),
514
- "lines" : np .array (reference .lines , dtype = np .int64 ),
515
- "points" : np .array (reference .points , dtype = np .float64 ),
516
- }
517
-
518
- return n , d , c , domain_def , reference
509
+ return n , d , c , domain_def , geom_def
0 commit comments