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