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. Example figure; hold on; axis equal; axis([-5 65 -5 45 -5 45]); view(3); drawGrid3d([0 0 0], [20 20 20]); See also drawLine3d, drawEdge3d, clipLine3d
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 % Example 0029 % figure; hold on; axis equal; axis([-5 65 -5 45 -5 45]); view(3); 0030 % drawGrid3d([0 0 0], [20 20 20]); 0031 % 0032 % 0033 % See also 0034 % drawLine3d, drawEdge3d, clipLine3d 0035 0036 % ------ 0037 % Author: David Legland 0038 % E-mail: david.legland@inrae.fr 0039 % Created: 2005-11-17 0040 % Copyright 2005-2024 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas) 0041 0042 %% initialize variables 0043 0044 % extract handle of axis to draw on 0045 [hAx, varargin] = parseAxisHandle(varargin{:}); 0046 0047 % default values 0048 closed = true; 0049 origin = [0 0 0]; 0050 spacing = [1 1 1]; 0051 0052 % check if grid is open or not 0053 str = ''; 0054 if ~isempty(varargin) 0055 str = varargin{end}; 0056 end 0057 if ischar(str) 0058 if strncmpi(str, 'open', 4) 0059 closed = false; 0060 end 0061 varargin = varargin(1:end-1); 0062 end 0063 0064 % check origin and grid spacing 0065 if isscalar(varargin) 0066 spacing = varargin{1}; 0067 elseif length(varargin)==2 0068 origin = varargin{1}; 0069 spacing = varargin{2}; 0070 end 0071 0072 %% Compute internal data 0073 0074 % get axis limits 0075 ax = axis(hAx); 0076 x0 = ax(1); x1 = ax(2); 0077 y0 = ax(3); y1 = ax(4); 0078 z0 = ax(5); z1 = ax(6); 0079 0080 % get first and last coordinates of the grid in each direction 0081 dx = spacing(1); dy = spacing(2); dz = spacing(3); 0082 xe = x0 + mod(origin(1) - x0, dx); 0083 xf = x1 - mod(x1 - origin(1), dx); 0084 ye = y0 + mod(origin(2) - y0, dy); 0085 yf = y1 - mod(y1 - origin(2), dy); 0086 ze = z0 + mod(origin(1) - z0, dz); 0087 zf = z1 - mod(z1 - origin(1), dz); 0088 0089 % update first and last coordinate if grid is 'closed' 0090 if closed 0091 x0 = xe; x1 = xf; 0092 y0 = ye; y1 = yf; 0093 z0 = ze; z1 = zf; 0094 end 0095 0096 0097 %% Draw the grid 0098 0099 % header array, one header for each line segment 0100 h = []; 0101 0102 % draw lines parallel to x axis 0103 for y = ye:dy:yf 0104 for z = ze:dz:zf 0105 h = [h; drawEdge3d(hAx, [x0 y z x1 y z])]; %#ok<AGROW> 0106 end 0107 end 0108 0109 % draw lines parallel to y axis 0110 for x = xe:dx:xf 0111 for z = ze:dz:zf 0112 h = [h; drawEdge3d(hAx, [x y0 z x y1 z])]; %#ok<AGROW> 0113 end 0114 end 0115 0116 % draw lines parallel to z axis 0117 for x = xe:dx:xf 0118 for y = ye:dy:yf 0119 h = [h; drawEdge3d(hAx, [x y z0 x y z1])]; %#ok<AGROW> 0120 end 0121 end 0122 0123 0124 %% Check output arguments 0125 0126 if nargout > 0 0127 varargout{1} = h; 0128 end