| 28 |
package jp.nyatla.nyar4psg; |
package jp.nyatla.nyar4psg; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
import processing.core.*; |
import processing.core.*; |
| 33 |
|
|
| 34 |
import jp.nyatla.nyartoolkit.*; |
import jp.nyatla.nyartoolkit.core.*; |
|
import jp.nyatla.nyartoolkit.core.param.*; |
|
|
import jp.nyatla.nyartoolkit.core.rasterreader.NyARPerspectiveRasterReader; |
|
|
import jp.nyatla.nyartoolkit.core.transmat.*; |
|
|
import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d; |
|
|
import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d; |
|
|
import jp.nyatla.nyartoolkit.core.types.NyARIntSize; |
|
| 35 |
import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44; |
import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44; |
| 36 |
|
|
| 37 |
|
|
| 40 |
* このクラスは、NyARToolkit for Processingのベースクラスです。 |
* このクラスは、NyARToolkit for Processingのベースクラスです。 |
| 41 |
* ARToolkit座標系の環境定数、環境設定機能を継承クラスに対して提供します。 |
* ARToolkit座標系の環境定数、環境設定機能を継承クラスに対して提供します。 |
| 42 |
*/ |
*/ |
| 43 |
class NyARPsgBaseClass |
abstract class NyARPsgBaseClass |
| 44 |
{ |
{ |
| 45 |
/** |
/** |
| 46 |
* nearクリップ面のデフォルト値です。 |
* nearクリップ面のデフォルト値です。 |
| 55 |
* バージョン文字列です。 |
* バージョン文字列です。 |
| 56 |
* NyAR4psgのバージョン情報を示します。 |
* NyAR4psgのバージョン情報を示します。 |
| 57 |
*/ |
*/ |
| 58 |
public final static String VERSION = "NyAR4psg/1.1.7;NyARToolkit for java/3.0.0+;ARToolKit/2.72.1"; |
public final static String VERSION = "NyAR4psg/1.2.0;NyARToolkit for java/4.0.1;ARToolKit/2.72.1"; |
| 59 |
/** 参照するAppletのインスタンスです。*/ |
/** 参照するAppletのインスタンスです。*/ |
| 60 |
protected PApplet _ref_papplet; |
protected PApplet _ref_papplet; |
|
/** ProcessingスタイルのProjectionMatrixです。*/ |
|
|
protected final PMatrix3D _ps_projection=new PMatrix3D(); |
|
| 61 |
/** バックグラウンド用のModelviewMatrixです。*/ |
/** バックグラウンド用のModelviewMatrixです。*/ |
| 62 |
protected final PMatrix3D _ps_background_mv=new PMatrix3D(); |
protected final PMatrix3D _ps_background_mv=new PMatrix3D(); |
| 63 |
|
|
| 64 |
/** ARToolkitパラメータのインスタンスです。*/ |
/** ARToolkitパラメータのインスタンスです。*/ |
|
protected final NyARParam _ar_param=new NyARParam(); |
|
|
protected final NyARFrustum _frustum=new NyARFrustum();; |
|
| 65 |
protected NyAR4PsgConfig _config; |
protected NyAR4PsgConfig _config; |
| 66 |
|
|
| 67 |
/** 入力画像ラスタです。{@link PImage}をラップします。継承クラスで入力画像をセットします。*/ |
|
|
protected PImageRaster _src_raster; |
|
|
/** 画像抽出用のオブジェクトです。{@link #_src_raster}を参照します。*/ |
|
|
protected NyARPerspectiveRasterReader _preader; |
|
| 68 |
|
|
| 69 |
private float _clip_far; |
private float _clip_far; |
| 70 |
private float _clip_near; |
private float _clip_near; |
| 71 |
|
|
| 72 |
/** |
/** |
| 73 |
* コンストラクタです。 |
* コンストラクタです。 |
| 74 |
*/ |
*/ |
| 75 |
protected NyARPsgBaseClass() |
protected NyARPsgBaseClass() |
| 76 |
{ |
{ |
| 77 |
} |
} |
| 78 |
protected void initInstance(PApplet parent,String i_cparam_file, int i_width,int i_height,NyAR4PsgConfig i_config) throws NyARException |
protected void initInstance(PApplet parent,NyAR4PsgConfig i_config) throws NyARException |
| 79 |
{ |
{ |
| 80 |
this._ref_papplet=parent; |
this._ref_papplet=parent; |
| 81 |
this._config=i_config; |
this._config=i_config; |
|
this._src_raster=new PImageRaster(i_width,i_height); |
|
|
this._preader=new NyARPerspectiveRasterReader(this._src_raster.getBufferType()); |
|
|
try{ |
|
|
this._ar_param.loadARParam(this._ref_papplet.createInput(i_cparam_file)); |
|
|
this._ar_param.changeScreenSize(i_width, i_height);//w,hはここでコピー |
|
|
|
|
| 82 |
//ProcessingのprojectionMatrixの計算と、Frustumの計算 |
//ProcessingのprojectionMatrixの計算と、Frustumの計算 |
| 83 |
this.setARClipping(FRUSTUM_DEFAULT_NEAR_CLIP,FRUSTUM_DEFAULT_FAR_CLIP); |
this.setARClipping(FRUSTUM_DEFAULT_NEAR_CLIP,FRUSTUM_DEFAULT_FAR_CLIP); |
|
}catch(NyARException e){ |
|
|
this._ref_papplet.die("Error while setting up NyARToolkit for java", e); |
|
|
} |
|
| 84 |
return; |
return; |
| 85 |
} |
} |
| 86 |
/** |
/** |
| 87 |
* この関数は、ProjectionMatrixを複製して返します。 |
* [readonly]この関数は、Processing形式のProjectionMatrixの参照値を返します。 |
| 88 |
|
* @return |
| 89 |
|
*/ |
| 90 |
|
public abstract PMatrix3D getProjectionMatrix(); |
| 91 |
|
/** |
| 92 |
|
* この関数は、ProjectionMatrixをi_bufへ複製して返します。 |
| 93 |
* @return |
* @return |
| 94 |
* ProjectionMatrixです。 |
* ProjectionMatrixです。 |
| 95 |
*/ |
*/ |
| 96 |
public PMatrix3D getProjectionMatrix() |
public abstract PMatrix3D getProjectionMatrix(PMatrix3D i_buf); |
|
{ |
|
|
return new PMatrix3D(this._ps_projection); |
|
|
} |
|
| 97 |
/** |
/** |
| 98 |
* この関数は、PImageをバックグラウンドへ描画します。PImageはfarclip面+1の部分に描画します。 |
* この関数は、PImageをバックグラウンドへ描画します。PImageはfarclip面+1の部分に描画します。 |
| 99 |
* <div>この関数は、次のコードと等価です。</div> |
* <div>この関数は、次のコードと等価です。</div> |
| 139 |
{ |
{ |
| 140 |
this._clip_far=i_far; |
this._clip_far=i_far; |
| 141 |
this._clip_near=i_near; |
this._clip_near=i_near; |
|
arPerspectiveMat2Projection(this._ar_param,i_near,i_far,this._ps_projection,this._frustum); |
|
| 142 |
this._ps_background_mv.reset(); |
this._ps_background_mv.reset(); |
| 143 |
this._ps_background_mv.translate(0,0,-i_far); |
this._ps_background_mv.translate(0,0,-i_far); |
| 144 |
} |
} |
| 175 |
*/ |
*/ |
| 176 |
public void setARPerspective() |
public void setARPerspective() |
| 177 |
{ |
{ |
| 178 |
this.setPerspective(this._ps_projection); |
this.setPerspective(this.getProjectionMatrix()); |
| 179 |
} |
} |
| 180 |
/** |
/** |
| 181 |
* この関数は、ProjectionMatrixをProcessingにセットします。 |
* この関数は、ProjectionMatrixをProcessingにセットします。 |
| 241 |
o_out[14]=i_in.m23; |
o_out[14]=i_in.m23; |
| 242 |
o_out[15]=i_in.m33; |
o_out[15]=i_in.m33; |
| 243 |
} |
} |
| 244 |
|
protected static void nyarMat2PsMat(NyARDoubleMatrix44 i_src,PMatrix3D i_dst) |
|
private static void arPerspectiveMat2Projection(NyARParam i_param,float i_near,float i_far,PMatrix3D o_projection,NyARFrustum o_frustum) |
|
|
{ |
|
|
NyARDoubleMatrix44 tmp=new NyARDoubleMatrix44(); |
|
|
NyARIntSize s=i_param.getScreenSize(); |
|
|
i_param.getPerspectiveProjectionMatrix().makeCameraFrustumRH(s.w,s.h,i_near,i_far,tmp); |
|
|
o_projection.m00=(float)(tmp.m00); |
|
|
o_projection.m01=(float)(tmp.m01); |
|
|
o_projection.m02=(float)(tmp.m02); |
|
|
o_projection.m03=(float)(tmp.m03); |
|
|
o_projection.m10=(float)(tmp.m10); |
|
|
o_projection.m11=(float)(tmp.m11); |
|
|
o_projection.m12=(float)(tmp.m12); |
|
|
o_projection.m13=(float)(tmp.m13); |
|
|
o_projection.m20=(float)(tmp.m20); |
|
|
o_projection.m21=(float)(tmp.m21); |
|
|
o_projection.m22=(float)(tmp.m22); |
|
|
o_projection.m23=(float)(tmp.m23); |
|
|
o_projection.m30=(float)(tmp.m30); |
|
|
o_projection.m31=(float)(tmp.m31); |
|
|
o_projection.m32=(float)(tmp.m32); |
|
|
o_projection.m33=(float)(tmp.m33); |
|
|
o_frustum.setValue(tmp, s.w, s.h); |
|
|
} |
|
|
protected static void matResult2GLArray(NyARTransMatResult i_src,double[] o_gl_array) |
|
| 245 |
{ |
{ |
| 246 |
o_gl_array[0 + 0 * 4] = i_src.m00; |
i_dst.m00=(float)(i_src.m00); |
| 247 |
o_gl_array[0 + 1 * 4] = i_src.m01; |
i_dst.m01=(float)(i_src.m01); |
| 248 |
o_gl_array[0 + 2 * 4] = i_src.m02; |
i_dst.m02=(float)(i_src.m02); |
| 249 |
o_gl_array[0 + 3 * 4] = i_src.m03; |
i_dst.m03=(float)(i_src.m03); |
| 250 |
o_gl_array[1 + 0 * 4] = -i_src.m10; |
i_dst.m10=(float)(i_src.m10); |
| 251 |
o_gl_array[1 + 1 * 4] = -i_src.m11; |
i_dst.m11=(float)(i_src.m11); |
| 252 |
o_gl_array[1 + 2 * 4] = -i_src.m12; |
i_dst.m12=(float)(i_src.m12); |
| 253 |
o_gl_array[1 + 3 * 4] = -i_src.m13; |
i_dst.m13=(float)(i_src.m13); |
| 254 |
o_gl_array[2 + 0 * 4] = -i_src.m20; |
i_dst.m20=(float)(i_src.m20); |
| 255 |
o_gl_array[2 + 1 * 4] = -i_src.m21; |
i_dst.m21=(float)(i_src.m21); |
| 256 |
o_gl_array[2 + 2 * 4] = -i_src.m22; |
i_dst.m22=(float)(i_src.m22); |
| 257 |
o_gl_array[2 + 3 * 4] = -i_src.m23; |
i_dst.m23=(float)(i_src.m23); |
| 258 |
o_gl_array[3 + 0 * 4] = 0.0; |
i_dst.m30=(float)(i_src.m30); |
| 259 |
o_gl_array[3 + 1 * 4] = 0.0; |
i_dst.m31=(float)(i_src.m31); |
| 260 |
o_gl_array[3 + 2 * 4] = 0.0; |
i_dst.m32=(float)(i_src.m32); |
| 261 |
o_gl_array[3 + 3 * 4] = 1.0; |
i_dst.m33=(float)(i_src.m33); |
| 262 |
} |
} |
| 263 |
|
|
| 264 |
/** |
/** |
| 265 |
* 左手系変換用の行列 |
* 左手系変換用の行列 |
| 266 |
*/ |
*/ |
| 299 |
o_pmatrix.apply(_lh_mat); |
o_pmatrix.apply(_lh_mat); |
| 300 |
} |
} |
| 301 |
} |
} |
| 302 |
/** |
protected static void matResult2GLArray(NyARDoubleMatrix44 i_src,double[] o_gl_array) |
|
* この関数は、スクリーン座標i_x,i_yを、マーカ座標系に変換します。 |
|
|
* @param i_mat |
|
|
* @param i_x |
|
|
* @param i_y |
|
|
* @return |
|
|
*/ |
|
|
protected PVector screen2MarkerCoordSystem(NyARDoubleMatrix44 i_mat,int i_x,int i_y) |
|
| 303 |
{ |
{ |
| 304 |
PVector ret=new PVector(); |
o_gl_array[0 + 0 * 4] = i_src.m00; |
| 305 |
NyARDoublePoint3d tmp=new NyARDoublePoint3d(); |
o_gl_array[0 + 1 * 4] = i_src.m01; |
| 306 |
this._frustum.unProjectOnMatrix(i_x, i_y,i_mat,tmp); |
o_gl_array[0 + 2 * 4] = i_src.m02; |
| 307 |
ret.x=(float)tmp.x; |
o_gl_array[0 + 3 * 4] = i_src.m03; |
| 308 |
ret.y=(float)tmp.y; |
o_gl_array[1 + 0 * 4] = -i_src.m10; |
| 309 |
ret.z=(float)tmp.z; |
o_gl_array[1 + 1 * 4] = -i_src.m11; |
| 310 |
if(this._config._coordinate_system==NyAR4PsgConfig.CS_LEFT_HAND){ |
o_gl_array[1 + 2 * 4] = -i_src.m12; |
| 311 |
ret.x*=-1; |
o_gl_array[1 + 3 * 4] = -i_src.m13; |
| 312 |
} |
o_gl_array[2 + 0 * 4] = -i_src.m20; |
| 313 |
return ret; |
o_gl_array[2 + 1 * 4] = -i_src.m21; |
| 314 |
} |
o_gl_array[2 + 2 * 4] = -i_src.m22; |
| 315 |
/** |
o_gl_array[2 + 3 * 4] = -i_src.m23; |
| 316 |
* PImageをラップしたラスタから画像を得ます。 |
o_gl_array[3 + 0 * 4] = 0.0; |
| 317 |
* @param i_mat |
o_gl_array[3 + 1 * 4] = 0.0; |
| 318 |
* @param i_x1 |
o_gl_array[3 + 2 * 4] = 0.0; |
| 319 |
* @param i_y1 |
o_gl_array[3 + 3 * 4] = 1.0; |
|
* @param i_x2 |
|
|
* @param i_y2 |
|
|
* @param i_x3 |
|
|
* @param i_y3 |
|
|
* @param i_x4 |
|
|
* @param i_y4 |
|
|
* @param i_out_w_pix |
|
|
* @param i_out_h_pix |
|
|
* @return |
|
|
*/ |
|
|
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) |
|
|
{ |
|
|
//WrapRasterの内容チェック |
|
|
if(!this._src_raster.hasBuffer()){ |
|
|
this._ref_papplet.die("_rel_detector is null.(Function detect() was never called. )"); |
|
|
} |
|
|
PImage img=new PImage(i_out_w_pix,i_out_h_pix); |
|
|
img.parent=this._ref_papplet; |
|
|
try{ |
|
|
NyARDoublePoint3d[] pos=NyARDoublePoint3d.createArray(4); |
|
|
i_mat.transform3d(i_x1, i_y1,0, pos[1]); |
|
|
i_mat.transform3d(i_x2, i_y2,0, pos[0]); |
|
|
i_mat.transform3d(i_x3, i_y3,0, pos[3]); |
|
|
i_mat.transform3d(i_x4, i_y4,0, pos[2]); |
|
|
//4頂点を作る。 |
|
|
NyARDoublePoint2d[] pos2=NyARDoublePoint2d.createArray(4); |
|
|
for(int i=3;i>=0;i--){ |
|
|
this._frustum.project(pos[i],pos2[i]); |
|
|
} |
|
|
PImageRaster out_raster=new PImageRaster(i_out_w_pix,i_out_h_pix); |
|
|
out_raster.wrapBuffer(img); |
|
|
if(!this._preader.read4Point(this._src_raster,pos2,0,0,1,out_raster)) |
|
|
{ |
|
|
throw new Exception("this._preader.read4Point failed."); |
|
|
} |
|
|
return img; |
|
|
}catch(Exception e){ |
|
|
e.printStackTrace(); |
|
|
this._ref_papplet.die("Exception occurred at MultiARTookitMarker.pickupImage"); |
|
|
return null; |
|
|
} |
|
|
} |
|
|
/** |
|
|
* この関数は、スクリーン座標を撮像点座標に変換します。 |
|
|
* 撮像点の座標系は、カメラ座標系になります。 |
|
|
* <p>公式 - |
|
|
* この関数は、gluUnprojectのビューポートとモデルビュー行列を固定したものです。 |
|
|
* 公式は、以下の物使用しました。 |
|
|
* http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml |
|
|
* ARToolKitの座標系に合せて計算するため、OpenGLのunProjectとはix,iyの与え方が違います。画面上の座標をそのまま与えてください。 |
|
|
* </p> |
|
|
* @param ix |
|
|
* スクリーン上の座標 |
|
|
* @param iy |
|
|
* 画像上の座標 |
|
|
* @param o_point_on_screen |
|
|
* 撮像点座標 |
|
|
*/ |
|
|
/* public final PVector unProject(double ix,double iy) |
|
|
{ |
|
|
double n=(this._frustum_rh.m23/(this._frustum_rh.m22-1)); |
|
|
NyARDoubleMatrix44 m44=this._inv_frustum_rh; |
|
|
double v1=(this._screen_size.w-ix-1)*2/this._screen_size.w-1.0;//ARToolKitのFrustramに合せてる。 |
|
|
double v2=(this._screen_size.h-iy-1)*2/this._screen_size.h-1.0; |
|
|
double v3=2*n-1.0; |
|
|
double b=1/(m44.m30*v1+m44.m31*v2+m44.m32*v3+m44.m33); |
|
|
o_point_on_screen.x=(m44.m00*v1+m44.m01*v2+m44.m02*v3+m44.m03)*b; |
|
|
o_point_on_screen.y=(m44.m10*v1+m44.m11*v2+m44.m12*v3+m44.m13)*b; |
|
|
o_point_on_screen.z=(m44.m20*v1+m44.m21*v2+m44.m22*v3+m44.m23)*b; |
|
|
return; |
|
|
} |
|
|
*/ /** |
|
|
* この関数は、スクリーン上の点と原点を結ぶ直線と、任意姿勢の平面の交差点を、カメラの座標系で取得します。 |
|
|
* この座標は、カメラ座標系です。 |
|
|
* @param ix |
|
|
* スクリーン上の座標 |
|
|
* @param iy |
|
|
* スクリーン上の座標 |
|
|
* @param i_mat |
|
|
* 平面の姿勢行列です。 |
|
|
* @param o_pos |
|
|
* 結果を受け取るオブジェクトです。 |
|
|
*/ |
|
|
/* public final PVector unProjectOnCamera(double ix,double iy,PMatrix3D i_mat) |
|
|
{ |
|
|
//画面→撮像点 |
|
|
this.unProject(ix,iy,o_pos); |
|
|
//撮像点→カメラ座標系 |
|
|
double nx=i_mat.m02; |
|
|
double ny=i_mat.m12; |
|
|
double nz=i_mat.m22; |
|
|
double mx=i_mat.m03; |
|
|
double my=i_mat.m13; |
|
|
double mz=i_mat.m23; |
|
|
double t=(nx*mx+ny*my+nz*mz)/(nx*o_pos.x+ny*o_pos.y+nz*o_pos.z); |
|
|
o_pos.x=t*o_pos.x; |
|
|
o_pos.y=t*o_pos.y; |
|
|
o_pos.z=t*o_pos.z; |
|
| 320 |
} |
} |
| 321 |
*/ /** |
|
| 322 |
* 画面上の点と原点を結ぶ直線と任意姿勢の平面の交差点を、平面の座標系で取得します。 |
} |
|
* ARToolKitの本P175周辺の実装と同じです。 |
|
|
* @param ix |
|
|
* スクリーン上の座標 |
|
|
* @param iy |
|
|
* スクリーン上の座標 |
|
|
* @param i_mat |
|
|
* 平面の姿勢行列です。 |
|
|
* @param o_pos |
|
|
* 結果を受け取るオブジェクトです。 |
|
|
* @return |
|
|
* 計算に成功すると、trueを返します。 |
|
|
*/ |
|
|
/* public final PVector unProjectOnMatrix(double ix,double iy,PMatrix3D i_mat) |
|
|
{ |
|
|
//交点をカメラ座標系で計算 |
|
|
unProjectOnCamera(ix,iy,i_mat,o_pos); |
|
|
//座標系の変換 |
|
|
NyARDoubleMatrix44 m=new NyARDoubleMatrix44(); |
|
|
if(!m.inverse(i_mat)){ |
|
|
return false; |
|
|
} |
|
|
m.transform3d(o_pos, o_pos); |
|
|
return true; |
|
|
} |
|
|
*/ /** |
|
|
* カメラ座標系の点を、スクリーン座標の点へ変換します。 |
|
|
* @param i_x |
|
|
* カメラ座標系の点 |
|
|
* @param i_y |
|
|
* カメラ座標系の点 |
|
|
* @param i_z |
|
|
* カメラ座標系の点 |
|
|
* @param o_pos2d |
|
|
* 結果を受け取るオブジェクトです。 |
|
|
*/ |
|
|
/* public final PVector project(double i_x,double i_y,double i_z) |
|
|
{ |
|
|
NyARDoubleMatrix44 m=this._frustum_rh; |
|
|
double v3_1=1/i_z*m.m32; |
|
|
double w=this._screen_size.w; |
|
|
double h=this._screen_size.h; |
|
|
o_pos2d.x=w-(1+(i_x*m.m00+i_z*m.m02)*v3_1)*w/2; |
|
|
o_pos2d.y=h-(1+(i_y*m.m11+i_z*m.m12)*v3_1)*h/2; |
|
|
return; |
|
|
} |
|
|
*/} |
|
| 323 |
|
|