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

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