1 /*=========================================================================
4 Module: $RCSfile: vvLandmarks.cxx,v $
6 Date: $Date: 2010/01/06 13:31:57 $
7 Version: $Revision: 1.1 $
8 Author : Pierre Seroul (pierre.seroul@gmail.com)
11 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
12 CREATIS-LRMN http://www.creatis.insa-lyon.fr
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, version 3 of the License.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 =========================================================================*/
27 #include "vvLandmarks.h"
35 #include "vtkPolyData.h"
36 #include "vtkPoints.h"
37 #include "vtkFloatArray.h"
38 #include "vtkPointData.h"
39 #include "clitkCommon.h"
41 vvLandmarks::vvLandmarks(int size)
46 for (int i = 0; i < size; i++)
48 vtkPoints *points = vtkPoints::New();
49 mPoints.push_back(points);
51 mPolyData = vtkPolyData::New();
52 mIds = vtkFloatArray::New();
55 vvLandmarks::~vvLandmarks()
57 for (unsigned int i = 0; i < mPoints.size(); i++) {
60 /*for(unsigned int i = 0; i < mText.size(); i++) {
69 void vvLandmarks::AddLandmark(float x,float y,float z,float t,double value)
72 point.coordinates[0] = x;
73 point.coordinates[1] = y;
74 point.coordinates[2] = z;
75 point.coordinates[3] = t;
76 point.pixel_value=value;
77 mLandmarks.push_back(point);
78 mPoints[int(t)]->InsertNextPoint(x,y,z);
80 /*std::stringstream numberVal;
81 numberVal << (mLandmarks.size()-1);
82 vvLandmarksGlyph *number = vvLandmarksGlyph::New();
83 number->SetText(numberVal.str().c_str());
85 mText.push_back(number);*/
86 mIds->InsertNextTuple1(0.55);
87 //mIds->InsertTuple1(mLandmarks.size(),mLandmarks.size());
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();
101 void vvLandmarks::ChangeComments(int index, std::string comments)
103 mLandmarks[index].comments = comments;
106 double vvLandmarks::GetPixelValue(int index)
108 return mLandmarks[index].pixel_value;
111 float* vvLandmarks::GetCoordinates(int index)
113 return mLandmarks[index].coordinates;
116 std::string vvLandmarks::GetComments(int index)
118 return mLandmarks[index].comments;
121 void vvLandmarks::LoadFile(std::string filename)
123 std::ifstream fp(filename.c_str(), std::ios::in|std::ios::binary);
126 std::cerr <<"Unable to open file " << filename << std::endl;
129 mFilename = filename;
132 for (unsigned int i = 0; i < mPoints.size();i++)
133 mPoints[i]->SetNumberOfPoints(0);
134 bool first_line=true;
135 while (fp.getline(line,255))
137 std::string stringline = line;
141 ///New landmark format: first line is "LANDMARKSXX", where XX is the version number
142 if (stringline.size() >= 10 && stringline.compare(0,9,"LANDMARKS")==0)
144 std::istringstream ss(stringline.c_str()+9);
145 ss >> mFormatVersion;
146 continue; //skip first line
152 if (stringline.size() > 1)
155 int previousSpace = 0;
157 if (mFormatVersion>0)
159 space = stringline.find(" ", previousSpace+1);
160 if (space < -1 || space > (int)stringline.size())
162 ErrorMsg(mLandmarks.size(),"index");
165 //int index = atoi(stringline.substr(previousSpace,space - previousSpace).c_str());
166 previousSpace = space;
168 space = stringline.find(" ", previousSpace+1);
169 if (space < -1 || space > (int)stringline.size())
171 ErrorMsg(mLandmarks.size(),"x position");
174 point.coordinates[0] = 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())
179 ErrorMsg(mLandmarks.size(),"y position");
182 point.coordinates[1] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
183 previousSpace = space;
184 space = stringline.find(" ", previousSpace+1);
185 if (space < -1 || space > (int)stringline.size())
187 ErrorMsg(mLandmarks.size(),"z position");
190 point.coordinates[2] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
191 previousSpace = space;
192 if (mFormatVersion>0)
194 space = stringline.find(" ", previousSpace+1);
195 if (space < -1 || space > (int)stringline.size())
197 ErrorMsg(mLandmarks.size(),"t position");
200 point.coordinates[3] = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
201 previousSpace = space;
202 space = stringline.find(" ", previousSpace+1);
203 if (space < -1 || space > (int)stringline.size())
205 ErrorMsg(mLandmarks.size(),"pixel value");
208 point.pixel_value = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
212 point.pixel_value=0.; //Not in file
213 point.coordinates[3]=0.;
215 previousSpace = space;
216 //this is the maximum size of comments
217 space = (stringline.find("\n", previousSpace+1) < 254 ? stringline.find("\n", previousSpace+1) : 254);
218 point.comments = stringline.substr(previousSpace,space - (previousSpace)).c_str();
219 mLandmarks.push_back(point);
220 mIds->InsertNextTuple1(0.55);
221 mPoints[int(point.coordinates[3])]->InsertNextPoint(
222 point.coordinates[0],point.coordinates[1],point.coordinates[2]);
228 bool vvLandmarks::ErrorMsg(int num,const char * text)
230 std::cerr << "error when loading point " << num << " at " << text << std::endl;
234 void vvLandmarks::SaveFile(std::string filename)
236 std::string fileContent = "LANDMARKS1\n"; //File format version identification
237 for (unsigned int i = 0; i < mLandmarks.size(); i++) {
238 std::stringstream out;
239 out.imbue(std::locale("C")); //This is to specify that the dot is to be used as the decimal separator
241 << mLandmarks[i].coordinates[0] << " "
242 << mLandmarks[i].coordinates[1] << " "
243 << mLandmarks[i].coordinates[2] << " "
244 << mLandmarks[i].coordinates[3] << " "
245 << mLandmarks[i].pixel_value << " ";
246 fileContent += out.str();
247 if (mLandmarks[i].comments.size() == 0)
250 fileContent += mLandmarks[i].comments;
253 std::ofstream fp(filename.c_str(), std::ios::trunc);
256 std::cerr << "Unable to open file" << std::endl;
259 fp << fileContent.c_str()<< std::endl;
263 void vvLandmarks::SetTime(int time)
265 if (time >= 0 && time <= ((int)mPoints.size() -1))
267 mPolyData->SetPoints(mPoints[time]);
268 mPolyData->GetPointData()->SetScalars(mIds);
269 mPolyData->Modified();
274 std::string vvLandmarks::replace_dots(std::string input)
276 ///Replaces the dots used in the file with the decimal separator in use on the platform
277 lconv * conv=localeconv();
278 unsigned int position = input.find( "." );
279 while ( position < input.size() )
281 input.replace(position, 1, conv->decimal_point);
282 position = input.find( ".", position + 1 );