チェンジセット 3669: as3/FLARToolKit

差分発生行の前後
無視リスト:
コミット日時:
2010/04/02 19:31:35 (3 年前)
コミッタ:
rokubou
ログメッセージ:

CallBack?クラスを外部ファイルに移動

ファイル:

凡例:

変更無し
追加
削除
更新
コピー
移動
  • as3/FLARToolKit/trunk/src/org/libspark/flartoolkit/detector/FLARMultiMarkerDetector.as

    r3656 r3669  
    1 /*  
     1/*  
    22 * PROJECT: FLARToolKit 
    33 * -------------------------------------------------------------------------------- 
     
    2929package org.libspark.flartoolkit.detector 
    3030{ 
     31        import flash.display.BitmapData; 
     32         
    3133        import jp.nyatla.nyartoolkit.as3.core.*; 
    3234        import jp.nyatla.nyartoolkit.as3.*; 
     
    4749        import org.libspark.flartoolkit.core.raster.rgb.*; 
    4850        import org.libspark.flartoolkit.core.transmat.*; 
     51        import org.libspark.flartoolkit.detector.multi.*; 
    4952 
    5053 
     
    5760        { 
    5861 
    59                 private var _detect_cb:DetectSquareCB; 
     62                private var _detect_cb:FLARDetectSquareCB; 
    6063                public static const AR_SQUARE_MAX:int = 300; 
    6164                private var _is_continue:Boolean = false; 
     
    98101                        var cw:int = i_ref_code[0].getWidth(); 
    99102                        var ch:int = i_ref_code[0].getHeight(); 
    100                          
    101                         // 枠線の割合(ARToolKit標準と同じなら、25 -> 1.0系と数値の扱いが異なるので注意!) 
    102                         var markerWidthByDec:Number = 25; 
    103                         var markerHeightByDec:Number = 25; 
    104                          
     103                        // 枠線の割合 
     104                        var markerWidthByDec:Number = i_ref_code[0].markerPercentWidth; 
     105                        var markerHeightByDec:Number = i_ref_code[0].markerPercentHeight; 
     106 
    105107                        //評価パターンのホルダを作成 
    106108                        // NyARColorPatt_Perspective_O2のパラメータ 
     
    115117                         
    116118                        //detectMarkerのコールバック関数 
    117                         this._detect_cb=new DetectSquareCB(patt,i_ref_code,i_number_of_code,i_ref_param); 
    118                          
     119                        this._detect_cb=new FLARDetectSquareCB( 
     120                               patt,i_ref_code,i_number_of_code,i_ref_param); 
    119121                        this._transmat = new NyARTransMat(i_ref_param); 
    120122                        //NyARToolkitプロファイル 
    121123                        this._square_detect =new FLARSquareContourDetector(i_ref_param.getDistortionFactor(),i_ref_param.getScreenSize()); 
    122                         this._tobin_filter=new FLARRasterFilter_Threshold(100); 
     124                        this._tobin_filter = new FLARRasterFilter_Threshold(100); 
    123125 
    124126                        //実サイズ保存 
     
    176178                public function getTransformMatrix(i_index:int, o_result:FLARTransMatResult):void 
    177179                { 
    178                         var result:NyARDetectMarkerResult = this._detect_cb.result_stack.getItem(i_index); 
     180                        var result:FLARDetectMarkerResult = this._detect_cb.result_stack.getItem(i_index); 
    179181                        // 一番一致したマーカーの位置とかその辺を計算 
    180182                        if (_is_continue) { 
     
    220222                        this._is_continue = i_is_continue; 
    221223                } 
    222  
     224                 
     225                // ▼追加 2値化した画像 
     226                public function get thresholdedBitmapData () :BitmapData { 
     227                        try { 
     228                                return BitmapData(FLARBinRaster(this._bin_raster).getBuffer()); 
     229                        } catch (e:Error) { 
     230                                return null; 
     231                        } 
     232                         
     233                        return null; 
     234                } 
     235                 
     236                // ▼追加 結果 
     237                public function getResult(i_index:int):FLARDetectMarkerResult 
     238                { 
     239                        return this._detect_cb.result_stack.getItem(i_index); 
     240                } 
     241                 
     242                public function getbcthreshold():int 
     243                { 
     244                        return this._detect_cb.threshold; 
     245                } 
     246                 
     247                /** 
     248                 * i_indexのマーカーの方位を返します。 
     249                 *  
     250                 * @param i_index 
     251                 * マーカーのインデックス番号を指定します。 直前に実行したdetectMarkerLiteの戻り値未満かつ0以上である必要があります。 
     252                 * @return 0,1,2,3の何れかを返します。 
     253                 */ 
     254                public function getDirection(i_index:int):int 
     255                { 
     256                        return this._detect_cb.result_stack.getItem(i_index).direction; 
     257                } 
    223258        } 
    224259} 
    225  
    226 import jp.nyatla.nyartoolkit.as3.core.match.*; 
    227 import jp.nyatla.nyartoolkit.as3.core.pickup.*; 
    228 import jp.nyatla.nyartoolkit.as3.core.squaredetect.*; 
    229 import jp.nyatla.nyartoolkit.as3.*; 
    230 import jp.nyatla.nyartoolkit.as3.core.types.stack.*; 
    231 import jp.nyatla.nyartoolkit.as3.core.match.*; 
    232 import jp.nyatla.nyartoolkit.as3.core.param.*; 
    233 import jp.nyatla.nyartoolkit.as3.core.raster.rgb.*; 
    234 import jp.nyatla.nyartoolkit.as3.detector.*; 
    235 import jp.nyatla.nyartoolkit.as3.core.types.*; 
    236 import org.libspark.flartoolkit.core.* 
    237 /** 
    238  * detectMarkerのコールバック関数 
    239  */ 
    240 class DetectSquareCB implements NyARSquareContourDetector_IDetectMarkerCallback 
    241 { 
    242         //公開プロパティ 
    243         public var result_stack:NyARDetectMarkerResultStack=new NyARDetectMarkerResultStack(NyARDetectMarker.AR_SQUARE_MAX); 
    244         //参照インスタンス 
    245         public var _ref_raster:INyARRgbRaster; 
    246         //所有インスタンス 
    247         private var _inst_patt:INyARColorPatt; 
    248         private var _deviation_data:NyARMatchPattDeviationColorData; 
    249         private var _match_patt:Vector.<NyARMatchPatt_Color_WITHOUT_PCA>; 
    250         private var __detectMarkerLite_mr:NyARMatchPattResult=new NyARMatchPattResult(); 
    251         private var _coordline:NyARCoord2Linear; 
    252          
    253         public function DetectSquareCB(i_inst_patt:INyARColorPatt, i_ref_code:Vector.<FLARCode>, i_num_of_code:int, i_param:NyARParam) 
    254         { 
    255                 var cw:int = i_ref_code[0].getWidth(); 
    256                 var ch:int = i_ref_code[0].getHeight(); 
    257  
    258                 this._inst_patt=i_inst_patt; 
    259                 this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor()); 
    260                 this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch); 
    261  
    262                 //NyARMatchPatt_Color_WITHOUT_PCA[]の作成 
    263                 this._match_patt=new Vector.<NyARMatchPatt_Color_WITHOUT_PCA>(i_num_of_code); 
    264                 this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]); 
    265                 for (var i:int = 1; i < i_num_of_code; i++){ 
    266                         //解像度チェック 
    267                         if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) { 
    268                                 throw new NyARException(); 
    269                         } 
    270                         this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]); 
    271                 } 
    272                 return; 
    273         } 
    274         private var __tmp_vertex:Vector.<NyARIntPoint2d>=NyARIntPoint2d.createArray(4); 
    275         /** 
    276          * 矩形が見付かるたびに呼び出されます。 
    277          * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。 
    278          */ 
    279         public function onSquareDetect(i_sender:NyARSquareContourDetector,i_coordx:Vector.<int>,i_coordy:Vector.<int>,i_coor_num:int ,i_vertex_index:Vector.<int>):void 
    280         { 
    281                 var mr:NyARMatchPattResult=this.__detectMarkerLite_mr; 
    282                 //輪郭座標から頂点リストに変換 
    283                 var vertex:Vector.<NyARIntPoint2d>=this.__tmp_vertex; 
    284                 vertex[0].x=i_coordx[i_vertex_index[0]]; 
    285                 vertex[0].y=i_coordy[i_vertex_index[0]]; 
    286                 vertex[1].x=i_coordx[i_vertex_index[1]]; 
    287                 vertex[1].y=i_coordy[i_vertex_index[1]]; 
    288                 vertex[2].x=i_coordx[i_vertex_index[2]]; 
    289                 vertex[2].y=i_coordy[i_vertex_index[2]]; 
    290                 vertex[3].x=i_coordx[i_vertex_index[3]]; 
    291                 vertex[3].y=i_coordy[i_vertex_index[3]]; 
    292          
    293                 //画像を取得 
    294                 if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){ 
    295                         return; 
    296                 } 
    297                 //取得パターンをカラー差分データに変換して評価する。 
    298                 this._deviation_data.setRaster(this._inst_patt); 
    299  
    300                 //最も一致するパターンを割り当てる。 
    301                 var square_index:int,direction:int; 
    302                 var confidence:Number; 
    303                 this._match_patt[0].evaluate(this._deviation_data,mr); 
    304                 square_index=0; 
    305                 direction=mr.direction; 
    306                 confidence=mr.confidence; 
    307                 //2番目以降 
    308                 var i:int; 
    309                 for(i=1;i<this._match_patt.length;i++){ 
    310                         this._match_patt[i].evaluate(this._deviation_data,mr); 
    311                         if (confidence > mr.confidence) { 
    312                                 continue; 
    313                         } 
    314                         // もっと一致するマーカーがあったぽい 
    315                         square_index = i; 
    316                         direction = mr.direction; 
    317                         confidence = mr.confidence; 
    318                 } 
    319                 //最も一致したマーカ情報を、この矩形の情報として記録する。 
    320                 var result:NyARDetectMarkerResult = this.result_stack.prePush(); 
    321                 result.arcode_id = square_index; 
    322                 result.confidence = confidence; 
    323  
    324                 var sq:NyARSquare=result.square; 
    325                 //directionを考慮して、squareを更新する。 
    326                 for(i=0;i<4;i++){ 
    327                         var idx:int=(i+4 - direction) % 4; 
    328                         this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]); 
    329                 } 
    330                 for (i = 0; i < 4; i++) { 
    331                         //直線同士の交点計算 
    332                         if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){ 
    333                                 throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK 
    334                         } 
    335                 } 
    336         } 
    337         public function init(i_raster:INyARRgbRaster):void 
    338         { 
    339                 this._ref_raster=i_raster; 
    340                 this.result_stack.clear(); 
    341                  
    342         } 
    343 } 
    344  
    345  
    346 class NyARDetectMarkerResult 
    347 { 
    348         public var arcode_id:int; 
    349         public var confidence:Number; 
    350  
    351         public var square:NyARSquare=new NyARSquare(); 
    352 } 
    353  
    354  
    355 class NyARDetectMarkerResultStack extends NyARObjectStack 
    356 { 
    357         public function NyARDetectMarkerResultStack(i_length:int) 
    358         { 
    359                 super(i_length); 
    360         } 
    361         protected override function createArray(i_length:int):Vector.<*> 
    362         { 
    363                 var ret:Vector.<NyARDetectMarkerResult>= new Vector.<NyARDetectMarkerResult>(i_length); 
    364                 for (var i:int =0; i < i_length; i++){ 
    365                         ret[i] = new NyARDetectMarkerResult(); 
    366                 } 
    367                 return Vector.<*>(ret); 
    368         }        
    369 }