Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c7f930f5 authored by Yang Li's avatar Yang Li
Browse files

Made GestureUtilities's several methods public; Changed GestureStroke's smoothening threshold.

parent bb04b631
Loading
Loading
Loading
Loading
+84 −2
Original line number Original line Diff line number Diff line
@@ -55200,6 +55200,88 @@
>
>
</field>
</field>
</class>
</class>
<class name="GestureUtilities"
 extends="java.lang.Object"
 abstract="false"
 static="false"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
<method name="computeOrientedBoundingBox"
 return="android.gesture.OrientedBoundingBox"
 abstract="false"
 native="false"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="originalPoints" type="java.util.ArrayList&lt;android.gesture.GesturePoint&gt;">
</parameter>
</method>
<method name="computeOrientedBoundingBox"
 return="android.gesture.OrientedBoundingBox"
 abstract="false"
 native="false"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="originalPoints" type="float[]">
</parameter>
</method>
<method name="spatialSampling"
 return="float[]"
 abstract="false"
 native="false"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="gesture" type="android.gesture.Gesture">
</parameter>
<parameter name="bitmapSize" type="int">
</parameter>
</method>
<method name="spatialSampling"
 return="float[]"
 abstract="false"
 native="false"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="gesture" type="android.gesture.Gesture">
</parameter>
<parameter name="bitmapSize" type="int">
</parameter>
<parameter name="keepAspectRatio" type="boolean">
</parameter>
</method>
<method name="temporalSampling"
 return="float[]"
 abstract="false"
 native="false"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="stroke" type="android.gesture.GestureStroke">
</parameter>
<parameter name="numPoints" type="int">
</parameter>
</method>
</class>
<class name="OrientedBoundingBox"
<class name="OrientedBoundingBox"
 extends="java.lang.Object"
 extends="java.lang.Object"
 abstract="false"
 abstract="false"
@@ -71687,7 +71769,7 @@
 type="float"
 type="float"
 transient="false"
 transient="false"
 volatile="false"
 volatile="false"
 value="0.0010f"
 value="0.001f"
 static="true"
 static="true"
 final="true"
 final="true"
 deprecated="not deprecated"
 deprecated="not deprecated"
@@ -208723,7 +208805,7 @@
 deprecated="not deprecated"
 deprecated="not deprecated"
 visibility="public"
 visibility="public"
>
>
<parameter name="arg0" type="T">
<parameter name="t" type="T">
</parameter>
</parameter>
</method>
</method>
</interface>
</interface>
+1 −1
Original line number Original line Diff line number Diff line
@@ -31,7 +31,7 @@ import java.util.ArrayList;
 * consists of a sequence of timed points. One or multiple strokes form a gesture.
 * consists of a sequence of timed points. One or multiple strokes form a gesture.
 */
 */
public class GestureStroke {
public class GestureStroke {
    static final float TOUCH_TOLERANCE = 8;
    static final float TOUCH_TOLERANCE = 3;


    public final RectF boundingBox;
    public final RectF boundingBox;


+69 −33
Original line number Original line Diff line number Diff line
@@ -26,7 +26,7 @@ import java.io.IOException;


import static android.gesture.GestureConstants.*;
import static android.gesture.GestureConstants.*;


final class GestureUtilities {
public final class GestureUtilities {
  
  
    private static final float SCALING_THRESHOLD = 0.26f;
    private static final float SCALING_THRESHOLD = 0.26f;
    private static final float NONUNIFORM_SCALE = (float) Math.sqrt(2);
    private static final float NONUNIFORM_SCALE = (float) Math.sqrt(2);
@@ -49,14 +49,38 @@ final class GestureUtilities {
        }
        }
    }
    }
    
    
    static float[] spatialSampling(Gesture gesture, int sampleMatrixDimension) {
    /**
        return spatialSampling(gesture, sampleMatrixDimension, false);
     * Samples the gesture spatially by rendering the gesture into a 2D 
     * grayscale bitmap. Scales the gesture to fit the size of the bitmap. 
     * The scaling does not necessarily keep the aspect ratio of the gesture. 
     * 
     * @param gesture the gesture to be sampled
     * @param bitmapSize the size of the bitmap
     * @return a bitmapSize x bitmapSize grayscale bitmap that is represented 
     *         as a 1D array. The float at index i represents the grayscale 
     *         value at pixel [i%bitmapSize, i/bitmapSize] 
     */
    public static float[] spatialSampling(Gesture gesture, int bitmapSize) {
        return spatialSampling(gesture, bitmapSize, false);
    }
    }


    static float[] spatialSampling(Gesture gesture, int sampleMatrixDimension, 
    /**
            boolean uniformScaling) {
     * Samples the gesture spatially by rendering the gesture into a 2D 
        final float targetPatchSize = sampleMatrixDimension - 1; // edge inclusive
     * grayscale bitmap. Scales the gesture to fit the size of the bitmap. 
        float[] sample = new float[sampleMatrixDimension * sampleMatrixDimension];
     * 
     * @param gesture the gesture to be sampled
     * @param bitmapSize the size of the bitmap
     * @param keepAspectRatio if the scaling should keep the gesture's 
     *        aspect ratio
     * 
     * @return a bitmapSize x bitmapSize grayscale bitmap that is represented 
     *         as a 1D array. The float at index i represents the grayscale 
     *         value at pixel [i%bitmapSize, i/bitmapSize] 
     */
    public static float[] spatialSampling(Gesture gesture, int bitmapSize, 
            boolean keepAspectRatio) {
        final float targetPatchSize = bitmapSize - 1; 
        float[] sample = new float[bitmapSize * bitmapSize];
        Arrays.fill(sample, 0);
        Arrays.fill(sample, 0);
  
  
        RectF rect = gesture.getBoundingBox();
        RectF rect = gesture.getBoundingBox();
@@ -65,7 +89,7 @@ final class GestureUtilities {
        float sx = targetPatchSize / gestureWidth;
        float sx = targetPatchSize / gestureWidth;
        float sy = targetPatchSize / gestureHeight;
        float sy = targetPatchSize / gestureHeight;
        
        
        if (uniformScaling) {
        if (keepAspectRatio) {
            float scale = sx < sy ? sx : sy;
            float scale = sx < sy ? sx : sy;
            sx = scale;
            sx = scale;
            sy = scale;
            sy = scale;
@@ -122,16 +146,16 @@ final class GestureUtilities {
                if (segmentStartY > targetPatchSize) {
                if (segmentStartY > targetPatchSize) {
                    segmentStartY = targetPatchSize;
                    segmentStartY = targetPatchSize;
                }
                }
                plot(segmentStartX, segmentStartY, sample, sampleMatrixDimension);
                plot(segmentStartX, segmentStartY, sample, bitmapSize);
                if (segmentEndX != -1) {
                if (segmentEndX != -1) {
                    // evaluate horizontally
                    // Evaluate horizontally
                    if (segmentEndX > segmentStartX) {
                    if (segmentEndX > segmentStartX) {
                        xpos = (float) Math.ceil(segmentStartX);
                        xpos = (float) Math.ceil(segmentStartX);
                        float slope = (segmentEndY - segmentStartY) / 
                        float slope = (segmentEndY - segmentStartY) / 
                                      (segmentEndX - segmentStartX);
                                      (segmentEndX - segmentStartX);
                        while (xpos < segmentEndX) {
                        while (xpos < segmentEndX) {
                            ypos = slope * (xpos - segmentStartX) + segmentStartY;
                            ypos = slope * (xpos - segmentStartX) + segmentStartY;
                            plot(xpos, ypos, sample, sampleMatrixDimension); 
                            plot(xpos, ypos, sample, bitmapSize); 
                            xpos++;
                            xpos++;
                        }
                        }
                    } else if (segmentEndX < segmentStartX){
                    } else if (segmentEndX < segmentStartX){
@@ -140,18 +164,18 @@ final class GestureUtilities {
                                      (segmentEndX - segmentStartX);
                                      (segmentEndX - segmentStartX);
                        while (xpos < segmentStartX) {
                        while (xpos < segmentStartX) {
                            ypos = slope * (xpos - segmentStartX) + segmentStartY;
                            ypos = slope * (xpos - segmentStartX) + segmentStartY;
                            plot(xpos, ypos, sample, sampleMatrixDimension); 
                            plot(xpos, ypos, sample, bitmapSize); 
                            xpos++;
                            xpos++;
                        }
                        }
                    }
                    }
                    // evaluating vertically
                    // Evaluate vertically
                    if (segmentEndY > segmentStartY) {
                    if (segmentEndY > segmentStartY) {
                        ypos = (float) Math.ceil(segmentStartY);
                        ypos = (float) Math.ceil(segmentStartY);
                        float invertSlope = (segmentEndX - segmentStartX) / 
                        float invertSlope = (segmentEndX - segmentStartX) / 
                                            (segmentEndY - segmentStartY);
                                            (segmentEndY - segmentStartY);
                        while (ypos < segmentEndY) {
                        while (ypos < segmentEndY) {
                            xpos = invertSlope * (ypos - segmentStartY) + segmentStartX;
                            xpos = invertSlope * (ypos - segmentStartY) + segmentStartX;
                            plot(xpos, ypos, sample, sampleMatrixDimension); 
                            plot(xpos, ypos, sample, bitmapSize); 
                            ypos++;
                            ypos++;
                        }
                        }
                    } else if (segmentEndY < segmentStartY) {
                    } else if (segmentEndY < segmentStartY) {
@@ -160,7 +184,7 @@ final class GestureUtilities {
                                            (segmentEndY - segmentStartY);
                                            (segmentEndY - segmentStartY);
                        while (ypos < segmentStartY) {
                        while (ypos < segmentStartY) {
                            xpos = invertSlope * (ypos - segmentStartY) + segmentStartX; 
                            xpos = invertSlope * (ypos - segmentStartY) + segmentStartX; 
                            plot(xpos, ypos, sample, sampleMatrixDimension); 
                            plot(xpos, ypos, sample, bitmapSize); 
                            ypos++;
                            ypos++;
                        }
                        }
                    }
                    }
@@ -224,15 +248,16 @@ final class GestureUtilities {
    }
    }


    /**
    /**
     * Featurizes a stroke into a vector of a given number of elements
     * Samples a stroke temporally into a given number of evenly-distributed 
     * points.
     * 
     * 
     * @param stroke
     * @param stroke the gesture stroke to be sampled
     * @param sampleSize
     * @param numPoints the number of points
     * @return a float array
     * @return the sampled points in the form of [x1, y1, x2, y2, ..., xn, yn]
     */
     */
    static float[] temporalSampling(GestureStroke stroke, int sampleSize) {
    public static float[] temporalSampling(GestureStroke stroke, int numPoints) {
        final float increment = stroke.length / (sampleSize - 1);
        final float increment = stroke.length / (numPoints - 1);
        int vectorLength = sampleSize * 2;
        int vectorLength = numPoints * 2;
        float[] vector = new float[vectorLength];
        float[] vector = new float[vectorLength];
        float distanceSoFar = 0;
        float distanceSoFar = 0;
        float[] pts = stroke.points;
        float[] pts = stroke.points;
@@ -287,9 +312,9 @@ final class GestureUtilities {
    }
    }


    /**
    /**
     * Calculate the centroid 
     * Calculates the centroid of a set of points.
     * 
     * 
     * @param points
     * @param points the points in the form of [x1, y1, x2, y2, ..., xn, yn]
     * @return the centroid
     * @return the centroid
     */
     */
    static float[] computeCentroid(float[] points) {
    static float[] computeCentroid(float[] points) {
@@ -309,10 +334,10 @@ final class GestureUtilities {
    }
    }


    /**
    /**
     * calculate the variance-covariance matrix, treat each point as a sample
     * Calculates the variance-covariance matrix of a set of points.
     * 
     * 
     * @param points
     * @param points the points in the form of [x1, y1, x2, y2, ..., xn, yn]
     * @return the covariance matrix
     * @return the variance-covariance matrix
     */
     */
    private static float[][] computeCoVariance(float[] points) {
    private static float[][] computeCoVariance(float[] points) {
        float[][] array = new float[2][2];
        float[][] array = new float[2][2];
@@ -363,7 +388,7 @@ final class GestureUtilities {
    }
    }


    /**
    /**
     * Calculate the squared Euclidean distance between two vectors
     * Calculates the squared Euclidean distance between two vectors.
     * 
     * 
     * @param vector1
     * @param vector1
     * @param vector2
     * @param vector2
@@ -380,7 +405,7 @@ final class GestureUtilities {
    }
    }


    /**
    /**
     * Calculate the cosine distance between two instances
     * Calculates the cosine distance between two instances.
     * 
     * 
     * @param vector1
     * @param vector1
     * @param vector2
     * @param vector2
@@ -396,7 +421,7 @@ final class GestureUtilities {
    }
    }
    
    
    /**
    /**
     * Calculate the "minimum" cosine distance between two instances
     * Calculates the "minimum" cosine distance between two instances.
     * 
     * 
     * @param vector1
     * @param vector1
     * @param vector2
     * @param vector2
@@ -426,8 +451,13 @@ final class GestureUtilities {
        }
        }
    }
    }



    /**
    static OrientedBoundingBox computeOrientedBoundingBox(ArrayList<GesturePoint> originalPoints) {
     * Computes an oriented, minimum bounding box of a set of points.
     * 
     * @param originalPoints
     * @return an oriented bounding box
     */
    public static OrientedBoundingBox computeOrientedBoundingBox(ArrayList<GesturePoint> originalPoints) {
        final int count = originalPoints.size();
        final int count = originalPoints.size();
        float[] points = new float[count * 2];
        float[] points = new float[count * 2];
        for (int i = 0; i < count; i++) {
        for (int i = 0; i < count; i++) {
@@ -440,7 +470,13 @@ final class GestureUtilities {
        return computeOrientedBoundingBox(points, meanVector);
        return computeOrientedBoundingBox(points, meanVector);
    }
    }


    static OrientedBoundingBox computeOrientedBoundingBox(float[] originalPoints) {
    /**
     * Computes an oriented, minimum bounding box of a set of points.
     * 
     * @param originalPoints
     * @return an oriented bounding box
     */
    public static OrientedBoundingBox computeOrientedBoundingBox(float[] originalPoints) {
        int size = originalPoints.length;
        int size = originalPoints.length;
        float[] points = new float[size];
        float[] points = new float[size];
        for (int i = 0; i < size; i++) {
        for (int i = 0; i < size; i++) {