Home > matGeom > meshes3d > boxToMesh.m

boxToMesh

PURPOSE ^

BOXTOMESH Convert a box into a quad mesh with the same size.

SYNOPSIS ^

function varargout = boxToMesh(varargin)

DESCRIPTION ^

 BOXTOMESH Convert a box into a quad mesh with the same size.

   [V E F] = boxToMesh(BOX) 
   Create a box as a polyhedra representation. The box is defined by its  
   coordinate extents: BOX = [XMIN XMAX YMIN YMAX ZMIN ZMAX] 
   The result has the form [V E F], where V is a 8-by-3 array with vertex
   coordinates, E is a 12-by-2 array containing indices of neighbour
   vertices, and F is a 6-by-4 array containing vertices array of each
   face.

   [V F] = boxToMesh(BOX)
   Returns only the vertices and the face vertex indices.

   MESH = boxToMesh(BOX)
   Returns the data as a mesh structure, with fields 'vertices', 'edges'
   and 'faces'.
   
   ... = boxToMesh()
   Creates a unit cube

   Example
   [v, f] = boxToMesh([-2 -1 0 pi 2 3])
   drawMesh(v, f);
   
   See also
   meshes3d, drawMesh, triangulateFaces

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = boxToMesh(varargin)
0002 % BOXTOMESH Convert a box into a quad mesh with the same size.
0003 %
0004 %   [V E F] = boxToMesh(BOX)
0005 %   Create a box as a polyhedra representation. The box is defined by its
0006 %   coordinate extents: BOX = [XMIN XMAX YMIN YMAX ZMIN ZMAX]
0007 %   The result has the form [V E F], where V is a 8-by-3 array with vertex
0008 %   coordinates, E is a 12-by-2 array containing indices of neighbour
0009 %   vertices, and F is a 6-by-4 array containing vertices array of each
0010 %   face.
0011 %
0012 %   [V F] = boxToMesh(BOX)
0013 %   Returns only the vertices and the face vertex indices.
0014 %
0015 %   MESH = boxToMesh(BOX)
0016 %   Returns the data as a mesh structure, with fields 'vertices', 'edges'
0017 %   and 'faces'.
0018 %
0019 %   ... = boxToMesh()
0020 %   Creates a unit cube
0021 %
0022 %   Example
0023 %   [v, f] = boxToMesh([-2 -1 0 pi 2 3])
0024 %   drawMesh(v, f);
0025 %
0026 %   See also
0027 %   meshes3d, drawMesh, triangulateFaces
0028 
0029 %   ---------
0030 %   authors: David Legland, oqilipo
0031 %   created the 22/09/2016.
0032 
0033 p = inputParser;
0034 boxDefault = [0 1 0 1 0 1];
0035 boxDatatype = {'numeric'};
0036 boxAttribs = {'nonempty','vector','numel',6,'real','finite'};
0037 addOptional(p,'box',boxDefault,@(x)validateattributes(x,boxDatatype,boxAttribs))
0038 parse(p,varargin{:})
0039 
0040 xmin = p.Results.box(1);
0041 xmax = p.Results.box(2);
0042 ymin = p.Results.box(3);
0043 ymax = p.Results.box(4);
0044 zmin = p.Results.box(5);
0045 zmax = p.Results.box(6);
0046 
0047 vertices = [...
0048     xmin, ymin, zmin; ...
0049     xmax, ymin, zmin; ...
0050     xmin, ymax, zmin; ...
0051     xmax, ymax, zmin; ...
0052     xmin, ymin, zmax; ...
0053     xmax, ymin, zmax; ...
0054     xmin, ymax, zmax; ...
0055     xmax, ymax, zmax; ...
0056     ];
0057 
0058 edges = [1 2;1 3;1 5;2 4;2 6;3 4;3 7;4 8;5 6;5 7;6 8;7 8];
0059 
0060 % faces are oriented such that normals point outwards
0061 faces = [2 4 3 1;7 8 6 5;6 8 4 2;3 7 5 1;5 6 2 1;4 8 7 3];
0062 
0063 % format output
0064 varargout = formatMeshOutput(nargout, vertices, edges, faces);

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019