|
5 | 5 | # This code is part of the Fatiando a Terra project (https://www.fatiando.org) |
6 | 6 | # |
7 | 7 | """ |
8 | | -Functions for visualizing prisms through pyvista |
| 8 | +Functions for visualizing prisms in 3D. |
9 | 9 | """ |
10 | 10 | import numpy as np |
11 | 11 |
|
12 | 12 | try: |
13 | 13 | import pyvista |
14 | 14 | except ImportError: |
15 | 15 | pyvista = None |
16 | | -else: |
| 16 | + |
| 17 | +try: |
| 18 | + import vedo |
| 19 | +except ImportError: |
| 20 | + vedo = None |
| 21 | + |
| 22 | +if pyvista is not None or vedo is not None: |
17 | 23 | import vtk |
18 | 24 |
|
19 | 25 |
|
| 26 | +def prism_to_vedo(prisms, properties=None): |
| 27 | + """ |
| 28 | + Create a :class:`vedo.UnstructuredGrid` out of prisms. |
| 29 | +
|
| 30 | + Builds a :class:`vedo.UnstructuredGrid` out of a set of prisms that |
| 31 | + could be used to plot a 3D representation through :mod:`vedo`. |
| 32 | +
|
| 33 | + Parameters |
| 34 | + ---------- |
| 35 | + prisms : list or 2d-array |
| 36 | + List or 2d-array with the boundaries of the prisms. |
| 37 | + Each row contains the boundaries of each prism in the following order: |
| 38 | + ``west``, ``east``, ``south``, ``north``, ``bottom``, ``top``. |
| 39 | + properties : dict or None (optional) |
| 40 | + Dictionary with the physical properties of the prisms. |
| 41 | + Each key should be a string and its corresponding value a 1D array. |
| 42 | + If None, no celldata will be added to the |
| 43 | + :class:`vedo.UnstructuredGrid`. |
| 44 | + Default to None. |
| 45 | +
|
| 46 | + Returns |
| 47 | + ------- |
| 48 | + vedo.UnstructuredGrid |
| 49 | + :class:`vedo.UnstructuredGrid` that represents the prisms with their |
| 50 | + properties (if any). |
| 51 | +
|
| 52 | + Examples |
| 53 | + -------- |
| 54 | + Define a set of prisms and their densities: |
| 55 | +
|
| 56 | + >>> prisms = [ |
| 57 | + ... [0, 4, 0, 5, -10, 0], |
| 58 | + ... [0, 4, 7, 9, -12, -3], |
| 59 | + ... [6, 9, 2, 6, -7, 3], |
| 60 | + ... ] |
| 61 | + >>> densities = [2900, 3000, 2670] |
| 62 | +
|
| 63 | + Generate a :class:`vedo.UnstructuredGrid` out of them: |
| 64 | +
|
| 65 | + >>> import harmonica as hm |
| 66 | + >>> vedo_grid = hm.visualization.prism_to_vedo( |
| 67 | + ... prisms, properties={"density": densities} |
| 68 | + ... ) |
| 69 | + >>> print(vedo_grid) # doctest: +SKIP |
| 70 | + vedo.grids.UnstructuredGrid at (0x56070a0af5b0) |
| 71 | + nr. of verts : 24 |
| 72 | + nr. of cells : 3 |
| 73 | + cell types : ['HEXAHEDRON'] |
| 74 | + size : average=6.64122, diagonal=19.6723 |
| 75 | + center of mass: (3.83333, 4.83333, -4.83333) |
| 76 | + bounds : x=(0, 9.00), y=(0, 9.00), z=(-12.0, 3.00) |
| 77 | + celldata * : "density" (int64), ndim=1, range=(2.67e+3, 3.00e+3) |
| 78 | + """ |
| 79 | + # Check if vedo are installed |
| 80 | + if vedo is None: |
| 81 | + raise ImportError( |
| 82 | + "Missing optional dependency 'vedo' required for building vedo grids." |
| 83 | + ) |
| 84 | + # Get prisms and number of prisms |
| 85 | + prisms = np.atleast_2d(prisms) |
| 86 | + n_prisms = prisms.shape[0] |
| 87 | + # Get the vertices of the prisms |
| 88 | + vertices = _prisms_boundaries_to_vertices(prisms) |
| 89 | + # Generate the cells for the hexahedrons |
| 90 | + cells = np.arange(n_prisms * 8).reshape([n_prisms, 8]) |
| 91 | + # Define celltypes |
| 92 | + celltypes = [vtk.VTK_HEXAHEDRON for _ in range(n_prisms)] |
| 93 | + # Build the UnstructuredGrid |
| 94 | + grid = vedo.UnstructuredGrid([vertices, cells, celltypes]) |
| 95 | + # Add properties to the grid |
| 96 | + if properties is not None: |
| 97 | + for name, prop in properties.items(): |
| 98 | + # Check if the property is given as 1d array |
| 99 | + property = np.atleast_1d(prop) |
| 100 | + if property.ndim > 1: |
| 101 | + raise ValueError( |
| 102 | + f"Multidimensional array found in '{name}' property. " |
| 103 | + + "Please, pass prism properties as 1d arrays." |
| 104 | + ) |
| 105 | + # Assign the property to the cell_data |
| 106 | + grid.celldata[name] = property |
| 107 | + return grid |
| 108 | + |
| 109 | + |
20 | 110 | def prism_to_pyvista(prisms, properties=None): |
21 | 111 | """ |
22 | 112 | Create a :class:`pyvista.UnstructuredGrid` out of prisms |
|
0 commit comments