-
Notifications
You must be signed in to change notification settings - Fork 0
/
r5_makeSonoNerfResultPlot.m
216 lines (171 loc) · 8.17 KB
/
r5_makeSonoNerfResultPlot.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
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
%% Init:
clear
close all
clc
path(pathdef);
addpath( genpath( 'Source' ) )
%% Load a sononerf, a 3D model, etc
load('DataCalculated/TrainedSonoNERFs/sonoNerfTrained - UALogo.mat')
[ surfaceFaces, surfaceVertices ] = stlreadNonCleaning("Data/Models/UALogo.stl");
surfaceVertices = surfaceVertices - mean(surfaceVertices);
surfaceVertices = surfaceVertices* roty(0);
surfaceVertices = surfaceVertices/ 35;
thresholdEstimator = 27000;
limitsXComparison = [-0.1 0.1];
limitsYComparison = [ -0.25 0.22 ];
limitsZComparison = [ -0.15 0.18];
chunckSelectVec = [ 230 280 120];
limitsX = [-0.3 0.4];
limitsY = [ -0.4 0.4 ];
limitsZ = [ -0.2 0.2];
% load('DataCalculated/TrainedSonoNERFs/sonoNerfTrained - leafAndDragonFly.mat') % This is leaf with DF
% [ surfaceFaces, surfaceVertices ] = stlreadNonCleaning("Data/Models/leafAndDragonfly.stl");
% surfaceVertices = surfaceVertices - mean(surfaceVertices);
% surfaceVertices = surfaceVertices * roty(0);
% surfaceVertices = surfaceVertices/ 500;
% thresholdEstimator = 1000;
% limitsXComparison = [-0.1 0.1];
% limitsYComparison = [ -0.25 0.22 ];
% limitsZComparison = [ -0.21 0.18];
% chunckSelectVec = [ 230 280 120];
% limitsX = [-0.3 0.4];
% limitsY = [ -0.4 0.4 ];
% limitsZ = [ -0.2 0.2];
% load('DataCalculated/TrainedSonoNERFs/sonoNerfTrained - tripleBall.mat')
% [ surfaceFaces, surfaceVertices ] = stlreadNonCleaning("Data/Models/tripleBall.stl");
% surfaceVertices = surfaceVertices - mean(surfaceVertices);
% surfaceVertices = surfaceVertices* roty(0);
% surfaceVertices = surfaceVertices/ 35;
% thresholdEstimator = 20000;
% limitsXComparison = [-0.1 0.1];
% limitsYComparison = [ -0.15 0.18 ];
% limitsZComparison = [ -0.15 0.15];
% chunckSelectVec = [ 230 280 120];
% limitsX = [-0.3 0.4];
% limitsY = [ -0.4 0.4 ];
% limitsZ = [ -0.2 0.2];
[ batFaces, batVertices ] = stlreadNonCleaning("Data/Models/singleBall.stl" );
batVertices = batVertices - mean(batVertices);
batVertices = batVertices* rotz(-90);
batVertices = batVertices* roty(-20);
batVertices = batVertices / 300;
batVertices = batVertices + [ 0 0 0.01];
%% Inference: preparation
frequenciesERTF = structSonoNERF.SonoNERF.Layers(2).frequencySamples(1:end/2);
numFrequencies = length( frequenciesERTF );
directionsERTF = structSonoNERF.SonoNERF.Layers(2).directionsSampling;
%% Chuck Selection
numChunkPlots = length( chunckSelectVec );
posesBat = zeros( numChunkPlots, 6 );
for cntChunk = 1 : numChunkPlots
dataChunk = structSonoNERF.structSonoNERFData.dataChunkMatrix{chunckSelectVec(cntChunk)};
poseIn = dataChunk.inputData(1:6,1);
posesBat( cntChunk, : ) = poseIn(:);
end
posBatAllMajor = posesBat( :, 1:3)';
orientBatAllMajor = posesBat( :, 4:6)';
%% Show the original model with the poses for the chunks
cameraView = [57.5755 30.5700];
figPos = [-3394 228 1291 888];
figure;
set( gcf, 'position', figPos )
hp = patch('faces', surfaceFaces, 'vertices', surfaceVertices, 'FaceColor', [0.2 0.3 0.1], 'EdgeAlpha', 0.1);
hold on;
for posBatCnt = 1 : numChunkPlots
hpBat = drawTransformedBat(posBatAllMajor(:, posBatCnt), orientBatAllMajor( :, posBatCnt ), batFaces, batVertices);
hpBat.FaceLighting = 'gouraud';
hpBat.AmbientStrength = 1;
hpBat.DiffuseStrength = 0.8;
hpBat.SpecularStrength = 0.2;
hpBat.SpecularExponent = 25;
hpBat.BackFaceLighting = 'unlit';
end
hold off
axis equal
axis tight
grid on
xlim( limitsX )
ylim( limitsY );
zlim( limitsZ );
set( gca, 'view', cameraView )
lightangle(-45,30)
hp.FaceLighting = 'gouraud';
hp.AmbientStrength = 1;
hp.DiffuseStrength = 0.8;
hp.SpecularStrength = 0.2;
hp.SpecularExponent = 25;
hp.BackFaceLighting = 'unlit';
light('Position', [-1 0 1], 'Style', 'local');
light('Position', [1 0.5 -0.5], 'Style', 'local');
xlabel( 'X-axis (m)' )
ylabel( 'Y-axis (m)' )
zlabel( 'Z-axis (m)' )
set( gca, 'linewidth', 1.5)
set( gca, 'fontsize', 14)
%% Inference: Spectrogram
figure;
set( gcf, 'position', [-3791 743 2118 446]);
for cntChunk = 1 : numChunkPlots
dataTest = structSonoNERF.structSonoNERFData.dataChunkMatrix{ chunckSelectVec( cntChunk )};
specgramPred = structSonoNERF.SonoNERF.predict( dataTest.inputData' )';
subplot(2,numChunkPlots,cntChunk )
imagesc(specgramPred)
title('SonoNERF Prediction' )
subplot(2,numChunkPlots,cntChunk + numChunkPlots )
imagesc(dataTest.outputData / structSonoNERF.scalerOutput)
title('Ground Truth' )
end
%% Inference: Extract IsoSurface
xVec = -0.3 : 0.005: 0.3;
yVec = -0.1 : 0.005 : 0.1;
zVec = -0.2 : 0.005 : 0.2;
[ xGrid, yGrid, zGrid ] = meshgrid( yVec, xVec, zVec );
directionsInterrogation = eq_point_set( 2, 100 );
[ azInterrogation, elInterrogation, rInterrogation ] = cart2sph( directionsInterrogation(1,:), directionsInterrogation(2,:), directionsInterrogation(3,:) );
idxFreqCombiner = [ 1 : 10 ; 11 : 20 ; 21 : 30 ; 31 : 40 ];
dataStorageMatrix = zeros( length(xGrid(:)), length( azInterrogation ) );
for cntDirection = 1 : length( azInterrogation )
cntDirection
curAz = azInterrogation( cntDirection );
curEl = elInterrogation( cntDirection );
dataIn = gpuArray( [ xGrid(:) yGrid(:) zGrid(:) ones( size(zGrid(:) ) )*curEl ones( size(zGrid(:) ) )*curAz ] );
volumePredictedVecRaw = gather( structSonoNERF.SonoNERF.Layers(2).modelSonoNerf.predict(dataIn) );
% volumePredictedVecRaw = gather( net.Layers(2).modelSonoNerf.predict(dataIn) );
volumePredictedVecComplex = volumePredictedVecRaw(:,1:end/2) + 1i*volumePredictedVecRaw(:,end/2+1:end);
% volumePredictedVecEnergy = sqrt( sum( abs(volumePredictedVecComplex), 2 ) );
% volumePredictedVecEnergy = sum( abs(volumePredictedVecComplex(:, idxFreqCombiner(4,:)) ), 2 );
volumePredictedVecEnergy = sum( abs(volumePredictedVecComplex(:, :) ), 2 );
dataStorageMatrix( :, cntDirection ) = volumePredictedVecEnergy;
end
%% Render the full thing in 3D!!
volumePredictedVecEnergy = sum( dataStorageMatrix, 2 );
volumePredictedEnergy = reshape( volumePredictedVecEnergy, [ length(xVec) length(yVec) length(zVec)] );
volumePredictedEnergy = smooth3( volumePredictedEnergy, 'box', 9 );
[faces,verts] = isosurface(xGrid, yGrid, zGrid, volumePredictedEnergy, thresholdEstimator);
surfaceMeshExtracted = surfaceMesh( verts, faces );
surfaceMeshSmooth = smoothSurfaceMesh(surfaceMeshExtracted,1);
% surfaceMeshShow(surfaceMeshSmooth,Title="Extracted SonoNERF Surface")
figure;
% set( gcf, 'position', [ -3494 214 942 973])
hpRec = patch('faces',surfaceMeshSmooth.Faces,'vertices', surfaceMeshSmooth.Vertices, 'FaceColor', [0.2 0.3 0.1], 'EdgeAlpha', 0.1);
lightangle(-45,30)
hpRec.FaceLighting = 'gouraud';
hpRec.AmbientStrength = 1;
hpRec.DiffuseStrength = 0.8;
hpRec.SpecularStrength = 0.2;
hpRec.SpecularExponent = 25;
hpRec.BackFaceLighting = 'unlit';
light('Position', [-1 0 1], 'Style', 'local');
light('Position', [1 0.5 -0.5], 'Style', 'local');
axis equal
axis tight
grid on
xlim( limitsXComparison )
ylim( limitsYComparison );
zlim( limitsZComparison );
set( gca, 'view', cameraView )
xlabel( 'X-axis (m)' )
ylabel( 'Y-axis (m)' )
zlabel( 'Z-axis (m)' )
set( gca, 'linewidth', 1.5)
set( gca, 'fontsize', 14)