.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/Geometry/plot_brainStyle.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_Geometry_plot_brainStyle.py: Stylized brain ======================== Multiresolution meshing from an existing surface mesh. Original mesh, generated from MRI data, is available at ``_ .. GENERATED FROM PYTHON SOURCE LINES 11-47 .. code-block:: default import numpy as np import xcell import pyvista as pv import tqdm from vtk import VTK_QUAD stl = pv.read("full_oriented_simpler.stl") # pv.set_jupyter_backend(None) pts = stl.points ax = 2 bbox = np.hstack((np.min(pts, axis=0), np.max(pts, axis=0))) xmax = np.max(np.abs(bbox)) * 1.5 mindepth = 2 max_depth = 6 setup = xcell.Simulation("brain", xmax * np.sign(bbox), True) # interpolate by bounds idealDepth = mindepth + (max_depth - mindepth) * (pts[:, 2] - bbox[2]) / (bbox[5] - bbox[2]) metfun = xcell.general_metric setup.make_adaptive_grid( ref_pts=pts, max_depth=idealDepth.astype(int), min_l0_function=metfun, coefs=0.2 * np.ones_like(idealDepth), coarsen=False, ) setup.finalize_mesh() .. rst-class:: sphx-glr-script-out .. code-block:: none Make elements starting Make elements: 34.2734s [CPU], 34.2656s [wall] Finalize mesh starting Finalize mesh: 64.389 ms [CPU], 64.2962 ms [wall] Calculate conductances starting Calculate conductances: 116.174 ms [CPU], 116.174 ms [wall] Renumber nodes starting Renumber nodes: 321.12 ms [CPU], 244.579 ms [wall] .. GENERATED FROM PYTHON SOURCE LINES 48-55 .. code-block:: default adj = setup.mesh.get_element_adjacencies() # reg = xcell.io.Regions() # stl.cell_data['sigma'] = 1. # reg['Conductors'].append(stl) # reg.assign_sigma(setup.mesh, default_sigma=0) .. rst-class:: sphx-glr-script-out .. code-block:: none Calculating adjacency: 0%| | 0/22905 [00:00 0: neighbors = adj[ii] for jj in range(6): neighbor = neighbors[jj] for nei in neighbor: if nei.sigma == 0: if nei.depth > el.depth: otherFaceInd = jj + (-1) ** jj inds = np.flip(nei.vertices[quadorders[otherFaceInd]]) else: inds = el.vertices[quadorders[jj]] globalind = [setup.mesh.inverse_index_map[idx] for idx in inds] okfaces.append(globalind) fc = np.array(okfaces) usedNodes = np.unique(fc.ravel()) revNodes = {} for ii, n in enumerate(usedNodes): revNodes[n] = ii newpts = setup.mesh.node_coords[usedNodes] newFaces = np.array([revNodes[n] for n in fc.ravel()]).reshape((fc.shape[0], 4)) cells = np.hstack((4 * np.ones((newFaces.shape[0], 1), dtype=np.uint64), newFaces.astype(np.uint64))).ravel() cellTypes = [VTK_QUAD] * newFaces.shape[0] qmesh = pv.UnstructuredGrid(cells, cellTypes, newpts) qmesh.save("quad.vtk") qg = qmesh.compute_cell_sizes(length=False, volume=False) qg.set_active_scalars("Area") # qg.plot(style='wireframe') .. rst-class:: sphx-glr-script-out .. code-block:: none Checking faces: 0%| | 0/22905 [00:00, pyvista_ndarray([14.62477487, 14.6247767 , 14.62477852, ..., 3.65619691, 3.65619691, 3.65619509])) .. GENERATED FROM PYTHON SOURCE LINES 113-116 Segment mesh and visualize ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 116-127 .. code-block:: default fstem = "xcell%d-%d" % (mindepth, max_depth) result = inside.threshold(value=0.5, scalars="SelectedPoints").extract_largest() result.save(fstem + ".vtk") # obj = result.extract_surface() # pv.save_meshio(fstem+'.obj', obj) pv.save_meshio("quads.obj", qmesh) .. GENERATED FROM PYTHON SOURCE LINES 128-131 Generate animation ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 131-158 .. code-block:: default # to generate from premeshed file # result = pv.read('xcell2-9.vtk') thm = pv.themes.DarkTheme() thm.background = pv.Color(xcell.colors.DARK, opacity=0) offwhite = pv.Color(xcell.colors.OFFWHITE, opacity=1.0) pv.set_plot_theme(thm) p = pv.Plotter(off_screen=True) p.add_mesh(stl, color="blue", opacity=0.75) viewup = [0.2, -1.0, 0.0] p.add_mesh(result, show_edges=False, color=offwhite) p.enable_eye_dome_lighting() p.show(auto_close=False) path = p.generate_orbital_path(factor=1.5, n_points=32, viewup=viewup, shift=-0.2) p.open_movie("orbit.mp4") p.orbit_on_path(path, write_frames=True, viewup=viewup, step=0.1, progress_bar=True) .. image-sg:: /auto_examples/Geometry/images/sphx_glr_plot_brainStyle_001.png :alt: plot brainStyle :srcset: /auto_examples/Geometry/images/sphx_glr_plot_brainStyle_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/32 [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_brainStyle.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_