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