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 //--------------------------------------------------------------------
33 vvLandmarks::vvLandmarks(int size)
38 for (int i = 0; i < size; i++) {
39 vtkPoints *points = vtkPoints::New();
40 mPoints.push_back(points);
42 mPolyData = vtkPolyData::New();
43 mIds = vtkFloatArray::New();
45 //--------------------------------------------------------------------
48 //--------------------------------------------------------------------
49 vvLandmarks::~vvLandmarks()
51 for (unsigned int i = 0; i < mPoints.size(); i++) {
54 /*for(unsigned int i = 0; i < mText.size(); i++) {
62 //--------------------------------------------------------------------
65 //--------------------------------------------------------------------
66 void vvLandmarks::AddLandmark(float x,float y,float z,float t,double value)
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);
77 /*std::stringstream numberVal;
78 numberVal << (mLandmarks.size()-1);
79 vvLandmarksGlyph *number = vvLandmarksGlyph::New();
80 number->SetText(numberVal.str().c_str());
82 mText.push_back(number);*/
83 mIds->InsertNextTuple1(0.55);
84 //mIds->InsertTuple1(mLandmarks.size(),mLandmarks.size());
87 //--------------------------------------------------------------------
90 //--------------------------------------------------------------------
91 void vvLandmarks::RemoveLastLandmark()
93 mPoints[mLandmarks.back().coordinates[3]]->SetNumberOfPoints(
94 mPoints[mLandmarks.back().coordinates[3]]->GetNumberOfPoints()-1);
95 mPolyData->Modified();
97 mLandmarks.pop_back();
98 mIds->RemoveLastTuple();
100 //--------------------------------------------------------------------
103 //--------------------------------------------------------------------
104 void vvLandmarks::ChangeComments(int index, std::string comments)
106 mLandmarks[index].comments = comments;
108 //--------------------------------------------------------------------
111 //--------------------------------------------------------------------
112 double vvLandmarks::GetPixelValue(int index)
114 return mLandmarks[index].pixel_value;
116 //--------------------------------------------------------------------
119 //--------------------------------------------------------------------
120 float* vvLandmarks::GetCoordinates(int index)
122 return mLandmarks[index].coordinates;
124 //--------------------------------------------------------------------
127 //--------------------------------------------------------------------
128 std::string vvLandmarks::GetComments(int index)
130 return mLandmarks[index].comments;
132 //--------------------------------------------------------------------
135 //--------------------------------------------------------------------
136 void vvLandmarks::LoadFile(std::string filename)
138 std::ifstream fp(filename.c_str(), std::ios::in|std::ios::binary);
140 std::cerr <<"Unable to open file " << filename << std::endl;
143 mFilename = filename;
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)) {
151 std::string stringline = line;
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
162 if (stringline.size() > 1) {
164 int previousSpace = 0;
166 if (mFormatVersion>0) {
167 space = stringline.find(" ", previousSpace+1);
168 if (space < -1 || space > (int)stringline.size()) {
169 ErrorMsg(mLandmarks.size(),"index");
172 //int index = atoi(stringline.substr(previousSpace,space - previousSpace).c_str());
173 previousSpace = space;
175 space = stringline.find(" ", previousSpace+1);
176 if (space < -1 || space > (int)stringline.size()) {
177 ErrorMsg(mLandmarks.size(),"x position");
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");
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");
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");
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");
211 point.pixel_value = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
212 // DD(point.pixel_value);
214 point.pixel_value=0.; //Not in file
215 point.coordinates[3]=0.;
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();
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]);
232 //--------------------------------------------------------------------
235 //--------------------------------------------------------------------
236 bool vvLandmarks::ErrorMsg(int num,const char * text)
238 std::cerr << "error when loading point " << num << " at " << text << std::endl;
241 //--------------------------------------------------------------------
244 //--------------------------------------------------------------------
245 void vvLandmarks::SaveFile(std::string filename)
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
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)
261 fileContent += mLandmarks[i].comments;
264 std::ofstream fp(filename.c_str(), std::ios::trunc);
266 std::cerr << "Unable to open file" << std::endl;
269 fp << fileContent.c_str()<< std::endl;
272 //--------------------------------------------------------------------
275 //--------------------------------------------------------------------
276 void vvLandmarks::SetTime(int time)
278 if (time >= 0 && time <= ((int)mPoints.size() -1)) {
279 mPolyData->SetPoints(mPoints[time]);
280 mPolyData->GetPointData()->SetScalars(mIds);
281 mPolyData->Modified();
285 //--------------------------------------------------------------------
288 //--------------------------------------------------------------------
289 std::string vvLandmarks::replace_dots(std::string input)
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 );
300 //--------------------------------------------------------------------