00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "Neuro/SimulationViewerHand.H"
00040
00041
00042
00043 #include "Component/OptionManager.H"
00044
00045 #include "Image/ColorOps.H"
00046
00047 #include "Image/DrawOps.H"
00048 #include "Image/FilterOps.H"
00049
00050 #include "Image/Transforms.H"
00051 #include "Image/ShapeOps.H"
00052 #include "Neuro/NeuroOpts.H"
00053 #include "Neuro/NeuroSimEvents.H"
00054 #include "Neuro/SpatialMetrics.H"
00055 #include "Psycho/HandData.H"
00056 #include "Simulation/SimEventQueue.H"
00057
00058 #include "Transport/FrameOstream.H"
00059 #include "Util/sformat.H"
00060 #include "rutz/trace.h"
00061
00062 #include <fstream>
00063 #include <iostream>
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 SimulationViewerHand::
00076 SimulationViewerHand(OptionManager& mgr, const std::string& descrName,
00077 const std::string& tagName) :
00078 SimulationViewer(mgr, descrName, tagName),
00079 SIMCALLBACK_INIT(SimEventClockTick),
00080 SIMCALLBACK_INIT(SimEventSaveOutput),
00081 itsMetrics(new SpatialMetrics(mgr)),
00082 itsSaveTraj(&OPT_SVsaveTraj, this),
00083 itsSaveCombo(&OPT_SVHandSaveCombo, this),
00084 itsDisplayHand(&OPT_SVHandDisplay, this),
00085 itsPatchSize(&OPT_SVpatchSize, this),
00086 itsEraseMarker(&OPT_SVeraseMarker, this),
00087 itsMaxComboWidth(&OPT_SVHandMaxComboWidth, this),
00088 itsDrawings(), itsCurrTime(), itsFrameNumber(-1),
00089 itsHeaderCrafted(false), itsOutFields(), itsHandStyles(),
00090 itsTargets()
00091 {
00092 GVX_TRACE(__PRETTY_FUNCTION__);
00093
00094 this->addSubComponent(itsMetrics);
00095
00096
00097 getManager().setOptionValString(&OPT_HandControllerType, "HandTrack");
00098 }
00099
00100
00101 SimulationViewerHand::~SimulationViewerHand()
00102 {
00103 GVX_TRACE(__PRETTY_FUNCTION__);
00104 }
00105
00106
00107 void SimulationViewerHand::start1()
00108 {
00109 GVX_TRACE(__PRETTY_FUNCTION__);
00110
00111 SimulationViewer::start1();
00112 }
00113
00114
00115 void SimulationViewerHand::stop1()
00116 {
00117 GVX_TRACE(__PRETTY_FUNCTION__);
00118 }
00119
00120
00121 void SimulationViewerHand::
00122 onSimEventClockTick(SimEventQueue& q, rutz::shared_ptr<SimEventClockTick>& ect)
00123 {
00124
00125 itsCurrTime = q.now();
00126
00127
00128 if (SeC<SimEventRetinaImage> e = q.check<SimEventRetinaImage>(this)) {
00129 itsDrawings.resize(e->frame().getDims(), true);
00130 itsFrameNumber++;
00131
00132 }
00133
00134
00135 const Image<float> currsm = getMap(q, false);
00136
00137
00138
00139 SeC<SimEventRetinaImage> retev = q.check<SimEventRetinaImage>(this, SEQ_ANY);
00140 if (retev.is_valid() == false)
00141 LFATAL("I need some SimEventRetinaImage in the queue to function.");
00142
00143
00144 SeC<SimEventHandTrackerData> e = q.check<SimEventHandTrackerData>(this);
00145
00146 while(e.is_valid()) {
00147
00148 const rutz::shared_ptr<HandData> trackCurrPos = e->data();
00149 const uint tnum = e->trackerNum();
00150 const std::string tfn = e->trackerFilename();
00151
00152 if (itsHandStyles.size() <= tnum) {
00153 itsHandStyles.resize(tnum+1);
00154
00155 itsHandStyles[tnum].pSize = itsPatchSize.getVal();
00156 itsHandStyles[tnum].col = e->trackerColor();
00157
00158
00159
00160
00161 itsHandStyles[tnum].label = tfn.substr(tfn.find("data")+5,2);
00162 }
00163
00164
00165 if (itsDisplayHand.getVal()) {
00166
00167
00168 if (itsEraseMarker.getVal()) itsDrawings.resize(retev->frame().getDims(), true);
00169
00170
00171 drawHand(trackCurrPos, tnum);
00172 }
00173
00174
00175
00176 extraSampleProcessing(trackCurrPos);
00177
00178
00179 e = q.check<SimEventHandTrackerData>(this);
00180 }
00181 }
00182
00183
00184
00185
00186 void SimulationViewerHand::drawHand(const rutz::shared_ptr<HandData> rawPos, const uint tN)
00187 {
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 const Dims dims = itsDrawings.getDims();
00207
00208
00209
00210 const Dims numgrid (80,60);
00211 const Dims grid (dims.w()/numgrid.w(),dims.h()/numgrid.h());
00212
00213
00214 int rawX = rawPos->getX();
00215 int rawY = rawPos->getY();
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 Point2D<int> X1 (getGridCoord(grid, 2, numgrid.h()-1));
00227 Point2D<int> X2 (getGridCoord(grid, numgrid.w()-1, numgrid.h()-1));
00228 Point2D<int> XC ((127*(X2.i-X1.i)/255)+X1.i,X1.j);
00229 Point2D<int> XD (0,grid.h()/2);
00230 Point2D<int> Y1 (getGridCoord(grid, 1, 1));
00231 Point2D<int> Y2 (getGridCoord(grid, 1, numgrid.h()-2));
00232 Point2D<int> YC (grid.w(),(127*(Y2.j-Y1.j)/255)+Y1.j);
00233 Point2D<int> YD (grid.w()/2,0);
00234
00235 Point2D<int> X_ ((rawX*(X2.i-X1.i)/255)+X1.i, dims.h()-grid.h());
00236 Point2D<int> Y_ (grid.w(), (rawY*(Y2.j-Y1.j)/255)+Y1.j);
00237
00238
00239 const PixRGB<byte> colWhite (255,255,255);
00240
00241 drawLine(itsDrawings, X1, X2, colWhite);
00242 drawLine(itsDrawings, X1-XD, X1+XD, colWhite);
00243 drawLine(itsDrawings, XC-XD, XC+XD, colWhite);
00244 drawLine(itsDrawings, X2-XD, X2+XD, colWhite);
00245
00246 drawLine(itsDrawings, Y1, Y2, colWhite);
00247 drawLine(itsDrawings, Y1-YD, Y1+YD, colWhite);
00248 drawLine(itsDrawings, YC-YD, YC+YD, colWhite);
00249 drawLine(itsDrawings, Y2-YD, Y2+YD, colWhite);
00250
00251
00252
00253 drawPatchBB(itsDrawings, X_, itsHandStyles[tN].pSize,
00254 colWhite, PixRGB<byte>(1));
00255 drawPatchBB(itsDrawings, Y_, itsHandStyles[tN].pSize,
00256 colWhite, PixRGB<byte>(1));
00257
00258 PixRGB<byte> colB;
00259
00260 if (rawPos->isButtonEmpty() == false) {
00261 for (uint i = 0; i < rawPos->numButton(); i++) {
00262
00263 if (rawPos->isPressed(i))
00264 colB = PixRGB<byte> (255, 0, 0);
00265 else
00266 colB = PixRGB<byte> (0, 0, 0);
00267
00268
00269
00270
00271
00272 writeText(itsDrawings, Point2D<int>(getGridCoord(grid, 3*i+10, numgrid.h()-5)),
00273 sformat("%2d",i+1).c_str(), PixRGB<byte>(255), colB,SimpleFont::FIXED(10),false);
00274
00275
00276
00277 }
00278 }
00279 }
00280
00281
00282 void SimulationViewerHand::extraSampleProcessing(const rutz::shared_ptr<HandData>) {}
00283
00284
00285
00286 Image< PixRGB<byte> > SimulationViewerHand::getTraj(SimEventQueue& q)
00287 {
00288 GVX_TRACE(__PRETTY_FUNCTION__);
00289
00290
00291 Image< PixRGB<byte> > input;
00292 if (SeC<SimEventRetinaImage> e = q.check<SimEventRetinaImage>(this, SEQ_ANY))
00293 input = e->frame().colorByte();
00294 else
00295 LFATAL("Could not find required SimEventRetinaImage");
00296
00297
00298 Image< PixRGB<byte> > comp = composite(itsDrawings, input);
00299
00300
00301 if (itsSaveTraj.getVal()) return comp;
00302
00303
00304
00305
00306 const Dims dims = itsDrawings.getDims();
00307 Image< PixRGB<byte> > ret;
00308
00309 if (itsSaveCombo.getVal()) {
00310 ret = concatX(input, itsDrawings);
00311 drawLine(ret, Point2D<int>(dims.w()-1, 0), Point2D<int>(dims.w()-1, dims.h()-1),
00312 PixRGB<byte>(255, 255, 0), 1);
00313 drawLine(ret, Point2D<int>(dims.w(), 0), Point2D<int>(dims.w(), dims.h()-1),
00314 PixRGB<byte>(255, 255, 0), 1);
00315 }
00316
00317
00318 while (ret.getWidth() > itsMaxComboWidth.getVal())
00319 ret = decY(lowPass3y(decX(lowPass3x(ret))));
00320
00321 return ret;
00322 }
00323
00324
00325 void SimulationViewerHand::
00326 onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00327 {
00328 this->save1(e->sinfo());
00329 }
00330
00331
00332 void SimulationViewerHand::save1(const ModelComponentSaveInfo& sinfo)
00333 {
00334 GVX_TRACE(__PRETTY_FUNCTION__);
00335 SimEventQueue *q = dynamic_cast<const SimModuleSaveInfo&>(sinfo).q;
00336
00337
00338 Image< PixRGB<byte> > res = getTraj(*q);
00339
00340
00341 if (itsSaveCombo.getVal() || itsSaveTraj.getVal())
00342 {
00343
00344
00345 nub::ref<FrameOstream> ofs =
00346 dynamic_cast<const SimModuleSaveInfo&>(sinfo).ofs;
00347
00348 ofs->writeRGB(res, "T",
00349 FrameInfo("SimulationViewerHand trajectory", SRC_POS));
00350
00351 }
00352 }
00353
00354
00355
00356
00357
00358