From 1b4665378cd93aa49388d92831ce5785b4423bbf Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Sun, 14 Apr 2024 16:19:55 -0400
Subject: fixed coordinate system transform for ink

---
 .../collectionFreeForm/CollectionFreeFormView.tsx  | 60 +++++++++++-----------
 1 file changed, 29 insertions(+), 31 deletions(-)

(limited to 'src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx')

diff --git a/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx b/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx
index 2bf4f3c1e..57868825e 100644
--- a/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx
+++ b/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx
@@ -755,13 +755,12 @@ export class CollectionFreeFormView extends CollectionSubView<Partial<collection
                     if (Doc.ActiveTool === InkTool.SegmentEraser) {
                         segments = this.segmentErase(intersect.inkView, intersect.t); // intersect.t is where the eraser intersected the ink stroke - want to remove the segment that starts at the intersection just before this t value and goes to the one just after it
                     } else if (Doc.ActiveTool === InkTool.RadiusEraser) {
-                        const coords = this.props.ScreenToLocalTransform().inverse().transformPoint(e.clientX, e.clientY);
-                        const inkCoords = intersect.inkView.ComponentView?.ptFromScreen?.({X: coords[0], Y: coords[1]}); // coordinates in ink space
+                        const inkCoords = intersect.inkView.ComponentView?.ptFromScreen?.(currPoint); // coordinates in ink space
                         if (inkCoords !== undefined) {
                             segments = this.radiusErase(intersect.inkView, intersect.t, inkCoords);
                         }
                     }
-                        // } else if (Doc.ActiveTool === InkTool.RadiusEraser) {
+                    // } else if (Doc.ActiveTool === InkTool.RadiusEraser) {
                     //     segments = undefined;
                     // }
                     segments?.forEach(segment =>
@@ -842,15 +841,15 @@ export class CollectionFreeFormView extends CollectionSubView<Partial<collection
     };
 
     @action
-    radiusErase = (ink: DocumentView, eraseT: number, inkCoords: {X: number, Y: number}): Segment[] => {
+    radiusErase = (ink: DocumentView, eraseT: number, inkCoords: { X: number; Y: number }): Segment[] => {
         const segments: Segment[] = [];
         var segment1: Segment = [];
         var segment2: Segment = [];
         const eraseRadius = ActiveInkWidth() / 2;
         const inkStroke = ink?.ComponentView as InkingStroke;
-        const { inkData } = (inkStroke).inkScaledData();
+        const { inkData } = inkStroke.inkScaledData();
 
-        const eraserInkData = inkStroke.splitByEraser(inkCoords, eraseRadius);
+        const eraserInkData = inkStroke.splitByEraser(inkCoords, eraseRadius).inkData;
 
         const tVals: number[] = []; // should be the tvals of the intersections
 
@@ -858,16 +857,16 @@ export class CollectionFreeFormView extends CollectionSubView<Partial<collection
             const eraserBezier: Bezier = InkField.Segment(eraserInkData, i);
             segment1.push(eraserBezier);
             for (var j = 0; j < inkData.length; j += 4) {
-            const inkSegment: Bezier = InkField.Segment(inkData, i);
-            // this.bintersects(inkSegment, eraserBezier).forEach((val: string | number, i: number) => {
-            //     // Converting the Bezier.js Split type to a t-value number.
-            //     const t = +val.toString().split('/')[0];
-            //     if (i % 2 === 0 && !tVals.includes(t)) tVals.push(t); // bcz: Hack!  don't know why but intersection points are doubled from bezier.js (but not identical).
-            // });
+                const inkSegment: Bezier = InkField.Segment(inkData, i);
+                // this.bintersects(inkSegment, eraserBezier).forEach((val: string | number, i: number) => {
+                //     // Converting the Bezier.js Split type to a t-value number.
+                //     const t = +val.toString().split('/')[0];
+                //     if (i % 2 === 0 && !tVals.includes(t)) tVals.push(t); // bcz: Hack!  don't know why but intersection points are doubled from bezier.js (but not identical).
+                // });
             }
         }
         // segment1.push(eraserBezier);
-    
+
         // for (var i = 0; i < inkData.length - 3; i += 4) {
         //     const currCurveT = Math.floor(i/4);
         //     const inkSegment: Bezier = InkField.Segment(inkData, i);
@@ -897,7 +896,7 @@ export class CollectionFreeFormView extends CollectionSubView<Partial<collection
             segments.push(segment2);
         }
         return segments;
-    }
+    };
 
     /**
      * Erases ink strokes by segments. Locates intersections of the current ink stroke with all other ink strokes (including itself),
@@ -909,22 +908,21 @@ export class CollectionFreeFormView extends CollectionSubView<Partial<collection
      */
     @action
     segmentErase = (ink: DocumentView, excludeT: number): Segment[] => {
-
         const segments: Segment[] = [];
         var segment1: Segment = [];
         var segment2: Segment = [];
         const { inkData } = (ink?.ComponentView as InkingStroke).inkScaledData();
         var intersections: number[] = []; // list of the ink stroke's intersections
         var segmentIndexes: number[] = []; // list of indexes of the curve's segment where each intersection occured
-        
+
         // loops through each segment and adds intersections to the list
         for (var i = 0; i < inkData.length - 3; i += 4) {
             const inkSegment: Bezier = InkField.Segment(inkData, i);
             var currIntersects = this.getInkIntersections(i, ink, inkSegment).sort();
             // get current segments intersections (if any) and add the curve index
-            currIntersects = currIntersects.map(tVal => tVal + Math.floor(i/4));
+            currIntersects = currIntersects.map(tVal => tVal + Math.floor(i / 4));
             if (currIntersects.length) {
-                intersections = [...intersections, ...currIntersects]
+                intersections = [...intersections, ...currIntersects];
                 for (var j = 0; j < currIntersects.length; j++) {
                     segmentIndexes.push(Math.floor(i / 4));
                 }
@@ -937,9 +935,7 @@ export class CollectionFreeFormView extends CollectionSubView<Partial<collection
             isClosedCurve = true;
 
             // now reduce intersections list to the actual intersections because closed curves have additional ones with itself
-            const indices = intersections
-                .map((value, index) => value > 0.0001 && value < Math.floor(inkData.length / 4) ? index : -1)
-                .filter(index => index !== -1);
+            const indices = intersections.map((value, index) => (value > 0.0001 && value < Math.floor(inkData.length / 4) ? index : -1)).filter(index => index !== -1);
 
             // Filter intersections and segmentIndexes based on validIndices
             intersections = indices.map(index => intersections[index]);
@@ -959,9 +955,11 @@ export class CollectionFreeFormView extends CollectionSubView<Partial<collection
                 // if not on the ends
                 splitSegment1 = segmentIndexes[closestTs[0]];
                 splitSegment2 = segmentIndexes[closestTs[1]];
-            } else if (closestTs[0] === -1) { // for a curve before an intersection
+            } else if (closestTs[0] === -1) {
+                // for a curve before an intersection
                 splitSegment2 = segmentIndexes[closestTs[1]];
-            } else { // for a curve after an intersection
+            } else {
+                // for a curve after an intersection
                 splitSegment1 = segmentIndexes[closestTs[0]];
             }
 
@@ -996,7 +994,8 @@ export class CollectionFreeFormView extends CollectionSubView<Partial<collection
                             segment2.push(inkSegment);
                         }
                     }
-                } else if (splitSegment1 === -1) { // case where left end is erased
+                } else if (splitSegment1 === -1) {
+                    // case where left end is erased
                     if (currCurveT === splitSegment2) {
                         segment2.push(inkSegment.split(intersections[closestTs[1]] - currCurveT, 1));
                         hasSplit = true;
@@ -1008,15 +1007,16 @@ export class CollectionFreeFormView extends CollectionSubView<Partial<collection
                             segment2.push(inkSegment);
                         }
                     }
-                } else { // case where right end is erased
+                } else {
+                    // case where right end is erased
                     if (currCurveT === segmentIndexes[0] && isClosedCurve) {
                         segment1.push(inkSegment.split(intersections[0] - currCurveT, 1));
                         hasSplit = true;
                     } else if (currCurveT === splitSegment1) {
                         segment1.push(inkSegment.split(0, intersections[closestTs[0]] - currCurveT));
                         hasSplit = false; // although this is not technically true, we set to false so the segments after don't get added
-                    // } else if (isClosedCurve && currCurveT === segmentIndexes[1]) {
-                    //     segment1.push(inkSegment.split(intersections[0] - currCurveT, 1));
+                        // } else if (isClosedCurve && currCurveT === segmentIndexes[1]) {
+                        //     segment1.push(inkSegment.split(intersections[0] - currCurveT, 1));
                     } else {
                         if ((isClosedCurve && hasSplit) || (!isClosedCurve && !hasSplit)) {
                             segment1.push(inkSegment);
@@ -1048,11 +1048,9 @@ export class CollectionFreeFormView extends CollectionSubView<Partial<collection
     getClosestTs = (tVals: number[], excludeT: number, startIndex: number, endIndex: number): number[] => {
         if (tVals[startIndex] >= excludeT) {
             return [-1, startIndex];
-        }
-        else if (tVals[endIndex] < excludeT) {
+        } else if (tVals[endIndex] < excludeT) {
             return [endIndex, -1];
-        }
-        else {
+        } else {
             const mid = Math.floor((startIndex + endIndex) / 2);
             if (excludeT >= tVals[mid]) {
                 if (mid + 1 <= endIndex && tVals[mid + 1] > excludeT) {
-- 
cgit v1.2.3-70-g09d2

