2 * PROJECT: NyARToolkit Java3D utilities.
\r
3 * --------------------------------------------------------------------------------
\r
5 * Copyright (c) 2008 nyatla
\r
6 * airmail(at)ebony.plala.or.jp
\r
7 * http://nyatla.jp/nyartoolkit/
\r
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
10 * of this software and associated documentation files (the "Software"), to deal
\r
11 * in the Software without restriction, including without limitation the rights
\r
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
13 * copies of the Software, and to permit persons to whom the Software is
\r
14 * furnished to do so, subject to the following conditions:
\r
15 * The above copyright notice and this permission notice shall be included in
\r
16 * all copies or substantial portions of the Software.
\r
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
27 package jp.nyatla.nyartoolkit.java3d.utils;
\r
29 import java.util.Enumeration;
\r
31 import javax.media.Buffer;
\r
32 import javax.media.j3d.*;
\r
33 import javax.vecmath.*;
\r
35 import jp.nyatla.nyartoolkit.NyARException;
\r
36 import jp.nyatla.nyartoolkit.jmf.utils.*;
\r
37 import jp.nyatla.nyartoolkit.core.*;
\r
38 import jp.nyatla.nyartoolkit.core.param.NyARParam;
\r
39 import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult;
\r
40 import jp.nyatla.nyartoolkit.detector.*;
\r
41 import jp.nyatla.nyartoolkit.core.types.*;
\r
44 * NyARToolkitと連動したBehaviorを返却するクラスです。
\r
45 * 提供できるBehaviorは、BackgroundとTransformgroupです。
\r
48 public class NyARSingleMarkerBehaviorHolder implements JmfCaptureListener
\r
50 private NyARParam _cparam;
\r
52 private JmfCaptureDevice _capture;
\r
54 private J3dNyARRaster_RGB _nya_raster;//最大3スレッドで共有されるので、排他制御かけること。
\r
56 private NyARSingleDetectMarker _nya;
\r
59 private NyARBehavior _nya_behavior;
\r
61 public NyARSingleMarkerBehaviorHolder(NyARParam i_cparam, float i_rate, NyARCode i_ar_code, double i_marker_width) throws NyARException
\r
63 this._nya_behavior = null;
\r
64 final NyARIntSize scr_size = i_cparam.getScreenSize();
\r
65 this._cparam = i_cparam;
\r
67 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
\r
68 this._capture=devlist.getDevice(0);
\r
69 this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,scr_size.w, scr_size.h,15f);
\r
70 this._capture.setOnCapture(this);
\r
71 this._nya_raster = new J3dNyARRaster_RGB(this._cparam);
\r
72 this._nya = new NyARSingleDetectMarker(this._cparam, i_ar_code, i_marker_width);
\r
73 this._nya_behavior = new NyARBehavior(this._nya, this._nya_raster, i_rate);
\r
76 public Behavior getBehavior()
\r
78 return this._nya_behavior;
\r
82 * i_back_groundにキャプチャ画像を転送するようにBehaviorを設定します。
\r
83 * i_back_groungはALLOW_IMAGE_WRITE属性を持つものである必要があります。
\r
84 * @param i_back_groung
\r
87 public void setBackGround(Background i_back_ground)
\r
90 this._nya_behavior.setRelatedBackGround(i_back_ground);
\r
94 * i_trgroupの座標系をマーカーにあわせるようにBehaviorを設定します。
\r
97 public void setTransformGroup(TransformGroup i_trgroup)
\r
100 this._nya_behavior.setRelatedTransformGroup(i_trgroup);
\r
104 * 座標系再計算後に呼び出されるリスナです。
\r
105 * @param i_listener
\r
107 public void setUpdateListener(NyARSingleMarkerBehaviorListener i_listener)
\r
110 this._nya_behavior.setUpdateListener(i_listener);
\r
114 * ラスタを更新 コールバック関数だから呼んじゃらめえ
\r
116 public void onUpdateBuffer(Buffer i_buffer)
\r
119 synchronized (this._nya_raster) {
\r
120 this._nya_raster.setBuffer(i_buffer);
\r
122 } catch (Exception e) {
\r
123 e.printStackTrace();
\r
127 public void start() throws NyARException
\r
130 this._capture.start();
\r
135 this._capture.stop();
\r
139 class NyARBehavior extends Behavior
\r
141 private NyARTransMatResult trans_mat_result = new NyARTransMatResult();
\r
143 private NyARSingleDetectMarker related_nya;
\r
145 private TransformGroup trgroup;
\r
147 private Background back_ground;
\r
149 private J3dNyARRaster_RGB raster;
\r
151 private WakeupCondition wakeup;
\r
153 private NyARSingleMarkerBehaviorListener listener;
\r
155 public void initialize()
\r
161 * i_related_ic2dの内容で定期的にi_back_groundを更新するBehavior
\r
162 * @param i_back_ground
\r
163 * @param i_related_ic2d
\r
165 public NyARBehavior(NyARSingleDetectMarker i_related_nya, J3dNyARRaster_RGB i_related_raster, float i_rate)
\r
168 wakeup = new WakeupOnElapsedTime((int) (1000 / i_rate));
\r
169 related_nya = i_related_nya;
\r
171 raster = i_related_raster;
\r
172 back_ground = null;
\r
174 this.setSchedulingBounds(new BoundingSphere(new Point3d(), 100.0));
\r
177 public void setRelatedBackGround(Background i_back_ground)
\r
179 synchronized (raster) {
\r
180 back_ground = i_back_ground;
\r
184 public void setRelatedTransformGroup(TransformGroup i_trgroup)
\r
186 synchronized (raster) {
\r
187 trgroup = i_trgroup;
\r
191 public void setUpdateListener(NyARSingleMarkerBehaviorListener i_listener)
\r
193 synchronized (raster) {
\r
194 listener = i_listener;
\r
201 public void processStimulus(Enumeration criteria)
\r
204 synchronized (raster) {
\r
205 Transform3D t3d = null;
\r
206 boolean is_marker_exist = false;
\r
207 if (back_ground != null) {
\r
208 raster.renewImageComponent2D();/*DirectXモードのときの対策*/
\r
209 back_ground.setImage(raster.getImageComponent2D());
\r
211 if (raster.hasData()) {
\r
212 is_marker_exist = related_nya.detectMarkerLite(raster, 100);
\r
213 if (is_marker_exist)
\r
215 final NyARTransMatResult src = this.trans_mat_result;
\r
216 related_nya.getTransmationMatrix(src);
\r
217 Matrix4d matrix = new Matrix4d(src.m00, -src.m10, -src.m20, 0, -src.m01, src.m11, src.m21, 0, -src.m02, src.m12, src.m22, 0, -src.m03, src.m13, src.m23, 1);
\r
218 matrix.transpose();
\r
219 t3d = new Transform3D(matrix);
\r
220 if (trgroup != null) {
\r
221 trgroup.setTransform(t3d);
\r
225 if (listener != null) {
\r
226 listener.onUpdate(is_marker_exist, t3d);
\r
230 } catch (Exception e) {
\r
231 e.printStackTrace();
\r