]> Creatis software - clitk.git/blob - vv/vvLandmarks.cxx
remove debug
[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 vvLandmarks::vvLandmarks(int size)
33 {
34   mLandmarks.resize(0);
35   mFilename = "";
36
37   for (int i = 0; i < size; i++) {
38     vtkPoints *points = vtkPoints::New();
39     mPoints.push_back(points);
40   }
41   mPolyData = vtkPolyData::New();
42   mIds = vtkFloatArray::New();
43 }
44
45 vvLandmarks::~vvLandmarks()
46 {
47   for (unsigned int i = 0; i < mPoints.size(); i++) {
48     mPoints[i]->Delete();
49   }
50   /*for(unsigned int i = 0; i < mText.size(); i++) {
51     mText[i]->Delete();
52     }*/
53   if (mIds)
54     mIds->Delete();
55   if (mPolyData)
56     mPolyData->Delete();
57 }
58
59 void vvLandmarks::AddLandmark(float x,float y,float z,float t,double value)
60 {
61   vvLandmark point;
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);
69
70   /*std::stringstream numberVal;
71     numberVal << (mLandmarks.size()-1);
72     vvLandmarksGlyph *number = vvLandmarksGlyph::New();
73     number->SetText(numberVal.str().c_str());
74     number->BackingOff();
75     mText.push_back(number);*/
76   mIds->InsertNextTuple1(0.55);
77   //mIds->InsertTuple1(mLandmarks.size(),mLandmarks.size());
78   SetTime(int(t));
79 }
80
81 void vvLandmarks::RemoveLastLandmark()
82 {
83   mPoints[mLandmarks.back().coordinates[3]]->SetNumberOfPoints(
84                                                                mPoints[mLandmarks.back().coordinates[3]]->GetNumberOfPoints()-1);
85   mPolyData->Modified();
86   //mText.pop_back();
87   mLandmarks.pop_back();
88   mIds->RemoveLastTuple();
89 }
90
91 void vvLandmarks::ChangeComments(int index, std::string comments)
92 {
93   mLandmarks[index].comments = comments;
94 }
95
96 double vvLandmarks::GetPixelValue(int index)
97 {
98   return mLandmarks[index].pixel_value;
99 }
100
101 float* vvLandmarks::GetCoordinates(int index)
102 {
103   return mLandmarks[index].coordinates;
104 }
105
106 std::string vvLandmarks::GetComments(int index)
107 {
108   return mLandmarks[index].comments;
109 }
110
111 void vvLandmarks::LoadFile(std::string filename)
112 {
113   std::ifstream fp(filename.c_str(), std::ios::in|std::ios::binary);
114   if (!fp.is_open()) {
115     std::cerr <<"Unable to open file " << filename << std::endl;
116     return;
117   }
118   mFilename = filename;
119   mLandmarks.clear();
120   char line[255];
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;
126     if (first_line) {
127       first_line=false;
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
133       } else
134         mFormatVersion=0;
135     }
136     if (stringline.size() > 1) {
137       vvLandmark point;
138       int previousSpace = 0;
139       int space=0;
140       if (mFormatVersion>0) {
141         space = stringline.find(" ", previousSpace+1);
142         if (space < -1 || space > (int)stringline.size()) {
143           ErrorMsg(mLandmarks.size(),"index");
144           continue;
145         }
146         //int index = atoi(stringline.substr(previousSpace,space - previousSpace).c_str());
147         previousSpace = space;
148       }
149       space = stringline.find(" ", previousSpace+1);
150       if (space < -1 || space > (int)stringline.size()) {
151         ErrorMsg(mLandmarks.size(),"x position");
152         continue;
153       }
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");
159         continue;
160       }
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");
166         continue;
167       }
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");
174           continue;
175         }
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");
181           continue;
182         }
183         point.pixel_value = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
184       } else {
185         point.pixel_value=0.; //Not in file
186         point.coordinates[3]=0.;
187       }
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]);
196     }
197   }
198   SetTime(0);
199 }
200
201 bool vvLandmarks::ErrorMsg(int num,const char * text)
202 {
203   std::cerr << "error when loading point " << num << " at " << text << std::endl;
204   return false;
205 }
206
207 void vvLandmarks::SaveFile(std::string filename)
208 {
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
213     out << i << " "
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)
221       fileContent += " ";
222     else
223       fileContent += mLandmarks[i].comments;
224     fileContent += "\n";
225   }
226   std::ofstream fp(filename.c_str(), std::ios::trunc);
227   if ( !fp ) {
228     std::cerr << "Unable to open file" << std::endl;
229     return;
230   }
231   fp << fileContent.c_str()<< std::endl;
232   fp.close();
233 }
234
235 void vvLandmarks::SetTime(int time)
236 {
237   if (time >= 0 && time <= ((int)mPoints.size() -1)) {
238     mPolyData->SetPoints(mPoints[time]);
239     mPolyData->GetPointData()->SetScalars(mIds);
240     mPolyData->Modified();
241     mPolyData->Update();
242   }
243 }
244
245 std::string vvLandmarks::replace_dots(std::string input)
246 {
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 );
253   }
254   return input;
255 }