From 06ca7fd5a2d44e1b25f56c30710f0cd8eaa25727 Mon Sep 17 00:00:00 2001 From: ftoromanoff Date: Tue, 19 Sep 2023 14:51:20 +0200 Subject: [PATCH] refactor(FeatureCtx): move class FeatureContext to Style and rename --- src/Converter/Feature2Mesh.js | 43 ++---------------------- src/Converter/Feature2Texture.js | 4 +-- src/Core/Style.js | 56 +++++++++++++++++++++++++++++++- src/Layer/LabelLayer.js | 5 ++- src/Main.js | 2 +- 5 files changed, 62 insertions(+), 48 deletions(-) diff --git a/src/Converter/Feature2Mesh.js b/src/Converter/Feature2Mesh.js index 374d82035a..1dba8bc556 100644 --- a/src/Converter/Feature2Mesh.js +++ b/src/Converter/Feature2Mesh.js @@ -7,49 +7,10 @@ import Extent from 'Core/Geographic/Extent'; import Crs from 'Core/Geographic/Crs'; import OrientationUtils from 'Utils/OrientationUtils'; import Coordinates from 'Core/Geographic/Coordinates'; +import { StyleContext } from 'Core/Style'; const coord = new Coordinates('EPSG:4326', 0, 0, 0); - -export class FeatureContext { - #worldCoord = new Coordinates('EPSG:4326', 0, 0, 0); - #localCoordinates = new Coordinates('EPSG:4326', 0, 0, 0); - #geometry = {}; - - constructor() { - this.globals = {}; - } - - setGeometry(g) { - this.#geometry = g; - } - - setCollection(c) { - this.collection = c; - this.#localCoordinates.setCrs(c.crs); - } - - setLocalCoordinatesFromArray(vertices, offset) { - this.#worldCoord.isLocal = true; - return this.#localCoordinates.setFromArray(vertices, offset); - } - - properties() { - return this.#geometry.properties; - } - - get coordinates() { - if (this.#worldCoord.isLocal) { - this.#worldCoord.isLocal = false; - this.#worldCoord.copy(this.#localCoordinates).applyMatrix4(this.collection.matrixWorld); - if (this.#localCoordinates.crs == 'EPSG:4978') { - return this.#worldCoord.as('EPSG:4326', this.#worldCoord); - } - } - return this.#worldCoord; - } -} - -const context = new FeatureContext(); +const context = new StyleContext(); const dim_ref = new THREE.Vector2(); const dim = new THREE.Vector2(); diff --git a/src/Converter/Feature2Texture.js b/src/Converter/Feature2Texture.js index e6318571fb..a5683ff93c 100644 --- a/src/Converter/Feature2Texture.js +++ b/src/Converter/Feature2Texture.js @@ -2,9 +2,9 @@ import * as THREE from 'three'; import { FEATURE_TYPES } from 'Core/Feature'; import Extent from 'Core/Geographic/Extent'; import Coordinates from 'Core/Geographic/Coordinates'; -import { FeatureContext } from 'Converter/Feature2Mesh'; +import { StyleContext } from 'Core/Style'; -const context = new FeatureContext(); +const context = new StyleContext(); const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); const matrix = svg.createSVGMatrix(); diff --git a/src/Core/Style.js b/src/Core/Style.js index 3fe3ec9c68..9dc1b26496 100644 --- a/src/Core/Style.js +++ b/src/Core/Style.js @@ -4,6 +4,7 @@ import Fetcher from 'Provider/Fetcher'; import * as mapbox from '@mapbox/mapbox-gl-style-spec'; import { Color } from 'three'; import { deltaE } from 'Renderer/Color'; +import Coordinates from 'Core/Geographic/Coordinates'; import itowns_stroke_single_before from './StyleChunk/itowns_stroke_single_before.css'; @@ -154,6 +155,58 @@ function defineStyleProperty(style, category, name, value, defaultValue) { style[category][name] = value; } +export class StyleContext { + #worldCoord = new Coordinates('EPSG:4326', 0, 0, 0); + #localCoordinates = new Coordinates('EPSG:4326', 0, 0, 0); + #feature = {}; + #geometry = {}; + + constructor() { + this.globals = {}; + } + + setFeature(f) { + this.#feature = f; + } + + setGeometry(g) { + this.#geometry = g; + } + + setCollection(c) { + this.collection = c; + this.#localCoordinates.setCrs(c.crs); + } + + setLocalCoordinatesFromArray(vertices, offset) { + this.#worldCoord.isLocal = true; + return this.#localCoordinates.setFromArray(vertices, offset); + } + + properties() { + return this.#geometry.properties; + } + + get type() { + return this.#feature.type; + } + + get localCoordinates() { + return this.#localCoordinates; + } + + get coordinates() { + if (this.#worldCoord.isLocal) { + this.#worldCoord.isLocal = false; + this.#worldCoord.copy(this.#localCoordinates).applyMatrix4(this.collection.matrixWorld); + if (this.#localCoordinates.crs == 'EPSG:4978') { + return this.#worldCoord.as('EPSG:4326', this.#worldCoord); + } + } + return this.#worldCoord; + } +} + /** * @typedef {Object} StyleOptions * @memberof StyleOptions @@ -560,7 +613,8 @@ class Style { * Map drawing properties style (fill, stroke and point) from context to object. * Only the necessary properties are mapped to object. * if a property is expression, the mapped value will be the expression result depending on context. - * @param {Object} context The context + * @param {StyleContext} context The context linked to the feature + * * @return {Style} mapped style depending on context. */ drawingStylefromContext(context) { diff --git a/src/Layer/LabelLayer.js b/src/Layer/LabelLayer.js index c6edbf8657..79de1cfc6b 100644 --- a/src/Layer/LabelLayer.js +++ b/src/Layer/LabelLayer.js @@ -6,11 +6,10 @@ import Coordinates from 'Core/Geographic/Coordinates'; import Extent from 'Core/Geographic/Extent'; import Label from 'Core/Label'; import { FEATURE_TYPES } from 'Core/Feature'; -import { readExpression } from 'Core/Style'; +import { readExpression, StyleContext } from 'Core/Style'; import { ScreenGrid } from 'Renderer/Label2DRenderer'; -import { FeatureContext } from 'Converter/Feature2Mesh'; -const context = new FeatureContext(); +const context = new StyleContext(); const coord = new Coordinates('EPSG:4326', 0, 0, 0); diff --git a/src/Main.js b/src/Main.js index 3e849a88d5..9400e8d3a4 100644 --- a/src/Main.js +++ b/src/Main.js @@ -39,7 +39,7 @@ export { CAMERA_TYPE } from 'Renderer/Camera'; // Internal itowns format export { default as Feature, FeatureCollection, FeatureGeometry, FEATURE_TYPES } from 'Core/Feature'; -export { default as Style } from 'Core/Style'; +export { default as Style, StyleContext } from 'Core/Style'; export { default as Label } from 'Core/Label'; // Layers provided by default in iTowns