#include "bbUtilitiesCatAxis.h" #include "bbUtilitiesPackage.h" namespace bbUtilities { BBTK_ADD_BLACK_BOX_TO_PACKAGE(Utilities,CatAxis) //BBTK_USER_BLACK_BOX_IMPLEMENTATION(CatAxis,bbtk::AtomicBlackBox); BBTK_BLACK_BOX_IMPLEMENTATION(CatAxis,bbtk::AtomicBlackBox); void CatAxis::Process() { std::string fileNameAxis= bbGetInputFileNameAxis(); std::string fileManualPoints = bbGetInputFileNameManualPoints(); std::string fileStartPoint = "pointS.txt"; std::string dir = bbGetInputDSDirectory(); int vessel = bbGetInputVessel(); std::vector vectx = bbGetInputPointsX(); std::vector vecty = bbGetInputPointsY(); std::vector vectz = bbGetInputPointsZ(); std::vector mp = bbGetInputManualPoint(); vtkImageData* img= bbGetInputImage(); double* spc = img->GetSpacing(); std::stringstream ss; std::string vesselStr; ss << vessel; ss >> vesselStr; std::string path = dir+ "/" + "vessel" + vesselStr + "/" + fileStartPoint; int sizex=vectx.size(); int sizey=vecty.size(); int sizez=vectz.size(); if(sizex==sizey && sizex==sizez && sizey==sizez && sizex >0 && sizey >0 && sizez >0) { double xs=0, ys=0, zs=0; if(getPointFile(&xs, &ys, &zs,path)){ double dist = sqrt(pow(xs, 2)+pow(ys, 2)+pow(zs, 2)); double x0=vectx[0]*spc[0], y0=vecty[0]*spc[1], z0=vectz[0]*spc[2]; double x1=vectx[vectx.size()-1]*spc[0], y1=vecty[vectx.size()-1]*spc[1], z1=vectz[vectx.size()-1]*spc[2]; double dist1 = sqrt(pow(x0-xs, 2)+pow(y0-ys, 2)+pow(z0-zs, 2)); double dist2 = sqrt(pow(x1-xs, 2)+pow(y1-ys, 2)+pow(z1-zs, 2)); if(dist1 > dist2) { invertAxis(&vectx, &vecty, &vectz); } } path = dir+ "/" + "vessel" + vesselStr + "/" + fileNameAxis; writeAxisFile(path, vectx, vecty, vectz, spc); path = dir+ "/" + "vessel" + vesselStr + "/" + fileManualPoints; writePointsFile(path, mp, spc); } } void CatAxis::writeAxisFile(std::string path, std::vector vectx, std::vector vecty, std::vector vectz, double* spc){ std::ofstream file1; file1.open( path.c_str() ); if(file1.is_open()) { for (int i = 0; i< vectx.size() ;i++) { double pointx=vectx[i]*spc[0]; double pointy=vecty[i]*spc[1]; double pointz=vectz[i]*spc[2]; if(i==(vectx.size()-1)) file1 << pointx << " " << pointy << " " << pointz; else{ file1 << pointx << " " << pointy << " " << pointz<< std::endl; } } file1.close(); } } void CatAxis::writePointsFile(std::string path, std::vector mp, double* spc){ std::ofstream file2; file2.open( path.c_str(), std::ios::out|std::ios::app|std::ios::ate); if(file2.is_open()) { double pointx=mp[0]*spc[0]; double pointy=mp[1]*spc[1]; double pointz=mp[2]*spc[2]; file2 << pointx << " " << pointy << " " << pointz << std::endl; } file2.close(); } void CatAxis::invertAxis(std::vector* vectx,std::vector* vecty,std::vector* vectz){ double temp; for(int i = 0; i <= vectx->size()/2; i++){ temp = (*vectx)[i]; (*vectx)[i] = (*vectx)[vectx->size()-1-i]; (*vectx)[vectx->size()-1-i]=temp; temp = (*vecty)[i]; (*vecty)[i] = (*vecty)[vectx->size()-1-i]; (*vecty)[vecty->size()-1-i]=temp; temp = (*vectz)[i]; (*vectz)[i] = (*vectz)[vectx->size()-1-i]; (*vectz)[vectx->size()-1-i]=temp; } } bool CatAxis::getPointFile(double* x, double* y, double* z, std::string path){ std::ifstream file; file.open( path.c_str() ); std::string line; if(file.is_open()){ std::getline(file,line); int pos=line.find(" "); int pos2=line.find_last_of(" "); int size=line.size(); std::string xs=line.substr(0,pos); std::string ys=line.substr(pos+1,pos2); std::string zs=line.substr(pos2+1,size); *x=atof(xs.c_str()); *y=atof(ys.c_str()); *z=atof(zs.c_str()); file.close(); return true; } return false; } void CatAxis::bbUserSetDefaultValues() { } void CatAxis::bbUserInitializeProcessing() { } void CatAxis::bbUserFinalizeProcessing() { } } // EO namespace bbUtilities