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