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) 2002, 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 // * Redistributions of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistributions 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.
47 #include "autorelease.h"
48 #include "ITracker3dFilter.h"
49 #include "Tracker3dPropertyPage.h"
51 #include "FilenamesDialog.h"
56 // Used by the DirectShow base classes to create instances
58 CUnknown *Tracker3dPropertyPage::CreateInstance(IUnknown *outer, HRESULT *phr)
60 CUnknown *punk = new Tracker3dPropertyPage(outer, phr);
71 Tracker3dPropertyPage::Tracker3dPropertyPage(IUnknown *outer, HRESULT *phr) :
72 CBasePropertyPage(NAME("Tracker3d Property Page"), outer, IDD_Tracker3dPropertyPage, IDS_TITLE),
73 m_pITracker3dFilter(NULL),
74 m_internal_change(false),
79 Tracker3dPropertyPage::~Tracker3dPropertyPage()
81 SAFE_RELEASE(m_pITracker3dFilter);
84 STDMETHODIMP Tracker3dPropertyPage::NonDelegatingQueryInterface(REFIID riid, void **ppv)
86 if (riid == IID_ITracker3dCallback)
87 return GetInterface(static_cast<ITracker3dCallback *>(this), ppv);
89 return CBasePropertyPage::NonDelegatingQueryInterface(riid, ppv);
92 void Tracker3dPropertyPage::SetDirty()
94 if (!m_internal_change)
98 m_pPageSite->OnStatusChange(PROPPAGESTATUS_DIRTY);
105 // Handles the messages for our property window
107 BOOL Tracker3dPropertyPage::OnReceiveMessage(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
123 case IDC_CALIBRATE_CAMERAS:
124 bool continuous = (GetKeyState(VK_CONTROL) < 0);
125 bool dialog = (GetKeyState(VK_SHIFT) < 0 || m_camera_intrinsics_filenames.size() != m_num_trackers);
126 int m = GetDlgItemInt(m_Dlg, IDC_CHECKERBOARD_WIDTH, NULL, false);
127 int n = GetDlgItemInt(m_Dlg, IDC_CHECKERBOARD_HEIGHT, NULL, false);
130 if (!dialog || FilenamesDialog(m_Dlg, m_num_trackers, m_camera_intrinsics_filenames)) // returns false if cancelled by user
132 std::vector<const char *> filenames(m_num_trackers);
133 for (int i = 0; i < m_num_trackers; i++)
134 filenames[i] = m_camera_intrinsics_filenames[i].c_str();
136 if (m_pITracker3dFilter->CalibrateCameras(m, n, &filenames[0], 1.0f, continuous) != NOERROR)
137 MessageBox(hwnd, "Error in Calibrate_Cameras", "Tracker3d", 0);
147 case IDC_NUMBER_OF_CAMERAS:
157 case IDC_TRACKER_DLL:
167 HWND dlg = GetParent(GetParent(m_Dlg));
169 GetWindowRect(dlg, &rect);
170 int width = rect.right - rect.left;
171 int height = rect.bottom - rect.top;
173 GetWindowRect(GetDesktopWindow(), &screen_rect);
174 int screen_width = screen_rect.right - screen_rect.left;
175 int screen_height = screen_rect.bottom - screen_rect.top;
176 int top = (screen_height - 40) - height;
177 MoveWindow(dlg, screen_width - 10 - width, top, width, height, true);
178 HWND parent = GetParent(dlg);
180 GetWindowText(parent, text, sizeof(text));
181 if (strstr(text, "GraphEdit") != NULL)
182 MoveWindow(GetParent(dlg), 10, top, screen_width - 30 - width, height, true);
187 return CBasePropertyPage::OnReceiveMessage(hwnd, msg, w, l);
190 STDMETHODIMP Tracker3dPropertyPage::Callback(const std::vector<Cv3dTrackerTrackedObject> &objects, const unsigned char *, IUnknown *)
194 for (i = 0; i < MAX_OBJECTS; i++)
195 EnableWindow(GetDlgItem(m_Dlg, IDC_LOCATION1 + i), false);
197 for (i = 0; i < objects.size(); i++)
200 sprintf(text, "(%#6.1f, %#6.1f, %#6.1f)", objects[i].p.x, objects[i].p.y, objects[i].p.z);
201 SetDlgItemText(m_Dlg, IDC_LOCATION1 + objects[i].id, text);
202 EnableWindow(GetDlgItem(m_Dlg, IDC_LOCATION1 + objects[i].id), true);
210 // Called when we connect to the filter
212 HRESULT Tracker3dPropertyPage::OnConnect(IUnknown *pUnknown)
214 SAFE_RELEASE(m_pITracker3dFilter); // shouldn't happen
216 HRESULT hr = pUnknown->QueryInterface(IID_ITracker3dFilter, (void **) &m_pITracker3dFilter);
220 hr = m_pITracker3dFilter->AddCallback(this);
231 // Called when we disconnect from the filter
233 HRESULT Tracker3dPropertyPage::OnDisconnect()
235 if (m_pITracker3dFilter == NULL)
238 m_pITracker3dFilter->RemoveCallback(this);
239 SAFE_RELEASE(m_pITracker3dFilter);
247 // We are being activated
249 HRESULT Tracker3dPropertyPage::OnActivate()
251 PostMessage(m_Dlg, WM_APP, 0, 0);
253 m_internal_change = true;
258 m_pITracker3dFilter->GetDefaultTracker(tracker_clsid);
259 if (tracker_clsid != GUID_NULL)
260 SelectTracker(tracker_clsid);
262 ITracker3dFilter::InputSize size;
263 m_pITracker3dFilter->GetPreferredInputSize(size);
264 if (size == ITracker3dFilter::SIZE_640x480)
265 CheckRadioButton(m_Dlg, IDC_ANY_SIZE, IDC_640x480, IDC_640x480);
266 else if (size == ITracker3dFilter::SIZE_320x240)
267 CheckRadioButton(m_Dlg, IDC_ANY_SIZE, IDC_640x480, IDC_320x240);
269 CheckRadioButton(m_Dlg, IDC_ANY_SIZE, IDC_640x480, IDC_ANY_SIZE);
271 m_pITracker3dFilter->GetNumberOfCameras(m_num_trackers);
272 SetDlgItemInt(m_Dlg, IDC_NUMBER_OF_CAMERAS, m_num_trackers, false);
274 SetDlgItemInt(m_Dlg, IDC_CHECKERBOARD_WIDTH, 6, false);
275 SetDlgItemInt(m_Dlg, IDC_CHECKERBOARD_HEIGHT, 4, false);
277 bool any_connected, all_connected;
278 m_pITracker3dFilter->IsConnected(any_connected, all_connected);
281 EnableWindow(GetDlgItem(m_Dlg, IDC_ANY_SIZE), false);
282 EnableWindow(GetDlgItem(m_Dlg, IDC_640x480), false);
283 EnableWindow(GetDlgItem(m_Dlg, IDC_320x240), false);
284 EnableWindow(GetDlgItem(m_Dlg, IDC_NUMBER_OF_CAMERAS), false);
285 EnableWindow(GetDlgItem(m_Dlg, IDC_TRACKER_DLL), false);
288 m_internal_change = false;
296 // We are being deactivated
298 HRESULT Tracker3dPropertyPage::OnDeactivate(void)
306 // Apply any changes so far made
308 HRESULT Tracker3dPropertyPage::OnApplyChanges()
310 if (IsDlgButtonChecked(m_Dlg, IDC_ANY_SIZE))
311 m_pITracker3dFilter->SetPreferredInputSize(ITracker3dFilter::SIZE_Any);
312 else if (IsDlgButtonChecked(m_Dlg, IDC_640x480))
313 m_pITracker3dFilter->SetPreferredInputSize(ITracker3dFilter::SIZE_640x480);
314 else if (IsDlgButtonChecked(m_Dlg, IDC_320x240))
315 m_pITracker3dFilter->SetPreferredInputSize(ITracker3dFilter::SIZE_320x240);
317 int new_num_trackers = GetDlgItemInt(m_Dlg, IDC_NUMBER_OF_CAMERAS, NULL, false);
318 GUID new_tracker, prev_tracker;
319 GetSelectedTracker(new_tracker);
320 m_pITracker3dFilter->GetDefaultTracker(prev_tracker);
322 if (new_num_trackers != m_num_trackers || prev_tracker != new_tracker)
324 m_pITracker3dFilter->SetDefaultTracker(new_tracker);
325 m_pITracker3dFilter->SetTrackers(new_num_trackers, NULL); // force it to reallocate the trackers
326 m_num_trackers = new_num_trackers;
339 AutoCloseKey(HKEY *pkey) : m_pkey(pkey) { };
344 RegCloseKey(*m_pkey);
350 #define AUTO_CLOSE_KEY(f) AutoCloseKey AutoCloseKey##f(&f)
352 static const char key_name[] = "Software\\Intel\\VAI\\3d Tracker\\";
355 void Tracker3dPropertyPage::LoadTrackers()
359 ICatInformation *catman;
360 hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_ALL, IID_ICatInformation, (void **)&catman);
365 hr = catman->EnumClassesOfCategories(1, const_cast<GUID *>(&CATID_Trackers), -1, NULL, &e);
373 while (e->Next(1, &clsid, NULL) == S_OK)
375 char tracker_name[80];
380 StringFromGUID2(clsid, textw, 50);
383 wsprintf(key_name, "CLSID\\%ls", textw);
385 HKEY key = 0; AUTO_CLOSE_KEY(key);
386 r = RegOpenKeyEx(HKEY_CLASSES_ROOT, key_name, 0, KEY_READ, &key);
387 if (r == ERROR_SUCCESS)
389 DWORD size = sizeof(tracker_name);
390 r = RegQueryValueEx(key, "", 0, NULL, (BYTE *)tracker_name, &size);
393 if (r != ERROR_SUCCESS)
395 // Since we don't have a name, we just have to add the CLSID text
396 wcstombs(tracker_name, textw, 50);
399 GUID *guid = new GUID;
401 int item = SendDlgItemMessage(m_Dlg, IDC_TRACKER_DLL, CB_ADDSTRING, 0, (LPARAM)tracker_name);
402 SendDlgItemMessage(m_Dlg, IDC_TRACKER_DLL, CB_SETITEMDATA, item, (LPARAM)guid);
409 void Tracker3dPropertyPage::SelectTracker(const GUID &tracker_clsid)
411 // See if it's already there
413 int n = SendDlgItemMessage(m_Dlg, IDC_TRACKER_DLL, CB_GETCOUNT, 0, 0);
414 for (item = 0; item < n; item++)
415 if (tracker_clsid == *(GUID *)SendDlgItemMessage(m_Dlg, IDC_TRACKER_DLL, CB_GETITEMDATA, item, 0))
418 // If it wasn't found, add it.
419 // Since we don't have a name, we just have to add the CLSID text
424 StringFromGUID2(tracker_clsid, textw, 50);
425 wcstombs(text, textw, 50);
426 item = SendDlgItemMessage(m_Dlg, IDC_TRACKER_DLL, CB_ADDSTRING, 0, (LPARAM)text);
427 GUID *guid = new GUID;
428 *guid = tracker_clsid;
429 SendDlgItemMessage(m_Dlg, IDC_TRACKER_DLL, CB_SETITEMDATA, item, (LPARAM)guid);
432 SendDlgItemMessage(m_Dlg, IDC_TRACKER_DLL, CB_SETCURSEL, item, 0);
435 void Tracker3dPropertyPage::GetSelectedTracker(GUID &tracker_clsid)
437 int item = SendDlgItemMessage(m_Dlg, IDC_TRACKER_DLL, CB_GETCURSEL, 0, 0);
438 tracker_clsid = *(GUID *)SendDlgItemMessage(m_Dlg, IDC_TRACKER_DLL, CB_GETITEMDATA, item, 0);