00001 #ifndef Impala_Visualization_DirImViewer3D_h
00002 #define Impala_Visualization_DirImViewer3D_h
00003
00004 #include "OglGui/TitledWindow.h"
00005 #include "OglGui/Camera3DControlEx.h"
00006
00007 #include "OglGui/OglImageCache.h"
00008 #include "OglGui/Window.h"
00009
00010 #include "Util/DirImageFileNames.h"
00011 #include "Core/Array/ReadFile.h"
00012 #include "Visualization/RgbOglImage.h"
00013
00014 #include "Core/Stream/RgbDataSrcFactory.h"
00015
00016 namespace Impala {
00017 namespace Visualization {
00018
00019 class DirImViewer3D : public OglGui::Window
00020 {
00021 typedef std::vector<String*> StringPtrVector;
00022 typedef OglGui::OglImageCache OglImageCache;
00023 typedef OglGui::Camera3DControlEx Camera3DControlEx;
00024 typedef OglGui::TitledWindow TitledWindow;
00025
00026 typedef Core::Array::Array2dVec3UInt8 Array2dVec3UInt8;
00027 typedef Core::Stream::RgbDataSrcFactory RgbDataSrcFactory;
00028 typedef Core::Stream::RgbDataSrc RgbDataSrc;
00029
00030
00031 public:
00032 DirImViewer3D(int x, int y, int w, int h,
00033 int nrToShow=56, int cacheSize=112, bool camControl=true) :
00034 OglGui::Window(x, y, w, h, false)
00035 {
00036 Init(w, h, cacheSize, nrToShow, camControl);
00037 }
00038
00039 DirImViewer3D(OglGui::Window* parent, int w, int h,
00040 int nrToShow=56, int cacheSize=112, bool camControl=true) :
00041 OglGui::Window(parent, w, h, false)
00042 {
00043 Init(w, h, cacheSize, nrToShow, camControl);
00044 }
00045
00046 DirImViewer3D(OglGui::Window* parent, int x, int y, int w, int h,
00047 int nrToShow=56, int cacheSize=112, bool camControl=true) :
00048 OglGui::Window(parent, x, y, w, h, false)
00049 {
00050 Init(w, h, cacheSize, nrToShow, camControl);
00051 }
00052
00053 void OpenDir(CString dirName)
00054 {
00055 Clear();
00056 mDirName = dirName;
00057 String fExt = FileNameExt(dirName);
00058
00059 if ((fExt == "raw") || (fExt == "mpg") || (fExt == "mpeg"))
00060 {
00061 #ifdef REPOSITORY_USED
00062 OpenSrc(Persistency::RgbDataSrcLocator("file:", "abs", dirName));
00063 #else
00064 RgbDataSrcFactory& f = RgbDataSrcFactory::Instance();
00065 mRgbDataSrc = f.Construct(dirName, fExt);
00066 #endif //REPOSITORY_USED
00067 }
00068 else
00069 Util::DirImageFileNames(dirName, mFileNames);
00070 InitViews();
00071 }
00072
00073 #if 0
00074 void OpenDir(strconst dirName)
00075 {
00076 Clear();
00077 mDirName = dirName;
00078 std::string ext = FileNameExt(dirName, true);
00079 if (ext == "raw")
00080 mRgbDataSrc = new RgbDataSrcRaw(dirName);
00081 else
00082 Util::DirImageFileNames(dirName, mFileNames);
00083 InitViews();
00084 }
00085 #endif // 0
00086
00087 #if defined(REPOSITORY_USED)
00088 void OpenSrc(const Persistency::RgbDataSrcLocator& loc)
00089 {
00090 mDirName = loc.GetName();
00091 RgbDataSrcFactory& f = RgbDataSrcFactory::Instance();
00092 SetRgbDataSrc(f.Construct(loc, ""));
00093 }
00094 #endif
00095
00096 void SetRgbDataSrc(RgbDataSrc* rgbDataSrc)
00097 {
00098 Clear();
00099 mRgbDataSrc = rgbDataSrc;
00100 InitViews();
00101 }
00102
00103 void NrToShow(int nrToShow)
00104 {
00105 if (nrToShow < 8) nrToShow = 8;
00106 if (nrToShow > 108) nrToShow = 108;
00107 mNrToShow = nrToShow;
00108 }
00109
00110 void SetOglImageCache(OglImageCache* oglImCache, bool delOldCache=false)
00111 {
00112 if (oglImCache == mOglImageCache)
00113 return;
00114 if (delOldCache && mOglImageCache)
00115 delete mOglImageCache;
00116 mOglImageCache = oglImCache;
00117 }
00118
00119 int NrOfFiles()
00120 {
00121 if (mRgbDataSrc)
00122 return mRgbDataSrc->LastFrame();
00123 return mFileNames.size();
00124 }
00125
00126 void Clear()
00127 {
00128 ClearFiles();
00129 ClearViews();
00130 }
00131
00132 void ClearFiles()
00133 {
00134 if (mRgbDataSrc)
00135 {
00136 delete mRgbDataSrc;
00137 mRgbDataSrc = 0;
00138 }
00139 unsigned int i;
00140 for (i=0 ; i<mFileNames.size() ; i++)
00141 delete mFileNames[i];
00142 mFileNames.clear();
00143 }
00144
00145 void ClearViews()
00146 {
00147 view3DSys.DeleteViews(mOglWnd);
00148 mViews.clear();
00149 }
00150
00151 void InitViews()
00152 {
00153 ClearViews();
00154 int cnt = 0;
00155 int d = 0;
00156 int nrFiles = NrOfFiles();
00157 while (cnt < nrFiles)
00158 {
00159 d++;
00160 for (int y=1; y>-2; y--)
00161 for (int x=-1; x<2; x++)
00162 {
00163 if (!x && !y || cnt >= nrFiles)
00164 continue;
00165 OGLVIEW3D* v = view3DSys.View3D(mOglWnd, 0, 3*x, 3*y,
00166 -5*d - (cnt++%8)*0.7143,
00167 1, 1, 1);
00168 mViews.push_back(v);
00169 view3DSys.SetTags(v, FlexViewTags & ~deletableTag);
00170 SetUpPrintingID(v, cnt-1);
00171
00172 }
00173 }
00174 if (mCam3DControlEx)
00175 mCam3DControlEx->MinMaxZ(-5*d-12, 12);
00176 }
00177
00178 void InitDisplayFunc()
00179 {
00180 Window::InitDisplayFunc();
00181
00182 float camZ;
00183 view3DSys.GetCamera(mOglWnd, 0, 0, &camZ, 0, 0, 0);
00184
00185 int sz = mViews.size();
00186 for (int i=0; i<sz; i++)
00187 {
00188 float dist = camZ - mViews[i]->z;
00189 bool vis = dist > 1 && dist <= 1 + (mNrToShow*5)/8;
00190 HandleVisibility(i, vis);
00191 }
00192 }
00193
00194 virtual void InitFunc()
00195 {
00196 Window::InitFunc();
00197 if (sTxtFontBase)
00198 return;
00199
00200 void* font;
00201
00202 #ifdef OGL_USING_GLUT
00203 sTxtFontBase = oglSys.Create3DFont( mOglWnd, font, 1, 0.6f, NULL );
00204 #else
00205 font = (void*) CreateFont(
00206 -12, 0, 0, 0, FW_NORMAL, 0, 0, 0,
00207 ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
00208 ANTIALIASED_QUALITY, VARIABLE_PITCH | FF_DONTCARE, "Verdana" );
00209
00210
00211 sTxtFontBase = oglSys.Create3DFont( mOglWnd, font, 1, 0.6f, NULL );
00212 DeleteObject( (HFONT) font );
00213 #endif
00214 }
00215
00216 private:
00217 OGLIMAGE* ReadImageIdx( int imIdx )
00218 {
00219 Array2dVec3UInt8* ar = 0;
00220 if (mRgbDataSrc)
00221 {
00222 mRgbDataSrc->GotoFrame(imIdx);
00223 int imW = mRgbDataSrc->FrameWidth();
00224 int imH = mRgbDataSrc->FrameHeight();
00225 ar = new Array2dVec3UInt8(imW, imH, 0, 0, 0, false);
00226 memcpy((void*)ar->CPB(0,0), mRgbDataSrc->DataPtr(), imW*imH*3);
00227 }
00228 else
00229 {
00230 String fName = mDirName + "/" + *mFileNames[imIdx];
00231 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00232 Core::Array::ReadFile(ar, fName, &Util::Database::GetInstance());
00233 #else // REPOSITORY_USED
00234
00235 Persistency::FileLocator loc(fName);
00236 typedef Persistency::RepositoryInFileSystem FS;
00237 Persistency::File f = FS::GetInstance().GetFile(loc, false, false);
00238 Core::Array::ReadFile(ar, f);
00239 #endif // REPOSITORY_USED
00240 if (ar == 0)
00241 ILOG_WARN("Could not read file: " << fName)
00242 }
00243 return RgbOglImage::OglImage(ar);
00244 }
00245
00246 OGLIMAGE* LoadImageIdx(int id)
00247 {
00248 if (!mOglImageCache)
00249 return ReadImageIdx(id);
00250
00251 OGLIMAGE* im;
00252 if (! (im = mOglImageCache->GetImage(id)) )
00253 {
00254 if (!(im = ReadImageIdx(id)))
00255 return 0;
00256 mOglImageCache->AddNoScalingTextured(im, id);
00257
00258
00259 }
00260 return im;
00261 }
00262
00263 void HandleVisibility(int id, bool visible)
00264 {
00265 OGLIMAGE* im = 0;
00266 OGLVIEW3D* oglV3D = mViews[id];
00267
00268 view3DSys.SetTagsTo(oglV3D, visibleTag, visible);
00269 if (!visible)
00270 view3DSys.SetImage(oglV3D,0);
00271 else if (!(im = oglV3D->im))
00272 {
00273 im = LoadImageIdx(id);
00274 view3DSys.SetImage(oglV3D, im);
00275 ReleaseOglImage(im);
00276 }
00277
00278 if (!im) return;
00279
00280 if (mDoAspectRatio)
00281 {
00282
00283 float div = 100 * (1 + (im->w > im->h ? im->w : im->h)/200);
00284 oglV3D->w = im->w/div;
00285 oglV3D->h = im->h/div;
00286 }
00287
00288
00289 float tW = im->noTexScaling ? im->texW : im->w;
00290 float tH = im->noTexScaling ? im->texH : im->h;
00291 float zX = (oglV3D->w)*(tW/im->w);
00292 float zY = (oglV3D->h)*(tH/im->h);
00293 view3DSys.SetZoom(oglV3D, zX, zY);
00294 }
00295
00296 void FancySettings(OGLVIEW3D* v)
00297 {
00298
00299 view3DSys.SetObject3D(v, 1+ABSRND(8));
00300 view3DSys.SetRotationSpeed(v, ABSRND(30)/300.0, ABSRND(30)/300.0, 1);
00301 SetAlwaysDraw(true);
00302 }
00303
00304 static void MyOnDrawView(OGLVIEW3D* v)
00305 {
00306 StringPtrVector* fileNames = (StringPtrVector*) v->UserData2;
00307 int idx = (int) (long long) v->UserData1;
00308 char buf[200];
00309
00310 glPushMatrix();
00311 glColor3f( 0.0f, 1.0f, 0.0f );
00312
00313 if (fileNames)
00314 {
00315 sprintf(buf, "%s", (*fileNames)[idx]->c_str());
00316 oglSys.Printf3D(sTxtFontBase, -v->w/2.f-0.1, -v->h/2.f - 0.15, 0.1f,
00317 0.2f, 0.2f, 0.05f, buf);
00318 }
00319 else
00320 {
00321 sprintf(buf, "%d", idx);
00322 oglSys.Printf3D(sTxtFontBase, -0.1, -v->h/2.f - 0.15, 0.1f,
00323 0.3f, 0.3f, 0.1f, buf);
00324 }
00325 glPopMatrix();
00326 }
00327
00328 void SetUpPrintingID(OGLVIEW3D* v, int cnt)
00329 {
00330 v->UserData1 = (void *) cnt;
00331 v->UserData2 = (void *) (mRgbDataSrc ? 0 : &mFileNames);
00332 v->OnDrawView = MyOnDrawView;
00333 }
00334
00335 void SetUpCameraControl3D()
00336 {
00337 int prefW, prefH;
00338 Camera3DControlEx::PreferredSize(prefW,prefH);
00339
00340 TitledWindow* tWnd =
00341 new TitledWindow(this,10,10, prefW+8, prefH+32, "Camera3D Control");
00342
00343 mCam3DControlEx = new Camera3DControlEx(tWnd, 0, 0, 10, 10, this);
00344 tWnd->SetContentPane(mCam3DControlEx);
00345 mCam3DControlEx->SetBorderType(0);
00346 mCam3DControlEx->MinMaxX(-6, 6);
00347 mCam3DControlEx->MinMaxY(-6, 6);
00348 mCam3DControlEx->MinMaxZ(-12, 12);
00349 }
00350
00351 void Init(int w, int h, int cacheSize, int nrToShow, bool camControl)
00352 {
00353 NrToShow(nrToShow);
00354 mDoAspectRatio = true;
00355 mRgbDataSrc = 0;
00356
00357 mOglImageCache = (cacheSize>0) ? (new OglImageCache(cacheSize)) : 0;
00358
00359 oglSys.SetAllowCameraMove(mOglWnd, CamMove_Keys | CamMove_Mouse);
00360 oglSys.AllowPicking(mOglWnd, 1, 1);
00361
00362 mCam3DControlEx = 0;
00363 if (camControl)
00364 SetUpCameraControl3D();
00365 }
00366
00367 String mDirName;
00368 StringPtrVector mFileNames;
00369 RgbDataSrc* mRgbDataSrc;
00370
00371 std::vector<OGLVIEW3D*> mViews;
00372 OglImageCache* mOglImageCache;
00373
00374 Camera3DControlEx* mCam3DControlEx;
00375
00376 bool mDoAspectRatio;
00377 int mNrToShow;
00378
00379 static int sTxtFontBase;
00380
00381 ILOG_VAR_DEC;
00382 };
00383
00384 int DirImViewer3D::sTxtFontBase = 0;
00385
00386 ILOG_VAR_INIT(DirImViewer3D, Visualization);
00387
00388 }
00389 }
00390
00391 #endif // Impala_Visualization_DirImViewer3D_h