Skip to content

Commit

Permalink
chore: make direct reference from render targe to caches
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonflatval-cognite committed Dec 1, 2024
1 parent 8cdc451 commit 2cc155f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
*/
import { type CogniteClient } from '@cognite/sdk/dist/src';
import { AssetMappingAndNode3DCache } from '../../../components/CacheProvider/AssetMappingAndNode3DCache';
import { DomainObject } from '../../base/domainObjects/DomainObject';
import { type TranslationInput } from '../../base/utilities/TranslateInput';
import { FdmNodeCache } from '../../../components/CacheProvider/FdmNodeCache';
import { FdmSDK } from '../../../data-providers/FdmSDK';
import { type Fdm3dDataProvider } from '../../../data-providers/Fdm3dDataProvider';
import { PointCloudAnnotationCache } from '../../../components/CacheProvider/PointCloudAnnotationCache';
import { Image360AnnotationCache } from '../../../components/CacheProvider/Image360AnnotationCache';
import { type Cognite3DViewer, type DataSourceType } from '@cognite/reveal';

export class CachesDomainObject extends DomainObject {
export class CdfCaches {
private readonly _assetMappingAndNode3dCache: AssetMappingAndNode3DCache;
private readonly _fdmNodeCache: FdmNodeCache;
private readonly _pointCloudAnnotationCache: PointCloudAnnotationCache;
Expand All @@ -23,7 +21,6 @@ export class CachesDomainObject extends DomainObject {
fdm3dDataProvider: Fdm3dDataProvider,
viewer: Cognite3DViewer<DataSourceType>
) {
super();
const fdmClient = new FdmSDK(cdfClient);

this._assetMappingAndNode3dCache = new AssetMappingAndNode3DCache(cdfClient);
Expand All @@ -32,10 +29,6 @@ export class CachesDomainObject extends DomainObject {
this._image360AnnotationCache = new Image360AnnotationCache(cdfClient, viewer);
}

public override get typeName(): TranslationInput {
return { untranslated: 'Caches' };
}

public get assetMappingAndNode3dCache(): AssetMappingAndNode3DCache {
return this._assetMappingAndNode3dCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { Changes } from '../domainObjectsHelpers/Changes';
import { type CogniteClient } from '@cognite/sdk';
import { type BaseTool } from '../commands/BaseTool';
import { ContextMenuController } from './ContextMenuController';
import { type CdfCaches } from './CdfCaches';

const DIRECTIONAL_LIGHT_NAME = 'DirectionalLight';

Expand All @@ -50,6 +51,7 @@ export class RevealRenderTarget {
private readonly _commandsController: CommandsController;
private readonly _rootDomainObject: RootDomainObject;
private readonly _contextmenuController: ContextMenuController;
private readonly _cdfCaches: CdfCaches;
private _ambientLight: AmbientLight | undefined;
private _directionalLight: DirectionalLight | undefined;
private _clippedBoundingBox: Box3 | undefined;
Expand All @@ -65,7 +67,7 @@ export class RevealRenderTarget {
// CONSTRUCTOR
// ==================================================

constructor(viewer: Cognite3DViewer<DataSourceType>, sdk: CogniteClient) {
constructor(viewer: Cognite3DViewer<DataSourceType>, sdk: CogniteClient, cdfCaches: CdfCaches) {
this._viewer = viewer;

const cameraManager = this.cameraManager;
Expand All @@ -75,6 +77,7 @@ export class RevealRenderTarget {
this._commandsController = new CommandsController(this.domElement);
this._commandsController.addEventListeners();
this._contextmenuController = new ContextMenuController();
this._cdfCaches = cdfCaches;
this._rootDomainObject = new RootDomainObject(this, sdk);

this.initializeLights();
Expand Down Expand Up @@ -119,6 +122,10 @@ export class RevealRenderTarget {
return this._contextmenuController;
}

public get cdfCaches(): CdfCaches {
return this._cdfCaches;
}

public get cursor(): string {
return this.domElement.style.cursor;
}
Expand Down
18 changes: 3 additions & 15 deletions react-components/src/components/CacheProvider/CacheProvider.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
/*!
* Copyright 2024 Cognite AS
*/
import { useMemo } from 'react';
import { CachesDomainObject } from '../../architecture/concrete/caches/CachesDomainObject';
import { type CdfCaches } from '../../architecture/base/renderTarget/CdfCaches';
import { useRenderTarget } from '../RevealCanvas';
import { type AssetMappingAndNode3DCache } from './AssetMappingAndNode3DCache';
import { type FdmNodeCache } from './FdmNodeCache';
import { type PointCloudAnnotationCache } from './PointCloudAnnotationCache';
import { type Image360AnnotationCache } from './Image360AnnotationCache';

const useCacheObject = (): CachesDomainObject => {
const useCacheObject = (): CdfCaches => {
const revealRenderTarget = useRenderTarget();
const cacheObj = useMemo<CachesDomainObject>(() => {
const cachesDomainObject =
revealRenderTarget.rootDomainObject.getDescendantByType(CachesDomainObject);

if (cachesDomainObject === undefined) {
throw Error('CachesDomainObject was not yet initialized in RevealRenderTarget');
}

return cachesDomainObject;
}, [revealRenderTarget]);

return cacheObj;
return revealRenderTarget.cdfCaches;
};

export const useFdmNodeCache = (): FdmNodeCache => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { type CameraStateParameters } from '../RevealCanvas/hooks/useCameraState
import { CoreDm3dFdm3dDataProvider } from '../../data-providers/core-dm-provider/CoreDm3dDataProvider';
import { LegacyFdm3dDataProvider } from '../../data-providers/legacy-fdm-provider/LegacyFdm3dDataProvider';
import { FdmSDK } from '../../data-providers/FdmSDK';
import { CachesDomainObject } from '../../architecture/concrete/caches/CachesDomainObject';
import { CdfCaches } from '../../architecture/base/renderTarget/CdfCaches';
import { type Fdm3dDataProvider } from '../../data-providers/Fdm3dDataProvider';

export type RevealContextProps = {
Expand Down Expand Up @@ -123,9 +123,10 @@ const useRevealFromKeepAlive = (
useFlexibleCameraManager: true,
hasEventListeners: false
});
renderTarget = new RevealRenderTarget(viewer, sdk);
renderTarget.rootDomainObject.addChildInteractive(
new CachesDomainObject(sdk, fdm3dDataProvider, viewer)
renderTarget = new RevealRenderTarget(
viewer,
sdk,
new CdfCaches(sdk, fdm3dDataProvider, viewer)
);
if (revealKeepAliveData !== undefined) {
revealKeepAliveData.renderTargetRef.current = renderTarget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@

import { type ReactNode, type ReactElement, useRef, useEffect } from 'react';
import { RevealKeepAliveContext } from './RevealKeepAliveContext';
import { type FdmNodeCache } from '../CacheProvider/FdmNodeCache';
import { type AssetMappingAndNode3DCache } from '../CacheProvider/AssetMappingAndNode3DCache';
import { type PointCloudAnnotationCache } from '../CacheProvider/PointCloudAnnotationCache';
import { type Image360AnnotationCache } from '../CacheProvider/Image360AnnotationCache';
import { type SceneIdentifiers } from '../SceneContainer/sceneTypes';
import { type RevealRenderTarget } from '../../architecture/base/renderTarget/RevealRenderTarget';

export function RevealKeepAlive({ children }: { children?: ReactNode }): ReactElement {
const renderTargetRef = useRef<RevealRenderTarget>();
const isRevealContainerMountedRef = useRef<boolean>(false);
const sceneLoadedRef = useRef<SceneIdentifiers>();
const fdmNodeCache = useRef<FdmNodeCache>();
const assetMappingCache = useRef<AssetMappingAndNode3DCache>();
const pointCloudAnnotationCache = useRef<PointCloudAnnotationCache>();
const image360AnnotationCache = useRef<Image360AnnotationCache>();

useEffect(() => {
return () => {
Expand All @@ -31,11 +23,7 @@ export function RevealKeepAlive({ children }: { children?: ReactNode }): ReactEl
value={{
renderTargetRef,
isRevealContainerMountedRef,
sceneLoadedRef,
fdmNodeCache,
assetMappingCache,
pointCloudAnnotationCache,
image360AnnotationCache
sceneLoadedRef
}}>
{children}
</RevealKeepAliveContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@
* Copyright 2023 Cognite AS
*/
import { type MutableRefObject, createContext, useContext } from 'react';
import { type FdmNodeCache } from '../CacheProvider/FdmNodeCache';
import { type AssetMappingAndNode3DCache } from '../CacheProvider/AssetMappingAndNode3DCache';
import { type PointCloudAnnotationCache } from '../CacheProvider/PointCloudAnnotationCache';
import { type Image360AnnotationCache } from '../CacheProvider/Image360AnnotationCache';
import { type SceneIdentifiers } from '../SceneContainer/sceneTypes';
import { type RevealRenderTarget } from '../../architecture/base/renderTarget/RevealRenderTarget';

export type RevealKeepAliveData = {
renderTargetRef: MutableRefObject<RevealRenderTarget | undefined>;
isRevealContainerMountedRef: MutableRefObject<boolean>;
sceneLoadedRef: MutableRefObject<SceneIdentifiers | undefined>;
fdmNodeCache: MutableRefObject<FdmNodeCache | undefined>;
assetMappingCache: MutableRefObject<AssetMappingAndNode3DCache | undefined>;
pointCloudAnnotationCache: MutableRefObject<PointCloudAnnotationCache | undefined>;
image360AnnotationCache: MutableRefObject<Image360AnnotationCache | undefined>;
};

export const RevealKeepAliveContext = createContext<RevealKeepAliveData | undefined>(undefined);
Expand Down
33 changes: 19 additions & 14 deletions react-components/stories/utilities/RevealStoryContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
import { useRef, type ReactElement, useMemo } from 'react';
import { RevealKeepAliveContext } from '../../src/components/RevealKeepAlive/RevealKeepAliveContext';
import { RevealCanvas } from '../../src/components/RevealCanvas/RevealCanvas';
import { type FdmNodeCache } from '../../src/components/CacheProvider/FdmNodeCache';
import { type AssetMappingAndNode3DCache } from '../../src/components/CacheProvider/AssetMappingAndNode3DCache';
import { type CogniteClient } from '@cognite/sdk';
import { Cognite3DViewer, type DataSourceType } from '@cognite/reveal';
import { createSdkByUrlToken } from './createSdkByUrlToken';
import { type PointCloudAnnotationCache } from '../../src/components/CacheProvider/PointCloudAnnotationCache';
import {
RevealContext,
type RevealContextProps
} from '../../src/components/RevealContext/RevealContext';
import { type Image360AnnotationCache } from '../../src/components/CacheProvider/Image360AnnotationCache';
import { type SceneIdentifiers } from '../../src/components/SceneContainer/sceneTypes';
import { RevealRenderTarget } from '../../src/architecture/base/renderTarget/RevealRenderTarget';
import { StoryBookConfig } from '../../src/architecture/concrete/config/StoryBookConfig';
import { FdmSDK } from '../../src/data-providers/FdmSDK';
import { CoreDm3dFdm3dDataProvider } from '../../src/data-providers/core-dm-provider/CoreDm3dDataProvider';
import { LegacyFdm3dDataProvider } from '../../src/data-providers/legacy-fdm-provider/LegacyFdm3dDataProvider';
import { CdfCaches } from '../../src/architecture/base/renderTarget/CdfCaches';

type RevealStoryContainerProps = Omit<RevealContextProps, 'sdk'> & {
sdk?: CogniteClient;
Expand Down Expand Up @@ -51,28 +51,33 @@ export const RevealStoryContext = ({
});
}

const renderTarget = new RevealRenderTarget(viewer, sdkInstance);
const fdmSdk = new FdmSDK(sdkInstance);

const fdm3dDataProvider =
(rest.useCoreDm ?? false)
? new CoreDm3dFdm3dDataProvider([], fdmSdk)
: new LegacyFdm3dDataProvider(fdmSdk, sdkInstance);

const renderTarget = new RevealRenderTarget(
viewer,
sdkInstance,
new CdfCaches(sdkInstance, fdm3dDataProvider, viewer)
);

renderTarget.setConfig(new StoryBookConfig());
return renderTarget;
}, [viewer]);

const renderTargetRef = useRef<RevealRenderTarget | undefined>(renderTarget);
const isRevealContainerMountedRef = useRef<boolean>(true);
const sceneLoadedRef = useRef<SceneIdentifiers>();
const fdmNodeCache = useRef<FdmNodeCache | undefined>();
const assetMappingCache = useRef<AssetMappingAndNode3DCache | undefined>();
const pointCloudAnnotationCache = useRef<PointCloudAnnotationCache | undefined>();
const image360AnnotationCache = useRef<Image360AnnotationCache | undefined>();

return (
<RevealKeepAliveContext.Provider
value={{
renderTargetRef,
isRevealContainerMountedRef,
sceneLoadedRef,
fdmNodeCache,
assetMappingCache,
pointCloudAnnotationCache,
image360AnnotationCache
sceneLoadedRef
}}>
<RevealContext sdk={sdkInstance} {...rest}>
{children}
Expand Down

0 comments on commit 2cc155f

Please sign in to comment.