OSDN Git Service

[TAG]NyARToolkit for Java 2.4.2
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.4.2 / test / jp / nyatla / nyartoolkit / dev / OptimizeCompareTest.java
1 /* \r
2  * PROJECT: NyARToolkit JOGL sample program.\r
3  * --------------------------------------------------------------------------------\r
4  * The MIT License\r
5  * Copyright (c) 2008 nyatla\r
6  * airmail(at)ebony.plala.or.jp\r
7  * http://nyatla.jp/nyartoolkit/\r
8  * \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
17  * \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
24  * THE SOFTWARE.\r
25  * \r
26  */\r
27 package jp.nyatla.nyartoolkit.dev;\r
28 \r
29 import java.awt.event.*;\r
30 import java.awt.*;\r
31 import javax.media.Buffer;\r
32 import javax.media.opengl.*;\r
33 import com.sun.opengl.util.*;\r
34 import jp.nyatla.nyartoolkit.*;\r
35 import jp.nyatla.nyartoolkit.core.*;\r
36 import jp.nyatla.nyartoolkit.core.param.*;\r
37 import jp.nyatla.nyartoolkit.core.transmat.*;\r
38 import jp.nyatla.nyartoolkit.detector.*;\r
39 import jp.nyatla.nyartoolkit.jmf.utils.*;\r
40 import jp.nyatla.nyartoolkit.jogl.utils.*;\r
41 /**\r
42  * 2種類の最適化アルゴリズムを比較するテストプログラム\r
43  *\r
44  */\r
45 \r
46 class Program implements JmfCaptureListener\r
47 {\r
48         private OptimizeCompareTest _view1;\r
49         private OptimizeCompareTest _view2;\r
50         public Object _sync_object=new Object();        \r
51         public NyARParam _ar_param;\r
52         public NyARCode _ar_code;\r
53         private final static int SCREEN_X = 320;\r
54         private final static int SCREEN_Y = 240;\r
55         private JmfCaptureDevice _capture;\r
56         public GLNyARRaster_RGB _cap_image;\r
57         public Program(NyARParam i_param, NyARCode i_ar_code) throws NyARException\r
58         {\r
59                 // キャプチャの準備\r
60                 JmfCaptureDeviceList devlist = new JmfCaptureDeviceList();\r
61                 this._capture = devlist.getDevice(0);\r
62                 if (!this._capture.setCaptureFormat(SCREEN_X, SCREEN_Y, 30.0f)) {\r
63                         throw new NyARException();\r
64                 }\r
65                 this._ar_param=i_param;\r
66                 this._ar_code=i_ar_code;\r
67                 this._capture.setOnCapture(this);\r
68                 // GL対応のRGBラスタオブジェクト\r
69                 this._cap_image = new GLNyARRaster_RGB(i_param, this._capture.getCaptureFormat());      \r
70                 this._view1=new OptimizeCompareTest(this,NyARSingleDetectMarker.PF_NYARTOOLKIT);\r
71                 this._view2=new OptimizeCompareTest(this,NyARSingleDetectMarker.PF_NYARTOOLKIT_ARTOOLKIT_FITTING);\r
72                 this._capture.start();\r
73                 return;\r
74         }\r
75         public void onUpdateBuffer(Buffer i_buffer)\r
76         {\r
77                 try {\r
78                         synchronized (this._sync_object) {\r
79                                 this._cap_image.setBuffer(i_buffer);\r
80                                 this._view1.updateCapture(this._cap_image);\r
81                                 this._view2.updateCapture(this._cap_image);\r
82                         }\r
83                 } catch (Exception e) {\r
84                         e.printStackTrace();\r
85                 }\r
86         }\r
87         //GL API\r
88         void glDrawCube(GL i_gl)\r
89         {\r
90                 // Colour cube data.\r
91                 int polyList = 0;\r
92                 float fSize = 0.5f;// マーカーサイズに対して0.5倍なので、4cmの立方体\r
93                 int f, i;\r
94                 float[][] cube_vertices = new float[][] { { 1.0f, 1.0f, 1.0f }, { 1.0f, -1.0f, 1.0f }, { -1.0f, -1.0f, 1.0f }, { -1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, -1.0f }, { 1.0f, -1.0f, -1.0f }, { -1.0f, -1.0f, -1.0f }, { -1.0f, 1.0f, -1.0f } };\r
95                 float[][] cube_vertex_colors = new float[][] { { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } };\r
96                 int cube_num_faces = 6;\r
97                 short[][] cube_faces = new short[][] { { 3, 2, 1, 0 }, { 2, 3, 7, 6 }, { 0, 1, 5, 4 }, { 3, 0, 4, 7 }, { 1, 2, 6, 5 }, { 4, 5, 6, 7 } };\r
98 \r
99                 if (polyList == 0) {\r
100                         polyList = i_gl.glGenLists(1);\r
101                         i_gl.glNewList(polyList, GL.GL_COMPILE);\r
102                         i_gl.glBegin(GL.GL_QUADS);\r
103                         for (f = 0; f < cube_num_faces; f++)\r
104                                 for (i = 0; i < 4; i++) {\r
105                                         i_gl.glColor3f(cube_vertex_colors[cube_faces[f][i]][0], cube_vertex_colors[cube_faces[f][i]][1], cube_vertex_colors[cube_faces[f][i]][2]);\r
106                                         i_gl.glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize);\r
107                                 }\r
108                         i_gl.glEnd();\r
109                         i_gl.glColor3f(0.0f, 0.0f, 0.0f);\r
110                         for (f = 0; f < cube_num_faces; f++) {\r
111                                 i_gl.glBegin(GL.GL_LINE_LOOP);\r
112                                 for (i = 0; i < 4; i++)\r
113                                         i_gl.glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize);\r
114                                 i_gl.glEnd();\r
115                         }\r
116                         i_gl.glEndList();\r
117                 }\r
118 \r
119                 i_gl.glPushMatrix(); // Save world coordinate system.\r
120                 i_gl.glTranslatef(0.0f, 0.0f, 0.5f); // Place base of cube on marker surface.\r
121                 i_gl.glRotatef(0.0f, 0.0f, 0.0f, 1.0f); // Rotate about z axis.\r
122                 i_gl.glDisable(GL.GL_LIGHTING); // Just use colours.\r
123                 i_gl.glCallList(polyList); // Draw the cube.\r
124                 i_gl.glPopMatrix(); // Restore world coordinate system.\r
125                 return;\r
126         }\r
127         \r
128 }\r
129 /**\r
130  * simpleLiteと同じようなテストプログラム 出来る限りARToolKitのサンプルと似せて作ってあります。 最も一致する"Hiro"マーカーを一つ選択して、その上に立方体を表示します。\r
131  * \r
132  */\r
133 public class OptimizeCompareTest implements GLEventListener\r
134 {\r
135         private final static int SCREEN_X = 320;\r
136 \r
137         private final static int SCREEN_Y = 240;\r
138 \r
139         private Animator _animator;\r
140         \r
141         private Program _parent;\r
142 \r
143 \r
144 //      private JmfCaptureDevice _capture;\r
145 \r
146         private GL _gl;\r
147         private NyARGLUtil _glnya;\r
148 \r
149         // NyARToolkit関係\r
150         private NyARSingleDetectMarker _nya;\r
151         private NyARParam _ar_param;\r
152 \r
153         private double[] _camera_projection = new double[16];\r
154 \r
155 \r
156         public OptimizeCompareTest(Program i_program,int i_pf) throws NyARException\r
157         {\r
158                 this._parent=i_program;\r
159                 this._ar_param = i_program._ar_param;\r
160 \r
161                 Frame frame = new Frame("["+i_pf+"]");\r
162 \r
163                 // NyARToolkitの準備\r
164                 this._nya = new NyARSingleDetectMarker(this._ar_param, i_program._ar_code, 80.0,i_program._cap_image.getBufferReader().getBufferType(),i_pf);\r
165                 this._nya.setContinueMode(true);// ここをtrueにすると、transMatContinueモード(History計算)になります。\r
166                 \r
167                 // 3Dを描画するコンポーネント\r
168                 GLCanvas canvas = new GLCanvas();\r
169                 frame.add(canvas);\r
170                 canvas.addGLEventListener(this);\r
171                 frame.addWindowListener(new WindowAdapter() {\r
172                         public void windowClosing(WindowEvent e)\r
173                         {\r
174                                 System.exit(0);\r
175                         }\r
176                 });\r
177 \r
178                 frame.setVisible(true);\r
179                 Insets ins = frame.getInsets();\r
180                 frame.setSize(SCREEN_X + ins.left + ins.right, SCREEN_Y + ins.top + ins.bottom);\r
181                 canvas.setBounds(ins.left, ins.top, SCREEN_X, SCREEN_Y);\r
182         }\r
183 \r
184         public void init(GLAutoDrawable drawable)\r
185         {\r
186                 this._gl = drawable.getGL();\r
187                 this._gl.glEnable(GL.GL_DEPTH_TEST);\r
188                 this._gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r
189                 // NyARToolkitの準備\r
190                 try {\r
191                         // NyARToolkit用の支援クラス\r
192                         _glnya = new NyARGLUtil(_gl);\r
193                 } catch (Exception e) {\r
194                         e.printStackTrace();\r
195                 }\r
196                 // カメラパラメータの計算\r
197                 this._glnya.toCameraFrustumRH(this._ar_param,this._camera_projection);\r
198                 this._animator = new Animator(drawable);\r
199                 this._animator.start();\r
200                 return;\r
201         }\r
202 \r
203         public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)\r
204         {\r
205                 _gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);\r
206                 _gl.glViewport(0, 0, width, height);\r
207 \r
208                 // 視体積の設定\r
209                 _gl.glMatrixMode(GL.GL_PROJECTION);\r
210                 _gl.glLoadIdentity();\r
211                 // 見る位置\r
212                 _gl.glMatrixMode(GL.GL_MODELVIEW);\r
213                 _gl.glLoadIdentity();\r
214         }\r
215 \r
216         private boolean _is_marker_exist=false;\r
217         private NyARTransMatResult __display_transmat_result = new NyARTransMatResult();\r
218 \r
219         private double[] __display_wk = new double[16];\r
220 \r
221         public void display(GLAutoDrawable drawable)\r
222         {\r
223                 NyARTransMatResult transmat_result = __display_transmat_result;\r
224                 if (!this._parent._cap_image.hasData()) {\r
225                         return;\r
226                 }\r
227                 // 背景を書く\r
228                 this._gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.\r
229                 this._glnya.drawBackGround(this._parent._cap_image, 1.0);                       \r
230                 try{\r
231                         synchronized(this._parent._sync_object){\r
232                                 // マーカーがあれば、立方体を描画\r
233                                 if (this._is_marker_exist){\r
234                                         // マーカーの一致度を調査するならば、ここでnya.getConfidence()で一致度を調べて下さい。\r
235                                         // Projection transformation.\r
236                                         _gl.glMatrixMode(GL.GL_PROJECTION);\r
237                                         _gl.glLoadMatrixd(_camera_projection, 0);\r
238                                         _gl.glMatrixMode(GL.GL_MODELVIEW);\r
239                                         // Viewing transformation.\r
240                                         _gl.glLoadIdentity();\r
241                                         // 変換行列を取得\r
242                                         _nya.getTransmationMatrix(transmat_result);\r
243                                         // 変換行列をOpenGL形式に変換\r
244                                         _glnya.toCameraViewRH(transmat_result, __display_wk);\r
245                                         _gl.glLoadMatrixd(__display_wk, 0);\r
246                 \r
247                                         // All other lighting and geometry goes here.\r
248                                         this._parent.glDrawCube(_gl);\r
249                                 }\r
250                         }\r
251                         Thread.sleep(1);// タスク実行権限を一旦渡す\r
252                 }catch(Exception e){\r
253                         e.printStackTrace();\r
254                 }\r
255 \r
256         }\r
257 \r
258         public void updateCapture(GLNyARRaster_RGB i_img)\r
259         {\r
260                 try {\r
261                         synchronized (this._parent._sync_object) {\r
262                                 this._is_marker_exist =this._nya.detectMarkerLite(i_img, 110);\r
263                         }\r
264                 } catch (Exception e) {\r
265                         e.printStackTrace();\r
266                 }\r
267         }\r
268 \r
269         public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)\r
270         {\r
271         }\r
272 \r
273         private final static String CARCODE_FILE = "../Data/patt.hiro";\r
274 \r
275         private final static String PARAM_FILE = "../Data/camera_para.dat";\r
276 \r
277         public static void main(String[] args)\r
278         {\r
279                 try {\r
280                         NyARParam param = new NyARParam();\r
281                         param.loadARParamFromFile(PARAM_FILE);\r
282                         param.changeScreenSize(SCREEN_X, SCREEN_Y);\r
283 \r
284                         NyARCode code = new NyARCode(16, 16);\r
285                         code.loadARPattFromFile(CARCODE_FILE);\r
286 \r
287                         new Program(param, code);\r
288                 } catch (Exception e) {\r
289                         e.printStackTrace();\r
290                 }\r
291                 return;\r
292         }\r
293 }\r