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 839 - (show annotations) (download) (as text)
Sun Apr 24 13:37:50 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: 14980 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 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.matrix.NyARDoubleMatrix44;
40
41
42
43 /**
44 * ���������������������NyARToolkit for Processing������������������������������
45 * ARToolkit���������������������������������������������������������������������������������������������
46 */
47 class NyARPsgBaseClass
48 {
49
50
51 /**
52 * ���������������������������������
53 * NyAR4psg������������������������������������������
54 */
55 public final static String VERSION = "NyAR4psg/1.0.3;NyARToolkit for java/3.0.0+;ARToolKit/2.72.1";
56 /**���������������Applet������������������������������*/
57 protected PApplet _ref_papplet;
58 /**���Processing���������������ProjectionMatrix���������*/
59 protected final PMatrix3D _ps_projection=new PMatrix3D();
60 /**���ARToolkit���������������������������������������������*/
61 protected final NyARParam _ar_param=new NyARParam();
62 protected final NyARFrustum _frustum=new NyARFrustum();;
63 protected NyAR4PsgConfig _config;
64
65 /** ������������������������������{@link PImage}������������������������������������������������������������������������������*/
66 protected PImageRaster _src_raster;
67 /** ���������������������������������������������{@link #_src_raster}���������������������*/
68 protected NyARPerspectiveRasterReader _preader;
69 /**
70 * ������������������������������
71 */
72 protected NyARPsgBaseClass()
73 {
74 }
75 protected void initInstance(PApplet parent,String i_cparam_file, int i_width,int i_height,NyAR4PsgConfig i_config) throws NyARException
76 {
77 this._ref_papplet=parent;
78 this._config=i_config;
79 this._src_raster=new PImageRaster(i_width,i_height);
80 this._preader=new NyARPerspectiveRasterReader(this._src_raster.getBufferType());
81 try{
82 this._ar_param.loadARParam(this._ref_papplet.createInput(i_cparam_file));
83 this._ar_param.changeScreenSize(i_width, i_height);
84
85 //Processing���projectionMatrix���������������Frustum���������
86 arPerspectiveMat2Projection(this._ar_param.getPerspectiveProjectionMatrix(),i_width,i_height,this._ps_projection,this._frustum);
87 }catch(NyARException e){
88 this._ref_papplet.die("Error while setting up NyARToolkit for java", e);
89 }
90 return;
91 }
92 private final static float view_distance_min = 100;
93 private final static float view_distance_max = 100000;
94
95
96 /**
97 * ������������������ARToolKit���������ProjectionMatrix���Processing������������������������
98 * ���������������������������Processing���ProjectionMatrix���ARToolKit���������������������������������������������������������������������������������������������������������������������
99 * Processing������������������Frustum���������������������{@link PGraphics3D#perspective()}������������������
100 * Frustum���������������������������{@link PGraphics3D#perspective()}���{@link PGraphics3D#perspective()}���������������������������������
101 * @return
102 * ���������������������������projectionMatrix������������������
103 */
104 public PMatrix3D setARPerspective()
105 {
106 return this.setPerspective(this._ps_projection);
107 }
108 /**
109 * ������������������ProjectionMatrix���Processing������������������������
110 * @param i_projection
111 * ������������ProjectionMatrix���������������������
112 * @return
113 * ���������������������������projectionMatrix������������������
114 *
115 * <p>
116 * Processing/1.3������������������{@link PApplet#matrixMode}���������������������������������������������������������
117 * ������������������������frustum������
118 * </p>
119 */
120 public PMatrix3D setPerspective(PMatrix3D i_projection)
121 {
122 if(!(this._ref_papplet.g instanceof PGraphics3D)){
123 this._ref_papplet.die("NyAR4Psg require PGraphics3D instance.");
124 }
125 PGraphics3D g=(PGraphics3D)this._ref_papplet.g;
126 //���������ProjectionMatrix������������������
127 PMatrix3D ret=new PMatrix3D();
128 ret.set(g.projection);
129 //Projection���frustum������������������
130 float far=i_projection.m23/(i_projection.m22+1);
131 float near=i_projection.m23/(i_projection.m22-1);
132 this._ref_papplet.frustum(
133 (i_projection.m02-1)*near/i_projection.m00,
134 (i_projection.m02+1)*near/i_projection.m00,
135 (i_projection.m12-1)*near/i_projection.m11,
136 (i_projection.m12+1)*near/i_projection.m11,
137 near,far);
138 return ret;
139 }
140
141 protected static void PMatrix2GLProjection(PMatrix3D i_in,float[] o_out)
142 {
143 o_out[ 0]=i_in.m00;
144 o_out[ 1]=i_in.m10;
145 o_out[ 2]=i_in.m20;
146 o_out[ 3]=i_in.m30;
147 o_out[ 4]=i_in.m01;
148 o_out[ 5]=i_in.m11;
149 o_out[ 6]=i_in.m21;
150 o_out[ 7]=i_in.m31;
151 o_out[ 8]=i_in.m02;
152 o_out[ 9]=i_in.m12;
153 o_out[10]=i_in.m22;
154 o_out[11]=i_in.m32;
155 o_out[12]=i_in.m03;
156 o_out[13]=i_in.m13;
157 o_out[14]=i_in.m23;
158 o_out[15]=i_in.m33;
159 }
160 protected static void PMatrix2GLProjection(PMatrix3D i_in,double[] o_out)
161 {
162 o_out[ 0]=i_in.m00;
163 o_out[ 1]=i_in.m10;
164 o_out[ 2]=i_in.m20;
165 o_out[ 3]=i_in.m30;
166 o_out[ 4]=i_in.m01;
167 o_out[ 5]=i_in.m11;
168 o_out[ 6]=i_in.m21;
169 o_out[ 7]=i_in.m31;
170 o_out[ 8]=i_in.m02;
171 o_out[ 9]=i_in.m12;
172 o_out[10]=i_in.m22;
173 o_out[11]=i_in.m32;
174 o_out[12]=i_in.m03;
175 o_out[13]=i_in.m13;
176 o_out[14]=i_in.m23;
177 o_out[15]=i_in.m33;
178 }
179
180 private static void arPerspectiveMat2Projection(NyARPerspectiveProjectionMatrix i_prjmat,int i_w,int i_h,PMatrix3D o_projection,NyARFrustum o_frustum)
181 {
182 NyARDoubleMatrix44 tmp=new NyARDoubleMatrix44();
183 i_prjmat.makeCameraFrustumRH(i_w, i_h, view_distance_min, view_distance_max,tmp);
184 o_projection.m00=(float)(tmp.m00);
185 o_projection.m01=(float)(tmp.m01);
186 o_projection.m02=(float)(tmp.m02);
187 o_projection.m03=(float)(tmp.m03);
188 o_projection.m10=(float)(tmp.m10);
189 o_projection.m11=(float)(tmp.m11);
190 o_projection.m12=(float)(tmp.m12);
191 o_projection.m13=(float)(tmp.m13);
192 o_projection.m20=(float)(tmp.m20);
193 o_projection.m21=(float)(tmp.m21);
194 o_projection.m22=(float)(tmp.m22);
195 o_projection.m23=(float)(tmp.m23);
196 o_projection.m30=(float)(tmp.m30);
197 o_projection.m31=(float)(tmp.m31);
198 o_projection.m32=(float)(tmp.m32);
199 o_projection.m33=(float)(tmp.m33);
200 o_frustum.setValue(tmp, i_w, i_h);
201 }
202 protected static void matResult2GLArray(NyARTransMatResult i_src,double[] o_gl_array)
203 {
204 o_gl_array[0 + 0 * 4] = i_src.m00;
205 o_gl_array[0 + 1 * 4] = i_src.m01;
206 o_gl_array[0 + 2 * 4] = i_src.m02;
207 o_gl_array[0 + 3 * 4] = i_src.m03;
208 o_gl_array[1 + 0 * 4] = -i_src.m10;
209 o_gl_array[1 + 1 * 4] = -i_src.m11;
210 o_gl_array[1 + 2 * 4] = -i_src.m12;
211 o_gl_array[1 + 3 * 4] = -i_src.m13;
212 o_gl_array[2 + 0 * 4] = -i_src.m20;
213 o_gl_array[2 + 1 * 4] = -i_src.m21;
214 o_gl_array[2 + 2 * 4] = -i_src.m22;
215 o_gl_array[2 + 3 * 4] = -i_src.m23;
216 o_gl_array[3 + 0 * 4] = 0.0;
217 o_gl_array[3 + 1 * 4] = 0.0;
218 o_gl_array[3 + 2 * 4] = 0.0;
219 o_gl_array[3 + 3 * 4] = 1.0;
220 }
221 /**
222 * ���������������������������
223 */
224 private final static PMatrix3D _lh_mat=new PMatrix3D(-1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1);
225
226 /**
227 * ���������������Processing���Matrix���������������������
228 * @param i_src
229 * @param i_mode
230 * @param o_pmatrix
231 */
232 protected static void matResult2PMatrix3D(NyARDoubleMatrix44 i_src,int i_mode,PMatrix3D o_pmatrix)
233 {
234 o_pmatrix.m00 = (float)i_src.m00;
235 o_pmatrix.m01 = (float)i_src.m01;
236 o_pmatrix.m02 = (float)i_src.m02;
237 o_pmatrix.m03 = (float)i_src.m03;
238 o_pmatrix.m10 = (float)i_src.m10;//mirror
239 o_pmatrix.m11 = (float)i_src.m11;//mirror
240 o_pmatrix.m12 = (float)i_src.m12;//mirror
241 o_pmatrix.m13 = (float)i_src.m13;//mirror
242 o_pmatrix.m20 = (float)-i_src.m20;
243 o_pmatrix.m21 = (float)-i_src.m21;
244 o_pmatrix.m22 = (float)-i_src.m22;
245 o_pmatrix.m23 = (float)-i_src.m23;
246 o_pmatrix.m30 = 0.0f;
247 o_pmatrix.m31 = 0.0f;
248 o_pmatrix.m32 = 0.0f;
249 o_pmatrix.m33 = 1.0f;
250 if(i_mode==NyAR4PsgConfig.CS_LEFT_HAND)
251 {
252 o_pmatrix.apply(_lh_mat);
253 }
254 }
255 /**
256 * ������������������i_mat������������������������������������������������������������
257 * @param i_mat
258 * @param i_x
259 * @param i_y
260 * @return
261 */
262 protected PVector screen2MarkerCoordSystem(NyARDoubleMatrix44 i_mat,int i_x,int i_y)
263 {
264 PVector ret=new PVector();
265 NyARDoublePoint3d tmp=new NyARDoublePoint3d();
266 this._frustum.unProjectOnMatrix(i_x, i_y,i_mat,tmp);
267 ret.x=(float)tmp.x;
268 ret.y=(float)tmp.y;
269 ret.z=(float)tmp.z;
270 if(this._config._coordinate_system==NyAR4PsgConfig.CS_LEFT_HAND){
271 ret.x*=-1;
272 }
273 return ret;
274 }
275 /**
276 * PImage������������������������������������������������������
277 * @param i_mat
278 * @param i_x1
279 * @param i_y1
280 * @param i_x2
281 * @param i_y2
282 * @param i_x3
283 * @param i_y3
284 * @param i_x4
285 * @param i_y4
286 * @param i_out_w_pix
287 * @param i_out_h_pix
288 * @return
289 */
290 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)
291 {
292 //WrapRaster���������������������
293 if(!this._src_raster.hasBuffer()){
294 this._ref_papplet.die("_rel_detector is null.(Function detect() was never called. )");
295 }
296 PImage img=new PImage(i_out_w_pix,i_out_h_pix);
297 img.parent=this._ref_papplet;
298 try{
299 NyARDoublePoint3d[] pos=NyARDoublePoint3d.createArray(4);
300 i_mat.transform3d(i_x1, i_y1,0, pos[1]);
301 i_mat.transform3d(i_x2, i_y2,0, pos[0]);
302 i_mat.transform3d(i_x3, i_y3,0, pos[3]);
303 i_mat.transform3d(i_x4, i_y4,0, pos[2]);
304 //4������������������
305 NyARDoublePoint2d[] pos2=NyARDoublePoint2d.createArray(4);
306 for(int i=3;i>=0;i--){
307 this._frustum.project(pos[i],pos2[i]);
308 }
309 PImageRaster out_raster=new PImageRaster(i_out_w_pix,i_out_h_pix);
310 out_raster.wrapBuffer(img);
311 if(!this._preader.read4Point(this._src_raster,pos2,0,0,1,out_raster))
312 {
313 throw new Exception("this._preader.read4Point failed.");
314 }
315 return img;
316 }catch(Exception e){
317 e.printStackTrace();
318 this._ref_papplet.die("Exception occurred at MultiARTookitMarker.pickupImage");
319 return null;
320 }
321 }
322 /**
323 * ������������������������������������������������������������������������������
324 * ���������������������������������������������������������������
325 * <p>������ -
326 * ������������������gluUnproject������������������������������������������������������������������������������
327 * ���������������������������������������������
328 * http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml
329 * ARToolKit���������������������������������������������OpenGL���unProject������ix,iy���������������������������������������������������������������������������������������
330 * </p>
331 * @param ix
332 * ���������������������������
333 * @param iy
334 * ������������������
335 * @param o_point_on_screen
336 * ���������������
337 */
338 /* public final PVector unProject(double ix,double iy)
339 {
340 double n=(this._frustum_rh.m23/(this._frustum_rh.m22-1));
341 NyARDoubleMatrix44 m44=this._inv_frustum_rh;
342 double v1=(this._screen_size.w-ix-1)*2/this._screen_size.w-1.0;//ARToolKit���Frustram������������������
343 double v2=(this._screen_size.h-iy-1)*2/this._screen_size.h-1.0;
344 double v3=2*n-1.0;
345 double b=1/(m44.m30*v1+m44.m31*v2+m44.m32*v3+m44.m33);
346 o_point_on_screen.x=(m44.m00*v1+m44.m01*v2+m44.m02*v3+m44.m03)*b;
347 o_point_on_screen.y=(m44.m10*v1+m44.m11*v2+m44.m12*v3+m44.m13)*b;
348 o_point_on_screen.z=(m44.m20*v1+m44.m21*v2+m44.m22*v3+m44.m23)*b;
349 return;
350 }
351 */ /**
352 * ���������������������������������������������������������������������������������������������������������������������������������������������������������
353 * ���������������������������������������������
354 * @param ix
355 * ���������������������������
356 * @param iy
357 * ���������������������������
358 * @param i_mat
359 * ������������������������������
360 * @param o_pos
361 * ������������������������������������������������
362 */
363 /* public final PVector unProjectOnCamera(double ix,double iy,PMatrix3D i_mat)
364 {
365 //������������������
366 this.unProject(ix,iy,o_pos);
367 //������������������������������
368 double nx=i_mat.m02;
369 double ny=i_mat.m12;
370 double nz=i_mat.m22;
371 double mx=i_mat.m03;
372 double my=i_mat.m13;
373 double mz=i_mat.m23;
374 double t=(nx*mx+ny*my+nz*mz)/(nx*o_pos.x+ny*o_pos.y+nz*o_pos.z);
375 o_pos.x=t*o_pos.x;
376 o_pos.y=t*o_pos.y;
377 o_pos.z=t*o_pos.z;
378 }
379 */ /**
380 * ������������������������������������������������������������������������������������������������������������������������
381 * ARToolKit������P175���������������������������������
382 * @param ix
383 * ���������������������������
384 * @param iy
385 * ���������������������������
386 * @param i_mat
387 * ������������������������������
388 * @param o_pos
389 * ������������������������������������������������
390 * @return
391 * ���������������������������true������������������
392 */
393 /* public final PVector unProjectOnMatrix(double ix,double iy,PMatrix3D i_mat)
394 {
395 //������������������������������������
396 unProjectOnCamera(ix,iy,i_mat,o_pos);
397 //������������������
398 NyARDoubleMatrix44 m=new NyARDoubleMatrix44();
399 if(!m.inverse(i_mat)){
400 return false;
401 }
402 m.transform3d(o_pos, o_pos);
403 return true;
404 }
405 */ /**
406 * ������������������������������������������������������������������������������
407 * @param i_x
408 * ������������������������
409 * @param i_y
410 * ������������������������
411 * @param i_z
412 * ������������������������
413 * @param o_pos2d
414 * ������������������������������������������������
415 */
416 /* public final PVector project(double i_x,double i_y,double i_z)
417 {
418 NyARDoubleMatrix44 m=this._frustum_rh;
419 double v3_1=1/i_z*m.m32;
420 double w=this._screen_size.w;
421 double h=this._screen_size.h;
422 o_pos2d.x=w-(1+(i_x*m.m00+i_z*m.m02)*v3_1)*w/2;
423 o_pos2d.y=h-(1+(i_y*m.m11+i_z*m.m12)*v3_1)*h/2;
424 return;
425 }
426 */}
427

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