Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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