X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=vv%2FvvLandmarks.cxx;h=94fb9756b93918ca20349a1eb1ce96baf6b35406;hb=938771d6a7ece6b97623fef4fb15beff256497da;hp=f092729606f6b9a88d22d7caac6232b7c6f45818;hpb=2891992b085ed775e4983a09baa794b1c0d3583e;p=clitk.git diff --git a/vv/vvLandmarks.cxx b/vv/vvLandmarks.cxx index f092729..94fb975 100644 --- a/vv/vvLandmarks.cxx +++ b/vv/vvLandmarks.cxx @@ -3,7 +3,7 @@ Authors belong to: - University of LYON http://www.universite-lyon.fr/ - - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + - Léon Bérard cancer center http://www.centreleonberard.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr This software is distributed WITHOUT ANY WARRANTY; without even @@ -14,7 +14,7 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html - ======================================================================-====*/ + ===========================================================================**/ #include "vvLandmarks.h" #include @@ -28,7 +28,9 @@ #include "vtkFloatArray.h" #include "vtkPointData.h" #include "clitkCommon.h" +#include +//-------------------------------------------------------------------- vvLandmarks::vvLandmarks(int size) { mLandmarks.resize(0); @@ -40,8 +42,13 @@ vvLandmarks::vvLandmarks(int size) } mPolyData = vtkPolyData::New(); mIds = vtkFloatArray::New(); + mLabels = vtkStringArray::New(); + mLabels->SetName("labels"); } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- vvLandmarks::~vvLandmarks() { for (unsigned int i = 0; i < mPoints.size(); i++) { @@ -54,74 +61,151 @@ vvLandmarks::~vvLandmarks() mIds->Delete(); if (mPolyData) mPolyData->Delete(); + if (mLabels) + mLabels->Delete(); } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- void vvLandmarks::AddLandmark(float x,float y,float z,float t,double value) { vvLandmark point; + vtkIdType idPoint; point.coordinates[0] = x; point.coordinates[1] = y; point.coordinates[2] = z; point.coordinates[3] = t; point.pixel_value=value; mLandmarks.push_back(point); - mPoints[int(t)]->InsertNextPoint(x,y,z); - - /*std::stringstream numberVal; - numberVal << (mLandmarks.size()-1); - vvLandmarksGlyph *number = vvLandmarksGlyph::New(); - number->SetText(numberVal.str().c_str()); - number->BackingOff(); - mText.push_back(number);*/ + + idPoint = mPoints[int(t)]->InsertNextPoint(x,y,z); + std::string str_vtkIdType; // string which will contain the result + std::ostringstream convert; // stream used for the conversion + convert << idPoint; // insert the textual representation of 'idPoint' in the characters in the stream + str_vtkIdType = convert.str(); // set 'str_vtkIdType' to the contents of the stream + mLabels->InsertNextValue(str_vtkIdType.c_str()); + + std::stringstream numberVal; + numberVal << (mLandmarks.size()-1); + /* + vvLandmarksGlyph *number = vvLandmarksGlyph::New(); + number->SetText(numberVal.str().c_str()); + number->BackingOff(); + DD(numberVal.str().c_str()); + mText.push_back(number); + */ + mIds->InsertNextTuple1(0.55); //mIds->InsertTuple1(mLandmarks.size(),mLandmarks.size()); SetTime(int(t)); } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- void vvLandmarks::RemoveLastLandmark() { mPoints[mLandmarks.back().coordinates[3]]->SetNumberOfPoints( mPoints[mLandmarks.back().coordinates[3]]->GetNumberOfPoints()-1); - mPolyData->Modified(); - //mText.pop_back(); + // mText.pop_back(); mLandmarks.pop_back(); mIds->RemoveLastTuple(); + mLabels->SetNumberOfValues(mLabels->GetNumberOfValues()-1); + mLabels->Modified(); + mPolyData->Modified(); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void vvLandmarks::RemoveLandmark(int index) +{ + // erase a vtkPoint by shifiting the array . + // not a problem here because there are no + // pologyons linking the points + int npoints = mPoints[mLandmarks[index].coordinates[3]]->GetNumberOfPoints(); + int t = mLandmarks[index].coordinates[3]; + for (int i = index; i < npoints - 1; i++) { + mPoints[t]->InsertPoint(i, mPoints[t]->GetPoint(i+1)); + std::string str_i; // string which will contain the result + std::ostringstream convert; // stream used for the conversion + convert << i; // insert the textual representation of 'i' in the characters in the stream + str_i = convert.str(); // set 'str_i' to the contents of the stream + mLabels->SetValue(i,str_i.c_str()); + } + mPoints[t]->SetNumberOfPoints(npoints-1); + mLabels->SetNumberOfValues(npoints-1); + mLabels->Modified(); + mPolyData->Modified(); + + mLandmarks.erase(mLandmarks.begin() + index); + mIds->RemoveLastTuple(); } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- void vvLandmarks::ChangeComments(int index, std::string comments) { mLandmarks[index].comments = comments; } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- double vvLandmarks::GetPixelValue(int index) { return mLandmarks[index].pixel_value; } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- float* vvLandmarks::GetCoordinates(int index) { return mLandmarks[index].coordinates; } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- std::string vvLandmarks::GetComments(int index) { return mLandmarks[index].comments; } +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +bool vvLandmarks::LoadFile(std::string filename) +{ + std::string extension = itksys::SystemTools::GetFilenameExtension(filename); + if (extension == ".txt") + return LoadTxtFile(filename); + else if (extension == ".pts") + return LoadPtsFile(filename); + + return false; +} -void vvLandmarks::LoadFile(std::string filename) +//-------------------------------------------------------------------- +bool vvLandmarks::LoadTxtFile(std::string filename) { std::ifstream fp(filename.c_str(), std::ios::in|std::ios::binary); if (!fp.is_open()) { std::cerr <<"Unable to open file " << filename << std::endl; - return; + return false; } mFilename = filename; mLandmarks.clear(); + vtkIdType idPoint; char line[255]; for (unsigned int i = 0; i < mPoints.size(); i++) mPoints[i]->SetNumberOfPoints(0); bool first_line=true; while (fp.getline(line,255)) { + // DD(line); std::string stringline = line; if (first_line) { first_line=false; @@ -152,6 +236,7 @@ void vvLandmarks::LoadFile(std::string filename) continue; } point.coordinates[0] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str()); + // DD(point.coordinates[0]); previousSpace = space; space = stringline.find(" ", previousSpace+1); if (space < -1 || space > (int)stringline.size()) { @@ -159,6 +244,7 @@ void vvLandmarks::LoadFile(std::string filename) continue; } point.coordinates[1] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str()); + // DD(point.coordinates[1]); previousSpace = space; space = stringline.find(" ", previousSpace+1); if (space < -1 || space > (int)stringline.size()) { @@ -181,6 +267,7 @@ void vvLandmarks::LoadFile(std::string filename) continue; } point.pixel_value = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str()); + // DD(point.pixel_value); } else { point.pixel_value=0.; //Not in file point.coordinates[3]=0.; @@ -188,22 +275,129 @@ void vvLandmarks::LoadFile(std::string filename) previousSpace = space; //this is the maximum size of comments space = (stringline.find("\n", previousSpace+1) < 254 ? stringline.find("\n", previousSpace+1) : 254); - point.comments = stringline.substr(previousSpace,space - (previousSpace)).c_str(); + if (previousSpace != -1) { + point.comments = stringline.substr(previousSpace,space - (previousSpace)).c_str(); + } + // DD(point.comments); mLandmarks.push_back(point); mIds->InsertNextTuple1(0.55); - mPoints[int(point.coordinates[3])]->InsertNextPoint( + idPoint = mPoints[int(point.coordinates[3])]->InsertNextPoint( point.coordinates[0],point.coordinates[1],point.coordinates[2]); + std::string str_vtkIdType; // string which will contain the result + std::ostringstream convert; // stream used for the conversion + convert << idPoint; // insert the textual representation of 'idPoint' in the characters in the stream + str_vtkIdType = convert.str(); // set 'str_vtkIdType' to the contents of the stream + mLabels->InsertNextValue(str_vtkIdType.c_str()); } } SetTime(0); + + return true; } +//-------------------------------------------------------------------- +//-------------------------------------------------------------------- +bool vvLandmarks::LoadPtsFile(std::string filename) +{ + std::ifstream fp(filename.c_str(), std::ios::in|std::ios::binary); + if (!fp.is_open()) { + std::cerr <<"Unable to open file " << filename << std::endl; + return false; + } + mFilename = filename; + mLandmarks.clear(); + vtkIdType idPoint; + char line[255]; + for (unsigned int i = 0; i < mPoints.size(); i++) + mPoints[i]->SetNumberOfPoints(0); + bool first_line=true; + while (fp.getline(line,255)) { + // DD(line); + std::string stringline = line; + if (stringline.size() > 1) { + vvLandmark point; + int previousSpace = 0; + int space=0; + + if (stringline[0] == '#') // comments + continue; + + space = stringline.find("\t", previousSpace+1); + if (space < -1 || space > (int)stringline.size()) { + ErrorMsg(mLandmarks.size(),"x position"); + continue; + } + point.coordinates[0] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str()); + // DD(point.coordinates[0]); + previousSpace = space; + space = stringline.find("\t", previousSpace+1); + if (space < -1 || space > (int)stringline.size()) { + ErrorMsg(mLandmarks.size(),"y position"); + continue; + } + point.coordinates[1] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str()); + // DD(point.coordinates[1]); + previousSpace = space; + space = stringline.find("\t", previousSpace+1); + if (space < -1 || space > (int)stringline.size()) { + ErrorMsg(mLandmarks.size(),"z position"); + continue; + } + point.coordinates[2] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str()); + previousSpace = space; + if (mFormatVersion>0) { + space = stringline.find("\t", previousSpace+1); + if (space < -1 || space > (int)stringline.size()) { + ErrorMsg(mLandmarks.size(),"t position"); + continue; + } + point.coordinates[3] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str()); + previousSpace = space; + space = stringline.find("\t", previousSpace+1); + if (space < -1 || space > (int)stringline.size()) { + ErrorMsg(mLandmarks.size(),"pixel value"); + continue; + } + point.pixel_value = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str()); + // DD(point.pixel_value); + } else { + point.pixel_value=0.; //Not in file + point.coordinates[3]=0.; + } + previousSpace = space; + //this is the maximum size of comments + space = (stringline.find("\n", previousSpace+1) < 254 ? stringline.find("\n", previousSpace+1) : 254); + if (previousSpace != -1) { + point.comments = stringline.substr(previousSpace,space - (previousSpace)).c_str(); + } + // DD(point.comments); + mLandmarks.push_back(point); + mIds->InsertNextTuple1(0.55); + idPoint = mPoints[int(point.coordinates[3])]->InsertNextPoint( + point.coordinates[0],point.coordinates[1],point.coordinates[2]); + std::string str_vtkIdType; // string which will contain the result + std::ostringstream convert; // stream used for the conversion + convert << idPoint; // insert the textual representation of 'idPoint' in the characters in the stream + str_vtkIdType = convert.str(); // set 'str_vtkIdType' to the contents of the stream + mLabels->InsertNextValue(str_vtkIdType.c_str()); + } + } + SetTime(0); + + return true; +} +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- bool vvLandmarks::ErrorMsg(int num,const char * text) { std::cerr << "error when loading point " << num << " at " << text << std::endl; return false; } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- void vvLandmarks::SaveFile(std::string filename) { std::string fileContent = "LANDMARKS1\n"; //File format version identification @@ -231,17 +425,24 @@ void vvLandmarks::SaveFile(std::string filename) fp << fileContent.c_str()<< std::endl; fp.close(); } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- void vvLandmarks::SetTime(int time) { if (time >= 0 && time <= ((int)mPoints.size() -1)) { mPolyData->SetPoints(mPoints[time]); mPolyData->GetPointData()->SetScalars(mIds); + mPolyData->GetPointData()->AddArray(mLabels); mPolyData->Modified(); mPolyData->Update(); } } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- std::string vvLandmarks::replace_dots(std::string input) { ///Replaces the dots used in the file with the decimal separator in use on the platform @@ -253,3 +454,4 @@ std::string vvLandmarks::replace_dots(std::string input) } return input; } +//--------------------------------------------------------------------