.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/Fibers/fibers.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_examples_Fibers_fibers.py: Short fibre composite ===================== Rebuild the fibres of a short fibre composite from a tomography analysis: a CSV file gives the centre, diameter and orientation of about 15 000 fibres. Each fibre is a :class:`microgen.Cylinder`, whose orientation gives the fibre axis; the fibres are drawn as PyVista tubes. With the CAD backend (``pip install microgen[cad]``), the same cylinders can be exported to STEP with ``Cylinder.generate_cad()`` and :func:`microgen.cad.make_compound`, which takes several minutes for this number of fibres. .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: Python import numpy as np import pyvista as pv from microgen import Cylinder .. GENERATED FROM PYTHON SOURCE LINES 22-24 Read the fibres. Columns: centre (x, y, z), equivalent diameter and the two orientation angles, in µm and degrees. .. GENERATED FROM PYTHON SOURCE LINES 24-30 .. code-block:: Python data = np.loadtxt("fibers.csv", delimiter=",", skiprows=2) centers = data[:, 0:3] diameters = data[:, 3] phi, theta = data[:, 4], data[:, 5] print(f"{len(data)} fibres in a {np.ptp(centers, axis=0).round()} µm box") .. rst-class:: sphx-glr-script-out .. code-block:: none 14900 fibres in a [1321. 1322. 1331.] µm box .. GENERATED FROM PYTHON SOURCE LINES 31-33 One :class:`~microgen.Cylinder` per fibre, of constant length. Its axis is the local x axis, rotated by the Euler angles (ZXZ) of the analysis. .. GENERATED FROM PYTHON SOURCE LINES 33-45 .. code-block:: Python length = 250.0 # µm fibres = [ Cylinder( center=tuple(c), orientation=(p, 90.0, t), height=length, radius=0.5 * d, ) for c, d, p, t in zip(centers, diameters, phi, theta) ] axes = np.array([fibre.orientation.apply((1.0, 0.0, 0.0)) for fibre in fibres]) .. GENERATED FROM PYTHON SOURCE LINES 46-48 Draw each fibre as a tube between its two ends, coloured by the angle between the fibre and the z axis. .. GENERATED FROM PYTHON SOURCE LINES 48-65 .. code-block:: Python n = len(fibres) ends = np.empty((2 * n, 3)) ends[0::2] = centers - 0.5 * length * axes ends[1::2] = centers + 0.5 * length * axes lines = np.column_stack([np.full(n, 2), np.arange(0, 2 * n, 2), np.arange(1, 2 * n, 2)]) segments = pv.PolyData(ends, lines=lines) segments.point_data["radius"] = np.repeat(0.5 * diameters, 2) segments.point_data["angle to z (deg)"] = np.repeat( np.degrees(np.arccos(np.clip(np.abs(axes[:, 2]), 0.0, 1.0))), 2 ) tubes = segments.tube(scalars="radius", absolute=True, n_sides=8) tubes.set_active_scalars("angle to z (deg)") pl = pv.Plotter() pl.add_mesh(tubes, cmap="Oranges", smooth_shading=True) pl.add_mesh(pv.Box(bounds=segments.bounds), style="wireframe", color="#24445C") pl.show() .. image-sg:: /examples/Fibers/images/sphx_glr_fibers_001.png :alt: fibers :srcset: /examples/Fibers/images/sphx_glr_fibers_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.267 seconds) .. _sphx_glr_download_examples_Fibers_fibers.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: fibers.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: fibers.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: fibers.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_