gridviz-smoothing 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,130 @@
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ import { Style } from '../Style.js'
5
+ import { makeWebGLCanvas } from '../utils/webGLUtils'
6
+ import { WebGLSquareColoringKS } from '../utils/WebGLSquareColoringKS'
7
+
8
+ /**
9
+ * @author Julien Gaffuri
10
+ */
11
+ export class KernelSmoothingStyle extends Style {
12
+ //see
13
+ //https://stackoverflow.com/questions/8099979/creating-a-glsl-arrays-of-uniforms
14
+ //see https://gist.github.com/jasonkit/c5b4fd62e8cbfe2780cc
15
+
16
+ /** @param {object} opts */
17
+ constructor(opts) {
18
+ super(opts)
19
+ opts = opts || {}
20
+
21
+ /**
22
+ * The name of the column/attribute of the tabular data where to retrieve the variable for color.
23
+ * @type {string} */
24
+ this.colorCol = opts.colorCol
25
+
26
+ /**
27
+ * A function returning the t value (within [0,1]) of the cell.
28
+ * @type {function(number,number,import("../Style").Stat):number} */
29
+ this.tFun = opts.tFun || ((v, r, s) => v / s.max)
30
+
31
+ /**
32
+ * Distribution stretching method.
33
+ * The stretching is performed on GPU side (fragment shader).
34
+ * @type {{ fun:string, alpha:number }} */
35
+ this.stretching = opts.stretching
36
+
37
+ /**
38
+ * The sample of the color ramp.
39
+ * The color is computed on GPU side (fragment shader) based on those values (linear interpolation).
40
+ * @type {Array.<string>} */
41
+ this.colors =
42
+ opts.colors ||
43
+ [
44
+ 'rgb(158, 1, 66)',
45
+ 'rgb(248, 142, 83)',
46
+ 'rgb(251, 248, 176)',
47
+ 'rgb(137, 207, 165)',
48
+ 'rgb(94, 79, 162)',
49
+ ].reverse()
50
+ if (opts.color)
51
+ this.colors = [
52
+ opts.color(0),
53
+ opts.color(0.2),
54
+ opts.color(0.4),
55
+ opts.color(0.6),
56
+ opts.color(0.8),
57
+ opts.color(1),
58
+ ]
59
+
60
+ /**
61
+ * A function returning the size of the cells, in geographical unit. All cells have the same size.
62
+ * @type {function(number,number):number} */
63
+ this.size = opts.size // (resolution, zf) => ...
64
+ }
65
+
66
+ /**
67
+ * @param {Array.<import("../Dataset").Cell>} cells
68
+ * @param {number} r
69
+ * @param {import("../GeoCanvas").GeoCanvas} cg
70
+ */
71
+ draw(cells, r, cg) {
72
+ //filter
73
+ if (this.filter) cells = cells.filter(this.filter)
74
+
75
+ //zoom factor
76
+ const zf = cg.getZf()
77
+
78
+ //compute color variable statistics
79
+ const statColor = Style.getStatistics(cells, (c) => c[this.colorCol], true)
80
+
81
+ if (!statColor) return
82
+
83
+ //create canvas and webgl renderer
84
+ const cvWGL = makeWebGLCanvas(cg.w + '', cg.h + '')
85
+ if (!cvWGL) {
86
+ console.error('No webGL')
87
+ return
88
+ }
89
+
90
+ //add vertice and fragment data
91
+ const r2 = r / 2
92
+ let c,
93
+ nb = cells.length
94
+ const verticesBuffer = []
95
+ //const tBuffer = []
96
+ for (let i = 0; i < nb; i++) {
97
+ c = cells[i]
98
+ const t = this.tFun(c[this.colorCol], r, statColor)
99
+ if (t == null || t == undefined) continue
100
+ verticesBuffer.push(c.x + r2, c.y + r2)
101
+ //tBuffer.push(t > 1 ? 1 : t < 0 ? 0 : t)
102
+ }
103
+
104
+ const sizeGeo = this.size ? this.size(r, zf) : r + 0.2 * zf
105
+ const wgp = new WebGLSquareColoringKS(cvWGL.gl, this.colors, this.stretching, sizeGeo / zf)
106
+
107
+ //TODO - [i,j,t]
108
+ let data = []
109
+ for (const c of cells) {
110
+ const xGL = cg.geoToPixX(c.x + r2) //TODO within [-1,1] ?
111
+ data.push(xGL)
112
+ const yGL = cg.geoToPixY(c.y + r2) //TODO within [-1,1] ?
113
+ data.push(yGL)
114
+ const t = this.tFun(c[this.colorCol], r, statColor)
115
+ data.push(t)
116
+ }
117
+
118
+ data = [0.0, 2.0, 1.0, 0.0, 0.0, 0.0]
119
+
120
+ //draw
121
+ wgp.draw(verticesBuffer, data, cg.getWebGLTransform())
122
+
123
+ //draw in canvas geo
124
+ cg.initCanvasTransform()
125
+ cg.ctx.drawImage(cvWGL.canvas, 0, 0)
126
+
127
+ //update legends
128
+ this.updateLegends({ style: this, r: r, zf: zf, sColor: statColor })
129
+ }
130
+ }
package/src/index.js ADDED
@@ -0,0 +1,6 @@
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ export { KernelSmoothingStyle } from './KernelSmoothingStyle.js'
5
+ //export { KernelSmoothingOldStyle } from "./KernelSmoothingStyle"
6
+ //export { KernelSmoothingWGLStyle } from "./KernelSmoothingStyle"