@pirireis/webglobeplugins 0.6.27-a → 0.6.28-b

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.
@@ -1,406 +0,0 @@
1
- import { LineOnGlobeCache } from '../programs/line-on-globe/naive-accurate';
2
- import { CircleCache } from '../programs/line-on-globe/circle-accurate';
3
- import { LineToTheOriginCache } from "../programs/line-on-globe/to-the-origin"
4
- import { BufferOrchestrator, BufferManager } from "../util/account";
5
- import { ChainListMap } from "./chain-list-map";
6
- import { mapGetOrThrow } from "../util/check/get";
7
- import { keyMethod } from "./util";
8
- import { ContextTextWriter3 } from '../write-text/context-text3';
9
- // TODO: Add update buffer and update text mehods on add, delete, update methods
10
-
11
-
12
- /**
13
- * Insert info to chain list map (nodes and properties)
14
- *
15
- * ask chain list map for text data
16
- * ask chain list map for buffer data
17
- *
18
- *
19
- * addNode
20
- * insertBulk
21
- * deleteChain
22
- * deleteNodes
23
- * updateCoordinates()
24
- * updateChainProperties
25
- */
26
-
27
-
28
- /**
29
- * @typedef StyleProperties
30
- * @property {[0-1,0-1,0-1,0-1]} rgba
31
- * @property { 0-1 } dashOpacity
32
- * @property { 0-1 } dashRatio
33
- * @property {0-360} circleDashAngle
34
- * @property {Array<node>} nodes
35
- */
36
-
37
- const radian = Math.PI / 180;
38
-
39
- export class CircleLineChainPlugin {
40
- /**
41
- *
42
- * @param {*} id
43
- * @param {Map<[key, ContextTextWriter3]} textContextWriterInjectionMap //import { ContextTextWriter3 } from "@pirireis/webglobeplugins/write-text/context-text3";
44
- */
45
- constructor(id, {
46
- drawCircleOn = true,
47
- textContextWriterInjectionMap = new Map(),
48
- textDataPreAdaptor = null
49
- } = {}) {
50
- this.id = id;
51
- this._checkTextContextWriterInjectionMap(textContextWriterInjectionMap);
52
- this._textContextWriterInjectionMap = textContextWriterInjectionMap;
53
- this._textContextWriterInjectionMap.forEach((writer) => writer.setKeyAdaptor((v, i, c, properties) => v.__identity__));
54
- this._opacity = 1;
55
- this._chainListMap = new ChainListMap(keyMethod);
56
- this.bufferOrchestrator = new BufferOrchestrator({ capacity: 10 });
57
- this._drawCircleOn = drawCircleOn;
58
- this._textDataPreAdaptor = textDataPreAdaptor;
59
- }
60
-
61
- // init
62
-
63
- init(globe, gl) {
64
- this.gl = gl;
65
- this.globe = globe
66
- this._initOrchestrations()
67
- }
68
-
69
- _checkTextContextWriterInjectionMap(textContextWriterInjectionMap) {
70
- if (!(textContextWriterInjectionMap instanceof Map)) throw new Error("textContextWriterInjectionMap is not an instance of Map");
71
- textContextWriterInjectionMap.forEach((v) => {
72
- if (!(v instanceof ContextTextWriter3)) throw new Error("textContextWriterInjectionMap element is not an instance of ContextTextWriter3");
73
- });
74
- }
75
-
76
- _initOrchestrations() {
77
- const { gl, globe } = this;
78
- this.lineProgram = LineOnGlobeCache.get(globe);
79
- this.circleProgram = CircleCache.get(globe);
80
- this.lineToTheOriginProgram = LineToTheOriginCache.get(globe);
81
- {
82
- // createBuffers
83
- const bufferType = "DYNAMIC_DRAW";
84
- const initialCapacity = this.bufferOrchestrator.capacity;
85
- this.bufferManagersCompMap = new Map(
86
- [
87
- ["centerCoords2d", {
88
- 'bufferManager': new BufferManager(gl, 2, { bufferType, initialCapacity }),
89
- 'adaptor': (item) => new Float32Array(globe.api_GetMercator2DPoint(item.long, item.lat)),
90
- }],
91
- ["centerCoords3d", {
92
- 'bufferManager': new BufferManager(gl, 3, { bufferType, initialCapacity }),
93
- 'adaptor': (item) => new Float32Array(globe.api_GetCartesian3DPoint(item.long, item.lat, 0, 0)),
94
- }],
95
- ["targetCoords2d", {
96
- 'bufferManager': new BufferManager(gl, 2, { bufferType, initialCapacity }),
97
- 'adaptor': (item) => new Float32Array(globe.api_GetMercator2DPoint(item.targetLong, item.targetLat)),
98
- }],
99
- ["targetCoords3d", {
100
- 'bufferManager': new BufferManager(gl, 3, { bufferType, initialCapacity }),
101
- 'adaptor': (item) => new Float32Array(globe.api_GetCartesian3DPoint(item.targetLong, item.targetLat, 0, 0)),
102
- }],
103
- ["rgba", {
104
- 'bufferManager': new BufferManager(gl, 4, { bufferType, initialCapacity }),
105
- 'adaptor': (item) => {
106
- if (item.lineProperties?.rgba) return new Float32Array(item.lineProperties.rgba);
107
- return new Float32Array(item.chainProperties.rgba);
108
- }
109
- }],
110
- ["bigRadius", {
111
- 'bufferManager': new BufferManager(gl, 1, { bufferType, initialCapacity }),
112
- 'adaptor': (item) => new Float32Array([item.bigRadius])
113
- }],
114
- ["dashRatio", {
115
- 'bufferManager': new BufferManager(gl, 1, { bufferType, initialCapacity }),
116
- 'adaptor': (item) => new Float32Array([item.chainProperties.dashRatio])
117
- }],
118
-
119
- ["dashOpacity", {
120
- 'bufferManager': new BufferManager(gl, 1, { bufferType, initialCapacity }),
121
- 'adaptor': (item) => new Float32Array([item.chainProperties.dashOpacity])
122
- }],
123
- ["circleDashAngle", {
124
- 'bufferManager': new BufferManager(gl, 1, { bufferType, initialCapacity }),
125
- 'adaptor': (item) => {
126
- if (item.circleProperties?.circleDashAngle) return new Float32Array([item.circleProperties.circleDashAngle / 360]);
127
- return new Float32Array([item.chainProperties.circleDashAngle / 360]);
128
- }
129
- }],
130
- ["rgbaCircle", {
131
- "bufferManager": new BufferManager(gl, 4, { bufferType, initialCapacity }),
132
- "adaptor": (item) => {
133
- if (item.circleProperties?.rgba) return new Float32Array(item.circleProperties.rgba);
134
- return new Float32Array(item.chainProperties.rgba);
135
- }
136
- }]
137
- ]
138
- );
139
- // (startPotisionBufferObj, endPositionBufferObj, dashRatioBufferObj, colorBufferObj)
140
- const obj = function (bufferManagerComp) {
141
- return { 'buffer': bufferManagerComp.bufferManager.buffer, 'stride': 0, 'offset': 0 }
142
- };
143
- this.lineVao = this.lineProgram.createVAO(
144
- ...['centerCoords2d', 'centerCoords3d', 'targetCoords2d', 'targetCoords3d', 'dashRatio', 'dashOpacity', 'rgba'].map(key => obj(this.bufferManagersCompMap.get(key))));
145
- this.circleVao = this.circleProgram.createVAO(
146
- ...["centerCoords2d", "centerCoords3d", "bigRadius", "rgbaCircle", "circleDashAngle", "dashOpacity"].map(key => obj(this.bufferManagersCompMap.get(key))));
147
- this.toOriginVao = this.lineToTheOriginProgram.createVAO(
148
- ...["targetCoords3d", "rgba"].map(key => obj(this.bufferManagersCompMap.get(key))));
149
- }
150
-
151
- }
152
-
153
-
154
- // API
155
- setDrawCircleOn(bool) {
156
- if (typeof bool !== 'boolean') throw new Error("setDrawCircleOn parameter must be a boolean");
157
- this._drawCircleOn = bool;
158
- this.globe.DrawRender();
159
- }
160
- // -- update bulk family
161
- /**
162
- *
163
- * @param {Array<chain>} data
164
- * @typedef chain
165
- * @property {string} chainKey
166
- * @property {Array<node>} nodes
167
- * @typedef {Object} node
168
- * @property {string} key
169
- * @property {number} long
170
- * @property {number} lat
171
- */
172
- updateCoordinatesBulk(data, { textWriterIDs = [] } = {}) {
173
- // update implicit data structure
174
- // find keys to update.. (inserted data and the radius of "from")
175
- // updateBuffers
176
- // update text
177
- const chainKeys = [];
178
- for (const chain of data) {
179
- chainKeys.push(chain.chainKey);
180
- chain.nodes.forEach((node) => {
181
- this._chainListMap.updateCoordsinatesOfNode(node, chain.chainKey);
182
- });
183
- }
184
- this._reconstructChains(chainKeys);
185
- this._updateTexts(chainKeys, textWriterIDs);
186
- }
187
-
188
-
189
- /**
190
- *
191
- * @param {*} chainKey
192
- * @param {Array<{nodeKey, circleProperties:Map[propertyName ,value], lineProperties:Map[propertyName ,value] }} propertyMap
193
- */
194
- updateNodesProperties(chainKey, nodesAndPropertyMap, { textWriterIDs = [] } = {}) {
195
- this._chainListMap.updateNodesProperties(chainKey, nodesAndPropertyMap)
196
- this._reconstructChains([chainKey]);
197
- this._updateTexts([chainKey], textWriterIDs);
198
- this.globe.DrawRender();
199
- }
200
-
201
- /**
202
- *
203
- * @param {*} chainKey
204
- * @param {Map<propertyName ,value} propertyMap
205
- */
206
- updateChainProperties(chainKey, propertyMap, { textWriterIDs = [] } = {}) {
207
- this._chainListMap.updateChainProperties(chainKey, propertyMap);
208
- this._reconstructChains([chainKey]);
209
- if (textWriterIDs) this._updateTexts([chainKey], textWriterIDs);
210
- }
211
-
212
- // ---- insertBulk family
213
- /**
214
- *
215
- * @param {Array<chain>} data
216
- * @typedef chain
217
- * @property {string} chainKey
218
- * @property {StyleProperties} chainProperties
219
-
220
- * } chainProperties
221
- *
222
- * @typedef {Object} node
223
- * @property {string} key
224
- * @property {number} long
225
- * @property {number} lat
226
- * @property {StyleProperties} circleProperties
227
- */
228
- insertBulk(data, { textWriterIDs = [] } = []) {
229
- // first insert everything to implicit structure,
230
- // then iterate over data again to update text
231
- // let _reconstractChainBufferData method interact with data and bufferOrchestrator.
232
- const chainKeysToConstruct = [];
233
-
234
- for (const { chainKey, chainProperties, nodes } of data) {
235
- this._chainListMap.setChain(chainKey, nodes);
236
- this._chainListMap.setChainProperties(chainKey, chainProperties);
237
- chainKeysToConstruct.push(chainKey);
238
- }
239
- this._reconstructChains(chainKeysToConstruct);
240
- if (textWriterIDs) this._updateTexts(chainKeysToConstruct, textWriterIDs);
241
- this.globe.DrawRender();
242
- }
243
-
244
- /**
245
- *
246
- * @param {{key, long, lat}} node
247
- * @param {*} chainKey
248
- * @param {*} theNodeKeyFront | node key of the next node, null places to the last
249
- */
250
- addNode(node, chainKey, { theNodeKeyFront = null, textWriterIDs = [] } = {}) {
251
- // TODO: if before object is null update the last two elements of the chain only.
252
- this._chainListMap.addNode(node, chainKey, theNodeKeyFront);
253
- if (textWriterIDs) this._updateTexts([chainKey], textWriterIDs);
254
- this._reconstructChains([chainKey]);
255
- this.globe.DrawRender();
256
-
257
- }
258
-
259
-
260
-
261
- updateNodeCoordinates(node, chainKey, { textWriterIDs = [] } = {}) {
262
- this._chainListMap.updateCoordsinatesOfNode(node, chainKey);
263
- if (textWriterIDs) this._updateTexts([chainKey], textWriterIDs);
264
- this._reconstructChains([chainKey]);
265
- this.globe.DrawRender();
266
- }
267
-
268
-
269
-
270
- setOpacity(opacity) {
271
- if (typeof opacity !== 'number') throw new Error("opacity must be a number");
272
- if (opacity < 0 || 1 < opacity) throw new Error("opacity must be between 0-1");
273
- this._textContextWriterInjectionMap.forEach((writer) => writer.setOpacity(opacity));
274
- this._opacity = opacity;
275
- this.globe.DrawRender();
276
- }
277
-
278
-
279
-
280
- getChain(chainKey) {
281
- this._chainListMap.getChain(chainKey);
282
- }
283
-
284
- /**
285
- *
286
- * @param {*} chainKeys
287
- */
288
- deleteChains(chainKeys) {
289
- const bufferKeys = [];
290
- for (const chainKey of chainKeys) {
291
- bufferKeys.push(...this._chainListMap.deleteChainAndReturnChainKeys(chainKey));
292
- }
293
- this._textContextWriterInjectionMap.forEach((writer) => writer.deleteTextBulk(bufferKeys));
294
- this._updateTexts(chainKeys, this._textContextWriterInjectionMap.keys());
295
- this.bufferOrchestrator.deleteBulk(bufferKeys, this.bufferManagersCompMap);
296
- this.globe.DrawRender();
297
- }
298
-
299
-
300
- /**
301
- *
302
- * @param {Array<{chainKey, nodeKeys:[]} keysAndNodes
303
- */
304
- deleteNodes(keysAndNodes, { textWriterIDs = [] } = {}) {
305
- const bufferKeys = [];
306
- const chainKeysToReconstuct = [];
307
- keysAndNodes.forEach(({ chainKey, nodeKeys }) => {
308
- bufferKeys.push(...this._chainListMap.deleteNodesBelongToAChain(chainKey, nodeKeys));
309
- chainKeysToReconstuct.push(chainKey);
310
- });
311
- this._textContextWriterInjectionMap.forEach((writer) => writer.deleteTextBulk(bufferKeys));
312
- this.bufferOrchestrator.deleteBulk(bufferKeys, this.bufferManagersCompMap);
313
- this._reconstructChains(chainKeysToReconstuct);
314
- this._updateTexts(chainKeysToReconstuct, textWriterIDs);
315
- this.globe.DrawRender();
316
-
317
- }
318
-
319
-
320
- updateText(textWriterIDs) {
321
- const chainKeyIterator = this._chainListMap.getAllChainKeysIterator();
322
- this._updateTexts(chainKeyIterator, textWriterIDs);
323
- }
324
-
325
-
326
- // implicit
327
-
328
- _updateTexts(chainKeys, textWriterIDs) {
329
- if (textWriterIDs.length === 0) return;
330
- const textWriters = textWriterGetOrThrow(this._textContextWriterInjectionMap, textWriterIDs)
331
- chainKeys.forEach((chainKey) => {
332
- this._chainListMap.textUpdate(chainKey, textWriters, this._textDataPreAdaptor);
333
- })
334
- }
335
-
336
-
337
- _reconstructChains(chainKeys) {
338
- const { globe } = this;
339
- // this.lineVao = this.lineProgram.createVAO(
340
- // ...['centerCoords', 'targetCoords', 'dashRatio', 'dashOpacity', 'rgba'].map(key => obj(this.bufferManagersCompMap.get(key))));
341
- // this.circleVao = this.circleProgram.createVAO(
342
- // ...["centerCoords", "bigRadius", "rgba", "circleDashAngle", "dashOpacity"].map(key => obj(this.bufferManagersCompMap.get(key))));
343
- // }
344
-
345
- const radiusM = radiusMethod(globe);
346
- const callback = (v, i, array, chainProperties) => {
347
- if (i == array.length - 1) return null;
348
- return {
349
- chainProperties: chainProperties,
350
- bigRadius: radiusM(v, i, array),
351
- targetLong: array[i + 1].long,
352
- targetLat: array[i + 1].lat,
353
- long: v.long,
354
- lat: v.lat,
355
- lineProperties: v.lineProperties,
356
- circleProperties: v.circleProperties,
357
- key: v.__identity__
358
- }
359
- }
360
- const bulkData = [];
361
- chainKeys.forEach((k) => {
362
- this._chainListMap.calculateBufferPropertiesChain(k, callback, bulkData);
363
- })
364
-
365
- this._insertBulk(bulkData);
366
- }
367
-
368
-
369
- _insertBulk(bulkData) {
370
- this.bufferOrchestrator.insertBulk(bulkData, this.bufferManagersCompMap);
371
- this.globe.DrawRender();
372
- }
373
-
374
-
375
- // GLOBE API
376
- //TODO:
377
- free() {
378
- if (this.isFreed) return;
379
- this.bufferManagersCompMap.forEach(({ bufferManager }) => {
380
- bufferManager.free();
381
- })
382
- LineOnGlobeCache.release(this.globe);
383
- CircleCache.release(this.globe);
384
- this.lineProgram = null;
385
- this.circleProgram = null;
386
- this.isFreed = true;
387
- }
388
-
389
- draw3D() {
390
- const { gl, globe } = this;
391
- gl.disable(gl.DEPTH_TEST);
392
- this.lineProgram.draw(this.lineVao, this.bufferOrchestrator.length, this._opacity);
393
- if (this._drawCircleOn) this.circleProgram.draw(this.circleVao, this.bufferOrchestrator.length, this._opacity);
394
- this._textContextWriterInjectionMap.forEach((writer) => writer.draw());
395
- this.lineToTheOriginProgram.draw(this.toOriginVao, this.bufferOrchestrator.length, this._opacity);
396
- gl.enable(gl.DEPTH_TEST);
397
- }
398
- }
399
-
400
-
401
-
402
- const radiusMethod = (globe) => (v, i, array) => {
403
- return globe.Math.GetDist3D(v.long, v.lat, array[i + 1].long, array[i + 1].lat)
404
- }
405
-
406
- const textWriterGetOrThrow = mapGetOrThrow("textWriterIds is invalid")