00001 #include <iostream>
00002 #include <sstream>
00003
00004 #include "SketchSpace.h"
00005 #include "ShapeRoot.h"
00006 #include "ShapeSpace.h"
00007 #include "Sketch.h"
00008 #include "BaseData.h"
00009 #include "ViewerConnection.h"
00010 #include "Vision/colors.h"
00011
00012 using namespace std;
00013
00014 namespace DualCoding {
00015
00016 SketchSpace::SketchSpace(std::string const _name, ReferenceFrameType_t _refFrameType,
00017 int const init_id, size_t const _width, size_t const _height) :
00018 name(_name), width(_width), height(_height), numPixels(_width*_height),
00019 Tmat(fmat::Transform()), Tmatinv(fmat::Transform()),
00020 idCounter(1), refreshCounter(1),
00021 dualSpace(new ShapeSpace(this,init_id,_name,_refFrameType)),
00022 boolPool(this,"bool"), ucharPool(this,"uchar"),
00023 usintPool(this,"usint"), uintPool(this,"uint"), floatPool(this,"float"), yuvPool(this,"yuv"),
00024 dummy(numPixels), idx(NULL),
00025 idxN(NULL), idxS(NULL), idxE(NULL), idxW(NULL),
00026 idxNE(NULL), idxNW(NULL), idxSE(NULL), idxSW(NULL),
00027 viewer(new ViewerConnection)
00028 {
00029 }
00030
00031 void SketchSpace::requireIdx() {
00032 if ( idx == NULL ) {
00033 idx = new Sketch<skindex>(*this, "idx");
00034 uint i = 0;
00035 for (size_t y = 0; y < height; y++)
00036 for (size_t x = 0; x < width; x++)
00037 setIdx(*idx, x, y, i++);
00038 }
00039 }
00040
00041 void SketchSpace::requireIdx4way() {
00042 if ( idxN == NULL) {
00043 idxN = new Sketch<skindex>(*this,"idN");
00044 idxS = new Sketch<skindex>(*this,"idS");
00045 idxE = new Sketch<skindex>(*this,"idE");
00046 idxW = new Sketch<skindex>(*this,"idW");
00047 int i = 0;
00048 for (size_t y = 0; y < height; y++) {
00049 for (size_t x = 0; x < width; x++) {
00050 setIdx(*idxN, x, y, i-width);
00051 setIdx(*idxS, x, y, i+width);
00052 setIdx(*idxW, x, y, i-1);
00053 setIdx(*idxE, x, y, i+1);
00054 i++;
00055 }
00056 }
00057 }
00058 }
00059
00060 void SketchSpace::requireIdx8way() {
00061 requireIdx4way();
00062 if ( idxNE == NULL) {
00063 idxNE = new Sketch<skindex>(*this,"idNE");
00064 idxNW = new Sketch<skindex>(*this,"idNW");
00065 idxSE = new Sketch<skindex>(*this,"idSE");
00066 idxSW = new Sketch<skindex>(*this,"idSW");
00067 int i = 0;
00068 for (size_t y = 0; y < height; y++) {
00069 for (size_t x = 0; x < width; x++) {
00070 setIdx(*idxNE, x, y, i-width+1);
00071 setIdx(*idxNW, x, y, i-width-1);
00072 setIdx(*idxSE, x, y, i+width+1);
00073 setIdx(*idxSW, x, y, i+width-1);
00074 i++;
00075 }
00076 }
00077 }
00078 }
00079
00080 void SketchSpace::freeIndexes() {
00081 delete idx;
00082 idx=NULL;
00083 delete idxN; delete idxS; delete idxE; delete idxW;
00084 idxN=idxS=idxE=idxW=NULL;
00085 delete idxNE; delete idxNW; delete idxSE; delete idxSW;
00086 idxNE=idxNW=idxSE=idxSW=NULL;
00087 }
00088
00089
00090 void SketchSpace::resize(const size_t new_width, const size_t new_height) {
00091
00092 freeIndexes();
00093 boolPool.deleteElements();
00094 ucharPool.deleteElements();
00095 usintPool.deleteElements();
00096 uintPool.deleteElements();
00097 floatPool.deleteElements();
00098 yuvPool.deleteElements();
00099
00100 width = new_width;
00101 height = new_height;
00102 numPixels = new_width * new_height;
00103 dummy = numPixels;
00104 }
00105
00106 SketchSpace::~SketchSpace() {
00107 delete dualSpace;
00108
00109 freeIndexes();
00110 delete viewer;
00111 }
00112
00113
00114 void SketchSpace::dumpSpace() const {
00115 boolPool.dumpPool();
00116 ucharPool.dumpPool();
00117 usintPool.dumpPool();
00118 uintPool.dumpPool();
00119 floatPool.dumpPool();
00120 yuvPool.dumpPool();
00121 }
00122
00123 void SketchSpace::clear(bool clearRetained) {
00124 boolPool.clear(clearRetained);
00125 ucharPool.clear(clearRetained);
00126 usintPool.clear(clearRetained);
00127 uintPool.clear(clearRetained);
00128 floatPool.clear(clearRetained);
00129 yuvPool.clear(clearRetained);
00130 }
00131
00132 void SketchSpace::setTmat(const fmat::Transform &mat) {
00133 Tmat = mat;
00134 Tmatinv = mat.inverse();
00135
00136
00137 for ( std::vector<ShapeRoot>::iterator it = dualSpace->allShapes().begin();
00138 it != dualSpace->allShapes().end(); it++ )
00139 (*it)->deleteRendering();
00140 }
00141
00142 void SketchSpace::setTmat(float scale, float tx, float ty) {
00143
00144 setTmat(fmat::Transform(fmat::Matrix<3,3>::identity() * scale, fmat::pack(tx,ty,0.f)));
00145 }
00146
00147 void SketchSpace::setTmat(const BoundingBox2D &b) {
00148 float const scale = b.getDimensions().min();
00149 setTmat(scale, -b.min[0], -b.min[1]);
00150 }
00151
00152 void SketchSpace::applyTmat(fmat::Column<3> &vec) {
00153 vec = Tmat * vec;
00154 }
00155
00156 void SketchSpace::applyTmatinv(fmat::Column<3> &vec) {
00157 vec = Tmatinv * vec;
00158 }
00159
00160 void SketchSpace::setIdx(Sketch<skindex>& indices, int const x, int const y, int const indexval) {
00161 int const indexval_x = indexval % width;
00162 int const indexval_y = indexval / width;
00163 indices(x,y) = (indexval_x < 0 || indexval_y < 0 || indexval_x >= (int)width || indexval_y >= (int)height
00164 || abs(indexval_x-x)+1 == (int)width)
00165 ? dummy : indexval;
00166 }
00167
00168 std::string SketchSpace::getTmatForGUI() {
00169 std::ostringstream tmat_stream;
00170 tmat_stream << "tmat" << endl;
00171 for (int i=0; i<3; i++)
00172 for (int j=0; j<4; j++)
00173 tmat_stream << " " << Tmat(i,j);
00174 tmat_stream << endl;
00175 return tmat_stream.str();
00176 }
00177
00178 std::string SketchSpace::getSketchListForGUI() {
00179 bumpRefreshCounter();
00180 std::string sketchlist;
00181 sketchlist += boolPool.getSketchListForGUI();
00182 sketchlist += ucharPool.getSketchListForGUI();
00183 sketchlist += usintPool.getSketchListForGUI();
00184 sketchlist += uintPool.getSketchListForGUI();
00185 sketchlist += floatPool.getSketchListForGUI();
00186 sketchlist += yuvPool.getSketchListForGUI();
00187 return sketchlist;
00188 }
00189
00190 SketchDataRoot* SketchSpace::retrieveSketch(int const id) {
00191 SketchDataRoot* sketchp;
00192
00193 sketchp = boolPool.retrieveSketch(id);
00194 if ( sketchp ) return sketchp;
00195 sketchp = ucharPool.retrieveSketch(id);
00196 if ( sketchp ) return sketchp;
00197 sketchp = usintPool.retrieveSketch(id);
00198 if ( sketchp ) return sketchp;
00199 sketchp = uintPool.retrieveSketch(id);
00200 if ( sketchp ) return sketchp;
00201 sketchp = floatPool.retrieveSketch(id);
00202 if ( sketchp ) return sketchp;
00203 sketchp = yuvPool.retrieveSketch(id);
00204 return sketchp;
00205 }
00206
00207 }