Skip to content

Commit

Permalink
refactor(PointCloudLayer): linked to review
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Feb 11, 2025
1 parent 4508206 commit bd16768
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 127 deletions.
25 changes: 4 additions & 21 deletions src/Layer/CopcLayer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as THREE from 'three';
import CopcNode from 'Core/CopcNode';
import PointCloudLayer from 'Layer/PointCloudLayer';
import proj4 from 'proj4';

/**
* A layer for [Cloud Optimised Point Cloud](https://copc.io) (COPC) datasets.
Expand Down Expand Up @@ -44,30 +43,14 @@ class CopcLayer extends PointCloudLayer {
const { pageOffset, pageLength } = source.info.rootHierarchyPage;

this.spacing = source.info.spacing;

this.root = new CopcNode(0, 0, 0, 0, pageOffset, pageLength, this, -1);

let forward = (x => x);
if (this.source.crs !== this.crs) {
try {
forward = proj4(this.source.crs, this.crs).forward;
} catch (err) {
throw new Error(`${err} is not defined in proj4`);
}
}

this.minElevationRange = this.minElevationRange ?? source.header.min[2];
this.maxElevationRange = this.maxElevationRange ?? source.header.max[2];

this.scale = new THREE.Vector3(1.0, 1.0, 1.0);
this.offset = new THREE.Vector3(0.0, 0.0, 0.0);

const bounds = [
...forward(cube.slice(0, 3)),
...forward(cube.slice(3, 6)),
];
this.setElevationRange(source.header.min[2], source.header.max[2]);

this.root = new CopcNode(0, 0, 0, 0, pageOffset, pageLength, this, -1);

this.root.bbox.setFromArray(bounds);
this.setRootBbox(cube.slice(0, 3), cube.slice(3, 6));

return this.root.loadOctree().then(resolve);
});
Expand Down
35 changes: 6 additions & 29 deletions src/Layer/EntwinePointTileLayer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import * as THREE from 'three';
import EntwinePointTileNode from 'Core/EntwinePointTileNode';
import PointCloudLayer from 'Layer/PointCloudLayer';
import Extent from 'Core/Geographic/Extent';
import proj4 from 'proj4';

const bboxMesh = new THREE.Mesh();
const box3 = new THREE.Box3();
bboxMesh.geometry.boundingBox = box3;

/**
* @property {boolean} isEntwinePointTileLayer - Used to checkout whether this
Expand Down Expand Up @@ -54,35 +48,18 @@ class EntwinePointTileLayer extends PointCloudLayer {
this.scale = new THREE.Vector3(1, 1, 1);

const resolve = this.addInitializationStep();
this.whenReady = this.source.whenReady.then(() => {
this.whenReady = this.source.whenReady.then((source) => {
// NOTE: this spacing is kinda arbitrary here, we take the width and
// length (height can be ignored), and we divide by the specified
// span in ept.json. This needs improvements.
this.spacing = (Math.abs(this.source.bounds[3] - this.source.bounds[0])
+ Math.abs(this.source.bounds[4] - this.source.bounds[1])) / (2 * this.source.span);

this.root = new EntwinePointTileNode(0, 0, 0, 0, this, -1);
this.spacing = (Math.abs(source.bounds[3] - source.bounds[0])
+ Math.abs(source.bounds[4] - source.bounds[1])) / (2 * source.span);

let forward = (x => x);
if (this.source.crs !== this.crs) {
try {
forward = proj4(this.source.crs, this.crs).forward;
} catch (err) {
throw new Error(`${err} is not defined in proj4`);
}
}
this.setElevationRange(source.boundsConforming[2], source.boundsConforming[5]);

this.minElevationRange = this.minElevationRange ?? this.source.boundsConforming[2];
this.maxElevationRange = this.maxElevationRange ?? this.source.boundsConforming[5];

const bounds = [
...forward(this.source.bounds.slice(0, 3)),
...forward(this.source.bounds.slice(3, 6)),
];

this.root.bbox.setFromArray(bounds);
this.root = new EntwinePointTileNode(0, 0, 0, 0, this, -1);

this.extent = Extent.fromBox3(this.crs, this.root.bbox);
this.setRootBbox(source.bounds.slice(0, 3), source.bounds.slice(3, 6));

return this.root.loadOctree().then(resolve);
});
Expand Down
24 changes: 24 additions & 0 deletions src/Layer/PointCloudLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as THREE from 'three';
import GeometryLayer from 'Layer/GeometryLayer';
import PointsMaterial, { PNTS_MODE } from 'Renderer/PointsMaterial';
import Picking from 'Core/Picking';
import proj4 from 'proj4';

const point = new THREE.Vector3();
const bboxMesh = new THREE.Mesh();
Expand Down Expand Up @@ -231,6 +232,29 @@ class PointCloudLayer extends GeometryLayer {
this.root = undefined;
}

setRootBbox(min, max) {
let forward = (x => x);
if (this.source.crs !== this.crs) {
try {
forward = proj4(this.source.crs, this.crs).forward;
} catch (err) {
throw new Error(`${err} is not defined in proj4`);
}
}

const bounds = [
...forward(min),
...forward(max),
];

this.root.bbox.setFromArray(bounds);
}

setElevationRange(zmin, zmax) {
this.minElevationRange = this.minElevationRange ?? zmin;
this.maxElevationRange = this.maxElevationRange ?? zmax;
}

preUpdate(context, changeSources) {
// See https://cesiumjs.org/hosted-apps/massiveworlds/downloads/Ring/WorldScaleTerrainRendering.pptx
// slide 17
Expand Down
40 changes: 6 additions & 34 deletions src/Layer/Potree2Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ of the authors and should not be interpreted as representing official policies,
import * as THREE from 'three';
import PointCloudLayer from 'Layer/PointCloudLayer';
import Potree2Node from 'Core/Potree2Node';
import proj4 from 'proj4';
import Extent from 'Core/Geographic/Extent';

import { PointAttribute, Potree2PointAttributes, PointAttributeTypes } from 'Core/Potree2PointAttributes';

const bboxMesh = new THREE.Mesh();
const box3 = new THREE.Box3();
bboxMesh.geometry.boundingBox = box3;

const typeNameAttributeMap = {
double: PointAttributeTypes.DATA_TYPE_DOUBLE,
float: PointAttributeTypes.DATA_TYPE_FLOAT,
Expand Down Expand Up @@ -155,8 +149,7 @@ class Potree2Layer extends PointCloudLayer {
this.isPotreeLayer = true;

const resolve = this.addInitializationStep();

this.source.whenReady.then((metadata) => {
this.whenReady = this.source.whenReady.then((metadata) => {
this.scale = new THREE.Vector3(1, 1, 1);
this.metadata = metadata;
this.pointAttributes = parseAttributes(metadata.attributes);
Expand All @@ -168,41 +161,20 @@ class Potree2Layer extends PointCloudLayer {
this.material.defines[normal.name] = 1;
}

const root = new Potree2Node(0, 0, this);

let forward = (x => x);
if (this.source.crs !== this.crs) {
try {
forward = proj4(this.source.crs, this.crs).forward;
} catch (err) {
throw new Error(`${err} is not defined in proj4`);
}
}

this.minElevationRange = metadata.boundingBox.min[2];
this.maxElevationRange = metadata.boundingBox.max[2];

const bounds = [
...forward(metadata.boundingBox.min),
...forward(metadata.boundingBox.max),
];

root.bbox.setFromArray(bounds);

this.minElevationRange = this.minElevationRange ?? metadata.boundingBox.min[2];
this.maxElevationRange = this.maxElevationRange ?? metadata.boundingBox.max[2];
// currently the spec on potree2 only have boundingBox and no boundsConforming
this.setElevationRange(metadata.boundingBox.min[2], metadata.boundingBox.max[2]);

const root = new Potree2Node(0, 0, this);
root.id = 'r';
root.depth = 0;
root.nodeType = 2;
root.hierarchyByteOffset = 0n;
root.hierarchyByteSize = BigInt(metadata.hierarchy.firstChunkSize);

root.byteOffset = 0;

this.root = root;

this.extent = Extent.fromBox3(this.source.crs || 'EPSG:4326', this.root.bbox);
this.setRootBbox(metadata.boundingBox.min, metadata.boundingBox.max);

return this.root.loadOctree().then(resolve);
});
}
Expand Down
35 changes: 5 additions & 30 deletions src/Layer/PotreeLayer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import * as THREE from 'three';
import PointCloudLayer from 'Layer/PointCloudLayer';
import PotreeNode from 'Core/PotreeNode';
import proj4 from 'proj4';
import Extent from 'Core/Geographic/Extent';

const bboxMesh = new THREE.Mesh();
const box3 = new THREE.Box3();
bboxMesh.geometry.boundingBox = box3;

/**
* @property {boolean} isPotreeLayer - Used to checkout whether this layer
Expand Down Expand Up @@ -53,8 +47,7 @@ class PotreeLayer extends PointCloudLayer {
this.isPotreeLayer = true;

const resolve = this.addInitializationStep();

this.source.whenReady.then((cloud) => {
this.whenReady = this.source.whenReady.then((cloud) => {
this.scale = new THREE.Vector3().addScalar(cloud.scale);
this.spacing = cloud.spacing;
this.hierarchyStepSize = cloud.hierarchyStepSize;
Expand All @@ -67,31 +60,13 @@ class PotreeLayer extends PointCloudLayer {

this.supportsProgressiveDisplay = (this.source.extension === 'cin');

this.root = new PotreeNode(0, 0, this);

let forward = (x => x);
if (this.source.crs !== this.crs) {
try {
forward = proj4(this.source.crs, this.crs).forward;
} catch (err) {
throw new Error(`${err} is not defined in proj4`);
}
}

this.minElevationRange = cloud.tightBoundingBox.lz;
this.maxElevationRange = cloud.tightBoundingBox.uz;
this.setElevationRange(cloud.tightBoundingBox.lz, cloud.tightBoundingBox.uz);

const bounds = [
...forward([cloud.boundingBox.lx, cloud.boundingBox.ly, cloud.boundingBox.lz]),
...forward([cloud.boundingBox.ux, cloud.boundingBox.uy, cloud.boundingBox.uz]),
];

this.root.bbox.setFromArray(bounds);
this.root = new PotreeNode(0, 0, this);

this.minElevationRange = this.minElevationRange ?? cloud.boundingBox.lz;
this.maxElevationRange = this.maxElevationRange ?? cloud.boundingBox.uz;
this.setRootBbox([cloud.boundingBox.lx, cloud.boundingBox.ly, cloud.boundingBox.lz],
[cloud.boundingBox.ux, cloud.boundingBox.uy, cloud.boundingBox.uz]);

this.extent = Extent.fromBox3(this.source.crs || 'EPSG:4326', this.root.bbox);
return this.root.loadOctree().then(resolve);
});
}
Expand Down
8 changes: 1 addition & 7 deletions src/Source/CopcSource.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import proj4 from 'proj4';
import { Binary, Info, Las } from 'copc';
import Extent from 'Core/Geographic/Extent';
import Fetcher from 'Provider/Fetcher';
import LASParser from 'Parser/LASParser';
import Source from 'Source/Source';
import * as THREE from 'three';

/**
* @param {function(number, number):Promise<Uint8Array>} fetcher
Expand Down Expand Up @@ -99,6 +97,7 @@ class CopcSource extends Source {
range: `bytes=${begin}-${end - 1}`,
},
}).then(buffer => new Uint8Array(buffer));

this.whenReady = getHeaders(get).then((metadata) => {
this.header = metadata.header;
this.info = metadata.info;
Expand All @@ -119,11 +118,6 @@ class CopcSource extends Source {
proj4.defs(this.crs, projCS);
}

const bbox = new THREE.Box3();
bbox.min.fromArray(this.info.cube, 0);
bbox.max.fromArray(this.info.cube, 3);
this.extent = Extent.fromBox3(this.crs, bbox);

return this;
});
}
Expand Down
12 changes: 6 additions & 6 deletions src/Source/EntwinePointTileSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ class EntwinePointTileSource extends Source {
this.isEntwinePointTileSource = true;
this.colorDepth = config.colorDepth;

this.fetcher = Fetcher.arrayBuffer;

// Necessary because we use the url without the ept.json part as a base
this.url = this.url.replace('/ept.json', '');

// https://entwine.io/entwine-point-tile.html#ept-json
this.whenReady = Fetcher.json(`${this.url}/ept.json`, this.networkOptions).then((metadata) => {
this.boundsConforming = metadata.boundsConforming;
this.bounds = metadata.bounds;
this.span = metadata.span;

// Set parser and its configuration from schema
this.parse = metadata.dataType === 'laszip' ? LASParser.parse : PotreeBinParser.parse;
this.extension = metadata.dataType === 'laszip' ? 'laz' : 'bin';
Expand All @@ -59,14 +65,8 @@ class EntwinePointTileSource extends Source {
}
}

this.boundsConforming = metadata.boundsConforming;
this.bounds = metadata.bounds;
this.span = metadata.span;

return this;
});

this.fetcher = Fetcher.arrayBuffer;
}
}

Expand Down

0 comments on commit bd16768

Please sign in to comment.