Browse Source

测距优化

wxp
ctw 2 months ago
parent
commit
718b81263c
  1. 2
      ruoyi-ui/src/views/cesiumMap/ContextMenu.vue
  2. 3
      ruoyi-ui/src/views/cesiumMap/HoverTooltip.vue
  3. 104
      ruoyi-ui/src/views/cesiumMap/index.vue

2
ruoyi-ui/src/views/cesiumMap/ContextMenu.vue

@ -4,8 +4,6 @@
<div class="menu-item" @click="handleDelete">
<span class="menu-icon">🗑</span>
<span>删除</span>
<span>删除</span>
<!-- 111 -->
</div>
</div>

3
ruoyi-ui/src/views/cesiumMap/HoverTooltip.vue

@ -3,10 +3,9 @@
<div class="tooltip-content">
{{ content }}
</div>
<span>删除</span>
</div>
</template>
<!-- 111 -->
<script>
export default {
name: 'HoverTooltip',

104
ruoyi-ui/src/views/cesiumMap/index.vue

@ -673,21 +673,36 @@ export default {
}
// 线
if (entityData && entityData.type === 'line') {
const length = this.calculateLineLength(entityData.positions)
//
const bearingType = entityData.bearingType || 'true';
const bearing = bearingType === 'magnetic'
? this.calculateMagneticBearing(entityData.positions)
: this.calculateTrueBearing(entityData.positions);
//
this.hoverTooltip = {
visible: true,
content: `长度:${length.toFixed(2)}\n${bearingType === 'magnetic' ? '磁方位' : '真方位'}${bearing.toFixed(2)}°`,
position: {
x: movement.endPosition.x + 10,
y: movement.endPosition.y - 10
// 线
const segmentIndex = this.findClosestSegment(entityData.positions, movement.endPosition);
if (segmentIndex !== -1) {
// segmentIndex+1
let cumulativeLength = 0;
for (let i = 0; i < segmentIndex + 1; i++) {
if (i < entityData.positions.length - 1) {
cumulativeLength += Cesium.Cartesian3.distance(entityData.positions[i], entityData.positions[i + 1]);
}
}
};
//
const currentSegmentPositions = [entityData.positions[segmentIndex], entityData.positions[segmentIndex + 1]];
//
const bearingType = entityData.bearingType || 'true';
const bearing = bearingType === 'magnetic'
? this.calculateMagneticBearing(currentSegmentPositions)
: this.calculateTrueBearing(currentSegmentPositions);
//
this.hoverTooltip = {
visible: true,
content: `累计长度:${cumulativeLength.toFixed(2)}\n${bearingType === 'magnetic' ? '磁方位' : '真方位'}${bearing.toFixed(2)}°`,
position: {
x: movement.endPosition.x + 10,
y: movement.endPosition.y - 10
}
};
} else {
//
this.hoverTooltip.visible = false;
}
} else {
// 线
this.hoverTooltip.visible = false;
@ -3016,6 +3031,67 @@ export default {
this.viewer.destroy()
this.viewer = null
}
},
// 线
findClosestSegment(positions, mousePosition) {
if (!positions || positions.length < 2) {
return -1;
}
let closestDistance = Number.MAX_VALUE;
let closestSegmentIndex = -1;
// 线
for (let i = 0; i < positions.length - 1; i++) {
const startPoint = positions[i];
const endPoint = positions[i + 1];
// 线
const distance = this.distanceToSegment(mousePosition, startPoint, endPoint);
//
if (distance < closestDistance) {
closestDistance = distance;
closestSegmentIndex = i;
}
}
//
const threshold = 10; //
return closestDistance < threshold ? closestSegmentIndex : -1;
},
// 线
distanceToSegment(mousePosition, startPoint, endPoint) {
// 3D
const startScreen = this.viewer.scene.cartesianToCanvasCoordinates(startPoint);
const endScreen = this.viewer.scene.cartesianToCanvasCoordinates(endPoint);
if (!startScreen || !endScreen) {
return Number.MAX_VALUE;
}
// 线
const lineVec = [endScreen.x - startScreen.x, endScreen.y - startScreen.y];
// 线
const mouseVec = [mousePosition.x - startScreen.x, mousePosition.y - startScreen.y];
// 线
const lineLengthSquared = lineVec[0] * lineVec[0] + lineVec[1] * lineVec[1];
if (lineLengthSquared === 0) {
// 线0线
return Math.sqrt(mouseVec[0] * mouseVec[0] + mouseVec[1] * mouseVec[1]);
}
// 线t
const t = Math.max(0, Math.min(1, (mouseVec[0] * lineVec[0] + mouseVec[1] * lineVec[1]) / lineLengthSquared));
//
const projection = [startScreen.x + t * lineVec[0], startScreen.y + t * lineVec[1]];
//
const distanceVec = [mousePosition.x - projection[0], mousePosition.y - projection[1]];
return Math.sqrt(distanceVec[0] * distanceVec[0] + distanceVec[1] * distanceVec[1]);
}
}
}

Loading…
Cancel
Save