OSDN Git Service

5aba4a880c495042f39771eab8b23965307c988c
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core / pca2d / NyARPca2d_MatrixPCA.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 version ARToolkit class library.\r
11  * Copyright (C)2008 R.Iizuka\r
12  *\r
13  * This program is free software; you can redistribute it and/or\r
14  * modify it under the terms of the GNU General Public License\r
15  * as published by the Free Software Foundation; either version 2\r
16  * of the License, or (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 framework; if not, write to the Free Software\r
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
26  * \r
27  * For further information please contact.\r
28  *      http://nyatla.jp/nyatoolkit/\r
29  *      <airmail(at)ebony.plala.or.jp>\r
30  * \r
31  */\r
32 package jp.nyatla.nyartoolkit.core.pca2d;\r
33 \r
34 import jp.nyatla.nyartoolkit.NyARException;\r
35 import jp.nyatla.nyartoolkit.core.NyARMat;\r
36 import jp.nyatla.nyartoolkit.core.NyARVec;\r
37 import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d;\r
38 import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix22;\r
39 /**\r
40  * NyARMatrixを利用した主成分分析\r
41  * ARToolKitと同じ処理をします。\r
42  */\r
43 public class NyARPca2d_MatrixPCA implements INyARPca2d\r
44 {\r
45         private final NyARMat __pca_input = new NyARMat(1, 2);\r
46         private final NyARMat __pca_evec = new NyARMat(2, 2);\r
47         private final NyARVec __pca_ev = new NyARVec(2);\r
48         private final NyARVec __pca_mean = new NyARVec(2);      \r
49         \r
50         public void pca(double[] i_x,double[] i_y,int i_number_of_point,NyARDoubleMatrix22 o_evec, NyARDoublePoint2d o_ev,NyARDoublePoint2d o_mean) throws NyARException\r
51         {\r
52                 final NyARMat input = this.__pca_input;// 次処理で初期化される。             \r
53                 // pcaの準備\r
54                 input.realloc(i_number_of_point, 2);\r
55                 final double[][] input_array=input.getArray();\r
56                 for(int i=0;i<i_number_of_point;i++){\r
57                         input_array[i][0]=i_x[i];\r
58                         input_array[i][1]=i_y[i];\r
59                 }\r
60                 // 主成分分析\r
61                 input.matrixPCA(this.__pca_evec, this.__pca_ev, this.__pca_mean);\r
62                 final double[] mean_array = this.__pca_mean.getArray();\r
63                 final double[][] evec_array = this.__pca_evec.getArray();\r
64                 final double[] ev_array=this.__pca_ev.getArray();\r
65                 o_evec.m00=evec_array[0][0];\r
66                 o_evec.m01=evec_array[0][1];\r
67                 o_evec.m10=evec_array[1][0];\r
68                 o_evec.m11=evec_array[1][1];\r
69                 o_ev.x=ev_array[0];\r
70                 o_ev.x=ev_array[1];\r
71                 o_mean.x=mean_array[0];\r
72                 o_mean.y=mean_array[1];\r
73                 return;\r
74         }\r
75 }\r