File Exchange

image thumbnail


version (3.54 KB) by Dirk-Jan Kroon
Convert a triangulated mesh into a voxel volume


Updated 20 May 2019

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This function polygon2function will convert a Triangulated Mesh into a Voxel Volume which will contain the discretized mesh.

Discretization of a polygon is done by splitting the polygon in smaller polygons, until the longest edge is smaller than 0.5 voxel, then the voxel beneath the vertice coordinates is set to one.

Note, this new version is pure Matlab (previous version required c++ compiler)

Try the example.

Bugs, successes and other comments are welcome!

Cite As

Dirk-Jan Kroon (2021). Polygon2Voxel (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (22)

David Alston

Carlo Robilotta

wenqian zhang


Hi, I am a biologist. Can you show me an example how to use polygon2voxel? Thanks a lot.

Lorenzo Grassi

Works great and it is fast and not memory-eager.

Ankit Dulani

If the size(FV.faces) and size(FV.vertices) the function "polygontovoxel" doesn't work.
I have N vertices and I have used these vertices to create deluanay triangulation mesh. So the no of triangle in the mesh are more than number of vertices.
Do you suggest anything to resolve it.

Phill Johanson

It works!

Alexandros Iliopoulos

Vincent Jaouen

Vincent Jaouen

As Stefan Roth pointed out, there is a problem on UNIX platforms. I had the same problem when trying to compile under ubuntu 11.10. Adding the macros indeed helps.

Stefan Roth

Great job thanks!

On Mac OS compyling the mex file leads to undefined symbol errors.

Adding the macros helps:

#define min(X,Y) ((X) < (Y) ? (X) : (Y))
#define max(X,Y) ((X) > (Y) ? (X) : (Y))

Dirk-Jan Kroon

Only integer volume sizes are supported


please check some kind of limitation.

I test a FV data,the data is from real CT images,so the range of object's x,y,z coordinate is about: 140:230 ,90:170, 725:820 , but in your code, I can not set the real range,only use [100 100 100], after test 'none','auto','center'.
only in 'auto', I can see the medical structure in the top left corner, but the structure is scaled down and original details can not be restored.



Dirk-Jan Kroon

*Rigo Trosendo
Thanks you for your comment. The code is basic, thus not based on any publications.

Rigo Trosendo

Great Job, Exactly what I needed. Is you algorithm based on any publication(s)?

Yuanming Suo

Great tool. Just solve my need. Well documented.

Dirk-Jan Kroon

*Siyi Deng
I have updated the code today with an new version which outputs a logical volume.

Siyi Deng

THis is indeed fantastic, however I would suggest to change the output Volume from datatype double to uint8 in c file and eventually to logical in m file; this will save lots of memory and make it faster, especially when volumeSize is large;

Incredible performance! Very good

Siyi Deng

very helpful. Good job.

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!