From 6756148b1b82dc96c931501427bce3475d87a9eb 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 --- docs/config.json | 1 + src/Converter/Feature2Mesh.js | 57 ++------------------------------ src/Converter/Feature2Texture.js | 5 ++- src/Core/Style.js | 56 +++++++++++++++++++++++++++++++ src/Layer/LabelLayer.js | 5 ++- src/Main.js | 2 +- 6 files changed, 64 insertions(+), 62 deletions(-) diff --git a/docs/config.json b/docs/config.json index d024eef942..5ad8155d11 100644 --- a/docs/config.json +++ b/docs/config.json @@ -15,6 +15,7 @@ "FeatureBuildingOptions", "Style", "StyleOptions", + "StyleContext", "Label" ], diff --git a/src/Converter/Feature2Mesh.js b/src/Converter/Feature2Mesh.js index 2f9a97bdce..8a66e700bd 100644 --- a/src/Converter/Feature2Mesh.js +++ b/src/Converter/Feature2Mesh.js @@ -7,63 +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); - -/** - * @class - * @classdesc FeatureContext is a class to store all informations - * about context to generate the style of each FeatureGeometry. - * - * @property {Coordinates} worldCoord @private Coordinates of the FeatureGeometry in world system. - * @property {Coordinates} localCoordinates @private Are the coordinates systeme origin local or global. - * @property {boolean} isProjected @private Are the coordinates already been projected. - * @property {FeatureGeometry} geometry @private - * @property {Object} globals - * @property {Object} collection - * @property {Coordinates} coordinates - */ -export class FeatureContext { - #worldCoord = new Coordinates('EPSG:4326', 0, 0, 0); - #localCoordinates = new Coordinates('EPSG:4326', 0, 0, 0); - #isProjected = true; - #geometry = {}; - - constructor() { - this.globals = {}; - } - - setGeometry(g) { - this.#geometry = g; - } - - setCollection(c) { - this.collection = c; - this.#localCoordinates.setCrs(c.crs); - } - - setLocalCoordinatesFromArray(vertices, offset) { - this.#isProjected = false; - return this.#localCoordinates.setFromArray(vertices, offset); - } - - get properties() { - return this.#geometry.properties; - } - - get coordinates() { - if (!this.#isProjected) { - this.#isProjected = true; - 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 afcd645c2c..9eed70ee3e 100644 --- a/src/Converter/Feature2Texture.js +++ b/src/Converter/Feature2Texture.js @@ -2,10 +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 Style from 'Core/Style'; -import { FeatureContext } from 'Converter/Feature2Mesh'; +import Style, { StyleContext } from 'Core/Style'; -const context = new FeatureContext(); +const context = new StyleContext(); /** * Draw polygon (contour, line edge and fill) based on feature vertices into canvas diff --git a/src/Core/Style.js b/src/Core/Style.js index 6c51f3f0d9..2dfc338c07 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'; @@ -160,6 +161,61 @@ function defineStyleProperty(style, category, name, value, defaultValue) { style[category][name] = value; } +/** + * @class + * @classdesc StyleContext is a class to store all informations + * about context to generate the style of each FeatureGeometry. + * + * @property {Coordinates} worldCoord @private Coordinates of the FeatureGeometry in world system. + * @property {Coordinates} localCoordinates @private Are the coordinates systeme origin local or global. + * @property {boolean} isProjected @private Are the coordinates already been projected. + * @property {FeatureGeometry} geometry @private + * @property {Object} globals + * @property {Object} collection + * @property {Coordinates} coordinates + */ +export class StyleContext { + #worldCoord = new Coordinates('EPSG:4326', 0, 0, 0); + #localCoordinates = new Coordinates('EPSG:4326', 0, 0, 0); + #isProjected = true; + #geometry = {}; + /** + * @constructor + */ + constructor() { + this.globals = {}; + } + + setGeometry(g) { + this.#geometry = g; + } + + setCollection(c) { + this.collection = c; + this.#localCoordinates.setCrs(c.crs); + } + + setLocalCoordinatesFromArray(vertices, offset) { + this.#isProjected = false; + return this.#localCoordinates.setFromArray(vertices, offset); + } + + get properties() { + return this.#geometry.properties; + } + + get coordinates() { + if (!this.#isProjected) { + this.#isProjected = true; + 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 diff --git a/src/Layer/LabelLayer.js b/src/Layer/LabelLayer.js index 5b553ef970..b7443416c1 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