Fits a square grid surface of the form z = f(x,y) to a triangular mesh surface.
Updated 13 Apr 2007

No License

The function GRIDTRIMESH fits a surface of the form Z = F(X,Y) to a given triangular mesh. Inputs X and Y are assumed to be produced by something similar to MESHGRID and the result could then be visualized by a simple SURF(X,Y,Z).

The triangular mesh on which this function operates should be defined in the usual way by two matrices F and V. MATLAB functions that use similar representations include TRIMESH, TRISURF and REDUCEPATCH. The included example file BEETHOVEN.MAT contains two such matrices.

A main advantage that this function has over the internal function GRIDDATA is that it operates not only on the set of vertices but also on the given triangulation. GRIDDATA first performs a Delaunay triangulation on the projected data points and subsequently always yields a surface that has a convex domain in the xy-plane. Another major drawback of GRIDDATA is that it handles surfaces that are not so-called 2.5D poorly (a "2.5D" surface has the property that for any xy-pair there exists at most one z-value on the surface, e.g. any function z = f(x,y)). This is because GRIDDATA projects all points to the xy-plane and then triangulates them without considering their original positions in 3-space. GRIDTRIMESH, on the other hand, considers all intersections that the ray through a particular gridpoint makes with the surface in 3D and then picks the maximum z-value. This emulates the idea of dropping a piece of cloth over the surface from above (i.e. from somewhere on the positive z-axis).

Another favourable trait of this function is its speed. I've tested it on the well-known Stanford bunny that consists of approximately 70,000 triangles. For a 100-by-100 grid (which is 10,000 points to be sampled) the function executes in about 2.4 seconds.

For those requiring more speed a MEX version of the function, called MXGRIDTRIMESH, is also supplied. This function may be used in exactly the same manner and produces exacly the same answer as GRIDTRIMESH. But since MXGRIDTRIMESH was written in C and then compiled with MEX it performs better. It required about 0.2 seconds for the Stanford bunny on a 100-by-100 grid and only about 2 seconds for a 1,000-by-1,000 grid (a million points!!).

The attached zip-file also includes an example script called DEMONSTRATION which can be run directly (you may need to recompile MXGRIDTRIMESH first, however).

CAUTION: at this stage the inputs X and Y must be generated by meshgrid(x,y) where x and y are monotonically increasing vectors. The function might go into an infinite loop if this is not the case!!

Cite As

Willie Brink (2024). gridtrimesh (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14SP2
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!
Version Published Release Notes