OSDN Git Service

[更新]NyARToolkit for Java
[nyartoolkit-and/nyartoolkit-and.git] / trunk / src / jp / nyatla / nyartoolkit / core / pickup / NyARColorPatt_PseudoAffine.java
1 /* \r
2  * PROJECT: NyARToolkit(Extension)\r
3  * --------------------------------------------------------------------------------\r
4  * The NyARToolkit is Java edition ARToolKit class library.\r
5  * Copyright (C)2008-2009 Ryo Iizuka\r
6  *\r
7  * This program is free software: you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation, either version 3 of the License, or\r
10  * (at your option) any later version.\r
11  * \r
12  * This program is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
19  * \r
20  * For further information please contact.\r
21  *      http://nyatla.jp/nyatoolkit/\r
22  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
23  * \r
24  */\r
25 package jp.nyatla.nyartoolkit.core.pickup;\r
26 \r
27 \r
28 import jp.nyatla.nyartoolkit.NyARException;\r
29 import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
30 import jp.nyatla.nyartoolkit.core.rasterreader.*;\r
31 import jp.nyatla.nyartoolkit.core.types.*;\r
32 import jp.nyatla.nyartoolkit.core.types.matrix.*;\r
33 \r
34 \r
35 \r
36 /**\r
37  * 疑似アフィン変換を使用して、ラスタ上の四角形から任意解像度\r
38  * の矩形パターンを作成します。\r
39  *\r
40  */\r
41 public class NyARColorPatt_PseudoAffine implements INyARColorPatt\r
42 {\r
43         private int[] _patdata;\r
44         private NyARBufferReader _buf_reader;\r
45         private NyARRgbPixelReader_INT1D_X8R8G8B8_32 _pixelreader;\r
46         private NyARIntSize _size;\r
47                 \r
48         public final int getWidth()\r
49         {\r
50                 return this._size.w;\r
51         }\r
52         \r
53         public final int getHeight()\r
54         {\r
55                 return this._size.h;\r
56         }\r
57         \r
58         public final NyARIntSize getSize()\r
59         {\r
60                 return  this._size;\r
61         }\r
62         \r
63         public final INyARBufferReader getBufferReader()\r
64         {\r
65                 return this._buf_reader;\r
66         }\r
67         \r
68         public final INyARRgbPixelReader getRgbPixelReader()\r
69         {\r
70                 return this._pixelreader;\r
71         }\r
72         NyARDoubleMatrix44 _invmat=new NyARDoubleMatrix44();\r
73         /**\r
74          * @param i_width\r
75          * @param i_height\r
76          */\r
77         public NyARColorPatt_PseudoAffine(int i_width, int i_height)\r
78         {               \r
79                 this._size=new NyARIntSize(i_width,i_height);\r
80                 this._patdata = new int[i_height*i_width];\r
81                 this._buf_reader=new NyARBufferReader(this._patdata,NyARBufferReader.BUFFERFORMAT_INT1D_X8R8G8B8_32);\r
82                 this._pixelreader=new NyARRgbPixelReader_INT1D_X8R8G8B8_32(this._patdata,this._size);\r
83                 //疑似アフィン変換のパラメタマトリクスを計算します。\r
84                 //長方形から計算すると、有効要素がm00,m01,m02,m03,m10,m11,m20,m23,m30になります。\r
85                 final NyARDoubleMatrix44 mat=this._invmat;\r
86                 mat.m00=0;\r
87                 mat.m01=0;\r
88                 mat.m02=0;\r
89                 mat.m03=1.0;\r
90                 mat.m10=0;\r
91                 mat.m11=i_width-1;\r
92                 mat.m12=0;\r
93                 mat.m13=1.0;\r
94                 mat.m20=(i_width-1)*(i_height-1);\r
95                 mat.m21=i_width-1;\r
96                 mat.m22=i_height-1;\r
97                 mat.m23=1.0;\r
98                 mat.m30=0;\r
99                 mat.m31=0;\r
100                 mat.m32=i_height-1;\r
101                 mat.m33=1.0;\r
102                 mat.inverse(mat);\r
103                 return;\r
104         }       \r
105 \r
106         /**\r
107          * 変換行列と頂点座標から、パラメータを計算\r
108          * o_paramの[0..3]にはXのパラメタ、[4..7]にはYのパラメタを格納する。\r
109          * @param i_vertex\r
110          * @param pa\r
111          * @param pb\r
112          */\r
113         private void calcPara(NyARIntPoint2d[] i_vertex,double[] o_cparam)\r
114         {\r
115                 final NyARDoubleMatrix44 invmat=this._invmat;\r
116                 double v1,v2,v4;\r
117                 //変換行列とベクトルの積から、変換パラメタを計算する。\r
118                 v1=i_vertex[0].x;\r
119                 v2=i_vertex[1].x;\r
120                 v4=i_vertex[3].x;\r
121                 \r
122                 o_cparam[0]=invmat.m00*v1+invmat.m01*v2+invmat.m02*i_vertex[2].x+invmat.m03*v4;\r
123                 o_cparam[1]=invmat.m10*v1+invmat.m11*v2;//m12,m13は0;\r
124                 o_cparam[2]=invmat.m20*v1+invmat.m23*v4;//m21,m22は0;\r
125                 o_cparam[3]=v1;//m30は1.0で、m31,m32,m33は0\r
126                 \r
127                 v1=i_vertex[0].y;\r
128                 v2=i_vertex[1].y;\r
129                 v4=i_vertex[3].y;\r
130 \r
131                 o_cparam[4]=invmat.m00*v1+invmat.m01*v2+invmat.m02*i_vertex[2].y+invmat.m03*v4;\r
132                 o_cparam[5]=invmat.m10*v1+invmat.m11*v2;//m12,m13は0;\r
133                 o_cparam[6]=invmat.m20*v1+invmat.m23*v4;//m21,m22は0;\r
134                 o_cparam[7]=v1;//m30は1.0で、m31,m32,m33は0\r
135                 return;\r
136         }\r
137 \r
138         /**\r
139          * 疑似アフィン変換の変換パラメタ\r
140          */\r
141         private double[] _convparam=new double[8];\r
142         \r
143         /**\r
144          * @see INyARColorPatt#pickFromRaster\r
145          */\r
146         public boolean pickFromRaster(INyARRgbRaster image,NyARIntPoint2d[] i_vertexs)throws NyARException\r
147         {\r
148                 final double[] conv_param=this._convparam;\r
149             int rx2,ry2;\r
150                 rx2=this._size.w;\r
151                 ry2=this._size.h;\r
152                 int[] rgb_tmp=new int[3];\r
153 \r
154                 INyARRgbPixelReader reader=image.getRgbPixelReader();\r
155                 // 変形先領域の頂点を取得\r
156 \r
157                 //変換行列から現在の座標系への変換パラメタを作成\r
158                 calcPara(i_vertexs,conv_param);// 変換パラメータを求める\r
159                 for(int y=0;y<ry2;y++){\r
160                         for(int x=0;x<rx2;x++){\r
161                                 final int ttx=(int)((conv_param[0]*x*y+conv_param[1]*x+conv_param[2]*y+conv_param[3])+0.5);\r
162                                 final int tty=(int)((conv_param[4]*x*y+conv_param[5]*x+conv_param[6]*y+conv_param[7])+0.5);\r
163                                 reader.getPixel((int)ttx,(int)tty,rgb_tmp);\r
164                                 this._patdata[x+y*rx2]=(rgb_tmp[0]<<16)|(rgb_tmp[1]<<8)|rgb_tmp[2];                             \r
165                         }\r
166                 }\r
167                 return true;\r
168         }\r
169 }