OSDN Git Service

ライセンス文の更新
[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\r
8  * modify it under the terms of the GNU Lesser General Public License\r
9  * as published by the Free Software Foundation; either version 3\r
10  * of the License, or (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 Lesser General Public License for more details\r
16  * \r
17  * You should have received a copy of the GNU Lesser General Public\r
18  * License 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.squaredetect.NyARSquare;\r
32 import jp.nyatla.nyartoolkit.core.types.*;\r
33 import jp.nyatla.nyartoolkit.core.types.matrix.*;\r
34 \r
35 \r
36 \r
37 /**\r
38  * 疑似アフィン変換を使用して、ラスタ上の四角形から任意解像度\r
39  * の矩形パターンを作成します。\r
40  *\r
41  */\r
42 public class NyARColorPatt_PseudoAffine implements INyARColorPatt\r
43 {\r
44         private int[] _patdata;\r
45         private NyARBufferReader _buf_reader;\r
46         private NyARRgbPixelReader_INT1D_X8R8G8B8_32 _pixelreader;\r
47         private NyARIntSize _size;\r
48                 \r
49         public final int getWidth()\r
50         {\r
51                 return this._size.w;\r
52         }\r
53         \r
54         public final int getHeight()\r
55         {\r
56                 return this._size.h;\r
57         }\r
58         \r
59         public final NyARIntSize getSize()\r
60         {\r
61                 return  this._size;\r
62         }\r
63         \r
64         public final INyARBufferReader getBufferReader()\r
65         {\r
66                 return this._buf_reader;\r
67         }\r
68         \r
69         public final INyARRgbPixelReader getRgbPixelReader()\r
70         {\r
71                 return this._pixelreader;\r
72         }\r
73         NyARDoubleMatrix44 _invmat=new NyARDoubleMatrix44();\r
74         /**\r
75          * @param i_width\r
76          * @param i_height\r
77          */\r
78         public NyARColorPatt_PseudoAffine(int i_width, int i_height)\r
79         {               \r
80                 this._size=new NyARIntSize(i_width,i_height);\r
81                 this._patdata = new int[i_height*i_width];\r
82                 this._buf_reader=new NyARBufferReader(this._patdata,NyARBufferReader.BUFFERFORMAT_INT1D_X8R8G8B8_32);\r
83                 this._pixelreader=new NyARRgbPixelReader_INT1D_X8R8G8B8_32(this._patdata,this._size);\r
84                 //疑似アフィン変換のパラメタマトリクスを計算します。\r
85                 //長方形から計算すると、有効要素がm00,m01,m02,m03,m10,m11,m20,m23,m30になります。\r
86                 final NyARDoubleMatrix44 mat=this._invmat;\r
87                 mat.m00=0;\r
88                 mat.m01=0;\r
89                 mat.m02=0;\r
90                 mat.m03=1.0;\r
91                 mat.m10=0;\r
92                 mat.m11=i_width-1;\r
93                 mat.m12=0;\r
94                 mat.m13=1.0;\r
95                 mat.m20=(i_width-1)*(i_height-1);\r
96                 mat.m21=i_width-1;\r
97                 mat.m22=i_height-1;\r
98                 mat.m23=1.0;\r
99                 mat.m30=0;\r
100                 mat.m31=0;\r
101                 mat.m32=i_height-1;\r
102                 mat.m33=1.0;\r
103                 mat.inverse(mat);\r
104                 return;\r
105         }       \r
106 \r
107         /**\r
108          * 変換行列と頂点座標から、パラメータを計算\r
109          * o_paramの[0..3]にはXのパラメタ、[4..7]にはYのパラメタを格納する。\r
110          * @param i_vertex\r
111          * @param pa\r
112          * @param pb\r
113          */\r
114         private void calcPara(NyARIntPoint2d[] i_vertex,double[] o_cparam)\r
115         {\r
116                 final NyARDoubleMatrix44 invmat=this._invmat;\r
117                 double v1,v2,v4;\r
118                 //変換行列とベクトルの積から、変換パラメタを計算する。\r
119                 v1=i_vertex[0].x;\r
120                 v2=i_vertex[1].x;\r
121                 v4=i_vertex[3].x;\r
122                 \r
123                 o_cparam[0]=invmat.m00*v1+invmat.m01*v2+invmat.m02*i_vertex[2].x+invmat.m03*v4;\r
124                 o_cparam[1]=invmat.m10*v1+invmat.m11*v2;//m12,m13は0;\r
125                 o_cparam[2]=invmat.m20*v1+invmat.m23*v4;//m21,m22は0;\r
126                 o_cparam[3]=v1;//m30は1.0で、m31,m32,m33は0\r
127                 \r
128                 v1=i_vertex[0].y;\r
129                 v2=i_vertex[1].y;\r
130                 v4=i_vertex[3].y;\r
131 \r
132                 o_cparam[4]=invmat.m00*v1+invmat.m01*v2+invmat.m02*i_vertex[2].y+invmat.m03*v4;\r
133                 o_cparam[5]=invmat.m10*v1+invmat.m11*v2;//m12,m13は0;\r
134                 o_cparam[6]=invmat.m20*v1+invmat.m23*v4;//m21,m22は0;\r
135                 o_cparam[7]=v1;//m30は1.0で、m31,m32,m33は0\r
136                 return;\r
137         }\r
138 \r
139         /**\r
140          * 疑似アフィン変換の変換パラメタ\r
141          */\r
142         private double[] _convparam=new double[8];\r
143         \r
144         public boolean pickFromRaster(INyARRgbRaster image, NyARSquare i_square)throws NyARException\r
145         {\r
146                 final double[] conv_param=this._convparam;\r
147             int rx2,ry2;\r
148                 rx2=this._size.w;\r
149                 ry2=this._size.h;\r
150                 int[] rgb_tmp=new int[3];\r
151 \r
152                 INyARRgbPixelReader reader=image.getRgbPixelReader();\r
153                 // 変形先領域の頂点を取得\r
154 \r
155                 //変換行列から現在の座標系への変換パラメタを作成\r
156                 calcPara(i_square.imvertex,conv_param);// 変換パラメータを求める\r
157                 for(int y=0;y<ry2;y++){\r
158                         for(int x=0;x<rx2;x++){\r
159                                 final int ttx=(int)((conv_param[0]*x*y+conv_param[1]*x+conv_param[2]*y+conv_param[3])+0.5);\r
160                                 final int tty=(int)((conv_param[4]*x*y+conv_param[5]*x+conv_param[6]*y+conv_param[7])+0.5);\r
161                                 reader.getPixel((int)ttx,(int)tty,rgb_tmp);\r
162                                 this._patdata[x+y*rx2]=(rgb_tmp[0]<<16)|(rgb_tmp[1]<<8)|rgb_tmp[2];                             \r
163                         }\r
164                 }\r
165                 return true;\r
166         }\r
167 }