OSDN Git Service

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