OSDN Git Service

Merge branch 'git-svn'
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.1 / 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 import jp.nyatla.nyartoolkit.core.raster.*;\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 NyARRgbPixelReader_INT1D_X8R8G8B8_32 _pixelreader;\r
46         private NyARIntSize _size;\r
47         private static final int BUFFER_FORMAT=NyARBufferType.INT1D_X8R8G8B8_32;\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         public final INyARRgbPixelReader getRgbPixelReader()\r
64         {\r
65                 return this._pixelreader;\r
66         }\r
67         public Object getBuffer()\r
68         {\r
69                 return this._patdata;\r
70         }\r
71         public boolean hasBuffer()\r
72         {\r
73                 return this._patdata!=null;\r
74         }\r
75         public void wrapBuffer(Object i_ref_buf) throws NyARException\r
76         {\r
77                 NyARException.notImplement();\r
78         }\r
79         final public int getBufferType()\r
80         {\r
81                 return BUFFER_FORMAT;\r
82         }\r
83         final public boolean isEqualBufferType(int i_type_value)\r
84         {\r
85                 return BUFFER_FORMAT==i_type_value;\r
86         }       \r
87         NyARDoubleMatrix44 _invmat=new NyARDoubleMatrix44();\r
88         /**\r
89          * @param i_width\r
90          * @param i_height\r
91          */\r
92         public NyARColorPatt_PseudoAffine(int i_width, int i_height)\r
93         {               \r
94                 this._size=new NyARIntSize(i_width,i_height);\r
95                 this._patdata = new int[i_height*i_width];\r
96                 this._pixelreader=new NyARRgbPixelReader_INT1D_X8R8G8B8_32(this._patdata,this._size);\r
97                 //疑似アフィン変換のパラメタマトリクスを計算します。\r
98                 //長方形から計算すると、有効要素がm00,m01,m02,m03,m10,m11,m20,m23,m30になります。\r
99                 final NyARDoubleMatrix44 mat=this._invmat;\r
100                 mat.m00=0;\r
101                 mat.m01=0;\r
102                 mat.m02=0;\r
103                 mat.m03=1.0;\r
104                 mat.m10=0;\r
105                 mat.m11=i_width-1;\r
106                 mat.m12=0;\r
107                 mat.m13=1.0;\r
108                 mat.m20=(i_width-1)*(i_height-1);\r
109                 mat.m21=i_width-1;\r
110                 mat.m22=i_height-1;\r
111                 mat.m23=1.0;\r
112                 mat.m30=0;\r
113                 mat.m31=0;\r
114                 mat.m32=i_height-1;\r
115                 mat.m33=1.0;\r
116                 mat.inverse(mat);\r
117                 return;\r
118         }       \r
119 \r
120         /**\r
121          * 変換行列と頂点座標から、パラメータを計算\r
122          * o_paramの[0..3]にはXのパラメタ、[4..7]にはYのパラメタを格納する。\r
123          * @param i_vertex\r
124          * @param pa\r
125          * @param pb\r
126          */\r
127         private void calcPara(NyARIntPoint2d[] i_vertex,double[] o_cparam)\r
128         {\r
129                 final NyARDoubleMatrix44 invmat=this._invmat;\r
130                 double v1,v2,v4;\r
131                 //変換行列とベクトルの積から、変換パラメタを計算する。\r
132                 v1=i_vertex[0].x;\r
133                 v2=i_vertex[1].x;\r
134                 v4=i_vertex[3].x;\r
135                 \r
136                 o_cparam[0]=invmat.m00*v1+invmat.m01*v2+invmat.m02*i_vertex[2].x+invmat.m03*v4;\r
137                 o_cparam[1]=invmat.m10*v1+invmat.m11*v2;//m12,m13は0;\r
138                 o_cparam[2]=invmat.m20*v1+invmat.m23*v4;//m21,m22は0;\r
139                 o_cparam[3]=v1;//m30は1.0で、m31,m32,m33は0\r
140                 \r
141                 v1=i_vertex[0].y;\r
142                 v2=i_vertex[1].y;\r
143                 v4=i_vertex[3].y;\r
144 \r
145                 o_cparam[4]=invmat.m00*v1+invmat.m01*v2+invmat.m02*i_vertex[2].y+invmat.m03*v4;\r
146                 o_cparam[5]=invmat.m10*v1+invmat.m11*v2;//m12,m13は0;\r
147                 o_cparam[6]=invmat.m20*v1+invmat.m23*v4;//m21,m22は0;\r
148                 o_cparam[7]=v1;//m30は1.0で、m31,m32,m33は0\r
149                 return;\r
150         }\r
151 \r
152         /**\r
153          * 疑似アフィン変換の変換パラメタ\r
154          */\r
155         private double[] _convparam=new double[8];\r
156         \r
157         /**\r
158          * @see INyARColorPatt#pickFromRaster\r
159          */\r
160         public boolean pickFromRaster(INyARRgbRaster image,NyARIntPoint2d[] i_vertexs)throws NyARException\r
161         {\r
162                 final double[] conv_param=this._convparam;\r
163             int rx2,ry2;\r
164                 rx2=this._size.w;\r
165                 ry2=this._size.h;\r
166                 int[] rgb_tmp=new int[3];\r
167 \r
168                 INyARRgbPixelReader reader=image.getRgbPixelReader();\r
169                 // 変形先領域の頂点を取得\r
170 \r
171                 //変換行列から現在の座標系への変換パラメタを作成\r
172                 calcPara(i_vertexs,conv_param);// 変換パラメータを求める\r
173                 for(int y=0;y<ry2;y++){\r
174                         for(int x=0;x<rx2;x++){\r
175                                 final int ttx=(int)((conv_param[0]*x*y+conv_param[1]*x+conv_param[2]*y+conv_param[3])+0.5);\r
176                                 final int tty=(int)((conv_param[4]*x*y+conv_param[5]*x+conv_param[6]*y+conv_param[7])+0.5);\r
177                                 reader.getPixel((int)ttx,(int)tty,rgb_tmp);\r
178                                 this._patdata[x+y*rx2]=(rgb_tmp[0]<<16)|(rgb_tmp[1]<<8)|rgb_tmp[2];                             \r
179                         }\r
180                 }\r
181                 return true;\r
182         }\r
183 }