トライコントアウト スムーズ デロネ

Beginner

This tutorial is from open-source community. Access the source code

はじめに

このチュートリアルでは、Matplotlib を使って高解像度の三角メッシュの等高線プロットを生成する方法を示します。三角メッシュの等高線は、非構造化三角メッシュ上のデータを表すために使用される手法です。データが不規則な間隔で収集される場合、またはデータが本質的に三角形状の場合によく使用されます。このチュートリアルでは、ランダムな点のセットを生成し、それらの点に対してデロネ三角分割を行い、メッシュ内の一部の三角形をマスクアウトし、データをリファインして補間し、最後に Matplotlib のtricontour関数を使ってリファインされたデータをプロットする方法を示します。

VM のヒント

VM の起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebook を使って練習しましょう。

時々、Jupyter Notebook が読み込み終了するまで数秒待つ必要があります。Jupyter Notebook の制限により、操作の検証を自動化することはできません。

学習中に問題に遭遇した場合は、Labby にお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。

必要なライブラリをインポートする

最初のステップは、必要なライブラリをインポートすることです。このチュートリアルでは、NumPy と Matplotlib を使用します。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.tri import TriAnalyzer, Triangulation, UniformTriRefiner

テスト関数を定義する

次に、実験の結果を表す関数を定義します。この関数は、テスト用のデータポイントを生成するために使用されます。

def experiment_res(x, y):
    """An analytic function representing experiment results."""
    x = 2 * x
    r1 = np.sqrt((0.5 - x)**2 + (0.5 - y)**2)
    theta1 = np.arctan2(0.5 - x, 0.5 - y)
    r2 = np.sqrt((-x - 0.2)**2 + (-y - 0.2)**2)
    theta2 = np.arctan2(-x - 0.2, -y - 0.2)
    z = (4 * (np.exp((r1/10)**2) - 1) * 30 * np.cos(3 * theta1) +
         (np.exp((r2/10)**2) - 1) * 30 * np.cos(5 * theta2) +
         2 * (x**2 + y**2))
    return (np.max(z) - z) / (np.max(z) - np.min(z))

テスト用のデータポイントを生成する

-1 から 1 の間の x と y の値を持つ、ランダムなテスト用のデータポイントのセットを生成します。また、手順 2 で定義したexperiment_res関数を使用して、対応する z 値のセットも生成します。

## User parameters for data test points

## Number of test data points, tested from 3 to 5000 for subdiv=3
n_test = 200

## Random points
random_gen = np.random.RandomState(seed=19680801)
x_test = random_gen.uniform(-1., 1., size=n_test)
y_test = random_gen.uniform(-1., 1., size=n_test)
z_test = experiment_res(x_test, y_test)

デロネ三角分割を行う

matplotlib.triモジュールのTriangulation関数を使って、テスト用のデータポイントに対してデロネ三角分割を行います。

## meshing with Delaunay triangulation
tri = Triangulation(x_test, y_test)
ntri = tri.triangles.shape[0]

一部の三角形をマスクする

メッシュ内の一部の三角形をマスクして、無効なデータをシミュレートします。init_mask_fracパラメータに基づいて、三角形のサブセットをランダムに選択します。

## Some invalid data are masked out
mask_init = np.zeros(ntri, dtype=bool)
masked_tri = random_gen.randint(0, ntri, int(ntri * init_mask_frac))
mask_init[masked_tri] = True
tri.set_mask(mask_init)

三角分割を改善する

三角分割の境界から形状が悪い(扁平な)三角形を取り除くことで三角分割を改善するために、TriAnalyzerを使用します。その後、set_maskを使ってマスクを三角分割に適用します。

## masking badly shaped triangles at the border of the triangular mesh.
mask = TriAnalyzer(tri).get_flat_tri_mask(min_circle_ratio)
tri.set_mask(mask)

データの微調整と補間

UniformTriRefinerを使用して、データを微調整し補間します。

## refining the data
refiner = UniformTriRefiner(tri)
tri_refi, z_test_refi = refiner.refine_field(z_test, subdiv=subdiv)

データをプロットする

Matplotlib のtricontour関数を使って、微調整されたデータをプロットします。

## Graphical options for tricontouring
levels = np.arange(0., 1., 0.025)

fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.set_title("Filtering a Delaunay mesh\n"
             "(application to high-resolution tricontouring)")

## plot of the refined (computed) data contours:
ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap='Blues',
              linewidths=[2.0, 0.5, 1.0, 0.5])

plt.show()

まとめ

このチュートリアルでは、Matplotlib を使って高解像度のトライコントアウト線プロットを生成する方法を示しました。まず、一連のランダムなテストデータポイントを生成し、それらの点に対してデロネ三角分割を行い、メッシュ内の一部の三角形をマスクし、データを微調整し補間し、最後に Matplotlib のtricontour関数を使って微調整されたデータをプロットしました。