diff options
| author | bobzel <zzzman@gmail.com> | 2023-04-05 22:44:03 -0400 |
|---|---|---|
| committer | bobzel <zzzman@gmail.com> | 2023-04-05 22:44:03 -0400 |
| commit | 9b41da1af16b982ee8ac2fc09f2f8b5d67eac9fb (patch) | |
| tree | bc3f57cd5b31fd453d272c925f6d5b728ab63bae /src/pen-gestures | |
| parent | 9dae453967183b294bf4f7444b948023a1d52d39 (diff) | |
| parent | 8f7e99641f84ad15f34ba9e4a60b664ac93d2e5d (diff) | |
Merge branch 'master' into data-visualization-view-naafi
Diffstat (limited to 'src/pen-gestures')
| -rw-r--r-- | src/pen-gestures/GestureUtils.ts | 46 | ||||
| -rw-r--r-- | src/pen-gestures/ndollar.ts | 230 |
2 files changed, 145 insertions, 131 deletions
diff --git a/src/pen-gestures/GestureUtils.ts b/src/pen-gestures/GestureUtils.ts index 65f2bf80c..41917aac9 100644 --- a/src/pen-gestures/GestureUtils.ts +++ b/src/pen-gestures/GestureUtils.ts @@ -1,40 +1,32 @@ -import { Rect } from "react-measure"; -import { PointData } from "../fields/InkField"; -import { NDollarRecognizer } from "./ndollar"; +import { Rect } from 'react-measure'; +import { PointData } from '../fields/InkField'; +import { NDollarRecognizer } from './ndollar'; export namespace GestureUtils { export class GestureEvent { - constructor( - readonly gesture: Gestures, - readonly points: PointData[], - readonly bounds: Rect, - readonly text?: any - ) { } + constructor(readonly gesture: Gestures, readonly points: PointData[], readonly bounds: Rect, readonly text?: any) {} } - export interface GestureEventDisposer { (): void; } + export interface GestureEventDisposer { + (): void; + } - export function MakeGestureTarget( - element: HTMLElement, - func: (e: Event, ge: GestureEvent) => void - ): GestureEventDisposer { + export function MakeGestureTarget(element: HTMLElement, func: (e: Event, ge: GestureEvent) => void): GestureEventDisposer { const handler = (e: Event) => func(e, (e as CustomEvent<GestureEvent>).detail); - element.addEventListener("dashOnGesture", handler); - return () => element.removeEventListener("dashOnGesture", handler); + element.addEventListener('dashOnGesture', handler); + return () => element.removeEventListener('dashOnGesture', handler); } export enum Gestures { - Box = "box", - Line = "line", - StartBracket = "startbracket", - EndBracket = "endbracket", - Stroke = "stroke", - Scribble = "scribble", - Text = "text", - Triangle = "triangle", - Circle = "circle", - Rectangle = "rectangle", + Line = 'line', + Stroke = 'stroke', + Scribble = 'scribble', + Text = 'text', + Triangle = 'triangle', + Circle = 'circle', + Rectangle = 'rectangle', + Arrow = 'arrow', } export const GestureRecognizer = new NDollarRecognizer(false); -}
\ No newline at end of file +} diff --git a/src/pen-gestures/ndollar.ts b/src/pen-gestures/ndollar.ts index ecd8df3e7..3ee9506cb 100644 --- a/src/pen-gestures/ndollar.ts +++ b/src/pen-gestures/ndollar.ts @@ -1,4 +1,4 @@ -import { GestureUtils } from "./GestureUtils"; +import { GestureUtils } from './GestureUtils'; /** * The $N Multistroke Recognizer (JavaScript version) @@ -69,20 +69,20 @@ import { GestureUtils } from "./GestureUtils"; * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. -**/ + **/ // // Point class // export class Point { - constructor(public X: number, public Y: number) { } + constructor(public X: number, public Y: number) {} } // // Rectangle class // export class Rectangle { - constructor(public X: number, public Y: number, public Width: number, public Height: number) { } + constructor(public X: number, public Y: number, public Width: number, public Height: number) {} } // @@ -113,8 +113,11 @@ export class Multistroke { public NumStrokes: number; public Unistrokes: Unistroke[]; - constructor(public Name: string, useBoundedRotationInvariance: boolean, strokes: any[]) // constructor - { + constructor( + public Name: string, + useBoundedRotationInvariance: boolean, + strokes: any[] // constructor + ) { this.NumStrokes = strokes.length; // number of individual strokes const order = new Array(strokes.length); // array of integer indices @@ -136,13 +139,13 @@ export class Multistroke { // Result class // export class Result { - constructor(public Name: string, public Score: any, public Time: any) { } + constructor(public Name: string, public Score: any, public Time: any) {} } // // NDollarRecognizer constants // -const NumMultistrokes = 7; +let NumMultistrokes = 0; const NumPoints = 96; const SquareSize = 250.0; const OneDThreshold = 0.25; // customize to desired gesture set (usually 0.20 - 0.35) @@ -152,60 +155,66 @@ const HalfDiagonal = 0.5 * Diagonal; const AngleRange = Deg2Rad(45.0); const AnglePrecision = Deg2Rad(2.0); const Phi = 0.5 * (-1.0 + Math.sqrt(5.0)); // Golden Ratio -const StartAngleIndex = (NumPoints / 8); // eighth of gesture length +const StartAngleIndex = NumPoints / 8; // eighth of gesture length const AngleSimilarityThreshold = Deg2Rad(30.0); // // NDollarRecognizer class // export class NDollarRecognizer { - public Multistrokes: Multistroke[]; + public Multistrokes: Multistroke[] = []; - /** - * @IMPORTANT - IF YOU'RE ADDING A NEW GESTURE, BE SURE TO INCREMENT THE NumMultiStrokes CONST RIGHT ABOVE THIS CLASS. - */ - constructor(useBoundedRotationInvariance: boolean) // constructor - { + constructor( + useBoundedRotationInvariance: boolean // constructor + ) { // // one predefined multistroke for each multistroke type // - this.Multistrokes = new Array(NumMultistrokes); - this.Multistrokes[0] = new Multistroke(GestureUtils.Gestures.Box, useBoundedRotationInvariance, new Array( - new Array( - new Point(30, 146), //new Point(29, 160), new Point(30, 180), new Point(31, 200), - new Point(30, 222), //new Point(50, 219), new Point(70, 225), new Point(90, 230), - new Point(106, 225), //new Point(100, 200), new Point(106, 180), new Point(110, 160), - new Point(106, 146), //new Point(80, 150), new Point(50, 146), - new Point(30, 143)) - )); - this.Multistrokes[1] = new Multistroke(GestureUtils.Gestures.Line, useBoundedRotationInvariance, new Array( - new Array(new Point(12, 347), new Point(119, 347)) - )); - this.Multistrokes[2] = new Multistroke(GestureUtils.Gestures.StartBracket, useBoundedRotationInvariance, new Array( - // new Array(new Point(145, 20), new Point(30, 21), new Point(34, 150)) - new Array(new Point(31, 25), new Point(145, 20), new Point(31, 25), new Point(34, 150)) - )); - this.Multistrokes[3] = new Multistroke(GestureUtils.Gestures.EndBracket, useBoundedRotationInvariance, new Array( - // new Array(new Point(150, 21), new Point(149, 150), new Point(26, 152)) - // new Array(new Point(150, 150), new Point(150, 0), new Point(150, 150), new Point(0, 150)) - new Array(new Point(10, 100), new Point(100, 100), new Point(150, 12), new Point(200, 103), new Point(300, 100)) - )); - this.Multistrokes[4] = new Multistroke(GestureUtils.Gestures.Triangle, useBoundedRotationInvariance, new Array( - new Array(new Point(40, 100), new Point(100, 200), new Point(140, 102), new Point(42, 100)) - )); - this.Multistrokes[5] = new Multistroke(GestureUtils.Gestures.Circle, useBoundedRotationInvariance, new Array( - new Array(new Point(200, 250), new Point(240, 230), new Point(248, 210), new Point(248, 190), new Point(240, 170), new Point(200, 150), new Point(160, 170), new Point(151, 190), new Point(151, 210), new Point(160, 230), new Point(201, 250)) - )); - this.Multistrokes[6] = new Multistroke(GestureUtils.Gestures.Rectangle, useBoundedRotationInvariance, new Array( - new Array( - new Point(30, 146), //new Point(29, 160), new Point(30, 180), new Point(31, 200), - new Point(30, 222), //new Point(50, 219), new Point(70, 225), new Point(90, 230), - new Point(106, 225), //new Point(100, 200), new Point(106, 180), new Point(110, 160), - new Point(106, 146), //new Point(80, 150), new Point(50, 146), - new Point(30, 143), - new Point(29, 220)) - )); - + this.Multistrokes.push( + new Multistroke( + GestureUtils.Gestures.Rectangle, + useBoundedRotationInvariance, + new Array( + new Array( + new Point(30, 146), //new Point(29, 160), new Point(30, 180), new Point(31, 200), + new Point(30, 222), //new Point(50, 219), new Point(70, 225), new Point(90, 230), + new Point(106, 225), //new Point(100, 200), new Point(106, 180), new Point(110, 160), + new Point(106, 146), //new Point(80, 150), new Point(50, 146), + new Point(30, 143) + ) + ) + ) + ); + this.Multistrokes.push(new Multistroke(GestureUtils.Gestures.Line, useBoundedRotationInvariance, new Array(new Array(new Point(12, 347), new Point(119, 347))))); + this.Multistrokes.push( + new Multistroke( + GestureUtils.Gestures.Triangle, // equilateral + useBoundedRotationInvariance, + new Array(new Array(new Point(40, 100), new Point(100, 200), new Point(140, 102), new Point(42, 100))) + ) + ); + this.Multistrokes.push( + new Multistroke( + GestureUtils.Gestures.Circle, + useBoundedRotationInvariance, + new Array( + new Array( + new Point(200, 250), + new Point(240, 230), + new Point(248, 210), + new Point(248, 190), + new Point(240, 170), + new Point(200, 150), + new Point(160, 170), + new Point(151, 190), + new Point(151, 210), + new Point(160, 230), + new Point(201, 250) + ) + ) + ) + ); + NumMultistrokes = this.Multistrokes.length; // NumMultistrokes flags the end of the non user-defined gstures strokes // // PREDEFINED STROKES // @@ -281,23 +290,25 @@ export class NDollarRecognizer { Recognize = (strokes: any[], useBoundedRotationInvariance: boolean = false, requireSameNoOfStrokes: boolean = false, useProtractor: boolean = true) => { const t0 = Date.now(); const points = CombineStrokes(strokes); // make one connected unistroke from the given strokes - const candidate = new Unistroke("", useBoundedRotationInvariance, points); + const candidate = new Unistroke('', useBoundedRotationInvariance, points); var u = -1; var b = +Infinity; - for (var i = 0; i < this.Multistrokes.length; i++) // for each multistroke template - { - if (!requireSameNoOfStrokes || strokes.length === this.Multistrokes[i].NumStrokes) // optional -- only attempt match when same # of component strokes - { - for (const unistroke of this.Multistrokes[i].Unistrokes) // for each unistroke within this multistroke - { - if (AngleBetweenUnitVectors(candidate.StartUnitVector, unistroke.StartUnitVector) <= AngleSimilarityThreshold) // strokes start in the same direction - { + for ( + var i = 0; + i < this.Multistrokes.length; + i++ // for each multistroke template + ) { + if (!requireSameNoOfStrokes || strokes.length === this.Multistrokes[i].NumStrokes) { + // optional -- only attempt match when same # of component strokes + for (const unistroke of this.Multistrokes[i].Unistrokes) { + // for each unistroke within this multistroke + if (AngleBetweenUnitVectors(candidate.StartUnitVector, unistroke.StartUnitVector) <= AngleSimilarityThreshold) { + // strokes start in the same direction var d; if (useProtractor) { d = OptimalCosineDistance(unistroke.Vector, candidate.Vector); // Protractor - } - else { + } else { d = DistanceAtBestAngle(candidate.Points, unistroke, -AngleRange, +AngleRange, AnglePrecision); // Golden Section Search (original $N) } if (d < b) { @@ -309,8 +320,8 @@ export class NDollarRecognizer { } } const t1 = Date.now(); - return (u === -1) ? null : new Result(this.Multistrokes[u].Name, useProtractor ? (1.0 - b) : (1.0 - b / HalfDiagonal), t1 - t0); - } + return u === -1 ? null : new Result(this.Multistrokes[u].Name, useProtractor ? 1.0 - b : 1.0 - b / HalfDiagonal, t1 - t0); + }; AddGesture = (name: string, useBoundedRotationInvariance: boolean, strokes: any[]) => { this.Multistrokes[this.Multistrokes.length] = new Multistroke(name, useBoundedRotationInvariance, strokes); @@ -321,15 +332,14 @@ export class NDollarRecognizer { } } return num; - } + }; DeleteUserGestures = () => { this.Multistrokes.length = NumMultistrokes; // clear any beyond the original set return NumMultistrokes; - } + }; } - // // Private helper functions from here on down // @@ -339,11 +349,13 @@ function HeapPermute(n: number, order: any[], /*out*/ orders: any[]) { } else { for (var i = 0; i < n; i++) { HeapPermute(n - 1, order, orders); - if (n % 2 === 1) { // swap 0, n-1 + if (n % 2 === 1) { + // swap 0, n-1 const tmp = order[0]; order[0] = order[n - 1]; order[n - 1] = tmp; - } else { // swap i, n-1 + } else { + // swap i, n-1 const tmp = order[i]; order[i] = order[n - 1]; order[n - 1] = tmp; @@ -355,15 +367,18 @@ function HeapPermute(n: number, order: any[], /*out*/ orders: any[]) { function MakeUnistrokes(strokes: any, orders: any) { const unistrokes = new Array(); // array of point arrays for (const order of orders) { - for (var b = 0; b < Math.pow(2, order.length); b++) // use b's bits for directions - { + for ( + var b = 0; + b < Math.pow(2, order.length); + b++ // use b's bits for directions + ) { const unistroke = new Array(); // array of points for (var i = 0; i < order.length; i++) { var pts; - if (((b >> i) & 1) === 1) {// is b's bit at index i on? + if (((b >> i) & 1) === 1) { + // is b's bit at index i on? pts = strokes[order[i]].slice().reverse(); // copy and reverse - } - else { + } else { pts = strokes[order[i]].slice(); // copy } for (const point of pts) { @@ -391,17 +406,17 @@ function Resample(points: any, n: any) { const newpoints = new Array(points[0]); for (var i = 1; i < points.length; i++) { const d = Distance(points[i - 1], points[i]); - if ((D + d) >= I) { + if (D + d >= I) { const qx = points[i - 1].X + ((I - D) / d) * (points[i].X - points[i - 1].X); const qy = points[i - 1].Y + ((I - D) / d) * (points[i].Y - points[i - 1].Y); const q = new Point(qx, qy); newpoints[newpoints.length] = q; // append new point 'q' points.splice(i, 0, q); // insert 'q' at position i in points s.t. 'q' will be the next i D = 0.0; - } - else D += d; + } else D += d; } - if (newpoints.length === n - 1) {// sometimes we fall a rounding-error short of adding the last point, so add it if so + if (newpoints.length === n - 1) { + // sometimes we fall a rounding-error short of adding the last point, so add it if so newpoints[newpoints.length] = new Point(points[points.length - 1].X, points[points.length - 1].Y); } return newpoints; @@ -410,8 +425,8 @@ function IndicativeAngle(points: any) { const c = Centroid(points); return Math.atan2(c.Y - points[0].Y, c.X - points[0].X); } -function RotateBy(points: any, radians: any) // rotates points around centroid -{ +function RotateBy(points: any, radians: any) { + // rotates points around centroid const c = Centroid(points); const cos = Math.cos(radians); const sin = Math.sin(radians); @@ -423,8 +438,8 @@ function RotateBy(points: any, radians: any) // rotates points around centroid } return newpoints; } -function ScaleDimTo(points: any, size: any, ratio1D: any) // scales bbox uniformly for 1D, non-uniformly for 2D -{ +function ScaleDimTo(points: any, size: any, ratio1D: any) { + // scales bbox uniformly for 1D, non-uniformly for 2D const B = BoundingBox(points); const uniformly = Math.min(B.Width / B.Height, B.Height / B.Width) <= ratio1D; // 1D or 2D gesture test const newpoints = new Array(); @@ -435,8 +450,8 @@ function ScaleDimTo(points: any, size: any, ratio1D: any) // scales bbox uniform } return newpoints; } -function TranslateTo(points: any, pt: any) // translates points' centroid -{ +function TranslateTo(points: any, pt: any) { + // translates points' centroid const c = Centroid(points); const newpoints = new Array(); for (const { X, Y } of points) { @@ -446,8 +461,8 @@ function TranslateTo(points: any, pt: any) // translates points' centroid } return newpoints; } -function Vectorize(points: any, useBoundedRotationInvariance: any) // for Protractor -{ +function Vectorize(points: any, useBoundedRotationInvariance: any) { + // for Protractor var cos = 1.0; var sin = 0.0; if (useBoundedRotationInvariance) { @@ -471,8 +486,8 @@ function Vectorize(points: any, useBoundedRotationInvariance: any) // for Protra } return vector; } -function OptimalCosineDistance(v1: any, v2: any) // for Protractor -{ +function OptimalCosineDistance(v1: any, v2: any) { + // for Protractor var a = 0.0; var b = 0.0; for (var i = 0; i < v1.length; i += 2) { @@ -509,7 +524,8 @@ function DistanceAtAngle(points: any, T: any, radians: any) { return PathDistance(newpoints, T.Points); } function Centroid(points: any) { - var x = 0.0, y = 0.0; + var x = 0.0, + y = 0.0; for (const point of points) { x += point.X; y += point.Y; @@ -519,7 +535,10 @@ function Centroid(points: any) { return new Point(x, y); } function BoundingBox(points: any) { - var minX = +Infinity, maxX = -Infinity, minY = +Infinity, maxY = -Infinity; + var minX = +Infinity, + maxX = -Infinity, + minY = +Infinity, + maxY = -Infinity; for (const { X, Y } of points) { minX = Math.min(minX, X); minY = Math.min(minY, Y); @@ -528,38 +547,41 @@ function BoundingBox(points: any) { } return new Rectangle(minX, minY, maxX - minX, maxY - minY); } -function PathDistance(pts1: any, pts2: any) // average distance between corresponding points in two paths -{ +function PathDistance(pts1: any, pts2: any) { + // average distance between corresponding points in two paths var d = 0.0; - for (var i = 0; i < pts1.length; i++) {// assumes pts1.length == pts2.length + for (var i = 0; i < pts1.length; i++) { + // assumes pts1.length == pts2.length d += Distance(pts1[i], pts2[i]); } return d / pts1.length; } -function PathLength(points: any) // length traversed by a point path -{ +function PathLength(points: any) { + // length traversed by a point path var d = 0.0; for (var i = 1; i < points.length; i++) { d += Distance(points[i - 1], points[i]); } return d; } -function Distance(p1: any, p2: any) // distance between two points -{ +function Distance(p1: any, p2: any) { + // distance between two points const dx = p2.X - p1.X; const dy = p2.Y - p1.Y; return Math.sqrt(dx * dx + dy * dy); } -function CalcStartUnitVector(points: any, index: any) // start angle from points[0] to points[index] normalized as a unit vector -{ +function CalcStartUnitVector(points: any, index: any) { + // start angle from points[0] to points[index] normalized as a unit vector const v = new Point(points[index]?.X - points[0]?.X, points[index]?.Y - points[0]?.Y); const len = Math.sqrt(v.X * v.X + v.Y * v.Y); return new Point(v.X / len, v.Y / len); } -function AngleBetweenUnitVectors(v1: any, v2: any) // gives acute angle between unit vectors from (0,0) to v1, and (0,0) to v2 -{ - const n = (v1.X * v2.X + v1.Y * v2.Y); +function AngleBetweenUnitVectors(v1: any, v2: any) { + // gives acute angle between unit vectors from (0,0) to v1, and (0,0) to v2 + const n = v1.X * v2.X + v1.Y * v2.Y; const c = Math.max(-1.0, Math.min(1.0, n)); // ensure [-1,+1] return Math.acos(c); // arc cosine of the vector dot product } -function Deg2Rad(d: any) { return (d * Math.PI / 180.0); }
\ No newline at end of file +function Deg2Rad(d: any) { + return (d * Math.PI) / 180.0; +} |
