Develop and Download Open Source Software

Browse Subversion Repository

Contents of /nyar4psg/trunk/src/src/jp/nyatla/nyar4psg/NyARPsgBaseClass.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 828 - (show annotations) (download) (as text)
Sun Apr 24 07:31:39 2011 UTC (12 years, 11 months ago) by nyatla
Original Path: nyar4psg/trunk/src/jp/nyatla/nyar4psg/NyARPsgBaseClass.java
File MIME type: text/x-java
File size: 15401 byte(s)


1 /*
2 * PROJECT: NyARToolkit for proce55ing.
3 * --------------------------------------------------------------------------------
4 * The MIT License
5 * Copyright (c) 2008 nyatla
6 * airmail(at)ebony.plala.or.jp
7 * http://nyatla.jp/nyartoolkit/
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
28 package jp.nyatla.nyar4psg;
29
30
31 import javax.media.opengl.*;
32
33 import processing.core.*;
34 import processing.opengl.*;
35
36
37 import jp.nyatla.nyartoolkit.*;
38 import jp.nyatla.nyartoolkit.core.param.*;
39 import jp.nyatla.nyartoolkit.core.rasterreader.NyARPerspectiveRasterReader;
40 import jp.nyatla.nyartoolkit.core.transmat.*;
41 import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d;
42 import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d;
43 import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44;
44
45
46
47 /**
48 * ���������������������NyARToolkit for Processing������������������������������
49 * ARToolkit���������������������������������������������������������������������������������������������
50 */
51 class NyARPsgBaseClass
52 {
53
54
55 /**
56 * ���������������������������������
57 * NyAR4psg������������������������������������������
58 */
59 public final static String VERSION = "NyAR4psg/1.0.1;NyARToolkit for java/3.0.0+;ARToolKit/2.72.1";
60 /**���������������Applet������������������������������*/
61 protected PApplet _ref_papplet;
62 /**���Processing���������������ProjectionMatrix���������*/
63 protected final PMatrix3D _ps_projection=new PMatrix3D();
64 /**���ARToolkit���������������������������������������������*/
65 protected final NyARParam _ar_param=new NyARParam();
66 protected final NyARFrustum _frustum=new NyARFrustum();;
67 protected NyAR4PsgConfig _config;
68
69 /** ������������������������������{@link PImage}������������������������������������������������������������������������������*/
70 protected PImageRaster _src_raster;
71 /** ���������������������������������������������{@link #_src_raster}���������������������*/
72 protected NyARPerspectiveRasterReader _preader;
73 private int _g_type;
74 /**
75 * ������������������������������
76 */
77 protected NyARPsgBaseClass()
78 {
79 }
80 protected void initInstance(PApplet parent,String i_cparam_file, int i_width,int i_height,NyAR4PsgConfig i_config) throws NyARException
81 {
82 this._ref_papplet=parent;
83 this._config=i_config;
84 this._src_raster=new PImageRaster(i_width,i_height);
85 this._preader=new NyARPerspectiveRasterReader(this._src_raster.getBufferType());
86 try{
87 this._g_type=getGraphicsType(parent.g);
88 this._ar_param.loadARParam(this._ref_papplet.createInput(i_cparam_file));
89 this._ar_param.changeScreenSize(i_width, i_height);
90
91 //Processing���projectionMatrix���������������Frustum���������
92 arPerspectiveMat2Projection(this._ar_param.getPerspectiveProjectionMatrix(),i_width,i_height,this._ps_projection,this._frustum);
93 }catch(NyARException e){
94 this._ref_papplet.die("Error while setting up NyARToolkit for java", e);
95 }
96 return;
97 }
98 private final static double view_distance_min = 100;
99 private final static double view_distance_max = 100000;
100 private final static int GT_P3D=0;
101 private final static int GT_OPENGL=1;
102 /**
103 * {@link PGraphics}������������������������������������������������
104 * @param i_g
105 * @return
106 * @throws NyARException
107 */
108 private static int getGraphicsType(PGraphics i_g) throws NyARException
109 {
110 String n=i_g.getClass().getName();
111 if(n.compareTo("processing.opengl.PGraphicsOpenGL")==0){
112 return GT_OPENGL;
113 }else if(n.compareTo("processing.core.PGraphics3D")==0){
114 return GT_P3D;
115 }
116 throw new NyARException("Unknown Graphics");
117
118 }
119
120
121 private float[] _tmpf=new float[16];
122
123 /**
124 * ������������������ARToolKit���������ProjectionMatrix���Processing������������������������
125 * ���������������������������Processing���ProjectionMatrix���ARToolKit���������������������������������������������������������������������������������������������������������������������
126 * Processing������������������Frustum���������������������{@link PGraphics3D#perspective()}������������������
127 * Frustum���������������������������{@link PGraphics3D#perspective()}���{@link PGraphics3D#perspective()}���������������������������������
128 * @return
129 * ���������������������������projectionMatrix������������������
130 */
131 public PMatrix3D setARPerspective()
132 {
133 return this.setPerspective(this._ps_projection);
134 }
135 /**
136 * ������������������ProjectionMatrix���Processing������������������������
137 * @param i_projection
138 * ������������ProjectionMatrix���������������������
139 * @return
140 * ���������������������������projectionMatrix������������������
141 */
142 public PMatrix3D setPerspective(PMatrix3D i_projection)
143 {
144 if(!(this._ref_papplet.g instanceof PGraphics3D)){
145 this._ref_papplet.die("NyAR4Psg require PGraphics3D instance.");
146 }
147 PGraphics3D g=(PGraphics3D)this._ref_papplet.g;
148 //���������ProjectionMatrix������������������
149 PMatrix3D ret=new PMatrix3D();
150 ret.set(g.projection);
151 //ProjectionMatrix���������
152 g.projection.set(i_projection);
153 //OpenGL���������������������������
154 if(this._g_type==GT_OPENGL)
155 {
156 GL gl=((PGraphicsOpenGL)g).gl;
157 gl.glMatrixMode(GL.GL_PROJECTION);
158 PMatrix2GLProjection(i_projection,_tmpf);
159 gl.glLoadMatrixf(_tmpf,0);
160 }
161 return ret;
162 }
163
164 protected static void PMatrix2GLProjection(PMatrix3D i_in,float[] o_out)
165 {
166 o_out[ 0]=i_in.m00;
167 o_out[ 1]=i_in.m10;
168 o_out[ 2]=i_in.m20;
169 o_out[ 3]=i_in.m30;
170 o_out[ 4]=i_in.m01;
171 o_out[ 5]=i_in.m11;
172 o_out[ 6]=i_in.m21;
173 o_out[ 7]=i_in.m31;
174 o_out[ 8]=i_in.m02;
175 o_out[ 9]=i_in.m12;
176 o_out[10]=i_in.m22;
177 o_out[11]=i_in.m32;
178 o_out[12]=i_in.m03;
179 o_out[13]=i_in.m13;
180 o_out[14]=i_in.m23;
181 o_out[15]=i_in.m33;
182 }
183 protected static void PMatrix2GLProjection(PMatrix3D i_in,double[] o_out)
184 {
185 o_out[ 0]=i_in.m00;
186 o_out[ 1]=i_in.m10;
187 o_out[ 2]=i_in.m20;
188 o_out[ 3]=i_in.m30;
189 o_out[ 4]=i_in.m01;
190 o_out[ 5]=i_in.m11;
191 o_out[ 6]=i_in.m21;
192 o_out[ 7]=i_in.m31;
193 o_out[ 8]=i_in.m02;
194 o_out[ 9]=i_in.m12;
195 o_out[10]=i_in.m22;
196 o_out[11]=i_in.m32;
197 o_out[12]=i_in.m03;
198 o_out[13]=i_in.m13;
199 o_out[14]=i_in.m23;
200 o_out[15]=i_in.m33;
201 }
202
203 private static void arPerspectiveMat2Projection(NyARPerspectiveProjectionMatrix i_prjmat,int i_w,int i_h,PMatrix3D o_projection,NyARFrustum o_frustum)
204 {
205 NyARDoubleMatrix44 tmp=new NyARDoubleMatrix44();
206 i_prjmat.makeCameraFrustumRH(i_w, i_h, view_distance_min, view_distance_max,tmp);
207 o_projection.m00=(float)(tmp.m00);
208 o_projection.m01=(float)(tmp.m01);
209 o_projection.m02=(float)(tmp.m02);
210 o_projection.m03=(float)(tmp.m03);
211 o_projection.m10=(float)(tmp.m10);
212 o_projection.m11=(float)(tmp.m11);
213 o_projection.m12=(float)(tmp.m12);
214 o_projection.m13=(float)(tmp.m13);
215 o_projection.m20=(float)(tmp.m20);
216 o_projection.m21=(float)(tmp.m21);
217 o_projection.m22=(float)(tmp.m22);
218 o_projection.m23=(float)(tmp.m23);
219 o_projection.m30=(float)(tmp.m30);
220 o_projection.m31=(float)(tmp.m31);
221 o_projection.m32=(float)(tmp.m32);
222 o_projection.m33=(float)(tmp.m33);
223 o_frustum.setValue(tmp, i_w, i_h);
224 }
225 protected static void matResult2GLArray(NyARTransMatResult i_src,double[] o_gl_array)
226 {
227 o_gl_array[0 + 0 * 4] = i_src.m00;
228 o_gl_array[0 + 1 * 4] = i_src.m01;
229 o_gl_array[0 + 2 * 4] = i_src.m02;
230 o_gl_array[0 + 3 * 4] = i_src.m03;
231 o_gl_array[1 + 0 * 4] = -i_src.m10;
232 o_gl_array[1 + 1 * 4] = -i_src.m11;
233 o_gl_array[1 + 2 * 4] = -i_src.m12;
234 o_gl_array[1 + 3 * 4] = -i_src.m13;
235 o_gl_array[2 + 0 * 4] = -i_src.m20;
236 o_gl_array[2 + 1 * 4] = -i_src.m21;
237 o_gl_array[2 + 2 * 4] = -i_src.m22;
238 o_gl_array[2 + 3 * 4] = -i_src.m23;
239 o_gl_array[3 + 0 * 4] = 0.0;
240 o_gl_array[3 + 1 * 4] = 0.0;
241 o_gl_array[3 + 2 * 4] = 0.0;
242 o_gl_array[3 + 3 * 4] = 1.0;
243 }
244 /**
245 * ���������������������������
246 */
247 private final static PMatrix3D _lh_mat=new PMatrix3D(-1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1);
248
249 /**
250 * ���������������Processing���Matrix���������������������
251 * @param i_src
252 * @param i_mode
253 * @param o_pmatrix
254 */
255 protected static void matResult2PMatrix3D(NyARDoubleMatrix44 i_src,int i_mode,PMatrix3D o_pmatrix)
256 {
257 o_pmatrix.m00 = (float)i_src.m00;
258 o_pmatrix.m01 = (float)i_src.m01;
259 o_pmatrix.m02 = (float)i_src.m02;
260 o_pmatrix.m03 = (float)i_src.m03;
261 o_pmatrix.m10 = (float)i_src.m10;//mirror
262 o_pmatrix.m11 = (float)i_src.m11;//mirror
263 o_pmatrix.m12 = (float)i_src.m12;//mirror
264 o_pmatrix.m13 = (float)i_src.m13;//mirror
265 o_pmatrix.m20 = (float)-i_src.m20;
266 o_pmatrix.m21 = (float)-i_src.m21;
267 o_pmatrix.m22 = (float)-i_src.m22;
268 o_pmatrix.m23 = (float)-i_src.m23;
269 o_pmatrix.m30 = 0.0f;
270 o_pmatrix.m31 = 0.0f;
271 o_pmatrix.m32 = 0.0f;
272 o_pmatrix.m33 = 1.0f;
273 if(i_mode==NyAR4PsgConfig.CS_LEFT_HAND)
274 {
275 o_pmatrix.apply(_lh_mat);
276 }
277 }
278 /**
279 * ������������������i_mat������������������������������������������������������������
280 * @param i_mat
281 * @param i_x
282 * @param i_y
283 * @return
284 */
285 protected PVector screen2MarkerCoordSystem(NyARDoubleMatrix44 i_mat,int i_x,int i_y)
286 {
287 PVector ret=new PVector();
288 NyARDoublePoint3d tmp=new NyARDoublePoint3d();
289 this._frustum.unProjectOnMatrix(i_x, i_y,i_mat,tmp);
290 ret.x=(float)tmp.x;
291 ret.y=(float)tmp.y;
292 ret.z=(float)tmp.z;
293 if(this._config._coordinate_system==NyAR4PsgConfig.CS_LEFT_HAND){
294 ret.x*=-1;
295 }
296 return ret;
297 }
298 /**
299 * PImage������������������������������������������������������
300 * @param i_mat
301 * @param i_x1
302 * @param i_y1
303 * @param i_x2
304 * @param i_y2
305 * @param i_x3
306 * @param i_y3
307 * @param i_x4
308 * @param i_y4
309 * @param i_out_w_pix
310 * @param i_out_h_pix
311 * @return
312 */
313 protected PImage pickupMarkerImage(NyARDoubleMatrix44 i_mat,int i_x1,int i_y1,int i_x2,int i_y2,int i_x3,int i_y3,int i_x4,int i_y4,int i_out_w_pix,int i_out_h_pix)
314 {
315 //WrapRaster���������������������
316 if(!this._src_raster.hasBuffer()){
317 this._ref_papplet.die("_rel_detector is null.(Function detect() was never called. )");
318 }
319 PImage img=new PImage(i_out_w_pix,i_out_h_pix);
320 img.parent=this._ref_papplet;
321 try{
322 NyARDoublePoint3d[] pos=NyARDoublePoint3d.createArray(4);
323 i_mat.transform3d(i_x1, i_y1,0, pos[1]);
324 i_mat.transform3d(i_x2, i_y2,0, pos[0]);
325 i_mat.transform3d(i_x3, i_y3,0, pos[3]);
326 i_mat.transform3d(i_x4, i_y4,0, pos[2]);
327 //4������������������
328 NyARDoublePoint2d[] pos2=NyARDoublePoint2d.createArray(4);
329 for(int i=3;i>=0;i--){
330 this._frustum.project(pos[i],pos2[i]);
331 }
332 PImageRaster out_raster=new PImageRaster(i_out_w_pix,i_out_h_pix);
333 out_raster.wrapBuffer(img);
334 if(!this._preader.read4Point(this._src_raster,pos2,0,0,1,out_raster))
335 {
336 throw new Exception("this._preader.read4Point failed.");
337 }
338 return img;
339 }catch(Exception e){
340 e.printStackTrace();
341 this._ref_papplet.die("Exception occurred at MultiARTookitMarker.pickupImage");
342 return null;
343 }
344 }
345 /**
346 * ������������������������������������������������������������������������������
347 * ���������������������������������������������������������������
348 * <p>������ -
349 * ������������������gluUnproject������������������������������������������������������������������������������
350 * ���������������������������������������������
351 * http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml
352 * ARToolKit���������������������������������������������OpenGL���unProject������ix,iy���������������������������������������������������������������������������������������
353 * </p>
354 * @param ix
355 * ���������������������������
356 * @param iy
357 * ������������������
358 * @param o_point_on_screen
359 * ���������������
360 */
361 /* public final PVector unProject(double ix,double iy)
362 {
363 double n=(this._frustum_rh.m23/(this._frustum_rh.m22-1));
364 NyARDoubleMatrix44 m44=this._inv_frustum_rh;
365 double v1=(this._screen_size.w-ix-1)*2/this._screen_size.w-1.0;//ARToolKit���Frustram������������������
366 double v2=(this._screen_size.h-iy-1)*2/this._screen_size.h-1.0;
367 double v3=2*n-1.0;
368 double b=1/(m44.m30*v1+m44.m31*v2+m44.m32*v3+m44.m33);
369 o_point_on_screen.x=(m44.m00*v1+m44.m01*v2+m44.m02*v3+m44.m03)*b;
370 o_point_on_screen.y=(m44.m10*v1+m44.m11*v2+m44.m12*v3+m44.m13)*b;
371 o_point_on_screen.z=(m44.m20*v1+m44.m21*v2+m44.m22*v3+m44.m23)*b;
372 return;
373 }
374 */ /**
375 * ���������������������������������������������������������������������������������������������������������������������������������������������������������
376 * ���������������������������������������������
377 * @param ix
378 * ���������������������������
379 * @param iy
380 * ���������������������������
381 * @param i_mat
382 * ������������������������������
383 * @param o_pos
384 * ������������������������������������������������
385 */
386 /* public final PVector unProjectOnCamera(double ix,double iy,PMatrix3D i_mat)
387 {
388 //������������������
389 this.unProject(ix,iy,o_pos);
390 //������������������������������
391 double nx=i_mat.m02;
392 double ny=i_mat.m12;
393 double nz=i_mat.m22;
394 double mx=i_mat.m03;
395 double my=i_mat.m13;
396 double mz=i_mat.m23;
397 double t=(nx*mx+ny*my+nz*mz)/(nx*o_pos.x+ny*o_pos.y+nz*o_pos.z);
398 o_pos.x=t*o_pos.x;
399 o_pos.y=t*o_pos.y;
400 o_pos.z=t*o_pos.z;
401 }
402 */ /**
403 * ������������������������������������������������������������������������������������������������������������������������
404 * ARToolKit������P175���������������������������������
405 * @param ix
406 * ���������������������������
407 * @param iy
408 * ���������������������������
409 * @param i_mat
410 * ������������������������������
411 * @param o_pos
412 * ������������������������������������������������
413 * @return
414 * ���������������������������true������������������
415 */
416 /* public final PVector unProjectOnMatrix(double ix,double iy,PMatrix3D i_mat)
417 {
418 //������������������������������������
419 unProjectOnCamera(ix,iy,i_mat,o_pos);
420 //������������������
421 NyARDoubleMatrix44 m=new NyARDoubleMatrix44();
422 if(!m.inverse(i_mat)){
423 return false;
424 }
425 m.transform3d(o_pos, o_pos);
426 return true;
427 }
428 */ /**
429 * ������������������������������������������������������������������������������
430 * @param i_x
431 * ������������������������
432 * @param i_y
433 * ������������������������
434 * @param i_z
435 * ������������������������
436 * @param o_pos2d
437 * ������������������������������������������������
438 */
439 /* public final PVector project(double i_x,double i_y,double i_z)
440 {
441 NyARDoubleMatrix44 m=this._frustum_rh;
442 double v3_1=1/i_z*m.m32;
443 double w=this._screen_size.w;
444 double h=this._screen_size.h;
445 o_pos2d.x=w-(1+(i_x*m.m00+i_z*m.m02)*v3_1)*w/2;
446 o_pos2d.y=h-(1+(i_y*m.m11+i_z*m.m12)*v3_1)*h/2;
447 return;
448 }
449 */}
450

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26