1
1
from __future__ import print_function
2
2
import numpy as np
3
3
4
- from matplotlib ._delaunay import compute_planes , linear_interpolate_grid , nn_interpolate_grid
4
+ from matplotlib ._delaunay import compute_planes , linear_interpolate_grid
5
+ from matplotlib ._delaunay import nn_interpolate_grid
5
6
from matplotlib ._delaunay import nn_interpolate_unstructured
6
7
7
8
__all__ = ['LinearInterpolator' , 'NNInterpolator' ]
8
9
10
+
9
11
def slice2gridspec (key ):
10
12
"""Convert a 2-tuple of slices to start,stop,steps for x and y.
11
13
@@ -34,6 +36,7 @@ def slice2gridspec(key):
34
36
35
37
return x0 , x1 , xstep , y0 , y1 , ystep
36
38
39
+
37
40
class LinearInterpolator (object ):
38
41
"""Interpolate a function defined on the nodes of a triangulation by
39
42
using the planes defined by the three function values at each corner of
@@ -60,10 +63,10 @@ class LinearInterpolator(object):
60
63
61
64
Linear Interpolation
62
65
--------------------
63
- Given the Delauany triangulation (or indeed *any* complete triangulation) we
64
- can interpolate values inside the convex hull by locating the enclosing
65
- triangle of the interpolation point and returning the value at that point of
66
- the plane defined by the three node values.
66
+ Given the Delauany triangulation (or indeed *any* complete triangulation)
67
+ we can interpolate values inside the convex hull by locating the enclosing
68
+ triangle of the interpolation point and returning the value at that point
69
+ of the plane defined by the three node values.
67
70
68
71
f = planes[tri,0]*x + planes[tri,1]*y + planes[tri,2]
69
72
@@ -81,11 +84,14 @@ def __init__(self, triangulation, z, default_value=np.nan):
81
84
82
85
def __getitem__ (self , key ):
83
86
x0 , x1 , xstep , y0 , y1 , ystep = slice2gridspec (key )
84
- grid = linear_interpolate_grid (x0 , x1 , xstep , y0 , y1 , ystep , self .default_value ,
87
+ grid = linear_interpolate_grid (
88
+ x0 , x1 , xstep , y0 , y1 , ystep , self .default_value ,
85
89
self .planes , self .triangulation .x , self .triangulation .y ,
86
- self .triangulation .triangle_nodes , self .triangulation .triangle_neighbors )
90
+ self .triangulation .triangle_nodes ,
91
+ self .triangulation .triangle_neighbors )
87
92
return grid
88
93
94
+
89
95
class NNInterpolator (object ):
90
96
"""Interpolate a function defined on the nodes of a triangulation by
91
97
the natural neighbors method.
@@ -109,23 +115,23 @@ class NNInterpolator(object):
109
115
-------------------------------
110
116
One feature of the Delaunay triangulation is that for each triangle, its
111
117
circumcircle contains no other point (although in degenerate cases, like
112
- squares, other points may be *on* the circumcircle). One can also construct
113
- what is called the Voronoi diagram from a Delaunay triangulation by
114
- connecting the circumcenters of the triangles to those of their neighbors to
115
- form a tesselation of irregular polygons covering the plane and containing
116
- only one node from the triangulation. Each point in one node's Voronoi
117
- polygon is closer to that node than any other node.
118
+ squares, other points may be *on* the circumcircle). One can also
119
+ construct what is called the Voronoi diagram from a Delaunay triangulation
120
+ by connecting the circumcenters of the triangles to those of their
121
+ neighbors to form a tesselation of irregular polygons covering the plane
122
+ and containing only one node from the triangulation. Each point in one
123
+ node's Voronoi polygon is closer to that node than any other node.
118
124
119
125
To compute the Natural Neighbors interpolant, we consider adding the
120
- interpolation point to the triangulation. We define the natural neighbors of
121
- this point as the set of nodes participating in Delaunay triangles whose
122
- circumcircles contain the point. To restore the Delaunay-ness of the
126
+ interpolation point to the triangulation. We define the natural neighbors
127
+ of this point as the set of nodes participating in Delaunay triangles
128
+ whose circumcircles contain the point. To restore the Delaunay-ness of the
123
129
triangulation, one would only have to alter those triangles and Voronoi
124
- polygons. The new Voronooi diagram would have a polygon around the inserted
125
- point. This polygon would "steal" area from the original Voronoi polygons.
126
- For each node i in the natural neighbors set, we compute the area stolen
127
- from its original Voronoi polygon, stolen[i]. We define the natural
128
- neighbors coordinates
130
+ polygons. The new Voronoi diagram would have a polygon around the
131
+ inserted point. This polygon would "steal" area from the original Voronoi
132
+ polygons. For each node i in the natural neighbors set, we compute the
133
+ area stolen from its original Voronoi polygon, stolen[i]. We define the
134
+ natural neighbors coordinates
129
135
130
136
phi[i] = stolen[i] / sum(stolen,axis=0)
131
137
@@ -134,8 +140,8 @@ class NNInterpolator(object):
134
140
135
141
The interpolated surface is C1-continuous except at the nodes themselves
136
142
across the convex hull of the input points. One can find the set of points
137
- that a given node will affect by computing the union of the areas covered by
138
- the circumcircles of each Delaunay triangle that node participates in.
143
+ that a given node will affect by computing the union of the areas covered
144
+ by the circumcircles of each Delaunay triangle that node participates in.
139
145
"""
140
146
141
147
def __init__ (self , triangulation , z , default_value = np .nan ):
@@ -145,7 +151,8 @@ def __init__(self, triangulation, z, default_value=np.nan):
145
151
146
152
def __getitem__ (self , key ):
147
153
x0 , x1 , xstep , y0 , y1 , ystep = slice2gridspec (key )
148
- grid = nn_interpolate_grid (x0 , x1 , xstep , y0 , y1 , ystep , self .default_value ,
154
+ grid = nn_interpolate_grid (
155
+ x0 , x1 , xstep , y0 , y1 , ystep , self .default_value ,
149
156
self .triangulation .x , self .triangulation .y , self .z ,
150
157
self .triangulation .circumcenters ,
151
158
self .triangulation .triangle_nodes ,
0 commit comments