-
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): initial support for Observations (#4631)
* feat(react-components): initial support for Observations --------- Co-authored-by: cognite-bulldozer[bot] <51074376+cognite-bulldozer[bot]@users.noreply.github.com>
- Loading branch information
1 parent
dfb1097
commit c437a3f
Showing
24 changed files
with
374 additions
and
80 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
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
79 changes: 79 additions & 0 deletions
79
...components/src/architecture/concrete/observationsDomainObject/ObservationsDomainObject.ts
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,79 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { CDF_TO_VIEWER_TRANSFORMATION, type Overlay3D, Overlay3DCollection } from '@cognite/reveal'; | ||
import { OBSERVATION_SOURCE, type ObservationProperties, type Observation } from './models'; | ||
import { VisualDomainObject } from '../../base/domainObjects/VisualDomainObject'; | ||
import { type ThreeView } from '../../base/views/ThreeView'; | ||
import { ObservationsView } from './ObservationsView'; | ||
import { type TranslateKey } from '../../base/utilities/TranslateKey'; | ||
import { type FdmSDK, type InstanceFilter } from '../../../utilities/FdmSDK'; | ||
import { Vector3 } from 'three'; | ||
import { DEFAULT_OVERLAY_COLOR } from './constants'; | ||
import { Changes } from '../../base/domainObjectsHelpers/Changes'; | ||
|
||
export class ObservationsDomainObject extends VisualDomainObject { | ||
private _selectedOverlay: Overlay3D<Observation> | undefined; | ||
|
||
private readonly _collection = new Overlay3DCollection<Observation>([], { | ||
defaultOverlayColor: DEFAULT_OVERLAY_COLOR | ||
}); | ||
|
||
constructor(fdmSdk: FdmSDK) { | ||
super(); | ||
void fetchObservations(fdmSdk).then((observations) => { | ||
this.initializeCollection(observations); | ||
this.notify(Changes.geometry); | ||
}); | ||
} | ||
|
||
public override get typeName(): TranslateKey { | ||
return { fallback: ObservationsDomainObject.name }; | ||
} | ||
|
||
protected override createThreeView(): ThreeView<ObservationsDomainObject> | undefined { | ||
return new ObservationsView(); | ||
} | ||
|
||
private initializeCollection(observations: Observation[]): void { | ||
const observationOverlays = observations.map((observation) => { | ||
const position = new Vector3( | ||
observation.properties.positionX, | ||
observation.properties.positionY, | ||
observation.properties.positionZ | ||
).applyMatrix4(CDF_TO_VIEWER_TRANSFORMATION); | ||
|
||
return { | ||
position, | ||
content: observation | ||
}; | ||
}); | ||
|
||
this._collection.addOverlays(observationOverlays); | ||
} | ||
|
||
public get overlayCollection(): Overlay3DCollection<Observation> { | ||
return this._collection; | ||
} | ||
|
||
public getSelectedOverlay(): Overlay3D<Observation> | undefined { | ||
return this._selectedOverlay; | ||
} | ||
|
||
public setSelectedOverlay(overlay: Overlay3D<Observation> | undefined): void { | ||
this._selectedOverlay = overlay; | ||
this.notify(Changes.selected); | ||
} | ||
} | ||
|
||
async function fetchObservations(fdmSdk: FdmSDK): Promise<Observation[]> { | ||
const observationsFilter: InstanceFilter = {}; | ||
|
||
const observationResult = await fdmSdk.filterAllInstances<ObservationProperties>( | ||
observationsFilter, | ||
'node', | ||
OBSERVATION_SOURCE | ||
); | ||
|
||
return observationResult.instances; | ||
} |
58 changes: 58 additions & 0 deletions
58
react-components/src/architecture/concrete/observationsDomainObject/ObservationsTool.ts
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,58 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { type IconType } from '@cognite/cogs.js'; | ||
import { type TranslateKey } from '../../base/utilities/TranslateKey'; | ||
import { ObservationsDomainObject } from './ObservationsDomainObject'; | ||
import { BaseEditTool } from '../../base/commands/BaseEditTool'; | ||
import { type VisualDomainObject } from '../../base/domainObjects/VisualDomainObject'; | ||
|
||
export class ObservationsTool extends BaseEditTool { | ||
public override get icon(): IconType { | ||
return 'Location'; | ||
} | ||
|
||
public override get tooltip(): TranslateKey { | ||
return { fallback: 'Show observations' }; | ||
} | ||
|
||
public override onActivate(): void { | ||
let domainObject = this.getObservationsDomainObject(); | ||
if (domainObject === undefined) { | ||
domainObject = new ObservationsDomainObject(this.renderTarget.fdmSdk); | ||
this.renderTarget.rootDomainObject.addChild(domainObject); | ||
} | ||
domainObject.setVisibleInteractive(true, this.renderTarget); | ||
} | ||
|
||
public override onDeactivate(): void { | ||
this.getObservationsDomainObject()?.setVisibleInteractive(false, this.renderTarget); | ||
} | ||
|
||
public override async onClick(event: PointerEvent): Promise<void> { | ||
const intersection = await this.getIntersection(event); | ||
|
||
const domainObject = this.getIntersectedSelectableDomainObject(intersection); | ||
if (!(domainObject instanceof ObservationsDomainObject)) { | ||
await super.onClick(event); | ||
return; | ||
} | ||
|
||
const normalizedCoords = this.getNormalizedPixelCoordinates(event); | ||
|
||
const intersectedOverlay = domainObject.overlayCollection.intersectOverlays( | ||
normalizedCoords, | ||
this.renderTarget.camera | ||
); | ||
|
||
domainObject.setSelectedOverlay(intersectedOverlay); | ||
} | ||
|
||
protected override canBeSelected(domainObject: VisualDomainObject): boolean { | ||
return domainObject instanceof ObservationsDomainObject; | ||
} | ||
|
||
public getObservationsDomainObject(): ObservationsDomainObject | undefined { | ||
return this.rootDomainObject.getDescendantByType(ObservationsDomainObject); | ||
} | ||
} |
Oops, something went wrong.