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://www.centreleonberard.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);
80 vvLandmarksGlyph *number = vvLandmarksGlyph::New();
81 number->SetText(numberVal.str().c_str());
83 DD(numberVal.str().c_str());
84 mText.push_back(number);
87 mIds->InsertNextTuple1(0.55);
88 //mIds->InsertTuple1(mLandmarks.size(),mLandmarks.size());
91 //--------------------------------------------------------------------
94 //--------------------------------------------------------------------
95 void vvLandmarks::RemoveLastLandmark()
97 mPoints[mLandmarks.back().coordinates[3]]->SetNumberOfPoints(
98 mPoints[mLandmarks.back().coordinates[3]]->GetNumberOfPoints()-1);
99 mPolyData->Modified();
101 mLandmarks.pop_back();
102 mIds->RemoveLastTuple();
104 //--------------------------------------------------------------------
107 //--------------------------------------------------------------------
108 void vvLandmarks::ChangeComments(int index, std::string comments)
110 mLandmarks[index].comments = comments;
112 //--------------------------------------------------------------------
115 //--------------------------------------------------------------------
116 double vvLandmarks::GetPixelValue(int index)
118 return mLandmarks[index].pixel_value;
120 //--------------------------------------------------------------------
123 //--------------------------------------------------------------------
124 float* vvLandmarks::GetCoordinates(int index)
126 return mLandmarks[index].coordinates;
128 //--------------------------------------------------------------------
131 //--------------------------------------------------------------------
132 std::string vvLandmarks::GetComments(int index)
134 return mLandmarks[index].comments;
136 //--------------------------------------------------------------------
139 //--------------------------------------------------------------------
140 void vvLandmarks::LoadFile(std::string filename)
142 std::ifstream fp(filename.c_str(), std::ios::in|std::ios::binary);
144 std::cerr <<"Unable to open file " << filename << std::endl;
147 mFilename = filename;
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)) {
155 std::string stringline = line;
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
166 if (stringline.size() > 1) {
168 int previousSpace = 0;
170 if (mFormatVersion>0) {
171 space = stringline.find(" ", previousSpace+1);
172 if (space < -1 || space > (int)stringline.size()) {
173 ErrorMsg(mLandmarks.size(),"index");
176 //int index = atoi(stringline.substr(previousSpace,space - previousSpace).c_str());
177 previousSpace = space;
179 space = stringline.find(" ", previousSpace+1);
180 if (space < -1 || space > (int)stringline.size()) {
181 ErrorMsg(mLandmarks.size(),"x position");
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");
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");
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");
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");
215 point.pixel_value = atof(replace_dots(stringline.substr(previousSpace,space - previousSpace)).c_str());
216 // DD(point.pixel_value);
218 point.pixel_value=0.; //Not in file
219 point.coordinates[3]=0.;
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();
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]);
236 //--------------------------------------------------------------------
239 //--------------------------------------------------------------------
240 bool vvLandmarks::ErrorMsg(int num,const char * text)
242 std::cerr << "error when loading point " << num << " at " << text << std::endl;
245 //--------------------------------------------------------------------
248 //--------------------------------------------------------------------
249 void vvLandmarks::SaveFile(std::string filename)
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
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)
265 fileContent += mLandmarks[i].comments;
268 std::ofstream fp(filename.c_str(), std::ios::trunc);
270 std::cerr << "Unable to open file" << std::endl;
273 fp << fileContent.c_str()<< std::endl;
276 //--------------------------------------------------------------------
279 //--------------------------------------------------------------------
280 void vvLandmarks::SetTime(int time)
282 if (time >= 0 && time <= ((int)mPoints.size() -1)) {
283 mPolyData->SetPoints(mPoints[time]);
284 mPolyData->GetPointData()->SetScalars(mIds);
285 mPolyData->Modified();
289 //--------------------------------------------------------------------
292 //--------------------------------------------------------------------
293 std::string vvLandmarks::replace_dots(std::string input)
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 );
304 //--------------------------------------------------------------------