OSDN Git Service

[Backup]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / core / match / NyARMatchPatt_Color_WITH_PCA.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\r
13  * This program is free software: you can redistribute it and/or modify\r
14  * it under the terms of the GNU General Public License as published by\r
15  * the Free Software Foundation, either version 3 of the License, or\r
16  * (at your option) any later version.\r
17  * \r
18  * This program is distributed in the hope that it will be useful,\r
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
21  * GNU General Public License for more details.\r
22  *\r
23  * You should have received a copy of the GNU General Public License\r
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
25  * \r
26  * For further information please contact.\r
27  *      http://nyatla.jp/nyatoolkit/\r
28  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.core.match;\r
32 \r
33 import jp.nyatla.nyartoolkit.NyARException;\r
34 import jp.nyatla.nyartoolkit.core.NyARCode;\r
35 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;\r
36 \r
37 /**\r
38  * AR_TEMPLATE_MATCHING_COLORかつAR_MATCHING_WITH_PCAと同等のルールで マーカーを評価します。\r
39  * \r
40  */\r
41 public class NyARMatchPatt_Color_WITH_PCA extends NyARMatchPatt_Color_WITHOUT_PCA\r
42 {\r
43         private final int EVEC_MAX = 10;// #define EVEC_MAX 10\r
44 \r
45         private int evec_dim;// static int evec_dim;\r
46         private double[][] evec;// static double evec[EVEC_MAX][AR_PATT_SIZE_Y*AR_PATT_SIZE_X*3];\r
47         private double[][] epat = new double[4][EVEC_MAX];// static double epat[AR_PATT_NUM_MAX][4][EVEC_MAX];\r
48 \r
49 \r
50         public NyARMatchPatt_Color_WITH_PCA(int i_width, int i_height)\r
51         {\r
52                 super(i_width,i_height);\r
53                 return;\r
54         }\r
55         public NyARMatchPatt_Color_WITH_PCA(NyARCode i_code_ref)\r
56         {\r
57                 super(i_code_ref);\r
58                 return;\r
59         }       \r
60         public boolean evaluate(NyARMatchPattDeviationColorData i_patt,NyARMatchPattResult o_result) throws NyARException\r
61         {\r
62                 final int[] linput = i_patt.refData();\r
63                 int sum;\r
64                 double max = 0.0;\r
65                 int res = NyARSquare.DIRECTION_UNKNOWN;\r
66 /*              \r
67                 NyARException.trap(\r
68                         "NyARMatchPatt_Color_WITH_PCA\n"+\r
69                         "この箇所の移植は不完全です!"+\r
70                         "ARToolKitの移植条件を完全に再現できていないため、evec,epatの計算が無視されています。"+\r
71                         "gen_evec(void)も含めて移植の必要があるはずですが、まだ未解析です。");\r
72 */              double[] invec = new double[EVEC_MAX];\r
73                 for (int i = 0; i < this.evec_dim; i++) {\r
74                         invec[i] = 0.0;\r
75                         for(int j=0;j<this._rgbpixels;i++){\r
76                                 invec[i] += this.evec[i][j] * linput[j];\r
77                         }\r
78                         invec[i] /= i_patt.getPow();\r
79                 }\r
80                 double min = 10000.0;\r
81                 for (int j = 0; j < 4; j++) {\r
82                         double sum2 = 0;\r
83                         for (int i = 0; i < this.evec_dim; i++) {\r
84                                 sum2 += (invec[i] - this.epat[j][i]) * (invec[i] - this.epat[j][i]);\r
85                         }\r
86                         if (sum2 < min) {\r
87                                 min = sum2;\r
88                                 res = j;\r
89                                 // res2 = k;//kは常にインスタンスを刺すから、省略可能\r
90                         }\r
91                 }\r
92                 sum=0;\r
93                 final int[] code_data=this._code_patt.getColorData(res).refData();\r
94                 for (int i = 0; i < this._rgbpixels; i++) {// for(int\r
95                         sum += linput[i] * code_data[i];// sum +=input[i][i2][i3]*pat[res2][res][i][i2][i3];\r
96                 }\r
97                 max = sum /  this._code_patt.getColorData(res).getPow() / i_patt.getPow();\r
98                 o_result.direction = res;\r
99                 o_result.confidence = max;\r
100                 return true;\r
101         }\r
102 }\r