function hiso = volume_plot(xStart, xStep, v, isoVal)
% function to do a nice plot of 3d scalar volume data, along the lines of
% http://www.mathworks.com/help/matlab/visualize/techniques-for-visualizing-scalar-volume-data.html
%
% Mike McCann July 2015

xSize = size(v);

D = length(xSize);

if isempty(xStart)
	xStart = zeros(1, D);
end

if isempty(xStep)
	xStep = ones(1, D);
end


vals = cell(D, 1);
for d = 1:D
	vals{d} = (0:xSize(d)-1)*xStep(d) + xStart(d);
end

% smooth the data
s = smooth3(v, 'gaussian', 5);

% iso surface
hiso = patch(isosurface(vals{1}, vals{2}, vals{3}, permute(s, [2 1 3]), isoVal),...
	'FaceColor',[.65,.75,.65],...
	'EdgeColor','none');
xlabel('x_0', 'rot', 0);
ylabel('x_1', 'rot', 0);
zlabel('x_2', 'rot', 0);

% normals
% seems to not work, causes the shading to be very dark
isonormals(vals{1}, vals{2}, vals{3}, permute(s, [2 1 3]), hiso)


xlim([vals{1}(1), vals{1}(end)]);
ylim([vals{2}(1), vals{2}(end)]);
zlim([vals{3}(1), vals{3}(end)]);


% plot origin and axes
hold on
%plot3(0,0,0, '.')
plot3(vals{1}, zeros(size(vals{1})), zeros(size(vals{1})), 'linewidth', 3)
plot3(zeros(size(vals{2})), vals{2}, zeros(size(vals{2})), 'linewidth', 3)
plot3(zeros(size(vals{3})), zeros(size(vals{3})), vals{3}, 'linewidth', 3)
hold off

%xlim( [-rWidth/2, rWidth/2] )
%ylim( [-rWidth/2, rWidth/2] )
%zlim( [-rWidth/2, rWidth/2] )
daspect([1 1 1]);



% cutaways
%hcap = patch(isocaps(vals{2}, vals{1}, vals{3}, v,isoVal),...
%	'FaceColor','interp',...
%	'EdgeColor','none');

% light
lighting gouraud


lightangle(45,30);
hcap.AmbientStrength = 0.6;
hiso.SpecularColorReflectance = 0;
hiso.SpecularExponent = 50;