13 #ifdef HAVE_APPLE_OPENGL_FRAMEWORK
14 #include <OpenGL/gl.h>
22 #define WIN32_LEAN_AND_MEAN
34 set(640,480,1000,1000,320,240,0);
65 if ((nearPlane > 0) && (nearPlane < farPlane)) {
66 this->nearPlane = nearPlane;
67 this->farPlane = farPlane;
80 uv[0] = (f * eye[0] + s * eye[1]) / eye[2] + cx;
81 uv[1] = (g * eye[1]) / eye[2] + cy;
90 if (w==0) w = nearPlane;
92 eye[1] = (v - cy)*(f/g);
93 eye[0] = (u - cx - s*eye[1]);
105 imageToEye(uv[0], uv[1], eye, w);
121 glMatrixMode(GL_PROJECTION);
125 for (
int i=0; i<16; ++i) m[i]=0;
126 double w = (double) width;
127 double h = (double) height;
129 double _near = this->nearPlane;
130 double _far = this->farPlane;
134 m[0 + 2*4] = (-2*cx/w +1);
136 m[1 + 2*4] = (-2*cy/h +1);
137 m[2 + 2*4] = -(_far+_near)/(_far-_near);
138 m[2 + 3*4] = -2*_far*_near/(_far-_near);
143 cerr <<
"camera.cpp was not compiled with OpenGL support.\n";
149 eyeToImageMat.m[0][0] = f;
150 eyeToImageMat.m[1][0] = 0;
151 eyeToImageMat.m[2][0] = 0;
152 eyeToImageMat.m[0][1] = s;
153 eyeToImageMat.m[1][1] = g;
154 eyeToImageMat.m[2][1] = 0;
155 eyeToImageMat.m[0][2] = cx;
156 eyeToImageMat.m[1][2] = cy;
157 eyeToImageMat.m[2][2] = 1;
171 if (distortion == 0)
return false;
173 for (
int y=0; y<xmap->height; y++) {
174 for (
int x=0; x<ymap->height; y++) {
175 float image[2], distor[2];
177 image[0] = (x - (cx))/f;
178 image[1] = (y - (cy))/g;
180 distor[0] = image[0] - distortion * image[0] * (image[0]*image[0]+image[1]*image[1]);
181 distor[1] = image[1] - distortion * image[1] * (image[0]*image[0]+image[1]*image[1]);
184 cvSetReal2D(xmap, y, x, distor[0]*f + cx);
185 cvSetReal2D(ymap, y, x, distor[1]*g + cy);
193 clearExternalParams();
199 uvw.
setMul(worldToImageMat, p);
200 uvw.
v[0] /= uvw.
v[2];
201 uvw.
v[1] /= uvw.
v[2];
206 worldToImage(*((
Vec3 *)p), *((
Vec3 *)uvw));
211 dst.
setMul(worldToEyeMat, src);
216 ((
Vec3 *)dst)->setMul(worldToEyeMat, *((
Vec3 *)src));
224 for (
int i=0; i<3; ++i)
225 for (
int j=0;j<4; ++j)
226 m[i+j*4] = worldToEyeMat.m[i][j];
227 m[3+0*4] = m[3+1*4] = m[3+2*4] = 0;
229 glMatrixMode(GL_MODELVIEW);
234 cerr <<
"camera.cpp was not compiled with OpenGL support.\n";
243 Vec3 up(sin(roll), cos(roll), 0);
251 for (
int i=0; i<3; i++) {
252 worldToEyeMat.m[0][i] = x[i];
253 worldToEyeMat.m[1][i] = y[i];
254 worldToEyeMat.m[2][i] = z[i];
257 for (
int i=0; i<3; ++i) {
258 worldToEyeMat.m[i][3] = -worldToEyeMat.m[i][0]*pos[0]
259 - worldToEyeMat.m[i][1]*pos[1]
260 - worldToEyeMat.m[i][2]*pos[2];
262 cmpWorldToImageMat();
267 double t7 = m[0][0]*m[0][0];
268 double t8 = m[0][1]*m[0][1];
269 double t9 = m[0][2]*m[0][2];
270 double t14 = m[0][0]*m[1][0]+m[0][1]*m[1][1]+m[0][2]*m[1][2];
271 double t18 = m[0][0]*m[2][0]+m[0][1]*m[2][1]+m[0][2]*m[2][2];
272 double t19 = m[1][0]*m[1][0];
273 double t20 = m[1][1]*m[1][1];
274 double t21 = m[1][2]*m[1][2];
275 double t26 = m[1][0]*m[2][0]+m[1][1]*m[2][1]+m[1][2]*m[2][2];
276 double t27 = m[2][0]*m[2][0];
277 double t28 = m[2][1]*m[2][1];
278 double t29 = m[2][2]*m[2][2];
279 mmt[0][0] = t7+t8+t9;
283 mmt[1][1] = t19+t20+t21;
287 mmt[2][2] = t27+t28+t29;
303 double l = sqrt(_tdir[2][0]*_tdir[2][0]
304 + _tdir[2][1]*_tdir[2][1]
305 + _tdir[2][2]*_tdir[2][2]);
306 for (
int i=0; i<3; ++i)
307 for (
int j=0;j<4; ++j)
308 tdir[i][j] = _tdir[i][j]/l;
312 worldToEyeMat.m[2][0] = tdir[2][0];
313 worldToEyeMat.m[2][1] = tdir[2][1];
314 worldToEyeMat.m[2][2] = tdir[2][2];
315 worldToEyeMat.m[2][3] = tdir[2][3];
321 g = sqrt(ppt[1][1]-cy*cy);
323 s = (ppt[1][0]-cx*cy)/g;
326 f = sqrt(ppt[0][0]-cx*cx-s*s);
328 worldToEyeMat.m[1][0] = (tdir[1][0] - cy*worldToEyeMat.m[2][0])/g;
329 worldToEyeMat.m[0][0] = (tdir[0][0] - s*worldToEyeMat.m[1][0] - cx*worldToEyeMat.m[2][0])/f;
331 worldToEyeMat.m[1][1] = (tdir[1][1] - cy*worldToEyeMat.m[2][1])/g;
332 worldToEyeMat.m[0][1] = (tdir[0][1] - s*worldToEyeMat.m[1][1] - cx*worldToEyeMat.m[2][1])/f;
334 worldToEyeMat.m[1][2] = (tdir[1][2] - cy*worldToEyeMat.m[2][2])/g;
335 worldToEyeMat.m[0][2] = (tdir[0][2] - s*worldToEyeMat.m[1][2] - cx*worldToEyeMat.m[2][2])/f;
337 worldToEyeMat.m[1][3] = (tdir[1][3] - cy*worldToEyeMat.m[2][3])/g;
338 worldToEyeMat.m[0][3] = (tdir[0][3] - s*worldToEyeMat.m[1][3] - cx*worldToEyeMat.m[2][3])/f;
340 if (worldToEyeMat.det() < 0) {
342 for (
int i=0;i<4;++i) worldToEyeMat.m[1][i] = -worldToEyeMat.m[1][i];
346 eyeToWorldMat.setInverseByTranspose(worldToEyeMat);
347 memcpy(&worldToImageMat, tdir,
sizeof(worldToImageMat));
361 if (!file.good())
return false;
364 for (
int j=0; j<3; ++j)
365 for (
int i=0; i<4; ++i)
368 while (file.getline(line,256)) {
369 if (memcmp(line,
"distortion:", 11)==0) {
371 if (sscanf(line+11,
"%lf", &d)==1) {
373 printf(
"Camera distortion: %f\n", d);
375 }
else if (memcmp(line,
"width:", 6)==0) {
377 if (sscanf(line+6,
"%d", &nw)==1) {
380 }
else if (memcmp(line,
"height:", 7)==0) {
382 if (sscanf(line+7,
"%d", &nh)==1) {
385 }
else if (memcmp(line,
"near:", 5)==0) {
387 if (sscanf(line+5,
"%lf", &d)==1) {
389 printf(
"Near clipping plane: %f\n", d);
391 }
else if (memcmp(line,
"far:", 4)==0) {
393 if (sscanf(line+4,
"%lf", &d)==1) {
395 printf(
"Near clipping plane: %f\n", d);
403 if (strlen(tdirFile) > 5) {
405 strncpy(fn, tdirFile, 511);
406 strcpy(fn + strlen(fn) - strlen(
".tdir"),
".plane");
410 double d,n=nearPlane,f=farPlane;
411 for (
int i=0; i<4; ++i)
413 if (file.good()) file >> n;
414 if (file.good()) file >> f;
418 std::cout <<
"Reading clipping planes from " << fn
420 <<
" far=" << f << endl;
422 std::cerr <<
"Troubles loading " << fn <<
"!\n";
431 static double diag(
double x,
double y)
433 return sqrt(x*x+y*y);
438 double mdiag =
diag(
double(m.
w),
double(m.
h));
439 return fabs(
double(mdiag -
diag(2*dx,2*dy)));
452 if (!loadTdir(tdirFile,0,0))
return false;
454 if ((width !=0) && (height !=0))
return true;
469 double bestDelta =
diagDiff(mode[0], cx, cy);
470 for (
int i=1; mode[i].
w != -1; ++i) {
471 double delta =
diagDiff(mode[i], cx, cy);
472 if (delta < bestDelta) {
477 width = mode[best].
w;
478 height = mode[best].
h;
486 worldToEyeMat.setIdentity();
487 cmpWorldToImageMat();
492 worldToImageMat.setMul(eyeToImageMat, worldToEyeMat);
493 eyeToWorldMat.setInverseByTranspose(worldToEyeMat);
498 w.
setMul(eyeToWorldMat, uvw);
504 imageToEye(u,v, uvw.
v, depth);
510 worldToEyeMat.m[0][3] -= dx;
511 worldToEyeMat.m[1][3] -= dy;
512 worldToEyeMat.m[2][3] -= dz;
513 cmpWorldToImageMat();
519 cmpWorldToImageMat();
526 if (!f.good())
return false;
528 for (
int l=0; l<3; l++) {
529 for (
int c=0; c<4; c++) {
530 f << worldToImageMat.m[l][c] <<
" ";
534 f <<
"width: " << width << endl;
535 f <<
"height: " << height << endl;
536 f <<
"distortion: " << distortion << endl;
537 f <<
"near: " << nearPlane << endl;
538 f <<
"far: " << farPlane << endl;
551 <<
", width="<<cam.
width
552 <<
", height="<<cam.
height;