-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReflectivityLayer.kt
More file actions
266 lines (222 loc) · 9.51 KB
/
ReflectivityLayer.kt
File metadata and controls
266 lines (222 loc) · 9.51 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package dev.abhishekbansal.nexrad.layers
import android.content.Context
import android.opengl.GLES20
import com.google.gson.Gson
import dev.abhishekbansal.nexrad.R
import dev.abhishekbansal.nexrad.models.Reflectivity
import dev.abhishekbansal.nexrad.utils.Shader
import dev.abhishekbansal.nexrad.utils.extensions.rawResToString
import timber.log.Timber
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import kotlin.math.cos
import kotlin.math.sin
class ReflectivityLayer(val context: Context) : Layer {
/**
* Approximation of distance covered along longitude or latitude in a per degree
*/
private val meterPerDegree = 111111
/**
* How many bytes per float.
*/
private val bytesPerFloat = 4
/**
* This will be used to pass in model position information.
*/
private var positionHandle: Int = 0
/**
* This will be used to pass in the transformation matrix.
*/
private var mvpMatrixHandle: Int = 0
/**
* This will be used to pass in model color information.
*/
private var colorHandle = 0
/**
* Offset of the position data.
*/
private val positionOffset = 0
/**
* Size of the position data in elements.
*/
private val positionDataSize = 3
/**
* Offset of the color data.
*/
private val colorOffset = 3
/**
* Size of the color data in elements.
*/
private val colorDataSize = 3
/**
* How many elements per vertex.
*/
private val strideBytes = (colorDataSize + positionDataSize) * bytesPerFloat
private val meshShader by lazy {
Shader(
context.rawResToString(R.raw.basic_vertex),
context.rawResToString(R.raw.basic_fragment)
)
}
private var meshSize = 0
/**
* Store our model data in a float buffer.
*/
private lateinit var meshVertices: FloatBuffer
override fun prepare() {
generateVertexData()
meshShader.link(arrayOf("a_Position", "a_Reflectivity"))
// Set program handles. These will later be used to pass in values to the program.
// Set program handles. These will later be used to pass in values to the program.
// Set program handles. These will later be used to pass in values to the program.
mvpMatrixHandle = GLES20.glGetUniformLocation(meshShader.handle, "u_MVPMatrix")
positionHandle = GLES20.glGetAttribLocation(meshShader.handle, "a_Position")
colorHandle = GLES20.glGetAttribLocation(meshShader.handle, "a_Color")
}
override fun draw(mvpMatrix: FloatArray) {
// Pass in the position information
meshShader.activate()
// Pass in the position information
meshVertices.position(positionOffset)
GLES20.glVertexAttribPointer(
positionHandle, positionDataSize, GLES20.GL_FLOAT, false,
strideBytes, meshVertices
)
GLES20.glEnableVertexAttribArray(positionHandle)
// Pass in the color information
// Pass in the color information
meshVertices.position(colorOffset)
GLES20.glVertexAttribPointer(
colorHandle, colorDataSize, GLES20.GL_FLOAT, false,
strideBytes, meshVertices
)
GLES20.glEnableVertexAttribArray(colorHandle)
GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0)
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, meshSize)
meshShader.deactivate()
}
private fun generateVertexData() {
val data = getData(context)
// per vertex data = 3xyz + 3rgb
val perVertexElements = 3 + 3
// since we don't want go beyond last gate hence gate-1
val totalVertices = data.azimuth.size * (data.gates.size - 1)
// Each vertex is part of 6 triangles,
meshSize = totalVertices * perVertexElements * 6
val reflectivityMesh = FloatArray(meshSize)
var index = 0
val nAzimuth = data.azimuth.size
val nGates = data.gates.size
for (r in 0 until nGates - 1) {
// since we want distances along longitude we approximate that by dividing approximate meters in a degree
val radius1 = data.gates[r] / meterPerDegree
val radius2 = data.gates[r + 1] / meterPerDegree
for (angleIndex in 0 until nAzimuth) {
// vertex 1 x
val angle = data.azimuth[angleIndex]
val angle2 = if (angleIndex != nAzimuth - 1) {
data.azimuth[angleIndex + 1]
} else {
data.azimuth[0]
}
val reflectivity = data.reflectivity[angleIndex][r]
// precalculate coordinates
val r1SinTheta1 = radius1 * sin(Math.toRadians(angle.toDouble())).toFloat()
val r1SinTheta2 = radius1 * sin(Math.toRadians(angle2.toDouble())).toFloat()
val r2SinTheta2 = radius2 * sin(Math.toRadians(angle2.toDouble())).toFloat()
val r2SinTheta1 = radius2 * sin(Math.toRadians(angle.toDouble())).toFloat()
val r1CosTheta1 = radius1 * cos(Math.toRadians(angle.toDouble())).toFloat()
val r1CosTheta2 = radius1 * cos(Math.toRadians(angle2.toDouble())).toFloat()
val r2CosTheta2 = radius2 * cos(Math.toRadians(angle2.toDouble())).toFloat()
val r2CosTheta1 = radius2 * cos(Math.toRadians(angle.toDouble())).toFloat()
///// Begin Triangle 1 /////
// r1 theta1
reflectivityMesh[index++] = r1SinTheta1
reflectivityMesh[index++] = r1CosTheta1
reflectivityMesh[index++] = 0.0f
// color information for triangle 1 vertex 1
reflectivityMesh[index++] = getColor(reflectivity)[0]
reflectivityMesh[index++] = getColor(reflectivity)[1]
reflectivityMesh[index++] = getColor(reflectivity)[2]
// r2 theta1
reflectivityMesh[index++] = r2SinTheta1
reflectivityMesh[index++] = r2CosTheta1
reflectivityMesh[index++] = 0.0f
// color information for triangle 1 vertex 2
reflectivityMesh[index++] = getColor(reflectivity)[0]
reflectivityMesh[index++] = getColor(reflectivity)[1]
reflectivityMesh[index++] = getColor(reflectivity)[2]
// r1 theta2
reflectivityMesh[index++] = r1SinTheta2
reflectivityMesh[index++] = r1CosTheta2
reflectivityMesh[index++] = 0.0f
// color information for triangle 1 vertex 3
reflectivityMesh[index++] = getColor(reflectivity)[0]
reflectivityMesh[index++] = getColor(reflectivity)[1]
reflectivityMesh[index++] = getColor(reflectivity)[2]
///// Begin Triangle 2 /////
// r1 theta2
reflectivityMesh[index++] = r1SinTheta2
reflectivityMesh[index++] = r1CosTheta2
reflectivityMesh[index++] = 0.0f
// color information for triangle 1 vertex 3
reflectivityMesh[index++] = getColor(reflectivity)[0]
reflectivityMesh[index++] = getColor(reflectivity)[1]
reflectivityMesh[index++] = getColor(reflectivity)[2]
// r2 theta2
reflectivityMesh[index++] = r2SinTheta2
reflectivityMesh[index++] = r2CosTheta2
reflectivityMesh[index++] = 0.0f
// color information for triangle 1 vertex 3
reflectivityMesh[index++] = getColor(reflectivity)[0]
reflectivityMesh[index++] = getColor(reflectivity)[1]
reflectivityMesh[index++] = getColor(reflectivity)[2]
// r2 theta1
reflectivityMesh[index++] = r2SinTheta1
reflectivityMesh[index++] = r2CosTheta1
reflectivityMesh[index++] = 0.0f
// color information for triangle 1 vertex 2
reflectivityMesh[index++] = getColor(reflectivity)[0]
reflectivityMesh[index++] = getColor(reflectivity)[1]
reflectivityMesh[index++] = getColor(reflectivity)[2]
}
}
Timber.i("index: $index, meshsize: $meshSize")
// Initialize the buffers.
meshVertices = ByteBuffer.allocateDirect(reflectivityMesh.size * bytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer()
meshVertices.put(reflectivityMesh)?.position(0)
}
private fun getData(context: Context): Reflectivity {
return Gson().fromJson(context.rawResToString(R.raw.l3_data), Reflectivity::class.java)
}
private fun getColor(reflectivity: Float): FloatArray {
return when {
reflectivity <= 0 -> {
floatArrayOf(0.0f, 0.0f, 0.0f)
}
reflectivity < 10 -> {
floatArrayOf(1.0f, 1.0f, 0.0f)
}
reflectivity < 15 -> {
floatArrayOf(1.0f, 0.0f, 1.0f)
}
reflectivity < 20 -> {
floatArrayOf(0.0f, 0.0f, 1.0f)
}
reflectivity < 25 -> {
floatArrayOf(.5f, 1.0f, 0.0f)
}
reflectivity < 30 -> {
floatArrayOf(0.0f, 1.0f, 0.0f)
}
reflectivity < 35 -> {
floatArrayOf(0.0f, 1.0f, 1.0f)
}
else -> {
floatArrayOf(1.0f, .2f, .2f)
}
}
}
}