1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #include "vvLandmarks.h"
26 #include "vtkPolyData.h"
27 #include "vtkPoints.h"
28 #include "vtkFloatArray.h"
29 #include "vtkPointData.h"
30 #include "clitkCommon.h"
32 vvLandmarks::vvLandmarks(int size)
37 for (int i = 0; i < size; i++) {
38 vtkPoints *points = vtkPoints::New();
39 mPoints.push_back(points);
41 mPolyData = vtkPolyData::New();
42 mIds = vtkFloatArray::New();
45 vvLandmarks::~vvLandmarks()
47 for (unsigned int i = 0; i < mPoints.size(); i++) {
50 /*for(unsigned int i = 0; i < mText.size(); i++) {
59 void vvLandmarks::AddLandmark(float x,float y,float z,float t,double value)
62 point.coordinates[0] = x;
63 point.coordinates[1] = y;
64 point.coordinates[2] = z;
65 point.coordinates[3] = t;
66 point.pixel_value=value;
67 mLandmarks.push_back(point);
68 mPoints[int(t)]->InsertNextPoint(x,y,z);
70 /*std::stringstream numberVal;
71 numberVal << (mLandmarks.size()-1);
72 vvLandmarksGlyph *number = vvLandmarksGlyph::New();
73 number->SetText(numberVal.str().c_str());
75 mText.push_back(number);*/
76 mIds->InsertNextTuple1(0.55);
77 //mIds->InsertTuple1(mLandmarks.size(),mLandmarks.size());
81 void vvLandmarks::RemoveLastLandmark()
83 mPoints[mLandmarks.back().coordinates[3]]->SetNumberOfPoints(
84 mPoints[mLandmarks.back().coordinates[3]]->GetNumberOfPoints()-1);
85 mPolyData->Modified();
87 mLandmarks.pop_back();
88 mIds->RemoveLastTuple();
91 void vvLandmarks::ChangeComments(int index, std::string comments)
93 mLandmarks[index].comments = comments;
96 double vvLandmarks::GetPixelValue(int index)
98 return mLandmarks[index].pixel_value;
101 float* vvLandmarks::GetCoordinates(int index)
103 return mLandmarks[index].coordinates;
106 std::string vvLandmarks::GetComments(int index)
108 return mLandmarks[index].comments;
111 void vvLandmarks::LoadFile(std::string filename)
113 std::ifstream fp(filename.c_str(), std::ios::in|std::ios::binary);
115 std::cerr <<"Unable to open file " << filename << std::endl;
118 mFilename = filename;
121 for (unsigned int i = 0; i < mPoints.size(); i++)
122 mPoints[i]->SetNumberOfPoints(0);
123 bool first_line=true;
124 while (fp.getline(line,255)) {
125 std::string stringline = line;
128 ///New landmark format: first line is "LANDMARKSXX", where XX is the version number
129 if (stringline.size() >= 10 && stringline.compare(0,9,"LANDMARKS")==0) {
130 std::istringstream ss(stringline.c_str()+9);
131 ss >> mFormatVersion;
132 continue; //skip first line
136 if (stringline.size() > 1) {
138 int previousSpace = 0;
140 if (mFormatVersion>0) {
141 space = stringline.find(" ", previousSpace+1);
142 if (space < -1 || space > (int)stringline.size()) {
143 ErrorMsg(mLandmarks.size(),"index");
146 //int index = atoi(stringline.substr(previousSpace,space - previousSpace).c_str());
147 previousSpace = space;
149 space = stringline.find(" ", previousSpace+1);
150 if (space < -1 || space > (int)stringline.size()) {
151 ErrorMsg(mLandmarks.size(),"x position");
154 point.coordinates[0] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
155 previousSpace = space;
156 space = stringline.find(" ", previousSpace+1);
157 if (space < -1 || space > (int)stringline.size()) {
158 ErrorMsg(mLandmarks.size(),"y position");
161 point.coordinates[1] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
162 previousSpace = space;
163 space = stringline.find(" ", previousSpace+1);
164 if (space < -1 || space > (int)stringline.size()) {
165 ErrorMsg(mLandmarks.size(),"z position");
168 point.coordinates[2] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
169 previousSpace = space;
170 if (mFormatVersion>0) {
171 space = stringline.find(" ", previousSpace+1);
172 if (space < -1 || space > (int)stringline.size()) {
173 ErrorMsg(mLandmarks.size(),"t position");
176 point.coordinates[3] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
177 previousSpace = space;
178 space = stringline.find(" ", previousSpace+1);
179 if (space < -1 || space > (int)stringline.size()) {
180 ErrorMsg(mLandmarks.size(),"pixel value");
183 point.pixel_value = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
185 point.pixel_value=0.; //Not in file
186 point.coordinates[3]=0.;
188 previousSpace = space;
189 //this is the maximum size of comments
190 space = (stringline.find("\n", previousSpace+1) < 254 ? stringline.find("\n", previousSpace+1) : 254);
191 point.comments = stringline.substr(previousSpace,space - (previousSpace)).c_str();
192 mLandmarks.push_back(point);
193 mIds->InsertNextTuple1(0.55);
194 mPoints[int(point.coordinates[3])]->InsertNextPoint(
195 point.coordinates[0],point.coordinates[1],point.coordinates[2]);
201 bool vvLandmarks::ErrorMsg(int num,const char * text)
203 std::cerr << "error when loading point " << num << " at " << text << std::endl;
207 void vvLandmarks::SaveFile(std::string filename)
209 std::string fileContent = "LANDMARKS1\n"; //File format version identification
210 for (unsigned int i = 0; i < mLandmarks.size(); i++) {
211 std::stringstream out;
212 out.imbue(std::locale("C")); //This is to specify that the dot is to be used as the decimal separator
214 << mLandmarks[i].coordinates[0] << " "
215 << mLandmarks[i].coordinates[1] << " "
216 << mLandmarks[i].coordinates[2] << " "
217 << mLandmarks[i].coordinates[3] << " "
218 << mLandmarks[i].pixel_value << " ";
219 fileContent += out.str();
220 if (mLandmarks[i].comments.size() == 0)
223 fileContent += mLandmarks[i].comments;
226 std::ofstream fp(filename.c_str(), std::ios::trunc);
228 std::cerr << "Unable to open file" << std::endl;
231 fp << fileContent.c_str()<< std::endl;
235 void vvLandmarks::SetTime(int time)
237 if (time >= 0 && time <= ((int)mPoints.size() -1)) {
238 mPolyData->SetPoints(mPoints[time]);
239 mPolyData->GetPointData()->SetScalars(mIds);
240 mPolyData->Modified();
245 std::string vvLandmarks::replace_dots(std::string input)
247 ///Replaces the dots used in the file with the decimal separator in use on the platform
248 lconv * conv=localeconv();
249 unsigned int position = input.find( "." );
250 while ( position < input.size() ) {
251 input.replace(position, 1, conv->decimal_point);
252 position = input.find( ".", position + 1 );