Skip to content

Commit

Permalink
refactor(FeatureCtx): move class FeatureContext to Style and rename
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Oct 18, 2023
1 parent 0bc6bdd commit 38bced9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 48 deletions.
43 changes: 2 additions & 41 deletions src/Converter/Feature2Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/Converter/Feature2Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
56 changes: 55 additions & 1 deletion src/Core/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions src/Layer/LabelLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38bced9

Please sign in to comment.