From 53b73dec4d6e71899d183335509591b7c89a419b Mon Sep 17 00:00:00 2001 From: mutantturkey Date: Thu, 14 Jun 2012 18:34:23 -0400 Subject: pushing the fly-tools into this repository --- fly-tools/misc/linesweep.cpp | 693 +++++++++++++++++++++++++++++++++++++++++++ fly-tools/misc/makeimage.cpp | 51 ++++ fly-tools/misc/test.cpp | 92 ++++++ 3 files changed, 836 insertions(+) create mode 100644 fly-tools/misc/linesweep.cpp create mode 100644 fly-tools/misc/makeimage.cpp create mode 100644 fly-tools/misc/test.cpp (limited to 'fly-tools/misc') diff --git a/fly-tools/misc/linesweep.cpp b/fly-tools/misc/linesweep.cpp new file mode 100644 index 0000000..fa55522 --- /dev/null +++ b/fly-tools/misc/linesweep.cpp @@ -0,0 +1,693 @@ +#include +#include +#include +#include +#include +#include +#include +#include "jzImage.h" + +using namespace Magick; +using namespace std; + +unsigned int jzImage::_h; +unsigned int jzImage::_w; +char* jzImage::data = NULL; + +// colors in debugging +const int C = 10; + +static map len; +bool inWhite; +bool startedWhite; +int x_init, y_init; + +inline int roundT(double v) { return int(v+0.5); } +inline int dist(int x0, int y0, int x1, int y1) {return roundT(sqrt(pow(double(x1)-x0, 2.0)+pow(double(y1)-y0, 2.0)));} + +inline void drawLine(int x0, int y0, int x1, int y1, jzImage& img); +inline void lookAt(int x, int y, jzImage& img); + + +void writeHist(const char* filename) +{ + map::iterator front = len.begin(), + back = len.end(); + back--; + + + unsigned int first = front->first, last = back->first; + /*if (cutoff != -1 && cutoff < int(last)) + last = cutoff; + */ + cout << "Min: " << first << endl + << "Max: " << last << endl + << "Count: " << last-first << endl; + //vector hist(last-first, 0); + vector hist(last+1, 0); + + cout << "hist size: " << hist.size() << endl; + try{ + for(unsigned int j = 0; j= int(hist.size()) ) + hist.resize(j-first,0); + hist[roundT(j-first)] = len[j]; + */ + + /*if ( roundT(j) >= int(hist.size()) ) + hist.resize(j,0); + hist[roundT(j)] = len[j]; + */ + hist[j] = len[j]; + } + } + catch (...) + { cerr << "Bad histogram bucketing" << endl; } + + /*if ( (cutoff >= 0) && (cutoff len) +{ + map::iterator front = len.begin(), + back = len.end(); + back--; + + + unsigned int first = front->first, last = back->first; + /*if (cutoff != -1 && cutoff < int(last)) + last = cutoff; + */ + cout<< "Min: " << first << endl + << "Max: " << last << endl + << "Count: " << last-first << endl; + //vector hist(last-first, 0); + vector hist(last+1, 0); + + cout << "hist size: " << hist.size() << endl; + try{ + for(unsigned int j = 0; j= int(hist.size()) ) + hist.resize(j-first,0); + hist[roundT(j-first)] = len[j]; + */ + + /*if ( roundT(j) >= int(hist.size()) ) + hist.resize(j,0); + hist[roundT(j)] = len[j]; + */ + hist[j] = len[j]; + } + } + catch (...) + { cerr << "Bad histogram bucketing" << endl; } + + /*if ( (cutoff >= 0) && (cutoff " << endl; + return -1; + } + + int i, j; + + inWhite = false; + len.clear(); + + MagickCore::SetMagickResourceLimit(MagickCore::MemoryResource, 1536); + MagickCore::SetMagickResourceLimit(MagickCore::MapResource, 4096); + Image* img = new Image(argv[1]); + + int width = img->columns(), + height = img->rows(); + + jzImage &myImg = jzImage::Instance(img); + + delete img; + + /* + // doesn't take pixel(0,0) and also takes pixel(width-1, height-1) twice + //map, pair >, bool> hitMap; + const int boundSize = 2*(width+height-2); + static pair* boundary = new pair[boundSize]; + + for (i = 0; i, pair >, bool> hitMap; + const int boundSize = 2*(width+height-2); + static pair* boundary = new pair[boundSize]; + + for (i = 0; i<=width-1; i++) + { + boundary[i].first = i; + boundary[i].second = 0; + boundary[width+(height-2)+i].first = i; + boundary[width+(height-2)+i].second = height-1; + } + + for (i = 0; i= 1 + //drawLine(3,2,5,5,myImg); + // 0<=m<1 + //drawLine(5,5,1,4,myImg); + // -1< m < 0 + //drawLine(7,0,2,3,myImg); + // m<-1 + //drawLine(1,7,3,2,myImg); + // dx = 0 + //drawLine(5,0,5,5,myImg); + // dy = 0 + //drawLine(0,3,7,3,myImg); + // multiple length + //drawLine(1,7,7,1,myImg); + + + + for (i = 0; i lineSweepValLog; + map::iterator front = len.begin(), back = len.end(); + + for( front; front != back; front++ ) { + double logvalue = log(front->second); + lineSweepValLog[front->first] = logvalue; + } + + string fileNameLog(argv[2]); + fileNameLog = fileNameLog + "_log_hist.txt"; + writeHistLog(fileNameLog.c_str(), lineSweepValLog); + + + delete[] boundary; + + return 0; +} + +/* +void lookAt(int x, int y, jzImage& img) +{ + if (img.pixelColor(x,y) == 1) + // if it was the first white pixel then + if (!inWhite) + { + inWhite = true; + x_init = x; + y_init = y; + } + + if (img.pixelColor(x,y) == 0) + if (inWhite) + { + int val = roundT(dist(x_init, y_init, x, y)); + len[val]++; + inWhite = false; + } +} +*/ + +void lookAt(int x, int y, jzImage& img) +{ + int imageWidth =img.width(); + int imageHeight = img.height(); + // if current pixel is white + if (img.pixelColor(x,y) == 1) { + // if it was the first white pixel then + if (!inWhite) + { + // check whether it started as a white pixel; if it started as white pixel then this line segment should + // not be counted. because it is part of the larger white blob + if ( (x == 0 || x == (imageWidth -1) ) || ( y == 0 || y == (imageHeight - 1))) + startedWhite = true; + inWhite = true; + x_init = x; + y_init = y; + } + // if we are on a white region + /*else { + + } + */ + } + + if (img.pixelColor(x,y) == 0) { + // if we are going through a white line and reached the black pixel + if (inWhite) + { + // if the line started as a white pixel then discard the line fragment + if (startedWhite == false) { + int val = roundT(dist(x_init, y_init, x, y)); + //cout<<" val is = " << val << " at (x0,y0) = " << x_init << "," << y_init << " to (x1,y1) = " << x << "," << y < x1 + if (x0 > x1) + { + int temp = x0; + x0 = x1; + x1 = temp; + temp = y0; + y0 = y1; + y1 = temp; + } + + int dx, dy, d, x, y, incrE, incrNE; + dx = x1 - x0; + dy = y1 - y0; + y = y0; + x = x0; + + // if they are on a vertical line + if (dx == 0) + { + if (y0 < y1) + for (int i = y0; i<=y1; i++) + lookAt(x,i,img); + else + for (int i = y1; i<=y0; i++) + lookAt(x,i,img); + return; + } + + // if they are on a horizontal line + if (dy == 0) + { + for (int i = x0; i<=x1; i++) + lookAt(i,y,img); + return; + } + + int dir = 0; + double m = double(dy)/double(dx); + if (m >= 1.0) + dir = 1; + else if ( (m < 0.0) && (m > -1.0) ) + dir = 2; + else if (m <= -1.0) + dir = 3; + + switch(dir) + { + // when slope m: 0< m <1 + case 0: + d = dy*2 - dx; + incrE = dy*2; + incrNE = (dy - dx)*2; + x = x0; + y = y0; + + lookAt(x,y,img); + + while (x= 1 + case 1: + d = dx*2 - dy; + incrE = dx*2; + incrNE = (dx - dy)*2; + x = x0; + y = y0; + + lookAt(x,y,img); + + while (y= 0) + { + d+= incrE; + x++; + } + else + { + d+= incrNE; + x++; + y--; + } + + lookAt(x,y,img); + } + break; + // since initially we swapped the P0 with P1 so that P0 always holds values with smaller x cooridinate + // so we are sure that m<0 is the result of y0 being greater that + case 3: + d = dx*2 + dy; + incrE = dx*2; + incrNE = (dx + dy)*2; + x = x0; + y = y0; + + lookAt(x,y,img); + + while (y>y1) + { + if (d <= 0) + { + d+= incrE; + y--; + } + else + { + d+= incrNE; + x++; + y--; + } + lookAt(x,y,img); + } + break; + } + + + if (inWhite) + { + // it is a fraction of the blob so dont count it + //len[dist(x_init,y_init,x,y)+1]++; + inWhite = false; + startedWhite = false; + } +} +*/ + +void drawLine(int x0, int y0, int x1, int y1, jzImage& img) +{ + inWhite = false; + startedWhite = false; + + // always go from x0 -> x1 + if (x0 > x1) + { + int temp = x0; + x0 = x1; + x1 = temp; + temp = y0; + y0 = y1; + y1 = temp; + } + + int dx, dy, d, x, y, incrE, incrNE; + dx = x1 - x0; + dy = y1 - y0; + y = y0; + x = x0; + + // if they are on a vertical line + if (dx == 0) + { + if (y0 < y1) + for (int i = y0; i<=y1; i++) + lookAt(x,i,img); + else + for (int i = y1; i<=y0; i++) + lookAt(x,i,img); + return; + } + + // if they are on a horizontal line + if (dy == 0) + { + for (int i = x0; i<=x1; i++) + lookAt(i,y,img); + return; + } + + int dir = 0; + double m = double(dy)/double(dx); + if (m >= 1.0) + dir = 1; + else if ( (m < 0.0) && (m > -1.0) ) + dir = 2; + else if (m <= -1.0) + dir = 3; + + switch(dir) + { + // when slope m: 0< m <1 + case 0: + d = dy*2 - dx; + incrE = dy*2; + incrNE = (dy - dx)*2; + x = x0; + y = y0; + + lookAt(x,y,img); + + while (x= 1 + case 1: + d = dx*2 - dy; + incrE = dx*2; + incrNE = (dx - dy)*2; + x = x0; + y = y0; + + lookAt(x,y,img); + + while (y= 0) + { + d+= incrE; + x++; + } + else + { + d+= incrNE; + x++; + y--; + } + + lookAt(x,y,img); + } + break; + // since initially we swapped the P0 with P1 so that P0 always holds values with smaller x cooridinate + // so we are sure that m<0 is the result of y0 being greater that + case 3: + d = dx*2 + dy; + incrE = dx*2; + incrNE = (dx + dy)*2; + x = x0; + y = y0; + + lookAt(x,y,img); + + while (y>y1) + { + if (d <= 0) + { + d+= incrE; + y--; + } + else + { + d+= incrNE; + x++; + y--; + } + lookAt(x,y,img); + } + break; + } + + + if (inWhite) + { + // it is a fraction of the blob so dont count it + //len[dist(x_init,y_init,x,y)+1]++; + inWhite = false; + startedWhite = false; + } +} diff --git a/fly-tools/misc/makeimage.cpp b/fly-tools/misc/makeimage.cpp new file mode 100644 index 0000000..06e2897 --- /dev/null +++ b/fly-tools/misc/makeimage.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +using namespace std; +using namespace Magick; + +int main(int argc,char **argv) + +{ + // Construct the image object. Seperating image construction from the + // the read operation ensures that a failure to read the image file + // doesn't render the image object useless. + + Image* img; + char buffer[100]; + sprintf(buffer,"%ix%i",7,7); + + // residual image is initialized with black representing not visited. + //residual = new Image(buffer, "black"); + + img = new Image(buffer, "white"); + + for (int j=0; j<=3;j++) { + for (int i=0; i<=3-j; i++) { + img->pixelColor(i,j, "black"); + img->pixelColor(6-i,j, "black"); + } + } + + int k; + for (int j=4; j<=6;j++) { + for (int i=0; i<=j-3; i++) { + img->pixelColor(i,j, "black"); + img->pixelColor(6-i,j, "black"); + } + } + + + //img->pixelColor(0,3, "red"); + + + string namei = "7x7.png"; + img->write(namei.c_str()); + + delete img; + + return 0; + +} + diff --git a/fly-tools/misc/test.cpp b/fly-tools/misc/test.cpp new file mode 100644 index 0000000..a709482 --- /dev/null +++ b/fly-tools/misc/test.cpp @@ -0,0 +1,92 @@ +#include +#include + +using namespace std; + +void largestIncreasingPositiveDotProductSeq(vector velocityDirs, int &startIndex, int &endIndex) { + + int positiveVelSeqSize = 0; + int flag = false; + int maxSeqSize = 0; + int st = 0; + for (int j=0; j 0 && flag == false) { + st = j; + positiveVelSeqSize++; + flag = true; + cout << "In first if positiveSize "< 0 && flag == true) { + positiveVelSeqSize++; + cout << "In second if positive "< maxSeqSize) { + maxSeqSize = positiveVelSeqSize; + startIndex = st; + endIndex = st+positiveVelSeqSize; + cout << "maxseq updated \npositiveSize "< test(4); + test[0]=0; + test[1]=7; + test[2]=0; + test[3]=7; + /*test[4]=5; + test[5]=2; + test[6]=-3; + test[7]=-4; + test[8]=2; + test[9]=3; + */ + int st; + int endIndex; + + largestIncreasingPositiveDotProductSeq(test,st, endIndex ); + for (int i=st; i<=endIndex; i++) { + cout << test[i]<=st; i=i-5) { + c = i; + cout << c << endl; + size++; + } + if ((c-st) != 0) { + cout <<"additional " <