Skip to content

Commit

Permalink
refactor: let EPT loader take non-hardcoded object list
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonflatval-cognite committed May 6, 2022
1 parent caf63a7 commit bfa4c5f
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 100 deletions.
9 changes: 4 additions & 5 deletions viewer/packages/pointclouds/src/CognitePointCloudModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { PointCloudNode } from './PointCloudNode';
import { PotreePointColorType, PotreePointShape, PotreePointSizeType } from './potree-three-loader';

import { SupportedModelTypes, CogniteModelBase } from '@reveal/model-base';
import { RawStyleObject } from './styling/StyleObject';
import { RawStyledObject } from './styling/StyledObject';

/**
* Represents a point clouds model loaded from CDF.
Expand Down Expand Up @@ -213,15 +213,14 @@ export class CognitePointCloudModel extends THREE.Object3D implements CogniteMod
/**
* Gets the stylable objects associated with this point cloud.
*/
get stylableObjects(): RawStyleObject[] {
return this.pointCloudNode.objects;
get stylableObjects(): RawStyledObject[] {
return this.pointCloudNode.stylingInfo.styledObjects;
}

/**
* Sets the style of one object
*/
setObjectStyle(objectId: number, color: [number, number, number]) {
setObjectStyle(objectId: number, color: [number, number, number]): void {
this.pointCloudNode.setObjectStyle(objectId, color);
}

}
16 changes: 11 additions & 5 deletions viewer/packages/pointclouds/src/PointCloudFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PointCloudMetadata } from './PointCloudMetadata';
import { ModelDataProvider } from '@reveal/modeldata-api';

import { PointCloudOctree, Potree } from './potree-three-loader';
import { StyledObjectInfo } from './styling/StyledObjectInfo';

export class PointCloudFactory {
private readonly _potreeInstance: Potree;
Expand All @@ -20,12 +21,17 @@ export class PointCloudFactory {
return this._potreeInstance;
}

async createModel(modelMetadata: PointCloudMetadata): Promise<PotreeNodeWrapper> {
async createModel(
modelMetadata: PointCloudMetadata,
styledObjectInfo?: StyledObjectInfo
): Promise<PotreeNodeWrapper> {
const { modelBaseUrl } = modelMetadata;

return this._potreeInstance.loadPointCloud(modelBaseUrl, 'ept.json').then((pco: PointCloudOctree) => {
pco.name = `PointCloudOctree: ${modelBaseUrl}`;
return new PotreeNodeWrapper(pco);
});
return this._potreeInstance
.loadPointCloud(modelBaseUrl, 'ept.json', styledObjectInfo)
.then((pco: PointCloudOctree) => {
pco.name = `PointCloudOctree: ${modelBaseUrl}`;
return new PotreeNodeWrapper(pco, styledObjectInfo);
});
}
}
3 changes: 2 additions & 1 deletion viewer/packages/pointclouds/src/PointCloudManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { asyncScheduler, combineLatest, Observable, scan, Subject, throttleTime
import { ModelIdentifier } from '@reveal/modeldata-api';
import { MetricsLogger } from '@reveal/metrics';
import { SupportedModelTypes } from '@reveal/model-base';
import { hardCodedObjects } from './styling/staticObjects';

export class PointCloudManager {
private readonly _pointCloudMetadataRepository: PointCloudMetadataRepository;
Expand Down Expand Up @@ -129,7 +130,7 @@ export class PointCloudManager {
metadata.formatVersion
);

const nodeWrapper = await this._pointCloudFactory.createModel(metadata);
const nodeWrapper = await this._pointCloudFactory.createModel(metadata, hardCodedObjects);
this._pointCloudGroupWrapper.addPointCloud(nodeWrapper);
const node = new PointCloudNode(this._pointCloudGroupWrapper, nodeWrapper, metadata.cameraConfiguration);
node.setModelTransformation(metadata.modelMatrix);
Expand Down
21 changes: 7 additions & 14 deletions viewer/packages/pointclouds/src/PointCloudNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ import { PotreeNodeWrapper } from './PotreeNodeWrapper';
import { WellKnownAsprsPointClassCodes } from './types';
import { createPointClassKey } from './createPointClassKey';

import { RawStyleObject } from './styling/StyleObject';

import { PotreePointColorType, PotreePointShape, PotreePointSizeType } from './potree-three-loader';
import { hardCodedObjects } from './styling/staticObjects';

import { StyledObjectInfo } from './styling/StyledObjectInfo';

const PotreeDefaultPointClass = 'DEFAULT';

export class PointCloudNode extends THREE.Group {
private readonly _potreeGroup: PotreeGroupWrapper;
private readonly _potreeNode: PotreeNodeWrapper;
private readonly _cameraConfiguration?: CameraConfiguration;
private readonly _objects: RawStyleObject[];

constructor(
potreeGroup: PotreeGroupWrapper,
Expand All @@ -34,8 +32,7 @@ export class PointCloudNode extends THREE.Group {
this._potreeNode = potreeNode;
this._cameraConfiguration = cameraConfiguration;
this.add(this._potreeNode.octree);

this._objects = this.loadObjects();
this._potreeNode.octree;

this.matrixAutoUpdate = false;
}
Expand Down Expand Up @@ -100,12 +97,12 @@ export class PointCloudNode extends THREE.Group {
this._potreeNode.pointShape = value;
}

get objects(): RawStyleObject[] {
return this._objects;
get stylingInfo(): StyledObjectInfo {
return this._potreeNode.styledObjectInfo;
}

setObjectStyle(objectId: number, color: [number, number, number]) {
this.potreeNode.octree.material.setObjectColor(objectId, new THREE.Color(color[0], color[1], color[2]));
setObjectStyle(objectId: number, color: [number, number, number]): void {
this.potreeNode.setObjectStyle(objectId, color);
this.requestRedraw();
}

Expand Down Expand Up @@ -174,8 +171,4 @@ export class PointCloudNode extends THREE.Group {
getModelTransformation(out = new THREE.Matrix4()): THREE.Matrix4 {
return out.copy(this.matrix);
}

loadObjects(): RawStyleObject[] {
return hardCodedObjects;
}
}
14 changes: 13 additions & 1 deletion viewer/packages/pointclouds/src/PotreeNodeWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { PointCloudOctree, PotreePointColorType, PotreePointShape, IClassificati
import { WellKnownAsprsPointClassCodes } from './types';

import { createPointClassKey } from './createPointClassKey';
import { StyledObjectInfo } from './styling/StyledObjectInfo';

/**
* Wrapper around `Potree.PointCloudOctree` with some convenience functions.
Expand All @@ -18,17 +19,19 @@ export class PotreeNodeWrapper {
readonly octree: PointCloudOctree;
private _needsRedraw = false;
private readonly _classification: IClassification = {} as IClassification;
private readonly _styledObjectInfo: StyledObjectInfo | undefined;

get needsRedraw(): boolean {
return this._needsRedraw;
}

constructor(octree: PointCloudOctree) {
constructor(octree: PointCloudOctree, styledObjectInfo: StyledObjectInfo) {
this.octree = octree;
this.pointSize = 2;
this.pointColorType = PotreePointColorType.Rgb;
this.pointShape = PotreePointShape.Circle;
this._classification = octree.material.classification;
this._styledObjectInfo = styledObjectInfo;
}

get pointSize(): number {
Expand Down Expand Up @@ -72,6 +75,15 @@ export class PotreeNodeWrapper {
return this._classification;
}

get styledObjectInfo(): StyledObjectInfo {
return this._styledObjectInfo;
}

setObjectStyle(objectId: number, color: [number, number, number]): void {
this.octree.material.setObjectColor(objectId, new THREE.Color(color[0], color[1], color[2]));
this._needsRedraw = true;
}

setClassificationAndRecompute(pointClass: number | WellKnownAsprsPointClassCodes, visible: boolean): void {
const key = createPointClassKey(pointClass);

Expand Down
5 changes: 3 additions & 2 deletions viewer/packages/pointclouds/src/potree-three-loader/Potree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { BinaryHeap } from './utils/BinaryHeap';
import { Box3Helper } from './utils/box3-helper';
import { LRU } from './utils/lru';
import { ModelDataProvider } from '@reveal/modeldata-api';
import { StyledObjectInfo } from '../styling/StyledObjectInfo';

export class QueueItem {
constructor(
Expand Down Expand Up @@ -60,9 +61,9 @@ export class Potree implements IPotree {
async loadPointCloud(
baseUrl: string,
fileName: string,
_xhrRequest = (input: RequestInfo, init?: RequestInit) => fetch(input, init)
styledObjectInfo?: StyledObjectInfo
): Promise<PointCloudOctree> {
return EptLoader.load(baseUrl, fileName, this._modelDataProvider).then(
return EptLoader.load(baseUrl, fileName, this._modelDataProvider, styledObjectInfo).then(
geometry => new PointCloudOctree(this, geometry)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { IPointCloudTreeGeometry } from './IPointCloudTreeGeometry';
import proj4 from 'proj4';
import { ModelDataProvider } from '@reveal/modeldata-api';
import { toVector3, toBox3 } from './translationUtils';
import { StyledObjectInfo } from '../../styling/StyledObjectInfo';

type EptSchemaEntry = {
name: string;
Expand Down Expand Up @@ -52,6 +53,8 @@ export class PointCloudEptGeometry implements IPointCloudTreeGeometry {

private readonly _projection: string | null;

private readonly _styledObjectInfo: StyledObjectInfo | undefined;

get root(): PointCloudEptGeometryNode | undefined {
return this._root;
}
Expand Down Expand Up @@ -96,7 +99,7 @@ export class PointCloudEptGeometry implements IPointCloudTreeGeometry {
return this._loader;
}

constructor(url: string, info: any, dataLoader: ModelDataProvider) {
constructor(url: string, info: any, dataLoader: ModelDataProvider, styledInfo?: StyledObjectInfo) {
const schema = info.schema;
const bounds = info.bounds;
const boundsConforming = info.boundsConforming;
Expand Down Expand Up @@ -142,7 +145,7 @@ export class PointCloudEptGeometry implements IPointCloudTreeGeometry {
throw new Error('Could not read data type: ' + info.dataType);
}

this._loader = new EptBinaryLoader(dataLoader);
this._loader = new EptBinaryLoader(dataLoader, styledInfo);
}

dispose(): void {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ import { ParseCommand, ObjectsCommand } from '../workers/eptBinaryDecoder.worker

import { ParsedEptData, EptInputData } from '../workers/parseEpt';

import { hardCodedObjects } from '../../styling/staticObjects';
import { StyledObjectInfo } from '../../styling/StyledObjectInfo';

export class EptBinaryLoader implements ILoader {
private readonly _dataLoader: ModelDataProvider;
private readonly _styledObjectInfo: StyledObjectInfo | undefined;

static readonly WORKER_POOL = new WorkerPool(32, EptDecoderWorker);

extension(): string {
return '.bin';
}

constructor(dataLoader: ModelDataProvider) {
constructor(dataLoader: ModelDataProvider, styledObjectInfo?: StyledObjectInfo) {
this._dataLoader = dataLoader;
this._styledObjectInfo = styledObjectInfo;
}

async load(node: PointCloudEptGeometryNode): Promise<void> {
Expand Down Expand Up @@ -103,15 +105,17 @@ export class EptBinaryLoader implements ILoader {
mins: toArray(node.key.b.min)
};

const offsetVec = node.boundingBox.min;
if (this._styledObjectInfo) {
const offsetVec = node.boundingBox.min;

const objectMessage: ObjectsCommand = {
type: 'objects',
objects: hardCodedObjects,
pointOffset: [offsetVec.x, offsetVec.y, offsetVec.z] as [number, number, number]
};
const objectMessage: ObjectsCommand = {
type: 'objects',
objects: this._styledObjectInfo.styledObjects,
pointOffset: [offsetVec.x, offsetVec.y, offsetVec.z] as [number, number, number]
};

autoTerminatingWorker.worker.postMessage(objectMessage);
autoTerminatingWorker.worker.postMessage(objectMessage);
}

const parseMessage: ParseCommand = {
type: 'parse',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { ModelDataProvider } from '@reveal/modeldata-api';
import { StyledObjectInfo } from '../../styling/StyledObjectInfo';
import { PointCloudEptGeometry } from '../geometry/PointCloudEptGeometry';
import { PointCloudEptGeometryNode } from '../geometry/PointCloudEptGeometryNode';

export class EptLoader {
static async load(
baseUrl: string,
fileName: string,
modelDataProvider: ModelDataProvider
modelDataProvider: ModelDataProvider,
styledObjectInfo?: StyledObjectInfo
): Promise<PointCloudEptGeometry> {
return modelDataProvider.getJsonFile(baseUrl, fileName).then(async (json: any) => {
const url = baseUrl + '/';
const geometry = new PointCloudEptGeometry(url, json, modelDataProvider);
const geometry = new PointCloudEptGeometry(url, json, modelDataProvider, styledObjectInfo);
const root = new PointCloudEptGeometryNode(geometry, modelDataProvider);

geometry.root = root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ export class PointCloudMaterial extends RawShaderMaterial {
private readonly _gradient = SpectralGradient;
private gradientTexture: Texture | undefined = generateGradientTexture(this._gradient);

private readonly _objectTexture: THREE.DataTexture =
generateDataTexture(OBJECT_STYLING_TEXTURE_WIDTH, OBJECT_STYLING_TEXTURE_HEIGHT, new Color(0x000000));
private readonly _objectTexture: THREE.DataTexture = generateDataTexture(
OBJECT_STYLING_TEXTURE_WIDTH,
OBJECT_STYLING_TEXTURE_HEIGHT,
new Color(0x000000)
);

private _classification: IClassification = DEFAULT_CLASSIFICATION;
private classificationTexture: Texture | undefined = generateClassificationTexture(this._classification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Camera, WebGLRenderer } from 'three';
import { LRU } from '../utils/lru';
import { PointCloudOctree } from '../tree/PointCloudOctree';
import { IVisibilityUpdateResult } from './IVisibilityUpdateResult';
import { StyledObjectInfo } from '../../styling/StyledObjectInfo';

export interface IPotree {
pointBudget: number;
maxNumNodesLoading: number;
lru: LRU;

loadPointCloud(baseUrl: string, fileName: string): Promise<PointCloudOctree>;
loadPointCloud(baseUrl: string, fileName: string, styledObjectInfo?: StyledObjectInfo): Promise<PointCloudOctree>;

updatePointClouds(pointClouds: PointCloudOctree[], camera: Camera, renderer: WebGLRenderer): IVisibilityUpdateResult;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
* Copyright 2022 Cognite AS
*/

import { StyleObject } from '../../styling/StyleObject';
import { StyledObject } from '../../styling/StyledObject';
import { Vec3, v3Add } from '../../styling/shapes/linalg';

export function computeObjectIdBuffer(positionBuffer: Float32Array,
objectList: StyleObject[],
pointOffset: Vec3): ArrayBuffer {
const rawArrayBuffer = new ArrayBuffer(2 * positionBuffer.length / 3);
export function computeObjectIdBuffer(
positionBuffer: Float32Array,
objectList: StyledObject[],
pointOffset: Vec3
): ArrayBuffer {
const rawArrayBuffer = new ArrayBuffer((2 * positionBuffer.length) / 3);
const objectIdBuffer = new Uint16Array(rawArrayBuffer);

for (let i = 0; i < objectIdBuffer.length; i++) {

// 0 is default / unassigned value
objectIdBuffer[i] = 0;

const position: Vec3 =
v3Add([ positionBuffer[3 * i + 0],
positionBuffer[3 * i + 1],
positionBuffer[3 * i + 2] ],
pointOffset);
const position: Vec3 = v3Add(
[positionBuffer[3 * i + 0], positionBuffer[3 * i + 1], positionBuffer[3 * i + 2]],
pointOffset
);

for (const obj of objectList) {
if (obj.shape.containsPoint(position)) {
Expand Down
Loading

0 comments on commit bfa4c5f

Please sign in to comment.