forked from mapsforge/mapsforge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleShadingAlgorithm.java
219 lines (183 loc) · 7.31 KB
/
SimpleShadingAlgorithm.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright 2017 usrusr
*
* This program 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, either version 3 of the License, or (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mapsforge.map.layer.hills;
import org.mapsforge.core.util.IOUtils;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Simple, but expressive slope visualisation (e.g. no pretentions of physical accuracy, separate north and west lightsources instead of one northwest, so a round dome would not look round, saturation works different depending on slope direction)
*
* <p>variations can be created by overriding {@link #exaggerate(double)}</p>
*/
public class SimpleShadingAlgorithm implements ShadingAlgorithm {
private static final Logger LOGGER = Logger.getLogger(SimpleShadingAlgorithm.class.getName());
public final double linearity;
public final double scale;
private byte[] lookup;
private int lookupOffset;
public SimpleShadingAlgorithm(){
this(0.1d, 0.666d);
}
/**
* customization constructor for controlling some parameters of the shading formula
* @param linearity 1 or higher for linear grade, 0 or lower for a triple-applied
* sine of grade that gives high emphasis on changes in slope in
* near-flat areas, but reduces details within steep slopes
* (default 0.1)
* @param scale scales the input slopes, with lower values slopes will saturate later, but nuances closer to flat will suffer
* (default: 0.666d)
*/
public SimpleShadingAlgorithm(double linearity, double scale) {
this.linearity = Math.min(1d, Math.max(0d, linearity));
this.scale = Math.max(0d, scale);
}
/**
* should calculate values from -128 to +127 using whatever range required (within reason)
* @param in a grade, ascent per projected distance (along coordinate axis)
*/
protected double exaggerate(double in) {
double x = in * scale;
x = Math.max(-128d, Math.min(128d, x));
double ret = (Math.sin(0.5d*Math.PI*Math.sin(0.5d*Math.PI*Math.sin(0.5d*Math.PI*x/128d)))*128*(1d-linearity)+x*linearity);
return ret;
}
@Override
public int getAxisLenght(HgtCache.HgtFileInfo source) {
long size = source.getSize();
long elements = size / 2;
int rowLen = (int) Math.ceil(Math.sqrt(elements));
if (rowLen * rowLen * 2 != size) {
return 0;
}
return rowLen - 1;
}
@Override
public RawShadingResult transformToByteBuffer(HgtCache.HgtFileInfo source, int padding) {
int axisLength = getAxisLenght(source);
int rowLen = axisLength + 1;
BufferedInputStream in = null;
try {
in = source.openInputStream();
byte[] bytes = convert(in, axisLength, rowLen, padding);
return new RawShadingResult(bytes, axisLength, axisLength, padding);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
return null;
} finally {
IOUtils.closeQuietly(in);
}
}
private byte[] convert(InputStream in, int axisLength, int rowLen, int padding) throws IOException {
byte[] bytes;
short[] ringbuffer = new short[rowLen];
bytes = new byte[(axisLength + 2 * padding) * (axisLength + 2 * padding)];
DataInputStream din = new DataInputStream(in);
byte[] lookup = this.lookup;
if(lookup==null) {
fillLookup();
lookup = this.lookup;
}
int outidx = (axisLength + 2 * padding) * padding + padding;
int rbcur = 0;
{
short last = 0;
for (int col = 0; col < rowLen; col++) {
last = readNext(din, last);
ringbuffer[rbcur++] = last;
}
}
for (int line = 1; line <= axisLength; line++) {
if (rbcur >= rowLen) {
rbcur = 0;
}
short nw = ringbuffer[rbcur];
short sw = readNext(din, nw);
ringbuffer[rbcur++] = sw;
for (int col = 1; col <= axisLength; col++) {
short ne = ringbuffer[rbcur];
short se = readNext(din, ne);
ringbuffer[rbcur++] = se;
int noso = -((se - ne) + (sw - nw));
int eawe = -((ne - nw) + (se - sw));
noso = (int)exaggerate(lookup, noso);
eawe = (int)exaggerate(lookup, eawe);
int zeroIsFlat = noso + eawe ;
int intVal = Math.min(255, Math.max(0, zeroIsFlat + 127));
int shade = intVal & 0xFF;
bytes[outidx++] = (byte) shade;
nw = ne;
sw = se;
}
outidx += 2 * padding;
}
return bytes;
}
private byte exaggerate(byte[] lookup, int x) {
return lookup[Math.max(0, Math.min(lookup.length-1, x+lookupOffset))];
}
private void fillLookup(){
int lowest = 0;
while(lowest > -1024){
double exaggerate = exaggerate(lowest);
double exaggerated = Math.round(exaggerate);
if(exaggerated<=-128 ||exaggerated >= 127) break;
lowest--;
}
int highest = 0;
while(highest < 1024){
double exaggerated = Math.round(exaggerate(highest));
if(exaggerated<=-128 ||exaggerated >= 127) break;
highest++;
}
int size = 1 + highest - lowest;
byte[] nextLookup = new byte[size];
int in = lowest;
for(int i=0;i<size;i++){
byte exaggerated = (byte) Math.round(exaggerate(in));
nextLookup[i]=exaggerated;
in++;
}
lookup=nextLookup;
lookupOffset=-lowest;
}
private static short readNext(DataInputStream din, short fallback) throws IOException {
short read = din.readShort();
if (read == Short.MIN_VALUE)
return fallback;
return read;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SimpleShadingAlgorithm that = (SimpleShadingAlgorithm) o;
if (Double.compare(that.linearity, linearity) != 0) return false;
return Double.compare(that.scale, scale) == 0;
}
@Override
public int hashCode() {
int result;
long temp;
temp = Double.doubleToLongBits(linearity);
result = (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(scale);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}