1 | |
---|
2 | """Example where a surface is fitted to one set of points. |
---|
3 | Then that surface is interpolated using another set of points. |
---|
4 | """ |
---|
5 | |
---|
6 | import sys |
---|
7 | from os import sep |
---|
8 | sys.path.append('..'+sep+'pyvolution') |
---|
9 | from Numeric import array, allclose |
---|
10 | from least_squares import Interpolation |
---|
11 | |
---|
12 | |
---|
13 | #Simple function to provide example z values (z = x+y) |
---|
14 | def linear_function(point): |
---|
15 | point = array(point) |
---|
16 | return point[:,0]+point[:,1] |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | #Setup mesh used to represent fitted function |
---|
21 | a = [0.0, 0.0] |
---|
22 | b = [0.0, 2.0] |
---|
23 | c = [2.0, 0.0] |
---|
24 | d = [0.0, 4.0] |
---|
25 | e = [2.0, 2.0] |
---|
26 | f = [4.0, 0.0] |
---|
27 | |
---|
28 | points = [a, b, c, d, e, f] |
---|
29 | #bac, bce, ecf, dbe, daf, dae |
---|
30 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
31 | |
---|
32 | #Datapoints to fit from |
---|
33 | data_points1 = [[ 0.66666667, 0.66666667], |
---|
34 | [ 1.33333333, 1.33333333], |
---|
35 | [ 2.66666667, 0.66666667], |
---|
36 | [ 0.66666667, 2.66666667], |
---|
37 | [ 0.0, 1.0], |
---|
38 | [ 0.0, 3.0], |
---|
39 | [ 1.0, 0.0], |
---|
40 | [ 1.0, 1.0], |
---|
41 | [ 1.0, 2.0], |
---|
42 | [ 1.0, 3.0], |
---|
43 | [ 2.0, 1.0], |
---|
44 | [ 3.0, 0.0], |
---|
45 | [ 3.0, 1.0]] |
---|
46 | |
---|
47 | z = linear_function(data_points1) #Z-values |
---|
48 | |
---|
49 | |
---|
50 | #Fit surface to mesh |
---|
51 | interp = Interpolation(points, triangles, data_points1, alpha=0.0) |
---|
52 | f = interp.fit(z) #Fitted values at vertices |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | #New datapoints where interpolated values are sought |
---|
57 | data_points2 = [[ 0.0, 0.0], |
---|
58 | [ 0.5, 0.5], |
---|
59 | [ 1.0, 0.5], |
---|
60 | [ 2.8, 1.2]] |
---|
61 | |
---|
62 | |
---|
63 | #Build new A matrix based on new points |
---|
64 | interp.build_interpolation_matrix_A(data_points2) |
---|
65 | |
---|
66 | #Interpolate using fitted surface |
---|
67 | z1 = interp.interpolate(f) |
---|
68 | |
---|
69 | #Check result |
---|
70 | answer = linear_function(data_points2) |
---|
71 | |
---|
72 | print 'Result from fit:', z1 |
---|
73 | print 'Expected result:', answer |
---|
74 | |
---|
75 | assert allclose(z1, answer) |
---|
76 | print 'OK' |
---|