Skip to content

Commit

Permalink
refactor(feature2Mesh): context using
Browse files Browse the repository at this point in the history
REVIEW: this commit will be squatched !!
  • Loading branch information
gchoqueux committed Oct 9, 2023
1 parent e800e31 commit 2c35395
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
33 changes: 13 additions & 20 deletions src/Converter/Feature2Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class FeatureContext {
#worldCoord = new Coordinates('EPSG:4326', 0, 0, 0);
#localCoordinates = new Coordinates('EPSG:4326', 0, 0, 0);
#geometry = {};
#collection = {};

constructor() {
this.globals = {};
Expand All @@ -25,7 +24,7 @@ class FeatureContext {
}

setCollection(c) {
this.#collection = c;
this.collection = c;
this.#localCoordinates.setCrs(c.crs);
}

Expand All @@ -38,14 +37,10 @@ class FeatureContext {
return this.#geometry.properties;
}

get localCoordinates() {
return this.#localCoordinates;
}

get coordinates() {
if (this.#worldCoord.isLocal) {
this.#worldCoord.isLocal = false;
this.#worldCoord.copy(this.#localCoordinates).applyMatrix4(this.#collection.matrixWorld);
this.#worldCoord.copy(this.#localCoordinates).applyMatrix4(this.collection.matrixWorld);
if (this.#localCoordinates.crs == 'EPSG:4978') {
return this.#worldCoord.as('EPSG:4326', this.#worldCoord);
}
Expand Down Expand Up @@ -77,10 +72,15 @@ class FeatureMesh extends THREE.Group {
#place = new THREE.Group();
constructor(meshes, collection) {
super();

this.meshes = new THREE.Group().add(...meshes);

this.#collection = new THREE.Group().add(this.meshes);
this.#collection.quaternion.copy(collection.quaternion);
this.#collection.position.copy(collection.position);
normal.copy(collection.center.geodesicNormal).multiplyScalar(collection.center.z);
this.#collection.position.sub(normal);

this.#collection.scale.copy(collection.scale);
this.#collection.updateMatrix();

Expand Down Expand Up @@ -218,11 +218,10 @@ function featureToPoint(feature, options) {
const colors = new Uint8Array(ptsIn.length);
const batchIds = new Uint32Array(ptsIn.length);
const batchId = options.batchId || ((p, id) => id);
context.setCollection(options.collection);

let featureId = 0;
const vertices = new Float32Array(ptsIn);
inverseScale.setFromMatrixScale(options.collection.matrixWorldInverse);
inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse);
normal.set(0, 0, 1).multiply(inverseScale);
context.globals = { point: true };

Expand Down Expand Up @@ -265,7 +264,6 @@ function featureToLine(feature, options) {
const ptsIn = feature.vertices;
const colors = new Uint8Array(ptsIn.length);
const count = ptsIn.length / 3;
context.setCollection(options.collection);

const batchIds = new Uint32Array(count);
const batchId = options.batchId || ((p, id) => id);
Expand All @@ -283,7 +281,7 @@ function featureToLine(feature, options) {
const indices = getIntArrayFromSize(countIndices, count);

let i = 0;
inverseScale.setFromMatrixScale(options.collection.matrixWorldInverse);
inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse);
normal.set(0, 0, 1).multiply(inverseScale);
// Multi line case
for (const geometry of feature.geometries) {
Expand Down Expand Up @@ -339,10 +337,9 @@ function featureToPolygon(feature, options) {

const batchIds = new Uint32Array(vertices.length / 3);
const batchId = options.batchId || ((p, id) => id);
context.setCollection(options.collection);
context.globals = { fill: true };

inverseScale.setFromMatrixScale(options.collection.matrixWorldInverse);
inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse);
normal.set(0, 0, 1).multiply(inverseScale);
let featureId = 0;

Expand Down Expand Up @@ -425,14 +422,13 @@ function featureToExtrudedPolygon(feature, options) {

const batchIds = new Uint32Array(vertices.length / 3);
const batchId = options.batchId || ((p, id) => id);
context.setCollection(options.collection);

let featureId = 0;

context.globals = { fill: true };
inverseScale.setFromMatrixScale(options.collection.matrixWorldInverse);
inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse);
normal.set(0, 0, 1).multiply(inverseScale);
coord.setCrs(options.collection.crs);
coord.setCrs(context.collection.crs);

for (const geometry of feature.geometries) {
context.setGeometry(geometry);
Expand Down Expand Up @@ -609,7 +605,6 @@ function featureToMesh(feature, options) {
mesh.material.color = new THREE.Color(0xffffff);
}
mesh.feature = feature;
mesh.position.z = -options.GlobalZTrans;

if (options.layer) {
mesh.layer = options.layer;
Expand Down Expand Up @@ -669,14 +664,12 @@ export default {
options.layer = this;
}

options.collection = collection;
context.setCollection(collection);

const features = collection.features;

if (!features || features.length == 0) { return; }

options.GlobalZTrans = collection.center.z;

const meshes = features.map(feature => featureToMesh(feature, options));
const featureNode = new FeatureMesh(meshes, collection);

Expand Down
2 changes: 1 addition & 1 deletion src/Core/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const canvas = (typeof document !== 'undefined') ? document.createElement('canva
const style_properties = {};

function base_altitudeDefault(properties, ctx) {
return ctx?.coordinates?.z || 0;
return ctx?.coordinates?.z || ctx?.collection?.center?.z || 0;
}

function mapPropertiesFromContext(mainKey, from, to, context) {
Expand Down

0 comments on commit 2c35395

Please sign in to comment.