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 namespace DualCoding {
00012
00013 SketchSpace::SketchSpace(string const _name, ReferenceFrameType_t _refFrameType,
00014 int const init_id, size_t const _width, size_t const _height) :
00015 name(_name), width(_width), height(_height), numPixels(_width*_height),
00016 Tmat(NEWMAT::IdentityMatrix(4)), Tmatinv(NEWMAT::IdentityMatrix(4)),
00017 idCounter(1), refreshCounter(1),
00018 dualSpace(new ShapeSpace(this,init_id,_name,_refFrameType)),
00019 boolPool(this,0), ucharPool(this,0), usintPool(this,0), floatPool(this,0),
00020 dummy(width*height), idx(NULL),
00021 idxN(NULL), idxS(NULL), idxE(NULL), idxW(NULL),
00022 idxNE(NULL), idxNW(NULL), idxSE(NULL), idxSW(NULL),
00023 viewer(new ViewerConnection)
00024 {
00025 }
00026
00027 void SketchSpace::requireIdx() {
00028 if(idx!=NULL)
00029 return;
00030 idx=new Sketch<usint>(*this, "idx");
00031 int i = 0;
00032 for (size_t y = 0; y < height; y++) {
00033 for (size_t x = 0; x < width; x++) {
00034 setIdx(*idx, x, y, i);
00035 i++;
00036 }
00037 }
00038
00039 }
00040
00041 void SketchSpace::requireIdx4way() {
00042 if ( idxN == NULL) {
00043 idxN = new Sketch<usint>(*this,"idN");
00044 idxS = new Sketch<usint>(*this,"idS");
00045 idxE = new Sketch<usint>(*this,"idE");
00046 idxW = new Sketch<usint>(*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<usint>(*this,"idNE");
00064 idxNW = new Sketch<usint>(*this,"idNW");
00065 idxSE = new Sketch<usint>(*this,"idSE");
00066 idxSW = new Sketch<usint>(*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 SketchSpace::~SketchSpace() {
00091 delete dualSpace;
00092
00093 freeIndexes();
00094 delete viewer;
00095 }
00096
00097
00098 void SketchSpace::dumpSpace() const {
00099 boolPool.dumpPool();
00100 ucharPool.dumpPool();
00101 usintPool.dumpPool();
00102 floatPool.dumpPool();
00103 }
00104
00105 void SketchSpace::clear() {
00106 boolPool.clear();
00107 ucharPool.clear();
00108 usintPool.clear();
00109 floatPool.clear();
00110 }
00111
00112 void SketchSpace::setTmat(const NEWMAT::Matrix &mat) {
00113 Tmat = mat;
00114 Tmatinv = mat.i();
00115 }
00116
00117 void SketchSpace::setTmat(float scale, float tx, float ty) {
00118 NEWMAT::Matrix mat(4,4);
00119 mat << 1 << 0 << 0 << tx
00120 << 0 << 1 << 0 << ty
00121 << 0 << 0 << 1 << 0
00122 << 0 << 0 << 0 << 1/scale;
00123 setTmat(mat);
00124 }
00125
00126 void SketchSpace::setTmat(const BoundingBox &b) {
00127 float const xscale = (b.xmax - b.xmin) / width;
00128 float const yscale = (b.ymax - b.ymin) / height;
00129 float const scale = min(xscale,yscale);
00130 setTmat(scale, -b.xmin, -b.ymin);
00131 }
00132
00133 void SketchSpace::applyTmat(NEWMAT::ColumnVector &vec) {
00134 vec = Tmat * vec;
00135 if ( vec(4) != 0 )
00136 vec = vec / vec(4);
00137 }
00138
00139 void SketchSpace::applyTmatinv(NEWMAT::ColumnVector &vec) {
00140 vec = Tmatinv * vec;
00141 if ( vec(4) != 0 )
00142 vec = vec / vec(4);
00143 }
00144
00145 void SketchSpace::setIdx(Sketch<usint>& indices, int x, int y, int shift_i) {
00146 int shift_x = shift_i % width;
00147 int shift_y = shift_i / width;
00148 if (shift_x < 0 || shift_y < 0 || shift_x >= (int)width || shift_y >= (int)height
00149 || abs(shift_x-x)+1 == (int)width)
00150 shift_i = dummy;
00151 indices(x,y) = shift_i;
00152 }
00153
00154 std::string SketchSpace::getTmatForGUI() {
00155 std::ostringstream tmat_stream;
00156 tmat_stream << "tmat" << endl;
00157 for (int i=1; i<5; i++)
00158 for (int j=1; j<5; j++)
00159 tmat_stream << " " << Tmat(i,j);
00160 tmat_stream << endl;
00161 return tmat_stream.str();
00162 }
00163
00164 std::string SketchSpace::getSketchListForGUI() {
00165 bumpRefreshCounter();
00166 std::string sketchlist;
00167 sketchlist += boolPool.getSketchListForGUI();
00168 sketchlist += ucharPool.getSketchListForGUI();
00169 sketchlist += usintPool.getSketchListForGUI();
00170 sketchlist += floatPool.getSketchListForGUI();
00171 return sketchlist;
00172 }
00173
00174 SketchDataRoot* SketchSpace::retrieveSketch(int id) {
00175 SketchDataRoot* sketchp;
00176
00177 sketchp = boolPool.retrieveSketch(id);
00178 if(sketchp) return sketchp;
00179 sketchp = ucharPool.retrieveSketch(id);
00180 if(sketchp) return sketchp;
00181 sketchp = usintPool.retrieveSketch(id);
00182 if(sketchp) return sketchp;
00183 sketchp = floatPool.retrieveSketch(id);
00184 return sketchp;
00185 }
00186
00187 }