From a25bc77e01b4489ee0688d20ad4f7f7cdc8bced7 Mon Sep 17 00:00:00 2001
From: Zachary Zhang <zacharyzhang7@gmail.com>
Date: Wed, 31 Jul 2024 12:33:35 -0400
Subject: not working save

---
 src/client/views/GestureOverlay.tsx | 93 ++++++++++++++++++++++++++++++++-----
 1 file changed, 82 insertions(+), 11 deletions(-)

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

diff --git a/src/client/views/GestureOverlay.tsx b/src/client/views/GestureOverlay.tsx
index c2fe60e21..d439c3f48 100644
--- a/src/client/views/GestureOverlay.tsx
+++ b/src/client/views/GestureOverlay.tsx
@@ -2,7 +2,7 @@ import * as fitCurve from 'fit-curve';
 import { action, computed, makeObservable, observable, runInAction } from 'mobx';
 import { observer } from 'mobx-react';
 import * as React from 'react';
-import { returnEmptyDoclist, returnEmptyFilter, returnEmptyString, returnFalse, setupMoveUpEvents } from '../../ClientUtils';
+import { isTargetChildOf, returnEmptyDoclist, returnEmptyFilter, returnEmptyString, returnFalse, setupMoveUpEvents } from '../../ClientUtils';
 import { emptyFunction } from '../../Utils';
 import { Doc, Opt } from '../../fields/Doc';
 import { InkData, InkField, InkTool } from '../../fields/InkField';
@@ -32,7 +32,7 @@ import './GestureOverlay.scss';
 import { ObservableReactComponent } from './ObservableReactComponent';
 import { ActiveFillColor, DocumentView } from './nodes/DocumentView';
 import { CollectionFreeFormView } from './collections/collectionFreeForm';
-
+import { InkingStroke } from './InkingStroke';
 export enum ToolglassTools {
     InkToText = 'inktotext',
     IgnoreGesture = 'ignoregesture',
@@ -132,15 +132,85 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
             // SetActiveArrowEnd('none');
         }
     }
-    isScribble() {
+    isScribble(inkPoints: any) {
+        console.log(inkPoints);
         const ffView = DocumentView.allViews().find(view => view.ComponentView instanceof CollectionFreeFormView);
+        const points = this._points.map(p => ({ X: p.X, Y: p.Y }));
+        const cuspArray = this.getNumberOfCusps(inkPoints);
+        let cuspBooleanArray: boolean[] = [];
+        let docsToDelete: Doc[] = [];
+        const childDocs = (ffView?.ComponentView as CollectionFreeFormView).childDocs;
+        childDocs.map(doc => DocumentView.getDocumentView(doc, DocumentView.getDocumentView(doc))).filter(inkView => inkView?.ComponentView instanceof InkingStroke);
+        for (let doc of childDocs) {
+            console.log((DocumentView.getDocumentView(doc)?.ComponentView as InkingStroke).inkScaledData().inkData.map(point => ({ X: point.X, Y: point.Y })));
+        }
         if ((ffView?.ComponentView as CollectionFreeFormView).childDocs) {
-            console.log((ffView?.ComponentView as CollectionFreeFormView).childDocs.length);
+            if (cuspArray.length > 4) {
+                for (let i = 0; i < cuspArray.length - 2; i++) {
+                    outerLoop: for (let doc of childDocs) {
+                        const point1 = cuspArray[i];
+                        const point2 = cuspArray[i + 1];
+                        const point3 = cuspArray[i + 2];
+                        const triangleObject = { p1: { X: point1.X, Y: point1.Y }, p2: { X: point2.X, Y: point2.Y }, p3: { X: point3.X, Y: point3.Y } };
+                        //console.log(triangleObject + '     ' + (DocumentView.getDocumentView(doc)?.ComponentView as InkingStroke).inkScaledData().inkData);
+                        if (this.isAnyPointInTriangle(triangleObject, (DocumentView.getDocumentView(doc)?.ComponentView as InkingStroke).inkScaledData().inkData)) {
+                            docsToDelete.push(doc);
+                            cuspBooleanArray.push(true);
+                            break outerLoop;
+                        }
+                    }
+                    cuspBooleanArray.push(false);
+                }
+                if (this.determineIfScribble(cuspBooleanArray)) {
+                    docsToDelete.forEach(doc => {
+                        ffView?.ComponentView?.removeDocument?.(doc);
+                    });
+                }
+            }
+        }
+    }
+    determineIfScribble(cuspBooleanArray: boolean[]) {
+        const quarterArrayLength = Math.ceil((cuspBooleanArray.length - 2) * 0.25);
+        let hasObjectInFirstAndLast25 = true;
+        for (let i = 0; i < quarterArrayLength; i++) {
+            let hasObjectInFirstAndLast25 = true;
+            if (cuspBooleanArray[i] == false || cuspBooleanArray[cuspBooleanArray.length - 1 - i] == false) {
+                hasObjectInFirstAndLast25 = false;
+            }
         }
+        const trueCount = cuspBooleanArray.filter(value => value).length;
+        const percentageTrues = trueCount / cuspBooleanArray.length;
+        return percentageTrues > 0.75 || hasObjectInFirstAndLast25;
+    }
+    isRectangleOverlap(rect1: any, rect2: any): boolean {
+        const noOverlap = rect1.maxX < rect2.minX || rect1.minX > rect2.maxX || rect1.maxY < rect2.minY || rect1.minY > rect2.maxY;
+
+        return !noOverlap;
+    }
+    isPointInTriangle(pt: { X: number; Y: number }, triangle: any): boolean {
+        const area = (v1: { X: number; Y: number }, v2: { X: number; Y: number }, v3: { X: number; Y: number }) => Math.abs((v1.X * (v2.Y - v3.Y) + v2.X * (v3.Y - v1.Y) + v3.X * (v1.Y - v2.Y)) / 2.0);
+
+        const A = area(triangle.p1, triangle.p2, triangle.p3);
+
+        const A1 = area(pt, triangle.p2, triangle.p3);
+        const A2 = area(triangle.p1, pt, triangle.p3);
+        const A3 = area(triangle.p1, triangle.p2, pt);
+
+        return A === A1 + A2 + A3;
+    }
+    isAnyPointInTriangle(triangle: any, points: any[]): boolean {
+        for (const point of points) {
+            //console.log(point.X + '     ' + point.Y);
+            //console.log(triangle);
+            if (this.isPointInTriangle(point, triangle)) {
+                console.log('im true');
+                return true;
+            }
+        }
+        return false;
     }
     @action
     onPointerUp = () => {
-        this.isScribble();
         console.log('pointer up');
         DocumentView.DownDocView = undefined;
         if (this._points.length > 1) {
@@ -157,10 +227,9 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
             else {
                 // need to decide when to turn gestures back on
                 const result = points.length > 2 && GestureUtils.GestureRecognizer.Recognize([points]);
-                console.log(points);
-                console.log(this.getNumberOfCusps(points));
+                this.isScribble(points);
                 let actionPerformed = false;
-                console.log(result);
+                //console.log(result);
                 if (Doc.UserDoc().recognizeGestures && result && result.Score > 0.7) {
                     switch (result.Name) {
                         case Gestures.Line:
@@ -211,7 +280,8 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
         this._points.length = 0;
     };
     getNumberOfCusps(points: any) {
-        let numberOfSharpCusps = 0;
+        let arrayOfPoints: any[] = [];
+        arrayOfPoints.push(points[0]);
         for (let i = 0; i < points.length - 2; i++) {
             const point1 = points[i];
             const point2 = points[i + 1];
@@ -221,10 +291,11 @@ export class GestureOverlay extends ObservableReactComponent<React.PropsWithChil
             // console.log(point3);
             // console.log(this.find_angle(point1, point2, point3));
             if (this.find_angle(point1, point2, point3) < 90) {
-                numberOfSharpCusps += 1;
+                arrayOfPoints.push(point2);
             }
         }
-        return numberOfSharpCusps;
+        arrayOfPoints.push(points[points.length - 1]);
+        return arrayOfPoints;
     }
     find_angle(A: any, B: any, C: any) {
         let AB = Math.sqrt(Math.pow(B.X - A.X, 2) + Math.pow(B.Y - A.Y, 2));
-- 
cgit v1.2.3-70-g09d2

