Matlab: Surf2patch

The result looks identical to a surf plot, but now you have direct access to f and v . Here’s something less documented: surf2patch can also return face vertex colors (CData) from a surface:

% From grid data [f,v] = surf2patch(X,Y,Z); % From an existing surface object h = surf(peaks); [f,v] = surf2patch(h); 1. Exporting to Other Formats Patch structures are much easier to export to common 3D formats (OBJ, STL, PLY) than surface objects. Once you have f and v , writing a custom exporter becomes trivial. 2. Combining Multiple Surfaces Want to merge two separate surfaces into one object? Extract their vertices/faces, concatenate them, and create a single patch. 3. Advanced Lighting & Transparency Patches respond better to manual lighting and transparency settings. Converting a surface to a patch can give you finer artistic control. 4. Faster Animation Manipulating vertices directly is often faster than regenerating surface objects in a loop. A Quick Example: Peaks to Patch Let’s convert the classic peaks surface to a patch and display it. surf2patch matlab

% Add some lighting for drama camlight headlight; lighting gouraud; axis equal; colorbar; The result looks identical to a surf plot,

% Display as a patch figure; patch('Faces', f, 'Vertices', v, ... 'FaceColor', 'interp', ... 'EdgeColor', 'none', ... 'FaceLighting', 'gouraud'); Once you have f and v , writing

Enter surf2patch .

% Create surface data [X,Y,Z] = peaks(30); % Convert to patch format [f,v] = surf2patch(X,Y,Z);

Have you used surf2patch in a creative way? Let me know in the comments below! Happy coding, Your MATLAB blogger

Read Time: 6 min

Authors

Jump to top of page

Wiley Rein LLP Cookie Preference Center

Your Privacy

When you visit our website, we use cookies on your browser to collect information. The information collected might relate to you, your preferences, or your device, and is mostly used to make the site work as you expect it to and to provide a more personalized web experience. For more information about how we use Cookies, please see our Privacy Policy.

Strictly Necessary Cookies

Always Active

Necessary cookies enable core functionality such as security, network management, and accessibility. These cookies may only be disabled by changing your browser settings, but this may affect how the website functions.

Functional Cookies

Always Active

Some functions of the site require remembering user choices, for example your cookie preference, or keyword search highlighting. These do not store any personal information.

Form Submissions

Always Active

When submitting your data, for example on a contact form or event registration, a cookie might be used to monitor the state of your submission across pages.

Performance Cookies

Performance cookies help us improve our website by collecting and reporting information on its usage. We access and process information from these cookies at an aggregate level.

Powered by Firmseek