From 1c1347d4628510b47256b582a2d5b69cacb77e06 Mon Sep 17 00:00:00 2001 From: ChenX Date: Mon, 12 Jun 2023 15:57:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96:=E4=BB=85=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E4=BA=94=E9=87=91=E5=90=8E,=E5=A6=82=E6=9E=9C=E5=92=8C?= =?UTF-8?q?=E6=9D=BF=E4=B8=8D=E5=9C=A8=E7=9B=B8=E4=BA=A4(=E5=AE=B9?= =?UTF-8?q?=E5=B7=AE10),=E9=82=A3=E4=B9=88=E4=B8=8D=E5=86=8D=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=85=B3=E8=81=94=E4=BA=94=E9=87=91=20fix=20#I7CMSL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/Add-on/Copy.ts | 33 ++- src/Add-on/DrawDim/DimBoards.ts | 4 +- src/Common/InterfereUtil.ts | 8 +- src/DatabaseServices/3DSolid/ExtrudeHole.ts | 6 - src/DatabaseServices/3DSolid/SweepSolid.ts | 7 - src/DatabaseServices/Entity/Entity.ts | 11 + src/DatabaseServices/Entity/Extrude.ts | 6 - src/Geometry/OBB/obb.ts | 256 +------------------- 9 files changed, 50 insertions(+), 283 deletions(-) diff --git a/package.json b/package.json index 6f0160fa2..34946a6c8 100644 --- a/package.json +++ b/package.json @@ -128,7 +128,7 @@ "react-virtualized-auto-sizer": "^1.0.6", "react-window": "^1.8.7", "stats.js": "^0.17.0", - "three": "npm:three-cf@0.122.4", + "three": "npm:three-cf@0.122.5", "xaop": "^2.0.0" }, "resolutions": { diff --git a/src/Add-on/Copy.ts b/src/Add-on/Copy.ts index 949b6e4dc..613319a69 100644 --- a/src/Add-on/Copy.ts +++ b/src/Add-on/Copy.ts @@ -1,4 +1,5 @@ import { Intent } from '@blueprintjs/core'; +import { OBB } from 'three/examples/jsm/math/OBB'; import { app } from '../ApplicationServices/Application'; import { Log, LogType } from '../Common/Log'; import { DuplicateRecordCloning } from '../Common/Status'; @@ -300,28 +301,36 @@ export class Command_Copy implements Command } else if (e instanceof HardwareCompositeEntity)//五金 { + //如果仅仅复制了五金,但是没有复制五金关联的板(复合板),我们对它进行重新关联 + let oldId = GetOldObjectId(e.Id); let oldE = oldId.Object as HardwareCompositeEntity; + let newEObb: OBB; for (let id of oldE.RelevanceBoards) { if (!id || id.IsErase) continue; - let ent = id.Object; - if (ent instanceof Board)//只复制了五金 却没复制板 + let ent = id.Object as Entity; + + if (olds.has(ent)) continue;//复制了五金 也复制了板 + + //我们求obb 如果不相交,我们不再关联它 + newEObb = newEObb || e.OBB; + let entObb = ent.OBB; + entObb.halfSize.addScalar(10); + if (!entObb.intersectsOBB(newEObb)) + continue; + + //只复制了五金 却没复制板(或复合板) + if (ent instanceof Board) { - if (!olds.has(ent)) - { - e.RelevanceBoards.push(ent.Id); - ent.RelativeHardware.push(e.Id); - } + e.RelevanceBoards.push(ent.Id); + ent.RelativeHardware.push(e.Id); } else if (ent instanceof HardwareCompositeEntity) //复合板 { - if (!olds.has(ent)) - { - e.RelevanceBoards.push(ent.Id); - ent.RelevanceHardware.push(e.Id); - } + e.RelevanceBoards.push(ent.Id); + ent.RelevanceHardware.push(e.Id); } } } diff --git a/src/Add-on/DrawDim/DimBoards.ts b/src/Add-on/DrawDim/DimBoards.ts index fd2db03ed..3e59e4047 100644 --- a/src/Add-on/DrawDim/DimBoards.ts +++ b/src/Add-on/DrawDim/DimBoards.ts @@ -11,7 +11,7 @@ import { Polyline } from "../../DatabaseServices/Entity/Polyline"; import { Region } from "../../DatabaseServices/Entity/Region"; import { TemplateRecord } from "../../DatabaseServices/Template/TemplateRecord"; import { AsVector2, equaln, equalnn, isParallelTo, isPerpendicularityTo, midPoint, ZAxis } from "../../Geometry/GeUtils"; -import { OBB } from "../../Geometry/OBB/obb"; +import { GetObbFromOcsSize } from "../../Geometry/OBB/obb"; import { BoolOpeartionType } from "../../GraphicsSystem/BoolOperateUtils"; import { TestDraw } from "../test/TestUtil"; @@ -246,7 +246,7 @@ export class DimBoards let ocs = new Matrix4().makeBasis(_x, _y, _z).setPosition(c.StartPoint.setZ(-5000)) .premultiply(spaceFrontCS); - let cuObb = new OBB(ocs, new Vector3(c.Length, 20, 10000)); + let cuObb = GetObbFromOcsSize(ocs, new Vector3(c.Length, 20, 10000)); for (let br of brs) { diff --git a/src/Common/InterfereUtil.ts b/src/Common/InterfereUtil.ts index 03276f798..e23e56200 100644 --- a/src/Common/InterfereUtil.ts +++ b/src/Common/InterfereUtil.ts @@ -1,5 +1,8 @@ import { Geom3 } from "@jscad/modeling/src/geometries/types"; import { Box3, Material, Mesh } from "three"; +import { OBB } from "three/examples/jsm/math/OBB"; +import { FuzzyFactory } from "../csg/core/FuzzyFactory"; +import { CSG2Geometry2, Geometry2CSG2 } from "../csg/core/Geometry2CSG"; import { ExtrudeHole } from "../DatabaseServices/3DSolid/ExtrudeHole"; import { SweepSolid } from "../DatabaseServices/3DSolid/SweepSolid"; import { Board } from "../DatabaseServices/Entity/Board"; @@ -11,11 +14,8 @@ import { ProcessingGroupRecord } from "../DatabaseServices/ProcessingGroup/Proce import { TemplateLatticeRecord } from "../DatabaseServices/Template/ProgramTempate/TemplateLatticeRecord"; import { TemplateWineRackRecord } from "../DatabaseServices/Template/ProgramTempate/TemplateWineRackRecord"; import { BoxIsSolid } from "../Geometry/Box"; -import { OBB } from "../Geometry/OBB/obb"; -import { FuzzyFactory } from "../csg/core/FuzzyFactory"; -import { CSG2Geometry2, Geometry2CSG2 } from "../csg/core/Geometry2CSG"; -import { CSGIntersect } from "./CSGIntersect"; import { ColorMaterial } from "./ColorPalette"; +import { CSGIntersect } from "./CSGIntersect"; import { Log, LogType } from "./Log"; import { Sleep } from "./Sleep"; diff --git a/src/DatabaseServices/3DSolid/ExtrudeHole.ts b/src/DatabaseServices/3DSolid/ExtrudeHole.ts index 64ca5b1ec..2935eb107 100644 --- a/src/DatabaseServices/3DSolid/ExtrudeHole.ts +++ b/src/DatabaseServices/3DSolid/ExtrudeHole.ts @@ -22,7 +22,6 @@ import { GenUVForWorld } from "../Entity/GenUVForWorld"; import { Polyline } from "../Entity/Polyline"; import { PhysicalMaterialRecord } from "../PhysicalMaterialRecord"; import { Shape } from "../Shape"; -import { OBB } from './../../Geometry/OBB/obb'; import { Hole } from "./Hole"; @Factory @@ -451,11 +450,6 @@ export class ExtrudeHole extends Hole mesh.material = ColorMaterial.GetConceptualMaterial(this.ColorIndex); } } - get OBB(): OBB - { - let size = this.ContourCurve.BoundingBox.getSize(new Vector3).setZ(this.Height); - return new OBB(this.OCS, size.multiplyScalar(0.5)); - } ReadFile(file: CADFiler) { super.ReadFile(file); diff --git a/src/DatabaseServices/3DSolid/SweepSolid.ts b/src/DatabaseServices/3DSolid/SweepSolid.ts index c1d5b86c7..a2e1310f0 100644 --- a/src/DatabaseServices/3DSolid/SweepSolid.ts +++ b/src/DatabaseServices/3DSolid/SweepSolid.ts @@ -19,7 +19,6 @@ import { Polyline } from '../Entity/Polyline'; import { IsPointInPolyLine } from '../PointInPolyline'; import { Spline } from "../Spline"; import { FixIndex } from './../../Common/Utils'; -import { OBB } from './../../Geometry/OBB/obb'; import { PlaneExt } from './../../Geometry/Plane'; @Factory @@ -295,12 +294,6 @@ export class SweepSolid extends Entity return geom.boundingBox.clone().applyMatrix4(this._Matrix); } - get OBB(): OBB - { - let box = this.BoundingBox; - let size = box.getSize(new Vector3); - return new OBB(MoveMatrix(box.min), size.multiplyScalar(0.5)); - } GetObjectSnapPoints( snapMode: ObjectSnapMode, pickPoint: Vector3, diff --git a/src/DatabaseServices/Entity/Entity.ts b/src/DatabaseServices/Entity/Entity.ts index 1c3b88be3..78dda4d63 100644 --- a/src/DatabaseServices/Entity/Entity.ts +++ b/src/DatabaseServices/Entity/Entity.ts @@ -1,4 +1,5 @@ import { Material, Matrix3, Matrix4, MeshStandardMaterial, Object3D, Vector3 } from 'three'; +import { OBB } from 'three/examples/jsm/math/OBB'; import { iaop } from 'xaop'; import { HostApplicationServices } from '../../ApplicationServices/HostApplicationServices'; import { DisposeThreeObj, Object3DRemoveAll } from '../../Common/Dispose'; @@ -152,6 +153,16 @@ export class Entity extends CADObject */ Explode(): Entity[] { return []; } + get OBB(): OBB + { + let ocs = this.OCSNoClone; + let box = this.BoundingBoxInOCS; + let center = box.getCenter(new Vector3); + center.applyMatrix4(ocs); + let size = box.getSize(new Vector3).multiplyScalar(0.5); + return new OBB(center, size, new Matrix3().setFromMatrix4(ocs)); + } + /** * 返回对象的包围框. */ diff --git a/src/DatabaseServices/Entity/Extrude.ts b/src/DatabaseServices/Entity/Extrude.ts index ab411d643..ff687c036 100644 --- a/src/DatabaseServices/Entity/Extrude.ts +++ b/src/DatabaseServices/Entity/Extrude.ts @@ -21,7 +21,6 @@ import { FastExtrudeEdgeGeometry, FastExtrudeEdgeGeometryOfShape, FastWireframe import { EdgesGeometry } from "../../Geometry/EdgeGeometry"; import { ExtrudeGeometryBuilder } from "../../Geometry/ExtrudeMeshGeomBuilder/ExtrudeEdgeGeometry2"; import { AsVector2, equaln, equalv2, equalv3, IdentityMtx4, isIntersect, isParallelTo, isPerpendicularityTo, MoveMatrix, XAxis, YAxis, ZAxis, ZeroVec } from "../../Geometry/GeUtils"; -import { OBB } from "../../Geometry/OBB/obb"; import { ScaleUV, ScaleUV2 } from "../../Geometry/UVUtils"; import { RenderType } from "../../GraphicsSystem/RenderType"; import { BlockTableRecord } from "../BlockTableRecord"; @@ -157,11 +156,6 @@ export class ExtrudeSolid extends Entity return new Box3Ext().setFromPoints([new Vector3, new Vector3(this.width, this.height, this.thickness)]); } - get OBB(): OBB - { - return new OBB(this.OCS, new Vector3(this.width, this.height, this.thickness).multiplyScalar(0.5)); - } - get GroovesAddLength() { return this.groovesAddLength; diff --git a/src/Geometry/OBB/obb.ts b/src/Geometry/OBB/obb.ts index f72173846..d9b5fe264 100644 --- a/src/Geometry/OBB/obb.ts +++ b/src/Geometry/OBB/obb.ts @@ -1,249 +1,15 @@ -import { Vector3, Matrix4 } from 'three'; -// Quote from: -// https://github.com/Mugen87/yume/blob/master/src/javascript/engine/etc/OBB.js -// 即obb.js(本项目中已存在) +import { Matrix3, Matrix4, Vector3 } from 'three'; +import { OBB } from "three/examples/jsm/math/OBB"; -// Reference material: -//https://stackoverflow.com/questions/28499800/oriented-box-intersection-in-threejs -//http://www.cnblogs.com/iamzhanglei/archive/2012/06/07/2539751.html -//https://github.com/Mugen87/yume/blob/master/src/javascript/engine/etc/OBB.js - -export class OBB +/** + * @param ocs 坐标系 + * @param size 尺寸 + * @returns OBB + */ +export function GetObbFromOcsSize(ocs: Matrix4, size: Vector3): OBB { - _EPSILON = 1e-3; - - public center: Vector3; - - constructor(public ocs: Matrix4, public halfSizes: Vector3) - { - this.center = halfSizes.clone().applyMatrix4(ocs); - } - - intersectsOBB(obb: OBB, is2D?: boolean, ucsInv?: Matrix4): boolean - { - let newCenter: Vector3; - let newObbCenter: Vector3; - let cs: Matrix4; - let obbcs: Matrix4; - if (is2D) - { - let mtx1 = new Matrix4().multiplyMatrices(ucsInv, this.ocs); - let mtx2 = new Matrix4().multiplyMatrices(ucsInv, obb.ocs); - cs = mtx1; - obbcs = mtx2; - cs.elements[14] = 0; - obbcs.elements[14] = 0; - newCenter = this.halfSizes.clone().applyMatrix4(cs); - newObbCenter = obb.halfSizes.clone().applyMatrix4(obbcs); - } - let xAxisA = new Vector3(); - let yAxisA = new Vector3(); - let zAxisA = new Vector3(); - - let xAxisB = new Vector3(); - let yAxisB = new Vector3(); - let zAxisB = new Vector3(); - - let translation = new Vector3(); - - let vector = new Vector3(); - - let axisA: Vector3[] = []; - let axisB: Vector3[] = []; - let rotationMatrix = [[], [], []]; - let rotationMatrixAbs = [[], [], []]; - - let halfSizeA: number, halfSizeB: number; - let t: number, i: number; - - // extract each axis - (cs ?? this.ocs).extractBasis(xAxisA, yAxisA, zAxisA); - (obbcs ?? obb.ocs).extractBasis(xAxisB, yAxisB, zAxisB); - - // push basis vectors into arrays, so you can access them via indices - axisA.push(xAxisA, yAxisA, zAxisA); - axisB.push(xAxisB, yAxisB, zAxisB); - - // get displacement vector - vector.subVectors(newObbCenter ?? obb.center, newCenter ?? this.center); - - // express the translation vector in the coordinate frame of the current - // OBB (this) - for (i = 0; i < 3; i++) - { - translation.setComponent(i, vector.dot(axisA[i])); - } - - // generate a rotation matrix that transforms from world space to the - // OBB's coordinate space - for (i = 0; i < 3; i++) - { - for (let j = 0; j < 3; j++) - { - rotationMatrix[i][j] = axisA[i].dot(axisB[j]); - rotationMatrixAbs[i][j] = Math.abs(rotationMatrix[i][j]) + this._EPSILON; - } - } - - // test the three major axes of this OBB - for (i = 0; i < 3; i++) - { - vector.set(rotationMatrixAbs[i][0], rotationMatrixAbs[i][1], rotationMatrixAbs[i][2]); - - halfSizeA = this.halfSizes.getComponent(i); - halfSizeB = obb.halfSizes.dot(vector); - - if (Math.abs(translation.getComponent(i)) > halfSizeA + halfSizeB) - { - return false; - } - } - - // test the three major axes of other OBB - for (i = 0; i < 3; i++) - { - vector.set(rotationMatrixAbs[0][i], rotationMatrixAbs[1][i], rotationMatrixAbs[2][i]); - - halfSizeA = this.halfSizes.dot(vector); - halfSizeB = obb.halfSizes.getComponent(i); - - vector.set(rotationMatrix[0][i], rotationMatrix[1][i], rotationMatrix[2][i]); - t = translation.dot(vector); - - if (Math.abs(t) > halfSizeA + halfSizeB) - { - return false; - } - } - - // test the 9 different cross-axes - - // A.x B.x - halfSizeA = this.halfSizes.y * rotationMatrixAbs[2][0] + this.halfSizes.z * rotationMatrixAbs[1][0]; - halfSizeB = obb.halfSizes.y * rotationMatrixAbs[0][2] + obb.halfSizes.z * rotationMatrixAbs[0][1]; - - t = translation.z * rotationMatrix[1][0] - translation.y * rotationMatrix[2][0]; - - if (Math.abs(t) > halfSizeA + halfSizeB) - { - return false; - } - - // A.x < cross> B.y - halfSizeA = this.halfSizes.y * rotationMatrixAbs[2][1] + this.halfSizes.z * rotationMatrixAbs[1][1]; - halfSizeB = obb.halfSizes.x * rotationMatrixAbs[0][2] + obb.halfSizes.z * rotationMatrixAbs[0][0]; - - t = translation.z * rotationMatrix[1][1] - translation.y * rotationMatrix[2][1]; - - if (Math.abs(t) > halfSizeA + halfSizeB) - { - return false; - } - - // A.x B.z - halfSizeA = this.halfSizes.y * rotationMatrixAbs[2][2] + this.halfSizes.z * rotationMatrixAbs[1][2]; - halfSizeB = obb.halfSizes.x * rotationMatrixAbs[0][1] + obb.halfSizes.y * rotationMatrixAbs[0][0]; - - t = translation.z * rotationMatrix[1][2] - translation.y * rotationMatrix[2][2]; - - if (Math.abs(t) > halfSizeA + halfSizeB) - { - return false; - } - - // A.y B.x - halfSizeA = this.halfSizes.x * rotationMatrixAbs[2][0] + this.halfSizes.z * rotationMatrixAbs[0][0]; - halfSizeB = obb.halfSizes.y * rotationMatrixAbs[1][2] + obb.halfSizes.z * rotationMatrixAbs[1][1]; - - t = translation.x * rotationMatrix[2][0] - translation.z * rotationMatrix[0][0]; - - if (Math.abs(t) > halfSizeA + halfSizeB) - { - return false; - } - - // A.y B.y - halfSizeA = this.halfSizes.x * rotationMatrixAbs[2][1] + this.halfSizes.z * rotationMatrixAbs[0][1]; - halfSizeB = obb.halfSizes.x * rotationMatrixAbs[1][2] + obb.halfSizes.z * rotationMatrixAbs[1][0]; - - t = translation.x * rotationMatrix[2][1] - translation.z * rotationMatrix[0][1]; - - if (Math.abs(t) > halfSizeA + halfSizeB) - { - return false; - } - - // A.y B.z - halfSizeA = this.halfSizes.x * rotationMatrixAbs[2][2] + this.halfSizes.z * rotationMatrixAbs[0][2]; - halfSizeB = obb.halfSizes.x * rotationMatrixAbs[1][1] + obb.halfSizes.y * rotationMatrixAbs[1][0]; - - t = translation.x * rotationMatrix[2][2] - translation.z * rotationMatrix[0][2]; - - if (Math.abs(t) > halfSizeA + halfSizeB) - { - return false; - } - - // A.z B.x - halfSizeA = this.halfSizes.x * rotationMatrixAbs[1][0] + this.halfSizes.y * rotationMatrixAbs[0][0]; - halfSizeB = obb.halfSizes.y * rotationMatrixAbs[2][2] + obb.halfSizes.z * rotationMatrixAbs[2][1]; - - t = translation.y * rotationMatrix[0][0] - translation.x * rotationMatrix[1][0]; - - if (Math.abs(t) > halfSizeA + halfSizeB) - { - return false; - } - - // A.z B.y - halfSizeA = this.halfSizes.x * rotationMatrixAbs[1][1] + this.halfSizes.y * rotationMatrixAbs[0][1]; - halfSizeB = obb.halfSizes.x * rotationMatrixAbs[2][2] + obb.halfSizes.z * rotationMatrixAbs[2][0]; - - t = translation.y * rotationMatrix[0][1] - translation.x * rotationMatrix[1][1]; - - if (Math.abs(t) > halfSizeA + halfSizeB) - { - return false; - } - - // A.z B.z - halfSizeA = this.halfSizes.x * rotationMatrixAbs[1][2] + this.halfSizes.y * rotationMatrixAbs[0][2]; - halfSizeB = obb.halfSizes.x * rotationMatrixAbs[2][1] + obb.halfSizes.y * rotationMatrixAbs[2][0]; - - t = translation.y * rotationMatrix[0][2] - translation.x * rotationMatrix[1][2]; - - if (Math.abs(t) > halfSizeA + halfSizeB) - { - return false; - } - - // no separating axis exists, so the two OBB don't intersect - return true; - } - - // setFromObject(obj: THREE.Mesh): OBB; - // setFromAABB(aabb: THREE.Box3): OBB; - - // setFromSphere(sphere: THREE.Shape): OBB; - - // closestPoint(point: THREE.Vector3): THREE.Vector3 - // isPointContained(point: THREE.Vector3): boolean - // isAABBContained(aabb: THREE.Box3): boolean - // isLineContained(line: THREE.Line3): boolean - // isTriangleContained(tarianlg: THREE.Triangle): boolean - // intersectsAABB(box: THREE.Box3): boolean - // intersectsSphere(sphere: THREE.Sphere): boolean - // intersectsOBB(box: OBB): boolean; - // intersectsPlane(plane: Plane): boolean - // intersectsRay(ray: Ray): boolean - // intersectRay(ray: Ray): Vector3 - // intersectSphere(sphere: Sphere): Vector3 - // size(optionalTarget: Vector3): Vector3 - - // translate(offset: Vector3): OBB - - // copy(obb: OBB): OBB - // clone(obb: OBB): OBB - + size = size.clone().multiplyScalar(0.5); + let center = size.clone().applyMatrix4(ocs); + return new OBB(center, size, new Matrix3().setFromMatrix4(ocs)); }