1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
40 //M*/// Calib3DWindow.cpp: implementation of the CCalib3DWindow class.
42 //////////////////////////////////////////////////////////////////////
44 //#include "CalibFilterprop.h"
45 //#include "calibfilterprop.h"
47 #include "Calib3DWindow.h"
54 extern HINSTANCE g_hInst;
56 static LRESULT CALLBACK MyWindowProc( HWND m_hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
58 long data = ::GetWindowLong(m_hwnd,GWL_USERDATA);
61 return DefWindowProc( m_hwnd, uMsg, wParam, lParam);
65 return ((CCalib3DWindow*)data)->WindowProc( m_hwnd, uMsg, wParam, lParam);
69 //////////////////////////////////////////////////////////////////////
70 // Construction/Destruction
71 //////////////////////////////////////////////////////////////////////
72 static const char* _3DWindowClass = "3DWindow";
74 CCalib3DWindow::CCalib3DWindow()
76 /* Create window and show it */
89 /* Register window class */
92 wndclass.cbSize = sizeof(WNDCLASSEX);
93 wndclass.style = CS_DBLCLKS;
94 wndclass.lpfnWndProc = MyWindowProc;
95 wndclass.cbClsExtra = 0;
96 wndclass.cbWndExtra = 0;
97 wndclass.hInstance = g_hInst;
100 wndclass.hbrBackground = 0;
101 wndclass.lpszMenuName = 0;
102 wndclass.lpszClassName = _3DWindowClass;
103 wndclass.hIconSm = 0;
109 RegisterClassEx(&wndclass);
111 DWORD dwStyle = WS_BORDER |
120 m_hwnd = CreateWindowEx( 0, // extended window style
121 _3DWindowClass, // pointer to registered class name
122 "3D Window", // pointer to window name
123 dwStyle, // window style
124 100, // horizontal position of window
125 100, // vertical position of window
127 200, // window height
128 0, // handle to parent or owner window
129 0, // handle to menu, or child-window identifier
130 g_hInst, // handle to application instance
131 0 ); // pointer to window-creation data
135 int error = GetLastError();
137 sprintf(st,"Can't create window.\nError = %d",error);
138 MessageBox(0,st,"Error",MB_OK);
142 /* Set OpenGL Formats */
143 int iPixelType = PFD_TYPE_RGBA;
145 DWORD dwFlag = PFD_DOUBLEBUFFER |
149 PIXELFORMATDESCRIPTOR pfd;
150 memset(&pfd,0,sizeof(PIXELFORMATDESCRIPTOR));
152 pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
154 pfd.dwFlags = dwFlag;
155 pfd.iPixelType = (uchar)iPixelType;
161 pfd.cStencilBits = 32;
162 pfd.iLayerType = PFD_MAIN_PLANE;
164 m_hdc = GetDC( m_hwnd );
166 int nPixelFormat = ChoosePixelFormat( m_hdc, &pfd );
168 if (nPixelFormat == 0) {
169 MessageBox(0,"Error Choose pixel formar","Info",MB_OK);
172 BOOL dResult = SetPixelFormat( m_hdc, nPixelFormat, &pfd );
175 MessageBox(0,"Error Set pixel formar","Info",MB_OK);
178 m_hglrc = wglCreateContext( m_hdc );
181 MessageBox(0,"Error Create wgl Context","Info",MB_OK);
184 BOOL err = wglMakeCurrent(m_hdc,m_hglrc);
187 MessageBox(0,"Error Make wgl Context","Info",MB_OK);
189 /*---------------------*/
191 ::SetWindowLong(m_hwnd,GWL_USERDATA,(long)this);
195 CCalib3DWindow::~CCalib3DWindow()
198 result = wglMakeCurrent( 0, 0 );
199 error = GetLastError();
200 result = wglDeleteContext( m_hglrc );
201 error = GetLastError();
202 result = ReleaseDC( m_hwnd, m_hdc );
203 error = GetLastError();
204 result = ShowWindow( m_hwnd, SW_HIDE );
205 error = GetLastError();
206 result = DestroyWindow( m_hwnd );
207 error = GetLastError();
211 sprintf(st,"Can't register class.\nError = %d",error);
212 MessageBox(0,st,"Error",MB_OK);
219 LRESULT CALLBACK CCalib3DWindow::WindowProc( HWND /*hwnd*/, UINT uMsg,
220 WPARAM wParam, LPARAM lParam )
222 int fwKeys = wParam; /* key flags */
223 CvPoint pt = cvPoint( LOWORD(lParam), HIWORD(lParam)); /* cursor position */
224 //int width = LOWORD(lParam);
225 //int height = HIWORD(lParam);
244 OnLButtonDown( pt, fwKeys );
249 OnLButtonUp( pt, fwKeys );
254 OnRButtonDown( pt, fwKeys );
259 OnRButtonUp( pt, fwKeys );
264 OnMouseMove( pt, fwKeys );
269 CloseWindow( m_hwnd );
275 return DefWindowProc( m_hwnd, uMsg, wParam, lParam);
279 void CCalib3DWindow::SetParams( CvCameraParams* camera,
280 CvSize etalonSize, float squareSize )
285 m_etalonSize = etalonSize;
286 m_squareSize = squareSize;
288 m_haveParams = camera != 0;
290 InvalidateRect( m_hwnd, 0, FALSE );
291 UpdateWindow( m_hwnd );
294 void CCalib3DWindow::OnPaint()
298 BeginPaint( m_hwnd, &ps );
301 glClearColor(0.45f,0.705f,0.984f,1.0f);
302 glEnable(GL_DEPTH_TEST);
303 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
305 /* ========= Begin draw the etalon position ========= */
306 if( m_hwnd && m_haveParams && IsWindowVisible( m_hwnd ))
308 int numX = m_etalonSize.width - 1;
309 int numY = m_etalonSize.height - 1;
311 //float minX,minY,minZ,maxZ;
312 float floorHeight = 6;
336 CvMat Points = cvMat( numX * numY, 3, CV_MAT32F, 0 );
337 CvMat objPoint = cvMat( 3, 1, CV_MAT32F, 0 );
338 CvMat objPoints = cvMat( numX * numY, 3, CV_MAT32F, 0 );
339 CvMat tmp3 = cvMat( 3, 1, CV_MAT32F, 0 );
340 CvMat Point = cvMat( 3, 1, CV_MAT32F, 0 );
341 CvMat cameraMatr = cvMat( 3, 3, CV_MAT32F, m_camera.rotMatr );
342 CvMat transVect = cvMat( 3, 1, CV_MAT32F, m_camera.transVect );
347 cvmAlloc( &objPoints );
349 cvmAlloc( &objPoint );
353 etalonNum = numX * numY;
354 /* Fill etalon coordinates */
355 for( t = 0; t < numY; t++ )
357 for ( k = 0; k < numX; k++)
359 index = t * numX + k;
360 objPoints.data.fl[index * 3 + 0] = k * m_squareSize;
361 objPoints.data.fl[index * 3 + 1] = t * m_squareSize;
362 objPoints.data.fl[index * 3 + 2] = 0;
402 floorHeight = Box_H / 6;
404 yfloor = - Box_H /2 - floorHeight;
406 glMatrixMode(GL_PROJECTION);
415 glRotatef(m_baseAlpha + m_alpha,0,1,0);
416 glRotatef(m_baseBeta + m_beta,1,0,0);
419 setScale = m_baseScale + m_scale;
425 glTranslatef(0, 0,- Box_L / 2.0f);
429 glBegin(GL_LINE_STRIP);
431 glVertex3f(x1,y1,z1);
432 glVertex3f(x3,y3,z3);
433 glVertex3f(x4,y4,z4);
434 glVertex3f(x2,y2,z2);
435 glVertex3f(x1,y1,z1);
436 glVertex3f(x5,y5,z5);
437 glVertex3f(x6,y6,z6);
438 glVertex3f(x8,y8,z8);
439 glVertex3f(x7,y7,z7);
440 glVertex3f(x5,y5,z5);
446 glVertex3f(x3,y3,z3);
447 glVertex3f(x7,y7,z7);
449 glVertex3f(x4,y4,z4);
450 glVertex3f(x8,y8,z8);
452 glVertex3f(x2,y2,z2);
453 glVertex3f(x6,y6,z6);
457 /* Draw floor for the room */
459 /* First draw the lines */
461 glColor3f(0.0,1.0,0.0);
462 glVertex3f(x1,y1,z1);
463 glVertex3f(x5,y5,z5);
465 glVertex3f(x5,y5,z5);
466 glVertex3f(x6,y6,z6);
468 glVertex3f(x6,y6,z6);
469 glVertex3f(x2,y2,z2);
471 glVertex3f(x2,y2,z2);
472 glVertex3f(x1,y1,z1);
474 glVertex3f(x1,y1,z1);
475 glVertex3f(x1,yfloor,z1);
477 glVertex3f(x1,yfloor,z1);
478 glVertex3f(x5,yfloor,z5);
480 glVertex3f(x5,yfloor,z5);
481 glVertex3f(x6,yfloor,z6);
483 glVertex3f(x6,yfloor,z6);
484 glVertex3f(x2,yfloor,z2);
486 glVertex3f(x2,yfloor,z2);
487 glVertex3f(x1,yfloor,z1);
488 glVertex3f(x2,y2,z2);
489 glVertex3f(x2,yfloor,z2);
490 glVertex3f(x6,y6,z6);
491 glVertex3f(x6,yfloor,z6);
492 glVertex3f(x5,y5,z5);
493 glVertex3f(x5,yfloor,z5);
496 /* Draw floor under floor */
498 glColor3f(0.0f, 0.6f, 1.1f);
500 glVertex3f(x1,yfloor,z1);
501 glVertex3f(x5,yfloor,z5);
502 glVertex3f(x6,yfloor,z6);
503 glVertex3f(x2,yfloor,z2);
506 /* Draw walls under floor */
508 glColor3f(0.0f,0.3f,0.5f);
510 glVertex3f(x1,y1,z1);
511 glVertex3f(x5,y5,z5);
512 glVertex3f(x5,yfloor,z5);
513 glVertex3f(x1,yfloor,z1);
515 glVertex3f(x5,y5,z5);
516 glVertex3f(x6,y6,z6);
517 glVertex3f(x6,yfloor,z6);
518 glVertex3f(x5,yfloor,z5);
520 glVertex3f(x2,y2,z2);
521 glVertex3f(x6,y6,z6);
522 glVertex3f(x6,yfloor,z6);
523 glVertex3f(x2,yfloor,z2);
525 glVertex3f(x1,y1,z1);
526 glVertex3f(x2,y2,z2);
527 glVertex3f(x2,yfloor,z2);
528 glVertex3f(x1,yfloor,z1);
533 /* Draw the camera */
534 float camX0,camX1,camX2,camX3,camX4;
535 float camY0,camY1,camY2,camY3,camY4;
536 float camZ0,camZ1,camZ2,camZ3,camZ4;
538 camX0 = 0.0; camY0 = 0.0; camZ0 = 0.0;
539 camX1 = -1.0; camY1 = 1.0; camZ1 = 4.0;
540 camX2 = 1.0; camY2 = 1.0; camZ2 = 4.0;
541 camX3 = 1.0; camY3 = -1.0; camZ3 = 4.0;
542 camX4 = -1.0; camY4 = -1.0; camZ4 = 4.0;
544 glBegin(GL_TRIANGLE_FAN);
545 glVertex3f(camX0,camY0,camZ0);
546 glVertex3f(camX1,camY1,camZ1);
547 glVertex3f(camX2,camY2,camZ2);
548 glVertex3f(camX3,camY3,camZ3);
549 glVertex3f(camX4,camY4,camZ4);
552 glBegin(GL_LINE_STRIP);
553 glColor3f(1.0,0.0,0.0);
554 glVertex3f(camX0,camY0,camZ0);
555 glVertex3f(camX1,camY1,camZ1);
556 glVertex3f(camX2,camY2,camZ2);
557 glVertex3f(camX3,camY3,camZ3);
558 glVertex3f(camX4,camY4,camZ4);
559 glVertex3f(camX1,camY1,camZ1);
563 glVertex3f(camX0,camY0,camZ0);
564 glVertex3f(camX2,camY2,camZ2);
565 glVertex3f(camX0,camY0,camZ0);
566 glVertex3f(camX3,camY3,camZ3);
567 glVertex3f(camX0,camY0,camZ0);
568 glVertex3f(camX4,camY4,camZ4);
571 /* draw direct to ground */
575 glVertex3f(0,-Box_H/2,0);
578 /* Draw flat ground */
581 glColor3f(1.0f, 0.6f, 0.1f);
583 //glShadeModel(GL_FLAT);
585 glVertex3f(x1,y1,z1);
586 glVertex3f(x5,y5,z5);
587 glVertex3f(x6,y6,z6);
588 glVertex3f(x2,y2,z2);
593 for( t = 0; t < etalonNum; t++) {
595 objPoint.data.fl[0] = objPoints.data.fl[t * 3 + 0];
596 objPoint.data.fl[1] = objPoints.data.fl[t * 3 + 1];
597 objPoint.data.fl[2] = objPoints.data.fl[t * 3 + 2];
599 cvmMul( &cameraMatr, &objPoint, &tmp3 );
601 cvmAdd( &tmp3, &transVect, &Point );
604 Points.data.fl[t*3+0] = -Point.data.fl[0];
605 Points.data.fl[t*3+1] = -Point.data.fl[1];
606 Points.data.fl[t*3+2] = Point.data.fl[2];
613 /* Draw black and white quads */
614 float xq[4];//1,xq2,xq3,xq4;
615 float yq[4];//1,yq2,yq3,yq4;
616 float zq[4];//,zq2,zq3,zq4;
618 int inds[4];//1,ind2,ind3,ind4;
621 for(t = 0; t < numY - 1; t++)
623 for(k = 0; k < numX - 1; k++)
625 inds[0] = t * numX + k;
626 inds[1] = inds[0] + 1;
627 inds[2] = inds[0] + numX + 1;
628 inds[3] = inds[0] + numX;
630 for( curr = 0; curr < 4; curr++ )
632 xq[curr] = Points.data.fl[inds[curr] * 3 + 0];
633 yq[curr] = Points.data.fl[inds[curr] * 3 + 1];
634 zq[curr] = Points.data.fl[inds[curr] * 3 + 2];
637 if( (t + k & 1) == 1 )
639 glColor3f(0.1f, 0.1f, 0.1f);
643 glColor3f(1.0f, 1.0f, 1.0f);
646 for( curr = 0; curr < 4; curr++ )
648 glVertex3f(xq[curr],yq[curr],zq[curr]);
668 EndPaint( m_hwnd, &ps );
673 void CCalib3DWindow::OnLButtonDown( CvPoint pt, int /*fwKeys*/)
682 InvalidateRect( m_hwnd, 0, FALSE );
683 UpdateWindow( m_hwnd );
686 void CCalib3DWindow::OnLButtonUp( CvPoint /*pt*/, int /*fwKeys*/)
689 m_holdLMouse = false;
690 m_baseAlpha += m_alpha;
691 m_baseBeta += m_beta;
694 InvalidateRect( m_hwnd, 0, FALSE );
695 UpdateWindow( m_hwnd );
698 void CCalib3DWindow::OnRButtonDown( CvPoint pt, int /*fwKeys*/)
706 InvalidateRect( m_hwnd, 0, FALSE );
707 UpdateWindow( m_hwnd );
710 void CCalib3DWindow::OnRButtonUp( CvPoint /*pt*/, int /*fwKeys*/)
713 m_holdRMouse = false;
715 m_baseScale += m_scale;
716 if( m_baseScale <= 0.0) m_baseScale = 0.0;
718 InvalidateRect( m_hwnd, 0, FALSE );
719 UpdateWindow( m_hwnd );
722 void CCalib3DWindow::OnMouseMove( CvPoint pt, int /*fwKeys*/)
727 GetClientRect( m_hwnd, &rect );
728 float width = (float)(rect.right - rect.left);
729 float height = (float)(rect.bottom - rect.top);
731 float dx = (float)( pt.x - m_oldLPoint.x );
732 float dy = (float)( pt.y - m_oldLPoint.y );
733 m_alpha = (dx / width) * 360;
734 m_beta = (dy / height) * 360;
740 GetClientRect( m_hwnd, &rect );
741 float height = (float)(rect.bottom - rect.top);
742 float dy = (float)(pt.y - m_oldRPoint.y);
743 m_scale = (float)((dy/height)*0.1);
746 InvalidateRect( m_hwnd, 0, FALSE );
747 UpdateWindow( m_hwnd );
750 void CCalib3DWindow::Show( bool show )
752 ShowWindow( m_hwnd, show ? SW_SHOWNORMAL : SW_HIDE );
757 void CCalib3DWindow::Resize()
760 GetClientRect( m_hwnd, &rect );
762 glViewport(0,0,rect.right - rect.left,rect.bottom - rect.top);