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 881 - (show annotations) (download) (as text)
Sat Jan 21 07:24:50 2012 UTC (12 years, 2 months ago) by nyatla
Original Path: nyar4psg/trunk/src/jp/nyatla/nyar4psg/NyARPsgBaseClass.java
File MIME type: text/x-java
File size: 18355 byte(s)
1.1.7 marker2ScreenCoordSystem関数の追加
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 processing.core.*;
32
33 import jp.nyatla.nyartoolkit.*;
34 import jp.nyatla.nyartoolkit.core.param.*;
35 import jp.nyatla.nyartoolkit.core.rasterreader.NyARPerspectiveRasterReader;
36 import jp.nyatla.nyartoolkit.core.transmat.*;
37 import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint2d;
38 import jp.nyatla.nyartoolkit.core.types.NyARDoublePoint3d;
39 import jp.nyatla.nyartoolkit.core.types.NyARIntSize;
40 import jp.nyatla.nyartoolkit.core.types.matrix.NyARDoubleMatrix44;
41
42
43
44 /**
45 * ���������������������NyARToolkit for Processing������������������������������
46 * ARToolkit���������������������������������������������������������������������������������������������
47 */
48 class NyARPsgBaseClass
49 {
50 /**
51 * near���������������������������������������������
52 */
53 public final float FRUSTUM_DEFAULT_NEAR_CLIP=100;
54 /**
55 * far���������������������������������������������
56 */
57 public final float FRUSTUM_DEFAULT_FAR_CLIP=100000;
58
59 /**
60 * ���������������������������������
61 * NyAR4psg������������������������������������������
62 */
63 public final static String VERSION = "NyAR4psg/1.1.7;NyARToolkit for java/3.0.0+;ARToolKit/2.72.1";
64 /**���������������Applet������������������������������*/
65 protected PApplet _ref_papplet;
66 /**���Processing���������������ProjectionMatrix���������*/
67 protected final PMatrix3D _ps_projection=new PMatrix3D();
68 /**���������������������������������ModelviewMatrix���������*/
69 protected final PMatrix3D _ps_background_mv=new PMatrix3D();
70
71 /**���ARToolkit���������������������������������������������*/
72 protected final NyARParam _ar_param=new NyARParam();
73 protected final NyARFrustum _frustum=new NyARFrustum();;
74 protected NyAR4PsgConfig _config;
75
76 /** ������������������������������{@link PImage}������������������������������������������������������������������������������*/
77 protected PImageRaster _src_raster;
78 /** ���������������������������������������������{@link #_src_raster}���������������������*/
79 protected NyARPerspectiveRasterReader _preader;
80
81 private float _clip_far;
82 private float _clip_near;
83
84 /**
85 * ������������������������������
86 */
87 protected NyARPsgBaseClass()
88 {
89 }
90 protected void initInstance(PApplet parent,String i_cparam_file, int i_width,int i_height,NyAR4PsgConfig i_config) throws NyARException
91 {
92 this._ref_papplet=parent;
93 this._config=i_config;
94 this._src_raster=new PImageRaster(i_width,i_height);
95 this._preader=new NyARPerspectiveRasterReader(this._src_raster.getBufferType());
96 try{
97 this._ar_param.loadARParam(this._ref_papplet.createInput(i_cparam_file));
98 this._ar_param.changeScreenSize(i_width, i_height);//w,h���������������������
99
100 //Processing���projectionMatrix���������������Frustum���������
101 this.setARClipping(FRUSTUM_DEFAULT_NEAR_CLIP,FRUSTUM_DEFAULT_FAR_CLIP);
102 }catch(NyARException e){
103 this._ref_papplet.die("Error while setting up NyARToolkit for java", e);
104 }
105 return;
106 }
107 /**
108 * ������������������ProjectionMatrix������������������������������
109 * @return
110 * ProjectionMatrix���������
111 */
112 public PMatrix3D getProjectionMatrix()
113 {
114 return new PMatrix3D(this._ps_projection);
115 }
116 /**
117 * ������������������PImage������������������������������������������������PImage���farclip���+1������������������������������
118 * <div>���������������������������������������������������</div>
119 * <hr/>
120 * :<br/>
121 * PMatrix3D om=new PMatrix3D(((PGraphics3D)g).projection);<br/>
122 * setBackgroundOrtho(img.width,img.height)<br/>
123 * pushMatrix();<br/>
124 * resetMatrix();<br/>
125 * translate(0,0,-(far*0.99f));<br/>
126 * image(img,-width/2,-height/2);<br/>
127 * popMatrix();<br/>
128 * setPerspective(om);<br/>
129 * :<br/>
130 * <hr/>
131 * ������������������PrjectionMatrix���ModelViewMatrix���������������������������������������������������������������������
132 * ���������������������������������������������������������������������
133 * @param i_img
134 * ���������������������������������
135 */
136 public void drawBackground(PImage i_img)
137 {
138 PApplet pa=this._ref_papplet;
139 PMatrix3D om=new PMatrix3D(((PGraphics3D)pa.g).projection);
140 this.setBackgroundOrtho(i_img.width,i_img.height);
141 pa.pushMatrix();
142 pa.setMatrix(this._ps_background_mv);
143 pa.image(i_img,-i_img.width/2,-i_img.height/2);
144 pa.popMatrix();
145 //���������������
146 this.setPerspective(om);
147 }
148
149 /**
150 * ������������������������������������������������������������������������������������������������������{@link #FRUSTUM_DEFAULT_NEAR_CLIP}���{@link #FRUSTUM_DEFAULT_FAR_CLIP}���������
151 * ������������������������{@link #setARPerspective()}������������������������������������������������������������������������������������������������������������������
152 * @param i_near
153 * NearPlane������������������������������������[mm]���������
154 * @param i_far
155 * FarPlane������������������������������������[mm]���������
156 */
157 public void setARClipping(float i_near,float i_far)
158 {
159 this._clip_far=i_far;
160 this._clip_near=i_near;
161 arPerspectiveMat2Projection(this._ar_param,i_near,i_far,this._ps_projection,this._frustum);
162 this._ps_background_mv.reset();
163 this._ps_background_mv.translate(0,0,-i_far);
164 }
165 /**
166 * ������������������������������������Processing������������������������
167 * ������������������0,0���������������������������
168 * near���������������������{@link #setARClipping}������������������������������������������������������
169 *���far���������������������{@link #setARClipping}���������������������������������+1���������������������
170 * <div>���������������������������������������������������</div>
171 * <hr/>
172 * :<br/>
173 * ortho(-i_width/2, i_width/2,-i_height/2,i_height/2,near,far+1);<br/>
174 * :<br/>
175 * <hr/>
176 * @param i_width
177 * ������������������������
178 * @param i_height
179 * ���������������������������
180 */
181 public void setBackgroundOrtho(int i_width,int i_height)
182 {
183 float half_w=i_width/2;
184 float half_h=i_height/2;
185 this._ref_papplet.ortho(-half_w, half_w,-half_h,half_h,this._clip_near,this._clip_far+1);
186 }
187 /**
188 * ������������������ARToolKit���������ProjectionMatrix���Processing������������������������
189 * ���������������������������Processing���ProjectionMatrix���ARToolKit���������������������������������������������������������������������������������������������������������������������
190 * Processing������������������Frustum���������������������{@link PGraphics3D#perspective()}������������������
191 * Frustum���������������������������{@link PGraphics3D#perspective()}���{@link PGraphics3D#perspective()}���������������������������������
192 * <p>
193 * Version 1.1.0���������������projection matrix���������������������������������������projection matrix���������������������{@link PGraphics3D#projection}���������������������������������������
194 * </p>
195 */
196 public void setARPerspective()
197 {
198 this.setPerspective(this._ps_projection);
199 }
200 /**
201 * ������������������ProjectionMatrix���Processing������������������������
202 * @param i_projection
203 * ������������ProjectionMatrix���������������������
204 * <p>
205 * Processing/1.3������������������{@link PApplet#matrixMode}���������������������������������������������������������
206 * ������������������������frustum������
207 * </p>
208 * <p>
209 * Version 1.1.0���������������projection matrix���������������������������������������projection matrix���������������������{@link PGraphics3D#projection}���������������������������������������
210 * </p>
211 */
212 public void setPerspective(PMatrix3D i_projection)
213 {
214 //Projection���frustum������������������
215 float far=i_projection.m23/(i_projection.m22+1);
216 float near=i_projection.m23/(i_projection.m22-1);
217 this._ref_papplet.frustum(
218 (i_projection.m02-1)*near/i_projection.m00,
219 (i_projection.m02+1)*near/i_projection.m00,
220 (i_projection.m12-1)*near/i_projection.m11,
221 (i_projection.m12+1)*near/i_projection.m11,
222 near,far);
223 return;
224 }
225
226 protected static void PMatrix2GLProjection(PMatrix3D i_in,float[] o_out)
227 {
228 o_out[ 0]=i_in.m00;
229 o_out[ 1]=i_in.m10;
230 o_out[ 2]=i_in.m20;
231 o_out[ 3]=i_in.m30;
232 o_out[ 4]=i_in.m01;
233 o_out[ 5]=i_in.m11;
234 o_out[ 6]=i_in.m21;
235 o_out[ 7]=i_in.m31;
236 o_out[ 8]=i_in.m02;
237 o_out[ 9]=i_in.m12;
238 o_out[10]=i_in.m22;
239 o_out[11]=i_in.m32;
240 o_out[12]=i_in.m03;
241 o_out[13]=i_in.m13;
242 o_out[14]=i_in.m23;
243 o_out[15]=i_in.m33;
244 }
245 protected static void PMatrix2GLProjection(PMatrix3D i_in,double[] o_out)
246 {
247 o_out[ 0]=i_in.m00;
248 o_out[ 1]=i_in.m10;
249 o_out[ 2]=i_in.m20;
250 o_out[ 3]=i_in.m30;
251 o_out[ 4]=i_in.m01;
252 o_out[ 5]=i_in.m11;
253 o_out[ 6]=i_in.m21;
254 o_out[ 7]=i_in.m31;
255 o_out[ 8]=i_in.m02;
256 o_out[ 9]=i_in.m12;
257 o_out[10]=i_in.m22;
258 o_out[11]=i_in.m32;
259 o_out[12]=i_in.m03;
260 o_out[13]=i_in.m13;
261 o_out[14]=i_in.m23;
262 o_out[15]=i_in.m33;
263 }
264
265 private static void arPerspectiveMat2Projection(NyARParam i_param,float i_near,float i_far,PMatrix3D o_projection,NyARFrustum o_frustum)
266 {
267 NyARDoubleMatrix44 tmp=new NyARDoubleMatrix44();
268 NyARIntSize s=i_param.getScreenSize();
269 i_param.getPerspectiveProjectionMatrix().makeCameraFrustumRH(s.w,s.h,i_near,i_far,tmp);
270 o_projection.m00=(float)(tmp.m00);
271 o_projection.m01=(float)(tmp.m01);
272 o_projection.m02=(float)(tmp.m02);
273 o_projection.m03=(float)(tmp.m03);
274 o_projection.m10=(float)(tmp.m10);
275 o_projection.m11=(float)(tmp.m11);
276 o_projection.m12=(float)(tmp.m12);
277 o_projection.m13=(float)(tmp.m13);
278 o_projection.m20=(float)(tmp.m20);
279 o_projection.m21=(float)(tmp.m21);
280 o_projection.m22=(float)(tmp.m22);
281 o_projection.m23=(float)(tmp.m23);
282 o_projection.m30=(float)(tmp.m30);
283 o_projection.m31=(float)(tmp.m31);
284 o_projection.m32=(float)(tmp.m32);
285 o_projection.m33=(float)(tmp.m33);
286 o_frustum.setValue(tmp, s.w, s.h);
287 }
288 protected static void matResult2GLArray(NyARTransMatResult i_src,double[] o_gl_array)
289 {
290 o_gl_array[0 + 0 * 4] = i_src.m00;
291 o_gl_array[0 + 1 * 4] = i_src.m01;
292 o_gl_array[0 + 2 * 4] = i_src.m02;
293 o_gl_array[0 + 3 * 4] = i_src.m03;
294 o_gl_array[1 + 0 * 4] = -i_src.m10;
295 o_gl_array[1 + 1 * 4] = -i_src.m11;
296 o_gl_array[1 + 2 * 4] = -i_src.m12;
297 o_gl_array[1 + 3 * 4] = -i_src.m13;
298 o_gl_array[2 + 0 * 4] = -i_src.m20;
299 o_gl_array[2 + 1 * 4] = -i_src.m21;
300 o_gl_array[2 + 2 * 4] = -i_src.m22;
301 o_gl_array[2 + 3 * 4] = -i_src.m23;
302 o_gl_array[3 + 0 * 4] = 0.0;
303 o_gl_array[3 + 1 * 4] = 0.0;
304 o_gl_array[3 + 2 * 4] = 0.0;
305 o_gl_array[3 + 3 * 4] = 1.0;
306 }
307 /**
308 * ���������������������������
309 */
310 private final static PMatrix3D _lh_mat=new PMatrix3D(
311 -1,0,0,0,
312 0,1,0,0,
313 0,0,1,0,
314 0,0,0,1);
315
316 /**
317 * ���������������Processing���Matrix���������������������
318 * @param i_src
319 * @param i_mode
320 * @param o_pmatrix
321 */
322 protected static void matResult2PMatrix3D(NyARDoubleMatrix44 i_src,int i_mode,PMatrix3D o_pmatrix)
323 {
324 o_pmatrix.m00 = (float)i_src.m00;
325 o_pmatrix.m01 = (float)i_src.m01;
326 o_pmatrix.m02 = (float)i_src.m02;
327 o_pmatrix.m03 = (float)i_src.m03;
328 o_pmatrix.m10 = (float)i_src.m10;//mirror
329 o_pmatrix.m11 = (float)i_src.m11;//mirror
330 o_pmatrix.m12 = (float)i_src.m12;//mirror
331 o_pmatrix.m13 = (float)i_src.m13;//mirror
332 o_pmatrix.m20 = (float)-i_src.m20;
333 o_pmatrix.m21 = (float)-i_src.m21;
334 o_pmatrix.m22 = (float)-i_src.m22;
335 o_pmatrix.m23 = (float)-i_src.m23;
336 o_pmatrix.m30 = 0.0f;
337 o_pmatrix.m31 = 0.0f;
338 o_pmatrix.m32 = 0.0f;
339 o_pmatrix.m33 = 1.0f;
340 if(i_mode==NyAR4PsgConfig.CS_LEFT_HAND)
341 {
342 o_pmatrix.apply(_lh_mat);
343 }
344 }
345 /**
346 * ���������������������������������������i_x,i_y���������������������������������������������
347 * @param i_mat
348 * @param i_x
349 * @param i_y
350 * @return
351 */
352 protected PVector screen2MarkerCoordSystem(NyARDoubleMatrix44 i_mat,int i_x,int i_y)
353 {
354 PVector ret=new PVector();
355 NyARDoublePoint3d tmp=new NyARDoublePoint3d();
356 this._frustum.unProjectOnMatrix(i_x, i_y,i_mat,tmp);
357 ret.x=(float)tmp.x;
358 ret.y=(float)tmp.y;
359 ret.z=(float)tmp.z;
360 if(this._config._coordinate_system==NyAR4PsgConfig.CS_LEFT_HAND){
361 ret.x*=-1;
362 }
363 return ret;
364 }
365 /**
366 * PImage������������������������������������������������������
367 * @param i_mat
368 * @param i_x1
369 * @param i_y1
370 * @param i_x2
371 * @param i_y2
372 * @param i_x3
373 * @param i_y3
374 * @param i_x4
375 * @param i_y4
376 * @param i_out_w_pix
377 * @param i_out_h_pix
378 * @return
379 */
380 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)
381 {
382 //WrapRaster���������������������
383 if(!this._src_raster.hasBuffer()){
384 this._ref_papplet.die("_rel_detector is null.(Function detect() was never called. )");
385 }
386 PImage img=new PImage(i_out_w_pix,i_out_h_pix);
387 img.parent=this._ref_papplet;
388 try{
389 NyARDoublePoint3d[] pos=NyARDoublePoint3d.createArray(4);
390 i_mat.transform3d(i_x1, i_y1,0, pos[1]);
391 i_mat.transform3d(i_x2, i_y2,0, pos[0]);
392 i_mat.transform3d(i_x3, i_y3,0, pos[3]);
393 i_mat.transform3d(i_x4, i_y4,0, pos[2]);
394 //4������������������
395 NyARDoublePoint2d[] pos2=NyARDoublePoint2d.createArray(4);
396 for(int i=3;i>=0;i--){
397 this._frustum.project(pos[i],pos2[i]);
398 }
399 PImageRaster out_raster=new PImageRaster(i_out_w_pix,i_out_h_pix);
400 out_raster.wrapBuffer(img);
401 if(!this._preader.read4Point(this._src_raster,pos2,0,0,1,out_raster))
402 {
403 throw new Exception("this._preader.read4Point failed.");
404 }
405 return img;
406 }catch(Exception e){
407 e.printStackTrace();
408 this._ref_papplet.die("Exception occurred at MultiARTookitMarker.pickupImage");
409 return null;
410 }
411 }
412 /**
413 * ������������������������������������������������������������������������������
414 * ���������������������������������������������������������������
415 * <p>������ -
416 * ������������������gluUnproject������������������������������������������������������������������������������
417 * ���������������������������������������������
418 * http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml
419 * ARToolKit���������������������������������������������OpenGL���unProject������ix,iy���������������������������������������������������������������������������������������
420 * </p>
421 * @param ix
422 * ���������������������������
423 * @param iy
424 * ������������������
425 * @param o_point_on_screen
426 * ���������������
427 */
428 /* public final PVector unProject(double ix,double iy)
429 {
430 double n=(this._frustum_rh.m23/(this._frustum_rh.m22-1));
431 NyARDoubleMatrix44 m44=this._inv_frustum_rh;
432 double v1=(this._screen_size.w-ix-1)*2/this._screen_size.w-1.0;//ARToolKit���Frustram������������������
433 double v2=(this._screen_size.h-iy-1)*2/this._screen_size.h-1.0;
434 double v3=2*n-1.0;
435 double b=1/(m44.m30*v1+m44.m31*v2+m44.m32*v3+m44.m33);
436 o_point_on_screen.x=(m44.m00*v1+m44.m01*v2+m44.m02*v3+m44.m03)*b;
437 o_point_on_screen.y=(m44.m10*v1+m44.m11*v2+m44.m12*v3+m44.m13)*b;
438 o_point_on_screen.z=(m44.m20*v1+m44.m21*v2+m44.m22*v3+m44.m23)*b;
439 return;
440 }
441 */ /**
442 * ���������������������������������������������������������������������������������������������������������������������������������������������������������
443 * ���������������������������������������������
444 * @param ix
445 * ���������������������������
446 * @param iy
447 * ���������������������������
448 * @param i_mat
449 * ������������������������������
450 * @param o_pos
451 * ������������������������������������������������
452 */
453 /* public final PVector unProjectOnCamera(double ix,double iy,PMatrix3D i_mat)
454 {
455 //������������������
456 this.unProject(ix,iy,o_pos);
457 //������������������������������
458 double nx=i_mat.m02;
459 double ny=i_mat.m12;
460 double nz=i_mat.m22;
461 double mx=i_mat.m03;
462 double my=i_mat.m13;
463 double mz=i_mat.m23;
464 double t=(nx*mx+ny*my+nz*mz)/(nx*o_pos.x+ny*o_pos.y+nz*o_pos.z);
465 o_pos.x=t*o_pos.x;
466 o_pos.y=t*o_pos.y;
467 o_pos.z=t*o_pos.z;
468 }
469 */ /**
470 * ������������������������������������������������������������������������������������������������������������������������
471 * ARToolKit������P175���������������������������������
472 * @param ix
473 * ���������������������������
474 * @param iy
475 * ���������������������������
476 * @param i_mat
477 * ������������������������������
478 * @param o_pos
479 * ������������������������������������������������
480 * @return
481 * ���������������������������true������������������
482 */
483 /* public final PVector unProjectOnMatrix(double ix,double iy,PMatrix3D i_mat)
484 {
485 //������������������������������������
486 unProjectOnCamera(ix,iy,i_mat,o_pos);
487 //������������������
488 NyARDoubleMatrix44 m=new NyARDoubleMatrix44();
489 if(!m.inverse(i_mat)){
490 return false;
491 }
492 m.transform3d(o_pos, o_pos);
493 return true;
494 }
495 */ /**
496 * ������������������������������������������������������������������������������
497 * @param i_x
498 * ������������������������
499 * @param i_y
500 * ������������������������
501 * @param i_z
502 * ������������������������
503 * @param o_pos2d
504 * ������������������������������������������������
505 */
506 /* public final PVector project(double i_x,double i_y,double i_z)
507 {
508 NyARDoubleMatrix44 m=this._frustum_rh;
509 double v3_1=1/i_z*m.m32;
510 double w=this._screen_size.w;
511 double h=this._screen_size.h;
512 o_pos2d.x=w-(1+(i_x*m.m00+i_z*m.m02)*v3_1)*w/2;
513 o_pos2d.y=h-(1+(i_y*m.m11+i_z*m.m12)*v3_1)*h/2;
514 return;
515 }
516 */}
517

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