Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Mesh.draw a simple wrapper to skfem.visuals.vedo #748

Merged
merged 5 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions skfem/io/meshio.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ def from_file(filename, out, **kwargs):

def to_meshio(mesh,
point_data=None,
cell_data=None):
cell_data=None,
encode_cell_data=True,
encode_point_data=False):

t = mesh.dofs.element_dofs.copy()
if isinstance(mesh, skfem.MeshHex2):
Expand All @@ -202,10 +204,15 @@ def to_meshio(mesh,
mtype = TYPE_MESH_MAPPING[type(mesh)]
cells = {mtype: t.T}

if cell_data is None:
cell_data = {}
if encode_cell_data:
if cell_data is None:
cell_data = {}
cell_data.update(mesh._encode_cell_data())

cell_data.update(mesh._encode_cell_data())
if encode_point_data:
if point_data is None:
point_data = {}
point_data.update(mesh._encode_point_data())

mio = meshio.Mesh(
mesh.p.T,
Expand All @@ -221,10 +228,14 @@ def to_file(mesh,
filename,
point_data=None,
cell_data=None,
encode_cell_data=True,
encode_point_data=False,
**kwargs):

meshio.write(filename,
to_meshio(mesh,
point_data,
cell_data),
cell_data,
encode_cell_data,
encode_point_data),
**kwargs)
38 changes: 26 additions & 12 deletions skfem/mesh/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,29 @@ def with_subdomains(self,
}
)

def _encode_point_data(self) -> Dict[str, List[ndarray]]:

subdomains = {} if self._subdomains is None else self._subdomains
boundaries = {} if self._boundaries is None else self._boundaries

def indicator(ix):
ind = np.zeros(self.nvertices)
ind[ix] = 1
return ind

return {
**{
f"skfem:s:{name}": indicator(np.unique(self.t[:, subdomain]
.flatten()))
for name, subdomain in subdomains.items()
},
**{
f"skfem:b:{name}": indicator(np.unique(self.facets[:, boundary]
.flatten()))
for name, boundary in boundaries.items()
},
}

def _encode_cell_data(self) -> Dict[str, List[ndarray]]:

subdomains = {} if self._subdomains is None else self._subdomains
Expand Down Expand Up @@ -756,16 +779,7 @@ def element_finder(self, mapping=None):
"""
raise NotImplementedError

def plot(self, *args, **kwargs):
"""Convenience wrapper for :func:`skfem.visuals.matplotlib.plot`."""
from skfem.visuals.matplotlib import plot, show
ax = plot(self, *args, **kwargs)
ax.show = show
return ax

def draw(self, *args, **kwargs):
"""Convenience wrapper for :func:`skfem.visuals.matplotlib.draw`."""
from skfem.visuals.matplotlib import draw, show
ax = draw(self, *args, **kwargs)
ax.show = show
return ax
"""Convenience wrapper for vedo."""
from skfem.visuals.vedo import draw
return draw(self, *args, **kwargs)
20 changes: 20 additions & 0 deletions skfem/visuals/vedo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import tempfile


def draw(m, backend=False, **kwargs):
"""Visualize meshes."""
import vedo
vedo.embedWindow(backend)
from vedo import Plotter
vp = Plotter()
tetmesh = None
with tempfile.NamedTemporaryFile() as tmp:
m.save(tmp.name + '.vtk',
encode_cell_data=False,
encode_point_data=True,
**kwargs)
tetmesh = vp.load(tmp.name + '.vtk')
# save these for further use
tetmesh.show = lambda: vp.show([tetmesh]).close()
tetmesh.plotter = vp
return tetmesh