From 161022c104b64d9bf234262ec15e2f7421e25b5a Mon Sep 17 00:00:00 2001 From: "Christopher J. Tannum" Date: Tue, 8 Nov 2022 14:30:25 +0100 Subject: [PATCH] fix: create a box3 when deserializing areas in treeindex collection (#2671) Co-authored-by: cognite-bulldozer[bot] <51074376+cognite-bulldozer[bot]@users.noreply.github.com> --- .../src/NodeCollectionDeserializer.test.ts | 40 ++++++++++++++++++- .../src/NodeCollectionDeserializer.ts | 12 +++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/viewer/packages/cad-styling/src/NodeCollectionDeserializer.test.ts b/viewer/packages/cad-styling/src/NodeCollectionDeserializer.test.ts index 65fadb5f7dc..6f8a720a2e0 100644 --- a/viewer/packages/cad-styling/src/NodeCollectionDeserializer.test.ts +++ b/viewer/packages/cad-styling/src/NodeCollectionDeserializer.test.ts @@ -4,12 +4,15 @@ import { CogniteClient } from '@cognite/sdk'; import { IndexSet } from '@reveal/utilities'; +import { Mock } from 'moq.ts'; import { createCadModel } from '../../../test-utilities/src/createCadModel'; +import { CdfModelNodeCollectionDataProvider } from './CdfModelNodeCollectionDataProvider'; import { NodeCollectionDeserializer } from './NodeCollectionDeserializer'; +import { SerializedNodeCollection } from './SerializedNodeCollection'; import { TreeIndexNodeCollection } from './TreeIndexNodeCollection'; -describe('NodeCollectionDeserializer', () => { +describe(NodeCollectionDeserializer.name, () => { test('deserialize TreeIndexSet without option parameter', async () => { const sdk = new CogniteClient({ appId: 'cognite.reveal.unittest', @@ -35,4 +38,39 @@ describe('NodeCollectionDeserializer', () => { }) ).toBeTruthy(); }); + + test('deserialize TreeIndexNodeCollection with areas', async () => { + const deserializer = NodeCollectionDeserializer.Instance; + + const sdkMock = new Mock().object(); + const nodeCollectionProviderMock = new Mock().object(); + const serializedNodeCollection: SerializedNodeCollection = { + token: 'TreeIndexNodeCollection', + state: [ + { + from: 0, + count: 3, + toInclusive: 2 + } + ], + options: { + areas: [ + { + min: { + x: 0, + y: 0, + z: 0 + }, + max: { + x: 1, + y: 1, + z: 1 + } + } + ] + } + }; + const deserialize = deserializer.deserialize(sdkMock, nodeCollectionProviderMock, serializedNodeCollection); + await expect(deserialize).resolves.not.toThrow(); + }); }); diff --git a/viewer/packages/cad-styling/src/NodeCollectionDeserializer.ts b/viewer/packages/cad-styling/src/NodeCollectionDeserializer.ts index 387ed0140ee..3483e7b19c8 100644 --- a/viewer/packages/cad-styling/src/NodeCollectionDeserializer.ts +++ b/viewer/packages/cad-styling/src/NodeCollectionDeserializer.ts @@ -2,6 +2,7 @@ * Copyright 2021 Cognite AS */ +import * as THREE from 'three'; import assert from 'assert'; import { CogniteClient } from '@cognite/sdk'; import { NumericRange, IndexSet } from '@reveal/utilities'; @@ -104,7 +105,16 @@ export class NodeCollectionDeserializer { descriptor.state.forEach((range: NumericRange) => indexSet.addRange(new NumericRange(range.from, range.count))); const nodeCollection = new TreeIndexNodeCollection(indexSet); if (descriptor.options?.areas !== undefined) { - nodeCollection.addAreas(descriptor.options.areas); + const areas = descriptor.options?.areas as { + min: { x: number; y: number; z: number }; + max: { x: number; y: number; z: number }; + }[]; + const areaBoxes = areas.map(area => { + const min = new THREE.Vector3(area.min.x, area.min.y, area.min.z); + const max = new THREE.Vector3(area.max.x, area.max.y, area.max.z); + return new THREE.Box3(min, max); + }); + nodeCollection.addAreas(areaBoxes); } return Promise.resolve(nodeCollection);