forked from RascalSoftware/RAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriggerEvent.m
More file actions
141 lines (126 loc) · 6.43 KB
/
Copy pathtriggerEvent.m
File metadata and controls
141 lines (126 loc) · 6.43 KB
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
function triggerEvent(eventType, varargin)
% Triggers the event type with the given varargin. The supported event types are
% 0, 1, and 2.
% * The input for the message event is a char array,
% * The inputs for the plot event are the result struct, problem struct and cell
% * The inputs for progress events are the message (char array) and
% percentage progress expressed as a decimal (i.e., between 0 and 1).
%
% triggerEvent(coderEnums.eventTypes.Message, 'Hello world');
% triggerEvent(coderEnums.eventTypes.Plot, result, problemStruct);
% triggerEvent(coderEnums.eventTypes.Progress, 'Hello world', 0.5);
persistent notified;
persistent helper;
initialised = false;
hasPlotHandler = false;
if isempty(notified)
notified = false;
end
coder.extrinsic('textProgressBar');
coder.extrinsic('eventManager.notify')
if coder.target('MATLAB') || coder.target('MEX')
if eventType == coderEnums.eventTypes.Message
fprintf("%s", varargin{1});
elseif eventType == coderEnums.eventTypes.Progress
textProgressBar(varargin{1}, varargin{2});
elseif eventType == coderEnums.eventTypes.Plot
result = varargin{1};
problemStruct = varargin{2};
plotData.reflectivity = result.reflectivity;
plotData.shiftedData = result.shiftedData;
plotData.subRoughs = result.contrastParams.subRoughs;
plotData.resample = problemStruct.resample;
plotData.dataPresent = problemStruct.dataPresent;
plotData.modelType = problemStruct.modelType;
plotData.contrastNames = problemStruct.names.contrasts;
[slds, resampledLayers, ~] = alignALProfiles(problemStruct.geometry, problemStruct.modelType, result.sldProfiles, result.resampledLayers, {});
plotData.sldProfiles = slds;
plotData.resampledLayers = resampledLayers;
eventManager.notify(eventType, plotData);
end
else
coder.cinclude('eventHelper.hpp');
coder.updateBuildInfo('addLinkFlags','-ldl');
if isempty(helper)
% Declaration for coder
helper = coder.opaque('eventHelper','NULL','HeaderFile','eventHelper.hpp');
% Make an instance
helper = coder.ceval('eventHelper');
path = [getenv('RAT_PATH'), 0];
coder.ceval('std::mem_fn(&eventHelper::init)', helper, path);
end
initialised = coder.ceval('std::mem_fn(&eventHelper::isInitialised)', helper);
if initialised
if eventType == coderEnums.eventTypes.Message
coder.ceval('std::mem_fn(&eventHelper::sendMessage)', helper, [varargin{1},0]);
elseif eventType == coderEnums.eventTypes.Progress
coder.ceval('std::mem_fn(&eventHelper::updateProgress)', helper, [varargin{1},0], varargin{2});
elseif eventType == coderEnums.eventTypes.Plot
result = varargin{1};
problemStruct = varargin{2};
[slds, resampledLayers, ~] = alignALProfiles(problemStruct.geometry, problemStruct.modelType, result.sldProfiles, result.resampledLayers, {});
result.sldProfiles = slds;
result.resampledLayers = resampledLayers;
subRoughs = result.contrastParams.subRoughs;
nContrast = length(result.reflectivity);
[reflect, nReflect] = packCellArray(result.reflectivity, 1);
[shiftedData, nShiftedData] = packCellArray(result.shiftedData, 1);
[sldProfiles, nSldProfiles] = packCellArray(result.sldProfiles, 1);
[layers, nLayers] = packCellArray(result.resampledLayers, 1);
names = problemStruct.names.contrasts;
contrastNames = strjoin(names, '');
nContrastNames = strlength(names);
switch problemStruct.TF
case coderEnums.calculationTypes.Domains
[sldProfiles2, nSldProfiles2] = packCellArray(result.sldProfiles, 2);
[layers2, nLayers2] = packCellArray(result.resampledLayers, 2);
otherwise
sldProfiles2 = coder.nullcopy(zeros(0));
nSldProfiles2 = coder.nullcopy(zeros(0));
layers2 = coder.nullcopy(zeros(0));
nLayers2 = coder.nullcopy(zeros(0));
end
modelType = [problemStruct.modelType, 0];
resample = problemStruct.resample;
dataPresent = problemStruct.dataPresent;
coder.ceval('std::mem_fn(&eventHelper::updatePlot)', helper, nContrast, reflect, nReflect, shiftedData, ...
nShiftedData, sldProfiles, nSldProfiles, layers, nLayers, sldProfiles2, nSldProfiles2, layers2, ...
nLayers2, subRoughs, resample, dataPresent, modelType, contrastNames, nContrastNames);
end
notified = false;
else
% This avoids printing the error message multiple times during the optimization.
if ~notified
fprintf(2, "\neventManager library could be loaded. Check that the dynamic library is present in the compile/events folder.\n");
notified = true;
end
if eventType == coderEnums.eventTypes.Message
fprintf("%s", varargin{1});
end
end
end
end
function [packedArray, dims] = packCellArray(cellArray, col)
% Packs a specified column of a cell array with different sized arrays into a
% single row array and an array of the dimensions for each cell. For the example
% below packedArray will be [1, 2, 3, 4, 5, 6, 7] and dims will be [3, 1, 4, 1]
%
% [packedArray, dims] = packCellArray({[1; 2; 3], [4; 5; 6; 7]}, 1);
rowSize = 0;
nCells = size(cellArray, 1);
dims = zeros(nCells*2, 1);
for i=1:nCells
shape = size(cellArray{i, col});
index = 2*i-1;
dims(index:index+1, :) = shape;
rowSize = rowSize + prod(shape);
end
start = 1;
packedArray = zeros(rowSize, 1);
for i=1:nCells
index = 2*i-1;
stop = start + prod(dims(index:index+1, 1));
packedArray(start:stop-1, :) = reshape(cellArray{i, col}, [], 1);
start = stop;
end
end