All files / systems/animation TrigramStanceTransitions.ts

94% Statements 47/50
85% Branches 17/20
83.33% Functions 5/6
95.74% Lines 45/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399                                                                          28x                                                                                   484x 58x     426x 426x   426x 3x 3x             423x 423x 423x     423x 423x 173x 58x                   28x 28x                                         6544x                                                                                                                 411x       411x 411x   411x 2x       409x               411x 411x   411x                                       411x 411x                 411x 411x   411x                                       411x 411x   411x                                     411x                                   28x                                           1x 8x 64x 128x 128x 128x                                                                       68x 68x                                                    
/**
 * Trigram Stance Transition System
 * 
 * Handles trigram-specific animation transitions between stances with smooth blending,
 * incorporating Korean martial arts footwork and weight transfer principles.
 * 
 * **Korean Context:**
 * - **자세 전환 (Jase Jeonhwan)**: Stance transition
 * - **발놀림 (Balnollim)**: Footwork
 * - **체중 이동 (Chejung Idong)**: Weight transfer
 * 
 * Generates all 64 stance-to-stance transitions (8×8) with proper footwork,
 * weight transfer, and guard pose blending based on authentic Korean martial arts.
 * 
 * @module systems/animation/TrigramStanceTransitions
 * @category Animation System
 * @korean 팔괘자세전환
 */
 
import { TrigramStance } from "../../types/common";
import type { SkeletalAnimation } from "../../types/skeletal";
import type { StanceLaterality } from "../trigram/types";
import { getGuardPoseForStance } from "./StanceGuardPoses";
import { MartialArtsAnimationBuilder } from "./MartialArtsAnimationBuilder";
import { BoneName } from "../../types/skeletal";
import * as THREE from "three";
 
/**
 * Order of trigram stances in the stance wheel (circular arrangement).
 * 
 * **Korean**: 팔괘 순서
 * 
 * The 8 trigrams arranged in traditional order around the Bagua octagon.
 * Adjacent stances in this array are considered "adjacent" for transition purposes.
 * 
 * @korean 팔괘순서
 */
export const TRIGRAM_STANCES_ORDER: readonly TrigramStance[] = [
  TrigramStance.GEON,  // ☰ Heaven
  TrigramStance.TAE,   // ☱ Lake
  TrigramStance.LI,    // ☲ Fire
  TrigramStance.JIN,   // ☳ Thunder
  TrigramStance.SON,   // ☴ Wind
  TrigramStance.GAM,   // ☵ Water
  TrigramStance.GAN,   // ☶ Mountain
  TrigramStance.GON,   // ☷ Earth
] as const;
 
/**
 * Calculate transition duration based on stance change magnitude.
 * 
 * **Korean**: 전환 시간 계산
 * 
 * Duration is based on the distance between stances on the octagonal wheel:
 * - Same stance (laterality change only): 300ms
 * - Adjacent stances (1-2 steps apart): 200ms
 * - Medium distance (3-4 steps): 300ms
 * - Opposite stances (4 steps): 400ms
 * 
 * @param from - Starting trigram stance
 * @param to - Ending trigram stance
 * @returns Transition duration in seconds
 * 
 * @example
 * ```typescript
 * calculateTransitionDuration(TrigramStance.GEON, TrigramStance.TAE); // 0.2 (adjacent)
 * calculateTransitionDuration(TrigramStance.GEON, TrigramStance.GON); // 0.4 (opposite)
 * calculateTransitionDuration(TrigramStance.GEON, TrigramStance.GEON); // 0.3 (laterality change)
 * ```
 * 
 * @public
 * @category Trigram System
 * @korean 전환시간계산
 */
export function calculateTransitionDuration(
  from: TrigramStance,
  to: TrigramStance
): number {
  // Same stance = laterality change only
  if (from === to) {
    return 0.3; // 300ms
  }
  
  const fromIndex = TRIGRAM_STANCES_ORDER.indexOf(from);
  const toIndex = TRIGRAM_STANCES_ORDER.indexOf(to);
  
  if (fromIndex === -1 || toIndex === -1) {
    console.warn(`Invalid stance in transition: ${from} -> ${to}`);
    return 0.3; // Default to medium duration
  }
  
  // Calculate shortest distance around the circular wheel
  // For a circular array, the shortest path between two positions can wrap around.
  // Example: In an 8-element array, going from index 7 to 0 is 1 step (wrapping),
  // not 7 steps (direct). We compare both paths and take the shorter one.
  const directDistance = Math.abs(toIndex - fromIndex);
  const wrapDistance = TRIGRAM_STANCES_ORDER.length - directDistance;
  const distance = Math.min(directDistance, wrapDistance);
  
  // Map distance to duration
  Iif (distance === 0) return 0.3; // Same stance
  if (distance <= 2) return 0.2;  // Adjacent stances (1-2 steps)
  if (distance <= 3) return 0.3;  // Medium distance (3 steps)
  return 0.4; // Opposite stances (4 steps)
}
 
/**
 * Weight transfer constants for realistic knee bending during stance transitions.
 * 
 * **Korean**: 체중 이동 상수
 * 
 * @korean 체중이동상수
 */
const WEIGHT_TRANSFER_KNEE_BEND = 0.1; // Additional knee bend during weight transfer (radians)
const MAX_KNEE_FLEX_ANGLE = 0.6; // Maximum knee flex angle to prevent over-bending (radians)
 
/**
 * Helper function for blending Euler rotations between two poses.
 * 
 * **Korean**: 오일러 회전 블렌딩
 * 
 * Linearly interpolates between two Euler angles based on a blend factor.
 * 
 * @param fromEuler - Starting rotation
 * @param toEuler - Target rotation
 * @param blend - Blend factor (0.0 = all from, 1.0 = all to)
 * @returns Blended rotation as [x, y, z] tuple
 * 
 * @korean 오일러회전블렌딩
 */
function blendRotation(
  fromEuler: THREE.Euler,
  toEuler: THREE.Euler,
  blend: number
): [number, number, number] {
  return [
    fromEuler.x + (toEuler.x - fromEuler.x) * blend,
    fromEuler.y + (toEuler.y - fromEuler.y) * blend,
    fromEuler.z + (toEuler.z - fromEuler.z) * blend,
  ];
}
 
/**
 * Generate trigram stance-to-stance transition animation.
 * 
 * **Korean**: 팔괘 자세 전환 애니메이션 생성
 * 
 * Creates a complete skeletal animation transitioning from one trigram stance to another,
 * including:
 * - Footwork keyframes (weight transfer, pivot, step)
 * - Guard pose blending (arms transition smoothly)
 * - Laterality support (left/right stance)
 * - Korean martial arts principles (proper weight transfer, footwork sequencing)
 * 
 * ## Animation Phases
 * 
 * 1. **Weight Transfer (0-40% of duration)**: Shift weight off moving foot
 * 2. **Pivot/Step (40-70% of duration)**: Reposition feet to target stance width
 * 3. **Guard Blend (0-100% of duration)**: Smoothly transition guard poses
 * 4. **Settle (70-100% of duration)**: Complete transition, stabilize in new stance
 * 
 * @param from - Starting trigram stance
 * @param to - Ending trigram stance  
 * @param laterality - Laterality (applies to ending stance)
 * @returns Transition animation with footwork and guard blending
 * 
 * @example
 * ```typescript
 * // Adjacent stance transition (200ms)
 * const transition = transitionBetweenStances(
 *   TrigramStance.GEON, 
 *   TrigramStance.TAE, 
 *   "left"
 * );
 * 
 * // Opposite stance transition (400ms)
 * const longTransition = transitionBetweenStances(
 *   TrigramStance.GEON,
 *   TrigramStance.GON,
 *   "right"
 * );
 * ```
 * 
 * @public
 * @category Trigram System
 * @korean 팔괘자세전환
 */
export function transitionBetweenStances(
  from: TrigramStance,
  to: TrigramStance,
  laterality: StanceLaterality
): SkeletalAnimation {
  const duration = calculateTransitionDuration(from, to);
  
  // Get guard poses - same laterality used for both source and target stances
  // (the transition maintains laterality throughout)
  const fromGuard = getGuardPoseForStance(from, laterality);
  const toGuard = getGuardPoseForStance(to, laterality);
  
  if (!fromGuard || !toGuard) {
    throw new Error(`Guard pose not found for transition: ${from} (${laterality}) -> ${to} (${laterality})`);
  }
  
  // Build transition animation  
  const builder = MartialArtsAnimationBuilder
    .create(
      `${from}_to_${to}_${laterality}`,
      `${from}에서${to}로_${laterality === "left" ? "왼" : "오른"}`
    )
    .asStance(duration, false);
  
  // Keyframe 1: Start from original guard pose (0% - t=0)
  const startTime = 0;
  const fromHalfWidth = fromGuard.stanceWidth / 2;
  
  builder.at(startTime, "ease-out")
    .rotate(BoneName.SHOULDER_L, ...fromGuard.leftArm.shoulder.toArray() as [number, number, number])
    .rotate(BoneName.ELBOW_L, ...fromGuard.leftArm.elbow.toArray() as [number, number, number])
    .rotate(BoneName.WRIST_L, ...fromGuard.leftArm.wrist.toArray() as [number, number, number])
    .rotate(BoneName.SHOULDER_R, ...fromGuard.rightArm.shoulder.toArray() as [number, number, number])
    .rotate(BoneName.ELBOW_R, ...fromGuard.rightArm.elbow.toArray() as [number, number, number])
    .rotate(BoneName.WRIST_R, ...fromGuard.rightArm.wrist.toArray() as [number, number, number])
    .rotate(BoneName.HIP_L, ...fromGuard.leftLeg.hip.toArray() as [number, number, number])
    .rotate(BoneName.KNEE_L, ...fromGuard.leftLeg.knee.toArray() as [number, number, number])
    .rotate(BoneName.FOOT_L, ...fromGuard.leftLeg.ankle.toArray() as [number, number, number])
    .rotate(BoneName.HIP_R, ...fromGuard.rightLeg.hip.toArray() as [number, number, number])
    .rotate(BoneName.KNEE_R, ...fromGuard.rightLeg.knee.toArray() as [number, number, number])
    .rotate(BoneName.FOOT_R, ...fromGuard.rightLeg.ankle.toArray() as [number, number, number])
    .rotate(BoneName.SPINE_UPPER, ...fromGuard.torso.toArray() as [number, number, number])
    .rotate(BoneName.PELVIS, ...fromGuard.pelvis.toArray() as [number, number, number])
    .position(BoneName.FOOT_L, -fromHalfWidth, 0, 0)
    .position(BoneName.FOOT_R, fromHalfWidth, 0, 0)
    .done<typeof builder>();
  
  // Keyframe 2: Weight transfer phase (40% of duration)
  const weightTransferTime = duration * 0.4;
  builder.at(weightTransferTime, "linear")
    .rotate(BoneName.KNEE_L, Math.min(fromGuard.leftLeg.knee.x + WEIGHT_TRANSFER_KNEE_BEND, MAX_KNEE_FLEX_ANGLE), 0, 0)
    .rotate(BoneName.KNEE_R, Math.min(fromGuard.rightLeg.knee.x + WEIGHT_TRANSFER_KNEE_BEND, MAX_KNEE_FLEX_ANGLE), 0, 0)
    .rotate(BoneName.PELVIS, fromGuard.pelvis.x, fromGuard.pelvis.y + (toGuard.pelvis.y - fromGuard.pelvis.y) * 0.3, fromGuard.pelvis.z)
    .rotate(BoneName.SHOULDER_L, ...blendRotation(fromGuard.leftArm.shoulder, toGuard.leftArm.shoulder, 0.2))
    .rotate(BoneName.ELBOW_L, ...blendRotation(fromGuard.leftArm.elbow, toGuard.leftArm.elbow, 0.2))
    .done<typeof builder>();
  
  // Keyframe 3: Guard blend phase (70% of duration)
  const guardBlendTime = duration * 0.7;
  const blendedHalfWidth = fromHalfWidth + ((toGuard.stanceWidth / 2) - fromHalfWidth) * 0.7;
  
  builder.at(guardBlendTime, "ease-in-out")
    .rotate(BoneName.SHOULDER_L, ...blendRotation(fromGuard.leftArm.shoulder, toGuard.leftArm.shoulder, 0.7))
    .rotate(BoneName.ELBOW_L, ...blendRotation(fromGuard.leftArm.elbow, toGuard.leftArm.elbow, 0.7))
    .rotate(BoneName.WRIST_L, ...blendRotation(fromGuard.leftArm.wrist, toGuard.leftArm.wrist, 0.7))
    .rotate(BoneName.SHOULDER_R, ...blendRotation(fromGuard.rightArm.shoulder, toGuard.rightArm.shoulder, 0.7))
    .rotate(BoneName.ELBOW_R, ...blendRotation(fromGuard.rightArm.elbow, toGuard.rightArm.elbow, 0.7))
    .rotate(BoneName.WRIST_R, ...blendRotation(fromGuard.rightArm.wrist, toGuard.rightArm.wrist, 0.7))
    .rotate(BoneName.HIP_L, ...blendRotation(fromGuard.leftLeg.hip, toGuard.leftLeg.hip, 0.6))
    .rotate(BoneName.KNEE_L, ...blendRotation(fromGuard.leftLeg.knee, toGuard.leftLeg.knee, 0.6))
    .rotate(BoneName.FOOT_L, ...blendRotation(fromGuard.leftLeg.ankle, toGuard.leftLeg.ankle, 0.6))
    .rotate(BoneName.HIP_R, ...blendRotation(fromGuard.rightLeg.hip, toGuard.rightLeg.hip, 0.6))
    .rotate(BoneName.KNEE_R, ...blendRotation(fromGuard.rightLeg.knee, toGuard.rightLeg.knee, 0.6))
    .rotate(BoneName.FOOT_R, ...blendRotation(fromGuard.rightLeg.ankle, toGuard.rightLeg.ankle, 0.6))
    .rotate(BoneName.SPINE_UPPER, ...blendRotation(fromGuard.torso, toGuard.torso, 0.7))
    .rotate(BoneName.PELVIS, ...blendRotation(fromGuard.pelvis, toGuard.pelvis, 0.7))
    .position(BoneName.FOOT_L, -blendedHalfWidth, 0, 0)
    .position(BoneName.FOOT_R, blendedHalfWidth, 0, 0)
    .done<typeof builder>();
  
  // Keyframe 4: Complete transition to target guard pose (100% - t=duration)
  const endTime = duration;
  const toHalfWidth = toGuard.stanceWidth / 2;
  
  builder.at(endTime, "ease-in")
    .rotate(BoneName.SHOULDER_L, ...toGuard.leftArm.shoulder.toArray() as [number, number, number])
    .rotate(BoneName.ELBOW_L, ...toGuard.leftArm.elbow.toArray() as [number, number, number])
    .rotate(BoneName.WRIST_L, ...toGuard.leftArm.wrist.toArray() as [number, number, number])
    .rotate(BoneName.SHOULDER_R, ...toGuard.rightArm.shoulder.toArray() as [number, number, number])
    .rotate(BoneName.ELBOW_R, ...toGuard.rightArm.elbow.toArray() as [number, number, number])
    .rotate(BoneName.WRIST_R, ...toGuard.rightArm.wrist.toArray() as [number, number, number])
    .rotate(BoneName.HIP_L, ...toGuard.leftLeg.hip.toArray() as [number, number, number])
    .rotate(BoneName.KNEE_L, ...toGuard.leftLeg.knee.toArray() as [number, number, number])
    .rotate(BoneName.FOOT_L, ...toGuard.leftLeg.ankle.toArray() as [number, number, number])
    .rotate(BoneName.HIP_R, ...toGuard.rightLeg.hip.toArray() as [number, number, number])
    .rotate(BoneName.KNEE_R, ...toGuard.rightLeg.knee.toArray() as [number, number, number])
    .rotate(BoneName.FOOT_R, ...toGuard.rightLeg.ankle.toArray() as [number, number, number])
    .rotate(BoneName.SPINE_UPPER, ...toGuard.torso.toArray() as [number, number, number])
    .rotate(BoneName.PELVIS, ...toGuard.pelvis.toArray() as [number, number, number])
    .position(BoneName.FOOT_L, -toHalfWidth, 0, 0)
    .position(BoneName.FOOT_R, toHalfWidth, 0, 0)
    .done<typeof builder>();
  
  return builder.build();
}
 
/**
 * Stance transition matrix containing all 64 transitions (8x8).
 * 
 * **Korean**: 팔괘 전환 행렬
 * 
 * Maps from every stance to every other stance for both left and right laterality.
 * Key format: "from_to_laterality" (e.g., "geon_tae_left")
 * 
 * Total transitions: 128 (8 source × 8 target × 2 laterality)
 * - 16 same-stance transitions (laterality changes, 300ms)
 * - ~48 adjacent transitions (200ms)
 * - ~64 indirect transitions (300-400ms)
 * 
 * @korean 팔괘전환행렬
 */
export const STANCE_TRANSITIONS: Map<string, SkeletalAnimation> = new Map();
 
/**
 * Initialize the stance transition matrix.
 * 
 * **Korean**: 전환 행렬 초기화
 * 
 * Generates all 128 stance transitions (8 from × 8 to × 2 laterality) and populates the transition map.
 * 
 * **Important**: Call this during application initialization, not during module load, to avoid
 * blocking the main thread and to provide better control over when transitions are generated.
 * 
 * @example
 * ```typescript
 * // During app startup
 * initializeStanceTransitions();
 * ```
 * 
 * @korean 전환행렬초기화
 */
export function initializeStanceTransitions(): void {
  // Generate all transitions for both lateralities
  for (const from of TRIGRAM_STANCES_ORDER) {
    for (const to of TRIGRAM_STANCES_ORDER) {
      for (const laterality of ["left", "right"] as const) {
        const key = `${from}_${to}_${laterality}`;
        const transition = transitionBetweenStances(from, to, laterality);
        STANCE_TRANSITIONS.set(key, transition);
      }
    }
  }
}
 
/**
 * Get stance transition animation.
 * 
 * **Korean**: 자세 전환 가져오기
 * 
 * Retrieves the transition animation for moving from one stance to another with specific laterality.
 * 
 * @param from - Source stance
 * @param to - Target stance
 * @param laterality - Stance laterality (left/right)
 * @returns Transition animation, or undefined if not found
 * 
 * @example
 * ```typescript
 * const transition = getStanceTransition(
 *   TrigramStance.GEON, 
 *   TrigramStance.TAE,
 *   "right"
 * );
 * console.log(transition.duration); // 0.2
 * console.log(transition.keyframes.length); // 4
 * ```
 * 
 * @korean 자세전환가져오기
 */
export function getStanceTransition(
  from: TrigramStance,
  to: TrigramStance,
  laterality: StanceLaterality
): SkeletalAnimation | undefined {
  const key = `${from}_${to}_${laterality}`;
  return STANCE_TRANSITIONS.get(key);
}
 
/**
 * Ensure stance transitions have been initialized.
 * 
 * **Korean**: 전환이미초기화확인
 * 
 * This helper provides a convenient way for application startup code to lazily
 * initialize the stance transition matrix without performing any work at module
 * load time. It is safe to call multiple times; transitions will only be generated
 * on the first call when the internal map is still empty.
 * 
 * @example
 * ```typescript
 * // During app startup or before using stance transitions
 * ensureStanceTransitionsInitialized();
 * ```
 * 
 * @korean 전환이미초기화확인
 */
export function ensureStanceTransitionsInitialized(): void {
  if (STANCE_TRANSITIONS.size === 0) {
    initializeStanceTransitions();
  }
}