diff options
Diffstat (limited to 'core/java/android/gesture/Instance.java')
-rwxr-xr-x | core/java/android/gesture/Instance.java | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/core/java/android/gesture/Instance.java b/core/java/android/gesture/Instance.java index 9ac0a96..7922fab 100755 --- a/core/java/android/gesture/Instance.java +++ b/core/java/android/gesture/Instance.java @@ -16,6 +16,7 @@ package android.gesture; +import android.graphics.Matrix; /** * An instance represents a sample if the label is available or a query if the @@ -27,9 +28,7 @@ class Instance { private static final int PATCH_SAMPLE_SIZE = 16; private final static float[] ORIENTATIONS = { - 0, (float) (Math.PI / 4), (float) (Math.PI / 2), (float) (Math.PI * 3 / 4), - (float) Math.PI, -0, (float) (-Math.PI / 4), (float) (-Math.PI / 2), - (float) (-Math.PI * 3 / 4), (float) -Math.PI + 0, 45, 90, 135, 180, -0, -45, -90, -135, -180 }; // the feature vector @@ -40,13 +39,13 @@ class Instance { // the id of the instance final long id; - + private Instance(long id, float[] sample, String sampleName) { this.id = id; vector = sample; label = sampleName; } - + private void normalize() { float[] sample = vector; float sum = 0; @@ -56,7 +55,7 @@ class Instance { sum += sample[i] * sample[i]; } - float magnitude = (float)Math.sqrt(sum); + float magnitude = (float) Math.sqrt(sum); for (int i = 0; i < size; i++) { sample[i] /= magnitude; } @@ -69,11 +68,11 @@ class Instance { * @param label * @return the instance */ - static Instance createInstance(int sequenceType, int orientationType, Gesture gesture, String label) { + static Instance createInstance(int samplingType, Gesture gesture, String label) { float[] pts; Instance instance; - if (sequenceType == GestureLibrary.SEQUENCE_SENSITIVE) { - pts = temporalSampler(orientationType, gesture); + if (samplingType == GestureLibrary.SEQUENCE_SENSITIVE) { + pts = temporalSampler(samplingType, gesture); instance = new Instance(gesture.getID(), pts, label); instance.normalize(); } else { @@ -82,19 +81,20 @@ class Instance { } return instance; } - + private static float[] spatialSampler(Gesture gesture) { return GestureUtilities.spatialSampling(gesture, PATCH_SAMPLE_SIZE); } - private static float[] temporalSampler(int orientationType, Gesture gesture) { + private static float[] temporalSampler(int samplingType, Gesture gesture) { float[] pts = GestureUtilities.temporalSampling(gesture.getStrokes().get(0), SEQUENCE_SAMPLE_SIZE); float[] center = GestureUtilities.computeCentroid(pts); - float orientation = (float)Math.atan2(pts[1] - center[1], pts[0] - center[0]); + float orientation = (float) Math.atan2(pts[1] - center[1], pts[0] - center[0]); + orientation *= 180 / Math.PI; float adjustment = -orientation; - if (orientationType == GestureLibrary.ORIENTATION_SENSITIVE) { + if (samplingType == GestureLibrary.ORIENTATION_SENSITIVE) { int count = ORIENTATIONS.length; for (int i = 0; i < count; i++) { float delta = ORIENTATIONS[i] - orientation; @@ -104,8 +104,10 @@ class Instance { } } - GestureUtilities.translate(pts, -center[0], -center[1]); - GestureUtilities.rotate(pts, adjustment); + Matrix m = new Matrix(); + m.setTranslate(-center[0], -center[1]); + m.postRotate(adjustment); + m.mapPoints(pts); return pts; } |