Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Make example code for training and some changes in the architecture #4543

Merged
merged 34 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d21937d
Initial commit
nilscognite May 25, 2024
9ac6395
Some movements
nilscognite May 25, 2024
0c69ba7
Fix linter problems
nilscognite May 25, 2024
53499d6
Make the axis a tiny better
nilscognite May 25, 2024
648d916
Comments only
nilscognite May 25, 2024
c23aa7d
Some rafactoring
nilscognite May 25, 2024
e0bed21
Make it better
nilscognite May 26, 2024
39f5ae6
Update MeasurementTool.ts
nilscognite May 26, 2024
89243a8
Fix lint
nilscognite May 26, 2024
1f05ec0
Make better axis tick labels, but still not perfect
nilscognite May 26, 2024
3f8ed41
handleEscape better
nilscognite May 26, 2024
bc61459
Changes in RevealRenderTarget
nilscognite May 26, 2024
7d0b716
Update RootDomainObject.ts
nilscognite May 26, 2024
96f9bd1
Fix smal bug
nilscognite May 27, 2024
2a4de0a
Moving code
nilscognite May 27, 2024
7826f7c
Merge branch 'master' into np/architecture-continued
nilscognite May 27, 2024
a580f55
Fix typo
nilscognite May 27, 2024
7570059
Set new default value
nilscognite May 27, 2024
0f91bd0
Fix typo
nilscognite May 27, 2024
ad31e2d
Merge branch 'master' into np/example-code
nilscognite May 27, 2024
7f4ee11
Merge branch 'master' into np/example-code
nilscognite May 28, 2024
ad63029
Initial commit
nilscognite May 28, 2024
f2748f2
Update Architecture.stories.tsx
nilscognite May 28, 2024
98d8406
Update yarn.lock
nilscognite May 28, 2024
316fd10
Fix MeasureLineView so lines are visible from long distance
nilscognite May 28, 2024
d42f04c
Make a DefaultRevealConfig
nilscognite May 28, 2024
d4804cc
Add override keyword
nilscognite May 28, 2024
b5bcab4
Remove some unneccesary code
nilscognite May 28, 2024
0b55801
Merge branch 'master' into np/architecture-continued
nilscognite May 28, 2024
92fa70e
Merge branch 'master' into np/example-code
nilscognite May 28, 2024
4bddd3f
Merge branch 'np/architecture-continued' of https://github.com/cognit…
nilscognite May 28, 2024
f6bc2c0
Update ExampleTool.ts
nilscognite May 28, 2024
2d0fa22
Merge branch 'master' into np/architecture-continued
nilscognite May 28, 2024
7fcbcc6
Update react-components/stories/Architecture.stories.tsx
nilscognite May 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions react-components/src/architecture/base/commands/BaseTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export abstract class BaseTool extends RenderTargetCommand {
return 'default';
}

public getToolbar(): Array<BaseCommand | undefined> | undefined {
return undefined; // Override this to add extra buttons to a separate toolbar
public getToolbar(): Array<BaseCommand | undefined> {
return []; // Override this to add extra buttons to a separate toolbar
}

public getToolbarStyle(): PopupStyle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { BLACK_COLOR, WHITE_COLOR } from '../utilities/colors/colorExtensions';
import { Views } from '../domainObjectsHelpers/Views';
import { type PanelInfo } from '../domainObjectsHelpers/PanelInfo';
import { PopupStyle } from '../domainObjectsHelpers/PopupStyle';
import { RootDomainObject } from './RootDomainObject';

/**
* Represents an abstract base class for domain objects.
Expand Down Expand Up @@ -242,6 +243,22 @@ export abstract class DomainObject {
*/
protected notifyCore(change: DomainObjectChange): void {
this.views.notify(this, change);

// This is a little bit dirty, but will be refacored by using onIdle()
if (
change.isChanged(
Changes.visibleState,
Changes.active,
Changes.active,
Changes.selected,
Changes.childAdded,
Changes.childDeleted
)
) {
if (this.root instanceof RootDomainObject) {
this.root.renderTarget.toolController.update();
}
}
}

// ==================================================
Expand Down Expand Up @@ -663,10 +680,12 @@ export abstract class DomainObject {
return true;
}

public removeInteractive(): void {
// You may call canBeDeleted() before calling this
public removeInteractive(checkCanBeDeleted = true): void {
if (checkCanBeDeleted && !this.canBeRemoved) {
return;
}
for (const child of this.children) {
child.removeInteractive();
child.removeInteractive(false); // If parent can be removed, so the children also
}
const { parent } = this;
this.notify(Changes.deleted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export abstract class BaseDragger {
// ==================================================

protected constructor(props: CreateDraggerProps) {
// Note: that yje point and the ray comes in CDF coordinates
nilscognite marked this conversation as resolved.
Show resolved Hide resolved
this.point = props.point;
this.ray = props.ray;
}
Expand All @@ -38,7 +39,7 @@ export abstract class BaseDragger {
}

// This must be overriden
// Notte that the ray comes in CDF coordinates
// Note: that the ray comes in CDF coordinates
public abstract onPointerDrag(_event: PointerEvent, ray: Ray): boolean;

public onPointerUp(_event: PointerEvent): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

type PopupProps = {
horizontal?: boolean;
left?: number;
right?: number;
top?: number;
Expand All @@ -13,6 +14,7 @@ type PopupProps = {
};

export class PopupStyle {
private readonly _horizontal: boolean = true;
private readonly _left?: number = undefined;
private readonly _right?: number = undefined;
private readonly _top?: number = undefined;
Expand All @@ -21,6 +23,9 @@ export class PopupStyle {
private readonly _padding: number = 16; // margin inside the popup

public constructor(props: PopupProps) {
if (props.horizontal !== undefined) {
this._horizontal = props.horizontal;
}
this._left = props.left;
this._right = props.right;
this._top = props.top;
Expand All @@ -33,6 +38,14 @@ export class PopupStyle {
}
}

public get flexFlow(): string {
return this._horizontal ? 'row' : 'column';
}

public get isDividerHorizontal(): boolean {
return !this._horizontal;
}

public get leftPx(): string {
return PopupStyle.getStringWithPx(this._left);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*!
* Copyright 2024 Cognite AS
*/

import { type AxisGizmoTool } from '@cognite/reveal/tools';
import { type BaseCommand } from '../commands/BaseCommand';
import { PopupStyle } from '../domainObjectsHelpers/PopupStyle';
import { NavigationTool } from '../commands/NavigationTool';
import { type BaseTool } from '../commands/BaseTool';

export abstract class BaseRevealConfig {
// ==================================================
// VIRTUAL METHODS: Override these to config the viewer
// ==================================================

public createMainToolbar(): Array<BaseCommand | undefined> {
return [];
}

public createMainToolbarStyle(): PopupStyle {
return new PopupStyle({ right: 0, top: 0, horizontal: false });
}

public createAxisGizmoTool(): AxisGizmoTool | undefined {
return undefined;
}

public createDefaultTool(): BaseTool {
return new NavigationTool();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*!
* Copyright 2024 Cognite AS
*/

import { BaseRevealConfig } from './BaseRevealConfig';

export class DefaultRevealConfig extends BaseRevealConfig {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
type Cognite3DViewer,
type IFlexibleCameraManager
} from '@cognite/reveal';
import { NavigationTool } from '../commands/NavigationTool';
import {
Vector3,
AmbientLight,
Expand All @@ -26,7 +25,9 @@ import { getResizeCursor } from '../utilities/geometry/getResizeCursor';
import { VisualDomainObject } from '../domainObjects/VisualDomainObject';
import { ThreeView } from '../views/ThreeView';
import { type DomainObject } from '../domainObjects/DomainObject';
import { AxisGizmoTool } from '@cognite/reveal/tools';
import { type AxisGizmoTool } from '@cognite/reveal/tools';
import { type BaseRevealConfig } from './BaseRevealConfig';
import { DefaultRevealConfig } from './DefaultRevealConfig';

const DIRECTIONAL_LIGHT_NAME = 'DirectionalLight';

Expand All @@ -43,6 +44,7 @@ export class RevealRenderTarget {
private _cropBoxBoundingBox: Box3 | undefined;
private _cropBoxName: string | undefined = undefined;
private _axisGizmoTool: AxisGizmoTool | undefined;
private _config: BaseRevealConfig | undefined = undefined;

// ==================================================
// CONTRUCTORS
Expand All @@ -63,10 +65,7 @@ export class RevealRenderTarget {
this._viewer.on('cameraChange', this.cameraChangeHandler);
this._viewer.on('beforeSceneRendered', this.beforeSceneRenderedHandler);

const navigationTool = new NavigationTool();
navigationTool.attach(this);
this.toolController.add(navigationTool);
this.toolController.setDefaultTool(navigationTool);
this.setConfig(new DefaultRevealConfig());
}

// ==================================================
Expand All @@ -77,6 +76,10 @@ export class RevealRenderTarget {
return this._viewer;
}

public get config(): BaseRevealConfig | undefined {
return this._config;
}

public get rootDomainObject(): RootDomainObject {
return this._rootDomainObject;
}
Expand Down Expand Up @@ -129,9 +132,19 @@ export class RevealRenderTarget {
// INSTANCE METHODS
// ==================================================

public addAxisGizmo(): void {
this._axisGizmoTool = new AxisGizmoTool();
this._axisGizmoTool.connect(this._viewer);
public setConfig(config: BaseRevealConfig): void {
this._config = config;

const defaultTool = config.createDefaultTool();
defaultTool.attach(this);
this.toolController.add(defaultTool);
this.toolController.setDefaultTool(defaultTool);

const axisGizmoTool = config.createAxisGizmoTool();
if (axisGizmoTool !== undefined) {
axisGizmoTool.connect(this._viewer);
this._axisGizmoTool = axisGizmoTool;
}
}

public dispose(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class MeasureBoxDragger extends BaseDragger {
// OVERRIDES
// ==================================================

public get domainObject(): DomainObject {
public override get domainObject(): DomainObject {
return this._domainObject;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export abstract class MeasureDomainObject extends VisualDomainObject {
}

public override getPanelInfoStyle(): PopupStyle {
// bottom = 66 because the measurement toolbar is below
// bottom = 66 because the toolbar is below
return new PopupStyle({ bottom: 66, left: 0 });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class MeasureLineRenderStyle extends MeasureRenderStyle {
// INSTANCE FIELDS
// ==================================================

public lineWidth = 2;
public pipeRadius = 0.03;
public pipeRadius = 0.015;
public lineWidth = 1;
public selectedLineWidth = 2;

// ==================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

/* eslint-disable @typescript-eslint/consistent-type-imports */
import {
BufferAttribute,
BufferGeometry,
CylinderGeometry,
FrontSide,
Line,
LineBasicMaterial,
Mesh,
MeshPhongMaterial,
Quaternion,
Expand Down Expand Up @@ -73,7 +77,8 @@ export class MeasureLineView extends GroupThreeView {
// ==================================================

protected override addChildren(): void {
this.addChild(this.createCylinders());
this.addChild(this.createPipe());
this.addChild(this.createLines()); // Create a line so it can be seen from long distance
this.addLabels();
}

Expand All @@ -91,17 +96,17 @@ export class MeasureLineView extends GroupThreeView {
// INSTANCE METHODS:
// ==================================================

private createCylinders(): Mesh | undefined {
private createPipe(): Mesh | undefined {
const { domainObject, style } = this;
const radius = style.pipeRadius;
if (radius <= 0) {
return;
}
const { points } = domainObject;
const { length } = points;
if (length < 2) {
return undefined;
}
const radius = style.pipeRadius;
if (radius <= 0) {
return;
}
const geometries: CylinderGeometry[] = [];
const loopLength = domainObject.measureType === MeasureType.Polygon ? length + 1 : length;

Expand Down Expand Up @@ -138,17 +143,20 @@ export class MeasureLineView extends GroupThreeView {
return new Mesh(mergeGeometries(geometries, false), material);
}

private createLines(): Wireframe | undefined {
private createWireframe(): Wireframe | undefined {
nilscognite marked this conversation as resolved.
Show resolved Hide resolved
const { domainObject, style } = this;
const linewidth = domainObject.isSelected ? style.selectedLineWidth : style.lineWidth;
if (linewidth === 0) {
return undefined;
}
const vertices = createVertices(domainObject);
if (vertices === undefined) {
return undefined;
}
const color = domainObject.getColorByColorType(style.colorType);
const linewidth = domainObject.isSelected ? style.selectedLineWidth : style.lineWidth;
const geometry = new LineSegmentsGeometry().setPositions(vertices);
const material = new LineMaterial({
linewidth: linewidth / 50,
linewidth,
color,
resolution: new Vector2(1000, 1000),
worldUnits: true,
Expand All @@ -157,6 +165,30 @@ export class MeasureLineView extends GroupThreeView {
return new Wireframe(geometry, material);
}

private createLines(): Line | undefined {
const { domainObject, style } = this;
const vertices = createVertices(domainObject);
if (vertices === undefined) {
return undefined;
}
const color = domainObject.getColorByColorType(style.colorType);
const linewidth = domainObject.isSelected ? style.selectedLineWidth : style.lineWidth;
const geometry = createBufferGeometry(vertices);
const material = new LineBasicMaterial({
linewidth,
color,
depthTest: style.depthTest
});
return new Line(geometry, material);

function createBufferGeometry(vertices: number[]): BufferGeometry {
const verticesArray = new Float32Array(vertices);
const geometry = new BufferGeometry();
geometry.setAttribute('position', new BufferAttribute(verticesArray, 3));
return geometry;
}
}

private addLabels(): void {
const { domainObject, style } = this;
const { points } = domainObject;
Expand Down
Loading
Loading