/
BilinearInterpolatorTest.java
136 lines (121 loc) · 3.86 KB
/
BilinearInterpolatorTest.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2002-2011, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.process.vector;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Tests the {@link BilinearInterpolator}
*
* @author Martin Davis, OpenGeo
*
*/
public class BilinearInterpolatorTest {
@Test
public void testSquareWithTriangleData() {
float NO_DATA = -99.0f;
float[][] input = new float[2][2];
input[0][0] = 0;
input[0][1] = NO_DATA;
input[1][0] = 10;
input[1][1] = 20;
BilinearInterpolator interp = new BilinearInterpolator(input, NO_DATA);
float[][] output = interp.interpolate(10, 10, true);
printGrid(output);
//assertTrue(isMonotonicTriangle(output, 0, 0, 10, 10, true, NO_DATA));
assertTrue(isMonotonic(output, NO_DATA));
}
private boolean isMonotonic(float[][] grid, float noDataValue) {
// check monotonicity in X direction
for (int j = 0; j < grid[0].length; j++) {
float slice[] = sliceX(grid, j);
if (!isMonotonicSequence(slice, noDataValue))
return false;
}
// check monotonicity in Y direction
for (int x = 0; x < grid[0].length; x++) {
float slice[] = sliceY(grid, x);
if (!isMonotonicSequence(slice, noDataValue))
return false;
}
return true;
}
/**
* Extracts a slice of a grid along the X dimension (a row)
*
* @param grid
* @param j
* @return
*/
private float[] sliceX(float[][] grid, int y) {
float[] slice = new float[grid.length];
for (int i = 0; i < slice.length; i++) {
slice[i] = grid[i][y];
}
return slice;
}
/**
* Extracts a slice of a grid along the Y dimension (a column)
*
* @param grid
* @param y
* @return
*/
private float[] sliceY(float[][] grid, int x) {
float[] slice = new float[grid[0].length];
for (int i = 0; i < slice.length; i++) {
slice[i] = grid[x][i];
}
return slice;
}
/**
* Checks if a sequence of values is monotonic,
* ignoring values at the end of the sequence which are NO_DATA.
*
* @param seq
* @param noDataValue
* @return
*/
private boolean isMonotonicSequence(float[] seq, final float noDataValue)
{
int istart = 0;
for (int i = 0; i < seq.length; i++) {
if (seq[i] != noDataValue)
istart = i;
}
int iend = 0;
for (int i = seq.length - 1; i >= 0; i--) {
if (seq[i] != noDataValue)
iend = i;
}
float globalSlope = Math.signum(seq[iend] - seq[istart]);
// verify slope is identical throughout sequence
for (int i = istart; i < iend; i++) {
float localSlope = Math.signum(seq[i+1] - seq[i]);
if (localSlope != globalSlope)
return false;
}
return true;
}
private void printGrid(float[][] grid) {
for (int j = grid[0].length - 1; j >= 0; j--) {
for (int i = 0; i < grid.length; i++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
}
}