00001
00002 #ifndef Impala_Visualization_RotorBrowser_RotorViewCache_h
00003 #define Impala_Visualization_RotorBrowser_RotorViewCache_h
00004
00005 #include <list>
00006
00007 #include "RotorView.h"
00008 #include "Core/Trec/ThreadSet.h"
00009 #include "Core/Table/AnnotationTable.h"
00010
00011
00012 namespace Impala {
00013 namespace Visualization {
00014 namespace RotorBrowser {
00015
00016
00017
00018
00019
00020 class RotorViewCache : public RotorViewCacheResolver
00021 {
00022 public:
00023 typedef std::list<RotorView*> RotorViewList;
00024 typedef std::list<RotorDoDView*> RotorDoDViewList;
00025 typedef Core::Trec::Thread Thread;
00026 typedef Core::Trec::ThreadSet ThreadSet;
00027 typedef Impala::Core::Table::AnnotationTable AnnotationTable;
00028
00029
00030
00031 RotorViewCache(OglGui::Window *window, ThreadSet *threadset,
00032 RotorViewListener *listenerTarget)
00033 {
00034 mWnd = window;
00035 mThreadSet = threadset;
00036 mListenerTarget = listenerTarget;
00037 mHits = 0;
00038 mMisses = 0;
00039 mRHits = 0;
00040 mRMisses = 0;
00041
00042 mShotViewed = new AnnotationTable();
00043 }
00044
00045 static RotorView* OGLVIEW3DToRotorView(OGLVIEW3D* v)
00046 {
00047 if (!v) return 0;
00048 return (RotorView*)view3DSys.GetUserData(v, RotorView::VIEW_ROTORVIEW);
00049 }
00050
00051 void DoHide(RotorViewList views)
00052 {
00053 RotorViewList::const_iterator i;
00054 for (i=views.begin(); i != views.end(); i++)
00055 (*i)->Hide();
00056 }
00057
00058 void DoCleanup(RotorViewList views)
00059 {
00060 RotorViewList::const_iterator i;
00061 for (i=views.begin(); i != views.end(); i++)
00062 {
00063 if ((*i)->IsUnused())
00064 {
00065 if (mUnusedViewCache.size() < 200)
00066 mUnusedViewCache.push_back((*i)->RecycleView());
00067 delete *i;
00068 }
00069 }
00070 }
00071
00072
00073 RotorViewList GetViews()
00074 {
00075 RotorViewList views;
00076 RotorView* v;
00077
00078 for(LIST* o=mWnd->GetOGLWND()->objectList ; o ; o=o->next)
00079 {
00080 if (v = OGLVIEW3DToRotorView((OGLVIEW3D *) o->info))
00081 views.push_back(v);
00082 }
00083 return views;
00084 }
00085
00086
00087
00088 RotorViewList GetViews(String name)
00089 {
00090 RotorViewList views;
00091
00092 for(LIST* o=mWnd->GetOGLWND()->objectList ; o ; o=o->next)
00093 {
00094 RotorView* v = OGLVIEW3DToRotorView((OGLVIEW3D*) o->info);
00095 if (v && v->GetName() == name)
00096 views.push_back(v);
00097 }
00098 return views;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107 RotorDoDView *FindRecycledView(int w, int h)
00108 {
00109 for (RotorDoDViewList::iterator i = mUnusedViewCache.begin();
00110 i != mUnusedViewCache.end(); i++)
00111 {
00112 if ((*i)->HasResolution(w,h)) {
00113 RotorDoDView* recycleMe = *i;
00114 mUnusedViewCache.erase(i);
00115 mRHits++;
00116 return recycleMe;
00117 }
00118 }
00119
00120 RotorDoDView* recycleMe = mUnusedViewCache.front();
00121 mUnusedViewCache.pop_front();
00122 mRMisses++;
00123 return recycleMe;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 RotorView* GetViewFor(String thread, int pos)
00135 {
00136 RotorView *v = FindViewFor(thread, pos);
00137 if (v==NULL)
00138 {
00139 mMisses++;
00140 if (mUnusedViewCache.size() > 0)
00141 v = new RotorView(mWnd,mThreadSet,thread,pos,0.0,0.0,this);
00142 else
00143 v = new RotorView(mWnd,mThreadSet,thread,pos,0.0,0.0);
00144 }
00145 v->SetViewListener(mListenerTarget);
00146 v->Show();
00147 v->UpdateBookmarkState();
00148 return v;
00149 }
00150
00151 RotorView* FindViewFor(String thread, int pos)
00152 {
00153 for(LIST* o=mWnd->GetOGLWND()->objectList ; o ; o=o->next)
00154 {
00155 RotorView* v = OGLVIEW3DToRotorView((OGLVIEW3D *) o->info);
00156 if (v && v->IsSameAs(thread, pos))
00157 {
00158 mHits++;
00159 return v;
00160 }
00161 }
00162 return NULL;
00163 }
00164
00165 void CacheReport()
00166 {
00167 ILOG_DEBUG("RotorViewCache usage statistics:");
00168 ILOG_DEBUG("cache total: " << mHits + mMisses <<
00169 " ( = hits + misses ) ");
00170 ILOG_DEBUG("cache hits: " << mHits);
00171 ILOG_DEBUG("cache misses: " << mMisses <<
00172 " ( = recycled hits + total recreations)");
00173 ILOG_DEBUG("recycled hits: " << mRHits );
00174 ILOG_DEBUG("total recreate: " << mRMisses );
00175 }
00176
00177 bool ThreadExists(CString threadname)
00178 {
00179 return mThreadSet->GetThreadByName(threadname) ? true : false;
00180 }
00181
00182 Thread *GetThread(CString threadname)
00183 {
00184 return mThreadSet->GetThreadByName(threadname);
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194 void ShotIsViewed(int shot, double probability)
00195 {
00196 Core::VideoSet::Segmentation* seg = mThreadSet->GetSegmentation();
00197 return QuidShotIsViewed(seg->GetQuidShot(shot), probability);
00198 }
00199
00200 void QuidShotIsViewed(Quid shot, double probability)
00201 {
00202 double ipro = probability;
00203 if (probability > 1.0) probability = 1.0;
00204 if (probability < 0.0) probability = 0.0;
00205
00206 if (!mShotViewed->IsAnnotated(shot))
00207 mShotViewed->Add(shot, probability);
00208 else
00209 {
00210 int rank = mShotViewed->GetIndex(shot);
00211 probability += mShotViewed->Get2(rank);
00212 if (probability > 1.0) probability = 1.0;
00213 mShotViewed->Set2(rank, probability);
00214 }
00215
00216
00217 }
00218
00219 double GetViewedProbability(int shot)
00220 {
00221 Core::VideoSet::Segmentation* seg = mThreadSet->GetSegmentation();
00222 return GetQuidViewedProbability(seg->GetQuidShot(shot));
00223 }
00224
00225 double GetQuidViewedProbability(Quid shot)
00226 {
00227 if (mShotViewed->IsAnnotated(shot))
00228 return mShotViewed->GetQualification(shot);
00229 else
00230 return 0.0;
00231 }
00232
00233 void ClearShotViewed()
00234 {
00235 ILOG_DEBUG("ClearShotViewed()");
00236 mShotViewed->SetEmpty();
00237 }
00238
00239 AnnotationTable* GetViewedShots()
00240 {
00241 return mShotViewed;
00242 }
00243
00244 private:
00245
00246
00247 OglGui::Window* mWnd;
00248 ThreadSet* mThreadSet;
00249 RotorViewListener* mListenerTarget;
00250 AnnotationTable* mShotViewed;
00251
00252 RotorDoDViewList mUnusedViewCache;
00253
00254 int mHits;
00255 int mMisses;
00256 int mRHits;
00257 int mRMisses;
00258
00259 ILOG_VAR_DEC;
00260 };
00261
00262 ILOG_VAR_INIT(RotorViewCache, Visualization.RotorBrowser);
00263 }
00264 }
00265 }
00266
00267 #endif