Draw a 3D cuboid, eventually rotated. drawCuboid(CUBOID) Displays a 3D cuboid on current axis. CUBOID is given by: [XC YC ZC L W D YAW PITCH ROLL], where (XC, YC, ZC) is the cuboid center, L, W and H are the lengths of the cuboid main axes, and YAW PITCH ROLL are Euler angles representing the cuboid orientation, in degrees. If cuboid is axis-aligned, it can be specified using only center and side lengths: CUBOID = [XC YC ZC L W H] Example % Draw a basic rotated cuboid figure; hold on; drawCuboid([10 20 30 90 40 10 10 20 30], 'FaceColor', 'g'); axis equal; view(3); % Draw three "borromean" cuboids figure; hold on; drawCuboid([10 20 30 90 50 10], 'FaceColor', 'r'); drawCuboid([10 20 30 50 10 90], 'FaceColor', 'g'); drawCuboid([10 20 30 10 90 50], 'FaceColor', 'b'); view(3); axis equal; set(gcf, 'renderer', 'opengl') See also meshes3d, polyhedra, createCube, drawEllipsoid, drawCube
0001 function varargout = drawCuboid(cuboid, varargin) 0002 % Draw a 3D cuboid, eventually rotated. 0003 % 0004 % drawCuboid(CUBOID) 0005 % Displays a 3D cuboid on current axis. CUBOID is given by: 0006 % [XC YC ZC L W D YAW PITCH ROLL], 0007 % where (XC, YC, ZC) is the cuboid center, L, W and H are the lengths of 0008 % the cuboid main axes, and YAW PITCH ROLL are Euler angles representing 0009 % the cuboid orientation, in degrees. 0010 % 0011 % If cuboid is axis-aligned, it can be specified using only center and 0012 % side lengths: 0013 % CUBOID = [XC YC ZC L W H] 0014 % 0015 % Example 0016 % % Draw a basic rotated cuboid 0017 % figure; hold on; 0018 % drawCuboid([10 20 30 90 40 10 10 20 30], 'FaceColor', 'g'); 0019 % axis equal; 0020 % view(3); 0021 % 0022 % % Draw three "borromean" cuboids 0023 % figure; hold on; 0024 % drawCuboid([10 20 30 90 50 10], 'FaceColor', 'r'); 0025 % drawCuboid([10 20 30 50 10 90], 'FaceColor', 'g'); 0026 % drawCuboid([10 20 30 10 90 50], 'FaceColor', 'b'); 0027 % view(3); axis equal; 0028 % set(gcf, 'renderer', 'opengl') 0029 % 0030 % See also 0031 % meshes3d, polyhedra, createCube, drawEllipsoid, drawCube 0032 % 0033 0034 % ------ 0035 % Author: David Legland 0036 % e-mail: david.legland@inrae.fr 0037 % Created: 2011-06-29, using Matlab 7.9.0.529 (R2009b) 0038 % Copyright 2011 INRA - Cepia Software Platform. 0039 0040 phi = 0; 0041 theta = 0; 0042 psi = 0; 0043 0044 %% Parses the input 0045 if nargin == 0 0046 % no input: assumes cuboid with default shape 0047 xc = 0; yc = 0; zc = 0; 0048 a = 5; b = 4; c = 3; 0049 0050 else 0051 % one argument: parses elements 0052 xc = cuboid(:,1); 0053 yc = cuboid(:,2); 0054 zc = cuboid(:,3); 0055 a = cuboid(:,4); 0056 b = cuboid(:,5); 0057 c = cuboid(:,6); 0058 if size(cuboid, 2) >= 9 0059 k = pi / 180; 0060 phi = cuboid(:,7) * k; 0061 theta = cuboid(:,8) * k; 0062 psi = cuboid(:,9) * k; 0063 end 0064 end 0065 0066 0067 %% Compute cuboid coordinates 0068 0069 % create unit centered cube 0070 [v, f] = createCube; 0071 v = bsxfun(@minus, v, mean(v, 1)); 0072 0073 % convert unit basis to ellipsoid basis 0074 sca = createScaling3d(a, b, c); 0075 rotZ = createRotationOz(phi); 0076 rotY = createRotationOy(theta); 0077 rotX = createRotationOx(psi); 0078 tra = createTranslation3d([xc yc zc]); 0079 0080 % concatenate transforms 0081 trans = tra * rotZ * rotY * rotX * sca; 0082 0083 % transform mesh vertices 0084 [x, y, z] = transformPoint3d(v, trans); 0085 0086 0087 %% Process output 0088 if nargout == 0 0089 % no output: draw the cuboid 0090 drawMesh([x y z], f, varargin{:}); 0091 0092 elseif nargout == 1 0093 % one output: draw the cuboid and return handle 0094 varargout{1} = drawMesh([x y z], f, varargin{:}); 0095 0096 elseif nargout == 3 0097 % 3 outputs: return computed coordinates 0098 varargout{1} = x; 0099 varargout{2} = y; 0100 varargout{3} = z; 0101 end 0102