| 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.*; |
|---|
| 32 |
import jp.nyatla.nyartoolkit.as3.core.*; |
|---|
| 33 |
import jp.nyatla.nyartoolkit.as3.core.match.*; |
|---|
| 34 |
import jp.nyatla.nyartoolkit.as3.core.param.*; |
|---|
| 35 |
import jp.nyatla.nyartoolkit.as3.core.pickup.*; |
|---|
| 36 |
import jp.nyatla.nyartoolkit.as3.core.raster.*; |
|---|
| 37 |
import jp.nyatla.nyartoolkit.as3.core.raster.rgb.*; |
|---|
| 38 |
import jp.nyatla.nyartoolkit.as3.core.squaredetect.*; |
|---|
| 39 |
import jp.nyatla.nyartoolkit.as3.core.types.*; |
|---|
| 40 |
|
|---|
| 41 |
/** |
|---|
| 42 |
* detectMarkerのコールバック関数 |
|---|
| 43 |
*/ |
|---|
| 44 |
internal class SingleDetectSquareCB implements NyARSquareContourDetector_IDetectMarkerCallback |
|---|
| 45 |
{ |
|---|
| 46 |
//公開プロパティ |
|---|
| 47 |
public var confidence:Number; |
|---|
| 48 |
public var square:NyARSquare=new NyARSquare(); |
|---|
| 49 |
public var direction:int; |
|---|
| 50 |
|
|---|
| 51 |
//参照インスタンス |
|---|
| 52 |
private var _ref_raster:INyARRgbRaster; |
|---|
| 53 |
//所有インスタンス |
|---|
| 54 |
private var _inst_patt:INyARColorPatt; |
|---|
| 55 |
private var _deviation_data:NyARMatchPattDeviationColorData; |
|---|
| 56 |
private var _match_patt:NyARMatchPatt_Color_WITHOUT_PCA; |
|---|
| 57 |
private var __detectMarkerLite_mr:NyARMatchPattResult=new NyARMatchPattResult(); |
|---|
| 58 |
private var _coordline:NyARCoord2Linear; |
|---|
| 59 |
|
|---|
| 60 |
public function SingleDetectSquareCB(i_inst_patt:INyARColorPatt,i_ref_code:NyARCode,i_param:NyARParam) |
|---|
| 61 |
{ |
|---|
| 62 |
this._inst_patt=i_inst_patt; |
|---|
| 63 |
this._deviation_data=new NyARMatchPattDeviationColorData(i_ref_code.getWidth(),i_ref_code.getHeight()); |
|---|
| 64 |
this._coordline=new NyARCoord2Linear(i_param.getScreenSize(),i_param.getDistortionFactor()); |
|---|
| 65 |
this._match_patt=new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code); |
|---|
| 66 |
return; |
|---|
| 67 |
} |
|---|
| 68 |
private var __tmp_vertex:Vector.<NyARIntPoint2d>=NyARIntPoint2d.createArray(4); |
|---|
| 69 |
/** |
|---|
| 70 |
* 矩形が見付かるたびに呼び出されます。 |
|---|
| 71 |
* 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。 |
|---|
| 72 |
*/ |
|---|
| 73 |
public function onSquareDetect(i_sender:NyARSquareContourDetector,i_coordx:Vector.<int>,i_coordy:Vector.<int>,i_coor_num:int,i_vertex_index:Vector.<int>):void |
|---|
| 74 |
{ |
|---|
| 75 |
var i:int; |
|---|
| 76 |
var mr:NyARMatchPattResult=this.__detectMarkerLite_mr; |
|---|
| 77 |
//輪郭座標から頂点リストに変換 |
|---|
| 78 |
var vertex:Vector.<NyARIntPoint2d>=this.__tmp_vertex; |
|---|
| 79 |
vertex[0].x=i_coordx[i_vertex_index[0]]; |
|---|
| 80 |
vertex[0].y=i_coordy[i_vertex_index[0]]; |
|---|
| 81 |
vertex[1].x=i_coordx[i_vertex_index[1]]; |
|---|
| 82 |
vertex[1].y=i_coordy[i_vertex_index[1]]; |
|---|
| 83 |
vertex[2].x=i_coordx[i_vertex_index[2]]; |
|---|
| 84 |
vertex[2].y=i_coordy[i_vertex_index[2]]; |
|---|
| 85 |
vertex[3].x=i_coordx[i_vertex_index[3]]; |
|---|
| 86 |
vertex[3].y=i_coordy[i_vertex_index[3]]; |
|---|
| 87 |
|
|---|
| 88 |
//画像を取得 |
|---|
| 89 |
if (!this._inst_patt.pickFromRaster(this._ref_raster,vertex)){ |
|---|
| 90 |
return; |
|---|
| 91 |
} |
|---|
| 92 |
//取得パターンをカラー差分データに変換して評価する。 |
|---|
| 93 |
this._deviation_data.setRaster(this._inst_patt); |
|---|
| 94 |
if(!this._match_patt.evaluate(this._deviation_data,mr)){ |
|---|
| 95 |
return; |
|---|
| 96 |
} |
|---|
| 97 |
//現在の一致率より低ければ終了 |
|---|
| 98 |
if (this.confidence > mr.confidence){ |
|---|
| 99 |
return; |
|---|
| 100 |
} |
|---|
| 101 |
//一致率の高い矩形があれば、方位を考慮して頂点情報を作成 |
|---|
| 102 |
var sq:NyARSquare=this.square; |
|---|
| 103 |
this.confidence = mr.confidence; |
|---|
| 104 |
this.direction = mr.direction; |
|---|
| 105 |
//directionを考慮して、squareを更新する。 |
|---|
| 106 |
for(i=0;i<4;i++){ |
|---|
| 107 |
var idx:int=(i+4 - mr.direction) % 4; |
|---|
| 108 |
this._coordline.coord2Line(i_vertex_index[idx],i_vertex_index[(idx+1)%4],i_coordx,i_coordy,i_coor_num,sq.line[i]); |
|---|
| 109 |
} |
|---|
| 110 |
for (i = 0; i < 4; i++) { |
|---|
| 111 |
//直線同士の交点計算 |
|---|
| 112 |
if(!NyARLinear.crossPos(sq.line[i],sq.line[(i + 3) % 4],sq.sqvertex[i])){ |
|---|
| 113 |
throw new NyARException();//ここのエラー復帰するならダブルバッファにすればOK |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
public function init(i_raster:INyARRgbRaster):void |
|---|
| 118 |
{ |
|---|
| 119 |
this.confidence=0; |
|---|
| 120 |
this._ref_raster=i_raster; |
|---|
| 121 |
|
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|
| 124 |
} |
|---|