1
"""Methods to convert surface objects to VTK data objects"""
2

3

4 26
__all__ = [
5
    'surface_geom_to_vtk',
6
    'surface_grid_geom_to_vtk',
7
    'surface_to_vtk',
8
]
9

10 26
__displayname__ = 'Surface'
11

12 26
import numpy as np
13 26
import omf
14 26
import pyvista
15

16 26
from omfvista.utilities import check_orientation, check_orthogonal
17 26
from omfvista.utilities import add_data, add_textures
18

19

20 26
def surface_geom_to_vtk(surfgeom, origin=(0.0, 0.0, 0.0)):
21
    """Convert the triangulated surface to a :class:`pyvista.PolyData`
22
    object
23

24
    Args:
25
        surfgeom (:class:`omf.surface.SurfaceGeometry`): the surface geomotry to
26
            convert
27
    """
28 26
    pts = np.array(surfgeom.vertices)
29 26
    tris = np.array(surfgeom.triangles.array)
30 26
    faces = np.c_[np.full(len(tris), 3), tris]
31 26
    output = pyvista.PolyData(pts, faces)
32 26
    output.points += np.array(origin)
33 26
    return output
34

35

36 26
def surface_grid_geom_to_vtk(surfgridgeom, origin=(0.0, 0.0, 0.0)):
37
    """Convert the 2D grid to a :class:`pyvista.StructuredGrid` object.
38

39
    Args:
40
        surfgridgeom (:class:`omf.surface.SurfaceGridGeometry`): the surface
41
            grid geometry to convert
42

43
    """
44 26
    surfgridgeom._validate_mesh()
45

46 26
    axis_u = np.array(surfgridgeom.axis_u)
47 26
    axis_v = np.array(surfgridgeom.axis_v)
48 26
    axis_w = np.cross(axis_u, axis_v)
49 26
    if not check_orthogonal(axis_u, axis_v, axis_w):
50 0
        raise ValueError('axis_u, axis_v, and axis_w must be orthogonal')
51 26
    rotation_mtx = np.array([axis_u, axis_v, axis_w])
52 26
    ox, oy, oz = surfgridgeom.origin
53

54
    # Make coordinates along each axis
55 26
    x = ox + np.cumsum(surfgridgeom.tensor_u)
56 26
    x = np.insert(x, 0, ox)
57 26
    y = oy + np.cumsum(surfgridgeom.tensor_v)
58 26
    y = np.insert(y, 0, oy)
59

60 26
    z = np.array([oz])
61

62
    # Build out all nodes in the mesh
63 26
    xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')
64 26
    xx, yy, zz, = xx.ravel('F'), yy.ravel('F'), zz.ravel('F')
65 26
    zz += surfgridgeom.offset_w
66 26
    points = np.c_[xx, yy, zz]
67

68
    # Rotate the points based on the axis orientations
69 26
    points = points.dot(rotation_mtx)
70

71
    # Now build the output
72 26
    output = pyvista.StructuredGrid()
73 26
    output.points = points
74 26
    output.dimensions = len(x), len(y), len(z)
75

76 26
    output.points += np.array(origin)
77 26
    return output
78

79

80 26
def surface_to_vtk(surfel, origin=(0.0, 0.0, 0.0)):
81
    """Convert the surface to a its appropriate VTK data object type.
82

83
    Args:
84
        surfel (:class:`omf.surface.SurfaceElement`): the surface element to
85
            convert
86
    """
87

88 26
    geom = surfel.geometry
89

90 26
    if isinstance(geom, omf.surface.SurfaceGeometry):
91 26
        builder = surface_geom_to_vtk
92 26
    elif isinstance(geom, omf.surface.SurfaceGridGeometry):
93 26
        builder = surface_grid_geom_to_vtk
94

95 26
    output = builder(geom, origin=origin)
96

97
    # Now add point data:
98 26
    add_data(output, surfel.data)
99

100 26
    add_textures(output, surfel.textures, surfel.name)
101

102 26
    return output
103

104

105 26
surface_to_vtk.__displayname__ = 'Surface to VTK'
106 26
surface_grid_geom_to_vtk.__displayname__ = 'Surface Grid Geometry to VTK'
107 26
surface_geom_to_vtk.__displayname__ = 'Surface Geometry to VTK'

Read our documentation on viewing source code .

Loading