|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies. |
| 3 | +# SPDX-FileContributor: Lucas Givord - Kitware |
| 4 | +import pyvista as pv |
| 5 | + |
| 6 | +from geos_trame.schema_generated.schema_mod import Box |
| 7 | + |
| 8 | +import re |
| 9 | + |
| 10 | + |
| 11 | +class BoxViewer: |
| 12 | + """A BoxViewer represents a Box and its intersected cell in a mesh. |
| 13 | +
|
| 14 | + This mesh is represented in GEOS with a Box. |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__( self, mesh: pv.UnstructuredGrid, box: Box ) -> None: |
| 18 | + """Initialize the BoxViewer with a mesh and a box.""" |
| 19 | + self._mesh: pv.UnstructuredGrid = mesh |
| 20 | + |
| 21 | + self._box: Box = box |
| 22 | + self._box_polydata: pv.PolyData = None |
| 23 | + self._box_polydata_actor: pv.Actor = None |
| 24 | + |
| 25 | + self._extracted_cell: pv.UnstructuredGrid = None |
| 26 | + self._extracted_cell_actor: pv.Actor = None |
| 27 | + |
| 28 | + self._compute_box_as_polydata() |
| 29 | + self._compute_intersected_cell() |
| 30 | + |
| 31 | + def append_to_plotter( self, plotter: pv.Plotter ) -> None: |
| 32 | + """Append the box and the intersected cell to the plotter. |
| 33 | +
|
| 34 | + The box is represented as a polydata with a low opacity. |
| 35 | + """ |
| 36 | + self._box_polydata_actor = plotter.add_mesh( self._box_polydata, opacity=0.2 ) |
| 37 | + |
| 38 | + if self._extracted_cell is not None: |
| 39 | + self._extracted_cell_actor = plotter.add_mesh( self._extracted_cell, show_edges=True ) |
| 40 | + |
| 41 | + def reset( self, plotter: pv.Plotter ) -> None: |
| 42 | + """Reset the box viewer by removing the box and the intersected cell from the plotter.""" |
| 43 | + if self._box_polydata_actor is not None: |
| 44 | + plotter.remove_actor( self._box_polydata_actor ) |
| 45 | + |
| 46 | + if self._extracted_cell_actor is not None: |
| 47 | + plotter.remove_actor( self._extracted_cell_actor ) |
| 48 | + |
| 49 | + self._box_polydata = None |
| 50 | + self._extracted_cell = None |
| 51 | + |
| 52 | + def _compute_box_as_polydata( self ) -> None: |
| 53 | + """Create a polydata reresenting a BBox using pyvista and coordinates from the Geos Box.""" |
| 54 | + bounding_box: list[ float ] = self._retrieve_bounding_box() |
| 55 | + self._box_polydata = pv.Box( bounds=bounding_box ) |
| 56 | + |
| 57 | + def _retrieve_bounding_box( self ) -> list[ float ]: |
| 58 | + """This method converts bounding box information from Box into a list of coordinates readable by pyvista. |
| 59 | +
|
| 60 | + e.g., this Box: |
| 61 | +
|
| 62 | + <Box name="box_1" |
| 63 | + xMin="{ 1150, 700, 62 }" |
| 64 | + xMax="{ 1250, 800, 137 }"/> |
| 65 | +
|
| 66 | + will return [1150, 1250, 700, 800, 62, 137]. |
| 67 | + """ |
| 68 | + # split str and remove brackets |
| 69 | + min_point_str = re.findall( r"-?\d+\.\d+|-?\d+", self._box.x_min ) |
| 70 | + max_point_str = re.findall( r"-?\d+\.\d+|-?\d+", self._box.x_max ) |
| 71 | + |
| 72 | + min_point = list( map( float, min_point_str ) ) |
| 73 | + max_point = list( map( float, max_point_str ) ) |
| 74 | + |
| 75 | + return [ |
| 76 | + min_point[ 0 ], |
| 77 | + max_point[ 0 ], |
| 78 | + min_point[ 1 ], |
| 79 | + max_point[ 1 ], |
| 80 | + min_point[ 2 ], |
| 81 | + max_point[ 2 ], |
| 82 | + ] |
| 83 | + |
| 84 | + def _compute_intersected_cell( self ) -> None: |
| 85 | + """Extract the cells from the mesh that are inside the box.""" |
| 86 | + ids = self._mesh.find_cells_within_bounds( self._box_polydata.bounds ) |
| 87 | + |
| 88 | + saved_ids: list[ int ] = [] |
| 89 | + |
| 90 | + for id in ids: |
| 91 | + cell: pv.vtkCell = self._mesh.GetCell( id ) |
| 92 | + |
| 93 | + is_inside = self._check_cell_inside_box( cell, self._box_polydata.bounds ) |
| 94 | + if is_inside: |
| 95 | + saved_ids.append( id ) |
| 96 | + |
| 97 | + if len( saved_ids ) > 0: |
| 98 | + self._extracted_cell = self._mesh.extract_cells( saved_ids ) |
| 99 | + |
| 100 | + def _check_cell_inside_box( self, cell: pv.Cell, box_bounds: list[ float ] ) -> bool: |
| 101 | + """Check if the cell is inside the box bounds. |
| 102 | +
|
| 103 | + A cell is considered inside the box if his bounds are completely |
| 104 | + inside the box bounds. |
| 105 | + """ |
| 106 | + cell_bounds = cell.GetBounds() |
| 107 | + is_inside_in_x = cell_bounds[ 0 ] >= box_bounds[ 0 ] and cell_bounds[ 1 ] <= box_bounds[ 1 ] |
| 108 | + is_inside_in_y = cell_bounds[ 2 ] >= box_bounds[ 2 ] and cell_bounds[ 3 ] <= box_bounds[ 3 ] |
| 109 | + is_inside_in_z = cell_bounds[ 4 ] >= box_bounds[ 4 ] and cell_bounds[ 5 ] <= box_bounds[ 5 ] |
| 110 | + |
| 111 | + return is_inside_in_x and is_inside_in_y and is_inside_in_z |
0 commit comments