00001
00002
00003
00004
00005
00006 #ifndef Impala_Visualization_TimeLineViewStrip_h
00007 #define Impala_Visualization_TimeLineViewStrip_h
00008
00009 #ifndef OglGui_WindowTimeLine_h
00010 #include "OglGui/WindowTimeLine.h"
00011 #endif
00012
00013 #ifdef REPOSITORY_USED
00014
00015 #include "Core/Stream/RgbDataSrcFactory.h"
00016 #endif
00017
00018 #include "OglGui/FramesPerSecond.h"
00019 #include "Core/Array/Arrays.h"
00020 #include "Core/Stream/RgbDataSrcRaw.h"
00021 #include "Visualization/RgbOglImage.h"
00022
00023 namespace Impala {
00024 namespace Visualization {
00025
00026 class TimeLineViewStrip : public OglGui::WindowTimeLine
00027 {
00028 public:
00029 typedef OglGui::Window Window;
00030 typedef OglGui::WindowTimeLine WindowTimeLine;
00031 typedef Core::Stream::RgbDataSrc RgbDataSrc;
00032 typedef Core::Stream::RgbDataSrcRaw RgbDataSrcRaw;
00033 #ifdef REPOSITORY_USED
00034 typedef Core::Stream::RgbDataSrcFactory RgbDataSrcFactory;
00035 #endif
00036 typedef Core::Array::Array2dVec3UInt8 Array2dVec3UInt8;
00037
00038 TimeLineViewStrip(int x, int y, int w, int h, int nViews) :
00039 WindowTimeLine(x,y,w,h)
00040 {
00041 Init(w,h,nViews);
00042 }
00043
00044 TimeLineViewStrip(Window* parent, int w, int h, int nViews) :
00045 WindowTimeLine(parent,w,h)
00046 {
00047 Init(w,h,nViews);
00048 }
00049
00050 TimeLineViewStrip(Window* parent, int x, int y, int w, int h, int nViews) :
00051 WindowTimeLine(parent,x,y,w,h)
00052 {
00053 Init(w,h,nViews);
00054 }
00055
00056 void FrameError(int off) { mFrameError = off; }
00057 int FrameError() { return mFrameError; }
00058
00059 void ViewStripFrameStep(int step) { mViewFrameStep = step; }
00060 int ViewStripFrameStep() { return mViewFrameStep; }
00061 void ViewStripMinViewWidth(int w) { mMinViewW = w; }
00062 int ViewStripMinViewWidth() { return mMinViewW; }
00063 void ViewStripY(int y) { mViewStripY = y; }
00064 int ViewStripY() { return mViewStripY; }
00065 void CurrentViewOnTop(int mode) { mCurrentViewOnTop = mode % 3; }
00066 int CurrentViewOnTop() { return mCurrentViewOnTop; }
00067 RgbDataSrc* ViewStripRgbSrc() { return mRgbDataSrc; }
00068
00069 void ViewStripShow(bool m)
00070 {
00071 if (!(mShow = m))
00072 ViewStripClearViews();
00073 }
00074 bool ViewStripShow()
00075 {
00076 return mShow;
00077 }
00078
00079 void ViewStripZoomToShowViews(int mode=1)
00080 {
00081 int curPix = Unit2Pixel(mCurrent);
00082 int nrOfFrames = mRgbDataSrc->LastFrame();
00083 int docX = DocX();
00084 if (mode==0 || mode==1)
00085 DocW((nrOfFrames*(mMinViewW+(mode?0:-1)))/mViewFrameStep);
00086 if (mode==2)
00087 DocW(mMaxDocWidth);
00088 DocX(docX+(curPix-Unit2Pixel(mCurrent)));
00089 }
00090
00091 void ViewStripRgbDataSrc(RgbDataSrc* src)
00092 {
00093 if (mRgbDataSrc)
00094 delete mRgbDataSrc;
00095 if (!(mRgbDataSrc = src))
00096 {
00097 ViewStripClearViews();
00098 return;
00099 }
00100
00101 mImW = mRgbDataSrc->FrameWidth();
00102 mImH = mRgbDataSrc->FrameHeight();
00103 int nrFrames = mRgbDataSrc->LastFrame();
00104 SetSpan(nrFrames * 40);
00105 SetMinMaxDocWidth(200, nrFrames * mImW);
00106 }
00107
00108 bool ViewStripOpenRawFile(std::string fName, bool useMemory=false)
00109 {
00110 if (!FileExists(fName))
00111 {
00112 ILOG_ERROR("Reading: [" + fName + "] !! does not exist");
00113 return false;
00114 }
00115 if (mRgbDataSrc)
00116 delete mRgbDataSrc;
00117 mRgbDataSrc = 0;
00118
00119 #ifndef REPOSITORY_USED
00120 RgbDataSrcRaw* src = new RgbDataSrcRaw(fName,useMemory);
00121 #else
00122 CmdOptions& options = CmdOptions::GetInstance();
00123 String typeStr = options.GetString("TimeLineViewStripRgbSrc");
00124 if (typeStr.empty())
00125 typeStr = "raw";
00126 Persistency::RgbDataSrcLocator loc("dot", fName, options);
00127 RgbDataSrcFactory& factory = RgbDataSrcFactory::Instance();
00128 RgbDataSrc* src = factory.Construct(loc, typeStr);
00129 #endif
00130 if (!src->Valid())
00131 {
00132 ILOG_ERROR("Reading [" + fName + "] failed");
00133 delete src;
00134 return false;
00135 }
00136 ViewStripRgbDataSrc(src);
00137 return true;
00138 }
00139
00140 int FirstVisibleFrame()
00141 {
00142 int startFrame = Pixel2Unit(0) / 40;
00143 if (startFrame < 0)
00144 startFrame = 0;
00145 return startFrame;
00146 }
00147
00148 int LastVisibleFrame()
00149 {
00150 int endFrame = Pixel2Unit(W()) / 40 + 1;
00151 int lastSpanFrame = mSpan/40;
00152 int lastFrame = mRgbDataSrc ? mRgbDataSrc->LastFrame() : lastSpanFrame;
00153 if (endFrame > lastFrame)
00154 endFrame = lastFrame;
00155 if (endFrame > lastSpanFrame)
00156 endFrame = lastSpanFrame;
00157 return endFrame;
00158 }
00159
00160 int ViewStripFindIndex(int frameNr, int start=0)
00161 {
00162 if (start<0)
00163 start = 0;
00164 for (int i=start; i<mViewers.size(); i++)
00165 {
00166 if (frameNr == (long long) mViewers[i]->UserData1)
00167 return i;
00168 }
00169 return -1;
00170 }
00171
00172 int ViewStripFindIndexBwd(int frameNr, int start=10000)
00173 {
00174 if (start > mViewers.size()-1)
00175 start = mViewers.size()-1;
00176 for (int i=start; i>=0; i--)
00177 {
00178 if (frameNr == (long long) mViewers[i]->UserData1)
00179 return i;
00180 }
00181 return -1;
00182 }
00183
00184 void ViewStripClearView(int i)
00185 {
00186 if (i<0 || i>=mViewers.size())
00187 return;
00188 viewSys.SetImage(mViewers[i], 0);
00189 viewSys.ClearTags(mViewers[i], visibleTag);
00190 mViewers[i]->UserData1 = (void*)-1;
00191 }
00192
00193 void ViewStripClearViews(int start=0, int end=10000)
00194 {
00195 if (start < 0)
00196 start = 0;
00197 if (end >= mViewers.size())
00198 end = mViewers.size();
00199
00200 for (int i=start; i<end; i++)
00201 ViewStripClearView(i);
00202 }
00203
00204 void ViewStripCopyImageFromView(int dstIdx, int srcIdx, int frameNr)
00205 {
00206 viewSys.SetImage(mViewers[dstIdx], mViewers[srcIdx]->im);
00207 mViewers[dstIdx]->UserData1 = (void *) frameNr;
00208 }
00209
00210 void ViewStripCreateImage(int dstIdx, int frameNr)
00211 {
00212
00213 int imW = mRgbDataSrc->FrameWidth();
00214 int imH = mRgbDataSrc->FrameHeight();
00215 Array2dVec3UInt8* ar =
00216 new Array2dVec3UInt8(imW, imH, 0, 0, 0, false);
00217 mRgbDataSrc->GotoFrame(frameNr);
00218 memcpy( (void *) ar->CPB(0, 0), mRgbDataSrc->DataPtr(), imW*imH*3 );
00219 OGLIMAGE* oglIm = RgbOglImage::OglImage(ar);
00220 viewSys.SetImage(mViewers[dstIdx],oglIm);
00221 mViewers[dstIdx]->UserData1 = (void *) frameNr;
00222 ReleaseOglImage(oglIm);
00223 }
00224
00225 bool ViewStripNewPositionNeedsIm(int idx, int frameNr, int space)
00226 {
00227 int err = mBothMpg ? 0 : mFrameError;
00228 int x = Unit2Pixel((frameNr-err)*40);
00229 int w = (space+1) > mImW ? mImW : space+1;
00230 viewSys.SetDimensions(mViewers[idx], x-w/2, mViewStripY, w, mImH);
00231 viewSys.SetTags(mViewers[idx], visibleTag);
00232 return ((long long) mViewers[idx]->UserData1) != frameNr;
00233 }
00234
00235 void ShowCurrentViewOnTop()
00236 {
00237 int v = mViewers.size()-1;
00238 int f = Current()/40;
00239 int idx;
00240 if (ViewStripNewPositionNeedsIm(v, f, mImW))
00241 {
00242 if ((idx = ViewStripFindIndex(f,0))!=-1)
00243 ViewStripCopyImageFromView(v,idx,f);
00244 else
00245 ViewStripCreateImage(v,f);
00246 }
00247 }
00248
00249 virtual void InitDisplayFunc()
00250 {
00251 WindowTimeLine::InitDisplayFunc();
00252 if (!mShow || !mRgbDataSrc)
00253 return;
00254
00255 int space = Unit2Pixel(mViewFrameStep*40)-Unit2Pixel(0);
00256 if (space < mMinViewW && mOldSpace >= mMinViewW)
00257 ViewStripClearViews(0, mViewers.size());
00258
00259 if ((mOldSpace = space) < mMinViewW)
00260 {
00261 if (mCurrentViewOnTop==2)
00262 ShowCurrentViewOnTop();
00263 else
00264 ViewStripClearView(mViewers.size()-1);
00265 mOldStartFrame = -1;
00266 return;
00267 }
00268
00269 int err = mBothMpg ? 1 : mFrameError;
00270
00271 int startFrame = FirstVisibleFrame()+err-mViewFrameStep;
00272 if (startFrame < 0)
00273 startFrame = 0;
00274 int endFrame = LastVisibleFrame()+err+mViewFrameStep;
00275 if (endFrame > mRgbDataSrc->LastFrame())
00276 endFrame = mRgbDataSrc->LastFrame();
00277 int range = endFrame-startFrame-1;
00278
00279 if (range/mViewFrameStep >= mViewers.size())
00280 range = (mViewers.size()*mViewFrameStep)-1;
00281
00282 int i, idx, frameNr, vCnt;
00283 if (mOldStartFrame <= startFrame)
00284 {
00285 for (i=0, vCnt=-1; i <= range; i++)
00286 {
00287 if ((frameNr = i+startFrame) % mViewFrameStep)
00288 continue;
00289 if (!ViewStripNewPositionNeedsIm(++vCnt, frameNr, space))
00290 continue;
00291 if ((idx = ViewStripFindIndex(frameNr,vCnt))!=-1)
00292 ViewStripCopyImageFromView(vCnt,idx,frameNr);
00293 else
00294 ViewStripCreateImage(vCnt,frameNr);
00295 }
00296 ViewStripClearViews(vCnt+1, mViewers.size());
00297 }
00298 else
00299 {
00300 for (vCnt=0,i=range; i>=0; i--)
00301 if (!((i+startFrame) % mViewFrameStep))
00302 vCnt++;
00303 if (!vCnt) return;
00304 int lastV = vCnt;
00305 for (i=range; i>=0; i--)
00306 {
00307 if ((frameNr = i+startFrame) % mViewFrameStep)
00308 continue;
00309 if (!ViewStripNewPositionNeedsIm(--vCnt, frameNr, space))
00310 continue;
00311 if ((idx = ViewStripFindIndexBwd(frameNr,vCnt))!=-1)
00312 ViewStripCopyImageFromView(vCnt,idx,frameNr);
00313 else
00314 ViewStripCreateImage(vCnt,frameNr);
00315 }
00316 ViewStripClearViews(lastV, mViewers.size());
00317 }
00318
00319 if (mCurrentViewOnTop)
00320 ShowCurrentViewOnTop();
00321
00322 mOldStartFrame = startFrame;
00323 }
00324
00325 virtual void KeyboardFunc(int c, int state)
00326 {
00327 WindowTimeLine::KeyboardFunc(c,state);
00328 if (c=='v')
00329 mCurrentViewOnTop = (mCurrentViewOnTop+1) % 3;
00330 }
00331
00332 bool mBothMpg;
00333
00334 protected:
00335 int mFrameError;
00336
00337 private:
00338
00339 void Init(int w, int h, int nViews)
00340 {
00341 mRgbDataSrc = 0;
00342 for (int i=0; i<nViews; i++)
00343 {
00344 OGLVIEW* view = viewSys.View2D(mOglWnd,0,0,0,176,144);
00345 viewSys.SetTags(view, FlexViewTags);
00346 viewSys.ClearTags(view, visibleTag|showBorderTag|selectableTag);
00347 view->UserData1 = (void *) -1;
00348 mViewers.push_back(view);
00349 mBothMpg = false;
00350 }
00351 mOldSpace = 0;
00352 mOldStartFrame = -1;
00353 mMinViewW = 6;
00354 mViewFrameStep = 1;
00355 mViewStripY = 24;
00356 mFrameError = 0;
00357 mShow = true;
00358 mCurrentViewOnTop = 0;
00359 }
00360
00361 RgbDataSrc* mRgbDataSrc;
00362 std::vector<OGLVIEW*> mViewers;
00363
00364 bool mShow;
00365 int mCurrentViewOnTop;
00366
00367 int mViewStripY;
00368 int mOldSpace;
00369 int mOldStartFrame;
00370
00371 int mViewFrameStep;
00372 int mMinViewW;
00373
00374 int mImW;
00375 int mImH;
00376
00377 bool mPlaying;
00378 int mIncr;
00379
00380 ILOG_VAR_DEC;
00381 };
00382
00383 ILOG_VAR_INIT(TimeLineViewStrip, Visualization);
00384
00385 }
00386 }
00387 #endif