| 36 |
|
|
| 37 |
import jp.nyatla.nyartoolkit.*; |
import jp.nyatla.nyartoolkit.*; |
| 38 |
import jp.nyatla.nyartoolkit.core.param.*; |
import jp.nyatla.nyartoolkit.core.param.*; |
| 39 |
|
import jp.nyatla.nyartoolkit.core.rasterreader.NyARPerspectiveRasterReader; |
| 40 |
import jp.nyatla.nyartoolkit.core.transmat.*; |
import jp.nyatla.nyartoolkit.core.transmat.*; |
| 41 |
|
import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; |
| 42 |
import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; |
import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; |
| 43 |
import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44; |
import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44; |
| 44 |
|
|
| 50 |
*/ |
*/ |
| 51 |
class NyARPsgBaseClass |
class NyARPsgBaseClass |
| 52 |
{ |
{ |
| 53 |
/** |
|
| 54 |
* 定数値です。この値はコンストラクタで使います。 |
|
|
* RightHand系の座標を構築します。 |
|
|
* RightHand座標系は、ARToolKitと互換性のある座標系ですが、Processingの座標系と互換性がないため、text等の出力が鏡像になります。 |
|
|
*/ |
|
|
public final static int CS_RIGHT_HAND=0; |
|
|
/** |
|
|
* 定数値です。この値はコンストラクタで使います。 |
|
|
* LeftHand座標系を構築します。 |
|
|
* RightHand座標系は、ARToolKitと互換性のない座標系ですが、Processingの座標系と互換性があります。 |
|
|
* processing関数で描画する場合は、こちらを選択してください。 |
|
|
*/ |
|
|
public final static int CS_LEFT_HAND =1; |
|
| 55 |
/** |
/** |
| 56 |
* バージョン文字列です。 |
* バージョン文字列です。 |
| 57 |
* NyAR4psgのバージョン情報を示します。 |
* NyAR4psgのバージョン情報を示します。 |
| 58 |
*/ |
*/ |
| 59 |
public final static String VERSION = "NyAR4psg/0.9.0;NyARToolkit for java/3.0.0+;ARToolKit/2.72.1"; |
public final static String VERSION = "NyAR4psg/1.0.1;NyARToolkit for java/3.0.0+;ARToolKit/2.72.1"; |
|
/** ProcessingスタイルのProjectionMatrixです。*/ |
|
|
protected PMatrix3D _ps_projection=new PMatrix3D(); |
|
| 60 |
/** 参照するAppletのインスタンスです。*/ |
/** 参照するAppletのインスタンスです。*/ |
| 61 |
protected PApplet _ref_papplet; |
protected PApplet _ref_papplet; |
| 62 |
|
/** ProcessingスタイルのProjectionMatrixです。*/ |
| 63 |
|
protected final PMatrix3D _ps_projection=new PMatrix3D(); |
| 64 |
/** ARToolkitパラメータのインスタンスです。*/ |
/** ARToolkitパラメータのインスタンスです。*/ |
| 65 |
protected NyARParam _ar_param; |
protected final NyARParam _ar_param=new NyARParam(); |
| 66 |
protected NyARFrustum _frustum; |
protected final NyARFrustum _frustum=new NyARFrustum();; |
| 67 |
protected int _coord_system; |
protected NyAR4PsgConfig _config; |
| 68 |
|
|
| 69 |
|
/** 入力画像ラスタです。{@link PImage}をラップします。継承クラスで入力画像をセットします。*/ |
| 70 |
|
protected PImageRaster _src_raster; |
| 71 |
|
/** 画像抽出用のオブジェクトです。{@link #_src_raster}を参照します。*/ |
| 72 |
|
protected NyARPerspectiveRasterReader _preader; |
| 73 |
|
private int _g_type; |
| 74 |
/** |
/** |
| 75 |
* コンストラクタです。 |
* コンストラクタです。 |
| 76 |
*/ |
*/ |
| 77 |
protected NyARPsgBaseClass() |
protected NyARPsgBaseClass() |
| 78 |
{ |
{ |
| 79 |
} |
} |
| 80 |
protected void initInstance(PApplet parent,String i_cparam_file, int i_width,int i_height,int i_coord_system) |
protected void initInstance(PApplet parent,String i_cparam_file, int i_width,int i_height,NyAR4PsgConfig i_config) throws NyARException |
| 81 |
{ |
{ |
|
checkCoordinateSystemRange(parent,i_coord_system); |
|
| 82 |
this._ref_papplet=parent; |
this._ref_papplet=parent; |
| 83 |
this._coord_system=i_coord_system; |
this._config=i_config; |
| 84 |
|
this._src_raster=new PImageRaster(i_width,i_height); |
| 85 |
|
this._preader=new NyARPerspectiveRasterReader(this._src_raster.getBufferType()); |
| 86 |
try{ |
try{ |
| 87 |
this._frustum=new NyARFrustum(); |
this._g_type=getGraphicsType(parent.g); |
|
this._ar_param=new NyARParam(); |
|
| 88 |
this._ar_param.loadARParam(this._ref_papplet.createInput(i_cparam_file)); |
this._ar_param.loadARParam(this._ref_papplet.createInput(i_cparam_file)); |
| 89 |
this._ar_param.changeScreenSize(i_width, i_height); |
this._ar_param.changeScreenSize(i_width, i_height); |
| 90 |
|
|
| 97 |
} |
} |
| 98 |
private final static double view_distance_min = 100; |
private final static double view_distance_min = 100; |
| 99 |
private final static double view_distance_max = 100000; |
private final static double view_distance_max = 100000; |
| 100 |
private final static void checkCoordinateSystemRange(PApplet i_pa,int i_cs) |
private final static int GT_P3D=0; |
| 101 |
|
private final static int GT_OPENGL=1; |
| 102 |
|
/** |
| 103 |
|
* {@link PGraphics}をグラフィクス定数に変換します。 |
| 104 |
|
* @param i_g |
| 105 |
|
* @return |
| 106 |
|
* @throws NyARException |
| 107 |
|
*/ |
| 108 |
|
private static int getGraphicsType(PGraphics i_g) throws NyARException |
| 109 |
{ |
{ |
| 110 |
switch(i_cs){ |
String n=i_g.getClass().getName(); |
| 111 |
case NyARPsgBaseClass.CS_LEFT_HAND: |
if(n.compareTo("processing.opengl.PGraphicsOpenGL")==0){ |
| 112 |
case NyARPsgBaseClass.CS_RIGHT_HAND: |
return GT_OPENGL; |
| 113 |
return; |
}else if(n.compareTo("processing.core.PGraphics3D")==0){ |
| 114 |
default: |
return GT_P3D; |
|
i_pa.die("Please set constant CS_LEFT_HAND or CS_RIGHT_HAND."); |
|
| 115 |
} |
} |
| 116 |
|
throw new NyARException("Unknown Graphics"); |
| 117 |
|
|
| 118 |
} |
} |
| 119 |
|
|
| 120 |
|
|
| 121 |
private float[] _tmpf=new float[16]; |
private float[] _tmpf=new float[16]; |
| 122 |
|
|
| 123 |
/** |
/** |
| 151 |
//ProjectionMatrixの設定 |
//ProjectionMatrixの設定 |
| 152 |
g.projection.set(i_projection); |
g.projection.set(i_projection); |
| 153 |
//OpenGLの時はちょっと細工 |
//OpenGLの時はちょっと細工 |
| 154 |
if(this._ref_papplet.g instanceof PGraphicsOpenGL) |
if(this._g_type==GT_OPENGL) |
| 155 |
{ |
{ |
| 156 |
GL gl=((PGraphicsOpenGL)g).gl; |
GL gl=((PGraphicsOpenGL)g).gl; |
| 157 |
gl.glMatrixMode(GL.GL_PROJECTION); |
gl.glMatrixMode(GL.GL_PROJECTION); |
| 252 |
* @param i_mode |
* @param i_mode |
| 253 |
* @param o_pmatrix |
* @param o_pmatrix |
| 254 |
*/ |
*/ |
| 255 |
protected static void matResult2PMatrix3D(NyARTransMatResult i_src,int i_mode,PMatrix3D o_pmatrix) |
protected static void matResult2PMatrix3D(NyARDoubleMatrix44 i_src,int i_mode,PMatrix3D o_pmatrix) |
| 256 |
{ |
{ |
| 257 |
o_pmatrix.m00 = (float)i_src.m00; |
o_pmatrix.m00 = (float)i_src.m00; |
| 258 |
o_pmatrix.m01 = (float)i_src.m01; |
o_pmatrix.m01 = (float)i_src.m01; |
| 270 |
o_pmatrix.m31 = 0.0f; |
o_pmatrix.m31 = 0.0f; |
| 271 |
o_pmatrix.m32 = 0.0f; |
o_pmatrix.m32 = 0.0f; |
| 272 |
o_pmatrix.m33 = 1.0f; |
o_pmatrix.m33 = 1.0f; |
| 273 |
if(i_mode==CS_LEFT_HAND) |
if(i_mode==NyAR4PsgConfig.CS_LEFT_HAND) |
| 274 |
{ |
{ |
| 275 |
o_pmatrix.apply(_lh_mat); |
o_pmatrix.apply(_lh_mat); |
| 276 |
} |
} |
| 278 |
/** |
/** |
| 279 |
* この関数は、i_mat平面から、自由変形した画像を取得します。 |
* この関数は、i_mat平面から、自由変形した画像を取得します。 |
| 280 |
* @param i_mat |
* @param i_mat |
|
* @param i_id |
|
| 281 |
* @param i_x |
* @param i_x |
| 282 |
* @param i_y |
* @param i_y |
| 283 |
* @return |
* @return |
| 290 |
ret.x=(float)tmp.x; |
ret.x=(float)tmp.x; |
| 291 |
ret.y=(float)tmp.y; |
ret.y=(float)tmp.y; |
| 292 |
ret.z=(float)tmp.z; |
ret.z=(float)tmp.z; |
| 293 |
if(this._coord_system==CS_LEFT_HAND){ |
if(this._config._coordinate_system==NyAR4PsgConfig.CS_LEFT_HAND){ |
| 294 |
ret.x*=-1; |
ret.x*=-1; |
| 295 |
} |
} |
| 296 |
return ret; |
return ret; |
| 297 |
|
} |
| 298 |
|
/** |
| 299 |
|
* PImageをラップしたラスタから画像を得ます。 |
| 300 |
|
* @param i_mat |
| 301 |
|
* @param i_x1 |
| 302 |
|
* @param i_y1 |
| 303 |
|
* @param i_x2 |
| 304 |
|
* @param i_y2 |
| 305 |
|
* @param i_x3 |
| 306 |
|
* @param i_y3 |
| 307 |
|
* @param i_x4 |
| 308 |
|
* @param i_y4 |
| 309 |
|
* @param i_out_w_pix |
| 310 |
|
* @param i_out_h_pix |
| 311 |
|
* @return |
| 312 |
|
*/ |
| 313 |
|
protected PImage pickupMarkerImage(NyARDoubleMatrix44 i_mat,int i_x1,int i_y1,int i_x2,int i_y2,int i_x3,int i_y3,int i_x4,int i_y4,int i_out_w_pix,int i_out_h_pix) |
| 314 |
|
{ |
| 315 |
|
//WrapRasterの内容チェック |
| 316 |
|
if(!this._src_raster.hasBuffer()){ |
| 317 |
|
this._ref_papplet.die("_rel_detector is null.(Function detect() was never called. )"); |
| 318 |
|
} |
| 319 |
|
PImage img=new PImage(i_out_w_pix,i_out_h_pix); |
| 320 |
|
img.parent=this._ref_papplet; |
| 321 |
|
try{ |
| 322 |
|
NyARDoublePoint3d[] pos=NyARDoublePoint3d.createArray(4); |
| 323 |
|
i_mat.transform3d(i_x1, i_y1,0, pos[1]); |
| 324 |
|
i_mat.transform3d(i_x2, i_y2,0, pos[0]); |
| 325 |
|
i_mat.transform3d(i_x3, i_y3,0, pos[3]); |
| 326 |
|
i_mat.transform3d(i_x4, i_y4,0, pos[2]); |
| 327 |
|
//4頂点を作る。 |
| 328 |
|
NyARDoublePoint2d[] pos2=NyARDoublePoint2d.createArray(4); |
| 329 |
|
for(int i=3;i>=0;i--){ |
| 330 |
|
this._frustum.project(pos[i],pos2[i]); |
| 331 |
|
} |
| 332 |
|
PImageRaster out_raster=new PImageRaster(i_out_w_pix,i_out_h_pix); |
| 333 |
|
out_raster.wrapBuffer(img); |
| 334 |
|
if(!this._preader.read4Point(this._src_raster,pos2,0,0,1,out_raster)) |
| 335 |
|
{ |
| 336 |
|
throw new Exception("this._preader.read4Point failed."); |
| 337 |
|
} |
| 338 |
|
return img; |
| 339 |
|
}catch(Exception e){ |
| 340 |
|
e.printStackTrace(); |
| 341 |
|
this._ref_papplet.die("Exception occurred at MultiARTookitMarker.pickupImage"); |
| 342 |
|
return null; |
| 343 |
|
} |
| 344 |
} |
} |
| 345 |
/** |
/** |
| 346 |
* この関数は、スクリーン座標を撮像点座標に変換します。 |
* この関数は、スクリーン座標を撮像点座標に変換します。 |