-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateHeaderCode.m
152 lines (138 loc) · 6.52 KB
/
generateHeaderCode.m
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
function generateHeaderCode(N, matlab_code)
%GENERATEHEADERCODE(N) outputs a header file for calculating a 3D-SWIG
% trajectory of order N. If N is an array with more than one element,
% the header file will contain multiple functions to calculate
% trajectories of different orders.
%
% Options:
% - matlab_code: Output a MATLAB version of the code for validation.
%
if nargin < 2
matlab_code = false;
end
if ~exist([pwd filesep 'headers'], 'dir')
mkdir([pwd filesep 'headers']);
end
fid = fopen(['headers' filesep 'swig3d.h'], 'w+');
[~, curfilename, ~] = fileparts(mfilename('fullpath'));
rfid = fopen('reference.txt');
reference = fgetl(rfid);
fclose(rfid);
fprintf(fid, '/**\n');
fprintf(fid, '* \\brief Look-up tables for 3D-SWIG trajectories\n');
fprintf(fid, '* \n');
fprintf(fid, '* This code was auto-generated by the MATLAB function %s.m\n', curfilename);
fprintf(fid, '* Calculation of dMPhi1 and dMPhi2 must be handled by the sequence code, e.g.:\n');
fprintf(fid, '* \n');
fprintf(fid, '* > const double phi1 = 0.465571231876768\n');
fprintf(fid, '* > const double phi2 = 0.682327803828019\n');
fprintf(fid, '* > double u_dMPhi1 = fmod(lReorderIndex*phi1, 1.0);\n');
fprintf(fid, '* > double u_dMPhi2 = fmod(lReorderIndex*phi2, 1.0);\n');
fprintf(fid, '* \n');
fprintf(fid, '* If this code is useful for your research, please consider citing:\n');
fprintf(fid, '* %s\n', reference);
fprintf(fid, '* \n');
fprintf(fid, '* 2023 – Alexander Fyrdahl, PhD ([email protected])\n');
fprintf(fid, '*/\n');
for n = N
fprintf(fid, '\nvoid fSWIGTraj%i(long u_lTrig, double dMPhi1, double dMPhi2, double* x, double* y, double *z)\n{\n',12*n^2);
if matlab_code
wedges = 12*n^2;
fidm = fopen(sprintf('headers/fSWIGTraj%i.m', wedges), 'w+');
fprintf(fidm, '%%FSWIGTRAJ%i Look-up tables for a %i-wedge 3D-SWIG trajectory\n', wedges, wedges);
fprintf(fidm, '%% NOTE: This code uses 0-indexing to be consistent with the C++ code\n');
fprintf(fidm, '%% If this code is useful for your research, please consider citing:\n');
fprintf(fidm, '%% %s\n', reference);
fprintf(fidm, '%%\n');
fprintf(fidm, '%% 2023 – Alexander Fyrdahl, PhD ([email protected])\n');
fprintf(fidm, '%%\n\n');
fprintf(fidm, 'function [x, y, z] = fSWIGTraj%i(u_lTrig, dMPhi1, dMPhi2)\n', wedges);
end
[grid, idx] = utils.createCubeGrid(n);
grid(grid > 0) = 1;
grid( 1:n, n+1:end-n) = 2;
grid( n+1:end-n, 1:n) = 3;
grid( n+1:end-n, end-n+1:end) = 4;
grid(end-n+1:end, n+1:end-n) = 5;
fprintf(fid, ' switch(u_lTrig%%%i) {\n', 12*n^2);
if matlab_code; fprintf(fidm, ' switch mod(u_lTrig, %i)\n', 12*n^2); end
for beat = 0:12*n^2-1
fprintf(fid, ' case(%i):\n', beat);
if matlab_code; fprintf(fidm, ' case %i\n', beat); end
[row_index, col_index] = ind2sub(size(grid), idx(beat+1));
if grid(row_index, col_index) == 1 % Top face
fprintf(fid, ' *x = (-dMPhi1%s)/%i;\n', pps(row_index - 2*n), n);
fprintf(fid, ' *y = (dMPhi2%s)/%i;\n', pps(col_index - 2*n - 1), n);
fprintf(fid, ' *z = 1;\n');
if matlab_code
fprintf(fidm, ' x = (-dMPhi1%s)/%i;\n', pps(row_index - 2*n), n);
fprintf(fidm, ' y = (dMPhi2%s)/%i;\n', pps(col_index - 2*n - 1), n);
fprintf(fidm, ' z = 1;\n');
end
else % Sides
if grid(row_index, col_index) == 2
fprintf(fid, ' *x = -1;\n');
fprintf(fid, ' *y = (dMPhi1%s)/%i;\n', pps(col_index - 2*n - 1), n);
fprintf(fid, ' *z = (dMPhi2%s)/%i;\n', pps(row_index - 1), n);
if matlab_code
fprintf(fidm, ' x = -1;\n');
fprintf(fidm, ' y = (dMPhi1%s)/%i;\n', pps(col_index - 2*n - 1), n);
fprintf(fidm, ' z = (dMPhi2%s)/%i;\n', pps(row_index - 1), n);
end
elseif grid(row_index, col_index) == 3
fprintf(fid, ' *x = (-dMPhi1%s)/%i;\n', pps(row_index - 2*n), n);
fprintf(fid, ' *y = -1;\n');
fprintf(fid, ' *z = (dMPhi2%s)/%i;\n', pps(col_index - 1), n);
if matlab_code
fprintf(fidm, ' x = (-dMPhi1%s)/%i;\n', pps(row_index - 2*n), n);
fprintf(fidm, ' y = -1;\n');
fprintf(fidm, ' z = (dMPhi2%s)/%i;\n', pps(col_index - 1), n);
end
elseif grid(row_index, col_index) == 4
fprintf(fid, ' *x = (-dMPhi1%s)/%i;\n', pps(row_index - 2*n), n);
fprintf(fid, ' *y = 1;\n');
fprintf(fid, ' *z = (dMPhi2%s)/%i;\n', pps(4*n - col_index), n);
if matlab_code
fprintf(fidm, ' x = (-dMPhi1%s)/%i;\n', pps(row_index - 2*n), n);
fprintf(fidm, ' y = 1;\n');
fprintf(fidm, ' z = (dMPhi2%s)/%i;\n', pps(4*n - col_index), n);
end
elseif grid(row_index, col_index) == 5
fprintf(fid, ' *x = 1;\n');
fprintf(fid, ' *y = (dMPhi1%s)/%i;\n', pps(col_index - 2*n - 1), n);
fprintf(fid, ' *z = (dMPhi2%s)/%i;\n', pps(4*n - row_index), n);
if matlab_code
fprintf(fidm, ' x = 1;\n');
fprintf(fidm, ' y = (dMPhi1%s)/%i;\n', pps(col_index - 2*n - 1), n);
fprintf(fidm, ' z = (dMPhi2%s)/%i;\n', pps(4*n - row_index), n);
end
end
end
fprintf(fid, ' break;\n');
end
fprintf(fid, ' default:\n');
fprintf(fid, ' *x = 0.0;\n');
fprintf(fid, ' *y = 0.0;\n');
fprintf(fid, ' *z = 0.0;\n');
fprintf(fid, ' break;\n');
fprintf(fid,' }\n');
fprintf(fid,'}\n');
if matlab_code
fprintf(fidm, ' otherwise\n');
fprintf(fidm, ' x = 0;\n');
fprintf(fidm, ' y = 0;\n');
fprintf(fidm, ' z = 0;\n');
fprintf(fidm, ' end\nend\n');
fclose(fidm);
end
end
fclose(fid);
end
function str = pps(x)
% Print Prettier Strings
if x == 0
str = '';
else
str = sprintf('%+i',x);
end
end