#include"stdafx.h" #include"getdist.h" using namespace std; int getdist(char* filename, int first, int last, int m_t_upper, int m_t_lower, int m_b_upper, int m_b_lower, int left, int right, int manual, int invert, int thres) { int mint=100000,maxt=0; int i, thick, totalnumber=0, size; char txtfilename[512], bmp_file[512]; int MINTHICKNESS = m_b_upper - m_t_lower; int MAXTHICKNESS = m_b_lower - m_t_upper; for(i=first;i<=last;i++) { sprintf(txtfilename,"resultThickness//thick_%s%d.txt", filename,i); ifstream fin(txtfilename,ios::in); if(!fin.is_open()) { char log_txt[512]; sprintf(log_txt, "Get Distance: cannot open file %s.", txtfilename); write_to_log(log_txt); return 1; } fin >> thick; while(!fin.eof()) { if(thick<MINTHICKNESS){ fin >> thick; continue; } if(thick>MAXTHICKNESS){ fin >> thick; continue; } totalnumber++; if(thick<mint) mint=thick; if(thick>maxt) maxt=thick; fin >> thick; } fin.close(); } //open output file ofstream outfile("thickness_distribution.csv",ios::out); if(!outfile.is_open()) { char log_txt[512]; sprintf(log_txt, "Get Distance: cannot open file %s", "Thickness_distribution.csv"); write_to_log(log_txt); return 1; } //sample info sprintf(bmp_file,"%s###.bmp", filename); outfile << "Results file for thickness."<<endl<<endl; outfile << "Sample Name:,," << bmp_file << endl; outfile << "Run Parameters:,,,First Slice," << first << ",,Last Slice,"<< last << endl; outfile << ",,Top:,Upper limit," << m_t_upper << ",,Lower limit," << m_t_lower << endl; outfile << ",,Bottom:,Upper limit," << m_b_upper << ",,Lower limit," << m_b_lower << endl; outfile << ",,,Left limit," << left << ",,Right limit," << right << endl; outfile << ",,,Threshold," << thres; if (manual == 1) outfile << ",(manual),Inverted?,"; else outfile << ",(auto),Inverted?,"; if (invert == 1) outfile << "YES" << endl; else outfile << "NO" << endl; outfile << endl << endl; //if there is data present.. if (totalnumber > 0){ size = maxt - mint + 1; double* dist=(double*)malloc(sizeof(double)*size); if(!dist) { write_to_log("Get Distance: Out of Memory -dist-."); return 2; } for(i=0;i<size;i++) dist[i]=0.0; for(i=first;i<=last;i++) { //char txtfilename[40]; sprintf(txtfilename,"resultThickness//thick_%s%d.txt",filename,i); ifstream fin(txtfilename,ios::in); if(!fin.is_open()) { char log_txt[512]; sprintf(log_txt, "Get Dist Surfpit: cannot open file %s.", txtfilename); write_to_log(log_txt); return 1; } fin >> thick; while(!fin.eof()) { if(thick < MINTHICKNESS){ fin >> thick; continue; } if(thick > MAXTHICKNESS){ fin >> thick; continue; } dist[thick-mint] += 1.0; fin >> thick; } fin.close(); } for(i=0;i<size;i++) dist[i]/=(double)totalnumber; outfile << "Thickness" << "," << "Probability" << endl; // Write to output final result file for(i=0;i<size;i++)//{ //if (size < 1) outfile << (mint+i) << "," << dist[i] << endl; //else{ // outfile << (int)((floor(i*size)+mint)) << //",to," << (int)(mint+floor((1+i)*size)-1) << // "," << dist[i] << endl; //} //} outfile.close(); free(dist); } else{ //error message outfile << "No data present for distribution." << endl; outfile.close(); } return 0; } // // int getdistsurfpit(char* filename, char* outname, char* bmp_file, int sF, int sL, int first, int last, int left, int right, int manual, int invert, int divisions, int thres, int layer) { int mint, maxt; int i, j, totalnumber; double* dist; double size; int pitval; char txtfilename[512]; ofstream fout(outname, ios::out); if(!fout.is_open()) { char log_txt[512]; sprintf(log_txt, "Get Dist Surfpit: cannot open file %s.", outname); write_to_log(log_txt); return 1; } //sample info fout << "Results file for surface pit distributions on "; if (layer == 0) fout << "top layer."<<endl<<endl; else fout << "bottom layer."<<endl<<endl; fout << "Sample Name:,," << bmp_file << endl; fout << "Run Parameters:,,First Slice," << sF << ",,Last Slice,"<< sL << endl; // if (layer == 0) fout << ",,Upper limit," << first << ",,Lower limit," << last << endl;//top fout << ",,Upper limit," << first << ",,Lower limit," << last << endl;//bottom fout << ",,Left limit," << left << ",,Right limit," << right << endl; fout << ",,Threshold," << thres; if (manual == 1) fout << ",(manual),Inverted?,"; else fout << ",(auto),Inverted?,"; if (invert == 1) fout << "YES" << endl; else fout << "NO" << endl; fout << endl << endl; //for each layer.. for(i = 0; i <= (last - first); i++) { fout << "\nLayer " << i << "...\n"; sprintf(txtfilename,"%s%d.txt", filename, i); mint=1000000; maxt=0; ifstream fin(txtfilename,ios::in); if(!fin.is_open()) { char log_txt[512]; sprintf(log_txt, "Get Dist Surfpit: cannot open file %s.", txtfilename); write_to_log(log_txt); return 1; } //find the minimum num and maximum num limits fin>>pitval; totalnumber = 0; while(!fin.eof()) { totalnumber++; if(pitval < mint) mint = pitval; if(pitval > maxt) maxt = pitval; fin>>pitval; } fin.close(); //if numbers exist create dist if ( totalnumber > 0 ){ size = (float)(maxt - mint + 1)/(float)divisions; dist = (double*)malloc(sizeof(double)*divisions); if(!dist) { write_to_log("Get Dist Surfpit: Out of Memory -dist-."); return 2; } for(j=0; j < divisions; j++) dist[j] = 0.0; totalnumber = 0; ifstream fin2(txtfilename,ios::in); if(!fin2.is_open()) { char log_txt[512]; sprintf(log_txt, "Get Dist Surfpit: cannot open file %s.", txtfilename); write_to_log(log_txt); return 1; } fin2>>pitval; while(!fin2.eof()) { totalnumber++; if (size < 1) j = pitval-mint; else { j = (int)floor((pitval-mint+1)/size); if (j == divisions) j--; } if (j < 0){} // cout<<"invalid bucket!"<<endl; else dist[j]+=1.0; fin2>>pitval; } fin2.close(); //print out distribution information fout<< "\nbucket range:," << (double)size << ",,minimum pit length:," << mint << ",maximum pit length:," << maxt << ",total measurements:," << totalnumber << endl; for(j = 0; j < divisions; j++) dist[j]/=(double)totalnumber; for(j = 0; j < divisions; j++) { if (size < 1) fout << (int)(mint + j) << "," << dist[j] << endl; else{ fout << (int)((floor(j*size)+mint)) << //",to," //<< (int)(mint+floor((1+j)*size)-1) << "," << dist[j] << endl; } } free(dist); } else fout << "No data present." << endl; } fout.close(); return 0; }