/
Part2.cpp
186 lines (150 loc) · 5.19 KB
/
Part2.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <QImage>
#include <QtWidgets/QPushButton>
#include <string>
#include <vector>
#include <numeric>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
//#include "MHDHandler.h"
//#include "GetRay.h" //#include "SourceDetectorPair.h"
//#include "EntryPoint.h"//#include "GetRay.h"
//#include "RayTracing.h"
//#include "OnTheFlyMx.h"
#include "OnTheFlySystemMx.h"
#include "window.h"
using namespace std;
int main(int argc, char **argv) //argv[0]=./Part2, argv[1]=vol.mhd, argv[2]=traj.csv, argv[3]=CG/SIRT, argv[4]=rvol.mhd
//int main()
{
if (argc!=5)
{
std::cout<<"Error in arguments. Respect the order!!! e.g. ./Part2 ''vol.mhd'' ''trajectory.csv'' ''CG'' ''rvol.mhd''";
}
//--------------------------Read .mhd-------------------------------
MHDHandler MyHandler;
//std::string name ("box.mhd");
std::string name=argv[1];
Volume MyVolume=MyHandler.read(name);
Eigen::VectorXf MyData=MyVolume.getContent();
std::cout<<"Number of voxels=NX*NY*NZ="<<MyData.size()<<std::endl;
//--------------------------Geometry--------------------------------
//Read .cvs
//std::string filename {"ws15cpp_trajectory.csv"};
std::string filename=argv[2];
ifstream ist(filename);
std:string line;
std::getline(ist,line,'\n');//read the tags
std::vector<float> allValues;
while (std::getline(ist,line,'\n'))
{
std::stringstream line_ss(line);
std::string value;
while(std::getline(line_ss,value,','))
{
allValues.push_back(std::stof(value)); //allValues from the .csv. Tags NOT INCLUDED
}
}
//------------------------OnTheFlySystemMx--------------------------
OnTheFlySystemMx MyFly;
std::vector<float> y=MyFly.ForwardProjection(MyVolume, allValues, MyData);
/*Eigen::VectorXf back=Eigen::VectorXf::Zero(MyData.size());
std::vector<float> z=y;
back=MyFly.BackwardProjection(MyVolume, allValues, z);*/
//------------------R-E-C-O-N-S-T-R-U-C-T-I-O-N---------------------
//...result
Eigen::VectorXf result=Eigen::VectorXf::Zero(MyData.size());
if (std::string (argv[3])=="CG")
{
//-------------------------------CG---------------------------------
//...r
std::vector<float> Aresult=MyFly.ForwardProjection(MyVolume, allValues, result);
std::cout<<"y.size()="<<y.size()<<std::endl;
std::cout<<"Aresult.size()="<<Aresult.size()<<std::endl;
std::vector<float> yAresult;
for(int indvec=0;indvec<y.size();indvec=indvec+1)
{
yAresult.push_back(y[indvec]-Aresult[indvec]);
//std::cout<<indvec<<") yAresult=y-Aresult="<<y[indvec]<<"-"<<Aresult[indvec]<<"="<<yAresult[indvec]<<std::endl;
}
Eigen::VectorXf r=MyFly.BackwardProjection(MyVolume, allValues, yAresult);
//...d
Eigen::VectorXf d=r;
//...rtr
float rtr=r.dot(r);
std::cout<<"r.size(),d.size(),rtr="<<r.size()<<","<<d.size()<<","<<rtr<<std::endl;
std::vector<float> rtrVector;
rtrVector.push_back(rtr);
//...Main Loop
const int IterationsNum=200;
std::vector<float> q;
float alpha;
float oldRtr;
float qdotq;
Eigen::VectorXf Atq;
for (int iterator=0;iterator<IterationsNum;iterator=iterator+1)
{
q=MyFly.ForwardProjection(MyVolume,allValues,d);
float acc=0;
qdotq=std::inner_product(q.begin(),q.end(),q.begin(),acc);
alpha=rtr/qdotq;
result+=alpha*d;
Atq=MyFly.BackwardProjection(MyVolume,allValues,q);
r-=alpha*Atq;
oldRtr=rtr;
rtr=r.dot(r); rtrVector.push_back(rtr);
d=r+(rtr/oldRtr)*d;
//std::cout<<"q[20]="<<q[20]<<",q[50]="<<q[50]<<std::endl;
//std::cout<<"qdotq="<<qdotq<<std::endl;
//std::cout<<"alpha="<<alpha<<std::endl;
//std::cout<<"rtr="<<rtr<<std::endl;
//std::cout<<"rtrVector="<<rtrVector[iterator+1]<<std::endl;
}
std::cout<<"MyData-result="<<result.dot(MyData)<<std::endl;
}
if (std::string(argv[3])=="SIRT")
//---------------------------SIRT-----------------------------------
{
float m=y.size();
int SIRTIterations=200;
std::vector<float> AXk;
std::vector<float> yAXk;
Eigen::VectorXf rSIRT;
for (int SIRTIterator=0;SIRTIterator<SIRTIterations;SIRTIterator=SIRTIterator+1)
{
AXk=MyFly.ForwardProjection(MyVolume,allValues,result);
for (int no=0;no<y.size();no=no+1)
{
yAXk.push_back(y[no]-AXk[no]);
}
rSIRT=MyFly.BackwardProjection(MyVolume,allValues,yAXk);
result=result+(1/m)*rSIRT;
}
}
//----------------------SAVE RESULTS--------------------------------
Eigen::Vector3f lowLeftCorner=MyVolume.getLowerLeft();
Eigen::Vector3f upRightCorner=MyVolume.getUpperRight();
Eigen::VectorXf dimension=MyVolume.getDimSpacing();
Volume OutputVol(lowLeftCorner,upRightCorner,dimension);
OutputVol.setContent(result);
MyHandler.write(argv[4],OutputVol);
//----------------------------------------------------------------------
// Part3
//----------------------------------------------------------------------
// QApplication manages the GUI applications control flow
QApplication a(argc, argv);
// create a main window
QMainWindow qmw;
// create a widget that spans the entire main window
window* mainWidget = new window();
qmw.setCentralWidget(mainWidget);
qmw.showMaximized();
qmw.activateWindow();
qmw.raise();
qmw.show();
// start the applications event loop
return a.exec();
//return 0;
}