.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_probabilistic_modeling/distributions/plot_mesh_uniform.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_probabilistic_modeling_distributions_plot_mesh_uniform.py: Uniform distribution on a triangular mesh ========================================= .. GENERATED FROM PYTHON SOURCE LINES 7-16 Abstract -------- In this example, we present the :class:`~openturns.UniformOverMesh` distribution, which is the uniform distribution on a triangulated surface embedded in an ambient space of dimension strictly larger than the intrinsic dimension of the surface. We show how to build the distribution explicitly from a mesh, and how to estimate the mesh from a sample with the :class:`~openturns.experimental.UniformOverMeshFactory` class. .. GENERATED FROM PYTHON SOURCE LINES 18-22 .. code-block:: Python import openturns as ot import openturns.experimental as otexp import openturns.viewer as otv .. GENERATED FROM PYTHON SOURCE LINES 23-29 Build the reference mesh ------------------------ We consider a triangulation of the square :math:`[0,1]\times[0,1]` embedded in :math:`\Rset^3`. The mesh contains two triangles per cell of a :math:`16\times 16` regular grid. .. GENERATED FROM PYTHON SOURCE LINES 31-49 .. code-block:: Python gridSize = 16 vertices = ot.Sample(0, 3) simplices = [] for j in range(gridSize + 1): for i in range(gridSize + 1): x = i / gridSize y = j / gridSize vertices.add([x, y, 0.0]) for j in range(gridSize): for i in range(gridSize): index = j * (gridSize + 1) + i simplices.append([index, index + 1, index + gridSize + 1]) simplices.append([index + 1, index + gridSize + 2, index + gridSize + 1]) mesh = ot.Mesh(vertices, ot.IndicesCollection(simplices), False) distribution = ot.UniformOverMesh(mesh) print("Volume of the mesh: ", distribution.getVolume()) .. rst-class:: sphx-glr-script-out .. code-block:: none Volume of the mesh: 1.0 .. GENERATED FROM PYTHON SOURCE LINES 50-56 Draw a sample and check the on-manifold test -------------------------------------------- The simplices of the mesh are selected with probabilities proportional to their volumes, and each realization is generated uniformly within the selected simplex. .. GENERATED FROM PYTHON SOURCE LINES 58-65 .. code-block:: Python ot.RandomGenerator.SetSeed(0) sample = distribution.getSample(500) print( "The sample points belong to the mesh: ", all(distribution.computePDF(point) > 0.0 for point in sample), ) .. rst-class:: sphx-glr-script-out .. code-block:: none The sample points belong to the mesh: True .. GENERATED FROM PYTHON SOURCE LINES 66-74 Estimate the mesh from the sample --------------------------------- The :class:`~openturns.experimental.UniformOverMeshFactory` class reconstructs a surface from a sample: for each point, the local tangent plane is estimated by a principal component analysis of the nearest neighbors, the neighbors are Delaunay triangulated in the tangent plane, and the resulting triangles are merged and pruned. .. GENERATED FROM PYTHON SOURCE LINES 76-81 .. code-block:: Python factory = otexp.UniformOverMeshFactory() estimated = factory.buildAsUniformOverMesh(sample) print("Number of estimated simplices: ", estimated.getMesh().getSimplicesNumber()) print("Estimated volume: ", estimated.getVolume()) .. rst-class:: sphx-glr-script-out .. code-block:: none Number of estimated simplices: 2153 Estimated volume: 1.794488318552799 .. GENERATED FROM PYTHON SOURCE LINES 82-86 The estimated volume is of the same order of magnitude as the volume of the reference mesh, and the estimated distribution describes the sample: any point of the sample with a positive estimated PDF lies on the estimated manifold. .. GENERATED FROM PYTHON SOURCE LINES 88-94 .. code-block:: Python print("Reference volume: ", distribution.getVolume()) print( "Covered sample points ratio: ", sum(estimated.computePDF(point) > 0.0 for point in sample[:100]) / 100.0, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Reference volume: 1.0 Covered sample points ratio: 1.0 .. GENERATED FROM PYTHON SOURCE LINES 95-97 Draw a new sample from the estimated distribution and display it. The points of the original and the estimated sample are drawn on the surface. .. GENERATED FROM PYTHON SOURCE LINES 99-110 .. code-block:: Python ot.RandomGenerator.SetSeed(1) estimatedSample = estimated.getSample(200) cloud = ot.Cloud( estimatedSample.getMarginal([0, 1]), "blue", "fsquare", "Estimated sample" ) graph = ot.Graph( "Uniform distribution on a triangulated surface", "x", "y", True, "topright" ) graph.add(cloud) view = otv.View(graph) otv.View.ShowAll() .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_mesh_uniform_001.svg :alt: Uniform distribution on a triangulated surface :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_mesh_uniform_001.svg :class: sphx-glr-single-img .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_mesh_uniform.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mesh_uniform.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mesh_uniform.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_mesh_uniform.zip `