LCOV - code coverage report
Current view: top level - packages/mask-editor/src - StrokeProcessor.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 90.9 % 44 40
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { Point2D as Point } from '@comfyorg/shared-frontend-utils/geometry'
       2              : import { catmullRomSpline, resampleSegment } from './splineUtils'
       3              : 
       4           60 : export class StrokeProcessor {
       5              :   private controlPoints: Point[] = []
       6              :   private remainder: number = 0
       7              :   private spacing: number
       8              :   private isFirstPoint: boolean = true
       9              :   private hasProcessedSegment: boolean = false
      10              : 
      11              :   constructor(spacing: number) {
      12           35 :     this.spacing = spacing
      13              :   }
      14              : 
      15              :   /**
      16              :    * Adds a point to the stroke and returns any new equidistant points generated.
      17              :    * Maintain a sliding window of 4 control points for spline generation
      18              :    */
      19              :   public addPoint(point: Point): Point[] {
      20              :     // Initialize buffer with the first point
      21          382 :     if (this.isFirstPoint) {
      22           35 :       this.controlPoints.push(point) // p0: phantom start point
      23           35 :       this.controlPoints.push(point) // p1: actual start point
      24           35 :       this.isFirstPoint = false
      25           35 :       return [] // Wait for more points to form a segment
      26              :     }
      27              : 
      28          347 :     this.controlPoints.push(point)
      29              : 
      30              :     // Require 4 points for a spline segment
      31          382 :     if (this.controlPoints.length < 4) {
      32           35 :       return []
      33              :     }
      34              : 
      35              :     // Generate segment p1->p2
      36          312 :     const p0 = this.controlPoints[0]
      37          312 :     const p1 = this.controlPoints[1]
      38          312 :     const p2 = this.controlPoints[2]
      39          312 :     const p3 = this.controlPoints[3]
      40              : 
      41          312 :     const newPoints = this.processSegment(p0, p1, p2, p3)
      42              : 
      43              :     // Slide window
      44          312 :     this.controlPoints.shift()
      45              : 
      46          312 :     return newPoints
      47              :   }
      48              : 
      49              :   /**
      50              :    * End stroke and flush remaining segments
      51              :    */
      52              :   public endStroke(): Point[] {
      53           35 :     if (this.controlPoints.length < 2) {
      54              :       // Insufficient points for a segment
      55            0 :       return []
      56              :     }
      57              : 
      58              :     // Process remaining segments by duplicating the last point
      59              : 
      60           35 :     const newPoints: Point[] = []
      61              : 
      62              :     // Flush the buffer by processing the final segment
      63              : 
      64           35 :     while (this.controlPoints.length >= 3) {
      65           35 :       const p0 = this.controlPoints[0]
      66           35 :       const p1 = this.controlPoints[1]
      67           35 :       const p2 = this.controlPoints[2]
      68           35 :       const p3 = p2 // Duplicate last point as phantom end
      69              : 
      70           35 :       const points = this.processSegment(p0, p1, p2, p3)
      71           35 :       newPoints.push(...points)
      72              : 
      73           35 :       this.controlPoints.shift()
      74              :     }
      75              : 
      76              :     // Handle single point click
      77           35 :     if (!this.hasProcessedSegment && this.controlPoints.length >= 2) {
      78              :       // Process zero-length segment for single point
      79            0 :       const p = this.controlPoints[1]
      80            0 :       const points = this.processSegment(p, p, p, p)
      81            0 :       newPoints.push(...points)
      82              :     }
      83              : 
      84           35 :     return newPoints
      85              :   }
      86              : 
      87              :   private processSegment(p0: Point, p1: Point, p2: Point, p3: Point): Point[] {
      88          347 :     this.hasProcessedSegment = true
      89              :     // Generate dense points for the segment
      90          347 :     const densePoints: Point[] = []
      91              : 
      92              :     // Adaptive sampling based on segment length
      93          347 :     const dist = Math.hypot(p2.x - p1.x, p2.y - p1.y)
      94              :     // Use 1 sample per pixel, but at least 5 samples to ensure smoothness for short segments
      95              :     // and cap at a reasonable maximum if needed (though not strictly necessary with density)
      96          347 :     const samples = Math.max(5, Math.ceil(dist))
      97              : 
      98          347 :     for (let i = 0; i < samples; i++) {
      99         4588 :       const t = i / samples
     100         4588 :       densePoints.push(catmullRomSpline(p0, p1, p2, p3, t))
     101              :     }
     102              :     // Add segment end point
     103          347 :     densePoints.push(p2)
     104              : 
     105              :     // Resample points with carried-over remainder
     106          347 :     const { points, remainder } = resampleSegment(
     107              :       densePoints,
     108              :       this.spacing,
     109              :       this.remainder
     110              :     )
     111              : 
     112          347 :     this.remainder = remainder
     113          347 :     return points
     114              :   }
     115              : }
        

Generated by: LCOV version 2.0-1