Skip to content

Commit

Permalink
fixes for slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
vladyslav-tk committed Jan 6, 2025
1 parent 1ee3470 commit ebb33d8
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 87 deletions.
8 changes: 2 additions & 6 deletions src/apps/permits/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import '../../plugins/cesium/ngv-plugin-cesium-model-interact';
import '../../plugins/cesium/ngv-plugin-cesium-slicing';
import type {CesiumWidget, DataSourceCollection} from '@cesium/engine';

import {PrimitiveCollection, CustomDataSource} from '@cesium/engine';
import {PrimitiveCollection} from '@cesium/engine';
import type {ViewerInitializedDetails} from '../../plugins/cesium/ngv-plugin-cesium-widget.js';

@customElement('ngv-app-permits')
Expand All @@ -27,7 +27,6 @@ export class NgvAppPermits extends ABaseApp<IPermitsConfig> {
private uploadedModelsCollection: PrimitiveCollection =
new PrimitiveCollection();
private dataSourceCollection: DataSourceCollection;
private slicingDataSource: CustomDataSource = new CustomDataSource();

private storeOptions = {
localStoreKey: 'permits-localStoreModels',
Expand Down Expand Up @@ -79,7 +78,7 @@ export class NgvAppPermits extends ABaseApp<IPermitsConfig> {
<ngv-plugin-cesium-slicing
.viewer="${this.viewer}"
.tiles3dCollection="${this.collections.tiles3d}"
.slicingDataSource="${this.slicingDataSource}"
.dataSourceCollection="${this.dataSourceCollection}"
></ngv-plugin-cesium-slicing>
`
: ''}
Expand All @@ -92,9 +91,6 @@ export class NgvAppPermits extends ABaseApp<IPermitsConfig> {
this.viewer.scene.primitives.add(this.uploadedModelsCollection);
this.dataSourceCollection = evt.detail.dataSourceCollection;
this.collections = evt.detail.primitiveCollections;
this.dataSourceCollection
.add(this.slicingDataSource)
.catch((e) => console.error(e));
}}
></ngv-plugin-cesium-widget>
</ngv-structure-app>
Expand Down
10 changes: 10 additions & 0 deletions src/icons/rotate-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 57 additions & 29 deletions src/plugins/cesium/draw.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type {CesiumWidget, ConstantProperty} from '@cesium/engine';
import type {
CesiumWidget,
ConstantProperty,
CustomDataSource,
} from '@cesium/engine';
import {
PositionProperty,
Entity,
Expand All @@ -12,7 +16,6 @@ import {
Cartographic,
ClassificationType,
Color,
CustomDataSource,
HeightReference,
Intersections2D,
JulianDate,
Expand Down Expand Up @@ -79,9 +82,9 @@ export class CesiumDraw extends EventTarget {
private isDoubleClick = false;
private singleClickTimer: NodeJS.Timeout | null = null;
private segmentsInfo: SegmentInfo[] = [];
private julianDate = new JulianDate();
type: GeometryTypes | undefined;
julianDate = new JulianDate();
drawingDataSource = new CustomDataSource('drawing');
drawingDataSource: CustomDataSource;
minPointsStop: boolean;
moveEntity = false;
entityForEdit: Entity | undefined;
Expand All @@ -90,7 +93,11 @@ export class CesiumDraw extends EventTarget {
// todo line options?
lineClampToGround: boolean = true;

constructor(viewer: CesiumWidget, dataSource: CustomDataSource, options?: DrawOptions) {
constructor(
viewer: CesiumWidget,
dataSource: CustomDataSource,
options?: DrawOptions,
) {
super();
// todo move default values to constants
this.viewer_ = viewer;
Expand Down Expand Up @@ -208,7 +215,8 @@ export class CesiumDraw extends EventTarget {
activateEditing(): void {
if (!this.eventHandler_ || !this.entityForEdit) return;
this.eventHandler_.setInputAction(
(event: ScreenSpaceEventHandler.PositionedEvent) => this.onLeftDown_(event),
(event: ScreenSpaceEventHandler.PositionedEvent) =>
this.onLeftDown_(event),
ScreenSpaceEventType.LEFT_DOWN,
);
this.eventHandler_.setInputAction(
Expand All @@ -223,11 +231,16 @@ export class CesiumDraw extends EventTarget {
// todo
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
this.entityForEdit.position = new CallbackProperty(() => this.activePoints_[0] || position, false);
this.entityForEdit.position = new CallbackProperty(
() => this.activePoints_[0] || position,
false,
);
break;
case 'line':
positions = [
...(<Cartesian3[]>this.entityForEdit.polyline.positions.getValue(this.julianDate))
...(<Cartesian3[]>(
this.entityForEdit.polyline.positions.getValue(this.julianDate)
)),
];
this.entityForEdit.polyline.positions = new CallbackProperty(
() => this.activePoints_,
Expand All @@ -237,8 +250,9 @@ export class CesiumDraw extends EventTarget {
break;
case 'polygon':
positions = [
...(<PolygonHierarchy>this.entityForEdit.polygon.hierarchy.getValue(this.julianDate)
).positions,
...(<PolygonHierarchy>(
this.entityForEdit.polygon.hierarchy.getValue(this.julianDate)
)).positions,
];
this.entityForEdit.polygon.hierarchy = new CallbackProperty(
() => new PolygonHierarchy(this.activePoints_),
Expand All @@ -248,8 +262,9 @@ export class CesiumDraw extends EventTarget {
break;
case 'rectangle':
positions = [
...(<PolygonHierarchy>this.entityForEdit.polygon.hierarchy.getValue(this.julianDate))
.positions,
...(<PolygonHierarchy>(
this.entityForEdit.polygon.hierarchy.getValue(this.julianDate)
)).positions,
];
this.entityForEdit.polygon.hierarchy = new CallbackProperty(
() => new PolygonHierarchy(this.activePoints_),
Expand All @@ -267,7 +282,7 @@ export class CesiumDraw extends EventTarget {
);
}, false),
billboard: {
image: './images/rotate-icon.svg',
image: '../../icons/rotate-icon.svg',
disableDepthTestDistance: Number.POSITIVE_INFINITY,
heightReference: HeightReference.CLAMP_TO_GROUND,
},
Expand Down Expand Up @@ -744,15 +759,17 @@ export class CesiumDraw extends EventTarget {
prevRealSP,
this.sketchPoint_,
);
this.sketchPoints_[prevRealSPIndex + 1].position = new ConstantPositionProperty(prevVirtualPosition);
this.sketchPoints_[prevRealSPIndex + 1].position =
new ConstantPositionProperty(prevVirtualPosition);

const nextRealSPIndex = ((spLen + idx + 1) * 2) % spLen;
const nextRealSP = this.sketchPoints_[nextRealSPIndex];
const nextVirtualPosition = this.halfwayPosition_(
nextRealSP,
this.sketchPoint_,
);
this.sketchPoints_[idx * 2 + 1].position = new ConstantPositionProperty(nextVirtualPosition);
this.sketchPoints_[idx * 2 + 1].position =
new ConstantPositionProperty(nextVirtualPosition);
}
if (this.type === 'line') {
// move virtual SPs
Expand All @@ -763,19 +780,17 @@ export class CesiumDraw extends EventTarget {
prevRealSP,
this.sketchPoint_,
);
this.sketchPoints_[(idx - 1) * 2 + 1].position = new ConstantPositionProperty(
prevVirtualPosition
);
this.sketchPoints_[(idx - 1) * 2 + 1].position =
new ConstantPositionProperty(prevVirtualPosition);
}
if (idx < this.activePoints_.length - 1) {
const nextRealSP = this.sketchPoints_[(idx + 1) * 2];
const nextVirtualPosition = this.halfwayPosition_(
nextRealSP,
this.sketchPoint_,
);
this.sketchPoints_[(idx + 1) * 2 - 1].position = new ConstantPositionProperty(
nextVirtualPosition
);
this.sketchPoints_[(idx + 1) * 2 - 1].position =
new ConstantPositionProperty(nextVirtualPosition);
}
} else {
const positions = this.activePoints_;
Expand Down Expand Up @@ -890,10 +905,17 @@ export class CesiumDraw extends EventTarget {
onLeftDown_(event: ScreenSpaceEventHandler.PositionedEvent): void {
this.leftPressedPixel_ = Cartesian2.clone(event.position);
if (this.entityForEdit) {
const objects: any[] = this.viewer_.scene.drillPick(event.position, 5, 5, 5);
const objects: any[] = this.viewer_.scene.drillPick(
event.position,
5,
5,
5,
);
if (objects.length) {
const selectedPoint = <{id: Entity} | undefined>objects.find(
(obj: {id: Entity}) => !!obj.id.point || !!obj.id.billboard,
const selectedPoint = <{id: Entity} | undefined>(
objects.find(
(obj: {id: Entity}) => !!obj.id.point || !!obj.id.billboard,
)
);
if (!selectedPoint) return;
const selectedEntity = selectedPoint.id;
Expand All @@ -918,13 +940,19 @@ export class CesiumDraw extends EventTarget {
}

halfwayPosition_(
a: Entity | Cartesian3 | PositionProperty,
b: Entity | Cartesian3 | PositionProperty,
a: Entity | Cartesian3 | PositionProperty | ConstantPositionProperty,
b: Entity | Cartesian3 | PositionProperty | ConstantPositionProperty,
): Cartesian3 {
a = a instanceof Entity ? a.position : a;
b = b instanceof Entity ? b.position : b;
a = a instanceof PositionProperty ? a.getValue(this.julianDate) : a;
b = b instanceof PositionProperty ? b.getValue(this.julianDate) : b;
a =
a instanceof ConstantPositionProperty || a instanceof PositionProperty
? <Cartesian3>a.getValue(this.julianDate)
: a;
b =
b instanceof ConstantPositionProperty || b instanceof PositionProperty
? <Cartesian3>b.getValue(this.julianDate)
: b;
const position = Cartesian3.add(a, b, new Cartesian3());
Cartesian3.divideByScalar(position, 2, position);
return position;
Expand Down Expand Up @@ -1221,7 +1249,7 @@ function rectanglify(coordinates: Cartesian3[]) {

function triangulate(positions: Cartesian2[], holes: number[]): number[] {
const flattenedPositions: number[] = Cartesian2.packArray(positions);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-call

return earcut(flattenedPositions, holes, 2);
}

Expand Down
12 changes: 7 additions & 5 deletions src/plugins/cesium/interactionHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
import type {
Cesium3DTileset,
ClippingPolygonCollection,
CustomDataSource,
Globe,
Model,
PrimitiveCollection,
Scene,
} from '@cesium/engine';
import {ClippingPolygonCollection} from '@cesium/engine';
import {
ArcType,
Axis,
Expand Down Expand Up @@ -623,16 +623,18 @@ export function updateHeightForCartesianPositions(
positions: Cartesian3[],
height?: number,
scene?: Scene,
assignBack: boolean = false
assignBack: boolean = false,
): Cartesian3[] {
return positions.map(p => {
return positions.map((p) => {
const cartographicPosition = Cartographic.fromCartesian(p);
if (typeof height === 'number' && !isNaN(height))
cartographicPosition.height = height;
if (scene) {
const altitude = scene.globe.getHeight(cartographicPosition) || 0;
cartographicPosition.height += altitude;
}
return assignBack ? Cartographic.toCartesian(cartographicPosition, Ellipsoid.WGS84, p) : Cartographic.toCartesian(cartographicPosition);
return assignBack
? Cartographic.toCartesian(cartographicPosition, Ellipsoid.WGS84, p)
: Cartographic.toCartesian(cartographicPosition);
});
}
23 changes: 23 additions & 0 deletions src/plugins/cesium/localStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,26 @@ export function getStoredModels(storeKey: string): StoredModel[] {
return [];
}
}

export type StoredClipping = {
name: string;
positions: number[][];
terrainClipping: boolean;
tilesClipping: boolean;
};

const CLIPPING_KEY = 'clipping-polygons';

export function updateClippingInLocalStore(clipping: StoredClipping[]): void {
localStorage.setItem(CLIPPING_KEY, JSON.stringify(clipping));
}

export function getStoredClipping(): StoredClipping[] {
if (!localStorage.getItem(CLIPPING_KEY)) return [];
try {
return <StoredClipping[]>JSON.parse(localStorage.getItem(CLIPPING_KEY));
} catch (e) {
console.error('Not possible to parse clippings from local storage', e);
return [];
}
}
1 change: 0 additions & 1 deletion src/plugins/cesium/ngv-cesium-factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
HeadingPitchRoll,
Transforms,
Ellipsoid,
ClippingPolygon,
} from '@cesium/engine';

import type {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/cesium/ngv-plugin-cesium-model-interact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ export class NgvPluginCesiumModelInteract extends LitElement {
tilesClipping: this.chosenModel.id.tilesClipping,
},
}}"
.showDone=${true}
@clippingChange=${(evt: {detail: ClippingChangeDetail}) => {
this.chosenModel.id.terrainClipping = evt.detail.terrainClipping;
this.chosenModel.id.tilesClipping = evt.detail.tilesClipping;
Expand Down
Loading

0 comments on commit ebb33d8

Please sign in to comment.