📄 calib3dwindow.cpp
字号:
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/// Calib3DWindow.cpp: implementation of the CCalib3DWindow class.
//
//////////////////////////////////////////////////////////////////////
//#include "CalibFilterprop.h"
//#include "calibfilterprop.h"
#include "CV.h"
#include "Calib3DWindow.h"
#include <stdio.h >
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern HINSTANCE g_hInst;
LRESULT CALLBACK MyWindowProc( HWND m_hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
long data = ::GetWindowLong(m_hwnd,GWL_USERDATA);
if( data == 0)
{
return DefWindowProc( m_hwnd, uMsg, wParam, lParam);
}
else
{
return ((CCalib3DWindow*)data)->WindowProc( m_hwnd, uMsg, wParam, lParam);
}
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
static const char* _3DWindowClass = "3DWindow";
CCalib3DWindow::CCalib3DWindow()
{
/* Create window and show it */
m_haveParams = false;
m_alpha = 0;
m_beta = 0;
m_baseAlpha = 0;
m_baseBeta = 0;
m_holdLMouse = 0;
m_holdRMouse = 0;
m_baseScale = 0.02f;
m_scale = 0.0;
/* Register window class */
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_DBLCLKS;
wndclass.lpfnWndProc = MyWindowProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = g_hInst;
wndclass.hIcon = 0;
wndclass.hCursor = 0;
wndclass.hbrBackground = 0;
wndclass.lpszMenuName = 0;
wndclass.lpszClassName = _3DWindowClass;
wndclass.hIconSm = 0;
m_hwnd = 0;
m_hdc = 0;
m_hglrc = 0;
RegisterClassEx(&wndclass);
DWORD dwStyle = WS_BORDER |
WS_CAPTION |
WS_MAXIMIZEBOX |
WS_MINIMIZEBOX |
WS_VISIBLE |
WS_THICKFRAME |
WS_CLIPCHILDREN |
WS_CLIPSIBLINGS;
m_hwnd = CreateWindowEx( 0, // extended window style
_3DWindowClass, // pointer to registered class name
"3D Window", // pointer to window name
dwStyle, // window style
100, // horizontal position of window
100, // vertical position of window
200, // window width
200, // window height
0, // handle to parent or owner window
0, // handle to menu, or child-window identifier
g_hInst, // handle to application instance
0 ); // pointer to window-creation data
if( m_hwnd == 0 )
{
int error = GetLastError();
char st[100];
sprintf(st,"Can't create window.\nError = %d",error);
MessageBox(0,st,"Error",MB_OK);
}
else
{
/* Set OpenGL Formats */
int iPixelType = PFD_TYPE_RGBA;
DWORD dwFlag = PFD_DOUBLEBUFFER |
PFD_SUPPORT_OPENGL |
PFD_DRAW_TO_WINDOW;
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd,0,sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = dwFlag;
pfd.iPixelType = (uchar)iPixelType;
pfd.cColorBits = 24;
pfd.cAlphaBits = 32;
pfd.cAccumBits = 32;
pfd.cDepthBits = 32;
pfd.cStencilBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
m_hdc = GetDC( m_hwnd );
int nPixelFormat = ChoosePixelFormat( m_hdc, &pfd );
if (nPixelFormat == 0) {
MessageBox(0,"Error Choose pixel formar","Info",MB_OK);
}
BOOL dResult = SetPixelFormat( m_hdc, nPixelFormat, &pfd );
if ( !dResult) {
MessageBox(0,"Error Set pixel formar","Info",MB_OK);
}
m_hglrc = wglCreateContext( m_hdc );
if( m_hglrc == 0 ) {
MessageBox(0,"Error Create wgl Context","Info",MB_OK);
}
BOOL err = wglMakeCurrent(m_hdc,m_hglrc);
if (err==FALSE) {
MessageBox(0,"Error Make wgl Context","Info",MB_OK);
}
/*---------------------*/
::SetWindowLong(m_hwnd,GWL_USERDATA,(long)this);
}
}
CCalib3DWindow::~CCalib3DWindow()
{
int result, error;
result = wglMakeCurrent( 0, 0 );
error = GetLastError();
result = wglDeleteContext( m_hglrc );
error = GetLastError();
result = ReleaseDC( m_hwnd, m_hdc );
error = GetLastError();
result = ShowWindow( m_hwnd, SW_HIDE );
error = GetLastError();
result = DestroyWindow( m_hwnd );
error = GetLastError();
if( !result )
{
char st[100];
sprintf(st,"Can't register class.\nError = %d",error);
MessageBox(0,st,"Error",MB_OK);
}
m_hglrc = 0;
m_hdc = 0;
m_hwnd = 0;
}
LRESULT CALLBACK CCalib3DWindow::WindowProc( HWND /*hwnd*/, UINT uMsg,
WPARAM wParam, LPARAM lParam )
{
int fwKeys = wParam; /* key flags */
CvPoint pt = cvPoint( LOWORD(lParam), HIWORD(lParam)); /* cursor position */
//int width = LOWORD(lParam);
//int height = HIWORD(lParam);
switch( uMsg )
{
case WM_PAINT:
{
OnPaint();
return 0;
}
case WM_SIZE:
{
Resize();
///OnPaint();
return 0;
}
case WM_LBUTTONDOWN:
{
OnLButtonDown( pt, fwKeys );
return 0;
}
case WM_LBUTTONUP:
{
OnLButtonUp( pt, fwKeys );
return 0;
}
case WM_RBUTTONDOWN:
{
OnRButtonDown( pt, fwKeys );
return 0;
}
case WM_RBUTTONUP:
{
OnRButtonUp( pt, fwKeys );
return 0;
}
case WM_MOUSEMOVE:
{
OnMouseMove( pt, fwKeys );
return 0;
}
case WM_CLOSE:
{
CloseWindow( m_hwnd );
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc( m_hwnd, uMsg, wParam, lParam);
}
void CCalib3DWindow::SetParams( CvCameraParams* camera,
CvSize etalonSize, float squareSize )
{
if( camera )
{
m_camera = *camera;
m_etalonSize = etalonSize;
m_squareSize = squareSize;
}
m_haveParams = camera != 0;
InvalidateRect( m_hwnd, 0, FALSE );
UpdateWindow( m_hwnd );
}
void CCalib3DWindow::OnPaint()
{
int noerror;
PAINTSTRUCT ps;
BeginPaint( m_hwnd, &ps );
/* 115 180 251*/
glClearColor(0.45f,0.705f,0.984f,1.0f);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* ========= Begin draw the etalon position ========= */
if( m_hwnd && m_haveParams && IsWindowVisible( m_hwnd ))
{
int numX = m_etalonSize.width - 1;
int numY = m_etalonSize.height - 1;
float maxX,maxY,maxZ;
float minX,minY,minZ;
float floorHeight = 6;
minX = -20;
minY = -20;
minZ = 0;
maxX = 20;
maxY = 20;
maxZ = 80;
float x1,y1,z1;
float x2,y2,z2;
float x3,y3,z3;
float x4,y4,z4;
float x5,y5,z5;
float x6,y6,z6;
float x7,y7,z7;
float x8,y8,z8;
float yfloor;
float Box_W;
float Box_H;
float Box_L;
CvMat Points = cvMat( numX * numY, 3, CV_MAT32F, 0 );
CvMat objPoint = cvMat( 3, 1, CV_MAT32F, 0 );
CvMat objPoints = cvMat( numX * numY, 3, CV_MAT32F, 0 );
CvMat tmp3 = cvMat( 3, 1, CV_MAT32F, 0 );
CvMat Point = cvMat( 3, 1, CV_MAT32F, 0 );
CvMat cameraMatr = cvMat( 3, 3, CV_MAT32F, m_camera.rotMatr );
CvMat transVect = cvMat( 3, 1, CV_MAT32F, m_camera.transVect );
int t,k,index;
int etalonNum;
cvmAlloc( &objPoints );
cvmAlloc( &Points );
cvmAlloc( &objPoint );
cvmAlloc( &tmp3 );
cvmAlloc( &Point );
etalonNum = numX * numY;
/* Fill etalon coordinates */
for( t = 0; t < numY; t++ )
{
for ( k = 0; k < numX; k++)
{
index = t * numX + k;
objPoints.data.fl[index * 3 + 0] = k * m_squareSize;
objPoints.data.fl[index * 3 + 1] = t * m_squareSize;
objPoints.data.fl[index * 3 + 2] = 0;
}
}
Box_W = 40;
Box_H = 40;
Box_L = 80;
x1 = Box_W / 2;
y1 = -Box_H / 2;
z1 = 0;
x2 = -Box_W / 2;
y2 = -Box_H / 2;
z2 = 0;
x3 = Box_W / 2;
y3 = Box_H / 2;
z3 = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -