Move the sources to trunk
[opencv] / filters / CalibFilter / CalibFilterProp.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 // This class implements the property page for the CCalibFilter filter
43
44 #pragma warning( disable: 4201 4710 )
45
46 #include <windows.h>
47 #include <cvstreams.h>
48 #include <commctrl.h>
49 #include <olectl.h>
50 #include "resource.h"
51 #include "iCalibFilter.h"
52 #include "CalibFilterprop.h"
53 #include "CalibFilter.h"
54 #include "CalibFilteruids.h"
55 #include <assert.h>
56 #include "math.h"
57 #include <stdio.h>
58 #include "Calib3DWindow.h"
59
60 //
61 // CreateInstance
62 //
63 // This goes in the factory template table to create new filter instances
64 //
65 const signed char MaxLevel = 127;
66 const signed char MinLevel = -128;
67
68 CUnknown * WINAPI CCalibFilterProperties::CreateInstance(LPUNKNOWN lpunk, HRESULT *phr)
69 {
70     CUnknown *punk = new CCalibFilterProperties(lpunk, phr);
71     if (punk == NULL) {
72     *phr = E_OUTOFMEMORY;
73     }
74     return punk;
75
76 } // CreateInstance
77
78
79 //
80 // Constructor
81 //
82 CCalibFilterProperties::CCalibFilterProperties(LPUNKNOWN pUnk, HRESULT *phr) :
83     CBasePropertyPage(NAME("CalibFilter Property Page"),pUnk,
84                       IDD_CCalibFilterPROP,
85                       IDS_TITLE),
86     m_pCalibFilter(NULL)
87 {
88     InitCommonControls();
89 } // (Constructor)
90
91
92 //
93 // SetDirty
94 //
95 // Sets m_bDirty and notifies the property page site of the change
96 //
97 void CCalibFilterProperties::SetDirty()
98 {
99     m_bDirty = TRUE;
100     if (m_pPageSite) m_pPageSite->OnStatusChange(PROPPAGESTATUS_DIRTY);
101 } // SetDirty
102
103
104 void CCalibFilterProperties::SetControls( HWND  hwnd )
105 {
106     char buffer[100];
107     SetDlgItemInt( hwnd, IDC_WIDTH,  (int)m_params.etalon_params[0], 0 );
108     SetDlgItemInt( hwnd, IDC_HEIGHT, (int)m_params.etalon_params[1], 0 );
109     sprintf(buffer,"%.2f", m_params.etalon_params[2]);
110     SetDlgItemText( hwnd, IDC_SQUARE_SIZE, buffer );
111     SetDlgItemInt( hwnd, IDC_INTERVAL, m_params.frame_interval, 0 );
112     SetDlgItemInt( hwnd, IDC_MAX_FRAMES, m_params.frames_to_collect, 0 );
113 ////////////////////
114     Button_SetCheck( GetDlgItem( hwnd, IDC_UNDISTORTION), m_params.enable_undistortion != 0 );
115     Button_SetCheck( GetDlgItem( hwnd, IDC_SHOW3D), m_params.show_3d_window != 0 );
116 }
117
118
119 void CCalibFilterProperties::GetControls( HWND  hwnd )
120 {
121     char buffer[100];
122     BOOL ok = 0;
123     float fval;
124     
125     unsigned val = GetDlgItemInt( hwnd, IDC_WIDTH, &ok, 0 );
126     if( ok ) m_params.etalon_params[0] = (float)val;
127     
128     val = GetDlgItemInt( hwnd, IDC_HEIGHT, &ok, 0 );
129     if( ok ) m_params.etalon_params[1] = (float)val;
130     
131     val = GetDlgItemInt( hwnd, IDC_INTERVAL, &ok, 0 );
132     if( ok ) m_params.frame_interval = val;
133
134     val = GetDlgItemInt( hwnd, IDC_MAX_FRAMES, &ok, 0 );
135     if( ok ) m_params.frames_to_collect = val;
136
137     GetDlgItemText( hwnd, IDC_SQUARE_SIZE, buffer, sizeof(buffer));
138     if( sscanf(buffer,"%f", &fval ) == 1 )
139     {
140         m_params.etalon_params[2] = fval;
141     }
142
143 /////////////////////////////////
144     
145     val = Button_GetCheck( GetDlgItem( hwnd, IDC_UNDISTORTION) );
146     m_params.enable_undistortion = val;
147
148     val = Button_GetCheck( GetDlgItem( hwnd, IDC_SHOW3D) );
149     m_params.show_3d_window = val;
150 }
151
152
153 void CCalibFilterProperties::SetParameters()
154 {
155     if( m_pCalibFilter )
156     {
157         m_pCalibFilter->set_EtalonParams(
158               m_params.etalon_type,
159               m_params.etalon_params, 3 );
160     
161         m_pCalibFilter->set_FrameInterval( m_params.frame_interval );
162
163         m_pCalibFilter->set_FramesToCollect( m_params.frames_to_collect );
164 ///////////
165         m_pCalibFilter->set_EnableUndistortion( m_params.enable_undistortion );
166         m_pCalibFilter->set_Show3DWindow( m_params.show_3d_window );
167 //        m_pCalibFilter->set_Show3DEtalon( m_params.show_3d_etalon );
168
169     }
170 }
171
172
173 void CCalibFilterProperties::GetParameters()
174 {
175     if( m_pCalibFilter )
176     {
177         long count = sizeof(m_params.etalon_params)/sizeof(float);
178
179         m_pCalibFilter->get_EtalonParams(
180              &m_params.etalon_type,
181              m_params.etalon_params,
182              &count );
183         m_params.etalon_params_count = count;
184     
185         m_pCalibFilter->get_FrameInterval( &m_params.frame_interval );
186
187         m_pCalibFilter->get_FramesToCollect( &m_params.frames_to_collect );
188 ////////        
189         m_pCalibFilter->get_EnableUndistortion( &m_params.enable_undistortion );
190         m_pCalibFilter->get_Show3DWindow( &m_params.show_3d_window );
191     }
192 }
193
194
195 void CCalibFilterProperties::StartCalibration()
196 {
197     if( m_pCalibFilter )
198     {
199         if( m_bDirty ) Apply();
200         m_pCalibFilter->StartCalibrate();
201     }
202 }
203 void CCalibFilterProperties::SaveCameraParams()
204 {
205     if( m_pCalibFilter )
206     {
207         m_pCalibFilter->SaveCameraParams();
208     }
209 }
210 void CCalibFilterProperties::LoadCameraParams()
211 {
212     if( m_pCalibFilter )
213     {
214         m_pCalibFilter->LoadCameraParams();
215     }
216 }
217
218
219 void CCalibFilterProperties::PrintStatus()
220 {
221     if( m_pCalibFilter && m_hwnd &&
222         m_pCalibFilter->GetState(
223              &m_params.calib_state,   &m_params.frames_collected,
224              &m_params.frames_passed, &m_params.last_frame_time ) == NOERROR )
225     {
226         char buffer[1000] = "";
227
228         /* My print status */
229         switch( m_params.calib_state )
230         {
231         case  CalibState_Initial:
232         case  CalibState_NotCalibrated:
233             sprintf(buffer,"Camera is not calibrated");
234             break;
235         case  CalibState_Calibrated:
236             {
237                 CvCameraParams camera;
238                 m_pCalibFilter->GetCameraParams( &camera );
239
240                 sprintf(buffer,"Camera is Calibrated\r\n"
241                                "principal point: (%5.1f, %5.1f)\r\n"
242                                "focal length: (%7.3f x %7.3f)\r\n"
243                                "distortion: k1 = %6.3f, k2 = %6.3f\r\n"
244                                "\tp1 = %6.3f, p2 = %6.3f",
245                                camera.principalPoint[0], camera.principalPoint[1],
246                                camera.focalLength[0], camera.focalLength[1],
247                                camera.distortion[0], camera.distortion[1],
248                                camera.distortion[2], camera.distortion[3] );
249             }
250             break;
251         case  CalibState_CalibrationProcess:
252             sprintf(buffer,"Processing... Collected %d frames",m_params.frames_collected);
253             break;
254         default:
255             break;
256         }
257         SetDlgItemText( m_hwnd, IDC_STATUS, buffer );
258     }
259 }
260
261 //
262 // OnReceiveMessage
263 //
264 // Virtual method called by base class with Window messages
265 //
266 BOOL CCalibFilterProperties::OnReceiveMessage(HWND hwnd,
267                                               UINT uMsg,
268                                               WPARAM wParam,
269                                               LPARAM lParam)
270 {
271     switch (uMsg)
272     {        
273         case WM_INITDIALOG:
274         {
275         m_hwnd = hwnd;
276         SetControls( hwnd );
277         SetTimer( hwnd, 0, 50, 0 );
278         return 1L;
279         }
280
281         case WM_VSCROLL:
282         {
283         return 1L;
284         }
285
286         case WM_COMMAND:
287         if( HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDC_START_CALIB )
288         {
289             StartCalibration();
290         }
291         if( HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDC_SAVECALIBRATION )
292         {
293             SaveCameraParams();
294         }
295         if( HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDC_LOADCALIBRATION )
296         {
297             LoadCameraParams();
298         }
299         else if( HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDC_STOP )
300         {
301             //StopCalibration();
302         }
303         else if( HIWORD(wParam) == EN_CHANGE || HIWORD(wParam) == BN_CLICKED )
304         {
305             SetDirty();
306         }
307         return 1L;
308
309         case WM_TIMER:
310         {
311             PrintStatus();
312         }
313         return 1L;
314
315         case WM_DESTROY:
316         {
317             KillTimer( hwnd, 0 );
318         }
319         return 1L;
320     }
321     return CBasePropertyPage::OnReceiveMessage(hwnd,uMsg,wParam,lParam);
322
323 } // OnReceiveMessage
324
325
326 //
327 // OnConnect
328 //
329 // Called when the property page connects to a filter
330 //
331 HRESULT CCalibFilterProperties::OnConnect(IUnknown *pUnknown)
332 {
333     ASSERT(m_pCalibFilter == NULL);
334
335     HRESULT hr = pUnknown->QueryInterface(IID_ICalibFilter, (void **) &m_pCalibFilter);
336     if( FAILED(hr) ) return E_NOINTERFACE;
337
338     ASSERT(m_pCalibFilter);
339
340     GetParameters();
341     m_bDirty = FALSE;
342
343     return NOERROR;
344 } // OnConnect
345
346
347 //
348 // OnDisconnect
349 //
350 // Called when we're disconnected from a filter
351 //
352 HRESULT CCalibFilterProperties::OnDisconnect()
353 {
354     // Release of Interface after setting the parameters
355     if( !m_pCalibFilter ) return E_UNEXPECTED;
356     m_pCalibFilter->Release();
357     m_pCalibFilter = NULL;
358     return NOERROR;
359 } // OnDisconnect
360
361
362 //
363 // OnDeactivate
364 //
365 // We are being deactivated
366 //
367 HRESULT CCalibFilterProperties::OnDeactivate(void)
368 {
369     //MessageBox(0,"OnDeactivate","Info",MB_OK);
370     return NOERROR;
371 } // OnDeactivate
372
373
374 //
375 // OnApplyChanges
376 //
377 // Changes made should be kept. Change the  variable
378 //
379 HRESULT CCalibFilterProperties::OnApplyChanges()
380 {
381     GetControls(m_hwnd);
382     SetParameters();
383     m_bDirty = FALSE;
384     return  NOERROR;
385 } // OnApplyChanges
386
387 /* End of file. */
388