Debian Codegraph @

vcg/wrap/gl/shot.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25 $Log: shot.h,v $
26 Revision 1.13 2007/05/31 09:39:56 cignoni
27 Small gcc compiling issues
28
29 Revision 1.12 2006/12/18 16:02:57 matteodelle
30 minor eroor correction on variable names
31
32 Revision 1.11 2006/12/18 15:26:24 callieri
33 added a function to approximate a far plane value given a shot and the mesh bbox
34
35 Revision 1.10 2006/12/18 14:28:07 matteodelle
36 *** empty log message ***
37
38 Revision 1.9 2006/12/18 09:46:39 callieri
39 camera+shot revamp: changed field names to something with more sense, cleaning of various functions, correction of minor bugs/incongruences, removal of the infamous reference in shot.
40
41 Revision 1.8 2006/01/11 16:06:25 matteodelle
42 *** empty log message ***
43
44
45 Revision 1.8 2005/01/11 17:06:30 dellepiane
46 FromTrackball() coorected (similarity->Extrinsics
47
48 Revision 1.7 2005/11/25 10:33:33 spinelli
49 shot.camera -> shot.Intrinsics
50 shot.similarity.Matrix() -> shot.Extrinsics.Matrix()
51
52 Revision 1.6 2005/02/22 11:15:01 ganovelli
53 added vcg namespace
54
55 Revision 1.5 2005/02/11 11:43:09 tommyfranken
56 FromTrackball() corrected
57
58 Revision 1.4 2004/12/15 18:45:06 tommyfranken
59 *** empty log message ***
60
61 Revision 1.3 2004/11/03 09:41:57 ganovelli
62 added FromTrackball and fixed include names (Poiint to point)
63
64 Revision 1.2 2004/10/05 19:04:45 ganovelli
65 changed from classes to functions
66
67 Revision 1.1 2004/09/15 22:59:13 ganovelli
68 creation
69
70 Revision 1.2 2004/09/06 21:41:30 ganovelli
71 *** empty log message ***
72
73 Revision 1.1 2004/09/03 13:01:51 ganovelli
74 creation
75
76 ****************************************************************************/
77
78
79 #ifndef __VCGLIB_GLSHOT
80 #define __VCGLIB_GLSHOT
81
82 // include vcg stuff
83 #include <vcg/space/point2.h>
84 #include <vcg/space/point3.h>
85 #include <vcg/math/similarity.h>
86
87 #include <vcg/math/shot.h>
88
89 // include wrap stuff
90 #include <wrap/gui/trackball.h>
91 #include <wrap/gl/math.h>
92 #include <wrap/gl/camera.h>
93
94 template <class ShotType>
95 struct GlShot {
96
97 typedef typename ShotType::ScalarType ScalarType;
98 typedef GlCamera<typename ShotType::CameraType> GlCameraType;
99
100 /// returns the OpenGL 4x4 MODELVIEW matrix that describes the shot position and orientation (extrinsics)
101 static void MatrixGL(ShotType & shot,vcg::Matrix44<ScalarType> & m)
102 {
103 m = shot.Extrinsics.Matrix();
104 }
105
106 /// set the OpenGL MODELVIEW matrix to match the shot (extrinsics)
107 static void TransformGL(vcg::Shot<ScalarType> & shot)
108 {
109 vcg::Matrix44<ScalarType> m;
110 MatrixGL(shot,m);
111 glMultMatrix(m);
112 }
113
114 /// set the OpenGL PROJECTION and MODELVIEW matrix to match camera+shot. requires near and far plane
115 static void SetView(vcg::Shot<ScalarType> & shot, ScalarType nearDist, ScalarType farDist)
116 {
117 assert(glGetError() == 0);
118 glMatrixMode(GL_PROJECTION);
119 glPushMatrix();
120 glLoadIdentity();
121
122 assert(glGetError() == 0);
123 GlCameraType::TransformGL(shot.Intrinsics, nearDist, farDist); // apply camera/projection transformation
124 assert(glGetError() == 0);
125
126 glMatrixMode(GL_MODELVIEW);
127 glPushMatrix();
128 glLoadIdentity();
129
130 GlShot<ShotType>::TransformGL(shot); // apply similarity/modelview transformation
131 assert(glGetError() == 0);
132 }
133
134 /// restore the previous OpenGL modelview and projection state. to be called AFTER a SetView
135 static void UnsetView()
136 {
137 glPushAttrib(GL_TRANSFORM_BIT);
138 glMatrixMode(GL_MODELVIEW);
139 glPopMatrix();
140 glMatrixMode(GL_PROJECTION);
141 glPopMatrix();
142 glPopAttrib();
143 }
144
145 /// given a shot and the mesh bounding box, returns an approximate far plane distance
146 /// distance is always approximated by excess
147 static ScalarType GetFarPlane(vcg::Shot<ScalarType> & shot, vcg::Box3<ScalarType> bbox)
148 {
149 ScalarType farDist;
150
151 vcg::Point3<ScalarType> farcorner;
152 vcg::Point3<ScalarType> campos = shot.Extrinsics.tra;
153
154 if (abs(campos.X() - bbox.max.X()) > abs(campos.X() - bbox.min.X()))
155 farcorner.X() = bbox.max.X();
156 else
157 farcorner.X() = bbox.min.X();
158
159 if (abs(campos.Y() - bbox.max.Y()) > abs(campos.Y() - bbox.min.Y()))
160 farcorner.Y() = bbox.max.Y();
161 else
162 farcorner.Y() = bbox.min.Y();
163
164 if (abs(campos.Z() - bbox.max.Z()) > abs(campos.Z() - bbox.min.Z()))
165 farcorner.Z() = bbox.max.Z();
166 else
167 farcorner.Z() = bbox.min.Z();
168
169 farDist = (campos - farcorner).Norm();
170
171 return farDist;
172 }
173
174 static void SetSubView(vcg::Shot<ScalarType> & shot,
175 vcg::Point2<ScalarType> p1,
176 vcg::Point2<ScalarType> p2)
177 {
178 glMatrixMode(GL_PROJECTION);
179 glPushMatrix();
180 glLoadIdentity();
181 assert(glGetError() == 0);
182 GlCameraType::SetSubView(shot.Intrinsics,p1,0,1000,p2);
183 assert(glGetError() == 0);
184 glMatrixMode(GL_MODELVIEW);
185 glPushMatrix();
186 glLoadIdentity();
187 GlShot<ShotType>::TransformGL(shot); // apply similarity/modelview transformation
188 assert(glGetError() == 0);
189 }
190
191
192
193 /**********************************
194 DEFINE SHOT FROM TRACKBALL
195 Adds to a given shot the trackball transformations.
196 After this operation the trackball should be resetted, to avoid
197 multiple apply of the same transformation.
198 ***********************************/
199 static void FromTrackball(const vcg::Trackball & tr,
200 vcg::Shot<ScalarType> & sShot,
201 vcg::Shot<ScalarType> & shot )
202 {
203 vcg::Point3<ScalarType> cen; cen.Import(tr.center);
204 vcg::Point3<ScalarType> tra; tra.Import(tr.track.tra);
205 vcg::Matrix44<ScalarType> trM; trM.FromMatrix(tr.track.Matrix());
206
207 vcg::Point3<ScalarType> vp = Inverse(trM)*(sShot.GetViewPoint()-cen) +cen +tra;
208
209 shot.SetViewPoint(vp);
210 shot.Extrinsics.rot = sShot.Extrinsics.rot*trM;
211 shot.Extrinsics.sca = sShot.Extrinsics.sca*(ScalarType)tr.track.sca;
212 }
213 };
214 #endif
215
216
217
218
219
220
221 //private:
222 //
223 // static inline S SQRT( S x) { return sqrt(fabs(x)); }
224 // static inline S CBRT ( S x )
225 // {
226 // if (x == 0) return 0;
227 // else if (x > 0) return pow (x, 1.0 / 3.0);
228 // else return -pow (-x, 1.0 / 3.0);
229 // }
230 // static inline void SINCOS( S x, S & s, S & c)
231 // {
232 // s=sin(x);
233 // c=cos(x);
234 // }
235 // static inline void SINCOSd( double x, double & s, double & c)
236 // {
237 // s=sin(x);
238 // c=cos(x);
239 // }
240 // static inline S CUB( S x ) { return x*x*x; }
241 // static inline S SQR( S x ) { return x*x; }
242 //
243 //public:
244 // void undistorted_to_distorted_sensor_coord (S Xu, S Yu, S & Xd, S & Yd) const
245 // {
246 // const S SQRT3 = S(1.732050807568877293527446341505872366943);
247 // S Ru,Rd,lambda,c,d,Q,R,D,S,T,sinT,cosT;
248 //
249 // if((Xu==0 && Yu==0) || k[0] == 0)
250 // {
251 // Xd = Xu;
252 // Yd = Yu;
253 // return;
254 // }
255 //
256 // Ru = hypot (Xu, Yu); /* SQRT(Xu*Xu+Yu*Yu) */
257 // c = 1 / k[0];
258 // d = -c * Ru;
259 //
260 // Q = c / 3;
261 // R = -d / 2;
262 // D = CUB (Q) + SQR (R);
263 //
264 // if (D >= 0) /* one real root */
265 // {
266 // D = SQRT (D);
267 // S = CBRT (R + D);
268 // T = CBRT (R - D);
269 // Rd = S + T;
270 //
271 // if (Rd < 0)
272 // Rd = SQRT (-1 / (3 * k[0]));
273 // }
274 // else /* three real roots */
275 // {
276 // D = SQRT (-D);
277 // S = CBRT (hypot (R, D));
278 // T = atan2 (D, R) / 3;
279 // SINCOS (T, sinT, cosT);
280 //
281 // /* the larger positive root is 2*S*cos(T) */
282 // /* the smaller positive root is -S*cos(T) + SQRT(3)*S*sin(T) */
283 // /* the negative root is -S*cos(T) - SQRT(3)*S*sin(T) */
284 // Rd = -S * cosT + SQRT3 * S * sinT; /* use the smaller positive root */
285 // }
286 //
287 // lambda = Rd / Ru;
288 //
289 // Xd = Xu * lambda;
290 // Yd = Yu * lambda;
291 // }
292 //
293 //
294 //void correction(double k, float i, float j, float &disi, float &disj)
295 //{
296 // // (i,j) punto nell'immagine distorta
297 // // (disi,disj) punto nell'immagine corretta (undistorted)
298 // float hyp;
299 // float I,J,ni,nj;
300 // float ratio = 1;
301 //
302 // ni = i-viewport[0]/2;
303 // nj = j-viewport[1]/2;
304 // hyp = ni*ni + nj*nj;
305 //
306 // I = ni * (1+ k * hyp);
307 // J = nj * (1+ k * hyp);
308 //
309 // disi = (I*ratio+viewport[0]/2);
310 // disj = (J*ratio+viewport[1]/2);
311 //}
312 //
313 //
314 //
315 //void distorsion( float k ,float i, float j,double & disi, double &disj)
316 //{
317 // // (i,j) punto nell'immagine corretta (undistorted)
318 // // (disi,disj) punto nell'immagine distorta
319 // float hyp;
320 // int _I,_J;
321 // float I,J,ni,nj;
322 // I = i-viewport[0]/2;
323 // J = j-viewport[1]/2;
324 // hyp = sqrt(I*I + J*J);
325 // if((k==0.0) || (hyp <0.001))
326 // {
327 // disi = i;
328 // disj = j;
329 // }
330 // else
331 // {
332 // undistorted_to_distorted_sensor_coord (I, J, disi, disj);
333 // disi += viewport[0]/2;
334 // disj += viewport[1]/2;
335 //
336 //
337 //// hyp = (viewport[0]*viewport[0] + viewport[1]*viewport[1])/4;
338 //// ni = SX/2 + SX/2 * cam.k[0] * hyp;
339 ///// nj = SY/2 + SY/2 * cam.k[0] * hyp;
340 //// float ratio = sqrt(hyp/(ni*ni + nj*nj));
341 // float ratio=1;
342 //
343 //
344 //
345 // //----------- Maple
346 // // float t0,t1,t2,sol;
347 //
348 // //t0 = 1/k*pow((108.0*hyp+12.0*sqrt(3.0)*sqrt((4.0+27.0*hyp*hyp*k)/k))*k*k,0.3333333333333333)/6.0-2.0/pow((108.0*hyp+12.0*sqrt(3.0)*sqrt((4.0+27.0*hyp*hyp*k)/k))*k*k,0.3333333333333333);
349 //
350 //
351 // //t1 = -1/k*pow((108.0*hyp+12.0*sqrt(3.0)*sqrt((4.0+27.0*hyp*hyp*k)/k))*k*k,0.3333333333333333)/12.0+1/pow((108.0*hyp+12.0*sqrt(3.0)*sqrt((4.0+27.0*hyp*
352 // //hyp*k)/k))*k*k,0.3333333333333333)+sqrt(-1.0)*sqrt(3.0)*(1/k*pow((108.0*hyp+
353 // //12.0*sqrt(3.0)*sqrt((4.0+27.0*hyp*hyp*k)/k))*k*k,0.3333333333333333)/6.0+2.0/
354 // //pow((108.0*hyp+12.0*sqrt(3.0)*sqrt((4.0+27.0*hyp*hyp*k)/k))*k*k,
355 // //0.3333333333333333))/2.0;
356 //
357 // //t2 = -1/k*pow((108.0*hyp+12.0*sqrt(3.0)*sqrt((4.0+27.0*hyp*hyp*k)/k))*k*k,0.3333333333333333)/12.0+1/pow((108.0*hyp+12.0*sqrt(3.0)*sqrt((4.0+27.0*hyp*
358 // //hyp*k)/k))*k*k,0.3333333333333333)-sqrt(-1.0)*sqrt(3.0)*(1/k*pow((108.0*hyp+
359 // //12.0*sqrt(3.0)*sqrt((4.0+27.0*hyp*hyp*k)/k))*k*k,0.3333333333333333)/6.0+2.0/
360 // //pow((108.0*hyp+12.0*sqrt(3.0)*sqrt((4.0+27.0*hyp*hyp*k)/k))*k*k,
361 // //0.3333333333333333))/2.0;
362 //
363 // //sol = (t0>t1)?t0:t1;
364 // //sol = (sol<t2)?t2:sol;
365 // //sol = t0;
366 // //ni = sol*I/hyp;
367 // //nj = sol*J/hyp;
368 // ////-----------
369 //
370 // //disi = (ni*ratio+viewport[0]/2);
371 // //disj = (nj*ratio+viewport[1]/2);
372 // }
373 //}
374 // void ResizeGridMap(const int & si,const int & sj ){
375 // int j;
376 // gridMap.resize(sj+1);
377 // for(j=0; j < sj+1; j++)
378 // gridMap[j].resize(si+1);
379 // }
380 // void UpdateGridMap(){
381 // int sj = gridMap.size();
382 // int si = gridMap[0].size();
383 // int i,j;
384 // for(j=0; j < sj; j++)
385 // for(i=0; i < gridMap[0].size(); i++)
386 // // gridMap[i][j] = Point2<scalar> (i/(double)(si-1),j/(double)(sj-1));
387 // {
388 // double disi,disj;
389 // distorsion( k[0] ,(i/(double)(si-1))*viewport[0], (j/(double)(sj-1))*viewport[1],disi,disj);
390 // gridMap[i][j] = Point2<scalar> (disi/viewport[0],disj/viewport[1]);
391 // }
392 // }
393 //
394 // inline Intrinsics
395 // {
396 // k[0]=k[1]=k[2]=k[3]=0.0;
397 // valid = false;
398 // ortho = false;
399 // ResizeGridMap(100,100);// da spostare altrove
400 // }
401 //
402 // inline bool IsValid()
403 // {
404 // return valid;
405 // }
406 //
407 // inline bool IsOrtho() const
408 // {
409 // return ortho;
410 // }
411 //
412 // inline void SetInvalid()
413 // {
414 // valid = false;
415 // }
416 //
417 // inline void SetOrtho(bool isOrtho=true)
418 // {
419 // ortho = isOrtho;
420 // }
421 //
422 // // Genera una camera standard
423 // void Standard()
424 // {
425 // valid = true;
426 // ortho = false;
427 // view_p = vectorial(0,0,0);
428 // x_axis = vectorial(1,0,0);
429 // y_axis = vectorial(0,1,0);
430 // z_axis = vectorial(0,0,1);
431 // f = 25.75;
432 // s = Point2<S>(0.0074,0.0074);
433 // c = Point2<S>(320,240);
434 // viewport[0] = 640;
435 // viewport[1] = 480;
436 // k[0] = 0;
437 // k[1] = 0;
438 // k[2] = 0;
439 // k[3] = 0;
440 // }
441 //
442 // // Trasla la camera (world coordinate)
443 // inline void Translate( const vectorial & t )
444 // {
445 // view_p += t;
446 // }
447 //
448 // // Trasla la camera (camera coordinate)
449 // inline void Move( const vectorial & t )
450 // {
451 // view_p+= x_axis * t[0]+y_axis * t[1] + z_axis * t[2];
452 // }
453 //
454 // // scala la camera
455 // inline void Scale(const scalar & sc){
456 // view_p *=sc;
457 // s[0]*=sc;
458 // s[1]*=sc;
459 // f*=sc;
460 // //printf("sc\n");
461 // }
462 //
463 //
464 //
465 // // NOTA funziona solo se l'ultima colonna di m e' 0,0,0,1
466 // void Apply( const Matrix44<S> & m )
467 // {
468 // // Passo 1: calcolo pseudo inversa di m
469 // S s11,s12,s13;
470 // S s21,s22,s23;
471 // S s31,s32,s33;
472 // S s41,s42,s43;
473 //
474 // {
475 // S t4 = m[0][0]*m[1][1];
476 // S t6 = m[0][0]*m[2][1];
477 // S t8 = m[1][0]*m[0][1];
478 // S t10 = m[1][0]*m[2][1];
479 // S t12 = m[2][0]*m[0][1];
480 // S t14 = m[2][0]*m[1][1];
481 // S t17 = 1/(t4*m[2][2]-t6*m[1][2]-t8*m[2][2]+t10*m[0][2]+t12*m[1][2]-t14*m[0][2]);
482 // S t27 = m[1][0]*m[2][2];
483 // S t28 = m[2][0]*m[1][2];
484 // S t31 = m[0][0]*m[2][2];
485 // S t32 = m[2][0]*m[0][2];
486 // S t35 = m[0][0]*m[1][2];
487 // S t36 = m[1][0]*m[0][2];
488 // S t49 = m[3][0]*m[1][1];
489 // S t51 = m[3][0]*m[2][1];
490 // S t59 = m[3][0]*m[0][1];
491 // s11 = -(-m[1][1]*m[2][2]+m[2][1]*m[1][2])*t17;
492 // s12 = -( m[0][1]*m[2][2]-m[2][1]*m[0][2])*t17;
493 // s13 = ( m[0][1]*m[1][2]-m[1][1]*m[0][2])*t17;
494 // s21 = (-t27+t28)*t17;
495 // s22 = -(-t31+t32)*t17;
496 // s23 = -( t35-t36)*t17;
497 // s31 = -(-t10+t14)*t17;
498 // s32 = (-t6 +t12)*t17;
499 // s33 = ( t4 -t8 )*t17;
500 // s41 = -(t10*m[3][2]-t27*m[3][1]-t14*m[3][2]+t28*m[3][1]+t49*m[2][2]-t51*m[1][2])*t17;
501 // s42 = -(-t6*m[3][2]+t31*m[3][1]+t12*m[3][2]-t32*m[3][1]-t59*m[2][2]+t51*m[0][2])*t17;
502 // s43 = (-t4*m[3][2]+t35*m[3][1]+t8 *m[3][2]-t36*m[3][1]-t59*m[1][2]+t49*m[0][2])*t17;
503 // 1.0;
504 // }
505 //
506 // //Matrix44<S> t2 = tt*m;
507 // //print(t2);
508 // // Fase 2: Calcolo nuovo punto di vista
509 // {
510 // S t1 = view_p[2]*s31;
511 // S t3 = view_p[2]*s21;
512 // S t5 = s43*s21;
513 // S t7 = s43*s31;
514 // S t9 = view_p[1]*s31;
515 // S t11 = view_p[1]*s21;
516 // S t13 = s42*s31;
517 // S t15 = s42*s21;
518 // S t17 = view_p[0]*s32;
519 // S t19 = view_p[0]*s22;
520 // S t21 = s41*s32;
521 // S t23 = s41*s22;
522 // S t25 = -t1*s22+t3*s32-t5*s32+t7*s22+t9*s23-t11*s33-t13*s23+t15*s33-t17*s23+t19*s33+t21*s23-t23*s33;
523 // S t39 = 1/(s11*s22*s33-s11*s32*s23-s21*s12*s33+s21*s32*s13+s31*s12*s23-s31*s22*s13);
524 // S t41 = view_p[0]*s12;
525 // S t45 = s41*s12;
526 // S t47 = view_p[2]*s11;
527 // S t50 = s43*s11;
528 // S t53 = view_p[1]*s11;
529 // S t56 = s42*s11;
530 // S t59 = t41*s33-t17*s13+t21*s13-t45*s33+t47*s32-t1*s12-t50*s32+t7*s12-t53*s33+t9*s13+t56*s33-t13*s13;
531 // S t73 = t15*s13-t56*s23+t19*s13-t41*s23-t23*s13+t45*s23-t11*s13+t53*s23+t3*s12-t47*s22-t5*s12+t50*s22;
532 //
533 // view_p[0] = t25*t39;
534 // view_p[1] = -t59*t39;
535 // view_p[2] = -t73*t39;
536 // }
537 //
538 // // Fase 3: Calcol nuovo sistema di riferimento
539 // {
540 // S A00 = s11*x_axis[0]+s12*x_axis[1]+s13*x_axis[2];
541 // S A01 = s11*y_axis[0]+s12*y_axis[1]+s13*y_axis[2];
542 // S A02 = s11*z_axis[0]+s12*z_axis[1]+s13*z_axis[2];
543 // // S A03 = 0.0;
544 // S A10 = s21*x_axis[0]+s22*x_axis[1]+s23*x_axis[2];
545 // S A11 = s21*y_axis[0]+s22*y_axis[1]+s23*y_axis[2];
546 // S A12 = s21*z_axis[0]+s22*z_axis[1]+s23*z_axis[2];
547 // // S A13 = 0.0;
548 // S A20 = s31*x_axis[0]+s32*x_axis[1]+s33*x_axis[2];
549 // S A21 = s31*y_axis[0]+s32*y_axis[1]+s33*y_axis[2];
550 // S A22 = s31*z_axis[0]+s32*z_axis[1]+s33*z_axis[2];
551 //
552 // x_axis[0] = A00; x_axis[1] = A10; x_axis[2] = A20;
553 // y_axis[0] = A01; y_axis[1] = A11; y_axis[2] = A21;
554 // z_axis[0] = A02; z_axis[1] = A12; z_axis[2] = A22;
555 // // S A1[2][3] = 0.0;
556 // // S A1[3][0] = 0.0;
557 // // S A1[3][1] = 0.0;
558 // // S A1[3][2] = 0.0;
559 // // S A1[3][3] = 1.0;
560 // }
561 // }
562 //
563 // /*
564 // // Applica una trasformazione
565 // void Apply( const Matrix44<S> & m )
566 // {
567 // Point3<S> tx = view_p+x_axis;
568 // Point3<S> ty = view_p+y_axis;
569 // Point3<S> tz = view_p+z_axis;
570 //
571 // view_p = m.Apply(view_p);
572 //
573 // x_axis = m.Apply(tx) - view_p;
574 // y_axis = m.Apply(ty) - view_p;
575 // z_axis = m.Apply(tz) - view_p;
576 // }
577 //
578 // // Applica una trasformazione ma bene!
579 // void Stable_Apply( const Matrix44<S> & m )
580 // {
581 // Point3<S> tx = view_p+x_axis;
582 // Point3<S> ty = view_p+y_axis;
583 // Point3<S> tz = view_p+z_axis;
584 //
585 // view_p = m.Stable_Apply(view_p);
586 //
587 // x_axis = m.Stable_Apply(tx) - view_p;
588 // y_axis = m.Stable_Apply(ty) - view_p;
589 // z_axis = m.Stable_Apply(tz) - view_p;
590 // }
591 //
592 // */
593 //
594 // void Project( const vectorial & p, Point2<S> & q ) const
595 // {
596 // vectorial dp = p - view_p;
597 // S dx = dp*x_axis;
598 // S dy = dp*y_axis;
599 // S dz = dp*z_axis;
600 //
601 // S tx = dx;
602 // S ty = -dy;
603 // S qx,qy;
604 //
605 // // nota: per le camere ortogonali viewportM vale 1
606 // if(!IsOrtho())
607 // {
608 // tx *= f/dz;
609 // ty *= f/dz;
610 //
611 // undistorted_to_distorted_sensor_coord(tx,ty,qx,qy);
612 //
613 // q[0] = qx/s[0]+c[0];
614 // q[1] = qy/s[1]+c[1];
615 // }
616 // else
617 // {
618 // q[0] = tx/(s[0]*viewportM)+c[0];
619 // q[1] = ty/(s[1]*viewportM)+c[1];
620 // }
621 // }
622 //
623 //#if 1
624 // void Show( FILE * fp )
625 // {
626 // if(valid)
627 // fprintf(fp,
628 // "posiz.: %g %g %g\n"
629 // "x axis: %g %g %g\n"
630 // "y axis: %g %g %g\n"
631 // "z axis: %g %g %g\n"
632 // "focal : %g scale: %g %g center: %g %g\n"
633 // "viewp.: %d %d distorsion: %g %g %g %g\n"
634 // ,view_p[0],view_p[1],view_p[2]
635 // ,x_axis[0],x_axis[1],x_axis[2]
636 // ,y_axis[0],y_axis[1],y_axis[2]
637 // ,z_axis[0],z_axis[1],z_axis[2]
638 // ,f,s[0],s[1],c[0],c[1]
639 // ,viewport[0],viewport[1],k[0],k[1],k[2],k[3]
640 // );
641 // else
642 // fprintf(fp,"Invalid\n");
643 // }
644 //#endif
645 //
646 // // Legge una camera in descrizione tsai binario
647 // static void load_tsai_bin (FILE *fp, tsai_camera_parameters *cp, tsai_calibration_constants *cc)
648 // {
649 // double sa,
650 // ca,
651 // sb,
652 // cb,
653 // sg,
654 // cg;
655 //
656 // fread(&(cp->Ncx),sizeof(double),1,fp);
657 // fread(&(cp->Nfx),sizeof(double),1,fp);
658 // fread(&(cp->dx),sizeof(double),1,fp);
659 // fread(&(cp->dy),sizeof(double),1,fp);
660 // fread(&(cp->dpx),sizeof(double),1,fp);
661 // fread(&(cp->dpy),sizeof(double),1,fp);
662 // fread(&(cp->Cx),sizeof(double),1,fp);
663 // fread(&(cp->Cy),sizeof(double),1,fp);
664 // fread(&(cp->sx),sizeof(double),1,fp);
665 //
666 // fread(&(cc->f),sizeof(double),1,fp);
667 // fread(&(cc->kappa1),sizeof(double),1,fp);
668 // fread(&(cc->Tx),sizeof(double),1,fp);
669 // fread(&(cc->Ty),sizeof(double),1,fp);
670 // fread(&(cc->Tz),sizeof(double),1,fp);
671 // fread(&(cc->Rx),sizeof(double),1,fp);
672 // fread(&(cc->Ry),sizeof(double),1,fp);
673 // fread(&(cc->Rz),sizeof(double),1,fp);
674 //
675 //
676 // SINCOSd (cc->Rx, sa, ca);
677 // SINCOSd (cc->Ry, sb, cb);
678 // SINCOSd (cc->Rz, sg, cg);
679 //
680 // cc->r1 = cb * cg;
681 // cc->r2 = cg * sa * sb - ca * sg;
682 // cc->r3 = sa * sg + ca * cg * sb;
683 // cc->r4 = cb * sg;
684 // cc->r5 = sa * sb * sg + ca * cg;
685 // cc->r6 = ca * sb * sg - cg * sa;
686 // cc->r7 = -sb;
687 // cc->r8 = cb * sa;
688 // cc->r9 = ca * cb;
689 //
690 // fread(&(cc->p1),sizeof(double),1,fp);
691 // fread(&(cc->p2),sizeof(double),1,fp);
692 // }
693 //
694 // void load_tsai (FILE *fp, tsai_camera_parameters *cp, tsai_calibration_constants *cc)
695 // {
696 // double sa,
697 // ca,
698 // sb,
699 // cb,
700 // sg,
701 // cg;
702 //
703 // fscanf (fp, "%lf", &(cp->Ncx));
704 // fscanf (fp, "%lf", &(cp->Nfx));
705 // fscanf (fp, "%lf", &(cp->dx));
706 // fscanf (fp, "%lf", &(cp->dy));
707 // fscanf (fp, "%lf", &(cp->dpx));
708 // fscanf (fp, "%lf", &(cp->dpy));
709 // fscanf (fp, "%lf", &(cp->Cx));
710 // fscanf (fp, "%lf", &(cp->Cy));
711 // fscanf (fp, "%lf", &(cp->sx));
712 //
713 // fscanf (fp, "%lf", &(cc->f));
714 // fscanf (fp, "%lf", &(cc->kappa1));
715 // fscanf (fp, "%lf", &(cc->Tx));
716 // fscanf (fp, "%lf", &(cc->Ty));
717 // fscanf (fp, "%lf", &(cc->Tz));
718 // fscanf (fp, "%lf", &(cc->Rx));
719 // fscanf (fp, "%lf", &(cc->Ry));
720 // fscanf (fp, "%lf", &(cc->Rz));
721 //
722 // SINCOSd (cc->Rx, sa, ca);
723 // SINCOSd (cc->Ry, sb, cb);
724 // SINCOSd (cc->Rz, sg, cg);
725 //
726 // cc->r1 = cb * cg;
727 // cc->r2 = cg * sa * sb - ca * sg;
728 // cc->r3 = sa * sg + ca * cg * sb;
729 // cc->r4 = cb * sg;
730 // cc->r5 = sa * sb * sg + ca * cg;
731 // cc->r6 = ca * sb * sg - cg * sa;
732 // cc->r7 = -sb;
733 // cc->r8 = cb * sa;
734 // cc->r9 = ca * cb;
735 //
736 // fscanf (fp, "%lf", &(cc->p1));
737 // fscanf (fp, "%lf", &(cc->p2));
738 // }
739 //
740 // // Importa una camera dal formato tsai
741 // void import( const tsai_camera_parameters & cp,
742 // const tsai_calibration_constants & cc,
743 // const int image_viewport[2]
744 // )
745 // {
746 // assert(!IsOrtho());
747 // valid = true;
748 // x_axis[0] = cc.r1; x_axis[1] = cc.r2; x_axis[2] = cc.r3;
749 // y_axis[0] = cc.r4; y_axis[1] = cc.r5; y_axis[2] = cc.r6;
750 // z_axis[0] = cc.r7; z_axis[1] = cc.r8; z_axis[2] = cc.r9;
751 //
752 // view_p[0] = - (cc.Tx * x_axis[0] + cc.Ty * y_axis[0] + cc.Tz * z_axis[0]);
753 // view_p[1] = - (cc.Tx * x_axis[1] + cc.Ty * y_axis[1] + cc.Tz * z_axis[1]);
754 // view_p[2] = - (cc.Tx * x_axis[2] + cc.Ty * y_axis[2] + cc.Tz * z_axis[2]);
755 //
756 // s[0] = cp.dpx/cp.sx;
757 // s[1] = cp.dpy;
758 // c[0] = cp.Cx;
759 // c[1] = cp.Cy;
760 //
761 // f = cc.f;
762 // viewport[0] = image_viewport[0];
763 // viewport[1] = image_viewport[1];
764 //
765 // k[0] = cc.kappa1;
766 // k[1] = cc.kappa1;
767 // k[2] = 0;
768 // k[2] = 0;
769 // }
770 //
771 // // Esporta una camera in formato tsai
772 // void export( tsai_camera_parameters & cp,
773 // tsai_calibration_constants & cc,
774 // int image_viewport[2]
775 // )
776 // {
777 // assert(!IsOrtho());
778 // cc.r1 = x_axis[0]; cc.r2 = x_axis[1]; cc.r3= x_axis[2] ;
779 // cc.r4 = y_axis[0]; cc.r5 = y_axis[1]; cc.r6= y_axis[2] ;
780 // cc.r7 = z_axis[0]; cc.r8 = z_axis[1]; cc.r9= z_axis[2] ;
781 //
782 // cc.Tx = - (view_p[0] * x_axis[0] + view_p[1] * x_axis[1] + view_p[2] * x_axis[2]);
783 // cc.Ty = - (view_p[0] * y_axis[0] + view_p[1] * y_axis[1] + view_p[2] * y_axis[2]);
784 // cc.Tz = - (view_p[0] * z_axis[0] + view_p[1] * z_axis[1] + view_p[2] * z_axis[2]);
785 //
786 // cp.dpx = s[0];
787 // cp.dpy = s[1];
788 //
789 // cp.Cx= c[0] ;
790 // cp.Cy= c[1] ;
791 // cp.sx= 1;
792 //
793 // cc.f= f ;
794 //
795 // image_viewport[0] = viewport[0];
796 // image_viewport[1] = viewport[1];
797 //
798 // cc.kappa1= k[0] ;
799 // cc.kappa1= k[1] ;
800 // }
801 //
802 //
803 // void Save(FILE * out)
804 // {
805 // fprintf(out,"VIEW_POINT %f %f %f\n", view_p[0],view_p[1],view_p[2]);
806 // fprintf(out,"X_AXIS %f %f %f\n", x_axis[0],x_axis[1],x_axis[2]);
807 // fprintf(out,"Y_AXIS %f %f %f\n", y_axis[0],y_axis[1],y_axis[2]);
808 // fprintf(out,"Z_AXIS %f %f %f\n", z_axis[0],z_axis[1],z_axis[2]);
809 // fprintf(out,"FOCUS_LENGHT %f \n", f);
810 // fprintf(out,"SCALE %f %f \n", s[0], s[1]);
811 // fprintf(out,"VIEWPORT %d %d \n", viewport[0], viewport[1]);
812 // fprintf(out,"VIEWPORTM %f\n", viewportM);
813 // fprintf(out,"RADIAL_DISTORSION %.10g %.10g \n", k[0],k[1]);
814 // fprintf(out,"CENTER %f %f \n", c[0], c[1]);
815 // fprintf(out,"IS_VALID %d\n", IsValid());
816 // fprintf(out,"END_CAMERA\n");
817 // }
818 //
819 // void Load(FILE * in)
820 // {
821 // char row[255];
822 // Standard();
823 // while(!feof(in))
824 // {
825 // fscanf(in,"%s",row);
826 // if(strcmp(row,"VIEW_POINT")==0)
827 // fscanf(in,"%lg %lg %lg",&view_p[0],&view_p[1],&view_p[2]);
828 // else
829 // if(strcmp(row,"X_AXIS")==0)
830 // fscanf(in,"%lg %lg %lg",& x_axis[0],&x_axis[1],&x_axis[2]);
831 // else
832 // if(strcmp(row,"Y_AXIS")==0)
833 // fscanf(in,"%lg %lg %lg",& y_axis[0],&y_axis[1],&y_axis[2]);
834 // else
835 // if(strcmp(row,"Z_AXIS")==0)
836 // fscanf(in,"%lg %lg %lg",& z_axis[0],&z_axis[1],&z_axis[2]);
837 // else
838 // if(strcmp(row,"FOCUS_LENGHT")==0)
839 // fscanf(in,"%lg",&f);
840 // else
841 // if(strcmp(row,"SCALE")==0)
842 // fscanf(in,"%lg %lg",&s[0],&s[1]);
843 // else
844 // if(strcmp(row,"VIEWPORT")==0)
845 // fscanf(in,"%d %d", &viewport[0],&viewport[1]);
846 // else
847 // if(strcmp(row,"VIEWPORTM")==0)
848 // fscanf(in,"%f", &viewportM);
849 // else
850 // if(strcmp(row,"CENTER")==0)
851 // fscanf(in,"%lg %lg", &c[0],&c[1]);
852 // else
853 // if(strcmp(row,"RADIAL_DISTORSION")==0)
854 // fscanf(in,"%lg %lg", &k[0],&k[1]);
855 // else
856 // if(strcmp(row,"IS_VALID")==0)
857 // fscanf(in,"%d",&valid);
858 // if(strcmp(row,"END_CAMERA")==0)
859 // break;
860 // }
861 // }
862 //
863 //#ifdef __GL_H__
864 //
865 //// Prende in ingresso il bounding box dell'oggetto da inquadrare e setta projection e modelmatrix
866 //// in modo da matchare il piu' possibile quelle della camera. Ovviamente (?) si ignora le distorsioni radiali.
867 //// Nota che bb viene utilizzato solo per settare i near e far plane in maniera sensata.
868 //void SetGL(const Box3<scalar> &bb,scalar subx0=0, scalar subx1=1,scalar suby0=0,scalar suby1=1)
869 //{
870 // scalar _,__;
871 // SetGL(_,__,bb,subx0, subx1, suby0, suby1);
872 //
873 //}
874 //
875 //void SetGL(scalar &znear, scalar &zfar,const Box3<scalar> &bb,scalar subx0=0,
876 // scalar subx1=1,scalar suby0=0,scalar suby1=1)
877 //{
878 // glMatrixMode(GL_PROJECTION);
879 // glLoadIdentity();
880 // scalar left,right;
881 // scalar bottom, top;
882 // scalar w,h;
883 //
884 // // La lunghezza focale <f> e' la distanza del piano immagine dal centro di proiezione.
885 // // il che mappa direttamente nella chiamata glFrustum che prende in ingresso
886 // // le coordinate del piano immagine posto a znear.
887 //
888 // float imleft =-c[0]*s[0];
889 // float imright =(viewport[0]-c[0])*s[0];
890 // float imbottom =-c[1]*s[1];
891 // float imtop =(viewport[1]-c[1])*s[1];
892 // znear = Distance(view_p, bb.Center())-bb.Diag();
893 // zfar = Distance(view_p, bb.Center())+bb.Diag();
894 //
895 // w=imright-imleft;
896 // h=imtop-imbottom;
897 //
898 // // Quindi il frustum giusto sarebbe questo,
899 // // glFrustum(imleft, imright, imbottom, imtop, f, zfar);
900 // // ma per amor di opengl conviene spostare il near plane fino ad essere vicino all'oggetto da inquadrare.
901 // // Cambiare f significa amplificare in maniera proporzionale anche i left right ecc.
902 //
903 // // 8/5/02 Nota che il near plane va spostato verso l'oggetto solo se quello calcolato sopra e' maggiore di 'f'
904 // // nota che potrebbe anche succedere che znear <0 (viewpoint vicino ad un oggetto con il bb allungato);
905 // if(znear<f) znear=f;
906 //
907 // float nof = znear/f;
908 // if(subx0==0 && subx1 == 1 && suby0==0 && suby1 == 1)
909 // {
910 // if(!IsOrtho())
911 // glFrustum(imleft*nof, imright*nof, imbottom*nof, imtop*nof, znear, zfar);
912 // else
913 // glOrtho(imleft*viewportM, imright*viewportM, imbottom*viewportM, imtop*viewportM, znear, zfar);
914 // }
915 // else {// nel caso si voglia fare subboxing
916 // left = imleft+w*subx0;
917 // right = imleft+w*subx1;
918 // bottom = imbottom +h*suby0;
919 // top = imbottom +h*suby1;
920 // {
921 // if(!IsOrtho())
922 // glFrustum(left*nof, right*nof, bottom*nof, top*nof, znear, zfar);
923 // else
924 // glOrtho(left*viewportM, right*viewportM, bottom*viewportM, top*viewportM, znear, zfar);
925 // }
926 // }
927 //
928 // glMatrixMode(GL_MODELVIEW);
929 // glLoadIdentity();
930 // scalar l=max(scalar(1.0),view_p.Norm());
931 // gluLookAt(view_p[0], view_p[1], view_p[2],
932 // view_p[0] + (z_axis[0]*l),
933 // view_p[1] + (z_axis[1]*l),
934 // view_p[2] + (z_axis[2]*l),
935 // y_axis[0],y_axis[1],y_axis[2]);
936 //}
937 //// Sposta la camera a caso di in maniera che l'angolo di variazione rispetto al punt c passato sia inferiore a RadAngle
938 ////
939 //void Jitter(Point3<scalar> c, scalar RadAngle)
940 //{
941 // Point3<scalar> rnd(1.0 - 2.0*scalar(rand())/RAND_MAX,
942 // 1.0 - 2.0*scalar(rand())/RAND_MAX,
943 // 1.0 - 2.0*scalar(rand())/RAND_MAX);
944 // rnd.Normalize();
945 // Matrix44<scalar> m,t0,t1,tr;
946 // Point3<scalar> axis = rnd ^ (view_p-c).Normalize();
947 // scalar RadRandAngle=RadAngle*(1.0 - 2.0*scalar(rand())/RAND_MAX);
948 // t0.Translate(c);
949 // t1.Translate(-c);
950 // m.Rotate(ToDeg(RadRandAngle),axis);
951 // tr=t1*m*t0;
952 // Apply(tr);
953 //}
954 //
955 //
956 //
957 //
958 //void glTexGen(int offx =0, // angolo basso sinistra della
959 // int offy=0, // subtexture per la quale si vogliono settare le coordinate
960 // int sx=1, // Dimensioni in Texel
961 // int sy=1,
962 // int Tx=1, // Dimensioni della texture
963 // int Ty=1)
964 //{
965 // // prendi la rototraslazione che
966 // // trasforma la coordinata nel
967 // // sistema di coordinate della camera
968 // Matrix44d M;
969 // M[0][0] = x_axis[0];
970 // M[0][1] = x_axis[1];
971 // M[0][2] = x_axis[2];
972 // M[0][3] = -view_p* x_axis ;
973 //
974 // M[1][0] = y_axis[0];
975 // M[1][1] = y_axis[1];
976 // M[1][2] = y_axis[2];
977 // M[1][3] = -view_p* y_axis;
978 //
979 // M[2][0] = z_axis[0];
980 // M[2][1] = z_axis[1];
981 // M[2][2] = z_axis[2];
982 // M[2][3] = -view_p* z_axis;
983 //
984 // M[3][0] = 0.0;
985 // M[3][1] = 0.0;
986 // M[3][2] = 0.0;
987 // M[3][3] = 1.0;
988 //
989 // // prendi la matrice di proiezione
990 // Matrix44d P;
991 // P.Zero();
992 //
993 // if(!IsOrtho())// prospettica
994 // {
995 //
996 // P[0][0] = sx/(s[0]*viewport[0]*Tx);
997 // P[0][2] = (1/f)*(offx+0.5*sx)/Tx;
998 //
999 // P[1][1] = sy/(s[1]* viewport[1]*Ty);
1000 // P[1][2] = (1/f)*(offy+0.5*sy)/Ty;
1001 //
1002 // P[2][2] = 1;
1003 // P[3][2] = 1/f;
1004 // }
1005 // else // ortogonale
1006 // {
1007 // P[0][0] = sx/(s[0]*viewport[0]*viewportM*Tx);
1008 // P[0][3] = (offx+0.5*sx)/Tx; // l'effetto e' una traslazione di +1/2
1009 //
1010 // P[1][1] = sy/(s[1]* viewport[1]*viewportM*Ty);
1011 // P[1][3] = (offy+0.5*sy)/Ty; // l'effetto e' una traslazione di +1/2
1012 //
1013 // P[2][2] = 1;
1014 // P[3][3] = 1;
1015 // }
1016 // // componi
1017 // Matrix44d PM = P*M;
1018 //
1019 // glTexGend(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
1020 // glTexGend(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
1021 // glTexGend(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
1022 //
1023 // glTexGendv(GL_S,GL_OBJECT_PLANE,&PM[0][0]);
1024 // glTexGendv(GL_T,GL_OBJECT_PLANE,&PM[1][0]);
1025 // glTexGendv(GL_Q,GL_OBJECT_PLANE,&PM[3][0]);
1026 //
1027 // glEnable(GL_TEXTURE_GEN_S);
1028 // glEnable(GL_TEXTURE_GEN_T);
1029 // glDisable(GL_TEXTURE_GEN_R);
1030 // glEnable(GL_TEXTURE_GEN_Q);
1031 //}
1032 //
1033 //// versione per le texture rettangolare NV_TEXTURE_RECTANGLE
1034 //// la differenza da glTexGen e' che il mapping e' in [0..sx]X[0..sy]
1035 //void glTexGen_NV(int sx, // Texture Size
1036 // int sy)
1037 //{
1038 // // prendi la rototraslazione che
1039 // // trasforma la coordinata nel
1040 // // sistema di coordinate della camera
1041 // Matrix44d M;
1042 // M[0][0] = x_axis[0];
1043 // M[0][1] = x_axis[1];
1044 // M[0][2] = x_axis[2];
1045 // M[0][3] = -view_p* x_axis ;
1046 //
1047 // M[1][0] = y_axis[0];
1048 // M[1][1] = y_axis[1];
1049 // M[1][2] = y_axis[2];
1050 // M[1][3] = -view_p* y_axis;
1051 //
1052 // M[2][0] = z_axis[0];
1053 // M[2][1] = z_axis[1];
1054 // M[2][2] = z_axis[2];
1055 // M[2][3] = -view_p* z_axis;
1056 //
1057 // M[3][0] = 0.0;
1058 // M[3][1] = 0.0;
1059 // M[3][2] = 0.0;
1060 // M[3][3] = 1.0;
1061 //
1062 // // prendi la matrice di proiezione
1063 // Matrix44d P;
1064 // P.Zero();
1065 //
1066 // if(!IsOrtho())// prospettica
1067 // {
1068 //
1069 // P[0][0] = sx/(s[0]*viewport[0]);
1070 // P[0][2] = sx*(1/f)*( 0.5);
1071 //
1072 // P[1][1] = sy/(s[1]* viewport[1] );
1073 // P[1][2] = sy*(1/f)*( 0.5);
1074 //
1075 // P[2][2] = 1;
1076 // P[3][2] = 1/f;
1077 // }
1078 // else // ortogonale
1079 // {
1080 // P[0][0] = sx/(s[0]*viewport[0]*viewportM);
1081 // P[0][3] = sx* 0.5 ; // l'effetto e' una traslazione di +1/2
1082 //
1083 // P[1][1] = sy/(s[1]* viewport[1]*viewportM);
1084 // P[1][3] = sy* 0.5 ; // l'effetto e' una traslazione di +1/2
1085 //
1086 // P[2][2] = 1;
1087 // P[3][3] = 1;
1088 // }
1089 // // componi
1090 // Matrix44d PM = P*M;
1091 //
1092 // glTexGend(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
1093 // glTexGend(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
1094 // glTexGend(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
1095 //
1096 // glTexGendv(GL_S,GL_OBJECT_PLANE,&PM[0][0]);
1097 // glTexGendv(GL_T,GL_OBJECT_PLANE,&PM[1][0]);
1098 // glTexGendv(GL_Q,GL_OBJECT_PLANE,&PM[3][0]);
1099 //
1100 // glEnable(GL_TEXTURE_GEN_S);
1101 // glEnable(GL_TEXTURE_GEN_T);
1102 // glDisable(GL_TEXTURE_GEN_R);
1103 // glEnable(GL_TEXTURE_GEN_Q);
1104 //// glDisable(GL_TEXTURE_GEN_Q);
1105 //
1106 //}
1107 //#endif // __GL_H__
1108 //
1109 //};
1110 //} // End namespace vcg