From 6a07e9995264c913632e4247d553feddc6f58671 Mon Sep 17 00:00:00 2001
From: Zachary Zhang <zacharyzhang7@gmail.com>
Date: Wed, 25 Sep 2024 22:51:29 -0400
Subject: improve performance of scribble and added bounding box delete

---
 src/client/views/GestureOverlay.tsx | 61 +++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 30 deletions(-)

(limited to 'src/client/views/GestureOverlay.tsx')

diff --git a/src/client/views/GestureOverlay.tsx b/src/client/views/GestureOverlay.tsx
index 7e396d475..26cffb18b 100644
--- a/src/client/views/GestureOverlay.tsx
+++ b/src/client/views/GestureOverlay.tsx
@@ -37,6 +37,7 @@ import {
     SetActiveInkColor,
     SetActiveInkWidth,
 } from './nodes/DocumentView';
+import { docs } from 'googleapis/build/src/apis/docs';
 export enum ToolglassTools {
     InkToText = 'inktotext',
     IgnoreGesture = 'ignoregesture',
@@ -149,7 +150,7 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
             intersectArray[i] = false;
         }
         const docsToDelete: Doc[] = [];
-        const childDocs = (ffView?.ComponentView as CollectionFreeFormView).childDocs.slice(0, -1);
+        let childDocs = (ffView?.ComponentView as CollectionFreeFormView).childDocs.slice(0, -1);
         childDocs.filter(doc => doc.type === 'ink').map(doc => DocumentView.getDocumentView(doc, DocumentView.getDocumentView(doc)));
         if ((ffView?.ComponentView as CollectionFreeFormView).childDocs) {
             //how many cusps the scribble hsa
@@ -159,9 +160,7 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
                     const { inkData: otherInkData } = otherInk?.inkScaledData() ?? { inkData: [] };
                     const otherScreenPts = otherInkData.map(point => otherInk.ptToScreen(point));
                     if (this.isRectangleOverlap(this.getExtremeCoordinates(otherScreenPts), this.getExtremeCoordinates(inkData))) {
-                        console.log('somethings in');
                         const intersects = this.doInksIntersect(inkData, otherScreenPts);
-                        console.log(intersects);
                         intersects.forEach(intersect => {
                             let percentage = '';
                             if (intersect.includes('/')) {
@@ -171,6 +170,9 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
                                 percentage = intersect;
                             }
                             intersectArray[Math.floor((percentage as unknown as number) * cuspArray.length)] = true;
+                            const docsInBoundingBox = this.docsInBoundingBox(doc, childDocs);
+                            childDocs = childDocs.filter(doc => !docsInBoundingBox.includes(doc));
+                            docsToDelete.push(...docsInBoundingBox);
                             docsToDelete.push(doc);
                         });
                     }
@@ -178,6 +180,8 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
             }
             console.log(intersectArray);
             if (intersectArray.length > 3 && this.determineIfScribble(intersectArray)) {
+                const uniqueArray = Array.from(new Set(docsToDelete));
+                console.log(uniqueArray.length);
                 console.log('is a scribble');
                 docsToDelete.forEach(doc => {
                     ffView?.ComponentView?.removeDocument?.(doc);
@@ -188,6 +192,23 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
         }
         return false;
     }
+    docsInBoundingBox(mainDoc: Doc, childDocs: Doc[]): Doc[] {
+        return childDocs.filter(
+            doc =>
+                typeof doc.x === 'number' &&
+                typeof doc.y === 'number' &&
+                typeof doc.width === 'number' &&
+                typeof doc.height === 'number' &&
+                typeof mainDoc.x === 'number' &&
+                typeof mainDoc.y === 'number' &&
+                typeof mainDoc.width === 'number' &&
+                typeof mainDoc.height === 'number' &&
+                doc.x < mainDoc.x + mainDoc.width &&
+                doc.x + doc.width > mainDoc.x &&
+                doc.y < mainDoc.y + mainDoc.height &&
+                doc.y + doc.height > mainDoc.y
+        );
+    }
     /**
      * this method determines if what the user drew is a scribble based on certain criteria.
      * @param cuspBooleanArray will take in an array of booleans tht represent what triangles in the scribble
@@ -207,7 +228,7 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
         }
         const trueCount = cuspBooleanArray.filter(value => value).length;
         const percentageTrues = trueCount / cuspBooleanArray.length;
-        return percentageTrues > 0.5 || hasObjectInFirstAndLast25;
+        return percentageTrues >= 0.5 || hasObjectInFirstAndLast25;
     }
     /**
      * determines if two rectangles are overlapping each other
@@ -231,6 +252,7 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
     // eslint-disable-next-line @typescript-eslint/no-explicit-any
     doInksIntersect(scribble: InkData, inkStroke: InkData): string[] {
         let intersectArray: string[] = [];
+        const extremeScribble = this.getExtremeCoordinates(scribble);
         const ffView = DocumentView.allViews().find(view => view.ComponentView instanceof CollectionFreeFormView);
         if (ffView && ffView.ComponentView instanceof CollectionFreeFormView) {
             for (let i = 0; i < scribble.length - 3; i += 4) {
@@ -238,7 +260,11 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
                 for (let j = 0; j < inkStroke.length - 3; j += 4) {
                     const intersectCurve: Bezier = InkField.Segment(scribble, i); // other curve
                     const eraserCurve: Bezier = InkField.Segment(inkStroke, j); // eraser curve
-                    if (ffView && ffView.ComponentView instanceof CollectionFreeFormView) {
+                    const points = eraserCurve.points.map(point => ({
+                        X: point.x,
+                        Y: point.y,
+                    }));
+                    if (ffView && ffView.ComponentView instanceof CollectionFreeFormView && this.isRectangleOverlap(extremeScribble, this.getExtremeCoordinates(points))) {
                         const result = (ffView.ComponentView as CollectionFreeFormView).bintersects(intersectCurve, eraserCurve)[0];
                         if (result !== undefined) {
                             intersectArray.push(result.toString());
@@ -249,31 +275,6 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
         }
         return intersectArray;
     }
-    /**
-     * used in doesLineIntersectTriangle, splits up the triangle into 3 lines and runs this method
-     * individually
-     * @param line1 point data where 0th index is the coordinate of beginnning of line and last index is the coordinate of end of limne
-     * @param line2 same thing
-     * @returns
-     */
-    // eslint-disable-next-line @typescript-eslint/no-explicit-any
-    doLinesIntersect(points: any, line: any): boolean {
-        const ffView = DocumentView.allViews().find(view => view.ComponentView instanceof CollectionFreeFormView);
-        if (ffView && ffView.ComponentView instanceof CollectionFreeFormView) {
-            for (let i = 0; i < points.length - 3; i += 4) {
-                // iterate over each segment of bezier curve
-                for (let j = 0; j < line.length - 3; j += 4) {
-                    const intersectCurve: Bezier = InkField.Segment(points, i); // other curve
-                    const eraserCurve: Bezier = InkField.Segment(line, j); // eraser curve
-                    if (ffView && ffView.ComponentView instanceof CollectionFreeFormView) {
-                        //(ffView.ComponentView as CollectionFreeFormView).bintersects();
-                    }
-                }
-            }
-        }
-        return false;
-    }
-
     dryInk = () => {
         const newPoints = this._points.reduce((p, pts) => {
             p.push([pts.X, pts.Y]);
-- 
cgit v1.2.3-70-g09d2

