|
| 1 | +import type { Matrix3x3, Vector3D } from '../../types.js'; |
| 2 | +import type { |
| 3 | + FoldComputationContext, |
| 4 | + FoldConstraint, |
| 5 | + FoldConstraintEvaluation, |
| 6 | + FoldConstraintState, |
| 7 | +} from './types.js'; |
| 8 | + |
| 9 | +export interface FrictionOptions { |
| 10 | + id?: string; |
| 11 | + coefficient?: number; |
| 12 | + epsilon?: number; |
| 13 | +} |
| 14 | + |
| 15 | +const ZERO_GRADIENT: Vector3D = { x: 0, y: 0, z: 0 }; |
| 16 | +const ZERO_HESSIAN: Matrix3x3 = [ |
| 17 | + [0, 0, 0], |
| 18 | + [0, 0, 0], |
| 19 | + [0, 0, 0], |
| 20 | +]; |
| 21 | + |
| 22 | +export function createFrictionPotential(options: FrictionOptions = {}): FoldConstraint { |
| 23 | + const coefficient = options.coefficient ?? 0.5; |
| 24 | + const epsilon = options.epsilon ?? 1e-6; |
| 25 | + |
| 26 | + return { |
| 27 | + type: 'friction', |
| 28 | + id: options.id, |
| 29 | + enabled: true, |
| 30 | + evaluate(state: FoldConstraintState, context: FoldComputationContext): FoldConstraintEvaluation { |
| 31 | + void context; |
| 32 | + const contactForce = getContactForce(state.metadata?.contactForce); |
| 33 | + const tangent = getVector(state.metadata?.tangentDisplacement); |
| 34 | + |
| 35 | + if (!tangent || contactForce <= 0 || coefficient <= 0) { |
| 36 | + return { energy: 0, gradient: ZERO_GRADIENT, hessian: ZERO_HESSIAN }; |
| 37 | + } |
| 38 | + |
| 39 | + const rawMagnitude = Math.hypot(tangent.x, tangent.y, tangent.z); |
| 40 | + if (rawMagnitude <= epsilon) { |
| 41 | + return { energy: 0, gradient: ZERO_GRADIENT, hessian: ZERO_HESSIAN }; |
| 42 | + } |
| 43 | + |
| 44 | + const tangentMagnitude = rawMagnitude; |
| 45 | + const normalizedTangent = { |
| 46 | + x: tangent.x / tangentMagnitude, |
| 47 | + y: tangent.y / tangentMagnitude, |
| 48 | + z: tangent.z / tangentMagnitude, |
| 49 | + }; |
| 50 | + |
| 51 | + const stiffness = coefficient * contactForce / tangentMagnitude; |
| 52 | + const energy = 0.5 * stiffness * tangentMagnitude * tangentMagnitude; |
| 53 | + const gradient = scaleVector(normalizedTangent, stiffness * tangentMagnitude); |
| 54 | + const hessian = outerProduct(normalizedTangent, stiffness); |
| 55 | + |
| 56 | + return { energy, gradient, hessian }; |
| 57 | + }, |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +function getContactForce(value: unknown): number { |
| 62 | + return typeof value === 'number' && Number.isFinite(value) ? value : 0; |
| 63 | +} |
| 64 | + |
| 65 | +function getVector(value: unknown): Vector3D | null { |
| 66 | + if (!value) return null; |
| 67 | + const vector = value as Partial<Vector3D>; |
| 68 | + if ( |
| 69 | + typeof vector.x === 'number' && Number.isFinite(vector.x) && |
| 70 | + typeof vector.y === 'number' && Number.isFinite(vector.y) && |
| 71 | + typeof vector.z === 'number' && Number.isFinite(vector.z) |
| 72 | + ) { |
| 73 | + return { x: vector.x, y: vector.y, z: vector.z }; |
| 74 | + } |
| 75 | + return null; |
| 76 | +} |
| 77 | + |
| 78 | +function scaleVector(vector: Vector3D, scalar: number): Vector3D { |
| 79 | + return { |
| 80 | + x: vector.x * scalar, |
| 81 | + y: vector.y * scalar, |
| 82 | + z: vector.z * scalar, |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +function outerProduct(vector: Vector3D, scalar: number): Matrix3x3 { |
| 87 | + return [ |
| 88 | + [vector.x * vector.x * scalar, vector.x * vector.y * scalar, vector.x * vector.z * scalar], |
| 89 | + [vector.y * vector.x * scalar, vector.y * vector.y * scalar, vector.y * vector.z * scalar], |
| 90 | + [vector.z * vector.x * scalar, vector.z * vector.y * scalar, vector.z * vector.z * scalar], |
| 91 | + ]; |
| 92 | +} |
0 commit comments