DRAWCUBE Draw a 3D centered cube, eventually rotated. drawCube(CUBE) Displays a 3D cube on current axis. CUBE is given by: [XC YC ZC SIDE THETA PHI PSI] where (XC, YC, ZC) is the CUBE center, SIDE is the length of the cube main sides, and THETA PHI PSI are angles representing the cube orientation, in degrees. THETA is the colatitude of the cube, between 0 and 90 degrees, PHI is the azimut, and PSI is the rotation angle around the axis of the normal (see file angles3d.m). CUBE can be axis-aligned (also called 'isothetic cube'). In this case it should only contain center and side information: CUBE = [XC YC ZC SIDE] The function drawCuboid is closely related, but uses a different angle convention, and allows for different sizes along directions. Example % Draw a cube with small rotations figure; hold on; drawCube([10 20 30 50 10 20 30], 'FaceColor', 'g'); axis equal; view(3); See also angles3d, meshes3d, polyhedra, createCube, drawCuboid
0001 function varargout = drawCube(varargin) 0002 %DRAWCUBE Draw a 3D centered cube, eventually rotated. 0003 % 0004 % drawCube(CUBE) 0005 % Displays a 3D cube on current axis. CUBE is given by: 0006 % [XC YC ZC SIDE THETA PHI PSI] 0007 % where (XC, YC, ZC) is the CUBE center, SIDE is the length of the cube 0008 % main sides, and THETA PHI PSI are angles representing the cube 0009 % orientation, in degrees. THETA is the colatitude of the cube, between 0 0010 % and 90 degrees, PHI is the azimut, and PSI is the rotation angle 0011 % around the axis of the normal (see file angles3d.m). 0012 % 0013 % CUBE can be axis-aligned (also called 'isothetic cube'). In this case 0014 % it should only contain center and side information: 0015 % CUBE = [XC YC ZC SIDE] 0016 % 0017 % The function drawCuboid is closely related, but uses a different angle 0018 % convention, and allows for different sizes along directions. 0019 % 0020 % Example 0021 % % Draw a cube with small rotations 0022 % figure; hold on; 0023 % drawCube([10 20 30 50 10 20 30], 'FaceColor', 'g'); 0024 % axis equal; 0025 % view(3); 0026 % 0027 % See also 0028 % angles3d, meshes3d, polyhedra, createCube, drawCuboid 0029 % 0030 0031 % ------ 0032 % Author: David Legland 0033 % E-mail: david.legland@inrae.fr 0034 % Created: 2011-06-29, using Matlab 7.9.0.529 (R2009b) 0035 % Copyright 2011-2024 INRA - Cepia Software Platform 0036 0037 % extract handle of axis to draw on 0038 [hAx, varargin] = parseAxisHandle(varargin{:}); 0039 0040 cube = varargin{1}; 0041 varargin(1) = []; 0042 0043 % default values 0044 xc = 0; 0045 yc = 0; 0046 zc = 0; 0047 a = 1; 0048 theta = 0; 0049 phi = 0; 0050 psi = 0; 0051 0052 %% Parses the input 0053 if nargin > 0 0054 % one argument: parses elements 0055 xc = cube(:,1); 0056 yc = cube(:,2); 0057 zc = cube(:,3); 0058 % parses side length if present 0059 if size(cube, 2) >= 4 0060 a = cube(:,4); 0061 end 0062 % parses orientation if present 0063 if size(cube, 2) >= 6 0064 theta = deg2rad(cube(:,5)); 0065 phi = deg2rad(cube(:,6)); 0066 end 0067 if size(cube, 2) >= 7 0068 psi = deg2rad(cube(:,7)); 0069 end 0070 end 0071 0072 0073 %% Compute cube coordinates 0074 0075 % create unit centered cube 0076 [v, f] = createCube; 0077 v = bsxfun(@minus, v, mean(v, 1)); 0078 0079 % convert unit basis to cube basis 0080 sca = createScaling3d(a); 0081 rot1 = createRotationOz(psi); 0082 rot2 = createRotationOy(theta); 0083 rot3 = createRotationOz(phi); 0084 tra = createTranslation3d([xc yc zc]); 0085 0086 % concatenate transforms 0087 trans = tra * rot3 * rot2 * rot1 * sca; 0088 0089 % transform mesh vertices 0090 v = transformPoint3d(v, trans); 0091 0092 0093 %% Process output 0094 if nargout == 0 0095 % no output: draw the cube 0096 drawMesh(hAx, v, f, varargin{:}); 0097 0098 elseif nargout == 1 0099 % one output: draw the cube and return handle 0100 varargout{1} = drawMesh(hAx, v, f, varargin{:}); 0101 0102 elseif nargout == 3 0103 % 3 outputs: return computed coordinates 0104 varargout{1} = v(:,1); 0105 varargout{2} = v(:,2); 0106 varargout{3} = v(:,3); 0107 end 0108