-
Notifications
You must be signed in to change notification settings - Fork 0
/
possumfns.cc
3338 lines (3176 loc) · 130 KB
/
possumfns.cc
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* POSSUM
Ivana Drobnjak & Mark Jenkinson
Copyright (C) 2005-2007 University of Oxford */
/* Part of FSL - FMRIB's Software Library
http://www.fmrib.ox.ac.uk/fsl
fsl@fmrib.ox.ac.uk
Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance
Imaging of the Brain), Department of Clinical Neurology, Oxford
University, Oxford, UK
LICENCE
FMRIB Software Library, Release 5.0 (c) 2012, The University of
Oxford (the "Software")
The Software remains the property of the University of Oxford ("the
University").
The Software is distributed "AS IS" under this Licence solely for
non-commercial use in the hope that it will be useful, but in order
that the University as a charitable foundation protects its assets for
the benefit of its educational and research purposes, the University
makes clear that no condition is made or to be implied, nor is any
warranty given or to be implied, as to the accuracy of the Software,
or that it will be suitable for any particular purpose or for use
under any specific conditions. Furthermore, the University disclaims
all responsibility for the use which is made of the Software. It
further disclaims any liability for the outcomes arising from using
the Software.
The Licensee agrees to indemnify the University and hold the
University harmless from and against any and all claims, damages and
liabilities asserted by third parties (including claims for
negligence) which arise directly or indirectly from the use of the
Software or the sale of any products based on the Software.
No part of the Software may be reproduced, modified, transmitted or
transferred in any form or by any means, electronic or mechanical,
without the express permission of the University. The permission of
the University is not required if the said reproduction, modification,
transmission or transference is done without financial return, the
conditions of this Licence are imposed upon the receiver of the
product, and all original and amended source code is included in any
transmitted product. You may be held legally responsible for any
copyright infringement that is caused or encouraged by your failure to
abide by these terms and conditions.
You are not permitted under this Licence to use this Software
commercially. Use for which any financial return is received shall be
defined as commercial use, and includes (1) integration of all or part
of the source code or the Software into a product for sale or license
by or on behalf of Licensee to third parties or (2) use of the
Software or any derivative of it for research with the final aim of
developing software products for sale or license to a third party or
(3) use of the Software or any derivative of it for research with the
final aim of developing non-software products for sale or license to a
third party, or (4) use of the Software to provide any service to an
external organisation for which payment is received. If you are
interested in using the Software commercially, please contact Oxford
University Innovation ("OUI"), the technology transfer company of the
University, to negotiate a licence. Contact details are:
Innovation@innovation.ox.ac.uk quoting reference DE/9564. */
//POSSUM-FUNCTIONS
#include <assert.h>
#include <iostream>//standard c++ library
#include <string> //include string class
#include <fstream> //to read and write to the file
#include <unistd.h>//what is this?
#include <vector> //for precalcuation of some variables
#include "newmatap.h"//including NEWMAT library
#include "newmatio.h"//
#include "newimage/newimageall.h"//including NEW IMAGE library
#include "possumfns.h"//including possum functions I made
#include "libprob.h"
#include "miscmaths/miscprob.h"
#include "newimage/costfns.h"
#include <vector>
#define _GNU_SOURCE 1
#define POSIX_SOURCE 1
using namespace std;
using namespace MISCMATHS;
using namespace NEWIMAGE;
using namespace NEWMAT;
//string title="possumfns (Version 2.0)\nCopyright(c) 2003, University of Oxford (Ivana Drobnjak)";
vector<RowVector> gstatic;
double* g1static;
double* g2static;
double* g3static;
vector<double> g1motion;
vector<double> g2motion;
vector<double> g3motion;
vector<double> g4motion;
vector<double> rotmotion1;
vector<double> rotmotion2;
vector<double> rotmotion3;
vector<double> transmotion;
vector<double> b0tmp111(1,0.0);
vector<double> b0tmp112(1,0.0);
vector<double> b0tmp113(1,0.0);
vector<double> b0tmp221(1,0.0);
vector<double> b0tmp222(1,0.0);
vector<double> b0tmp223(1,0.0);
vector<double> b0tmp331(1,0.0);
vector<double> b0tmp332(1,0.0);
vector<double> b0tmp333(1,0.0);
vector<double> b0tmp111freq(1,0.0);
vector<double> b0tmp112freq(1,0.0);
vector<double> b0tmp113freq(1,0.0);
vector<double> b0tmp221freq(1,0.0);
vector<double> b0tmp222freq(1,0.0);
vector<double> b0tmp223freq(1,0.0);
vector<double> b0tmp331freq(1,0.0);
vector<double> b0tmp332freq(1,0.0);
vector<double> b0tmp333freq(1,0.0);
double glo_zero=1e-20;
//TABLE SINC
double* table_sinc=0;//table for SINC in range [0,Dsinc]
int glo_Nsinc=5000000;//no of elements
double glo_Dsinc=500.00;//domain
double glo_dsinc=glo_Dsinc/glo_Nsinc;//step size for SINC
double glo_idsinc=1/glo_dsinc;//inverse of step size for fster calc
//TABLE SIN AND COS
double* table_sin=0;//table for SIN in range [0,2pi]
double* table_cos=0;//table for COS in range [0,2pi]
int glo_Nsin=6000000;//no of elements
double glo_Dsin=2.0*M_PI;//domain
double glo_dsin=glo_Dsin/glo_Nsin;//step size for SIN and COS
double glo_idsin=1/glo_dsin;//inverse of the step size
double glo_twopi=2*M_PI;
double glo_itwopi=1/glo_twopi;
double glo_cx;
double glo_cy;
double glo_cz;
//SOME CONSTANTS
const double gammabar=42.58*1e06;//(in Hz/T)
const double gama=2*M_PI*gammabar;
/////////
//GENERAL
///////////////////////////////
double round_ivana(const double x, const int n){
//rounds a number up to n digits of precision
double xx,xxx,xxxx,nn;
nn=MISCMATHS::pow(10.0f,(double) n);
xx=nn*x;
if (x>0) xxx=floor((float)xx+0.5);
else xxx=ceil(xx-0.5);
xxxx=xxx/nn;
return xxxx;
}
/////////////////////////////
double norm(const RowVector q){
int x=q.Ncols();
double sqsum=0.0;
for (int i=1;i<=x;i++){
sqsum+=q(i)*q(i);
}
double norm_q=sqrt(sqsum);
return norm_q;
}
////////////////////////////////////////
void coeff(const double xold, const double xnew, const double told, const double tnew, double& a, double& b){//needs improvement
// return slope and intercept of a line
//cout<<"xold="<<xold<<" xnew="<<xnew<<" told="<<told<<" tnew="<<tnew<<" a="<<a<<" b="<<b<<endl;
if ((tnew-told)>1e-12){
a=xold-told*(xnew-xold)/(tnew-told); //intercept
b=(xnew-xold)/(tnew-told); //slope
}
//cout<<"xold="<<xold<<" xnew="<<xnew<<" told="<<told<<" tnew="<<tnew<<" a="<<a<<" b="<<b<<endl;
}
//////////////////////////////////////////from mj code costfns.cc in NEWIMAGE
double mj_sinc(const double x){
if (fabs(x)<1e-7) { return 1.0-fabs(x); }
double y=M_PI*x;
return sin(y)/y;
}
/////////////////////////////////////////////////
Matrix rot(const double alpha,const string axes){//WE USE RIGHT HAND RULE FOR THE ROTATIONS AND ORDER IN ROTATION SEQUENCE Rz*Ry*Rx
Matrix R(3,3);
if (axes=="z"){
//rotation right hand rule
R <<cos(alpha)<<-sin(alpha)<<0
<<sin(alpha)<<cos(alpha)<<0
<<0<<0<<1;
}
if (axes=="x"){
//rotation right hand rule
R <<1<<0<<0
<<0<<cos(alpha)<<-sin(alpha)
<<0<<sin(alpha)<<cos(alpha);
}
if (axes=="y"){
//rotation right hand rule
R <<cos(alpha)<<0<<-sin(alpha)
<<0<<1<<0
<<sin(alpha)<<0<<cos(alpha);
}
return R;
}
//////////////////////////////////////////////////////////////
//CONVERSIONS BETWEEN EULER ANGLES, QUATERNIONS and MATRICES
//////////////////////////////////////////////////////////////////////////
RowVector mult_quaternions(const RowVector a,const RowVector b){//the order of the multiplication is important as it is not comutative!!
//quaternion is of the form c=(cos(angle/2),axisx*sin(angle/2),axisy*sin(angle/2),axisz*sin(angle/2))=(w,x,y,z)
RowVector c(4);
c(1)=a(1)*b(1)-a(2)*b(2)-a(3)*b(3)-a(4)*b(4);
c(2)=a(1)*b(2)+a(2)*b(1)+a(3)*b(4)-a(4)*b(3);
c(3)=a(1)*b(3)-a(2)*b(4)+a(3)*b(1)+a(4)*b(2);
c(4)=a(1)*b(4)+a(2)*b(3)-a(3)*b(2)+a(4)*b(1);
return c;
}
////////////////////////////////////////////////////////////////////////////////
RowVector euler_to_quaternion(const double a,const double b,const double c){//ok
//constructed by having one basic quaternion for each rotation Rz(c), Ry(b) and Rx(a) and then multiplying them in order qz*qy*qx
//RowVector qx(4),qy(4),qz(4),q(4);
//qx<<cos(a/2)<<sin(a/2)<<0.0<<0.0; //the norm is one
//qy<<cos(b/2)<<0.0<<sin(b/2)<<0.0; //the norm is one
//qz<<cos(c/2)<<0.0<<0.0<<sin(c/2); //the norm is one
//RowVector tmp(4);
//tmp=mult_quaternions(qy,qx);
//q=mult_quaternions(qz,tmp);// the norm of qz*qy*qx has to be also one as the norm(q1*q2)=norm(q1)*norm(q2)
double cz=cos(c/2);
double cy=cos(b/2);
double cx=cos(a/2);
double sz=sin(c/2);
double sy=sin(b/2);
double sx=sin(a/2);
RowVector q(4);
q<<cz*cy*cx+sz*sy*sx<<cz*cy*sx-sz*sy*cx<<cz*sy*cx+sz*cy*sx<<sz*cy*cx-cz*sy*sx;
double norm_q=norm(q);//has to be one from the way we constructed q
if (fabs(norm_q-1)>1e-12) {
cout<<"Warning, euler_to_quat is producing quaternions with the norm different from 1!"<<"Norm difference from one is "<<norm_q-1<<"Quat is "<<q<<endl;
}
return q;
}
/////////////////////////////////////////////////////
RowVector quaternion_to_angleaxis(const RowVector q){//ok, but be carefull when applying it after angleaxis_to_quaternion to same vectors
//angleaxis is of the form (angle,axisx,axisy,axisz)
double norm_q=norm(q);
RowVector q_n(4);
q_n=q;
//cout<<norm_q-1<<endl;
//cout<<"quaternion is "<<q_n<<endl;
if (fabs(norm_q-1)>1e-12) {
cout<<"Warning, quaternion is not normalised !"<<"Norm_q-1 is "<<norm_q-1<<"q is "<<q<<endl;
q_n(1)=q(1)/norm_q;
q_n(2)=q(2)/norm_q;
q_n(3)=q(3)/norm_q;
q_n(4)=q(4)/norm_q;
}
double angle;
RowVector axis(3);
if ((q_n(1)-1)>0) q_n(1)=1; //the difference can be really small i.e. 1e-16, but acos gives nan for it (for 1+1e-16)
if ((q_n(1)+1)<0) q_n(1)=-1;
angle=acos(q_n(1))*2;
//cout<<q_n(1)-1<<" angle "<<angle<<endl;
//cout<<"angle "<<angle<<endl;//0<=acos()<=pi! Although we always get positive angle out, it is the axis that determines the rotation, so if angle<0 then negative axis
//double s=sqrt(1-q_n(1)*q_n(1));is not good as it can create 0 0 0 axis
double s= sqrt(q_n(2)*q_n(2)+q_n(3)*q_n(3)+q_n(4)*q_n(4));
//s=fabs(sin(angle/2));
//cout<<"s "<<s<<endl;
if (fabs(s)<1e-12){
angle=0;
axis(1)=0;
axis(2)=0;
axis(3)=1; //taken so that the axis is always normalised but has no real meaning as the angle iz zero
} else {
axis(1)=q_n(2)/s;
axis(2)=q_n(3)/s;
axis(3)=q_n(4)/s;
}
double norm_axis=norm(axis);//has to be one if the quaternion is normalised, easily proved that normalised quaternion iff normalised axis
if (fabs(norm_axis)<1e-12){
cout<<"Warning in quaternion_to_angleaxis: Norm of the axis is ZERO!"<<endl;
}
RowVector angleaxis(4);
angleaxis(1)=angle;
angleaxis(2)=axis(1);
angleaxis(3)=axis(2);
angleaxis(4)=axis(3);
return angleaxis;
}
////////////////////////////////////////////////////////////////////////
RowVector angleaxis_to_quaternion(const RowVector angleaxis){
//angleaxis is of the form (angle,x,y,z)
double angle=angleaxis(1);
RowVector axis(3);
axis<<angleaxis(2)<<angleaxis(3)<<angleaxis(4);
double norm_axis=norm(axis);
if (fabs(norm_axis-1)>1e-12) {
axis(1)=axis(1)/norm_axis;
axis(2)=axis(2)/norm_axis;
axis(3)=axis(3)/norm_axis;
}
double x=axis(1)*sin(angle/2);
double y=axis(2)*sin(angle/2);
double z=axis(3)*sin(angle/2);
double w=cos(angle/2);
RowVector q(4);
q<<w<<x<<y<<z;
return q;
}
////////////////////////////////////////////////////////
Matrix quaternion_to_matrix(const RowVector q){//not using
double norm_q=norm(q);
RowVector q_n(4);
q_n=q;
if (fabs(norm_q-1)>1e-12) {
q_n(1)=q(1)/norm_q;
q_n(2)=q(2)/norm_q;
q_n(3)=q(3)/norm_q;
q_n(4)=q(4)/norm_q;
}
double x=q_n(2);
double y=q_n(3);
double z=q_n(4);
double w=q_n(1);
Matrix R(3,3);
R<<1-2*y*y-2*z*z<<2*x*y-2*z*w<<2*x*z+2*y*w
<<2*x*y+2*z*w<<1-2*x*x-2*z*z<<2*y*z-2*x*w
<<2*x*z-2*y*w<<2*y*z+2*x*w<<1-2*x*x-2*y*y;
return R;
}
////////////////////////////////////////////////////////////////////////////////
RowVector matrix_to_quaternion(const Matrix M){//not using
double trace = M(1,1) + M(2,2) + M(3,3) + 1;
double s,x,y,z,w;
if( trace > 0 ) {
s = 0.5 / sqrt(trace);
w = 0.25f/s;
x = (M(3,2) - M(2,3))*s;
y = (M(1,3) - M(3,1))*s;
z = (M(2,1) - M(1,2))*s;
}
else {
if ( M(1,1) > M(2,2) && M(1,1) > M(3,3) ) {
double s = 2 * sqrt( 1 + M(1,1) - M(2,2) - M(3,3));
x = 0.25 * s;
y = (M(1,2)+M(2,1))/s;
z = (M(1,3)+M(3,1))/s;
w = (M(2,3)-M(3,2))/s;
}
else if (M(2,2) > M(3,3)) {
double s = 2 * sqrt( 1 + M(2,2) - M(1,1) - M(3,3));
x = (M(1,2) + M(2,1) ) / s;
y = 0.25f * s;
z = (M(2,3) + M(3,2) ) / s;
w = (M(1,3) - M(3,1) ) / s;
}
else {
double s = 2 * sqrt( 1 + M(3,3) - M(1,1) - M(2,2) );
x = (M(1,3) + M(3,1) ) / s;
y = (M(2,3) + M(3,2) ) / s;
z = 0.25 * s;
w = (M(1,2) - M(2,1) ) / s;
}
}
RowVector q(4);
q<<w<<x<<y<<z;
return q;
}
/////////////////////////////////////////////////////////////////////////////////////////////
Matrix euler_to_matrix(const double a,const double b, const double c){//not using
Matrix R(3,3);
R(1,1)=cos(b)*cos(c);
R(1,2)=-cos(b)*sin(c);
R(1,3)=-sin(b);
R(2,1)=-sin(a)*sin(b)*cos(c)+cos(a)*sin(c);
R(2,2)=sin(a)*sin(b)*sin(c)+cos(a)*cos(c);
R(2,3)=-sin(a)*cos(b);
R(3,1)=cos(a)*sin(b)*cos(c)+sin(a)*sin(c);
R(3,2)=-cos(a)*sin(b)*sin(c)+sin(a)*cos(c);
R(3,3)=cos(a)*cos(b);
return R;
}
////////////////////////////////
//STUFF FOR THE ROTATIONS
////////////////////////////////////////////////////////////////////////////////////////////
ReturnMatrix axismat(const RowVector angleaxis){//look more into it!
// returns A where R = I + sin(angle) A + (1 - cos(angle)) A^2
RowVector axis(3);
axis<<angleaxis(2)<<angleaxis(3)<<angleaxis(4);
double norm_axis=norm(axis);
if (fabs(norm_axis-1)>1e-12) {
if (fabs(norm_axis)>1e-12){
axis(1)=axis(1)/norm_axis;
axis(2)=axis(2)/norm_axis;
axis(3)=axis(3)/norm_axis;
}
else {
//cout<<"Warning in axismatm: Norm of the axis is ZERO!"<<endl;
}
}
Matrix m(3,3);
m <<0.0<<-axis(3)<<axis(2)
<<axis(3)<<0.0<<-axis(1)
<<-axis(2)<<axis(1)<<0.0;
m.Release();
return m;
}
///////////////////////////////////////////////////////////////////////////////////////////////
ReturnMatrix rotmat(const RowVector angleaxis){
// returns R where R = I + sin(angle) A + (1 - cos(angle) A^2
Matrix m(3,3);
Matrix d(3,3);
d=0;
d(1,1)=1;d(2,2)=1;d(3,3)=1;
Matrix A(3,3);
A=axismat(angleaxis);
double angle=angleaxis(1);
m=d+sin(angle)*A+(1-cos(angle))*A*A;
m.Release();
return m;
}
////////////////////////////////////////////////////////////////////////////////
ColumnVector free(const ColumnVector m,const double time_i,const RowVector tissue,const double phase_i,const double actinttt){
// m is the magnetization vector just after the last rf pulse. this modul calculates the magnetization vector after the free precession, just before the next rf pulse
// tissue:T1,T2,rho
// time_i=time-rftime where time is time from the bigining of the acquisition and rftime is the time of the last rf pulse
// phase_i=phase-rfphase the same logic as the time
Matrix W(3,3);
ColumnVector Q(3);
double e2=exp(-time_i/tissue(2)+actinttt);
double e1=exp(-time_i/tissue(1));
W <<e2<<0<<0
<<0<<e2<<0
<<0<<0<<e1;
Q=0.0;
Q(3)=tissue(3)*(1-e1);
ColumnVector m1(3);
m1=W*rot(phase_i,"z")*m+Q;
return m1;
}
////////////////////////////////////////////////////////////////////////////////
ColumnVector free_cout(const ColumnVector m,const double time_i,const RowVector tissue,const double phase_i,const double actinttt){
// m is the magnetization vector just after the last rf pulse. this modul calculates the magnetization vector after the free precession, just before the next rf pulse
// tissue:T1,T2,rho
// time_i=time-rftime where time is time from the bigining of the acquisition and rftime is the time of the last rf pulse
// phase_i=phase-rfphase the same logic as the time
Matrix W(3,3);
ColumnVector Q(3);
double e2=exp(-time_i/tissue(2)+actinttt);
double e1=exp(-time_i/tissue(1));
W <<e2<<0<<0
<<0<<e2<<0
<<0<<0<<e1;
Q=0.0;
Q(3)=tissue(3)*(1-e1);
ColumnVector m1(3);
m1=W*rot(phase_i,"z")*m+Q;
cout<<"Input: m="<<m<<", time="<<time_i<<", tissue="<<tissue<<", phase="<<phase_i<<", actint="<<actinttt<<". Output: e1="<<e1<<", e2="<<e2<<", rot output="<<rot(phase_i,"z")<<", W="<<W<<", Q="<<Q<<", m1="<<m1<<endl;
return m1;
}
///////////////////////////
//INTERGRALS
//////////////////////////////////////////////////////////////////////////////////////////////////////
double i1(const double gold,const double gnew,const double told,const double tnew){
// integral: i1 = \int G(t) dt
double i11;
double g1,g2;
g1=0.0;g2=0.0;
coeff(gold,gnew,told,tnew,g1,g2);
//i11=tnew*(g1+g2*tnew/2)-told*(g1+g2*told/2);
//tejas-changed 26.10.12
i11=(tnew-told)*g1+(tnew-told)*(tnew+told)*g2/2;
//tejas-end
return i11;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
double i1new(const double gold,const double gnew,const double told,const double tnew){
// integral: i1 = \int G(t) dt
double i11;
double g1,g2;
g1=0.0;g2=0.0;
coeff(gold,gnew,told,tnew,g1,g2);
cout<<"Gradient is g1+g2*(tnew-told): g1="<<g1<<"; g2="<<g2<<"; tnew-told="<<tnew-told<<endl;
//i11=tnew*(g1+g2*tnew/2)-told*(g1+g2*told/2);
i11=(tnew-told)*g1+(tnew-told)*(tnew+told)*g2/2;
return i11;
}
//////////////////////////////////////////////////////////////////////////////////////////////
double i2(const double gold,const double gnew,const double aold, const double anew, const double told,const double tnew){
// integral: i2 = \int sin(\alpha(t)) G(t) dt
double i22;
double g1,g2;
g1=0.0;g2=0.0;
coeff(gold,gnew,told,tnew,g1,g2);
double a1,a2;
a1=0.0;a2=0.0;
coeff(aold,anew,told,tnew,a1,a2);
//if ((tnew-told)>0.1)cout<<a2<<endl;
if (fabs(a2)*(tnew-told)<0.01){
i22=(2*g1*cos(a2*(tnew-told)/4)*sin(a1+a2*(told+tnew)/2)+g2*tnew*sin(a1+a2*(3*tnew+told)/4)+g2*told*sin(a1+a2*(tnew+3*told)/4))*(Sinc(a2*(tnew-told)/(4*M_PI))*(tnew-told)/2);//teylor done for sin(o(1e-04)) error expected to be less than 0.0016%
//i22=sin(a1)*i1(gold,gnew,told,tnew);
}
else {
i22=g1*2*(sin(a2*(tnew-told)/2)/a2)*sin(a1+a2*(tnew+told)/2)+g2*(sin(a1)*(tnew*sin(a2*tnew)/a2-told*sin(a2*told)/a2-2*(sin(a2*(tnew-told)/2)/a2)*(sin(a2*(tnew+told)/2)/a2))+cos(a1)*(-tnew*cos(a2*tnew)/a2+told*cos(a2*told)/a2+2*(sin(a2*(tnew-told)/2)/a2)*(cos(a2*(tnew+told)/2)/a2)));
if (fabs(a2)<0.00001) cout<<"WARNING check: a2="<<a2<<"i22="<<i22<<endl;
//i22=(-(g1+g2*tnew)*cos(a1+a2*tnew)+(g1+g2*told)*cos(a1+a2*told)+(g2*sin(a1+a2*tnew)-g2*sin(a1+a2*told))/a2)/a2;
}
return i22;
}
////////////////////////////////////////////////////////////////////////////////////////
double i2new(const double gold,const double gnew,const double aold, const double anew, const double told,const double tnew){
//USED ONLY DURING TESTING FOR OUTPUTING VALUES WHEN -V VERSION OF THE CODE
// integral: i2 = \int sin(\alpha(t)) G(t) dt
double i22;
double g1,g2;
g1=0.0;g2=0.0;
coeff(gold,gnew,told,tnew,g1,g2);
double a1,a2;
a1=0.0;a2=0.0;
coeff(aold,anew,told,tnew,a1,a2);
cout<<"Angle is a1+a2*(tnew-told): a1="<<a1<<"; a2="<<a2<<"; tnew-told="<<tnew-told<<endl;
cout<<"aold= "<<aold<<" anew= "<<anew<<endl;
//if ((tnew-told)>0.1)cout<<a2<<endl;
if (fabs(a2)*(tnew-told)<0.01){
cout<<"fabs(a2)*(tnew-told)="<<fabs(a2)*(tnew-told)<<" < 0.01"<<endl;
i22=(2*g1*cos(a2*(tnew-told)/4)*sin(a1+a2*(told+tnew)/2)+g2*tnew*sin(a1+a2*(3*tnew+told)/4)+g2*told*sin(a1+a2*(tnew+3*told)/4))*(Sinc(a2*(tnew-told)/(4*M_PI))*(tnew-told)/2);//teylor done for sin(o(1e-04)) error expected to be less than 0.0016%
//i22=sin(a1)*i1(gold,gnew,told,tnew);
}
else {
cout<<"fabs(a2)*(tnew-told)="<<fabs(a2)*(tnew-told)<<" > 0.01"<<endl;
i22=g1*2*(sin(a2*(tnew-told)/2)/a2)*sin(a1+a2*(tnew+told)/2)+g2*(sin(a1)*(tnew*sin(a2*tnew)/a2-told*sin(a2*told)/a2-2*(sin(a2*(tnew-told)/2)/a2)*(sin(a2*(tnew+told)/2)/a2))+cos(a1)*(-tnew*cos(a2*tnew)/a2+told*cos(a2*told)/a2+2*(sin(a2*(tnew-told)/2)/a2)*(cos(a2*(tnew+told)/2)/a2)));
if (fabs(a2)<0.00001) cout<<"WARNING check: a2="<<a2<<"i22="<<i22<<endl;
//i22=(-(g1+g2*tnew)*cos(a1+a2*tnew)+(g1+g2*told)*cos(a1+a2*told)+(g2*sin(a1+a2*tnew)-g2*sin(a1+a2*told))/a2)/a2;
}
return i22;
}
///////////////////////////////////////////////////////////////////////////////////////////
double i3(const double gold,const double gnew,const double aold,const double anew,const double told,const double tnew){
// integral: i3 = \int cos(\alpha(t)) G(t) dt
double i33;
double g1,g2;
g1=0.0;g2=0.0;
coeff(gold,gnew,told,tnew,g1,g2);
double a1,a2;
a1=0.0;a2=0.0;
coeff(aold,anew,told,tnew,a1,a2);
if (fabs(a2)*(tnew-told)<0.01){
i33=(2*g1*cos(a2*(tnew-told)/4)*cos(a1+a2*(told+tnew)/2)+g2*tnew*cos(a1+a2*(3*tnew+told)/4)+g2*told*cos(a1+a2*(tnew+3*told)/4))*(Sinc(a2*(tnew-told)/(4*M_PI))*(tnew-told)/2);//teylor done for sin(o(1e-04))
//i33=cos(a1)*i1(gold,gnew,told,tnew);
}
else {
i33=g1*2*(sin(a2*(tnew-told)/2)/a2)*cos(a1+a2*(tnew+told)/2)+g2*(sin(a1)*(tnew*cos(a2*tnew)/a2-told*cos(a2*told)/a2-2*(sin(a2*(tnew-told)/2)/a2)*(cos(a2*(tnew+told)/2)/a2))+cos(a1)*(tnew*sin(a2*tnew)/a2-told*sin(a2*told)/a2-2*(sin(a2*(tnew-told)/2)/a2)*(sin(a2*(tnew+told)/2)/a2)));
if (fabs(a2)<0.00001) cout<<"WARNING check: a2="<<a2<<"i33="<<i33<<endl;
//i33=((g1+g2*tnew)*sin(a1+a2*tnew)-(g1+g2*told)*sin(a1+a2*told)+(g2*cos(a1+a2*tnew)-g2*cos(a1+a2*told))/a2)/a2;
}
return i33;
}
///////////////////////////////////////////////////////////////////////////////////////////
double i3new(const double gold,const double gnew,const double aold,const double anew,const double told,const double tnew){
// integral: i3 = \int cos(\alpha(t)) G(t) dt
double i33;
double g1,g2;
g1=0.0;g2=0.0;
coeff(gold,gnew,told,tnew,g1,g2);
double a1,a2;
a1=0.0;a2=0.0;
coeff(aold,anew,told,tnew,a1,a2);
if (fabs(a2)*(tnew-told)<0.01){
i33=(2*g1*cos(a2*(tnew-told)/4)*cos(a1+a2*(told+tnew)/2)+g2*tnew*cos(a1+a2*(3*tnew+told)/4)+g2*told*cos(a1+a2*(tnew+3*told)/4))*(Sinc(a2*(tnew-told)/(4*M_PI))*(tnew-told)/2);//teylor done for sin(o(1e-04))
//i33=cos(a1)*i1(gold,gnew,told,tnew);
}
else {
i33=g1*2*(sin(a2*(tnew-told)/2)/a2)*cos(a1+a2*(tnew+told)/2)+g2*(sin(a1)*(tnew*cos(a2*tnew)/a2-told*cos(a2*told)/a2-2*(sin(a2*(tnew-told)/2)/a2)*(cos(a2*(tnew+told)/2)/a2))+cos(a1)*(tnew*sin(a2*tnew)/a2-told*sin(a2*told)/a2-2*(sin(a2*(tnew-told)/2)/a2)*(sin(a2*(tnew+told)/2)/a2)));
if (fabs(a2)<0.00001) cout<<"WARNING check: a2="<<a2<<"i33="<<i33<<endl;
//i33=((g1+g2*tnew)*sin(a1+a2*tnew)-(g1+g2*told)*sin(a1+a2*told)+(g2*cos(a1+a2*tnew)-g2*cos(a1+a2*told))/a2)/a2;
}
return i33;
}
/////////////////////////////////////////////////////////////////////////////////////////////
double i4(const double gold,const double gnew,const double trold,const double trnew, const double told,const double tnew){
// integral: i4 = \int T(t) G(t) dt : T(t) is translation
double i44;
double g1,g2;
g1=0.0;g2=0.0;
coeff(gold,gnew,told,tnew,g1,g2);
double tr1,tr2;
tr1=0.0;tr2=0.0;
coeff(trold,trnew,told,tnew,tr1,tr2);
double e=tnew-told;
double a=g1*tr1;
double b=(g1*tr2+g2*tr1)/2;
double c=g2*tr2/3;
i44=e*(a+e*(b+e*c))+told*e*(2*b+3*c*(e+told));
//i44=tnew*(g1*tr1+tnew*((g1*tr2+g2*tr1)/2+tnew*g2*tr2/3))-told*(g1*tr1+told*((g1*tr2+g2*tr1)/2+told*g2*tr2/3));
return i44;
}
/////////////////////////////////////////////////////////////////////////////////////////////
double i4new(const double gold,const double gnew,const double trold,const double trnew, const double told,const double tnew){
//Used ONLY when -v option for testing is on to see soome of the variables.
//Matrix M=rotmat(r2);
// integral: i4 = \int T(t) G(t) dt : T(t) is translation
double i44;
double g1,g2;
g1=0.0;g2=0.0;
coeff(gold,gnew,told,tnew,g1,g2);
double tr1,tr2;
tr1=0.0;tr2=0.0;
coeff(trold,trnew,told,tnew,tr1,tr2);
cout<<"coeff tr:"<<tr1<<" "<<tr2<<endl;
double e=tnew-told;
double a=g1*tr1;
double b=(g1*tr2+g2*tr1)/2;
double c=g2*tr2/3;
i44=e*(a+e*(b+e*c))+told*e*(2*b+3*c*(e+told));
//i44=tnew*(g1*tr1+tnew*((g1*tr2+g2*tr1)/2+tnew*g2*tr2/3))-told*(g1*tr1+told*((g1*tr2+g2*tr1)/2+told*g2*tr2/3));
return i44;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double I(const int h,const RowVector& r1,const RowVector& r2, const Matrix& g){
//Matrix M=rotmat(r2);
Matrix M=rotmat(r2);
Matrix A=axismat(r1);
Matrix AM=A*M;
Matrix A2M=A*AM;
Matrix v=g.Row(1)*M.Column(h)+g.Row(2)*AM.Column(h)+g.Row(3)*A2M.Column(h);
return v(1,1);
}
///////////////////////////////
double Inew(const int h,const RowVector& r1,const RowVector& r2, const Matrix& g){
//Used ONLY when -v option for testing is on to see soome of the variables.
//Matrix M=rotmat(r2);
Matrix M=rotmat(r2);
cout<<"M matrix "<<M<<endl;
Matrix A=axismat(r1);
cout<<"A matrix "<<A<<endl;
Matrix AM=A*M;
Matrix A2M=A*AM;
Matrix v=g.Row(1)*M.Column(h)+g.Row(2)*AM.Column(h)+g.Row(3)*A2M.Column(h);
return v(1,1);
}
////////////////////////////////
//STUFF FOR THE SORTER
////////////////////////////////////////////////////////////////////////////////////////////////
Matrix intertranslation(const double tx1,const double ty1,const double tz1,
const double tx2, const double ty2, const double tz2,
const ColumnVector vectortime){
// interpolate between two positions (translations)
// vectortime is a list of times to interpolate at *except* first point
// specifies when tx1,ty1,tz1 occurs and last point for tx2,ty2,tz2
// output is (tx,ty,tz) per row, with first row for *second* input time
// but still including last input time => (tx2,ty2,tz2)
int d=vectortime.Nrows();
double dtt=0;
Matrix inttra(d,3);
double dt=vectortime(d)-vectortime(1);
for (int k=1;k<=d-1;k++){
inttra(k,1)=dtt*(tx2-tx1)/dt+tx1;
inttra(k,2)=dtt*(ty2-ty1)/dt+ty1;
inttra(k,3)=dtt*(tz2-tz1)/dt+tz1;
dtt=dtt+vectortime(k+1)-vectortime(k);
}
inttra(d,1)=tx2;
inttra(d,2)=ty2;
inttra(d,3)=tz2;
return inttra;
}
///////////////////////////
Matrix intertranslation_old(const double tx1,const double ty1,const double tz1,
const double tx2, const double ty2, const double tz2,
const ColumnVector vectortime){
// interpolate between two positions (translations)
// vectortime is a list of times to interpolate at *except* first point
// specifies when tx1,ty1,tz1 occurs and last point for tx2,ty2,tz2
// output is (tx,ty,tz) per row, with first row for *second* input time
// but still including last input time => (tx2,ty2,tz2)
int d=vectortime.Nrows();
double dtt=0;
Matrix inttra(d,3);
double dt=vectortime(d)-vectortime(1);
for (int k=1;k<=d-1;k++){
dtt=dtt+vectortime(k+1)-vectortime(k);
inttra(k,1)=dtt*(tx2-tx1)/dt+tx1;
inttra(k,2)=dtt*(ty2-ty1)/dt+ty1;
inttra(k,3)=dtt*(tz2-tz1)/dt+tz1;
}
inttra(d,1)=inttra(d-1,1);
inttra(d,2)=inttra(d-1,2);
inttra(d,3)=inttra(d-1,3);
return inttra;
}
////////////////////
Matrix interrotation(const double a1,const double b1,const double c1,
const double a2,const double b2,const double c2,
const ColumnVector vectortime){
// as above but for rotations (input in euler angles, output in angle/axis)
int d=vectortime.Nrows();
Matrix introt(d,8);
RowVector aa(8);
RowVector q1=euler_to_quaternion(a1,b1,c1);
RowVector q2=euler_to_quaternion(a2,b2,c2);
RowVector q1c(4); //conjugate of q1
q1c <<q1(1)<<-q1(2)<<-q1(3)<<-q1(4);
RowVector q=mult_quaternions(q2,q1c);
RowVector qa=quaternion_to_angleaxis(q);
RowVector q1a=quaternion_to_angleaxis(q1);
RowVector q2a=quaternion_to_angleaxis(q2);
aa<<0.0<<qa(2)<<qa(3)<<qa(4)<<q1a(1)<<q1a(2)<<q1a(3)<<q1a(4);
introt.Row(1)=aa;
if (d>2){
double dtt=0;
double dt=vectortime(d)-vectortime(1);
RowVector angle(d-2);
for (int k=1;k<=d-2;k++){
dtt=dtt+vectortime(k+1)-vectortime(k);
angle(k)=dtt*qa(1)/dt;
aa<<angle(k)<<qa(2)<<qa(3)<<qa(4)<<q1a(1)<<q1a(2)<<q1a(3)<<q1a(4);
introt.Row(k+1)=aa;
}
}
aa<<0.0<<qa(2)<<qa(3)<<qa(4)<<q2a(1)<<q2a(2)<<q2a(3)<<q2a(4); //first angle just for help but it means nothing, with all zeros axis rot matrix witll be zero
introt.Row(d)=aa;//begining of new interval
return introt;
}
/////////////////////////////////////////////////////////////////////////////////////////////
Matrix interrotation_old(const double a1,const double b1,const double c1,
const double a2,const double b2,const double c2,
const ColumnVector vectortime){
// as above but for rotations (input in euler angles, output in angle/axis)
int d=vectortime.Nrows();
Matrix introt(d,8);
RowVector aa(8);
RowVector q1=euler_to_quaternion(a1,b1,c1);
RowVector q2=euler_to_quaternion(a2,b2,c2);
RowVector q1c(4); //conjugate of q1
q1c <<q1(1)<<-q1(2)<<-q1(3)<<-q1(4);
RowVector q=mult_quaternions(q2,q1c);
RowVector qa=quaternion_to_angleaxis(q);
RowVector q1a=quaternion_to_angleaxis(q1);
RowVector q2a=quaternion_to_angleaxis(q2);
if (d>2){
double dtt=0;
double dt=vectortime(d)-vectortime(1);
RowVector angle(d-2);
for (int k=1;k<=d-2;k++){
dtt=dtt+vectortime(k+1)-vectortime(k);
angle(k)=dtt*qa(1)/dt;
aa<<angle(k)<<qa(2)<<qa(3)<<qa(4)<<q1a(1)<<q1a(2)<<q1a(3)<<q1a(4);
introt.Row(k)=aa;
}
}
aa<<qa(1)<<qa(2)<<qa(3)<<qa(4)<<q1a(1)<<q1a(2)<<q1a(3)<<q1a(4);
introt.Row(d-1)=aa;
aa<<0.0<<qa(2)<<qa(3)<<qa(4)<<q2a(1)<<q2a(2)<<q2a(3)<<q2a(4); //first angle just for help but it means nothing, with all zeros axis rot matrix witll be zero
introt.Row(d)=aa;//begining of new interval
return introt;
}
////////////////////////////////////////////////////////////////////////////////
RowVector interpolation_gradients(const RowVector a,const RowVector b,const double c){
//interpolate between a (at t=ta) and b (t=tb), at time t=c
// NOTE: ta and tb are stored in the first elements of a and b
// rowvectors are from EPI sequence
RowVector p(8);
p=0;
p(1)=c;
if (fabs(b(1)-a(1))<1e-12) cout<<"Warning: gradients in the EPI sequence are spaced too close to each other"<<endl;
else{
p(6)=(c-a(1))*(b(6)-a(6))/(b(1)-a(1))+a(6);
p(7)=(c-a(1))*(b(7)-a(7))/(b(1)-a(1))+a(7);
p(8)=(c-a(1))*(b(8)-a(8))/(b(1)-a(1))+a(8);
}
return p;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void sorter(PMatrix& pulse, const Matrix& motion, const string pulsefile,const int opt_test){//ok
//matrix mainmatrix M is the output of this function
//(1)=time (s),
//(2)=rf angle(rad),(3)=rf frequency bandwidth df(Hz),(4)=rf center frequecy fc(Hz),
//(5)=readout (1/0),
//(6)=x gradient (T/m),(7)=y gradient (T/m),(8)=z gradient (T/m),
//(9)=Tx translation (m),(10)=Ty translation (m), (11)=Tz translation (m),
//(12)=b angle of rotation (rad),(13)=Bx,(14)=By,(15)=Bz rotation axis (m)---rotation of the interpolated motion point between A(k) and A(k+1)--relative to A(m)
//(16)=a angle of rotation (rad),(17)=Ax,(18)=Ay,(19)=Az rotation axis (m)---rotation at the control motion point A(k)
cout<<"Started the sorter"<<endl;
int dimp=pulse.Nrows();//size in time for pulse
int tp=1;// counter for the pulse seq
int dimm=motion.Nrows();//size in time for motion
//Counting the size of the new matrix
int ts=0;
for (int tm=1;tm<=dimm;tm++){
if (tp<=dimp){
while (tp<=dimp && motion(tm,1)-pulse.time(tp)>1e-6){
ts++;tp++;
}
ts++;
if (tp<=dimp && fabs(motion(tm,1)-pulse.time(tp))<1e-06){
tp++;
}
}
}
for (int k=tp;k<=dimp;k++){
ts++;
}
int dimnew=ts;
// Storing the order of the rows from pulse and motion into one column.
// tm is row number in motion matrix
// tp is row number in pulse matrix
// ts is row number in new, bigger, sorted matrix
// tm, tp and ts start at 1 ; stored values in ts2tm etc are row numbers that start at 1
vector<signed int> ts2tm(dimnew), ts2tp(dimnew), tm2ts(dimm), tp2ts(dimp);
ts=0;tp=1;
for (int tm=1;tm<=dimm;tm++){
if (tp<=dimp){
while (tp<=dimp && motion(tm,1)-pulse.time(tp)>1e-6){
ts++;
ts2tp[ts-1]=tp; tp2ts[tp-1]=ts;
tp++;
if (ts>1) ts2tm[ts-1]=-1;
}
ts++;
cout << "MJ TMP : setting tm2ts["<< tm-1<<"] ; and tp = " << tp<< endl;
ts2tm[ts-1]=tm; tm2ts[tm-1]=ts;
ts2tp[ts-1]=-1;
if (tp<=dimp && fabs(motion(tm,1)-pulse.time(tp))<1e-06){
ts2tp[ts-1]=tp; tp2ts[tp-1]=ts; tp++;
}
}
}
for (int k=tp;k<=dimp;k++){
ts++; ts2tp[ts-1]=k; tp2ts[k-1]=ts; ts2tm[ts-1]=-2;
}
cout<<"Pulse dim="<<dimp<<endl;
cout<<"Motion dim="<<dimm<<endl;
cout<<"Matrix dim="<<dimnew<<endl;
pulse.destroy();
cout << "MJ TMP Check 1" << endl;
pulse.ReSize(dimnew,19);
cout << "MJ TMP Check 2" << endl;
pulse=0.0;
cout << "MJ TMP Check 3" << endl;
read_binary_matrix(pulse,pulsefile);
cout << "MJ TMP Check 4" << endl;
// copy relevant info from either motion matrix or pulse matrix or both (interp later)
if (dimnew!=dimp){
for (int ts=dimnew;ts>=1;ts--){
if (ts2tp[ts-1]>0) { //copy
pulse.time(ts)=pulse.time(ts2tp[ts-1]);
for (int kk=2; kk<=8; kk++){
pulse(ts,kk)=pulse(ts2tp[ts-1],kk);
}
}
if (ts2tm[ts-1]>0) { //copy only the time as fun with quaternions comes later
pulse.time(ts)=motion(ts2tm[ts-1],1);
}
}
}
// MOTION FILL AND INTERPOLATE
cout << "MJ TMP Check 5" << endl;
// tmpvec that contains times
// relevant rows of motion matrix are tm-2 and tm-1
// tm2ts gives the row numbers in the sorted matrix to insert the new motion block
for (int tm=2; tm<=dimm; tm++) {
cout << "MJ TMP Check 5a (" << tm << "/"<< dimm << ")" << endl;
cout << "MJ TMP: tm2ts[tm-1] = " << tm2ts[tm-1] << "; tm2ts[tm-2] = " << tm2ts[tm-2] << endl;
if (tm2ts[tm-1]>0) {
ColumnVector tmpvec(tm2ts[tm-1]-tm2ts[tm-2]+1);
cout << "MJ TMP Check 5aa" << endl;
for (int cc=1, ts=tm2ts[tm-2]; ts<=tm2ts[tm-1]; ts++) {
tmpvec(cc++)=pulse.time(ts);
}
cout << "MJ TMP Check 5b" << endl;
if (tm==2) { cout << "MJ TMP : tmpvec = " << tmpvec << endl; }
Matrix R=interrotation(motion(tm-1,5),motion(tm-1,6),motion(tm-1,7),motion(tm,5),motion(tm,6),motion(tm,7),tmpvec);
for (int kk=1;kk<=8;kk++){
for (int ll=1;ll<=tmpvec.Nrows();ll++){
pulse(tm2ts[tm-2]+ll-1,kk+11)=R(ll,kk);
}
}
if (tm==2) { cout << "MJ TMP : R = " << R << endl; }
cout << "MJ TMP Check 5c" << endl;
if (tm==2) { cout << "MJ TMP : pulse = " << pulse(1,9)*1e6 <<","<< pulse(1,10)*1e6 <<","<< pulse(1,11)*1e6 << endl; }
Matrix T=intertranslation(motion(tm-1,2),motion(tm-1,3),motion(tm-1,4),motion(tm,2),motion(tm,3),motion(tm,4),tmpvec);
for (int kk=1;kk<=3;kk++){
for (int ll=1;ll<=tmpvec.Nrows();ll++){
pulse(tm2ts[tm-2]+ll-1,kk+8)=T(ll,kk);
}
}
if (tm==2) { cout << "MJ TMP : pulse = " << pulse(1,9)*1e6 <<","<< pulse(1,10)*1e6 <<","<< pulse(1,11)*1e6 << endl; }
if (tm==2) { cout << "MJ TMP : T = " << T*1e6 << endl; }
}
}
cout << "MJ TMP Check 6" << endl;
cout << "MJ TMP Check 6b : tm2ts[0] = " << tm2ts[0] << endl;
// fill in the ends in case motion points don't start or end
for (int ts=1; ts<=tm2ts[0]-1; ts++) { // start
for (int kk=9;kk<=19;kk++) { pulse(ts,kk)=pulse(tm2ts[0],kk); }
}
cout << "MJ TMP : pulse (mid) = " << pulse(1,9)*1e6 <<","<< pulse(1,10)*1e6 <<","<< pulse(1,11)*1e6 << endl;
unsigned int tm2ts_max = *std::max_element(tm2ts.begin(), tm2ts.end());
for (int ts=tm2ts_max+1; ts<=dimnew; ts++) { // end
// for (int ts=tm2ts[dimm]+1; ts<=dimnew; ts++) { // end // MJ TMP - removed as dimm might go beyond valid part
for (int kk=9;kk<=19;kk++) { pulse(ts,kk)=pulse(tm2ts_max,kk); }
}
cout << "MJ TMP : pulse (post) = " << pulse(1,9)*1e6 <<","<< pulse(1,10)*1e6 <<","<< pulse(1,11)*1e6 << endl;
// GRADIENT INTERPOLATION
cout << "MJ TMP Check 7" << endl;
RowVector a(8), b(8), G(8);
for (int tp=2; tp<=dimp; tp++) {
for (int ts=tp2ts[tp-2]+1; ts<=tp2ts[tp-1]-1; ts++) {
int prevtp=tp2ts[tp-2];
a<<pulse.time(prevtp)<<pulse(prevtp,2)<<pulse(prevtp,3)<<pulse(prevtp,4)<<pulse(prevtp,5)<<pulse(prevtp,6)<<pulse(prevtp,7)<<pulse(prevtp,8);
int nexttp=tp2ts[tp-1];
b<<pulse.time(nexttp)<<pulse(nexttp,2)<<pulse(nexttp,3)<<pulse(nexttp,4)<<pulse(nexttp,5)<<pulse(nexttp,6)<<pulse(nexttp,7)<<pulse(nexttp,8);
G=interpolation_gradients(a,b,pulse.time(ts));
for (int kk=2;kk<=8;kk++){
pulse(ts,kk)=G(kk);
}
}
}
cout << "MJ TMP Check 8" << endl;
}
//////////
Matrix sorter_old(const Matrix& epi,const Matrix& motion){//ok
//matrix mainmatrix M is the output of this function
//(1)=time (s),
//(2)=rf angle(rad),(3)=rf frequency bandwidth df(Hz),(4)=rf center frequecy fc(Hz),
//(5)=readout (1/0),
//(6)=x gradient (T/m),(7)=y gradient (T/m),(8)=z gradient (T/m),
//(9)=Tx translation (m),(10)=Ty translation (m), (11)=Tz translation (m),
//(12)=b angle of rotation (rad),(13)=Bx,(14)=By,(15)=Bz rotation axis (m)---rotation of the interpolated motion point between A(k) and A(k+1)--relative to A(m)
//(16)=a angle of rotation (rad),(17)=Ax,(18)=Ay,(19)=Az rotation axis (m)---rotation at the control motion point A(k)
int dim1=epi.Nrows();//EPI
int t1=2;
int dim2=motion.Nrows();//MOTION
Matrix mainmatrix(dim1+dim2*2,19);//MAINMATRIX
mainmatrix=0;
int t=1;
for (int t2=1;t2<=dim2-1;t2++){
cout<<"Motion counter= "<<t2<<" till "<<dim2-1<<endl;
ColumnVector timevector(dim1+dim2);
timevector=0;
int l=1;
timevector(l)=motion(t2,1);
while (motion(t2+1,1)-epi(t1,1)>1e-10){
l=l+1;
timevector(l)=epi(t1,1);
t1=t1+1;
}
l=l+1;
timevector(l)=motion(t2+1,1);
int tmp6=0;
RowVector G(8);
G=0;
if (fabs(motion(t2+1,1)-epi(t1,1))<=1e-10){
t1=t1+1;
tmp6=0;
}
else{
G=interpolation_gradients(epi.Row(t1-1),epi.Row(t1),motion(t2+1,1));
tmp6=1;
}
Matrix R=interrotation_old(motion(t2,5),motion(t2,6),motion(t2,7),motion(t2+1,5),motion(t2+1,6),motion(t2+1,7),timevector.Rows(1,l));
Matrix T=intertranslation_old(motion(t2,2),motion(t2,3),motion(t2,4),motion(t2+1,2),motion(t2+1,3),motion(t2+1,4),timevector.Rows(1,l));
for (int tt=1;tt<=l-2;tt++){
t=t+1;
RowVector tmp1(19);
tmp1.Columns(1,8)=epi.Row(t1-l+tt+tmp6);
tmp1.Columns(9,11)=T.Row(tt);
tmp1.Columns(12,19)=R.Row(tt);
mainmatrix.Row(t)=tmp1;
}
t=t+1;
if (tmp6==1){