P3-04: add OCCT pad pocket and revolution

This commit is contained in:
2026-08-02 07:30:29 -04:00
parent 9dc5cfe3fd
commit 8d8d1b7c26
8 changed files with 189 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
import { BitByBitOCCT, OccStateEnum } from '@bitbybit-dev/occt-worker'
import type { Inputs } from '@bitbybit-dev/occt'
import type { ApplyPlacementInput, BooleanCutInput, BooleanIntersectionInput, BooleanUnionInput, CreateBoxInput, CreateConeInput, CreateCylinderInput, CreateSphereInput, GeometryCapabilities, GeometryDocumentContext, MeshAsset, ShapeHandle } from './types'
import type { ApplyPlacementInput, BooleanCutInput, BooleanIntersectionInput, BooleanUnionInput, CreateBoxInput, CreateConeInput, CreateCylinderInput, CreateSphereInput, GeometryCapabilities, GeometryDocumentContext, LinearFeatureParameters, MeshAsset, PadInput, PlanarProfile, PocketInput, Point3, RevolutionInput, ShapeHandle } from './types'
type KernelShapeReference = Inputs.OCCT.TopoDSShapePointer
type KernelMesh = Inputs.OCCT.DecomposedMeshDto
@@ -101,6 +101,64 @@ export const validateBooleanCutInput = (input: BooleanCutInput) => {
export const validateBooleanIntersectionInput = (input: BooleanIntersectionInput) => validateBooleanShapes(input, input.shapes, 2)
const samePoint = (left: Point3, right: Point3, tolerance = 1e-9) => left.every((coordinate, axis) => Math.abs(coordinate - right[axis]) <= tolerance)
const subtract = (left: Point3, right: Point3): Point3 => [left[0] - right[0], left[1] - right[1], left[2] - right[2]]
const cross = (left: Point3, right: Point3): Point3 => [left[1] * right[2] - left[2] * right[1], left[2] * right[0] - left[0] * right[2], left[0] * right[1] - left[1] * right[0]]
const dot = (left: Point3, right: Point3) => left[0] * right[0] + left[1] * right[1] + left[2] * right[2]
const magnitude = (value: Point3) => Math.hypot(value[0], value[1], value[2])
const normalizedRing = (ring: Point3[]) => ring.length > 1 && samePoint(ring[0], ring.at(-1) as Point3) ? ring.slice(0, -1) : [...ring]
const ringNormal = (ring: Point3[]): Point3 | null => {
const origin = ring[0]
for (let index = 1; index < ring.length - 1; index += 1) {
const candidate = cross(subtract(ring[index], origin), subtract(ring[index + 1], origin))
if (magnitude(candidate) > 1e-9) return candidate
}
return null
}
export const validatePlanarProfile = (profile: PlanarProfile) => {
const rings = [normalizedRing(profile.outer), ...(profile.holes ?? []).map(normalizedRing)]
for (const [ringIndex, ring] of rings.entries()) {
if (ring.length < 3) throw new RangeError(`Profile ring ${ringIndex} requires at least three distinct points.`)
ring.forEach((point, pointIndex) => {
validateVector(point, `profile ring ${ringIndex} point ${pointIndex}`)
if (samePoint(point, ring[(pointIndex + 1) % ring.length])) throw new RangeError(`Profile ring ${ringIndex} contains consecutive duplicate points.`)
})
}
const origin = rings[0][0]
const normal = ringNormal(rings[0])
if (!normal) throw new RangeError('Profile ring 0 is collinear.')
const normalLength = magnitude(normal)
for (const [ringIndex, ring] of rings.entries()) {
if (!ringNormal(ring)) throw new RangeError(`Profile ring ${ringIndex} is collinear.`)
for (const point of ring) if (Math.abs(dot(normal, subtract(point, origin))) / normalLength > 1e-7) throw new RangeError(`Profile ring ${ringIndex} is not coplanar with the outer ring.`)
}
}
const validateLinearFeature = (input: GeometryDocumentContext & LinearFeatureParameters) => {
validateDocumentContext(input)
validatePlanarProfile(input.profile)
finitePositive(input.length, 'length')
validateVector(input.direction ?? [0, 1, 0], 'direction', false)
}
export const validatePadInput = (input: PadInput) => validateLinearFeature(input)
export const validatePocketInput = (input: PocketInput) => {
validateLinearFeature(input)
validateShapeContext(input, input.base)
}
export const validateRevolutionInput = (input: RevolutionInput) => {
validateDocumentContext(input)
validatePlanarProfile(input.profile)
validateVector(input.axisOrigin ?? [0, 0, 0], 'axisOrigin')
validateVector(input.axisDirection ?? [0, 1, 0], 'axisDirection', false)
validateAngle(input.angle ?? 360, 'angle')
}
export const assertShapeHandleIntegrity = (actual: ShapeHandle, expected: ShapeHandle) => {
if (actual.id !== expected.id || actual.kernel !== expected.kernel || actual.kind !== expected.kind || actual.documentId !== expected.documentId || actual.documentVersion !== expected.documentVersion) throw new Error(`Shape handle integrity check failed: ${actual.id}`)
}
@@ -293,6 +351,35 @@ export class BitbybitGeometryRuntime {
return this.registerShape(kernelShape, input.documentId, input.documentVersion)
}
async pad(input: PadInput): Promise<ShapeHandle> {
validatePadInput(input)
const client = await this.readyClient()
const kernelShape = await this.createExtrusion(client, input)
return this.registerShape(kernelShape, input.documentId, input.documentVersion)
}
async pocket(input: PocketInput): Promise<ShapeHandle> {
validatePocketInput(input)
const base = this.resolveShape(input.base).reference
const client = await this.readyClient()
const tool = await this.createExtrusion(client, input)
const kernelShape = await client.occt.booleans.difference({ shape: base, shapes: [tool], keepEdges: false })
return this.registerShape(kernelShape, input.documentId, input.documentVersion)
}
async revolution(input: RevolutionInput): Promise<ShapeHandle> {
validateRevolutionInput(input)
const client = await this.readyClient()
const face = await this.createProfileFace(client, input.profile)
const kernelShape = await client.occtWorkerManager.genericCallToWorkerPromise('plugins.feature.revolution', {
shape: face,
axisOrigin: input.axisOrigin ?? [0, 0, 0],
axisDirection: input.axisDirection ?? [0, 1, 0],
angle: input.angle ?? 360,
}) as KernelShapeReference
return this.registerShape(kernelShape, input.documentId, input.documentVersion)
}
async mesh(shape: ShapeHandle, precision = 0.05): Promise<MeshAsset> {
finitePositive(precision, 'precision')
const entry = this.resolveShape(shape)
@@ -335,6 +422,27 @@ export class BitbybitGeometryRuntime {
return this.client
}
private async createProfileFace(client: BitByBitOCCT, profile: PlanarProfile): Promise<KernelShapeReference> {
const rings = [normalizedRing(profile.outer), ...(profile.holes ?? []).map(normalizedRing)]
const outerNormal = ringNormal(rings[0]) as Point3
const orientedRings = rings.map((ring, index) => index > 0 && dot(outerNormal, ringNormal(ring) as Point3) > 0 ? [...ring].reverse() : ring)
const wires = await Promise.all(orientedRings.map((points) => client.occt.shapes.wire.createPolygonWire({ points })))
return client.occt.shapes.face.createFaceFromWires({ shapes: wires, planar: true })
}
private async createExtrusion(client: BitByBitOCCT, input: LinearFeatureParameters): Promise<KernelShapeReference> {
const direction = input.direction ?? [0, 1, 0]
const directionLength = magnitude(direction)
const sign = input.reversed ? -1 : 1
const extrusion: Point3 = direction.map((coordinate) => coordinate / directionLength * input.length * sign) as Point3
let face = await this.createProfileFace(client, input.profile)
if (input.symmetricToPlane) {
const translation = extrusion.map((coordinate) => -coordinate / 2) as Point3
face = await client.occt.transforms.translate({ shape: face, translation })
}
return client.occt.operations.extrude({ shape: face, direction: extrusion })
}
private registerShape(reference: KernelShapeReference, documentId: string, documentVersion: number) {
const handle: ShapeHandle = {
id: `shape-${Date.now().toString(36)}-${(++this.sequence).toString(36)}`,