From e2c07eaeaa1c7265e28e708b7f8e7232962036b4 Mon Sep 17 00:00:00 2001 From: ChenX Date: Thu, 12 May 2022 07:13:27 +0000 Subject: [PATCH] =?UTF-8?q?!1909=20=E4=BF=AE=E5=A4=8D:=E9=85=B7=E5=AE=B6?= =?UTF-8?q?=E4=B9=90=E5=AF=BC=E5=85=A5=E5=9B=A0=E4=B8=BA=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=82=B9=E5=AF=BC=E8=87=B4=E7=9A=84=E5=8D=8A=E5=9C=86=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __test__/ALG/__snapshots__/array.test.ts.snap | 21 ++++++++++++++++ __test__/ALG/array.test.ts | 12 +++++++++ src/Add-on/KJLImport.ts | 24 ++++-------------- src/Common/ArrayExt.ts | 25 +++++++++++++++++++ 4 files changed, 63 insertions(+), 19 deletions(-) create mode 100644 __test__/ALG/__snapshots__/array.test.ts.snap create mode 100644 __test__/ALG/array.test.ts diff --git a/__test__/ALG/__snapshots__/array.test.ts.snap b/__test__/ALG/__snapshots__/array.test.ts.snap new file mode 100644 index 000000000..501253ad9 --- /dev/null +++ b/__test__/ALG/__snapshots__/array.test.ts.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`删除多段线重复的点 1`] = ` +Array [ + 0, + 1, + 4, + 5, +] +`; + +exports[`删除多段线重复的点 2`] = ` +Array [ + Array [ + -0.1, + 2, + 3, + 0.2, + ], +] +`; diff --git a/__test__/ALG/array.test.ts b/__test__/ALG/array.test.ts new file mode 100644 index 000000000..7c45d15ae --- /dev/null +++ b/__test__/ALG/array.test.ts @@ -0,0 +1,12 @@ +import { ArrayRemoveDupSavePre } from "../../src/Common/ArrayExt"; + + +test('删除多段线重复的点', () => +{ + let pts = [0, 1, 1, 1, 1, 4, 5, 5, 5]; + let buls = [[-0.1, 0.1, 0.5, 1, 2, 3, 4, 0.1, 0.2]]; + ArrayRemoveDupSavePre(pts, buls); + + expect(pts).toMatchSnapshot(); + expect(buls).toMatchSnapshot(); +}); diff --git a/src/Add-on/KJLImport.ts b/src/Add-on/KJLImport.ts index 95f7d692f..2a4bb6b16 100644 --- a/src/Add-on/KJLImport.ts +++ b/src/Add-on/KJLImport.ts @@ -1,7 +1,7 @@ import { Intent } from "@blueprintjs/core"; import { Box3, Euler, Matrix4, Vector2, Vector3 } from "three"; import { app } from "../ApplicationServices/Application"; -import { arrayLast } from "../Common/ArrayExt"; +import { arrayLast, ArrayRemoveDupSavePre } from "../Common/ArrayExt"; import { EBoardKeyList } from "../Common/BoardKeyList"; import { safeEval } from "../Common/eval"; import { FileSystem } from "../Common/FileSystem"; @@ -883,24 +883,10 @@ function ParsePathOutlineAndHole(path: KJL_Path, edgeBandingAll?: number[], dril }); //移除重复的点 - let j = 1; - for (let i = 1, l = ptsAll.length; i < l; i++) - if (!(ptsAll[j - 1] === ptsAll[i])) - { - let jplus = j++; - ptsAll[jplus] = ptsAll[i]; - radiusAll[jplus] = radiusAll[i]; - if (edgeBandingAll) - edgeBandingAll[jplus] = edgeBandingAll[i]; - if (drillData?.drillSides) - drillData.drillSides[jplus] = drillData.drillSides[i]; - } - ptsAll.length = j; - if (edgeBandingAll) - edgeBandingAll.length = j; - if (drillData?.drillSides) - drillData.drillSides.length = j;; - radiusAll.length = j; + let arrs = [radiusAll]; + if (edgeBandingAll) arrs.push(edgeBandingAll); + if (drillData?.drillSides) arrs.push(drillData.drillSides); + ArrayRemoveDupSavePre(ptsAll, arrs); let polylines: Polyline[] = []; let edgeBandings: number[][] = []; diff --git a/src/Common/ArrayExt.ts b/src/Common/ArrayExt.ts index 9a8123a55..1e9c0dd99 100644 --- a/src/Common/ArrayExt.ts +++ b/src/Common/ArrayExt.ts @@ -91,6 +91,31 @@ export function arrayRemoveDuplicateBySort(arr: Array, checkFuction: (e1: return arr; } +//因为多段线的数据关系,我们需要保留下一段的数据 +export function ArrayRemoveDupSavePre(baseCompareArray: any[], arrs: (any[])[]) +{ + let index = 0;//set index + let pre = 0;//前一个点 + for (let next = 1; next <= baseCompareArray.length; next++) + { + if (baseCompareArray[pre] !== baseCompareArray[next]) + { + baseCompareArray[index] = baseCompareArray[pre]; + + for (let arr of arrs) + arr[index] = arr[pre]; + + index++; + } + + pre++; + } + + baseCompareArray.length = index; + for (let arr of arrs) + arr.length = index; +} + //原地更新数组,注意这个函数并不会比map快. export function arrayMap(arr: Array, mapFunc: (v: T) => T): Array {