DRAWGRID3D Draw a 3D grid on the current axis. drawGrid3d draws a 3D square grid, with origin (0,0,0) and spacing 1 in each direction, with bounds corresponding to the bounds of current axis. drawGrid3d(SPACING) where spacing is either a scalar or a [1x3] matrix, specifies the size of the unit cell. drawGrid3d(ORIGIN, SPACING) Also specify origin of grid. ORIGIN is a [1x3] array. drawGrid3d(..., EDGE) specifies whether function should draw edges touching edges of axis. EDGE is a characheter string, which can be : - 'OPEN' : each line start from one face of window to the opposite face. This results in a 'spiky' grid. - 'CLOSED' (default value) : each line stops at the last visible point of the grid for this line. The result looks like a box (no free spikes around the grid). H = drawGrid3d(...); return a vector of handles for each LINE object which was crated.
0001 function varargout = drawGrid3d(varargin) 0002 %DRAWGRID3D Draw a 3D grid on the current axis. 0003 % 0004 % drawGrid3d 0005 % draws a 3D square grid, with origin (0,0,0) and spacing 1 in each 0006 % direction, with bounds corresponding to the bounds of current axis. 0007 % 0008 % drawGrid3d(SPACING) 0009 % where spacing is either a scalar or a [1x3] matrix, specifies the size 0010 % of the unit cell. 0011 % 0012 % drawGrid3d(ORIGIN, SPACING) 0013 % Also specify origin of grid. ORIGIN is a [1x3] array. 0014 % 0015 % drawGrid3d(..., EDGE) 0016 % specifies whether function should draw edges touching edges of axis. 0017 % EDGE is a characheter string, which can be : 0018 % - 'OPEN' : each line start from one face of window to the opposite 0019 % face. This results in a 'spiky' grid. 0020 % - 'CLOSED' (default value) : each line stops at the last visible point 0021 % of the grid for this line. The result looks like a box (no free spikes 0022 % around the grid). 0023 % 0024 % H = drawGrid3d(...); 0025 % return a vector of handles for each LINE object which was crated. 0026 % 0027 0028 % ------ 0029 % Author: David Legland 0030 % e-mail: david.legland@grignon.inra.fr 0031 % Created: 2005-11-17 0032 % Copyright 2005 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas). 0033 0034 %% initialize variables ----- 0035 0036 % default values 0037 closed = true; 0038 origin = [0 0 0]; 0039 spacing = [1 1 1]; 0040 0041 % check if grid is open or not 0042 str = ''; 0043 if ~isempty(varargin) 0044 str = varargin{end}; 0045 end 0046 if ischar(str) 0047 if strncmpi(str, 'open', 4) 0048 closed = false; 0049 end 0050 varargin = varargin(1:end-1); 0051 end 0052 0053 % check origin and grid spacing 0054 if length(varargin)==1 0055 spacing = varargin{1}; 0056 elseif length(varargin)==2 0057 origin = varargin{1}; 0058 spacing = varargin{2}; 0059 end 0060 0061 %% Compute internam data ----- 0062 0063 % get axis limits 0064 ax = axis; 0065 x0 = ax(1); x1 = ax(2); 0066 y0 = ax(3); y1 = ax(4); 0067 z0 = ax(5); z1 = ax(6); 0068 0069 % get first and last coordinates of the grid in each direction 0070 dx = spacing(1); dy = spacing(2); dz = spacing(3); 0071 xe = x0 + mod(origin(1) - x0, dx); 0072 xf = x1 - mod(x1 - origin(1), dx); 0073 ye = y0 + mod(origin(2) - y0, dy); 0074 yf = y1 - mod(y1 - origin(2), dy); 0075 ze = z0 + mod(origin(1) - z0, dz); 0076 zf = z1 - mod(z1 - origin(1), dz); 0077 0078 % update first and last coordinate if grid is 'closed' 0079 if closed 0080 x0 = xe; x1 = xf; 0081 y0 = ye; y1 = yf; 0082 z0 = ze; z1 = zf; 0083 end 0084 0085 0086 %% Draw the grid ----- 0087 0088 h = []; 0089 %TODO: rewrite code, avoiding loops 0090 0091 % draw lines parallel to x axis 0092 for y = ye:dy:yf 0093 for z = ze:dz:zf 0094 h = [h; drawEdge3d([x0 y z x1 y z])]; %#ok<AGROW> 0095 end 0096 end 0097 0098 % draw lines parallel to y axis 0099 for x = xe:dx:xf 0100 for z = ze:dz:zf 0101 h = [h; drawEdge3d([x y0 z x y1 z])]; %#ok<AGROW> 0102 end 0103 end 0104 0105 % draw lines parallel to z axis 0106 for x = xe:dx:xf 0107 for y = ye:dy:yf 0108 h = [h; drawEdge3d([x y z0 x y z1])]; %#ok<AGROW> 0109 end 0110 end 0111 0112 0113 %% Check output arguments ----- 0114 0115 if nargout>0 0116 varargout{1} = h; 0117 end