-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-components): add useCameraNavigation hook (#3581)
* feat: add cameraNavigation hook * fix: restore stories and fix styling cleanup issue * chore: bump react components version to 0.10.0 * chore: import string
- Loading branch information
Showing
20 changed files
with
159 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
react-components/src/components/CameraController/CameraController.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
react-components/src/components/Reveal3DResources/ModelsLoadingContext.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/*! | ||
* Copyright 2023 Cognite AS | ||
*/ | ||
|
||
import { type CogniteCadModel } from '@cognite/reveal'; | ||
import { useReveal } from '../components/RevealContainer/RevealContext'; | ||
import { useFdmSdk } from '../components/RevealContainer/SDKProvider'; | ||
import { SYSTEM_3D_EDGE_SOURCE, type InModel3dEdgeProperties } from '../utilities/globalDataModels'; | ||
|
||
export type CameraNavigationActions = { | ||
fitCameraToAllModels: () => void; | ||
fitCameraToModelNode: (revisionId: number, nodeId: number) => Promise<void>; | ||
fitCameraToInstance: (externalId: string, space: string) => Promise<void>; | ||
}; | ||
|
||
export const useCameraNavigation = (): CameraNavigationActions => { | ||
const viewer = useReveal(); | ||
const fdmSDK = useFdmSdk(); | ||
|
||
const fitCameraToAllModels = (): void => { | ||
const models = viewer.models; | ||
if (models.length === 0) { | ||
return; | ||
} | ||
viewer.fitCameraToModels(models, undefined, true); | ||
}; | ||
|
||
const fitCameraToModelNode = async (revisionId: number, nodeId: number): Promise<void> => { | ||
const model = viewer.models.find((m) => m.revisionId === revisionId); | ||
if (model === undefined) { | ||
await Promise.reject(new Error(`Could not find model with revision ${revisionId}`)); | ||
return; | ||
} | ||
const nodeBoundingBox = await (model as CogniteCadModel).getBoundingBoxByNodeId(nodeId); | ||
viewer.cameraManager.fitCameraToBoundingBox(nodeBoundingBox); | ||
}; | ||
|
||
const fitCameraToInstance = async (externalId: string, space: string): Promise<void> => { | ||
const fdmAssetMappingFilter = { | ||
equals: { | ||
property: ['edge', 'startNode'], | ||
value: { externalId, space } | ||
} | ||
}; | ||
|
||
const assetEdges = await fdmSDK.filterInstances<InModel3dEdgeProperties>( | ||
fdmAssetMappingFilter, | ||
'edge', | ||
SYSTEM_3D_EDGE_SOURCE | ||
); | ||
|
||
if (assetEdges.edges.length === 0) { | ||
await Promise.reject( | ||
new Error(`Could not find a connected model to instance ${externalId} in space ${space}`) | ||
); | ||
return; | ||
} | ||
|
||
const { revisionId, revisionNodeId } = assetEdges.edges[0].properties; | ||
await fitCameraToModelNode(revisionId, revisionNodeId); | ||
}; | ||
|
||
return { | ||
fitCameraToAllModels, | ||
fitCameraToInstance, | ||
fitCameraToModelNode | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.