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