View Javadoc

1   /*
2    * @(#)$Id: HammingDistance.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose		:   Computes the hamming distance.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date				Who			Reason
13   *
14   * Dec 11, 2006		ukurmann	Initial Release
15   * Mar 18, 2007     bsigner     Cleanup
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.algorithm.signature;
28  
29  import java.util.BitSet;
30  
31  
32  /**
33   * Computes the hamming distance.
34   * 
35   * @version 1.0 Dec 2006
36   * @author Ueli Kurmann, igesture@uelikurmann.ch
37   * @author Beat Signer, signer@inf.ethz.ch
38   */
39  public class HammingDistance implements DistanceFunction {
40  
41     public int computeDistance(GestureSignature sig1, GestureSignature sig2) {
42        final int minLen = Math.min(sig1.getNumberOfPoints(), sig2
43              .getNumberOfPoints());
44        final int maxLen = Math.max(sig1.getNumberOfPoints(), sig2
45              .getNumberOfPoints());
46        int result = 0;
47  
48        for (int i = 0; i < minLen; i++) {
49           final BitSet bitSet = (BitSet)sig1.getPointSignature(i).clone();
50           bitSet.xor(sig2.getPointSignature(i));
51           result += bitSet.cardinality();
52        }
53  
54        if (maxLen != minLen) {
55           result += (maxLen - minLen) * sig1.getBitStringLength();
56        }
57  
58        return result;
59     } // computeDistance
60  
61  }