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. CUBE can be axis aligned, 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 meshes3d, polyhedra, createCube, drawCuboid
0001 function varargout = drawCube(cube, varargin) 0002 % 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. 0012 % 0013 % CUBE can be axis aligned, in this case it should only contain center 0014 % 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 % 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 INRA - Cepia Software Platform. 0036 0037 % Parse and check inputs 0038 hAx = gca; 0039 if nargin > 0 0040 if isAxisHandle(cube) 0041 hAx = cube; 0042 if ~isempty(varargin) 0043 cube = varargin{1}; 0044 varargin(1) = []; 0045 end 0046 end 0047 end 0048 0049 % default values 0050 xc = 0; 0051 yc = 0; 0052 zc = 0; 0053 a = 1; 0054 theta = 0; 0055 phi = 0; 0056 psi = 0; 0057 0058 %% Parses the input 0059 if nargin > 0 && ~isAxisHandle(cube) 0060 % one argument: parses elements 0061 xc = cube(:,1); 0062 yc = cube(:,2); 0063 zc = cube(:,3); 0064 % parses side length if present 0065 if size(cube, 2) >= 4 0066 a = cube(:,4); 0067 end 0068 % parses orientation if present 0069 if size(cube, 2) >= 6 0070 theta = deg2rad(cube(:,5)); 0071 phi = deg2rad(cube(:,6)); 0072 end 0073 if size(cube, 2) >= 7 0074 psi = deg2rad(cube(:,7)); 0075 end 0076 end 0077 0078 0079 %% Compute cube coordinates 0080 0081 % create unit centered cube 0082 [v, f] = createCube; 0083 v = bsxfun(@minus, v, mean(v, 1)); 0084 0085 % convert unit basis to cube basis 0086 sca = createScaling3d(a); 0087 rot1 = createRotationOz(psi); 0088 rot2 = createRotationOy(theta); 0089 rot3 = createRotationOz(phi); 0090 tra = createTranslation3d([xc yc zc]); 0091 0092 % concatenate transforms 0093 trans = tra * rot3 * rot2 * rot1 * sca; 0094 0095 % transform mesh vertices 0096 v = transformPoint3d(v, trans); 0097 0098 0099 %% Process output 0100 if nargout == 0 0101 % no output: draw the cube 0102 drawMesh(hAx, v, f, varargin{:}); 0103 0104 elseif nargout == 1 0105 % one output: draw the cube and return handle 0106 varargout{1} = drawMesh(hAx, v, f, varargin{:}); 0107 0108 elseif nargout == 3 0109 % 3 outputs: return computed coordinates 0110 varargout{1} = v(:,1); 0111 varargout{2} = v(:,2); 0112 varargout{3} = v(:,3); 0113 end 0114