Move the sources to trunk
[opencv] / apps / StereoGR / StereoGRView.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                        Intel License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.\r
14 // Third party copyrights are property of their respective owners.\r
15 //\r
16 // Redistribution and use in source and binary forms, with or without modification,\r
17 // are permitted provided that the following conditions are met:\r
18 //\r
19 //   * Redistribution's of source code must retain the above copyright notice,\r
20 //     this list of conditions and the following disclaimer.\r
21 //\r
22 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
23 //     this list of conditions and the following disclaimer in the documentation\r
24 //     and/or other materials provided with the distribution.\r
25 //\r
26 //   * The name of Intel Corporation may not be used to endorse or promote products\r
27 //     derived from this software without specific prior written permission.\r
28 //\r
29 // This software is provided by the copyright holders and contributors "as is" and\r
30 // any express or implied warranties, including, but not limited to, the implied\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
33 // indirect, incidental, special, exemplary, or consequential damages\r
34 // (including, but not limited to, procurement of substitute goods or services;\r
35 // loss of use, data, or profits; or business interruption) however caused\r
36 // and on any theory of liability, whether in contract, strict liability,\r
37 // or tort (including negligence or otherwise) arising in any way out of\r
38 // the use of this software, even if advised of the possibility of such damage.\r
39 //\r
40 //M*/// StereoGRView.cpp : implementation of the CStereoGRView class\r
41 //\r
42 \r
43 #include "stdafx.h"\r
44 #include "StereoGR.h"\r
45 #include "MainFrm.h"\r
46 \r
47 #include "StereoGRDoc.h"\r
48 #include "StereoGRView.h"\r
49 #include "ChildFrm.h"\r
50 #include "PGControl.h"\r
51 #include <math.h>\r
52 #include "cv.h"\r
53 #include <gl\glu.h>\r
54 #include "triclops.h"\r
55 \r
56 #ifdef _DEBUG\r
57 #define new DEBUG_NEW\r
58 #undef THIS_FILE\r
59 static char THIS_FILE[] = __FILE__;\r
60 #endif\r
61 \r
62 #define IS_PTGREY_DOCTYPE IS_PTGREY_DATATYPE(GetDocument()->GetDocType())\r
63 \r
64 /////////////////////////////////////////////////////////////////////////////\r
65 // CStereoGRView\r
66 \r
67 IMPLEMENT_DYNCREATE(CStereoGRView, CView)\r
68 \r
69 BEGIN_MESSAGE_MAP(CStereoGRView, COpenGLView)\r
70         //{{AFX_MSG_MAP(CStereoGRView)\r
71         ON_WM_SIZE()\r
72         ON_WM_ERASEBKGND()\r
73         ON_WM_MOUSEMOVE()\r
74         ON_WM_RBUTTONDBLCLK()\r
75         ON_WM_CHAR()\r
76         ON_WM_KEYDOWN()\r
77         //}}AFX_MSG_MAP\r
78         // Standard printing commands\r
79         ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)\r
80         ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)\r
81         ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)\r
82 END_MESSAGE_MAP()\r
83 \r
84 \r
85 /////////////////////////////////////////////////////////////////////////////\r
86 // CStereoGRView construction/destruction\r
87 \r
88 CStereoGRView::CStereoGRView()\r
89 {\r
90     m_image = 0;\r
91     m_auxImage = 0;\r
92     m_recipient = 0;\r
93     m_bitmap = 0;\r
94         memset(m_texture, 0, 3*sizeof(void*));\r
95         memset(m_planes, 0, 3*sizeof(IplImage*));\r
96 \r
97         m_dist = 0;\r
98         m_azim = 0;\r
99         m_incl = 0;\r
100         m_distInc = 0;\r
101         m_azimInc = 0;\r
102         m_inclInc = 0;\r
103 }\r
104 \r
105 CStereoGRView::~CStereoGRView()\r
106 {\r
107     ((CMainFrame*)AfxGetMainWnd())->RemoveView(this);\r
108     iplDeallocate(m_image, IPL_IMAGE_ALL);\r
109     cvReleaseImageHeader(&m_recipient);\r
110     if(m_bitmap)\r
111         ::DeleteObject(m_bitmap);\r
112 \r
113         PTGreyUnFreezeData();\r
114 \r
115         for(int i = 0; i < 3; i++)\r
116         {\r
117                 free(m_texture[i]);\r
118                 cvReleaseImage(&m_planes[i]);\r
119         }\r
120 }\r
121 \r
122 BOOL CStereoGRView::PreCreateWindow(CREATESTRUCT& cs)\r
123 {\r
124         // TODO: Modify the Window class or styles here by modifying\r
125         //  the CREATESTRUCT cs\r
126 \r
127         return CView::PreCreateWindow(cs);\r
128 }\r
129 \r
130 /////////////////////////////////////////////////////////////////////////////\r
131 // CStereoGRView drawing\r
132 \r
133 void CStereoGRView::OnDraw(CDC* pDC)\r
134 {\r
135     if(!m_wasInitialized)\r
136         return;\r
137 \r
138 \r
139 #ifdef _GR_TRACE\r
140     TRACE("OnDraw: time = %d\n", GetTickCount());\r
141 #endif\r
142         CStereoGRDoc* pDoc = GetDocument();\r
143         ASSERT_VALID(pDoc);\r
144 \r
145         DrawScene(pDC);\r
146 }\r
147 \r
148 /////////////////////////////////////////////////////////////////////////////\r
149 // CStereoGRView printing\r
150 \r
151 BOOL CStereoGRView::OnPreparePrinting(CPrintInfo* pInfo)\r
152 {\r
153         // default preparation\r
154         return DoPreparePrinting(pInfo);\r
155 }\r
156 \r
157 void CStereoGRView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)\r
158 {\r
159         // TODO: add extra initialization before printing\r
160 }\r
161 \r
162 void CStereoGRView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)\r
163 {\r
164         // TODO: add cleanup after printing\r
165 }\r
166 \r
167 /////////////////////////////////////////////////////////////////////////////\r
168 // CStereoGRView diagnostics\r
169 \r
170 #ifdef _DEBUG\r
171 void CStereoGRView::AssertValid() const\r
172 {\r
173         CView::AssertValid();\r
174 }\r
175 \r
176 void CStereoGRView::Dump(CDumpContext& dc) const\r
177 {\r
178         CView::Dump(dc);\r
179 }\r
180 \r
181 CStereoGRDoc* CStereoGRView::GetDocument() // non-debug version is inline\r
182 {\r
183         ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStereoGRDoc)));\r
184         return (CStereoGRDoc*)m_pDocument;\r
185 }\r
186 #endif //_DEBUG\r
187 \r
188 /////////////////////////////////////////////////////////////////////////////\r
189 // CStereoGRView message handlers\r
190 \r
191 void CStereoGRView::OnInitialUpdate() \r
192 {\r
193         CView::OnInitialUpdate();\r
194 \r
195     // Update the document\r
196     GetDocument()->Initialize();\r
197 \r
198     \r
199     // Now initialize the view\r
200     UpdateParams();\r
201 \r
202     // Initialize the parent's data\r
203     ((CChildFrame*)GetParent())->m_view = this;\r
204     ((CMainFrame*)AfxGetMainWnd())->AddView(this);\r
205 \r
206     // Initialize OpenGL\r
207     switch(GetDocument()->GetDocType())\r
208     {\r
209         case PT_POINTCLOUD_IMAGE:\r
210         case GR_MAGICCUBE:\r
211         InitGL();\r
212                 break;\r
213     }\r
214 \r
215         UpdatePosition();\r
216 \r
217     m_wasInitialized = TRUE;\r
218 }\r
219 \r
220 void CStereoGRView::UpdateParams()\r
221 {\r
222         STEREOGRAPP(app)\r
223     int dataType = GetDocument()->GetDocType();\r
224     int width = PTGreyGetWidth(IS_PTGREY_DOCTYPE ? dataType : PT_RIGHT_RAW_IMAGE);\r
225     int height = PTGreyGetHeight(IS_PTGREY_DOCTYPE ? dataType : PT_RIGHT_RAW_IMAGE);\r
226     m_pixelSize = PTGreyGetPixelSize(IS_PTGREY_DOCTYPE ? dataType : 1);\r
227 \r
228     ((CChildFrame*)GetParent())->m_imageSize.cx = width;\r
229     ((CChildFrame*)GetParent())->m_imageSize.cy = height;\r
230 \r
231     if(dataType != PT_POINTCLOUD_IMAGE)\r
232     {\r
233         m_image = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);\r
234         m_recipient = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, m_pixelSize);\r
235                 if(m_pixelSize == 4)\r
236                         m_recipient->alphaChannel = 4;\r
237     }\r
238 \r
239         /* Restoring the window size */\r
240     CWnd* parent = GetParent();\r
241     CRect client, window, mainRect;\r
242     CSize newSize;\r
243     parent->GetClientRect(&client);\r
244     parent->GetWindowRect(&window);\r
245         AfxGetMainWnd()->GetWindowRect(&mainRect);\r
246         POINT tl = app->m_TopLefts[dataType];\r
247         if(tl.x < 0 || tl.x > mainRect.Size().cx)\r
248                 tl.x = 0;\r
249         if(tl.y < 0 || tl.y > mainRect.Size().cy)\r
250                 tl.y = 0;\r
251 \r
252     newSize = CSize(width, height) + window.Size() - client.Size();\r
253 //    parent->MoveWindow(tl.x, tl.y, newSize.cx, newSize.cy);\r
254 //    parent->ShowWindow(SW_SHOW);\r
255 \r
256     if(!IS_PTGREY_DOCTYPE)\r
257     {\r
258         iplSet(m_image, 0);\r
259         Invalidate();\r
260         UpdateWindow();\r
261     }\r
262 }\r
263 \r
264 \r
265 BOOL CStereoGRView::RetrieveImage()\r
266 {\r
267     if(!m_wasInitialized)\r
268         return FALSE;\r
269 \r
270 #ifdef _GR_TRACE\r
271     TRACE("RetrieveImage: time = %d\n", GetTickCount());\r
272 #endif\r
273     \r
274     int dataType = GetDocument()->GetDocType();\r
275     \r
276     m_recipient->imageData = const_cast<char*>(PTGreyGetDataPointer(dataType));\r
277     switch(m_pixelSize)\r
278     {\r
279     case 4:\r
280         {\r
281             // Convert the data\r
282             for(int i = 0; i < 3; i++)\r
283             {\r
284                 m_image->roi = iplCreateROI(i + 1, 0, 0, m_image->width, m_image->height);\r
285                 m_recipient->roi = m_image->roi;\r
286                 iplCopy(m_recipient, m_image);\r
287                 iplDeleteROI(m_image->roi);\r
288             }\r
289             m_image->roi = 0;\r
290             m_recipient->roi = 0;\r
291             break;\r
292         }\r
293         \r
294     case 1:\r
295         {\r
296             int mind, maxd;\r
297             PTGreyGetProperty("MinDisparity", &mind);\r
298             PTGreyGetProperty("MaxDisparity", &maxd);\r
299 \r
300             int scale = (int)floor(255.0f/(maxd - mind));\r
301             for(int i = 0; i < 3; i++)\r
302             {\r
303                 m_image->roi = iplCreateROI(i + 1, 0, 0, m_image->width, m_image->height);\r
304                 m_recipient->roi = iplCreateROI(1, 0, 0, m_image->width, m_image->height);\r
305                 iplCopy(m_recipient, m_image);\r
306                 if(dataType == PT_DISPARITY_IMAGE)            \r
307                 {\r
308                     // Rescale the image\r
309                     iplMultiplyS(m_image, m_image, scale);\r
310                 }\r
311                 iplDeleteROI(m_image->roi);\r
312                 iplDeleteROI(m_recipient->roi);\r
313             }\r
314             m_image->roi = 0;\r
315             m_recipient->roi = 0;\r
316 \r
317             break;\r
318         }\r
319 \r
320     default:\r
321         ASSERT(0);\r
322         break;\r
323     }\r
324 \r
325     return TRUE;\r
326 }\r
327 \r
328 void CStereoGRView::OnSize(UINT nType, int cx, int cy) \r
329 {\r
330         \r
331         CStereoGRDoc* pDoc = GetDocument();\r
332         int docType = pDoc->GetDocType();\r
333         if(docType == PT_POINTCLOUD_IMAGE || docType == GR_MAGICCUBE)\r
334                 COpenGLView::OnSize(nType, cx, cy);\r
335         else\r
336                 CView::OnSize(nType, cx, cy);\r
337         \r
338     InvalidateRect(0, 0);       \r
339     UpdateWindow();\r
340 }\r
341 \r
342 BOOL CStereoGRView::OnEraseBkgnd(CDC* pDC) \r
343 {\r
344     return TRUE;\r
345 }\r
346 \r
347 void CStereoGRView::Update() \r
348 {\r
349     if(IsNewData())\r
350     {\r
351         UpdateData();\r
352         UpdateView();\r
353         if(GetDocument()->GetDocType() == PT_DISPARITY_IMAGE)\r
354             TRACE0("Updated the image\n");\r
355     }\r
356     else\r
357     {\r
358         DefaultData();\r
359         DefaultView();\r
360         if(GetDocument()->GetDocType() == PT_DISPARITY_IMAGE)\r
361             TRACE0("The image did not change\n");\r
362     }\r
363 }\r
364 \r
365 int gl_frame_count = -1;\r
366 \r
367 double camDist = 3, camAzim = 0, camIncl = 0/*-900*/, camX = 0, camY = -0.5;\r
368 int shifting = 0, xold, yold, WHICH_BUTTON_ON, DispCount = 0; \r
369 double coneMinX = -0.5, coneMaxX =  0.5, coneMinY = -0.5, coneMaxY =  0.5, coneMaxZ =  1.0;\r
370 const MinCloudCount = 200;\r
371 \r
372 void CStereoGRView::InitGL()\r
373 {\r
374     // Make the GL window a child of the main frame...\r
375         STEREOGRAPP(app)\r
376 \r
377         InitOpenGL();\r
378 \r
379         /* Initialize GL */\r
380         CRect clientRect;\r
381         GetClientRect(&clientRect);\r
382         glViewport(0, 0, clientRect.Size().cx, clientRect.Size().cy);\r
383         glEnable(GL_CULL_FACE);\r
384         glEnable(GL_DEPTH_TEST);\r
385 \r
386         if(GetDocument()->GetDocType() == GR_MAGICCUBE)\r
387         {\r
388                 /* initialize the model */\r
389                 InitMagicCube();\r
390         }\r
391 }\r
392 \r
393 void CStereoGRView::ProcessImage()\r
394 {\r
395     int dataType = GetDocument()->GetDocType();\r
396 }\r
397 \r
398 void CStereoGRView::UpdateData()\r
399 {\r
400         int dataType = GetDocument()->GetDocType();\r
401         if(dataType == PT_POINTCLOUD_IMAGE)\r
402         {\r
403                 RetrievePointCloud();\r
404         }\r
405         else if(IS_PTGREY_DATATYPE(dataType))\r
406     {\r
407                 RetrieveImage();\r
408         }\r
409         else\r
410         {\r
411         RetrieveUserImage();\r
412     }\r
413 }\r
414 \r
415 void CStereoGRView::UpdateView()\r
416 {\r
417     InvalidateRect(0, 0);\r
418     UpdateWindow();\r
419 }\r
420 \r
421 IplImage* CStereoGRView::GetImage()\r
422 {\r
423     return m_image;\r
424 }\r
425 \r
426 IplImage* CStereoGRView::GetRecipient()\r
427 {\r
428     return m_recipient;\r
429 }\r
430 \r
431 BOOL CStereoGRView::IsNewData()\r
432 {\r
433     int docType = GetDocument()->GetDocType();\r
434     if(IS_PTGREY_DATATYPE(docType))\r
435     {\r
436         return PTGreyIsNewData(docType);\r
437     }\r
438     else\r
439     {\r
440         STEREOGRAPP(app)\r
441         return app->IsNewData();\r
442     }\r
443 }\r
444 \r
445 BOOL CStereoGRView::RetrieveUserImage()\r
446 {\r
447     int dataType = GetDocument()->GetDocType();\r
448     switch(dataType)\r
449     {\r
450     case GR_MASK_VIEW:\r
451         {\r
452             STEREOGRAPP(app)\r
453             IplImage* image = app->m_findHand.m_mask;\r
454             if(image)\r
455             {\r
456                 iplSet(m_image, 0);\r
457                 IplROI roi = {0, 0, 0, 0, 0};\r
458                 roi.width = image->width;\r
459                 roi.height = image->height;\r
460                 m_image->roi = &roi;\r
461                 for(int i = 0; i < 3; i++)\r
462                 {\r
463                     roi.coi = i + 1;\r
464                     iplCopy(image, m_image);\r
465                 }\r
466                 m_image->roi = 0;\r
467             }\r
468             else\r
469                 iplSet(m_image, 0);\r
470         }\r
471         break;\r
472     }\r
473 \r
474     return TRUE;\r
475 }\r
476 \r
477 void CStereoGRView::DefaultData()\r
478 {\r
479     switch(GetDocument()->GetDocType())\r
480     {\r
481     case GR_MASK_VIEW:\r
482         iplSet(m_image, 0);\r
483         break;\r
484     }\r
485 }\r
486 \r
487 void CStereoGRView::DefaultView()\r
488 {\r
489     UpdateView();\r
490 }\r
491 \r
492 void CStereoGRView::UpdatePosition()\r
493 {\r
494         STEREOGRAPP(app)\r
495         int docType = GetDocument()->GetDocType();\r
496     int width = PTGreyGetWidth(IS_PTGREY_DOCTYPE ? docType : PT_RIGHT_RAW_IMAGE);\r
497     int height = PTGreyGetHeight(IS_PTGREY_DOCTYPE ? docType : PT_RIGHT_RAW_IMAGE);\r
498         CView* view = app->GetView(docType);\r
499         if(view)\r
500         {\r
501                 CRect windowRect, clientRect, mainRect;\r
502                 CWnd* wnd = GetParent();\r
503                 wnd->GetWindowRect(&windowRect);\r
504                 wnd->GetClientRect(&clientRect);\r
505                 ((CMainFrame*)AfxGetMainWnd())->GetClientRect(mainRect);\r
506                 CPoint tl = CPoint(app->m_TopLefts[docType]) + mainRect.TopLeft() - CPoint(4, 4);\r
507                 if(tl.x == -1)\r
508                         return;\r
509 \r
510                 if(tl.x < 0 || tl.x > clientRect.Size().cx)\r
511                         tl.x = 0;\r
512                 if(tl.y < 0 || tl.y > clientRect.Size().cy)\r
513                         tl.y = 0;\r
514                 CSize windowSize = CSize(width, height) + windowRect.Size() - clientRect.Size();\r
515                 ::MoveWindow(wnd->m_hWnd, tl.x, tl.y, windowSize.cx, windowSize.cy, TRUE);\r
516         }\r
517 }\r
518 \r
519 void CStereoGRView::OnDrawImage(CDC *pDC)\r
520 {\r
521     if(!m_image)\r
522     {\r
523         return;\r
524     }\r
525 \r
526     HDC hdc;\r
527     hdc = ::CreateCompatibleDC(pDC->m_hDC);\r
528 \r
529     // Display the current image\r
530     char buffer[sizeof(BITMAPINFO)];\r
531     BITMAPINFO* binfo = (BITMAPINFO*)buffer;\r
532     memset(binfo, 0, sizeof(BITMAPINFOHEADER));\r
533     binfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);\r
534     if(!m_bitmap)\r
535     {\r
536         // Create the bitmap\r
537 \r
538         BITMAPINFOHEADER* bih = &binfo->bmiHeader;\r
539                 bih->biBitCount = m_image->nChannels*8;\r
540                 bih->biWidth = m_image->width;\r
541                 bih->biHeight = m_image->height;\r
542         bih->biPlanes = 1;\r
543         bih->biCompression = BI_RGB;\r
544         bih->biSizeImage = 0;\r
545         \r
546         m_bitmap = CreateDIBSection(hdc, binfo, 0, 0, 0, DIB_RGB_COLORS);\r
547     }\r
548 \r
549     int colors = DIB_RGB_COLORS;\r
550     \r
551     ::SelectObject(hdc, m_bitmap);\r
552         binfo->bmiHeader.biBitCount = 0;\r
553         GetDIBits(hdc, m_bitmap, 0, 0, 0, binfo, colors);\r
554     binfo->bmiHeader.biBitCount = 24;\r
555     binfo->bmiHeader.biHeight = -abs(binfo->bmiHeader.biHeight);\r
556     SetDIBits(hdc, m_bitmap, 0, m_image->height, m_image->imageData, binfo, colors);\r
557 \r
558 \r
559     ::SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);\r
560     CRect client;\r
561     GetClientRect(&client);\r
562     ::StretchBlt(pDC->m_hDC, 0, 0, client.Size().cx, client.Size().cy, \r
563         hdc, 0, 0, m_image->width, m_image->height, SRCCOPY);\r
564 //    ::BitBlt(pDC->m_hDC, 0, 0, m_image->width, m_image->height, \r
565 //        hdc, 0, 0, SRCCOPY);\r
566     ::DeleteDC(hdc);\r
567 }\r
568 \r
569 void CStereoGRView::OnDrawPointCloud()\r
570 {\r
571         CvPoint3D32f *ZKeyBuffer; float v[6];\r
572         BeginScene();\r
573         glViewport(0, 0, m_width, m_height);\r
574 \r
575         // Setup projection matrix\r
576         glMatrixMode(GL_PROJECTION);\r
577         glLoadIdentity();\r
578         gluPerspective( /* field of view in degree */ 45.0,     /* aspect ratio */ 1.0, /* Z near */ 1.0, /* Z far */ 1000.0);\r
579 \r
580         // position the camera\r
581         glMatrixMode(GL_MODELVIEW);\r
582         glLoadIdentity();\r
583         glTranslated(0, 0, -camDist);\r
584         glTranslated(m_translate[0], m_translate[1], m_translate[2]);\r
585         glTranslated(0,0,-1.3);\r
586         glMultMatrixd(m_rotate);\r
587         glRotated(180, 0.0, 0.0, 1.0);\r
588 \r
589 //    PTGreyFreezeData(INFINITE);\r
590         ZKeyBuffer = (CvPoint3D32f*)m_points.begin();\r
591 \r
592         glColor3f(0,1,0);\r
593     int pointCount = m_points.size()/3;\r
594 \r
595         int swidth = PTGreyGetWidth(PT_DISPARITY_IMAGE);\r
596         int sheight = PTGreyGetHeight(PT_DISPARITY_IMAGE);\r
597         glVertexPointer(3, GL_FLOAT, 0, ZKeyBuffer);\r
598         glEnableClientState(GL_VERTEX_ARRAY);\r
599         glDrawArrays(GL_POINTS, 0, pointCount);\r
600 /*\r
601         glBegin(GL_POINTS);\r
602         for(int i = 0; i < pointCount; i++)\r
603         {\r
604                 glVertex3f(ZKeyBuffer[i].x, ZKeyBuffer[i].y, ZKeyBuffer[i].z);\r
605         }\r
606         glEnd();\r
607 */\r
608 \r
609 //    PTGreyUnFreezeData();\r
610     \r
611         if (pointCount > MinCloudCount) \r
612     {\r
613         float* OpenGLLine = (float*)&((CStereoGRApp*)AfxGetApp())->m_findHand.m_line;\r
614                 // draw the line \r
615                 glColor3f(1,0,0);\r
616                 glBegin(GL_LINE_STRIP);\r
617                 v[0] = OpenGLLine[3] - OpenGLLine[0];\r
618                 v[1] = OpenGLLine[4] - OpenGLLine[1];\r
619                 v[2] = OpenGLLine[5] - OpenGLLine[2];\r
620                 v[3] = OpenGLLine[3] + OpenGLLine[0];\r
621                 v[4] = OpenGLLine[4] + OpenGLLine[1];\r
622                 v[5] = OpenGLLine[5] + OpenGLLine[2];\r
623                 glVertex3d(v[0], v[1], v[2]);                   \r
624                 glVertex3d(v[3], v[4], v[5]);   \r
625                 glEnd();\r
626         }\r
627         \r
628         // draw the viewing cone\r
629         glColor3f(1,1,0); \r
630         // big square\r
631         glBegin(GL_LINE_STRIP);\r
632         glVertex3d(coneMinX, coneMinY, coneMaxZ);\r
633         glVertex3d(coneMinX, coneMaxY, coneMaxZ);\r
634         glVertex3d(coneMaxX, coneMaxY, coneMaxZ);\r
635         glVertex3d(coneMaxX, coneMinY, coneMaxZ);\r
636 \r
637         // four lines\r
638         glBegin(GL_LINES);\r
639         glVertex3d(0,0,0);\r
640         glVertex3d(coneMinX, coneMinY, coneMaxZ);\r
641         glVertex3d(0,0,0);\r
642         glVertex3d(coneMinX, coneMaxY, coneMaxZ);\r
643         glVertex3d(0,0,0);\r
644         glVertex3d(coneMaxX, coneMaxY, coneMaxZ);\r
645         glVertex3d(0,0,0);\r
646         glVertex3d(coneMaxX, coneMinY, coneMaxZ);\r
647         glEnd();\r
648 \r
649         glTranslated(0,0,1.3);\r
650 \r
651         EndScene();\r
652 }\r
653 \r
654 void CStereoGRView::OnMouseMove(UINT nFlags, CPoint point) \r
655 {\r
656         CStereoGRDoc* pDoc = GetDocument();\r
657         int docType = pDoc->GetDocType();\r
658         if(docType == PT_POINTCLOUD_IMAGE || docType == GR_MAGICCUBE)\r
659                 COpenGLView::OnMouseMove(nFlags, point);\r
660         else\r
661                 CView::OnMouseMove(nFlags, point);\r
662 }\r
663 \r
664 \r
665 void CStereoGRView::OnRButtonDblClk(UINT nFlags, CPoint point) \r
666 {\r
667         InitCameraPosition();   \r
668         OnDrawPointCloud();\r
669         COpenGLView::OnRButtonDblClk(nFlags, point);\r
670 }\r
671 \r
672 void CStereoGRView::OnDrawMagicCube()\r
673 {\r
674         bool is_texture = m_texture[0] ? true : false;\r
675         STEREOGRAPP(app);\r
676         SetOwnContext();\r
677         glViewport(0, 0, m_width, m_height);\r
678 \r
679         IplImage* data[3];\r
680         data[0] = app->GetView(PT_RIGHT_RAW_IMAGE)->GetImage();\r
681         data[1] = app->GetView(PT_TOP_RAW_IMAGE)->GetImage();\r
682         data[2] = app->GetView(PT_LEFT_RAW_IMAGE)->GetImage();\r
683         int width = data[0]->width;\r
684         int height = data[0]->height;\r
685         const char* bits[3];\r
686         for(int i = 0; i < 3; i++)\r
687                 bits[i] = data[i]->imageData;\r
688         \r
689         InitTexture(width, height);\r
690         ScaleTextures(width, height, (const void**)bits, 3);\r
691         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
692 \r
693 //    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);\r
694 //    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);\r
695     glShadeModel(GL_SMOOTH);\r
696 \r
697         BeginScene();\r
698         glDisable(GL_CULL_FACE);\r
699         glMatrixMode(GL_PROJECTION);\r
700         glLoadIdentity();\r
701         gluPerspective( /* field of view in degree */ 45.0,     /* aspect ratio */ 1.0, /* Z near */ 1.0, /* Z far */ 1000.0);\r
702         glMatrixMode(GL_MODELVIEW);\r
703         glLoadIdentity();\r
704         m_azim += m_azimInc*10;\r
705         m_incl += m_inclInc*10;\r
706         m_dist += m_distInc;\r
707         m_azimInc *= 0.95f;\r
708         m_inclInc *= 0.95f;\r
709         m_distInc *= 0.95f;\r
710         glTranslated(0, 0, -m_dist);\r
711         glTranslated(0, 0, -2);\r
712         glRotated(m_azim, 0.0, 1.0, 0.0);\r
713         glRotated(m_incl, 0.0, 0.0, 1.0);\r
714         glMultMatrixd(m_rotate);\r
715 \r
716         if(!is_texture)\r
717         {\r
718                 glTexImage2D(GL_TEXTURE_2D, 0, 3, m_tex_width, m_tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, m_texture[0]);\r
719         }\r
720         else\r
721         {\r
722                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_tex_width, m_tex_height, GL_RGB, GL_UNSIGNED_BYTE, m_texture[0]);\r
723         }\r
724     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);\r
725     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);\r
726     glEnable(GL_TEXTURE_2D);\r
727         glBegin(GL_QUADS);\r
728         glColor3f(1, 1, 1);\r
729 \r
730         glTexCoord2f(0, 1);     glVertex3f(-0.5, -0.5, -0.5);\r
731         glTexCoord2f(1, 1);     glVertex3f( 0.5, -0.5, -0.5);\r
732         glTexCoord2f(1, 0);     glVertex3f( 0.5,  0.5, -0.5);\r
733         glTexCoord2f(0, 0);     glVertex3f(-0.5,  0.5, -0.5);\r
734 \r
735         glTexCoord2f(0, 1);     glVertex3f(-0.5, -0.5, 0.5);\r
736         glTexCoord2f(1, 1);     glVertex3f( 0.5, -0.5, 0.5);\r
737         glTexCoord2f(1, 0);     glVertex3f( 0.5,  0.5, 0.5);\r
738         glTexCoord2f(0, 0);     glVertex3f(-0.5,  0.5, 0.5);\r
739 \r
740         glEnd();\r
741         \r
742         if(!is_texture)\r
743         {\r
744                 glTexImage2D(GL_TEXTURE_2D, 0, 3, m_tex_width, m_tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, m_texture[1]);\r
745         }\r
746         else\r
747         {\r
748                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_tex_width, m_tex_height, GL_RGB, GL_UNSIGNED_BYTE, m_texture[1]);\r
749         }\r
750     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);\r
751     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);\r
752     glEnable(GL_TEXTURE_2D);\r
753         glBegin(GL_QUADS);\r
754         glColor3f(1, 1, 1);\r
755 \r
756         glTexCoord2f(0, 1);     glVertex3f(0.5, -0.5,  -0.5);\r
757         glTexCoord2f(1, 1);     glVertex3f(0.5, -0.5,   0.5);\r
758         glTexCoord2f(1, 0);     glVertex3f(0.5,  0.5,   0.5);\r
759         glTexCoord2f(0, 0);     glVertex3f(0.5,  0.5,  -0.5);\r
760 \r
761         glTexCoord2f(0, 1);     glVertex3f(-0.5, -0.5,  -0.5);\r
762         glTexCoord2f(1, 1);     glVertex3f(-0.5, -0.5,   0.5);\r
763         glTexCoord2f(1, 0);     glVertex3f(-0.5,  0.5,   0.5);\r
764         glTexCoord2f(0, 0);     glVertex3f(-0.5,  0.5,  -0.5);\r
765 \r
766         glEnd();\r
767 \r
768         if(!is_texture)\r
769         {\r
770                 glTexImage2D(GL_TEXTURE_2D, 0, 3, m_tex_width, m_tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, m_texture[2]);\r
771         }\r
772         else\r
773         {\r
774                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_tex_width, m_tex_height, GL_RGB, GL_UNSIGNED_BYTE, m_texture[2]);\r
775         }\r
776     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);\r
777     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);\r
778     glEnable(GL_TEXTURE_2D);\r
779         glBegin(GL_QUADS);\r
780         glColor3f(1, 1, 1);\r
781 \r
782         glTexCoord2f(0, 1);     glVertex3f(-0.5, 0.5,  -0.5);\r
783         glTexCoord2f(1, 1);     glVertex3f(-0.5, 0.5,   0.5);\r
784         glTexCoord2f(1, 0);     glVertex3f( 0.5, 0.5,   0.5);\r
785         glTexCoord2f(0, 0);     glVertex3f( 0.5, 0.5,  -0.5);\r
786 \r
787         glTexCoord2f(0, 1);     glVertex3f(-0.5, -0.5,  -0.5);\r
788         glTexCoord2f(1, 1);     glVertex3f(-0.5, -0.5,   0.5);\r
789         glTexCoord2f(1, 0);     glVertex3f( 0.5, -0.5,   0.5);\r
790         glTexCoord2f(0, 0);     glVertex3f( 0.5, -0.5,  -0.5);\r
791 \r
792         glEnd();\r
793         EndScene();\r
794 }\r
795 \r
796 void CStereoGRView::InitMagicCube()\r
797 {\r
798         STEREOGRAPP(app);\r
799         SetOwnContext();\r
800 }\r
801 \r
802 void CStereoGRView::DrawScene(CDC* pDC)\r
803 {\r
804         switch(GetDocument()->GetDocType())\r
805         {\r
806         case PT_POINTCLOUD_IMAGE:\r
807                 OnDrawPointCloud();\r
808                 break;\r
809         case GR_MAGICCUBE:\r
810                 OnDrawMagicCube();\r
811                 break;\r
812 \r
813         default:\r
814                 OnDrawImage(pDC);\r
815                 break;\r
816         }\r
817 }\r
818 \r
819 void CStereoGRView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) \r
820 {\r
821         STEREOGRAPP(app)\r
822         CView::OnChar(nChar, nRepCnt, nFlags);\r
823         \r
824         COpenGLView::OnChar(nChar, nRepCnt, nFlags);\r
825 }\r
826 \r
827 void CStereoGRView::InitTexture(int width, int height)\r
828 {\r
829         if(m_texture[0])\r
830                 return;\r
831         \r
832         switch(width)\r
833         {\r
834         case 160:\r
835                 m_tex_width = 128;\r
836                 m_tex_height = 128;\r
837                 break;\r
838 \r
839         case 320:\r
840                 m_tex_width  = 256;\r
841                 m_tex_height = 256;\r
842                 break;\r
843 \r
844         case 640:\r
845                 m_tex_width = 512;\r
846                 m_tex_height = 512;\r
847                 break;\r
848         }\r
849 \r
850         int pixel_count = m_tex_width*m_tex_height;\r
851         for(int i = 0; i < 3; i++)\r
852                 m_texture[i] = malloc(3*pixel_count);\r
853 }\r
854 \r
855 \r
856 void CStereoGRView::ScaleTextures(int width, int height, const void** bits, int count)\r
857 {\r
858         int pixel_count = m_tex_width*m_tex_height;\r
859         IplImage* source = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, 3);\r
860         IplImage* dest = cvCreateImageHeader(cvSize(m_tex_width, m_tex_height), IPL_DEPTH_8U, 3);\r
861 \r
862         if(!m_planes[0])\r
863         {\r
864                 m_planes[0] = cvCreateImage(cvSize(m_tex_width, m_tex_height), IPL_DEPTH_8U, 1);\r
865                 m_planes[1] = cvCreateImage(cvSize(m_tex_width, m_tex_height), IPL_DEPTH_8U, 1);                \r
866         }\r
867         \r
868         for(int i = 0; i < count; i++)\r
869         {\r
870                 source->imageData = (char*)bits[i];\r
871                 dest->imageData = (char*)m_texture[i];\r
872 \r
873                 iplSet(dest, 255);\r
874                 iplResize(source, dest, m_tex_width, width, m_tex_height, height, IPL_INTER_NN);\r
875 \r
876                 dest->roi = iplCreateROI(1, 0, 0, m_tex_width, m_tex_height);\r
877                 iplCopy(dest, m_planes[0]);\r
878                 dest->roi->coi = 3;\r
879                 iplCopy(dest, m_planes[1]);\r
880                 iplCopy(m_planes[0], dest);\r
881                 dest->roi->coi = 1;\r
882                 iplCopy(m_planes[0], dest);\r
883                 iplDeleteROI(dest->roi);\r
884                 dest->roi = 0;\r
885         }\r
886         \r
887         cvReleaseImageHeader(&source);\r
888         cvReleaseImageHeader(&dest);\r
889 //      int e = gluScaleImage(GL_RGB, width, height, GL_UNSIGNED_BYTE, bits, \r
890 //              m_tex_width, m_tex_height*height/width, GL_UNSIGNED_BYTE, m_texture);\r
891 }\r
892 \r
893 void CStereoGRView::RetrievePointCloud()\r
894 {\r
895         m_points.clear();\r
896         PTGreyFreezeData(INFINITE);\r
897 \r
898         int count = PTGreyGetPointCount();\r
899         if(count > 0)\r
900         {\r
901                 const float* points = (const float*)PTGreyGetDataPointer(PT_POINTCLOUD_IMAGE);\r
902                 m_points.insert(m_points.begin(), &points[0], &points[3*count - 1]);\r
903         }\r
904         PTGreyUnFreezeData();\r
905 }\r
906 \r
907 void CStereoGRView::RotateAzim(float val)\r
908 {\r
909         m_azimInc = val;\r
910 }\r
911 \r
912 void CStereoGRView::RotateIncl(float val)\r
913 {\r
914         m_inclInc = val;\r
915 }\r
916 \r
917 void CStereoGRView::Translate(float val)\r
918 {\r
919         m_distInc = val;\r
920 }\r
921 \r
922 void CStereoGRView::InitModel()\r
923 {\r
924         m_azimInc = 0;\r
925         m_inclInc = 0;\r
926         m_distInc = 0;\r
927 \r
928         m_azim = 0;\r
929         m_incl = 0;\r
930         m_dist = 0;\r
931 }\r
932 \r
933 void CStereoGRView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) \r
934 {\r
935         if(nChar == '7')        \r
936         {\r
937                 InitModel();\r
938                 InitCameraPosition();\r
939                 OnDrawMagicCube();\r
940         }\r
941         COpenGLView::OnKeyDown(nChar, nRepCnt, nFlags);\r
942 }\r