Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

Window.h

Go to the documentation of this file.
00001 #ifndef Impala_Visualization_Window_h
00002 #define Impala_Visualization_Window_h
00003 
00004 #include <vector>
00005 #include <strstream>
00006 #include "OglGui/Window.h"
00007 #include "Basis/CmdOptions.h"
00008 #include "Visualization/ViewWithRect.h"
00009 #include "Visualization/AppController.h"
00010 #include "Visualization/AppControlGUI.h"
00011 #include "Core/Geometry/RectangleTiler.h"
00012 
00013 namespace Impala
00014 {
00015 namespace Visualization
00016 {
00017 
00018 
00019 class Window : public OglGui::Window, public AppControlGUI
00020 {
00021 public:
00022     typedef Core::Array::Array2dVec3UInt8 Array2dVec3UInt8;
00023 
00024     Window(int x, int y, int width, int height, bool is2d=true) :
00025         OglGui::Window(x, y, width, height, is2d),
00026         AppControlGUI(-1, true)
00027     {
00028         Init();
00029         AppController::Instance().AddControl(this);
00030     }
00031 
00032     Window(OglGui::Window* parent, int width, int height, bool is2d=true) :
00033         OglGui::Window(parent, width, height, is2d),
00034         AppControlGUI(-1, false)
00035     {
00036         Init();
00037     }
00038 
00039     Window(OglGui::Window* parent, int x, int y, int width, int height, bool is2d=true) :
00040         OglGui::Window(parent, x, y, width, height, is2d),
00041         AppControlGUI(-1, false)
00042     {
00043         Init();
00044     }
00045 
00046     virtual
00047     ~Window()
00048     {
00049 #ifdef DESTRUCT_VERBOSE
00050 printf("virtual Vis::Window destructor\n");
00051 #endif
00052     }
00053 
00054     virtual void
00055     HandleStart()
00056     {
00057         if (!Valid())
00058             return;
00059         OglGui::Sys::Instance().Start(mOglWnd);
00060     }
00061 
00062     static Window*
00063     CastToWindow( OglGui::Window *oWnd )
00064     {
00065         if( !oWnd )
00066             return 0;
00067         if( oWnd->GetRunTimeType() & 4 )
00068             // RvB: Next line should probably be a static_cast. But it seems to work
00069             return (Window *) oWnd;
00070         return 0;
00071     }
00072 
00073     Window*
00074     MakeViewport(int width, int height, bool is2d)
00075     {
00076         Window* w = new Window(this, width, height);
00077         w->SetIs2d(is2d);
00078         return w;
00079     }
00080 
00081     // viewer part
00082     int
00083     NrViewers()
00084     {
00085         return mOglView.size();
00086     }
00087 
00088     void
00089     SetMakeViewWithRect(bool flag)
00090     {
00091         mMakeViewWithRect = flag;
00092     }
00093 
00094     void
00095     MakeView(int imW, int imH, double scale, bool texturing, bool noTexScaling,
00096              bool keepDispImage, OglGui::ViewListener* listener)
00097     {
00098         int n = mOglView.size();
00099         View* view;
00100         if (mIs2d)
00101         {
00102             int pX2d;
00103             int pY2d;
00104             mViewTiler.GetPos2d(scale * imW, scale * imH, pX2d, pY2d);
00105             if (mMakeViewWithRect)
00106                 view = new ViewWithRect(mOglWnd, pX2d, pY2d, imW, imH, scale,
00107                                         texturing, noTexScaling, keepDispImage,
00108                                         listener);
00109             else
00110                 view = new View(mOglWnd, pX2d, pY2d, imW, imH, scale,
00111                                 texturing, noTexScaling, keepDispImage,
00112                                 listener);
00113         }
00114         else
00115         {
00116             if (n == 0)
00117             {
00118                 mFirstImSize = Max(imW, imH);
00119                 std::cout << "Setting firstImSize " << imW << " x " << imH << std::endl;
00120             }
00121             float scaleX = ((float) imW / mFirstImSize) * scale;
00122             float scaleY = ((float) imH / mFirstImSize) * scale;
00123             std::cout << "scale : " << scaleX << " x " << scaleY << std::endl;
00124             float pX3d;
00125             float pY3d;
00126             float pZ3d;
00127             mViewTiler.GetPos3d(scaleX, scaleY, pX3d, pY3d, pZ3d);
00128             view = new View(mOglWnd, pX3d, pY3d, pZ3d, imW, imH, scaleX,
00129                             scaleY, noTexScaling, keepDispImage, listener);
00130         }
00131         mOglView.push_back(view);
00132     }
00133 
00134     // update image data of given view by copying data from given buffer
00135     // view will be created if it doesn't exist
00136     // however, view should be the first vacancy in the list
00137     // in case it exists it should have the right sizes
00138     void
00139     UpdateView(int view, unsigned char* dataPtr, int width, int height,
00140                double scale = 1.0, OglGui::ViewListener* listener = 0)
00141     {
00142         if (! dataPtr)
00143         {
00144             std::cout << "Window::updateView : dataPtr is null" << std::endl;
00145             return;
00146         }
00147         if (view > mOglView.size())
00148         {
00149             std::cout << "Window::updateView : invalid view : " << view << std::endl;
00150             return;
00151         }
00152         if (view == mOglView.size())
00153             MakeView(width, height, scale, false, false, true, listener);
00154         mOglView[view]->UpdateImage(dataPtr);
00155     }
00156 
00157     void
00158     UpdateView(int view, Array2dVec3UInt8* image,
00159                OglGui::ViewListener* listener = 0)
00160     {
00161         UpdateView(view, image->CPB(0,0), image->W(), image->H(), 1.0, listener);
00162     }
00163 
00164     // update image data of given view by copying data from given buffer
00165     // view will be created if it doesn't exist
00166     // however, view should be the first vacancy in the list
00167     // in case it exists it should have the right sizes
00168     template <class ArrayT>
00169     void
00170     UpdateView(int view, ArrayT* im, std::string displayMode,
00171                double scale = 1.0, OglGui::ViewListener* listener = 0)
00172     {
00173         if (! im)
00174         {
00175             std::cout << "Window::updateView : im is null" << std::endl;
00176             return;
00177         }
00178         if (view > mOglView.size())
00179         {
00180             std::cout << "Window::updateView : invalid view : " << view << std::endl;
00181             return;
00182         }
00183         if (view == mOglView.size())
00184             MakeView(im->CW(), im->CH(), scale, false, false, true, listener);
00185         mOglView[view]->UpdateImage(im, displayMode);
00186         mOglView[view]->SetZoom(scale, scale);
00187     }
00188 
00189     void
00190     SetViewText(int viewIdx, std::string text)
00191     {
00192         View* view = GetView(viewIdx);
00193         if (!view)
00194             return;
00195         view->SetText(text);
00196     }
00197 
00198     void
00199     SetViewSelected(int viewIdx, bool clearOthers = true)
00200     {
00201         for (int i=0 ; i<mOglView.size() ; i++)
00202         {
00203             if (i == viewIdx)
00204                 mOglView[i]->SetSelected(true);
00205             else if (clearOthers)
00206                 mOglView[i]->SetSelected(false);
00207         }
00208     }
00209 
00210     void
00211     SetViewSelectedTo(int viewIdx, bool state)
00212     {
00213         if (viewIdx >=mOglView.size() || viewIdx < 0)
00214             return;
00215         mOglView[viewIdx]->SetSelected(state);
00216     }
00217 
00218     bool
00219     GetViewSelected(int viewIdx)
00220     {
00221         return mOglView[viewIdx]->GetSelected();
00222     }
00223 
00224     void
00225     SetViewVisible(int viewIdx, bool flag)
00226     {
00227         View* view = GetView(viewIdx);
00228         if (!view)
00229             return;
00230         view->SetVisible(flag);
00231     }
00232 
00233     void
00234     SetViewPos(int viewIdx, int x, int y)
00235     {
00236         View* view = GetView(viewIdx);
00237         if (!view)
00238             return;
00239         view->SetDimensions2D(x, y, RETAIN, RETAIN);
00240     }
00241 
00242     View*
00243     GetView(int index)
00244     {
00245         if ((index < 0) || (index >= mOglView.size()))
00246             return 0;
00247         return mOglView[index];
00248     }
00249 
00250     ViewWithRect*
00251     GetViewWithRect(int index)
00252     {
00253         return (ViewWithRect*) GetView(index);
00254     }
00255 
00256     int
00257     GetViewIdx(View* view)
00258     {
00259         for (int i=0 ; i<mOglView.size() ; i++)
00260             if (mOglView[i] == view)
00261                 return i;
00262         return -1;
00263     }
00264 
00265     // Deprecate?? Try using View::OnMouse or OnViewMouse
00266     View*
00267     FindView(int x, int y)
00268     {
00269         if (mIs2d)
00270         {
00271             OGLVIEW* view = viewSys.FindView(mOglWnd, x, y);
00272             if (!view)
00273                 return 0;
00274             OglGui::View* v = (OglGui::View*) viewSys.GetSysData(view, 1);
00275             return (View*) v;
00276         }
00277         else
00278         {
00279             OGLVIEW3D* view = view3DSys.FindView(mOglWnd, x, y);
00280             if (!view)
00281                 return 0;
00282             OglGui::View* v = (OglGui::View*) view3DSys.GetSysData(view, 1);
00283             return (View*) v;
00284         }
00285     }
00286 
00287     void
00288     SetAllowRepositionViewers(bool flag)
00289     {
00290         mAllowRepositionViewers = flag;
00291     }
00292 
00293     virtual void
00294     RepositionViewers()
00295     {
00296         if (!mAllowRepositionViewers)
00297             return;
00298         mViewTiler.ResetWnd(WndWidth(), WndHeight());
00299         for (int v=0 ; v<mOglView.size() ; v++)
00300         {
00301             if (mIs2d)
00302             {
00303                 int x, y, w, h;
00304                 mOglView[v]->GetDimensions2D(&x, &y, &w, &h);
00305                 mViewTiler.GetPos2d(w, h, x, y);
00306                 mOglView[v]->SetDimensions2D(x, y, RETAIN, RETAIN);
00307             }
00308             else
00309             {
00310                 // todo
00311             }
00312         }
00313     }
00314 
00315     void
00316     DeleteViewers()
00317     {
00318         if (mIs2d)
00319             viewSys.DeleteViews(mOglWnd);
00320         else
00321             view3DSys.DeleteViews(mOglWnd);
00322         mOglView.clear();
00323         mViewTiler.ResetPos();
00324     }
00325 
00326     int
00327     SuggestWndWidth(double nrView, int imageWidth)
00328     {
00329         CmdOptions& options = CmdOptions::GetInstance();
00330         int reqWndWidth = options.GetInt("wndWidth");
00331         if (reqWndWidth != -1)
00332             return reqWndWidth;
00333         double viewScale = options.GetDouble("viewScale");
00334         return nrView * (viewScale * imageWidth + 5);
00335     }
00336 
00337     int
00338     SuggestWndHeight(double nrView, int imageHeight)
00339     {
00340         CmdOptions& options = CmdOptions::GetInstance();
00341         int reqWndHeight = options.GetInt("wndHeight");
00342         if (reqWndHeight != -1)
00343             return reqWndHeight;
00344         double viewScale = options.GetDouble("viewScale");
00345         return nrView * (viewScale * imageHeight + 5);
00346     }
00347 
00348     int
00349     SuggestWidth(std::string str)
00350     {
00351         if (! mOglWnd)
00352         {
00353             std::cout << "suggestWndWidth : no mOglwnd" << std::endl;
00354             return str.size() * 10;
00355         }
00356         OGLWND* top = oglSys.GetTopOGLWND(mOglWnd);
00357         if (! top->hFont)
00358         {
00359             std::cout << "suggestWidth : no hFont" << std::endl;
00360             return str.size() * 10;
00361         }
00362         int w;
00363         int h;
00364         oglSys.AbstFontTextSize(top, top->hFont, (char *) str.c_str(), &w, &h);
00365         return w;
00366     }
00367 
00368     // OGL user funcs
00369 
00370     virtual void
00371     InitFunc()
00372     {
00373         OglGui::Window::InitFunc();
00374 
00375         mInfoBox = oglSys.InfoBox(2, 2, 150, 18, 1);
00376         mInfoBox->transparent = 0.4f;
00377         oglSys.InfoBoxSolidCol(mInfoBox, oglSys.RGB2Color(255, 255, 255));
00378         oglSys.InfoBoxAddStr(mInfoBox, "This is a test", 2, 5, oglBLACK);
00379     }
00380 
00381     virtual void
00382     ExitFunc()
00383     {
00384         if (mInfoBox)
00385             oglSys.InfoBoxDestroy(mInfoBox);
00386         mInfoBox = 0;
00387         OglGui::Window::ExitFunc();
00388     }
00389 
00390     virtual void
00391     DisplayFunc()
00392     {
00393         if (mDoStatus)
00394             oglSys.SetStatusStr(mOglWnd, mStatusBuf);
00395 
00396         OglGui::Window::DisplayFunc();
00397 
00398         if (mInfoBoxActive && mInfoBoxDraw)
00399             oglSys.InfoBoxDraw(mOglWnd, mInfoBox);
00400     }
00401 
00402     virtual void
00403     MouseFunc(int msg, int but, int state, int x, int y)
00404     {
00405         if (mInfoBox) 
00406         {
00407             mInfoBox->x = x + 20;
00408             mInfoBox->y = y - 25;
00409             mInfoBoxDraw = false;
00410         }
00411        
00412         //if (msg == oglMouseUp)
00413         //    mPointerDrag = false;
00414 
00415         if (((msg==oglMouseDown && (but==oglLeftButton)) ||
00416              (state & oglLeftButton) && msg==oglMouseMove))
00417         {
00418             // RvB: Deze (FindView)is in 3D erg duur, vooral omdat view3DSys ook
00419             // nog eens op zoek gaat. Mischien moet in OGL C de OnViewer functies met
00420             // een OnMouse worden uitgebreid. Nadeel wel dat in 3D alleen goed
00421             // de mouse positie in image coordinaten om te zetten is wanneer de
00422             // viewer flat is en niet geroteerd is.
00423             /* todo : save InfoBox part
00424             mPointerView = FindView(x, y);
00425             if (mPointerView)
00426             {
00427                 Array2dVec3UInt8* disp;
00428                 mPointerView->WndToIm(x, y, mPointerX, mPointerY);
00429                 if( mInfoBoxActive && (disp = mPointerView->GetDispImage()) )
00430                 {
00431                     UInt8* ptr = disp->CPB(mPointerX, mPointerY);
00432                     std::ostrstream infoStream;
00433                     infoStream << mPointerX << "," << mPointerY << " = " 
00434                                << "(" << ptr[0] << "," << ptr[1] << ","
00435                                << ptr[2] << ")" << std::ends;
00436                     SetInfoBoxString(infoStream.str());
00437                     infoStream.freeze(false);
00438                     mInfoBoxDraw = true;
00439                 }
00440                 if ((msg == oglMouseDown) && (but == oglLeftButton))
00441                 {
00442                     mPointerDrag = true;
00443                     mPointerDragX = mPointerX;
00444                     mPointerDragY = mPointerY;
00445                 }
00446             }
00447             */
00448             //if (mListener)
00449             //    mListener->PointerEvent(this, mListenerData);
00450         }
00451         OglGui::Window::MouseFunc( msg, but, state, x, y );
00452     }
00453 
00454     void
00455     SetStatusStr(char* buf)
00456     {
00457         strcpy(mStatusBuf, buf);
00458         mDoStatus = true;
00459     }
00460 
00461     void
00462     ActivateInfoBox(bool doit)
00463     {
00464         mInfoBoxActive = doit;
00465     }
00466 
00467     void
00468     SetInfoBoxString(char* str)
00469     {
00470         oglSys.InfoBoxChangeStr(mInfoBox, 0, str, RETAIN, RETAIN, RETAIN);
00471     }
00472 
00473     // In OxWnd renamed to SetRoundness
00474     /*
00475     void
00476     SetBorderRoundness(float r0, float r1, float r2, float r3)
00477     {
00478         if( r0 != FRETAIN )
00479             mOglWnd->r0 = r0;
00480         if( r1 != FRETAIN )
00481             mOglWnd->r1 = r1;
00482         if( r2 != FRETAIN )
00483             mOglWnd->r2 = r2;
00484         if( r3 != FRETAIN )
00485             mOglWnd->r3 = r3;
00486     }
00487     */
00488 
00489     // In OxWnd renamed to GetRoundness
00490     /*
00491     void
00492     GetBorderRoundness(float& r0, float& r1, float& r2, float& r3)
00493     {
00494         r0 = mOglWnd->r0;
00495         r1 = mOglWnd->r1;
00496         r2 = mOglWnd->r2;
00497         r3 = mOglWnd->r3;
00498     }
00499     */
00500 
00501 protected:
00502 
00503     // Not used in OglGui::Window
00504     //unsigned int mHighlightColor;
00505     //unsigned int mNormalColor;
00506 
00507     bool mDoStatus;
00508     char mStatusBuf[256];
00509 
00510     INFOBOX* mInfoBox;
00511     bool     mInfoBoxDraw;
00512     bool     mInfoBoxActive;
00513 
00514 private:
00515 
00516     void
00517     Init()
00518     {
00519         if (!mOglWnd)
00520         {
00521             std::cout << "CreateOGLWND/ViewPort failed" << std::endl;
00522             return;
00523         }
00524 
00525         mRunTimeType |= 4;
00526 
00527         mViewTiler.ResetWnd(mOglWnd->width, mOglWnd->height);
00528         mMakeViewWithRect = false;
00529         mAllowRepositionViewers = true;
00530 
00531         //todo: add skinning
00532         //mNormalColor = 0xffd2c799;
00533         //mHighlightColor = 0xffddddaa;
00534 //        mOglWnd->bgCol = mNormalColor;
00535 
00536         
00537         mDoStatus = false;
00538         mInfoBoxActive = true;
00539         mInfoBoxDraw = false;
00540         mInfoBox = 0;
00541     }
00542 
00543     bool                       mMakeViewWithRect;
00544     std::vector<View*>         mOglView;
00545     Core::Geometry::RectangleTiler mViewTiler;
00546     bool                       mAllowRepositionViewers;
00547 
00548     int mFirstImSize; // for normalization of 3d viewers size, based on first view
00549 
00550 };
00551 
00552 } // namespace Visualization
00553 } // namespace Impala
00554 
00555 #endif

Generated on Fri Mar 19 09:31:41 2010 for ImpalaSrc by  doxygen 1.5.1