File Exchange

image thumbnail

NanSurf.zip

version 1.8.0.1 (2.09 MB) by Massimo Ciacci
Make surface plot when X,Y,Z contain NaNs, taking care of NaN-discontinuities

15 Downloads

Updated 09 Oct 2019

View License

% NANSURF(X,Y,Z,varargin) - makes a trisurf plot of X,Y,Z choosing a triangulation
% that minimizes zigzag edges due to NaN's in Z. No data interpolation takes place.
%
% inputs : X,Y,Z 2D matrixes representing a surface
% outputs: ph plot handle
% trgList the chosen triangulation
%
% NANSURF(X,Y,Z) plots the colored parametric surface defined by matrix arguments.
% it works the same as trisurf but choosing triangulation according to NaN's in Z.
%
% NANSURF(X,Y,Z,'PropertyName',PropertyValue,...) plots the data
% stored in the vectors X and Y, according to Property/Value pairs:
%
% QuadEdgesOnly
% 1 : only show quad edges as in surf, hides the triangle edges
% 0 : (Default) plots all triangle edges (can be changed outside with 'edgecolor')
%
% trgExtend
% 1 : (Default) enable extending triangles to better reduce NaN-edge-aliasing
% in close to horizonal/vertical lines.
% 0 : maximizes nr of rendered triangles without creating/extending them
% should only be used to compare/debug triangulation
% DBGTrg
% 1 : debug plot showing which triangles are affected by NaNs
% 0 : (Default) no debug plots
%
% IDEA BEHIND:
% When a face, e.g. quadruple of vertexes in a meshgrid, contain 1 Nan, surf does not render it.
% Choose triangles to maximize # rendered ones, maintaining correct face orientation.
% Correct face orientation (based on gradient) improves color rendering.
%
% METHOD:
% (1) NaNsurf makes a triangulation which maximizes the number of valid triangles
% a valid triangle contains no NaNs. output: TRG, a list of L/R triangles based on NaNs in Z
% (2) TRG list from step (1) is used as input to trisurf / patch: trisurf(TRG,X,Y,Z)
%
% EXAMPLE:
% plotSpec = {'edgecolor','k','facecolor','interp'};
% NaNsurf(X,Y,Z,plotSpec{:});
%
% EXAMPLE:
% NaNsurf(X,Y,Z,plotSpec{:},'trgExtend',1,'DBGTrg',1);
%
% EXAMPLE:
% M = 22; N = 34;
% Mh = (M+1)/2; Nh = (N+1)/2;
% [X,Y] = meshgrid((1:N)-Nh,(1:M)-Mh);
% Rn = sqrt((X/Nh).^2+(Y/Mh).^2);
% Z = sin(3*pi*Rn)./(3*pi*Rn);
% Z(find(X.^2+Y.^2 > Mh^2)) = NaN;
% NaNsurf(X,Y,Z,'edgecolor','k','facecolor','interp')
% axis([-Mh Mh -Nh Nh -.5 1])
%
% rev.1.8, Massimo Ciacci, September 20, 2019

Cite As

Massimo Ciacci (2019). NanSurf.zip (https://www.mathworks.com/matlabcentral/fileexchange/46809-nansurf-zip), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

@ Marco, relating to "is it possible to not draw the inner edge between the two triangles of the same quad?"
This was pretty easy by setting the edgecolor to 'none' and adding a Surf command with only the edges and facecolor set to 'none'.
I attempted resolving some asymmetry in display but they only work with the surf command and not with patch/trisurf.
Nevertheless the added function "reorder_XYZT_for_Surf_Symmetry", based on Bruno Luong answer on 17/Nov,2018 in
% https://nl.mathworks.com/matlabcentral/answers/430416-surf-function-with-interp-symmetric-surface-shown-assymmetrically, can be helpful to plot flat shaded surfaces without color asymmetries.

Very good job! Only one observation: is it possible to not draw the inner edge between the two triangles of the same quad? It would produce a visualization closer to Matlab built-in surf function.

Rob

Updates

1.8.0.1

Tested with ML2016b, updated screenshot

1.8.0.0

- Added option 'QuadEdgesOnly'
- Added method reorder_XYZT_for_Surf_Symmetry for preprocessing surf data

1.7.0.0

a preview button would have been nice..

1.6.0.0

oops, had forgotten the zip file..

1.5.0.0

embedded one debug-plot function were it belongs
slightly edited help section
changed screen-shoot to a simpler one

1.4.0.0

Yet a little formatting of the help section.

1.3.0.0

Changed screenshot, improved help to NanBasedTriangulation and placefig.

1.2.0.0

some minor comment editing

1.1.0.0

typo

MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags