矩形--多边形-细分优化

This commit is contained in:
joylink_zhaoerwei 2023-06-05 15:56:44 +08:00
parent 64ab0b7d00
commit fb82a1339f
7 changed files with 202 additions and 4 deletions

View File

@ -10,6 +10,9 @@
<template v-if="drawStore.drawGraphicType === Link.Type"> <template v-if="drawStore.drawGraphicType === Link.Type">
<link-template></link-template> <link-template></link-template>
</template> </template>
<template v-if="drawStore.drawGraphicType === Rect.Type">
<rect-template></rect-template>
</template>
<template v-if="drawStore.drawGraphicType === Platform.Type"> <template v-if="drawStore.drawGraphicType === Platform.Type">
<platform-template></platform-template> <platform-template></platform-template>
</template> </template>
@ -39,6 +42,9 @@
<link-property <link-property
v-if="drawStore.selectedGraphicType === Link.Type" v-if="drawStore.selectedGraphicType === Link.Type"
></link-property> ></link-property>
<rect-property
v-if="drawStore.selectedGraphicType === Rect.Type"
></rect-property>
<platform-property <platform-property
v-if="drawStore.selectedGraphicType === Platform.Type" v-if="drawStore.selectedGraphicType === Platform.Type"
></platform-property> ></platform-property>
@ -59,15 +65,18 @@
<script setup lang="ts"> <script setup lang="ts">
import LinkTemplate from './templates/LinkTemplate.vue'; import LinkTemplate from './templates/LinkTemplate.vue';
import RectTemplate from './templates/RectTemplate.vue';
import PlatformTemplate from './templates/PlatformTemplate.vue'; import PlatformTemplate from './templates/PlatformTemplate.vue';
import StationTemplate from './templates/StationTemplate.vue'; import StationTemplate from './templates/StationTemplate.vue';
import CanvasProperty from './properties/CanvasProperty.vue'; import CanvasProperty from './properties/CanvasProperty.vue';
import LinkProperty from './properties/LinkProperty.vue'; import LinkProperty from './properties/LinkProperty.vue';
import RectProperty from './properties/RectProperty.vue';
import PlatformProperty from './properties/PlatformProperty.vue'; import PlatformProperty from './properties/PlatformProperty.vue';
import StationProperty from './properties/StationProperty.vue'; import StationProperty from './properties/StationProperty.vue';
import TrainProperty from './properties/TrainProperty.vue'; import TrainProperty from './properties/TrainProperty.vue';
import IscsFanProperty from './properties/IscsFanProperty.vue'; import IscsFanProperty from './properties/IscsFanProperty.vue';
import { Link } from 'src/graphics/link/Link'; import { Link } from 'src/graphics/link/Link';
import { Rect } from 'src/graphics/rect/Rect';
import { Platform } from 'src/graphics/platform/Platform'; import { Platform } from 'src/graphics/platform/Platform';
import { Station } from 'src/graphics/station/Station'; import { Station } from 'src/graphics/station/Station';
import { Train } from 'src/graphics/train/Train'; import { Train } from 'src/graphics/train/Train';

View File

@ -0,0 +1,74 @@
<template>
<q-form>
<q-input outlined readonly v-model="stationModel.id" label="id" hint="" />
<q-input
outlined
v-model.number="stationModel.lineWidth"
type="number"
@blur="onUpdate"
label="线宽"
lazy-rules
:rules="[(val) => (val && val > 0) || '画布宽必须大于0']"
/>
<q-input
outlined
v-model="stationModel.lineColor"
@blur="onUpdate"
label="线色"
lazy-rules
:rules="[(val) => (val && val.length > 0) || '线色不能为空']"
>
<template v-slot:append>
<q-icon name="colorize" class="cursor-pointer">
<q-popup-proxy cover transition-show="scale" transition-hide="scale">
<q-color
v-model="stationModel.lineColor"
@change="
(val) => {
stationModel.lineColor = val;
onUpdate();
}
"
/>
</q-popup-proxy>
</q-icon>
</template>
</q-input>
</q-form>
</template>
<script setup lang="ts">
import { RectData } from 'src/examples/app/graphics/RectInteraction';
import { Rect } from 'src/graphics/rect/Rect';
import { useDrawStore } from 'src/stores/draw-store';
import { onMounted, reactive, watch } from 'vue';
const drawStore = useDrawStore();
const stationModel = reactive(new RectData());
drawStore.$subscribe;
watch(
() => drawStore.selectedGraphic,
(val) => {
if (val && val.type == Rect.Type) {
stationModel.copyFrom(val.saveData() as RectData);
}
}
);
onMounted(() => {
const station = drawStore.selectedGraphic as Rect;
if (station) {
stationModel.copyFrom(station.saveData());
}
});
function onUpdate() {
const station = drawStore.selectedGraphic as Rect;
if (station) {
drawStore.getDrawApp().updateGraphicAndRecord(station, stationModel);
}
}
</script>

View File

@ -0,0 +1,76 @@
<template>
<q-form>
<q-input
outlined
v-model.number="template.lineWidth"
type="number"
@blur="onUpdate"
label="线宽 *"
lazy-rules
:rules="[(val) => (val && val > 0) || '线宽必须大于0']"
/>
<q-input
outlined
v-model="template.lineColor"
@blur="onUpdate"
label="线色 *"
lazy-rules
:rules="[(val) => (val && val.length > 0) || '线色不能为空']"
>
<template v-slot:append>
<q-icon name="colorize" class="cursor-pointer">
<q-popup-proxy cover transition-show="scale" transition-hide="scale">
<q-color
v-model="template.lineColor"
@change="
(val) => {
template.lineColor = val;
onUpdate();
}
"
/>
</q-popup-proxy>
</q-icon>
</template>
</q-input>
</q-form>
</template>
<script setup lang="ts">
import { LinkTemplate } from 'src/graphics/link/Link';
import { useDrawStore } from 'src/stores/draw-store';
import { onMounted, reactive } from 'vue';
const drawStore = useDrawStore();
const template = reactive({
lineWidth: 1,
lineColor: '#0000ff',
curve: false,
segmentsCount: 10,
});
onMounted(() => {
const type = drawStore.drawGraphicType;
if (type) {
const gt = drawStore.drawGraphicTemplate;
if (gt) {
const lt = gt as LinkTemplate;
template.lineWidth = lt.lineWidth;
template.lineColor = lt.lineColor;
template.curve = lt.curve;
template.segmentsCount = lt.segmentsCount;
}
}
});
function onUpdate() {
const gt = drawStore.drawGraphicTemplate as LinkTemplate;
if (gt) {
gt.lineWidth = template.lineWidth;
gt.lineColor = template.lineColor;
gt.curve = template.curve;
gt.segmentsCount = template.segmentsCount;
}
}
</script>

View File

@ -48,7 +48,7 @@ export class Rect extends JlGraphic {
const r2 = new Point(r1.x + width, r1.y); const r2 = new Point(r1.x + width, r1.y);
const r3 = new Point(r1.x + width, r1.y + height); const r3 = new Point(r1.x + width, r1.y + height);
const r4 = new Point(r1.x, r1.y + height); const r4 = new Point(r1.x, r1.y + height);
this.datas.points = [r1, r2, r3, r4]; this.datas.points = [r1, r2, r3, r4, r1];
} }
const rectGraphic = this.rectGraphic; const rectGraphic = this.rectGraphic;
rectGraphic.clear(); rectGraphic.clear();

View File

@ -28,7 +28,7 @@ import {
addPolygonSegmentingPoint, addPolygonSegmentingPoint,
clearWayPoint, clearWayPoint,
clearWaypointsConfig, clearWaypointsConfig,
getWaypointRangeIndex, getWayLineIndex,
} from 'src/jlgraphic/plugins/GraphicEditPlugin'; } from 'src/jlgraphic/plugins/GraphicEditPlugin';
import { ContextMenu } from 'src/jlgraphic/ui/ContextMenu'; import { ContextMenu } from 'src/jlgraphic/ui/ContextMenu';
@ -240,7 +240,7 @@ export class RectPointsEditPlugin extends GraphicInteractionPlugin<Rect> {
addWaySegmentingConfig.handler = () => { addWaySegmentingConfig.handler = () => {
const linePoints = rect.linePoints; const linePoints = rect.linePoints;
const p = rect.screenToLocalPoint(e.global); const p = rect.screenToLocalPoint(e.global);
const { start, end } = getWaypointRangeIndex(linePoints, false, p); const { start, end } = getWayLineIndex(linePoints, p);
addPolygonSegmentingPoint(rect, start, end); addPolygonSegmentingPoint(rect, start, end);
}; };
clearWaypointsConfig.handler = () => { clearWaypointsConfig.handler = () => {

View File

@ -42,7 +42,6 @@ export class Station extends JlGraphic {
codeGraph.style.fill = this.datas.codeColor; codeGraph.style.fill = this.datas.codeColor;
codeGraph.setVectorFontSize(this.datas.codeFontSize); codeGraph.setVectorFontSize(this.datas.codeFontSize);
codeGraph.anchor.set(0.5); codeGraph.anchor.set(0.5);
codeGraph.style.fill = this.datas.codeColor;
} }
} }

View File

@ -20,6 +20,7 @@ import {
linePoint, linePoint,
pointPolygon, pointPolygon,
calculateLineSegmentingPoint, calculateLineSegmentingPoint,
calculateDistanceFromPointToLine,
} from '../utils'; } from '../utils';
import { GraphicTransformEvent, ShiftData } from './GraphicTransformPlugin'; import { GraphicTransformEvent, ShiftData } from './GraphicTransformPlugin';
@ -107,6 +108,45 @@ export abstract class LineEditPlugin extends GraphicEditPlugin<ILineGraphic> {
abstract initEditPoints(): void; abstract initEditPoints(): void;
} }
export function getWayLineIndex(
points: IPointData[],
p: IPointData
): { start: number; end: number } {
let start = 0;
let end = 0;
let minDistance = 0;
for (let i = 1; i < points.length; i++) {
const sp = points[i - 1];
const ep = points[i];
let distance = calculateDistanceFromPointToLine(sp, ep, p);
distance = Math.round(distance * 100) / 100;
if (i == 1) {
minDistance = distance;
}
if (distance == minDistance) {
const minX = Math.min(sp.x, ep.x);
const maxX = Math.max(sp.x, ep.x);
const minY = Math.min(sp.y, ep.y);
const maxY = Math.max(sp.y, ep.y);
const point = calculateFootPointFromPointToLine(sp, ep, p);
if (
point.x >= minX &&
point.x <= maxX &&
point.y >= minY &&
point.y <= maxY
) {
start = i - 1;
}
}
if (distance < minDistance) {
minDistance = distance;
start = i - 1;
}
}
end = start + 1;
return { start, end };
}
export function getWaypointRangeIndex( export function getWaypointRangeIndex(
points: IPointData[], points: IPointData[],
curve: boolean, curve: boolean,