Strata - v1.4.10
    Preparing search index...

    Function simplifyPath

    • Simplifies a path using the Ramer-Douglas-Peucker algorithm.

      Removes unnecessary intermediate waypoints while preserving the path's overall shape. Essential for reducing memory usage and improving performance when paths have many redundant points, such as grid-based paths or high-resolution navigation meshes.

      Parameters

      • positions: Position3D[]

        Array of positions to simplify

      • epsilon: number = 0.1

        Maximum distance threshold for point removal (higher = more aggressive, must be >= 0)

      Returns Position3D[]

      Simplified path with fewer waypoints

      import { simplifyPath } from '@jbcom/strata/core/pathfinding';

      // Path with many redundant points
      const detailedPath = [
      { x: 0, y: 0, z: 0 },
      { x: 1, y: 0, z: 0 },
      { x: 2, y: 0, z: 0 },
      { x: 3, y: 0, z: 0 }, // All in a straight line
      { x: 4, y: 0, z: 0 },
      { x: 5, y: 0, z: 0 },
      { x: 6, y: 0, z: 1 }, // Slight deviation
      { x: 7, y: 0, z: 2 },
      ];

      const simplified = simplifyPath(detailedPath, 0.5);
      // Removes intermediate points that lie on or near the line
      console.log('Reduced from', detailedPath.length, 'to', simplified.length);
      // Precise simplification - removes only truly redundant points
      const precise = simplifyPath(path, 0.1);

      // Normal simplification - good balance
      const normal = simplifyPath(path, 0.5);

      // Aggressive simplification - major shortcuts, fewer waypoints
      const aggressive = simplifyPath(path, 2.0);

      // Maximum simplification - only key turning points
      const minimal = simplifyPath(path, 5.0);
      import { createGridGraph, createPathfinder, simplifyPath } from '@jbcom/strata/core/pathfinding';

      const grid = createGridGraph(50, 50, 1.0);
      const pathfinder = createPathfinder(grid);
      const result = pathfinder.find('0_0', '49_49');

      console.log('Original waypoints:', result.nodeCount); // ~99 waypoints

      // Simplify the grid path
      const simplified = simplifyPath(result.positions, 0.5);
      console.log('Simplified waypoints:', simplified.length); // ~10 waypoints

      // Agent can move in straight lines between simplified points
      import { simplifyPath, smoothPath } from '@jbcom/strata/core/pathfinding';

      function optimizePath(rawPath) {
      // Step 1: Aggressively simplify to reduce waypoints
      let path = simplifyPath(rawPath, 1.0);

      // Step 2: Smooth for natural movement
      path = smoothPath(path, {
      iterations: 2,
      strength: 0.25
      });

      // Step 3: Final simplification to remove points added by smoothing
      path = simplifyPath(path, 0.3);

      return path;
      }

      const optimized = optimizePath(navMeshPath);
      // Result: Smooth, efficient path with minimal waypoints
      // Use different epsilon values based on path length
      function adaptiveSimplify(path) {
      const pathLength = calculatePathLength(path);

      let epsilon;
      if (pathLength < 10) {
      epsilon = 0.1; // Short paths need precision
      } else if (pathLength < 50) {
      epsilon = 0.5; // Medium paths
      } else {
      epsilon = 2.0; // Long paths can be heavily simplified
      }

      return simplifyPath(path, epsilon);
      }
      // Show original vs simplified paths
      function debugSimplification(originalPath) {
      const simplified = simplifyPath(originalPath, 1.0);

      console.log('Simplification Results:');
      console.log(' Original points:', originalPath.length);
      console.log(' Simplified points:', simplified.length);
      console.log(' Reduction:', ((1 - simplified.length / originalPath.length) * 100).toFixed(1) + '%');

      // Render both paths with different colors for comparison
      renderPath(originalPath, 'red');
      renderPath(simplified, 'green');
      }