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 854 - (show annotations) (download) (as text)
Fri May 6 06:21:40 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: 18135 byte(s)
nyar4psg
1.1.4

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

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