]> Creatis software - clitk.git/blob - vv/vvLandmarks.cxx
correct bug when comment
[clitk.git] / vv / vvLandmarks.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
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"
19
20 #include <ios>
21 #include <fstream>
22 #include <sstream>
23 #include <string>
24 #include <locale.h>
25
26 #include "vtkPolyData.h"
27 #include "vtkPoints.h"
28 #include "vtkFloatArray.h"
29 #include "vtkPointData.h"
30 #include "clitkCommon.h"
31
32 //--------------------------------------------------------------------
33 vvLandmarks::vvLandmarks(int size)
34 {
35   mLandmarks.resize(0);
36   mFilename = "";
37
38   for (int i = 0; i < size; i++) {
39     vtkPoints *points = vtkPoints::New();
40     mPoints.push_back(points);
41   }
42   mPolyData = vtkPolyData::New();
43   mIds = vtkFloatArray::New();
44 }
45 //--------------------------------------------------------------------
46
47
48 //--------------------------------------------------------------------
49 vvLandmarks::~vvLandmarks()
50 {
51   for (unsigned int i = 0; i < mPoints.size(); i++) {
52     mPoints[i]->Delete();
53   }
54   /*for(unsigned int i = 0; i < mText.size(); i++) {
55     mText[i]->Delete();
56     }*/
57   if (mIds)
58     mIds->Delete();
59   if (mPolyData)
60     mPolyData->Delete();
61 }
62 //--------------------------------------------------------------------
63
64
65 //--------------------------------------------------------------------
66 void vvLandmarks::AddLandmark(float x,float y,float z,float t,double value)
67 {
68   vvLandmark point;
69   point.coordinates[0] = x;
70   point.coordinates[1] = y;
71   point.coordinates[2] = z;
72   point.coordinates[3] = t;
73   point.pixel_value=value;
74   mLandmarks.push_back(point);
75   mPoints[int(t)]->InsertNextPoint(x,y,z);
76
77   /*std::stringstream numberVal;
78     numberVal << (mLandmarks.size()-1);
79     vvLandmarksGlyph *number = vvLandmarksGlyph::New();
80     number->SetText(numberVal.str().c_str());
81     number->BackingOff();
82     mText.push_back(number);*/
83   mIds->InsertNextTuple1(0.55);
84   //mIds->InsertTuple1(mLandmarks.size(),mLandmarks.size());
85   SetTime(int(t));
86 }
87 //--------------------------------------------------------------------
88
89
90 //--------------------------------------------------------------------
91 void vvLandmarks::RemoveLastLandmark()
92 {
93   mPoints[mLandmarks.back().coordinates[3]]->SetNumberOfPoints(
94                                                                mPoints[mLandmarks.back().coordinates[3]]->GetNumberOfPoints()-1);
95   mPolyData->Modified();
96   //mText.pop_back();
97   mLandmarks.pop_back();
98   mIds->RemoveLastTuple();
99 }
100 //--------------------------------------------------------------------
101
102
103 //--------------------------------------------------------------------
104 void vvLandmarks::ChangeComments(int index, std::string comments)
105 {
106   mLandmarks[index].comments = comments;
107 }
108 //--------------------------------------------------------------------
109
110
111 //--------------------------------------------------------------------
112 double vvLandmarks::GetPixelValue(int index)
113 {
114   return mLandmarks[index].pixel_value;
115 }
116 //--------------------------------------------------------------------
117
118
119 //--------------------------------------------------------------------
120 float* vvLandmarks::GetCoordinates(int index)
121 {
122   return mLandmarks[index].coordinates;
123 }
124 //--------------------------------------------------------------------
125
126
127 //--------------------------------------------------------------------
128 std::string vvLandmarks::GetComments(int index)
129 {
130   return mLandmarks[index].comments;
131 }
132 //--------------------------------------------------------------------
133
134
135 //--------------------------------------------------------------------
136 void vvLandmarks::LoadFile(std::string filename)
137 {
138   std::ifstream fp(filename.c_str(), std::ios::in|std::ios::binary);
139   if (!fp.is_open()) {
140     std::cerr <<"Unable to open file " << filename << std::endl;
141     return;
142   }
143   mFilename = filename;
144   mLandmarks.clear();
145   char line[255];
146   for (unsigned int i = 0; i < mPoints.size(); i++)
147     mPoints[i]->SetNumberOfPoints(0);
148   bool first_line=true;
149   while (fp.getline(line,255)) {
150     //    DD(line);
151     std::string stringline = line;
152     if (first_line) {
153       first_line=false;
154       ///New landmark format: first line is "LANDMARKSXX", where XX is the version number
155       if (stringline.size() >= 10 && stringline.compare(0,9,"LANDMARKS")==0) {
156         std::istringstream ss(stringline.c_str()+9);
157         ss >> mFormatVersion;
158         continue; //skip first line
159       } else
160         mFormatVersion=0;
161     }
162     if (stringline.size() > 1) {
163       vvLandmark point;
164       int previousSpace = 0;
165       int space=0;
166       if (mFormatVersion>0) {
167         space = stringline.find(" ", previousSpace+1);
168         if (space < -1 || space > (int)stringline.size()) {
169           ErrorMsg(mLandmarks.size(),"index");
170           continue;
171         }
172         //int index = atoi(stringline.substr(previousSpace,space - previousSpace).c_str());
173         previousSpace = space;
174       }
175       space = stringline.find(" ", previousSpace+1);
176       if (space < -1 || space > (int)stringline.size()) {
177         ErrorMsg(mLandmarks.size(),"x position");
178         continue;
179       }
180       point.coordinates[0] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
181       //      DD(point.coordinates[0]);
182       previousSpace = space;
183       space = stringline.find(" ", previousSpace+1);
184       if (space < -1 || space > (int)stringline.size()) {
185         ErrorMsg(mLandmarks.size(),"y position");
186         continue;
187       }
188       point.coordinates[1] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
189       //      DD(point.coordinates[1]);
190       previousSpace = space;
191       space = stringline.find(" ", previousSpace+1);
192       if (space < -1 || space > (int)stringline.size()) {
193         ErrorMsg(mLandmarks.size(),"z position");
194         continue;
195       }
196       point.coordinates[2] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
197       previousSpace = space;
198       if (mFormatVersion>0) {
199         space = stringline.find(" ", previousSpace+1);
200         if (space < -1 || space > (int)stringline.size()) {
201           ErrorMsg(mLandmarks.size(),"t position");
202           continue;
203         }
204         point.coordinates[3] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
205         previousSpace = space;
206         space = stringline.find(" ", previousSpace+1);
207         if (space < -1 || space > (int)stringline.size()) {
208           ErrorMsg(mLandmarks.size(),"pixel value");
209           continue;
210         }
211         point.pixel_value = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
212         //        DD(point.pixel_value);
213       } else {
214         point.pixel_value=0.; //Not in file
215         point.coordinates[3]=0.;
216       }
217       previousSpace = space;
218       //this is the maximum size of comments
219       space = (stringline.find("\n", previousSpace+1) < 254 ? stringline.find("\n", previousSpace+1) : 254);
220       if (previousSpace != -1) {
221         point.comments = stringline.substr(previousSpace,space - (previousSpace)).c_str();
222       }
223       //      DD(point.comments);
224       mLandmarks.push_back(point);
225       mIds->InsertNextTuple1(0.55);
226       mPoints[int(point.coordinates[3])]->InsertNextPoint(
227                                                           point.coordinates[0],point.coordinates[1],point.coordinates[2]);
228     }
229   }
230   SetTime(0);
231 }
232 //--------------------------------------------------------------------
233
234
235 //--------------------------------------------------------------------
236 bool vvLandmarks::ErrorMsg(int num,const char * text)
237 {
238   std::cerr << "error when loading point " << num << " at " << text << std::endl;
239   return false;
240 }
241 //--------------------------------------------------------------------
242
243
244 //--------------------------------------------------------------------
245 void vvLandmarks::SaveFile(std::string filename)
246 {
247   std::string fileContent = "LANDMARKS1\n"; //File format version identification
248   for (unsigned int i = 0; i < mLandmarks.size(); i++) {
249     std::stringstream out;
250     out.imbue(std::locale("C")); //This is to specify that the dot is to be used as the decimal separator
251     out << i << " "
252         << mLandmarks[i].coordinates[0] << " "
253         << mLandmarks[i].coordinates[1] << " "
254         << mLandmarks[i].coordinates[2] << " "
255         << mLandmarks[i].coordinates[3] << " "
256         << mLandmarks[i].pixel_value << " ";
257     fileContent += out.str();
258     if (mLandmarks[i].comments.size() == 0)
259       fileContent += " ";
260     else
261       fileContent += mLandmarks[i].comments;
262     fileContent += "\n";
263   }
264   std::ofstream fp(filename.c_str(), std::ios::trunc);
265   if ( !fp ) {
266     std::cerr << "Unable to open file" << std::endl;
267     return;
268   }
269   fp << fileContent.c_str()<< std::endl;
270   fp.close();
271 }
272 //--------------------------------------------------------------------
273
274
275 //--------------------------------------------------------------------
276 void vvLandmarks::SetTime(int time)
277 {
278   if (time >= 0 && time <= ((int)mPoints.size() -1)) {
279     mPolyData->SetPoints(mPoints[time]);
280     mPolyData->GetPointData()->SetScalars(mIds);
281     mPolyData->Modified();
282     mPolyData->Update();
283   }
284 }
285 //--------------------------------------------------------------------
286
287
288 //--------------------------------------------------------------------
289 std::string vvLandmarks::replace_dots(std::string input)
290 {
291   ///Replaces the dots used in the file with the decimal separator in use on the platform
292   lconv * conv=localeconv();
293   unsigned int position = input.find( "." );
294   while ( position < input.size() ) {
295     input.replace(position, 1, conv->decimal_point);
296     position = input.find( ".", position + 1 );
297   }
298   return input;
299 }
300 //--------------------------------------------------------------------