#include"stdafx.h" #include"getsurface.h" using namespace std; void getpeak(List<headtail>* result,int* high,int* low, int HIGHEST1, int LOWEST1, int HIGHEST2, int LOWEST2) { int peak1=1000,peak2=0; int i; for(i=result->getNumber();i>=1;i--) { if(result->getWhich(i)->head.y<HIGHEST1) continue; if(result->getWhich(i)->head.y>LOWEST1) continue; if(result->getWhich(i)->head.y<peak1) peak1=result->getWhich(i)->head.y; } for(i=result->getNumber();i>=1;i--) { if(result->getWhich(i)->tail.y<HIGHEST2) continue; if(result->getWhich(i)->tail.y>LOWEST2) continue; if(result->getWhich(i)->tail.y>peak2) peak2=result->getWhich(i)->tail.y; } *high=peak1; *low=peak2; } int getsurhigh(List<headtail>* result,int peak1,int peak2,char* outputname, int HIGHEST1, int LOWEST1, int HIGHEST2, int LOWEST2) { ofstream fout(outputname,ios::out|ios::app); if(!fout.is_open()) { char log_txt[512]; sprintf(log_txt, "Get Surhigh: cannot open file %s.", outputname); write_to_log(log_txt); return 1; } for(int i=result->getNumber();i>=1;i--) { if(result->getWhich(i)->head.y<HIGHEST1) continue; if(result->getWhich(i)->head.y>LOWEST1) continue; if(result->getWhich(i)->tail.y<HIGHEST2) continue; if(result->getWhich(i)->tail.y>LOWEST2) continue; int height1=result->getWhich(i)->head.y-peak1; int height2=peak2-result->getWhich(i)->tail.y; fout<<height1<<" "<<height2<<endl; } fout.close(); return 0; } int getsurdist(char* inputname, char* outputname, char*bmp_file, 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) { ifstream fin1(inputname,ios::in); if(!fin1.is_open()) { char log_txt[512]; sprintf(log_txt, "Get SurDist: cannot open file %s.", inputname); write_to_log(log_txt); return 1; } int min1=1000,min2=1000,max1=0,max2=0; while(!fin1.eof()) { int number1,number2; fin1>>number1>>number2; if(number1<min1) min1=number1; if(number1>max1) max1=number1; if(number2<min2) min2=number2; if(number2>max2) max2=number2; } fin1.close(); double* frontside=(double*)malloc(sizeof(double)*(max1-min1+1)); if(!frontside) { write_to_log("Get SurDist: Out of Memory -frontside-!"); return 2; } double* backside=(double*)malloc(sizeof(double)*(max2-min2+1)); if(!backside) { write_to_log("Get SurDist: Out of Memory -backside-!"); return 2; } int i; for(i=0;i<max1-min1+1;i++) frontside[i]=0.0; for(i=0;i<max2-min2+1;i++) backside[i]=0.0; ifstream fin2(inputname,ios::in); if(!fin2.is_open()) { char log_txt[512]; sprintf(log_txt, "Get SurDist: cannot open file %s.", inputname); write_to_log(log_txt); return 1; } ofstream fout(outputname,ios::out); if(!fout.is_open()) { char log_txt[512]; sprintf(log_txt, "Get SurDist: cannot open file %s.", outputname); write_to_log(log_txt); return 1; } //sample info fout << "Results file for surface volume distribution."<<endl<<endl; fout << "Sample Name:,," << bmp_file << endl; fout << "Run Parameters:,,,First Slice," << first << ",,Last Slice,"<< last << endl; fout << ",,Top:,Upper limit," << m_t_upper << ",,Lower limit," << m_t_lower << endl; fout << ",,Bottom:,Upper limit," << m_b_upper << ",,Lower limit," << m_b_lower << endl; 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; int totalnumber=0; while(!fin2.eof()) { int number1,number2; fin2>>number1>>number2; totalnumber++; frontside[number1-min1]+=1.0; backside[number2-min2]+=1.0; } fin2.close(); for(i=0;i<max1-min1+1;i++) frontside[i]/=(double)totalnumber; for(i=0;i<max2-min2+1;i++) backside[i]/=(double)totalnumber; fout<<"\nFrontside surface volume distribution."<<endl; for(i=0;i<max1-min1+1;i++) fout<<min1+i<<","<<frontside[i]<<endl; fout<<endl<<"Backside surface volume distribution."<<endl; for(i=0;i<max2-min2+1;i++) fout<<min2+i<<","<<backside[i]<<endl; fout.close(); free(frontside); free(backside); return 0; } double getcontactpit(char* samplename,int from,int to,int clevel,char* pitname, bool top, int threshold, int left, int right, int high1, int high2, int low1, int low2) { int i; ofstream fout(pitname,ios::out|ios::app); if(!fout.is_open()) { char log_txt[512]; sprintf(log_txt, "Get Contact Pit: cannot open file %s.", pitname); write_to_log(log_txt); exit(1); } char bmpname[512]; int totallength=0; int contact=0; int pit=0; for(i=from;i<=to;i++) { //cout<<"Getting from "<<i<<" slice"; sprintf(bmpname,"%s%d.bmp",samplename,i); List<headtail>* result; result=getboundary(bmpname, threshold, left, right); if(result->getNumber()<=100) { // cout<<endl; continue; } int localpeak1,localpeak2; getpeak(result,&localpeak1,&localpeak2,high1,high2,low1,low2); if(top) { int level=localpeak1+clevel; for(int j=result->getNumber();j>=1;j--) { totallength++; if(result->getWhich(j)->head.y<level) contact++; else { if(result->getWhich(j)->head.y>level) pit++; else { if(pit==0) continue; else { if (pit < (right - left)) fout<<pit<<endl; pit=0; } } } } } else { int level=localpeak2-clevel; for(int j=result->getNumber();j>=1;j--) { totallength++; if(result->getWhich(j)->tail.y>level) contact++; else { if(result->getWhich(j)->tail.y<level) pit++; else { if(pit==0) continue; else { if (pit < (right - left)) fout<<pit<<endl; pit=0; } } } } } result->Clear(); // cout<<"done!"<<endl; } fout.close(); double enter=(double)contact/(double)totallength; return enter; }