| 1 |
/* |
|---|
| 2 |
* PROJECT: FLARToolKit |
|---|
| 3 |
* -------------------------------------------------------------------------------- |
|---|
| 4 |
* This work is based on the NyARToolKit developed by |
|---|
| 5 |
* R.Iizuka (nyatla) |
|---|
| 6 |
* http://nyatla.jp/nyatoolkit/ |
|---|
| 7 |
* |
|---|
| 8 |
* The FLARToolKit is ActionScript 3.0 version ARToolkit class library. |
|---|
| 9 |
* Copyright (C)2008 Saqoosha |
|---|
| 10 |
* |
|---|
| 11 |
* This program is free software: you can redistribute it and/or modify |
|---|
| 12 |
* it under the terms of the GNU General Public License as published by |
|---|
| 13 |
* the Free Software Foundation, either version 3 of the License, or |
|---|
| 14 |
* (at your option) any later version. |
|---|
| 15 |
* |
|---|
| 16 |
* This program is distributed in the hope that it will be useful, |
|---|
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 19 |
* GNU General Public License for more details. |
|---|
| 20 |
* |
|---|
| 21 |
* You should have received a copy of the GNU General Public License |
|---|
| 22 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 23 |
* |
|---|
| 24 |
* For further information please contact. |
|---|
| 25 |
* http://www.libspark.org/wiki/saqoosha/FLARToolKit |
|---|
| 26 |
* <saq(at)saqoosha.net> |
|---|
| 27 |
* |
|---|
| 28 |
*/ |
|---|
| 29 |
package org.libspark.flartoolkit.detector |
|---|
| 30 |
{ |
|---|
| 31 |
import jp.nyatla.nyartoolkit.as3.core.match.*; |
|---|
| 32 |
import jp.nyatla.nyartoolkit.as3.core.pickup.*; |
|---|
| 33 |
import jp.nyatla.nyartoolkit.as3.core.squaredetect.*; |
|---|
| 34 |
import jp.nyatla.nyartoolkit.as3.*; |
|---|
| 35 |
import jp.nyatla.nyartoolkit.as3.core.types.stack.*; |
|---|
| 36 |
import jp.nyatla.nyartoolkit.as3.core.match.*; |
|---|
| 37 |
import jp.nyatla.nyartoolkit.as3.core.param.*; |
|---|
| 38 |
import jp.nyatla.nyartoolkit.as3.core.raster.rgb.*; |
|---|
| 39 |
import jp.nyatla.nyartoolkit.as3.detector.*; |
|---|
| 40 |
import jp.nyatla.nyartoolkit.as3.core.types.*; |
|---|
| 41 |
import org.libspark.flartoolkit.core.* |
|---|
| 42 |
/** |
|---|
| 43 |
* detectMarkerのコールバック関数 |
|---|
| 44 |
*/ |
|---|
| 45 |
internal class MultiDetectSquareCB implements NyARSquareContourDetector_IDetectMarkerCallback |
|---|
| 46 |
{ |
|---|
| 47 |
//公開プロパティ |
|---|
| 48 |
public var result_stack:FLARDetectMarkerResultStack=new FLARDetectMarkerResultStack(NyARDetectMarker.AR_SQUARE_MAX); |
|---|
| 49 |
//参照インスタンス |
|---|
| 50 |
public var _ref_raster:INyARRgbRaster; |
|---|
| 51 |
//所有インスタンス |
|---|
| 52 |
private var _inst_patt:INyARColorPatt; |
|---|
| 53 |
private var _deviation_data:NyARMatchPattDeviationColorData; |
|---|
| 54 |
private var _match_patt:Vector.<NyARMatchPatt_Color_WITHOUT_PCA>; |
|---|
| 55 |
private var __detectMarkerLite_mr:NyARMatchPattResult=new NyARMatchPattResult(); |
|---|
| 56 |
private var _coordline:NyARCoord2Linear; |
|---|
| 57 |
|
|---|
| 58 |
public function MultiDetectSquareCB(i_inst_patt:INyARColorPatt, i_ref_code:Vector.<FLARCode>, i_num_of_code:int, i_param:NyARParam) |
|---|
| 59 |
{ |
|---|
| 60 |
var cw:int = i_ref_code[0].getWidth(); |
|---|
| 61 |
var ch:int = i_ref_code[0].getHeight(); |
|---|
| 62 |
|
|---|
| 63 |
this._inst_patt=i_inst_patt; |
|---|
| 64 |
this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor()); |
|---|
| 65 |
this._deviation_data=new NyARMatchPattDeviationColorData(cw,ch); |
|---|
| 66 |
|
|---|
| 67 |
//NyARMatchPatt_Color_WITHOUT_PCA[]の作成 |
|---|
| 68 |
this._match_patt=new Vector.<NyARMatchPatt_Color_WITHOUT_PCA>(i_num_of_code); |
|---|
| 69 |
this._match_patt[0]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[0]); |
|---|
| 70 |
for (var i:int = 1; i < i_num_of_code; i++){ |
|---|
| 71 |
//解像度チェック |
|---|
| 72 |
if (cw != i_ref_code[i].getWidth() || ch != i_ref_code[i].getHeight()) { |
|---|
| 73 |
throw new NyARException(); |
|---|
| 74 |
} |
|---|
| 75 |
this._match_patt[i]=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code[i]); |
|---|
| 76 |
} |
|---|
| 77 |
return; |
|---|
| 78 |
} |
|---|
| 79 |
private var __tmp_vertex:Vector.<NyARIntPoint2d>=NyARIntPoint2d.createArray(4); |
|---|
| 80 |
/** |
|---|
| 81 |
* 矩形が見付かるたびに呼び出されます。 |
|---|
| 82 |
* 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。 |
|---|
| 83 |
*/ |
|---|
| 84 |
public function onSquareDetect(i_sender:NyARSquareContourDetector,i_coordx:Vector.<int>,i_coordy:Vector.<int>,i_coor_num:int ,i_vertex_index:Vector.<int>):void |
|---|
| 85 |
{ |
|---|
| 86 |
var mr:NyARMatchPattResult=this.__detectMarkerLite_mr; |
|---|
| 87 |
//輪郭座標から頂点リストに変換 |
|---|
| 88 |
var vertex:Vector.<NyARIntPoint2d>=this.__tmp_vertex; |
|---|
| 89 |
vertex[0].x=i_coordx[i_vertex_index[0]]; |
|---|
| 90 |
vertex[0].y=i_coordy[i_vertex_index[0]]; |
|---|
| 91 |
vertex[1].x=i_coordx[i_vertex_index[1]]; |
|---|
| 92 |
vertex[1].y=i_coordy[i_vertex_index[1]]; |
|---|
| 93 |
vertex[2].x=i_coordx[i_vertex_index[2]]; |
|---|
| 94 |
vertex[2].y=i_coordy[i_vertex_index[2]]; |
|---|
| 95 |
vertex[3].x=i_coordx[i_vertex_index[3]]; |
|---|
| 96 |
vertex[3].y=i_coordy[i_vertex_index[3]]; |
|---|
| 97 |
|
|---|
| 98 |
//画像を取得 |
|---|
| 99 |
if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){ |
|---|
| 100 |
return; |
|---|
| 101 |
} |
|---|
| 102 |
//取得パターンをカラー差分データに変換して評価する。 |
|---|
| 103 |
this._deviation_data.setRaster(this._inst_patt); |
|---|
| 104 |
|
|---|
| 105 |
//最も一致するパターンを割り当てる。 |
|---|
| 106 |
var square_index:int,direction:int; |
|---|
| 107 |
var confidence:Number; |
|---|
| 108 |
this._match_patt[0].evaluate(this._deviation_data,mr); |
|---|
| 109 |
square_index=0; |
|---|
| 110 |
direction=mr.direction; |
|---|
| 111 |
confidence=mr.confidence; |
|---|
| 112 |
//2番目以降 |
|---|
| 113 |
var i:int; |
|---|
| 114 |
for(i=1;i<this._match_patt.length;i++){ |
|---|
| 115 |
this._match_patt[i].evaluate(this._deviation_data,mr); |
|---|
| 116 |
if (confidence > mr.confidence) { |
|---|
| 117 |
continue; |
|---|
| 118 |
} |
|---|
| 119 |
// もっと一致するマーカーがあったぽい |
|---|
| 120 |
square_index = i; |
|---|
| 121 |
direction = mr.direction; |
|---|
| 122 |
confidence = mr.confidence; |
|---|
| 123 |
} |
|---|
| 124 |
//最も一致したマーカ情報を、この矩形の情報として記録する。 |
|---|
| 125 |
var result:FLARDetectMarkerResult = this.result_stack.prePush(); |
|---|
| 126 |
result.arcode_id = square_index; |
|---|
| 127 |
result.confidence = confidence; |
|---|
| 128 |
result.direction = direction; |
|---|
| 129 |
|
|---|
| 130 |
var sq:NyARSquare=result.square; |
|---|
| 131 |
//directionを考慮して、squareを更新する。 |
|---|
| 132 |
for(i=0;i<4;i++){ |
|---|
| 133 |
var idx:int=(i+4 - direction) % 4; |
|---|
| 134 |
this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]); |
|---|
| 135 |
} |
|---|
| 136 |
for (i = 0; i < 4; i++) { |
|---|
| 137 |
//直線同士の交点計算 |
|---|
| 138 |
if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){ |
|---|
| 139 |
throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
public function init(i_raster:INyARRgbRaster):void |
|---|
| 144 |
{ |
|---|
| 145 |
this._ref_raster=i_raster; |
|---|
| 146 |
this.result_stack.clear(); |
|---|
| 147 |
|
|---|
| 148 |
} |
|---|
| 149 |
} |
|---|
| 150 |
} |
|---|