BOUNDINGBOX3D Bounding box of a set of 3D points. BOX = boundingBox3d(POINTS) Returns the bounding box of the set of points POINTS. POINTS is a N-by-3 array containing points coordinates. The result BOX is a 1-by-6 array, containing: [XMIN XMAX YMIN YMAX ZMIN ZMAX] Example % Draw bounding box of a cubeoctehedron [v e f] = createCubeOctahedron; box3d = boundingBox3d(v); figure; hold on; drawMesh(v, f); drawBox3d(box3d); set(gcf, 'renderer', 'opengl') axis([-2 2 -2 2 -2 2]); view(3) See also boxes3d, drawBox3d
0001 function box = boundingBox3d(points) 0002 %BOUNDINGBOX3D Bounding box of a set of 3D points. 0003 % 0004 % BOX = boundingBox3d(POINTS) 0005 % Returns the bounding box of the set of points POINTS. POINTS is a 0006 % N-by-3 array containing points coordinates. The result BOX is a 1-by-6 0007 % array, containing: 0008 % [XMIN XMAX YMIN YMAX ZMIN ZMAX] 0009 % 0010 % Example 0011 % % Draw bounding box of a cubeoctehedron 0012 % [v e f] = createCubeOctahedron; 0013 % box3d = boundingBox3d(v); 0014 % figure; hold on; 0015 % drawMesh(v, f); 0016 % drawBox3d(box3d); 0017 % set(gcf, 'renderer', 'opengl') 0018 % axis([-2 2 -2 2 -2 2]); 0019 % view(3) 0020 % 0021 % See also 0022 % boxes3d, drawBox3d 0023 0024 % ------ 0025 % Author: David Legland 0026 % E-mail: david.legland@inrae.fr 0027 % Created: 2011-04-01, using Matlab 7.9.0.529 (R2009b) 0028 % Copyright 2011-2024 INRA - Cepia Software Platform 0029 0030 % Parse input 0031 if isstruct(points) 0032 if isfield(points, 'vertices') 0033 points = points.vertices; 0034 else 0035 error(['If a struct is passed to boundingBox3d, it must have ' ... 0036 'the field ''vertices''!']) 0037 end 0038 end 0039 0040 % compute extreme x and y values 0041 xmin = min(points(:,1)); 0042 xmax = max(points(:,1)); 0043 ymin = min(points(:,2)); 0044 ymax = max(points(:,2)); 0045 box = [xmin xmax ymin ymax]; 0046 0047 % process case of 3D points 0048 if size(points, 2) > 2 0049 zmin = min(points(:,3)); 0050 zmax = max(points(:,3)); 0051 box = [xmin xmax ymin ymax zmin zmax]; 0052 end