# How to triangulate a simple 2d surface to generate a constrained surface trimesh

I want to split a couple of simple 2d forms into tri meshes. The meshes should conserve the original geometry and ideally I'd like a possibility to influence refinement vs. tris count.

I have the 2d forms available as images (svg, png) and am happy to convert to something else if necessary (STL, ...?). Output should ideally be an IBO (index buffer containing points and triangle index numbers), but I will settle for something I can convert into that. I don't need a library, a manual process is fine, but I'm not above using one, if it gets the job done.

I found a number of possible solutions to this (gmsh, netgen, ... and more) but many of those seem too much (would require high time investment to learn) for my relatively simple problem.

I am moderately proficient in python, blender and common linux software and am happy to come up with a small script to solve this, but don't want to dive deep into e.g. GL in order to solve this - hence my question: What is the easiest workflow to generate a trimesh from a svg or png?

This feels like a "common" issue, but I'm having a bit of hard time to find a solution with good effort/result ratio (maybe my google-fu is broken).

• Triangle is easy to use (if you're comfortable with calling command-line programs) and generates high-quality triangulations in 2D. – user106 Jun 29 '19 at 16:58
• If you need something quick and dirty to bolt in look up ear clipping. Though dicretisizing a random svg is not so easy so find a tool that does this. – joojaa Jul 2 '19 at 23:19
• Triangle looks exactly like it provides the needed control, but there is no fast and easy way to use it with svg files - need to convert it to a special triangle file format first - probably with a separate script. – squarespiral Jul 3 '19 at 8:09

The fastest and easiest way to do this is using blender. Simply import the svg (supported out of the box) into blender. The svg will be imported as a "curve"-type geometry. Optionally do some clean up/refinement. The curves can then be converted to a mesh. The mesh defaults to a trimesh. Voila - svg to trimesh in a couple of seconds.

Mesh refinement is not very fine-grained, but there is some measure of control via the "beauty-fill", "subdivide" and "triangulate" as well as "merge vertices by distance" tools from within blender.

The IBO (vertices + triangle vertex indices) can then be extracted using a short python script:

import bpy
obdata = bpy.context.object.data # selected object
for v in obdata.vertices:
fh.write('({}, {}),\n'.format(v.co.x, v.co.y))
for f in obdata.polygons:
for v in f.vertices:
print('{} '.format(v))