-
Notifications
You must be signed in to change notification settings - Fork 5
/
RigidAlignment.cxx
268 lines (230 loc) · 8.03 KB
/
RigidAlignment.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <string>
#include <algorithm>
#include <map>
#include <iterator>
#include "RigidAlignmentCLP.h"
#include "RigidAlignmentImpl.h"
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkDoubleArray.h>
#include <vtkPolyDataReader.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyDataWriter.h>
#include <vtkPointLocator.h>
#include <vtksys/Directory.hxx>
#include <vtksys/SystemTools.hxx>
using namespace std;
#define NB_LINES 250
#define NB_WORDS 250
bool getListFile(const string& path, vector<string> &list, const string &suffix);
std::map<std::string, std::string> convertLandmarksToID(std::map<std::string, std::vector<int> > &landmarksMap, const vector<string> &meshList, const vector<string> &landmarkList, const string& suffixe_procalign = "_pp_surfSPHARM_procalign.vtk", const string& suffixe_surfSPHARM = "_pp_surfSPHARM.vtk");
void getIDlandmarks(const std::string& mesh, const std::string& landmarks, std::vector<int> &landmarkPids);
int main(int argc, char* argv[])
{
PARSE_ARGS;
if (argc < 3)
{
cout << "Usage: " << argv[0] << " --help" << endl;
return -1;
}
vector<std::string> landmarkList;
vector<std::string> meshList;
if (inputCSV != "0")
{
std::ifstream ifs;
ifs.open(inputCSV.c_str(), std::ifstream::in);
if ( ifs.good() != true )
{
std::cerr << " File CSV couldn't be open " << std::endl;
return EXIT_FAILURE;
}
else
{
while ( ifs.good() )
{
istringstream line_stream;
std::string line, mesh, fid;
std::getline (ifs, line);
line_stream.str(line); // set the input stream to line
getline(line_stream, mesh, ',');
getline(line_stream, fid, ',');
std::size_t pos_vtk = mesh.find(".vtk");
std::size_t pos_fcsv = fid.find(".fcsv");
if ( pos_vtk <= mesh.length() && pos_fcsv <= fid.length() )
{
landmarkList.push_back(fid);
meshList.push_back(mesh);
}
}
}
}
else
{
// Landmarks are initially stored in a fcsv files (fiducials list from Slicer, obtained with Q3DC for example)
if (!landmark.empty() && landmarkList.empty()) {
if(! getListFile(landmark.c_str(), landmarkList, "fcsv"))
return EXIT_FAILURE;
}
if (!mesh.empty() && meshList.empty()) {
if(! getListFile(mesh.c_str(), meshList, "vtk"))
return EXIT_FAILURE;
}
}
std::map<std::string, std::vector<int> > landmarksMap;
std::map<std::string, std::string > matchedNamesMap = convertLandmarksToID(landmarksMap, meshList, landmarkList, suffixe_procalign, suffixe_surfSPHARM);
if(landmarksMap.size() == 0){
cerr<<"No matched landmark files and mesh. Please check that the suffix from SPHARM files match the inputs and that landmark files have the right extensions."<<endl;
return 1;
}
std::map<std::string, std::string >::iterator itt = matchedNamesMap.begin(), itt_end = matchedNamesMap.end();
cout <<endl<<"Matched files with landmarks:"<<endl;
for (; itt != itt_end; itt ++)
cout << itt->first << " " << itt->second << endl;
cout << endl;
std::unique_ptr<RigidAlignment> RAlign(new RigidAlignment(landmarksMap, sphere.c_str(), output.c_str(), lmtype));
if (!outputLM.empty()) RAlign->saveLM(outputLM.c_str());
return 0;
}
bool endsWith(const string& value, const string& suffix) {
return value.size() >= suffix.size() && 0 == value.compare(value.size() - suffix.size(), suffix.size(), suffix);
}
bool getListFile(const string& path, vector<string> &list, const string &suffix)
{
vtksys::Directory dir;
std::string errorMessage = "";
dir.Load(path.c_str(), &errorMessage);
if (errorMessage != "")
{
cerr << "Failed to list directory " << path << ": " << errorMessage << endl;
return false;
}
for (unsigned long fileNum=0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
{
std::string filename = std::string(dir.GetFile(fileNum));
std::string filepath = path + "/" + filename;
if (vtksys::SystemTools::FileIsDirectory(filepath))
{
continue;
}
if (endsWith(filename, suffix))
{
list.push_back(filepath);
}
}
sort(list.begin(), list.end());
return true;
}
std::string getFilenameComponent(const std::string& filepath)
{
std::string fixedFilepath = filepath;
// Move all slashes to forward slash
std::replace(fixedFilepath.begin(), fixedFilepath.end(), '\\', '/');
int pivot = fixedFilepath.rfind('/') + 1;
return fixedFilepath.substr(pivot);
}
std::map<std::string, std::string> convertLandmarksToID(std::map<std::string, std::vector<int> > &landmarksMap, const vector<string> &meshList, const vector<string> &landmarkList, const string& suffixe_procalign, const string& suffixe_surfSPHARM)
{
// --------- subject names --------
int nSubj = meshList.size();
vector<string> subjName;
std::map<std::string, std::string> matchedNames;
if (nSubj > 0)
{
for (int i = 0; i < nSubj; i++)
{
std::string filename = getFilenameComponent(meshList[i]);
int suffixe_size = 0;
if ( filename.substr(filename.length() - suffixe_surfSPHARM.length()).compare(suffixe_surfSPHARM) == 0 )
suffixe_size = suffixe_surfSPHARM.length();
else if ( filename.substr(filename.length() - suffixe_procalign.length()).compare(suffixe_procalign) == 0 )
suffixe_size = suffixe_procalign.length();
std::string name = filename.substr(0, filename.length() - suffixe_size);
subjName.push_back(name);
// Find corresponding fiducials list
std::vector< std::string > :: const_iterator it = landmarkList.begin(), it_end = landmarkList.end();
for (; it != it_end; it++)
{
string suffix_fid = "_fid.fcsv";
string landmarkName = (*it).substr(0, (*it).length() - suffix_fid.length());
landmarkName = getFilenameComponent(landmarkName);
if (name == landmarkName) // On a trouve le bon!
{
std::vector<int> landmarkPids;
getIDlandmarks(meshList[i], *it, landmarkPids);
landmarksMap[name] = landmarkPids;
matchedNames[name] = landmarkName;
break;
}
}
}
}
else
{
std::cerr << "No subjects found" << std::endl;
}
return matchedNames;
}
void getIDlandmarks(const std::string& mesh, const std::string& landmarks, std::vector<int> &landmarkPids)
{
// Get all surface data from the file
vtkSmartPointer<vtkPolyDataReader> surfacereader = vtkSmartPointer<vtkPolyDataReader>::New();
surfacereader->SetFileName(mesh.c_str());
surfacereader->Update();
vtkPolyData* inputPolyData = surfacereader->GetOutput();
// Build a locator
vtkSmartPointer<vtkPointLocator> pointLocator = vtkSmartPointer<vtkPointLocator>::New();
pointLocator->SetDataSet(inputPolyData);
pointLocator->BuildLocator();
// -------------------- Reading FCSV file --------------------
// Get the Surface filename from the command line
std::fstream fcsvfile(landmarks.c_str());
std::string line, mot;
std::vector< std::vector< std::string > > words(NB_LINES); // !!!! WARNING DEFINE AND TO PROTECT IF SUPERIOR TO 20
int i,j, NbPoints;
if(fcsvfile)
{
getline(fcsvfile, line);
fcsvfile>>mot;
while(mot=="#")
{
if(getline(fcsvfile, line))
fcsvfile>>mot;
else
mot="#";
}
i=0;
do
{
words[i].resize(NB_WORDS);
std::size_t pos_end;
std::size_t pos1;
j=0;
do
{
std::size_t pos0 = 0;
pos1 = mot.find(',');
pos_end = mot.find(",,");
words[i][j] = mot.substr(pos0, pos1-pos0);
mot = mot.substr(pos1+1);
j++;
} while(pos1+1<pos_end);
i++;
} while(fcsvfile>>mot);
NbPoints = i;
for (int i = 0; i < NbPoints; ++i)
{
double x = atof(words[i][1].c_str());
double y = atof(words[i][2].c_str());
double z = atof(words[i][3].c_str());
// Find closest point
vtkIdType ptId;
double p[] = {0.0, 0.0, 0.0};
p[0] = x; p[1] = y; p[2] = z;
ptId = pointLocator->FindClosestPoint(p);
landmarkPids.push_back(ptId);
}
}
else
std::cout<<"Error !";
}