Visualizing fibres using meshalyzer

See code in GitLab.
Author: Tobias Gerach tobias.gerach@kit.edu

Visualizing fibres in meshalyzer

Cardiac tissue conducts faster along the myocyte (fibre) direction than across it. In openCARP this anisotropy is set per conductivity region, e.g. gregion[0].g_il along the fibre, gregion[0].g_it across it and gregion[0].g_in normal to the sheet, and the direction itself comes from the fibre file of the mesh. A wrong or corrupted fibre field therefore changes every activation map you compute, often without any error message. Looking at the fibres is the cheapest check you can do before running a simulation.

This example shows how to turn the fibres of an openCARP mesh into arrows in meshalyzer, how to make a dense fibre field readable, and how to check the result. It runs no simulation. To run the experiments of this example change directories as follows:

cd ${EXAMPLES}/06_visualization/05_meshalyzer_fibres

If you have never used meshalyzer, start with the basic meshalyzer example.

Fibres in openCARP meshes

Fibres are stored per element, not per node, in the .lon file. The first line is the number of axes, 1 (fibres only) or 2 (fibres and sheets). Every following line belongs to the element on the same line of the .elem file and holds the fibre direction \(\mathbf{f}\), followed by the sheet direction \(\mathbf{s}\) if there are 2 axes:

2
0.500000 -0.866025 0.000000 0.866025 0.500000 0.000000
0.500000 -0.866025 0.000000 0.866025 0.500000 0.000000
...

A fibre is an axis, not an arrow: \(\mathbf{f}\) and \(-\mathbf{f}\) describe the same tissue.

Fibres as vector data

meshalyzer cannot read a .lon file directly. Instead, it draws vector data, which needs two files with the same base name:

  • name.vpts: the positions of the arrows, in the same format (and units) as a .pts file
  • name.vec: one line per position with x y z [scalar], the direction of the arrow and an optional scalar used for colouring or scaling. Vectors equal to 0 0 0 are not drawn.

For fibres, the natural positions are the element centroids.

Meshes created with mesher already come with these files. Next to block.pts, block.elem and block.lon you will find block.vpts (element centres) and block.vec (fibre directions, no sheets).

For any other mesh, you can build them yourself. This is what run.py does with carputils:

import numpy as np
from carputils import mesh
from carputils.carpio import txt

msh = mesh.Mesh.from_files(meshname, um2mm=False)   # reads .pts, .elem and .lon
centroids = msh.element_centroids()                 # one point per element, in um
depth = centroids[:, 2]                             # scalar column: z-coordinate

# .vpts has the .pts format, but carputils only writes files named .pts
np.savetxt('fibres.vpts', centroids, fmt='%.3f', header=str(len(centroids)), comments='')
txt.open('fibres.vec', mode='w').write(np.column_stack((msh.fibres(), depth)))

Replace msh.fibres() by msh.sheets() to get the sheet directions. The 4th column is free to use. Here it holds the depth of each element, so we can colour the arrows by depth.

Generate the data

./run.py [--fibre-endo -60] [--fibre-epi 60] [--visualize [--show sheets]]

This creates a 5 mm x 5 mm slab, 2 mm thick, made of 16000 tetrahedra. The fibres lie in the slab plane, and their angle to the x-axis increases linearly with depth, from --fibre-endo at the bottom (smallest z) to --fibre-epi at the top. This mimics the transmural fibre rotation in the ventricular wall. The sheets lie in the slab plane, perpendicular to the fibres.

The output directory fibres_-60_60 contains fibres.vpts/.vec, sheets.vpts/.vec and slab.surf, the outer surface of the slab (see Showing the tissue surface). With --visualize, meshalyzer opens with the fibres, the surface and the view state view_fibres.mshz; add --show sheets to open it with the sheets instead. To start it by hand:

meshalyzer meshes/<date>_<id>/block fibres_-60_60/fibres.vpts fibres_-60_60/slab.surf view_fibres.mshz

Vector data can also be added to a running meshalyzer with File/Read Data, selecting the .vpts file.

Note

meshalyzer may print an IGB header error while it probes the .vec file. The vectors are still read correctly.

Making the fibres readable

Start meshalyzer without the view state:

meshalyzer meshes/<date>_<id>/block fibres_-60_60/fibres.vpts

You see a dark, uniform carpet of arrows. Seen from the top, all element layers of the slab lie on top of each other, and every one of the 16000 elements gets an arrow. The Vectors tab of the control window has everything needed to fix this:

  1. Turn off the heads. Since \(\mathbf{f}\) and \(-\mathbf{f}\) are the same fibre, the head carries no information. Unchecking Heads also makes drawing much cheaper.
  2. Fixed length. Choose Fixed in the Length dropdown. Fibre vectors should have unit length anyway. Be aware that a fixed length also hides wrong magnitudes; colour by Vector mag to detect them (see the exercises).
  3. Arrow size and L/W. Turn the Arrow Size roller until neighbouring arrows no longer overlap, and lower the L/W ratio to make the arrows thicker.
  4. Colour by scalar. Choose Scalar in the Colour dropdown and a colour scale. With the depth stored as the scalar, each layer gets its own colour. Fixed instead draws all arrows in one constant colour, chosen with the colour button.
  5. Lighting. Set Diffuse to 1 and Specular to 0. The arrows then show the colour of the colour scale, without white reflections.
  6. Stride. This slab is small enough to draw every arrow. On large meshes with small, dense elements, draw only every n-th arrow with stride. Regular meshes then give a regular pattern (aliasing); check Stochastic to sample randomly at the same average stride. Do this after you have positioned the model, because it is slow.
  7. Tilt the view. Rotate the slab until you see it from the side and the top at the same time.
05_meshalyzer_fibres/06_05_vis_meshalyzer_fibres.png
Fig. 1: Fibres of the slab with view_fibres.mshz: all 16000 fibres, no heads, fixed length, diffuse 1, specular 0, coloured by depth (blue: bottom, red: top), inside the transparent slab surface. On the side faces, the fibre angle turns from -60 degrees at the bottom to +60 degrees at the top.

Fig. 1 shows the result. view_fibres.mshz contains exactly these settings. Load it with File/Restore state, or save your own with File/Save state (see the basic meshalyzer example).

Inspecting the rotation

A stack of layers is still hard to read. Look at one layer at a time instead:

  1. Open Data/Clipping and turn on a clipping plane perpendicular to z.
  2. Turn on a second plane facing the other way, so that only a thin slice between the two planes remains.
  3. Drag the Intercept of both planes through the slab and watch the fibres turn from -60 to +60 degrees.

Showing the tissue surface

Arrows alone do not show where the tissue ends. A transparent surface around them does. The block mesh has no surface file, so run.py extracts the outer surface with the carputils wrapper of meshtool:

from carputils.carpio import meshtool
meshtool.extract_surface(job, meshname, 'fibres_-60_60/slab')   # writes slab.surf

Pass slab.surf to meshalyzer on the command line, as above. Alternatively, let meshalyzer compute the surface itself with File/Compute surfaces. Then:

  1. In the Surface tab, click Fill colour and choose an Opacity well below one, e.g. 0.25, so the arrows inside remain visible.
  2. In the Lights tab, set Fresnel to 0. Otherwise faces seen edge-on to the light become dark and opaque and hide the arrows behind them.

More on transparency and clipping is found in the advanced meshalyzer example.

Checking the fibres numerically

A picture shows you the pattern; a few lines of Python tell you whether the numbers behind it are right. For example, the mean fibre angle per layer of the slab:

import numpy as np
pts = np.loadtxt('fibres_-60_60/fibres.vpts', skiprows=1)
vec = np.loadtxt('fibres_-60_60/fibres.vec')
angle = np.degrees(np.arctan2(vec[:, 1], vec[:, 0]))
for z in np.unique(pts[:, 2]):
    print(f'z = {z:7.1f} um  fibre angle = {angle[pts[:, 2] == z].mean():6.1f} deg')

The same idea works for your own meshes: np.linalg.norm(vec[:, :3], axis=1) should be 1 everywhere, and for meshes with sheets, the dot product of fibres and sheets should be 0.

Tip

If a .lon file exists and no volume elements are tagged, meshalyzer puts elements with no fibre direction into region 0 and all others into region 1. This is a quick way to find elements without fibres.

Exercises

  1. Run ./run.py --fibre-endo 0 --fibre-epi 0 and load the new fibres. All arrows should point along x.
  2. Run ./run.py --visualize --show sheets, or load sheets.vpts with File/Read Data. Check that the sheets lie in the slab plane, perpendicular to the fibres, in every layer.
  3. Choose Vector mag in the Colour dropdown. A correct fibre file gives a single colour, because all fibres have unit length.
  4. Compare fibres_-60_60/fibres.vec with the block.vec written by mesher next to the mesh. The directions are identical; only the scalar column is new.
  5. Apply the carputils snippet above to one of your own meshes and inspect its fibres.

More details on the vector controls are found in the meshalyzer manual.

Recent questions tagged meshalyzer, fibres

There are tagged with meshalyzer, fibres.

Here we display the 5 most recent questions. You can click on each tag to show all questions for this tag.

You can also ask a new question.

© Copyright 2020 openCARP project    Supported by DFG and EuroHPC    Contact    Imprint and data protection