Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008-2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 17 | package android.gesture; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 18 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 19 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 20 | /** |
| 21 | * An instance represents a sample if the label is available or a query if the |
| 22 | * label is null. |
| 23 | */ |
| 24 | class Instance { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 25 | private static final int SEQUENCE_SAMPLE_SIZE = 16; |
| 26 | |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 27 | private static final int PATCH_SAMPLE_SIZE = 16; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 28 | |
| 29 | private final static float[] ORIENTATIONS = { |
Romain Guy | f40f074 | 2009-05-28 21:12:01 -0700 | [diff] [blame] | 30 | 0, (float) (Math.PI / 4), (float) (Math.PI / 2), (float) (Math.PI * 3 / 4), |
| 31 | (float) Math.PI, -0, (float) (-Math.PI / 4), (float) (-Math.PI / 2), |
| 32 | (float) (-Math.PI * 3 / 4), (float) -Math.PI |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | // the feature vector |
| 36 | final float[] vector; |
| 37 | |
| 38 | // the label can be null |
| 39 | final String label; |
| 40 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 41 | // the id of the instance |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 42 | final long id; |
Romain Guy | f40f074 | 2009-05-28 21:12:01 -0700 | [diff] [blame] | 43 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 44 | private Instance(long id, float[] sample, String sampleName) { |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 45 | this.id = id; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 46 | vector = sample; |
| 47 | label = sampleName; |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 48 | } |
Romain Guy | f40f074 | 2009-05-28 21:12:01 -0700 | [diff] [blame] | 49 | |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 50 | private void normalize() { |
| 51 | float[] sample = vector; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 52 | float sum = 0; |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 53 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 54 | int size = sample.length; |
| 55 | for (int i = 0; i < size; i++) { |
| 56 | sum += sample[i] * sample[i]; |
| 57 | } |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 58 | |
Romain Guy | f40f074 | 2009-05-28 21:12:01 -0700 | [diff] [blame] | 59 | float magnitude = (float)Math.sqrt(sum); |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 60 | for (int i = 0; i < size; i++) { |
| 61 | sample[i] /= magnitude; |
| 62 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * create a learning instance for a single stroke gesture |
| 67 | * |
| 68 | * @param gesture |
| 69 | * @param label |
| 70 | * @return the instance |
| 71 | */ |
Romain Guy | f40f074 | 2009-05-28 21:12:01 -0700 | [diff] [blame] | 72 | static Instance createInstance(int sequenceType, int orientationType, Gesture gesture, String label) { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 73 | float[] pts; |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 74 | Instance instance; |
Romain Guy | 0a63716 | 2009-05-29 14:43:54 -0700 | [diff] [blame] | 75 | if (sequenceType == GestureStore.SEQUENCE_SENSITIVE) { |
Romain Guy | f40f074 | 2009-05-28 21:12:01 -0700 | [diff] [blame] | 76 | pts = temporalSampler(orientationType, gesture); |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 77 | instance = new Instance(gesture.getID(), pts, label); |
| 78 | instance.normalize(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 79 | } else { |
| 80 | pts = spatialSampler(gesture); |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 81 | instance = new Instance(gesture.getID(), pts, label); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 82 | } |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 83 | return instance; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 84 | } |
Romain Guy | f40f074 | 2009-05-28 21:12:01 -0700 | [diff] [blame] | 85 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 86 | private static float[] spatialSampler(Gesture gesture) { |
Romain Guy | 46c5312 | 2010-02-04 14:19:50 -0800 | [diff] [blame] | 87 | return GestureUtils.spatialSampling(gesture, PATCH_SAMPLE_SIZE, false); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Romain Guy | f40f074 | 2009-05-28 21:12:01 -0700 | [diff] [blame] | 90 | private static float[] temporalSampler(int orientationType, Gesture gesture) { |
Romain Guy | 46c5312 | 2010-02-04 14:19:50 -0800 | [diff] [blame] | 91 | float[] pts = GestureUtils.temporalSampling(gesture.getStrokes().get(0), |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 92 | SEQUENCE_SAMPLE_SIZE); |
Romain Guy | 46c5312 | 2010-02-04 14:19:50 -0800 | [diff] [blame] | 93 | float[] center = GestureUtils.computeCentroid(pts); |
Romain Guy | f40f074 | 2009-05-28 21:12:01 -0700 | [diff] [blame] | 94 | float orientation = (float)Math.atan2(pts[1] - center[1], pts[0] - center[0]); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 95 | |
| 96 | float adjustment = -orientation; |
Yang Li | 4758f12 | 2009-12-14 15:41:07 -0800 | [diff] [blame] | 97 | if (orientationType != GestureStore.ORIENTATION_INVARIANT) { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 98 | int count = ORIENTATIONS.length; |
| 99 | for (int i = 0; i < count; i++) { |
| 100 | float delta = ORIENTATIONS[i] - orientation; |
| 101 | if (Math.abs(delta) < Math.abs(adjustment)) { |
| 102 | adjustment = delta; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Romain Guy | 46c5312 | 2010-02-04 14:19:50 -0800 | [diff] [blame] | 107 | GestureUtils.translate(pts, -center[0], -center[1]); |
| 108 | GestureUtils.rotate(pts, adjustment); |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 109 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 110 | return pts; |
| 111 | } |
| 112 | |
| 113 | } |