|
| 1 | +import type { Matrix3x3, Vector3D } from '../../types.js'; |
| 2 | +import type { |
| 3 | + FoldComputationContext, |
| 4 | + FoldConstraint, |
| 5 | + FoldConstraintEvaluation, |
| 6 | + FoldConstraintState, |
| 7 | +} from './types.js'; |
| 8 | +import { computeFrozenStiffness } from './stiffness.js'; |
| 9 | +import { createCubicBarrier } from './cubicBarrier.js'; |
| 10 | + |
| 11 | +export interface ContactBarrierOptions { |
| 12 | + id?: string; |
| 13 | + stiffnessOverride?: number; |
| 14 | + maxGap?: number; |
| 15 | + direction?: Vector3D; |
| 16 | + extendedDirectionScale?: number; |
| 17 | +} |
| 18 | + |
| 19 | +const ZERO_GRADIENT: Vector3D = { x: 0, y: 0, z: 0 }; |
| 20 | +const ZERO_HESSIAN: Matrix3x3 = [ |
| 21 | + [0, 0, 0], |
| 22 | + [0, 0, 0], |
| 23 | + [0, 0, 0], |
| 24 | +]; |
| 25 | + |
| 26 | +export function createContactBarrier(options: ContactBarrierOptions = {}): FoldConstraint { |
| 27 | + const extendedScale = options.extendedDirectionScale ?? 1.25; |
| 28 | + const baseBarrier = createCubicBarrier({ |
| 29 | + id: options.id, |
| 30 | + stiffnessOverride: options.stiffnessOverride, |
| 31 | + maxGap: options.maxGap, |
| 32 | + direction: options.direction, |
| 33 | + }); |
| 34 | + |
| 35 | + return { |
| 36 | + type: 'contact-barrier', |
| 37 | + id: options.id, |
| 38 | + enabled: true, |
| 39 | + evaluate(state: FoldConstraintState, context: FoldComputationContext): FoldConstraintEvaluation { |
| 40 | + const baseDirection = options.direction ?? state.direction; |
| 41 | + const contactDirection = state.extendedDirection ?? baseDirection ?? state.direction; |
| 42 | + const stiffness = options.stiffnessOverride ?? |
| 43 | + computeFrozenStiffness( |
| 44 | + { |
| 45 | + gap: state.gap, |
| 46 | + effectiveMass: state.effectiveMass ?? 0, |
| 47 | + direction: contactDirection ?? baseDirection ?? state.direction, |
| 48 | + hessian: (state.metadata?.hessian as Matrix3x3 | undefined) ?? ZERO_HESSIAN, |
| 49 | + }, |
| 50 | + { min: 0 } |
| 51 | + ); |
| 52 | + |
| 53 | + const maxGap = options.maxGap ?? state.maxGap; |
| 54 | + const direction = contactDirection ?? baseDirection ?? state.direction; |
| 55 | + |
| 56 | + if (!direction) { |
| 57 | + return { energy: 0, gradient: ZERO_GRADIENT, hessian: ZERO_HESSIAN }; |
| 58 | + } |
| 59 | + |
| 60 | + const violation = Math.max(0, maxGap - state.gap * extendedScale); |
| 61 | + if (violation <= 0 || stiffness <= 0) { |
| 62 | + return { energy: 0, gradient: ZERO_GRADIENT, hessian: ZERO_HESSIAN }; |
| 63 | + } |
| 64 | + |
| 65 | + const unit = normalise(direction); |
| 66 | + if (!unit) { |
| 67 | + return { energy: 0, gradient: ZERO_GRADIENT, hessian: ZERO_HESSIAN }; |
| 68 | + } |
| 69 | + |
| 70 | + const cubicEvaluation = baseBarrier.evaluate( |
| 71 | + { |
| 72 | + ...state, |
| 73 | + maxGap, |
| 74 | + direction, |
| 75 | + stiffness, |
| 76 | + }, |
| 77 | + context |
| 78 | + ); |
| 79 | + |
| 80 | + return cubicEvaluation; |
| 81 | + }, |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +function normalise(vector: Vector3D): Vector3D | null { |
| 86 | + const length = Math.hypot(vector.x, vector.y, vector.z); |
| 87 | + if (length <= 0) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + return { |
| 91 | + x: vector.x / length, |
| 92 | + y: vector.y / length, |
| 93 | + z: vector.z / length, |
| 94 | + }; |
| 95 | +} |
0 commit comments