OSDN Git Service

[TAG]NyARToolkit/2.5.0
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.0 / src / jp / nyatla / nyartoolkit / core / pickup / NyARColorPatt_PseudoAffine.java
diff --git a/tags/2.5.0/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_PseudoAffine.java b/tags/2.5.0/src/jp/nyatla/nyartoolkit/core/pickup/NyARColorPatt_PseudoAffine.java
new file mode 100644 (file)
index 0000000..35d3ec9
--- /dev/null
@@ -0,0 +1,183 @@
+/* \r
+ * PROJECT: NyARToolkit(Extension)\r
+ * --------------------------------------------------------------------------------\r
+ * The NyARToolkit is Java edition ARToolKit class library.\r
+ * Copyright (C)2008-2009 Ryo Iizuka\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ * \r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ * \r
+ * For further information please contact.\r
+ *     http://nyatla.jp/nyatoolkit/\r
+ *     <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
+ * \r
+ */\r
+package jp.nyatla.nyartoolkit.core.pickup;\r
+\r
+\r
+import jp.nyatla.nyartoolkit.NyARException;\r
+import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
+import jp.nyatla.nyartoolkit.core.rasterreader.*;\r
+import jp.nyatla.nyartoolkit.core.types.*;\r
+import jp.nyatla.nyartoolkit.core.types.matrix.*;\r
+import jp.nyatla.nyartoolkit.core.raster.*;\r
+\r
+\r
+\r
+/**\r
+ * 疑似アフィン変換を使用して、ラスタ上の四角形から任意解像度\r
+ * の矩形パターンを作成します。\r
+ *\r
+ */\r
+public class NyARColorPatt_PseudoAffine implements INyARColorPatt\r
+{\r
+       private int[] _patdata;\r
+       private NyARRgbPixelReader_INT1D_X8R8G8B8_32 _pixelreader;\r
+       private NyARIntSize _size;\r
+       private static final int BUFFER_FORMAT=NyARBufferType.INT1D_X8R8G8B8_32;\r
+               \r
+       public final int getWidth()\r
+       {\r
+               return this._size.w;\r
+       }\r
+       \r
+       public final int getHeight()\r
+       {\r
+               return this._size.h;\r
+       }\r
+       \r
+       public final NyARIntSize getSize()\r
+       {\r
+               return  this._size;\r
+       }\r
+       public final INyARRgbPixelReader getRgbPixelReader()\r
+       {\r
+               return this._pixelreader;\r
+       }\r
+       public Object getBuffer()\r
+       {\r
+               return this._patdata;\r
+       }\r
+       public boolean hasBuffer()\r
+       {\r
+               return this._patdata!=null;\r
+       }\r
+       public void wrapBuffer(Object i_ref_buf) throws NyARException\r
+       {\r
+               NyARException.notImplement();\r
+       }\r
+       final public int getBufferType()\r
+       {\r
+               return BUFFER_FORMAT;\r
+       }\r
+       final public boolean isEqualBufferType(int i_type_value)\r
+       {\r
+               return BUFFER_FORMAT==i_type_value;\r
+       }       \r
+       NyARDoubleMatrix44 _invmat=new NyARDoubleMatrix44();\r
+       /**\r
+        * @param i_width\r
+        * @param i_height\r
+        */\r
+       public NyARColorPatt_PseudoAffine(int i_width, int i_height)\r
+       {               \r
+               this._size=new NyARIntSize(i_width,i_height);\r
+               this._patdata = new int[i_height*i_width];\r
+               this._pixelreader=new NyARRgbPixelReader_INT1D_X8R8G8B8_32(this._patdata,this._size);\r
+               //疑似アフィン変換のパラメタマトリクスを計算します。\r
+               //長方形から計算すると、有効要素がm00,m01,m02,m03,m10,m11,m20,m23,m30になります。\r
+               final NyARDoubleMatrix44 mat=this._invmat;\r
+               mat.m00=0;\r
+               mat.m01=0;\r
+               mat.m02=0;\r
+               mat.m03=1.0;\r
+               mat.m10=0;\r
+               mat.m11=i_width-1;\r
+               mat.m12=0;\r
+               mat.m13=1.0;\r
+               mat.m20=(i_width-1)*(i_height-1);\r
+               mat.m21=i_width-1;\r
+               mat.m22=i_height-1;\r
+               mat.m23=1.0;\r
+               mat.m30=0;\r
+               mat.m31=0;\r
+               mat.m32=i_height-1;\r
+               mat.m33=1.0;\r
+               mat.inverse(mat);\r
+               return;\r
+       }       \r
+\r
+       /**\r
+        * 変換行列と頂点座標から、パラメータを計算\r
+        * o_paramの[0..3]にはXのパラメタ、[4..7]にはYのパラメタを格納する。\r
+        * @param i_vertex\r
+        * @param pa\r
+        * @param pb\r
+        */\r
+       private void calcPara(NyARIntPoint2d[] i_vertex,double[] o_cparam)\r
+       {\r
+               final NyARDoubleMatrix44 invmat=this._invmat;\r
+               double v1,v2,v4;\r
+               //変換行列とベクトルの積から、変換パラメタを計算する。\r
+               v1=i_vertex[0].x;\r
+               v2=i_vertex[1].x;\r
+               v4=i_vertex[3].x;\r
+               \r
+               o_cparam[0]=invmat.m00*v1+invmat.m01*v2+invmat.m02*i_vertex[2].x+invmat.m03*v4;\r
+               o_cparam[1]=invmat.m10*v1+invmat.m11*v2;//m12,m13は0;\r
+               o_cparam[2]=invmat.m20*v1+invmat.m23*v4;//m21,m22は0;\r
+               o_cparam[3]=v1;//m30は1.0で、m31,m32,m33は0\r
+               \r
+               v1=i_vertex[0].y;\r
+               v2=i_vertex[1].y;\r
+               v4=i_vertex[3].y;\r
+\r
+               o_cparam[4]=invmat.m00*v1+invmat.m01*v2+invmat.m02*i_vertex[2].y+invmat.m03*v4;\r
+               o_cparam[5]=invmat.m10*v1+invmat.m11*v2;//m12,m13は0;\r
+               o_cparam[6]=invmat.m20*v1+invmat.m23*v4;//m21,m22は0;\r
+               o_cparam[7]=v1;//m30は1.0で、m31,m32,m33は0\r
+               return;\r
+       }\r
+\r
+       /**\r
+        * 疑似アフィン変換の変換パラメタ\r
+        */\r
+       private double[] _convparam=new double[8];\r
+       \r
+       /**\r
+        * @see INyARColorPatt#pickFromRaster\r
+        */\r
+       public boolean pickFromRaster(INyARRgbRaster image,NyARIntPoint2d[] i_vertexs)throws NyARException\r
+       {\r
+               final double[] conv_param=this._convparam;\r
+           int rx2,ry2;\r
+               rx2=this._size.w;\r
+               ry2=this._size.h;\r
+               int[] rgb_tmp=new int[3];\r
+\r
+               INyARRgbPixelReader reader=image.getRgbPixelReader();\r
+               // 変形先領域の頂点を取得\r
+\r
+               //変換行列から現在の座標系への変換パラメタを作成\r
+               calcPara(i_vertexs,conv_param);// 変換パラメータを求める\r
+               for(int y=0;y<ry2;y++){\r
+                       for(int x=0;x<rx2;x++){\r
+                               final int ttx=(int)((conv_param[0]*x*y+conv_param[1]*x+conv_param[2]*y+conv_param[3])+0.5);\r
+                               final int tty=(int)((conv_param[4]*x*y+conv_param[5]*x+conv_param[6]*y+conv_param[7])+0.5);\r
+                               reader.getPixel((int)ttx,(int)tty,rgb_tmp);\r
+                               this._patdata[x+y*rx2]=(rgb_tmp[0]<<16)|(rgb_tmp[1]<<8)|rgb_tmp[2];                             \r
+                       }\r
+               }\r
+               return true;\r
+       }\r
+}
\ No newline at end of file