[9173ee]: / testing / mesh / meshTools / get_smoothed_scalars_test.py

Download this file

84 lines (65 with data), 2.9 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from lib2to3.pgen2.token import AT
import numpy as np
import vtk
from numpy.testing import assert_allclose
from vtk.util.numpy_support import numpy_to_vtk
from pymskt import ATOL, RTOL
from pymskt.mesh import meshTools
#
# get_smoothed_scalars
#
def dist(diff, epsilon=1e-7):
return np.sqrt(np.sum(np.square(diff + epsilon)))
def test_get_smoothed_scalars(
max_dist=1.1, # use 1.1 so only need to get single points in-line(x/y) & no diag for testing - but dont want 1.0 otherwise weighting = 0 for all other points.
order=2.0,
):
# Create small mesh that can easily manually calculate the outcomes.
width_height = 1
resolution = 1
n_points = int(width_height * (1 / resolution) + 1)
x = np.linspace(0, width_height, n_points)
xv, yv = np.meshgrid(x, x)
np_points = np.ones((len(xv.flatten()), 3))
np_points[:, 0] = xv.flatten(order="F")
np_points[:, 1] = yv.flatten(order="F")
np_points[:, 2] = 1
vtk_points_ = numpy_to_vtk(np_points)
vtk_points_.SetName("test")
vtk_points = vtk.vtkPoints()
vtk_points.SetData(vtk_points_)
mesh = vtk.vtkPolyData()
mesh.SetPoints(vtk_points)
np_scalars = np.random.randint(0, 1000, len(xv.flatten()))
np_scalars_reshaped = np.reshape(np_scalars, (width_height * 2, width_height * 2), order="F")
np_scalars_smoothed_test = np.zeros_like(np_scalars_reshaped).astype(float)
for i in range(width_height + 1):
for j in range(width_height + 1):
distances = [dist(0)]
scalars = [np_scalars_reshaped[i, j]]
if i > 0:
scalars.append(np_scalars_reshaped[i - 1, j])
distances.append(dist(1.0))
if j > 0:
scalars.append(np_scalars_reshaped[i, j - 1])
distances.append(dist(1.0))
if i < width_height:
scalars.append(np_scalars_reshaped[i + 1, j])
distances.append(dist(1.0))
if j < width_height:
scalars.append(np_scalars_reshaped[i, j + 1])
distances.append(dist(1.0))
weights = (max_dist - np.asarray(distances)) ** order
weighted_scalars = weights * np.asarray(scalars)
normalized_point = np.sum(weighted_scalars) / np.sum(weights)
np_scalars_smoothed_test[i, j] = normalized_point
# Apply scalars (and impulse) to mesh.
vtk_scalars = numpy_to_vtk(np.copy(np_scalars))
vtk_scalars.SetName("test")
mesh.GetPointData().AddArray(vtk_scalars)
mesh.GetPointData().SetActiveScalars("test")
scalars_smoothed = meshTools.get_smoothed_scalars(
mesh, max_dist=max_dist, order=order, gaussian=False
)
scalars_smoothed = np.reshape(scalars_smoothed, (width_height + 1, width_height + 1), order="F")
assert_allclose(np_scalars_smoothed_test, scalars_smoothed, rtol=1e-03, atol=ATOL)