Skip to content

Commit

Permalink
Merge branch 'master' into savokr/add-view-fetching-for-mapped-nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Savokr committed Sep 4, 2023
2 parents 8c687b0 + 42e4a45 commit 2a9783a
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 127 deletions.
6 changes: 3 additions & 3 deletions documentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
},
"dependencies": {
"@azure/msal-browser": "2.38.2",
"@codemirror/lang-javascript": "6.1.9",
"@codemirror/lang-javascript": "6.2.0",
"@docusaurus/core": "2.4.1",
"@docusaurus/preset-classic": "2.4.1",
"@docusaurus/remark-plugin-npm2yarn": "2.4.1",
"@uiw/codemirror-theme-material": "4.21.11",
"@uiw/react-codemirror": "4.21.9",
"@uiw/react-codemirror": "4.21.11",
"clsx": "2.0.0",
"cross-env": "7.0.3",
"ieee754": "1.2.1",
Expand All @@ -47,7 +47,7 @@
"replace": "1.2.2",
"rimraf": "5.0.1",
"typedoc": "0.24.8",
"typedoc-plugin-markdown": "3.15.4",
"typedoc-plugin-markdown": "3.16.0",
"typedoc-plugin-no-inherit": "1.4.0",
"typescript": "5.2.2"
},
Expand Down
40 changes: 20 additions & 20 deletions documentation/yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions react-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cognite/reveal-react-components",
"version": "0.13.1",
"version": "0.13.2",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module",
Expand All @@ -24,7 +24,7 @@
"styled-components": ">=5"
},
"devDependencies": {
"@babel/preset-env": "7.22.10",
"@babel/preset-env": "7.22.14",
"@babel/preset-react": "7.22.5",
"@babel/preset-typescript": "7.22.11",
"@cognite/cogs.js": "^9.17.0",
Expand Down
20 changes: 17 additions & 3 deletions react-components/src/components/NodeCacheProvider/FdmNodeCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class FdmNodeCache {
relevantFdmKeySet
);

const mappings = new Map(
const mappings = createMapWithAccumulatedValues(
relevantCachedEdgeData.map((data) => [data.edge.startNode.externalId, data.node])
);

Expand Down Expand Up @@ -145,11 +145,11 @@ export class FdmNodeCache {
relevantFdmKeySet: Set<FdmKey>
): ThreeDModelMappings {
if (edges === undefined || edges.length === 0)
return { modelId, revisionId, mappings: new Map<CogniteExternalId, Node3D>() };
return { modelId, revisionId, mappings: new Map<CogniteExternalId, Node3D[]>() };

const relevantEdges = intersectWithStartNodeIdSet(edges, relevantFdmKeySet);

const externalIdToNodeMap = new Map<CogniteExternalId, Node3D>(
const externalIdToNodeMap = createMapWithAccumulatedValues(
relevantEdges.map((edge) => [edge.edge.startNode.externalId, edge.node])
);

Expand Down Expand Up @@ -405,3 +405,17 @@ function intersectWithStartNodeIdSet(
return relevantFdmKeySet.has(fdmKey);
});
}

function createMapWithAccumulatedValues<K, V>(associations: Array<[K, V]>): Map<K, V[]> {
return associations.reduce((map, [key, value]) => {
const prevList = map.get(key);

if (prevList === undefined) {
map.set(key, [value]);
} else {
prevList.push(value);
}

return map;
}, new Map<K, V[]>());
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,19 @@
* Copyright 2023 Cognite AS
*/

import {
type CogniteInternalId,
type CogniteClient,
type Node3D,
type CogniteExternalId
} from '@cognite/sdk';
import { type CogniteClient, type Node3D } from '@cognite/sdk';
import { type FdmSDK } from '../../utilities/FdmSDK';
import {
type TreeIndex,
type Fdm3dNodeData,
type FdmEdgeWithNode,
type FdmCadEdge,
type ModelRevisionId
} from './types';
import { type TreeIndex, type Fdm3dNodeData, type FdmEdgeWithNode, type FdmCadEdge } from './types';

import {
fetchAncestorNodesForTreeIndex,
fetchNodesForNodeIds,
getMappingEdgesForNodeIds,
inspectNodes
} from './requests';

import { max } from 'lodash';

import assert from 'assert';
import { type ThreeDModelMappings } from '../../hooks/types';

export class RevisionFdmNodeCache {
private readonly _cogniteClient: CogniteClient;
Expand All @@ -51,31 +38,6 @@ export class RevisionFdmNodeCache {
this._revisionId = revisionId;
}

public async createExternalIdToNodeMapping(
modelRevisionId: ModelRevisionId,
externalIdToNodeMapping: Map<CogniteExternalId, CogniteInternalId>,
edgeMap: Map<CogniteExternalId, FdmCadEdge>
): Promise<ThreeDModelMappings> {
const externalIds = [...externalIdToNodeMapping.keys()];
const nodeIds = [...externalIdToNodeMapping.values()];

const nodes = await fetchNodesForNodeIds(
modelRevisionId.modelId,
modelRevisionId.revisionId,
nodeIds,
this._cogniteClient
);

const externalIdToNode = new Map<CogniteExternalId, Node3D>(
externalIds.map((externalId, ind) => [externalId, nodes[ind]])
);

return {
...modelRevisionId,
mappings: externalIdToNode
};
}

public async getClosestParentFdmData(searchTreeIndex: number): Promise<Fdm3dNodeData[]> {
const cachedFdmData = this._treeIndexToFdmEdges.get(searchTreeIndex);

Expand Down Expand Up @@ -230,13 +192,6 @@ export class RevisionFdmNodeCache {
public getAllEdges(): FdmEdgeWithNode[] {
return [...this._treeIndexToFdmEdges.values()].flat();
}

getIds(): ModelRevisionId {
return {
modelId: this._modelId,
revisionId: this._revisionId
};
}
}

function fdmEdgeWithNodeToFdm3dNodeData(fdmEdgeWithNode: FdmEdgeWithNode[]): Fdm3dNodeData[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const LayersButton = (): ReactElement => {
setPointCloudModelIds(pointCloudIds);
}
}
}, [viewer.models, cadModelIds, pointCloudModelIds]);
}, [viewer.models]);

const updated3DResourcesLayerData: Reveal3DResourcesLayerStates = useMemo(() => {
if (cadModelName.data === null && pointCloudModelName.data === null) {
Expand Down
2 changes: 1 addition & 1 deletion react-components/src/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type Node3D, type CogniteExternalId } from '@cognite/sdk';
export type ThreeDModelMappings = {
modelId: number;
revisionId: number;
mappings: Map<CogniteExternalId, Node3D>;
mappings: Map<CogniteExternalId, Node3D[]>;
};

export type Model3DEdgeProperties = {
Expand Down
4 changes: 3 additions & 1 deletion react-components/src/hooks/use3DModelName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export const use3DModelName = (ids: number[]): UseQueryResult<string[] | undefin
return modelResolvedNames;
};

const queryResult = useQuery<string[] | undefined>(['cdf', '3d', 'model', ids], queryFunction);
const queryResult = useQuery<string[] | undefined>(['cdf', '3d', 'model', ids], queryFunction, {
staleTime: Infinity
});

return queryResult;
};
45 changes: 32 additions & 13 deletions react-components/src/hooks/useCalculateModelsStyling.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
useMappedEdgesForRevisions
} from '../components/NodeCacheProvider/NodeCacheProvider';
import { useMemo } from 'react';
import { type FdmEdgeWithNode, type TreeIndex } from '../components/NodeCacheProvider/types';
import {
type NodeId,
type FdmEdgeWithNode,
type TreeIndex
} from '../components/NodeCacheProvider/types';
import {
type NodeStylingGroup,
type TreeIndexStylingGroup
Expand Down Expand Up @@ -168,12 +172,15 @@ function calculateCadModelStyling(

return resourcesStylingGroups
.map((resourcesGroup) => {
const modelMappedNodes = resourcesGroup.fdmAssetExternalIds
const modelMappedNodeLists = resourcesGroup.fdmAssetExternalIds
.map((uniqueId) => modelMappings.get(uniqueId.externalId))
.filter((node): node is Node3D => node !== undefined);
.filter((nodeMap): nodeMap is Map<NodeId, Node3D> => nodeMap !== undefined)
.map((nodeMap) => [...nodeMap.values()]);
return {
style: resourcesGroup.style.cad,
treeIndices: modelMappedNodes.flatMap((n) => getNodeSubtreeIndices(n))
treeIndices: modelMappedNodeLists.flatMap((nodes) =>
nodes.flatMap((n) => getNodeSubtreeIndices(n))
)
};
})
.filter((group) => group.treeIndices.length > 0);
Expand All @@ -186,26 +193,38 @@ function getNodeSubtreeIndices(node: Node3D): TreeIndex[] {
function getModelMappings(
mappings: ThreeDModelMappings[],
model: CadModelOptions
): Map<CogniteExternalId, Node3D> {
): Map<CogniteExternalId, Map<NodeId, Node3D>> {
return mappings
.filter(
(mapping) => mapping.modelId === model.modelId && mapping.revisionId === model.revisionId
)
.reduce(
// reduce is added to avoid duplication of a models that span several pages.
(acc, mapping) => {
mergeMaps(acc.mappings, mapping.mappings);
mergeMapsWithDeduplicatedNodes(acc.mappings, mapping.mappings);
return acc;
},
{ modelId: model.modelId, revisionId: model.revisionId, mappings: new Map<string, Node3D>() }
{
modelId: model.modelId,
revisionId: model.revisionId,
mappings: new Map<string, Map<NodeId, Node3D>>()
}
).mappings;
}

function mergeMaps(
targetMap: Map<string, Node3D>,
addedMap: Map<string, Node3D>
): Map<string, Node3D> {
addedMap.forEach((value, key) => targetMap.set(key, value));
function mergeMapsWithDeduplicatedNodes(
targetMap: Map<string, Map<NodeId, Node3D>>,
addedMap: Map<string, Node3D[]>
): Map<string, Map<NodeId, Node3D>> {
return [...addedMap.entries()].reduce((map, [fdmKey, nodesToAdd]) => {
const targetSet = map.get(fdmKey);

if (targetSet !== undefined) {
nodesToAdd.forEach((node) => targetSet.set(node.id, node));
} else {
map.set(fdmKey, new Map(nodesToAdd.map((node) => [node.id, node])));
}

return targetMap;
return map;
}, targetMap);
}
Loading

0 comments on commit 2a9783a

Please sign in to comment.