00001 /*!@file Media/SimFrameSeries.C a series of frames as SimModule */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Laurent Itti <itti@pollux.usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Media/SimFrameSeries.C $ 00035 // $Id: SimFrameSeries.C 13373 2010-05-09 04:28:40Z lior $ 00036 // 00037 00038 #include "Media/SimFrameSeries.H" 00039 00040 #include "Media/FrameSeries.H" 00041 #include "Simulation/SimModule.H" 00042 #include "Simulation/SimEvents.H" 00043 #include "Simulation/SimEventQueue.H" 00044 #include "Simulation/SimulationOpts.H" 00045 #include "GUI/ImageDisplayStream.H" 00046 #include "GUI/XWinManaged.H" 00047 00048 00049 00050 // ###################################################################### 00051 // ###################################################################### 00052 // ###################################################################### 00053 00054 //Used for dynamaicly loaded modules 00055 SIMMODULEINSTFUNC(SimInputFrameSeries); 00056 00057 SimInputFrameSeries::SimInputFrameSeries(OptionManager& mgr, 00058 const std::string& descrName, 00059 const std::string& tagName) : 00060 SimModule(mgr, descrName, tagName), 00061 SIMCALLBACK_INIT(SimEventClockTick), 00062 itsIFS(new InputFrameSeries(mgr)) 00063 { 00064 addSubComponent(itsIFS); 00065 } 00066 00067 // ###################################################################### 00068 SimInputFrameSeries::~SimInputFrameSeries() 00069 { } 00070 00071 // ###################################################################### 00072 void SimInputFrameSeries:: 00073 onSimEventClockTick(SimEventQueue& q, rutz::shared_ptr<SimEventClockTick>& e) 00074 { 00075 // update our FrameSeries: 00076 const FrameState fs = itsIFS->update(q.now()); 00077 00078 // do we have a new frame? 00079 if (fs == FRAME_NEXT || fs == FRAME_FINAL) 00080 { 00081 // read the frame and post it to our SimEventQueue: 00082 GenericFrame f = itsIFS->readFrame(); 00083 if (f.initialized()) 00084 { 00085 rutz::shared_ptr<SimEventInputFrame> 00086 ev(new SimEventInputFrame(this, f, itsIFS->frame())); 00087 q.post(ev); 00088 00089 // now is a great time to display memory usage stats if the 00090 // user wishes to see them: 00091 rutz::shared_ptr<SimEventShowMemStats> 00092 ee(new SimEventShowMemStats(this, itsIFS->frame(), 00093 f.frameSpec().dims.sz())); 00094 q.post(ee); 00095 } 00096 } 00097 00098 // input exhausted? 00099 if (fs == FRAME_COMPLETE) 00100 { 00101 // post a break event: 00102 rutz::shared_ptr<SimEventBreak> 00103 ev(new SimEventBreak(this, "Input frames exhausted")); 00104 q.post(ev); 00105 } 00106 00107 // does our FrameSeries wish to wait for user? 00108 if (itsIFS->shouldWait()) 00109 { 00110 // post a userwait event: 00111 rutz::shared_ptr<SimEventUserWait> ev(new SimEventUserWait(this)); 00112 q.post(ev); 00113 } 00114 } 00115 00116 // ###################################################################### 00117 GenericFrameSpec SimInputFrameSeries::peekFrameSpec() 00118 { return itsIFS->peekFrameSpec(); } 00119 00120 // ###################################################################### 00121 void SimInputFrameSeries::startStream() 00122 { itsIFS->startStream(); } 00123 00124 // ###################################################################### 00125 void SimInputFrameSeries::setFrameSource(const std::string& source) 00126 { itsIFS->setFrameSource(source); } 00127 00128 // ###################################################################### 00129 int SimInputFrameSeries::frame() const 00130 { return itsIFS->frame(); } 00131 00132 00133 00134 // ###################################################################### 00135 // ###################################################################### 00136 // ###################################################################### 00137 00138 //Used for dynamaicly loaded modules 00139 SIMMODULEINSTFUNC(SimOutputFrameSeries); 00140 00141 SimOutputFrameSeries::SimOutputFrameSeries(OptionManager& mgr, 00142 const std::string& descrName, 00143 const std::string& tagName) : 00144 SimModule(mgr, descrName, tagName), 00145 SIMCALLBACK_INIT(SimEventClockTick), 00146 SIMCALLBACK_INIT(SimEventRequestSaveOutput), 00147 itsTimeStep(&OPT_SimulationTimeStep, this), 00148 itsOFS(new OutputFrameSeries(mgr)), 00149 itsSaveRequested(false) 00150 { 00151 addSubComponent(itsOFS); 00152 } 00153 00154 // ###################################################################### 00155 SimOutputFrameSeries::~SimOutputFrameSeries() 00156 { } 00157 00158 // ###################################################################### 00159 void SimOutputFrameSeries:: 00160 onSimEventClockTick(SimEventQueue& q, rutz::shared_ptr<SimEventClockTick>& ect) 00161 { 00162 update(q, itsSaveRequested); 00163 } 00164 00165 // ###################################################################### 00166 void SimOutputFrameSeries:: 00167 onSimEventRequestSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventRequestSaveOutput>& e) 00168 { 00169 update(q, true); 00170 } 00171 00172 // ###################################################################### 00173 void SimOutputFrameSeries::update(SimEventQueue& q, const bool saveRequested) 00174 { 00175 // ok, we have a request to save; see whether we should save now: 00176 const FrameState fs = itsOFS->update(q.now(), saveRequested); 00177 00178 //Go though our windows and send any keyboard or mouse click events 00179 const nub::soft_ref<ImageDisplayStream> ids = itsOFS->findFrameDestType<ImageDisplayStream>(); 00180 if (ids.is_valid()) //Do we have a xwins 00181 { 00182 std::vector<rutz::shared_ptr<XWinManaged> > windows = ids->getWindows(); 00183 00184 for(uint i=0; i<windows.size(); i++) 00185 { 00186 const rutz::shared_ptr<XWinManaged> uiwin = windows[i]; 00187 if (uiwin.is_valid()) 00188 { 00189 Point2D<int> loc = uiwin->getLastMouseClick(); 00190 int key = uiwin->getLastKeyPress(); 00191 00192 if (loc.isValid() || key != -1) 00193 { 00194 rutz::shared_ptr<SimEventUserInput> e(new SimEventUserInput(this, uiwin->getTitle(), loc, key)); 00195 q.post(e); 00196 } 00197 } 00198 } 00199 } 00200 00201 // should we save results immediately? 00202 if (fs == FRAME_NEXT || fs == FRAME_FINAL) 00203 { 00204 // request that everybody save their outputs to our OFS: 00205 rutz::shared_ptr<SimModuleSaveInfo> sinfo(new SimModuleSaveInfo(itsOFS, q)); 00206 rutz::shared_ptr<SimEventSaveOutput> e(new SimEventSaveOutput(this, sinfo)); 00207 q.post(e); 00208 00209 // if we had some save requests, clear them since we just saved now: 00210 itsSaveRequested = false; 00211 } 00212 else 00213 // otherwise, just note that we should save them next time we write an output frame: 00214 if (saveRequested) itsSaveRequested = true; 00215 00216 // does our FrameSeries wish to wait for user? 00217 if (itsOFS->shouldWait()) 00218 { 00219 // post a userwait event which our SimEventQueue will catch at 00220 // the turn of the current time step: 00221 rutz::shared_ptr<SimEventUserWait> e(new SimEventUserWait(this)); 00222 q.post(e); 00223 } 00224 00225 // is this the end of our outputs? 00226 if (fs == FRAME_FINAL) 00227 { 00228 rutz::shared_ptr<SimEventBreak> e(new SimEventBreak(this, "Output frames complete")); 00229 q.post(e); 00230 } 00231 } 00232 00233 // ###################################################################### 00234 bool SimOutputFrameSeries::isVoid() const 00235 { return itsOFS->isVoid(); } 00236 00237 // ###################################################################### 00238 void SimOutputFrameSeries::addFrameDest(const std::string& dest) 00239 { itsOFS->addFrameDest(dest); } 00240 00241 // ###################################################################### 00242 int SimOutputFrameSeries::frame() const 00243 { return itsOFS->frame(); } 00244 00245 // ###################################################################### 00246 /* So things look consistent in everyone's emacs... */ 00247 /* Local Variables: */ 00248 /* indent-tabs-mode: nil */ 00249 /* End: */