Code covered by the BSD License  

Highlights from
Mesh voxelisation

4.83333

4.8 | 6 ratings Rate this file 46 Downloads (last 30 days) File Size: 131.55 KB File ID: #27390
image thumbnail

Mesh voxelisation

by Adam A

 

28 Apr 2010 (Updated 31 Aug 2011)

Voxelise a triangular-polygon mesh.

| Watch this File

File Information
Description

Polygon mesh voxelisation
=========================

Adam H. Aitkenhead
adam.aitkenhead@physics.cr.man.ac.uk
The Christie NHS Foundation Trust
14th May 2010

Voxelize a closed (ie. watertight) triangular-polygon mesh. The mesh can be in one of several formats: in an STL file; in a structure containing the faces and vertices data; in three 3xN arrays containing the x,y,z coordinates; or in a single Nx3x3 array defining the vertex coordinates for each of the N facets.
 
 
USAGE:
======

[gridOUTPUT,gridCOx,gridCOy,gridCOz] = VOXELISE(gridX,gridY,gridZ,STLin,raydirection)
  ...or...
[gridOUTPUT,gridCOx,gridCOy,gridCOz] = VOXELISE(gridX,gridY,gridZ,meshFV,raydirection)
  ...or...
[gridOUTPUT,gridCOx,gridCOy,gridCOz] = VOXELISE(gridX,gridY,gridZ,meshX,meshY,meshZ,raydirection)
  ...or...
[gridOUTPUT,gridCOx,gridCOy,gridCOz] = VOXELISE(gridX,gridY,gridZ,meshXYZ,raydirection)
 
 
INPUT PARAMETERS
================

gridX - Mandatory - 1xP array - List of the grid X coordinates.
                    OR an integer - Number of voxels in the grid in the X direction.

gridY - Mandatory - 1xQ array - List of the grid Y coordinates.
                    OR an integer - Number of voxels in the grid in the Y direction.

gridZ - Mandatory - 1xR array - List of the grid Z coordinates.
                    OR an integer - Number of voxels in the grid in the Z direction.

STLin - Optional - string - Filename of the STL file.

meshFV - Optional - structure - Structure containing the faces and vertices of the mesh, in the same format as that produced by the isosurface command.

meshX - Optional - 3xN array - List of the mesh X coordinates for the 3 vertices of each of the N facets
meshY - Optional - 3xN array - List of the mesh Y coordinates for the 3 vertices of each of the N facets
meshZ - Optional - 3xN array - List of the mesh Z coordinates for the 3 vertices of each of the N facets

meshXYZ - Optional - Nx3x3 array - The vertex positions for each facet, with:
                    1 row for each facet
                    3 columns for the x,y,z coordinates
                    3 pages for the three vertices

raydirection - Optional - String - Defines in which directions the ray-tracing is performed. The default is 'xyz', which traces in the x,y,z directions and combines the results.
 
 
OUTPUT PARAMETERS
=================

gridOUTPUT - Mandatory - PxQxR logical array - Voxelised data (1=>Inside the mesh, 0=>Outside the mesh)

gridCOx - Optional - 1xP array - List of the grid X coordinates.
gridCOy - Optional - 1xQ array - List of the grid Y coordinates.
gridCOz - Optional - 1xR array - List of the grid Z coordinates.
 
 
EXAMPLES
========

To voxelise an STL file:
>> [gridOUTPUT] = VOXELISE(gridX,gridY,gridZ,STLin)

To voxelise a mesh defined by a structure containing the faces and vertices:
>> [gridOUTPUT] = VOXELISE(gridX,gridY,gridZ,meshFV)

To voxelise a mesh where the x,y,z coordinates are defined by three 3xN arrays:
>> [gridOUTPUT] = VOXELISE(gridX,gridY,gridZ,meshX,meshY,meshZ)

To voxelise a mesh defined by a single Nx3x3 array:
>> [gridOUTPUT] = VOXELISE(gridX,gridY,gridZ,meshXYZ)

To also output the lists of X,Y,Z coordinates:
>> [gridOUTPUT,gridCOx,gridCOy,gridCOz] = VOXELISE(gridX,gridY,gridZ,STLin)

To use ray-tracing in only the z-direction:
>> [gridOUTPUT] = VOXELISE(gridX,gridY,gridZ,STLin,'z')
 
 
NOTES
=====

- Defining raydirection='xyz' means that the mesh is ray-traced in each of the x,y,z directions, with the overall result being a combination of the result from each direction. This gives the most reliable result at the expense of computation time.
- Tracing in only one direction (eg. raydirection='z') is faster, but can potentially lead to minor errors if rays exactly cross a facet edge.
  
 
REFERENCES
==========

- This code uses a ray intersection method similar to that described by:
  Patil S and Ravi B. Voxel-based representation, display and thickness analysis of intricate shapes. Ninth International Conference on Computer Aided Design and Computer Graphics (CAD/CG 2005)
 
 

MATLAB release MATLAB 7.10 (2010a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
07 Feb 2011 Amir Reza Riazi

Hi Adam, how could I just have the xyz coordinates of the points of the triangles without duplication?

I just want the points for easier analysis.

thanks

07 Feb 2011 Adam A

Hi Amir,
I've just uploaded a new version of the code, which will give a list of unique vertex coordinates using the following two lines of code. Hopefully it will be available for download shortly.
Adam

[coordVERTICES] = READ_stl(‘filename.stl');
[faces,vertices] = CONVERT_meshformat(coordVERTICES);

23 Feb 2011 K B  
23 Mar 2011 James

Great voxelising script, I've found it very useful. One thing I'd like to see if possible - if I try to voxelise multiple bodies that overlap, where they overlap the script returns 'void' voxels rather than 'solid'. Preferably I'd like to see these returned as solid as well. Cheers!

02 Dec 2011 Alan AF  
31 Jan 2012 Prakash Manandhar  
26 Apr 2012 Alexandre Zuquete Guarato

I really liked your mesh voxelisation code. I would like to know how can I insert into gridOUTPUT all surface voxels instead of just inside mesh voxels. thanks!!!

27 Apr 2012 Adam A

Hi Alexandre. If you want to obtain only the surface voxels rather than the entire volume, try the following line after doing the voxelisation. Hope this helps. Adam
gridSHELL = gridOUTPUT & imdilate(1-gridOUTPUT,ones(3,3,3),'same');

Please login to add a comment or rating.
Updates
28 Apr 2010

Fixed a bug which caused artifacts to appear for some STLs.

14 May 2010

Now also works with non-STL input. Changes also provide a significant speed improvement.

20 May 2010

Now optionally output the grid x,y,z coordinates. Robustness also improved.

02 Jun 2010

Provided a clearer example of how to use the code.

14 Jun 2010

Output is now a logical array, improving the meory efficiency.

15 Jun 2010

Fixed a bug introduced by the change to a logical output array.

22 Jun 2010

Improved the automatic detection of binary and ascii STL files.

23 Jun 2010

Enable ray-tracing in any combination of the x,y,z directions.

23 Jun 2010

Bug fix.

23 Jun 2010

And another minor bug fix.

28 Jun 2010

Allow input to be a structure containing [faces,vertices] data, similar to the type of structure output by isosurface.

07 Sep 2010

The grid no longer has to entirely enclose the mesh.

26 Nov 2010

Changed handling of automatic grid generation to reduce chance of artefacts.

02 Dec 2010

Fixed bug in automatic grid generation.

07 Feb 2011

Include the utility CONVERT_meshformat.m, which enables the vertex coordinates to be converted from an Nx3x3 array to faces,vertices info. Ie.
[faces,vertices] = CONVERT_meshformat(coordVERTICES)

03 Mar 2011

Improved method of finding which mesh facets can possibly be crossed by each ray. Up to 80% reduction in run-time.

31 Aug 2011

Minor edits to the documentation

Tag Activity for this File
Tag Applied By Date/Time
cad Adam A 28 Apr 2010 10:53:11
stl Adam A 28 Apr 2010 10:53:11
data import Adam A 28 Apr 2010 10:53:11
voxel Adam A 28 Apr 2010 10:53:11
voxelize Adam A 28 Apr 2010 10:53:11
voxelise Adam A 28 Apr 2010 10:53:11
patch Adam A 14 May 2010 10:57:35
mesh Adam A 14 May 2010 10:57:35
polygon Adam A 14 May 2010 10:57:35
3d Adam A 14 Mar 2011 10:55:45
mesh Bryce 09 Jun 2011 10:45:39
cad Mark 06 Sep 2011 11:47:25

Contact us at files@mathworks.com