From 00e40a93d11d225f774be314e42f108f7766d5f2 Mon Sep 17 00:00:00 2001 From: Gilberto Galvis Date: Tue, 19 Oct 2021 08:45:05 -0400 Subject: [PATCH] adding isosurface functionality to matlab_plotly --- plotly/plotlyfig_aux/core/updateData.m | 2 + .../handlegraphics/updateIsosurface.m | 190 ++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 plotly/plotlyfig_aux/handlegraphics/updateIsosurface.m diff --git a/plotly/plotlyfig_aux/core/updateData.m b/plotly/plotlyfig_aux/core/updateData.m index 0df2b26b..f474a96c 100644 --- a/plotly/plotlyfig_aux/core/updateData.m +++ b/plotly/plotlyfig_aux/core/updateData.m @@ -95,6 +95,8 @@ updateTernaryPlotPro(obj, dataIndex); elseif ismember('ternpcolor', lower(obj.PlotOptions.TreatAs)) updateTernaryPlotPro(obj, dataIndex); + elseif ismember('isosurface', lower(obj.PlotOptions.TreatAs)) + updateIsosurface(obj, dataIndex); else updatePatch(obj, dataIndex); end diff --git a/plotly/plotlyfig_aux/handlegraphics/updateIsosurface.m b/plotly/plotlyfig_aux/handlegraphics/updateIsosurface.m new file mode 100644 index 00000000..de2298a9 --- /dev/null +++ b/plotly/plotlyfig_aux/handlegraphics/updateIsosurface.m @@ -0,0 +1,190 @@ +function obj = updateIsosurface(obj, isoIndex) + + %-------------------------------------------------------------------------% + + %-INITIALIZATIONS-% + + axIndex = obj.getAxisIndex(obj.State.Plot(isoIndex).AssociatedAxis); + plotData = get(obj.State.Plot(isoIndex).Handle); + axisData = get(plotData.Parent); + [xSource, ySource] = findSourceAxis(obj, axIndex); + + %-update scene-% + updateScene(obj, isoIndex) + + %-get mesh data-% + xData = plotData.Vertices(:, 1); + yData = plotData.Vertices(:, 2); + zData = plotData.Vertices(:, 3); + + iData = plotData.Faces(:, 1) - 1; + jData = plotData.Faces(:, 2) - 1; + kData = plotData.Faces(:, 3) - 1; + + %-------------------------------------------------------------------------% + + %-get trace-% + obj.data{isoIndex}.type = 'mesh3d'; + obj.data{isoIndex}.name = plotData.DisplayName; + obj.data{isoIndex}.showscale = false; + + %-------------------------------------------------------------------------% + + %-set mesh data-% + obj.data{isoIndex}.x = xData; + obj.data{isoIndex}.y = yData; + obj.data{isoIndex}.z = zData; + + obj.data{isoIndex}.i = iData; + obj.data{isoIndex}.j = jData; + obj.data{isoIndex}.k = kData; + + %-------------------------------------------------------------------------% + + %-mesh coloring-% + faceColor = getFaceColor(plotData, axisData); + + if iscell(faceColor) + obj.data{isoIndex}.facecolor = faceColor; + else + obj.data{isoIndex}.color = faceColor; + end + + %-------------------------------------------------------------------------% + + %-lighting settings-% + if ~strcmp(plotData.FaceLighting, 'flat') + obj.data{isoIndex}.lighting.diffuse = plotData.DiffuseStrength; + obj.data{isoIndex}.lighting.ambient = plotData.AmbientStrength; + obj.data{isoIndex}.lighting.specular = plotData.SpecularStrength; + obj.data{isoIndex}.lighting.roughness = 0.2; + obj.data{isoIndex}.lighting.fresnel = 0.5; + obj.data{isoIndex}.lighting.vertexnormalsepsilon = 1e-12; + obj.data{isoIndex}.lighting.facenormalsepsilon = 1e-6; + end + + %-------------------------------------------------------------------------% + + %-associate scene to trace-% + obj.data{isoIndex}.scene = sprintf('scene%d', xSource); + + %-------------------------------------------------------------------------% +end + +function updateScene(obj, isoIndex) + + %-INITIALIZATIONS-% + axIndex = obj.getAxisIndex(obj.State.Plot(isoIndex).AssociatedAxis); + plotData = get(obj.State.Plot(isoIndex).Handle); + axisData = get(plotData.Parent); + [xSource, ySource] = findSourceAxis(obj, axIndex); + scene = eval( sprintf('obj.layout.scene%d', xSource) ); + + aspectRatio = axisData.PlotBoxAspectRatio; + cameraPosition = axisData.CameraPosition; + dataAspectRatio = axisData.DataAspectRatio; + cameraUpVector = axisData.CameraUpVector; + cameraEye = cameraPosition./dataAspectRatio; + normFac = 0.5*abs(min(cameraEye)); + + %-aspect ratio-% + scene.aspectratio.x = aspectRatio(1); + scene.aspectratio.y = aspectRatio(2); + scene.aspectratio.z = aspectRatio(3); + + %-camera eye-% + scene.camera.eye.x = cameraEye(1) / normFac; + scene.camera.eye.y = cameraEye(2) / normFac; + scene.camera.eye.z = cameraEye(3) / normFac; + + %-camera up-% + scene.camera.up.x = cameraUpVector(1); + scene.camera.up.y = cameraUpVector(2); + scene.camera.up.z = cameraUpVector(3); + + %-camera projection-% + % scene.camera.projection.type = axisData.Projection; + + %-scene axis configuration-% + scene.xaxis.range = axisData.XLim; + scene.yaxis.range = axisData.YLim; + scene.zaxis.range = axisData.ZLim; + + scene.xaxis.zeroline = false; + scene.yaxis.zeroline = false; + scene.zaxis.zeroline = false; + + scene.xaxis.showline = true; + scene.yaxis.showline = true; + scene.zaxis.showline = true; + + scene.xaxis.ticklabelposition = 'outside'; + scene.yaxis.ticklabelposition = 'outside'; + scene.zaxis.ticklabelposition = 'outside'; + + scene.xaxis.title = axisData.XLabel.String; + scene.yaxis.title = axisData.YLabel.String; + scene.zaxis.title = axisData.ZLabel.String; + + %-tick labels-% + scene.xaxis.tickvals = axisData.XTick; + scene.xaxis.ticktext = axisData.XTickLabel; + scene.yaxis.tickvals = axisData.YTick; + scene.yaxis.ticktext = axisData.YTickLabel; + scene.zaxis.tickvals = axisData.ZTick; + scene.zaxis.ticktext = axisData.ZTickLabel; + + scene.xaxis.tickcolor = 'rgba(0,0,0,1)'; + scene.yaxis.tickcolor = 'rgba(0,0,0,1)'; + scene.zaxis.tickcolor = 'rgba(0,0,0,1)'; + scene.xaxis.tickfont.size = axisData.FontSize; + scene.yaxis.tickfont.size = axisData.FontSize; + scene.zaxis.tickfont.size = axisData.FontSize; + scene.xaxis.tickfont.family = matlab2plotlyfont(axisData.FontName); + scene.yaxis.tickfont.family = matlab2plotlyfont(axisData.FontName); + scene.zaxis.tickfont.family = matlab2plotlyfont(axisData.FontName); + + %-grid-% + if strcmp(axisData.XGrid, 'off'), scene.xaxis.showgrid = false; end + if strcmp(axisData.YGrid, 'off'), scene.yaxis.showgrid = false; end + if strcmp(axisData.ZGrid, 'off'), scene.zaxis.showgrid = false; end + + %-SET SCENE TO LAYOUT-% + obj.layout = setfield(obj.layout, sprintf('scene%d', xSource), scene); +end + +function fillColor = getFaceColor(plotData, axisData) + + %-initializations-% + faceColor = plotData.FaceColor; + cData = plotData.CData; + cLim = axisData.CLim; + colorMap = axisData.Colormap; + + %-get face color depending of faceColor attribute + if isnumeric(faceColor) + numColor = 255 * faceColor; + fillColor = sprintf('rgb(%f,%f,%f)', numColor); + + elseif strcmpi(faceColor, 'flat') + fillColor = getStringColor(cData, colorMap, cLim); + + elseif strcmpi(faceColor, 'interp') + if size(cData, 1) ~= 1 + for n = 1:size(cData, 2) + fillColor{n} = getStringColor(mean(cData(:, n)), colorMap, cLim); + end + else + % TODO + end + end +end + +function stringColor = getStringColor(cData, colorMap, cLim) + nColors = size(colorMap, 1); + cIndex = max( min( cData, cLim(2) ), cLim(1) ); + scaleColor = (cIndex - cLim(1)) / diff(cLim); + cIndex = 1 + floor(scaleColor*(nColors-1)); + numColor = 255 * colorMap(cIndex, :); + stringColor = sprintf('rgb(%f,%f,%f)', numColor); +end \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy