11""" Periodic spline example in 3D """
22
3+ import random
34import numpy as np
45import cubinterpp .cubinterpp_py as cubinterpp # cubinterpp_py is a pybind11 module
56import mlpyqtgraph as mpg
89@mpg .plotter (projection = 'orthographic' )
910def main ():
1011 """ Interpolation of a torus to demonstrate periodic splines in 2D """
11- nx , ny = 5 , 5
12+ nx , ny = 9 , 9
1213 tx = np .linspace (0.0 , 1.0 , nx )
1314 ty = np .linspace (0.0 , 1.0 , ny )
1415 # generate torus points
1516 r_major = 3.0 # major radius
1617 r_minor = 1.0 # minor radius
1718
19+ random .seed (43 )
1820 x = np .zeros ((nx , ny ))
1921 y = np .zeros ((nx , ny ))
2022 z = np .zeros ((nx , ny ))
@@ -24,7 +26,10 @@ def main():
2426 v = 2.0 * np .pi * ty [j ]
2527 x [i , j ] = (r_major + r_minor * np .cos (v )) * np .cos (u )
2628 y [i , j ] = (r_major + r_minor * np .cos (v )) * np .sin (u )
27- z [i , j ] = r_minor * np .sin (v )
29+ if i == nx - 1 :
30+ z [i , j ] = z [0 , j ]
31+ else :
32+ z [i , j ] = (1.0 + random .randint (- 2 , + 2 )/ 8.0 ) * r_minor * np .sin (v )
2833
2934 # create periodic splines in 2D
3035 spline_x = cubinterpp .NaturalPeriodicSpline2D (tx , ty , x )
@@ -44,7 +49,7 @@ def main():
4449 z_fine [i , j ] = spline_z .eval (txi [i ], tyi [j ])
4550
4651
47- mpg .figure (title = 'Periodic Spline Interpolation of a Torus' )
52+ mpg .figure (title = 'Periodic Spline Interpolation of a distorted Torus' )
4853 mpg .surf (x_fine , y_fine , z_fine )
4954 ax = mpg .gca ()
5055 ax .azimuth = 225
@@ -55,6 +60,8 @@ def main():
5560 yp = y .flatten ()
5661 zp = z .flatten ()
5762 mpg .points3 (xp , yp , zp , color = (0.8 , 0.1 , 0.1 , 1 ), size = 5 )
63+ ax .export ('periodic_spline_2d.png' )
64+
5865
5966if __name__ == '__main__' :
6067 main ()
0 commit comments