Fast and Accurate Geodesic Distances in C++
Compute geodesic distances on a triangle mesh in C++. Distance along the surface from one seed or many, bounded by a radius, in float or double. Header-only, C++17.
A geodesic distance is the distance from a point to a seed, measured along the surface of a mesh. Straight-line distance cuts through the object. Geodesic distance walks over it. trueform computes it with one call, tf::make_geodesic_distances, new in version 0.10.8. It is header-only C++17. This tutorial covers one seed, many seeds, a bounded radius, float and double output, and turning distances into rings and patches.

Install
Header-only. It needs C++17 and oneTBB. Add it to your project via CMake FetchContent:
FetchContent_Declare(
trueform
GIT_REPOSITORY https://github.com/polydera/trueform
)
FetchContent_MakeAvailable(trueform)
target_link_libraries(my_app PRIVATE tf::trueform)
Also available via conan, nuget, and pip. You need version 0.10.8 or later. See the installation guide for all options.
Loading the mesh
#include <trueform/trueform.hpp>
auto mesh_buffer = tf::read_stl("part.stl");
auto mesh = mesh_buffer.polygons();
tf::read_stl returns a tf::polygons_buffer that owns the data. .polygons() is a view on it. tf::read_obj works the same way. If your faces and points already live in your own arrays, wrap them with tf::make_polygons(tf::make_faces<3>(faces), tf::make_points<3>(points)). No copy is made.
To follow along without a file, make a unit sphere:
auto sphere_buffer = tf::make_sphere_mesh(1.f, 200, 400);
auto mesh = sphere_buffer.polygons();
Distance from one seed
A seed is a point id. The result has one distance per point.
std::vector<int> seeds{0};
auto d = tf::make_geodesic_distances(mesh, seeds);
// d: tf::buffer<float>, one distance per point; d[0] == 0

The distance is zero at the seed. On the unit sphere, the farthest point is the opposite pole, at a distance close to π.
You can check the result on this sphere. The exact geodesic distance on a unit sphere is the great-circle angle:
auto p0 = mesh.points()[0];
float max_error = 0.f;
for (auto [pt, dist] : tf::zip(mesh.points(), d)) {
float c = std::clamp(
tf::dot(pt.as_vector_view(), p0.as_vector_view()), -1.f, 1.f);
max_error = std::max(max_error, std::abs(dist - std::acos(c)));
}
The difference is small, and it shrinks as the sphere gets finer.
Most of the time the seed is a position, not an id. Find the nearest mesh point first, with a tree on the points:
tf::aabb_tree<int, float, 3> point_tree(mesh.points(), tf::config_tree(4, 4));
auto cloud = mesh.points() | tf::tag(point_tree);
auto [seed, metric_point] =
tf::neighbor_search(cloud, tf::make_point(0.f, 0.f, -1.f));
auto from_south = tf::make_geodesic_distances(mesh, std::vector<int>{seed});
Distance from many seeds
Pass several ids. Each point gets the distance to its nearest seed.
The seeds can be a whole curve. Then the field is the distance from that curve, measured along the surface. That is the distance from an edge: an offset from a trim line, a falloff from a seam.
Take the bunny and cut it in half. The cut is the boundary of the half. Its points are the seeds:
auto bunny_buffer = tf::read_stl("bunny.stl");
auto bunny = bunny_buffer.polygons();
// cut it at the middle height; the bunny stands along y
tf::buffer<float> height;
height.allocate(bunny.points().size());
tf::parallel_transform(bunny.points(), height,
[](auto pt) { return pt[1]; });
auto [lo, hi] = std::minmax_element(height.begin(), height.end());
std::array<float, 1> middle{0.5f * (*lo + *hi)};
std::array<int, 1> top{1};
auto [half_buffer, labels, face_labels] = tf::make_isobands(
bunny, tf::make_range(height), tf::make_range(middle),
tf::make_range(top));
auto half = half_buffer.polygons();
// the cut is the open boundary of the half
auto cut = tf::make_boundary_paths(half);
auto from_cut = tf::make_geodesic_distances(half, cut.data_buffer());

Keep the top band. The bunny has holes in its base, so the top half has one boundary: the cut. The ids index the half's own points, so nothing maps back. The distance is zero on the cut and grows toward the ear tips.
One call handles all the seeds, here a few hundred. You do not loop over them. Isocontours of the field are curves parallel to the cut:
std::array<float, 3> levels{1.f, 2.f, 3.f};
auto rings = tf::make_isocontours(half, tf::make_range(from_cut),
tf::make_range(levels));
Bounded by a radius
Often you only care about the neighborhood of a seed. Pass a radius as the third argument. The front stops there.
auto near = tf::make_geodesic_distances(mesh, seeds, 0.5f);
std::vector<int> region; // point ids within 0.5 of the seed
for (int i = 0; i < (int)near.size(); ++i)
if (std::isfinite(near[i])) region.push_back(i);

Points past the radius get infinity. So do points on a piece of the mesh the seed is not connected to, with or without a radius. Test with std::isfinite, not with a comparison to a large number. A small radius on a large mesh only costs what the front touches.
float or double
The result has the mesh's coordinate type. A float mesh gives float distances. To get double distances from a float mesh, state the type:
auto wide = tf::make_geodesic_distances<double>(mesh, seeds);
// wide: tf::buffer<double>
The front itself runs in double precision either way. Distances are measured in the stored coordinates. A transformation tagged onto the mesh does not change them.
Reusing topology
The call needs the face membership and the vertex link. It builds them when they are missing. For many calls on one mesh, build them once and tag them on:
auto fm = tf::make_face_membership(mesh);
auto vl = tf::make_vertex_link(mesh | tf::tag(fm));
auto tagged = mesh | tf::tag(fm) | tf::tag(vl);
for (int s : {0, 100, 1000}) {
auto ds = tf::make_geodesic_distances(tagged, std::vector<int>{s});
}
Working with results
The distance field is a plain tf::buffer, one value per point. Any scalar-field tool takes it.
Draw rings of equal distance with isocontours:
std::vector<float> levels{0.25f, 0.5f, 1.0f};
auto rings = tf::make_isocontours(mesh, tf::make_range(d),
tf::make_range(levels));
for (auto ring : rings.curves()) {
for (auto pt : ring) {
// points along one ring
}
}

Cut out the patch within a distance as its own mesh with isobands:
std::array<float, 1> cut{0.5f};
std::array<int, 1> keep{0};
auto [patch, labels, face_labels] = tf::make_isobands(
mesh, tf::make_range(d), tf::make_range(cut), tf::make_range(keep));
tf::write_obj(patch.polygons(), "patch.obj");
Band 0 is everything below 0.5. The patch boundary is cut exactly at the 0.5 ring. face_labels maps each patch face back to its source face.
The field also feeds the curvature and boolean tools in the same library.
About other methods
People search for the heat method, fast marching, or exact MMP geodesics. They all answer the same question: distance along the surface from seeds. tf::make_geodesic_distances runs a label-correcting front with a triangle-unfolding update. It is not the heat method. On a planar mesh it gives the straight line. On a curved one it beats the shortest edge path.
Related tutorials
- Fast and Robust Isocontours on a Triangle Mesh in C++ — curves and bands of equal value on a scalar field.
- Fast and Robust Principal Curvatures of a Mesh in C++ — curvature values and directions at every vertex.
- Fast and Robust Mesh Slicing and Cross-Sections in C++ — plane sections, layers and slabs of a mesh.
- Fast and Robust Closest Point on a Mesh in C++ — nearest surface point for a batch of queries.
Performance
We measured the same 28 Thingi10K meshes and the same seeds as the Python page, from 10K to 3.2M faces, in C++. The peers are MeshLib's computeSurfaceDistances, libigl's heat geodesics and CGAL's heat method. Each time is the best of the timed runs, from arrays in to distances out. The mesh, its topology and any factorization are built inside the clock.
Apple M4 Max 16 threads Apple clang 17 · C++17 -O3
On the Stanford dragon, one seed and 32 seeds:
The heat methods spend most of their time on a sparse factorization, on one thread. Above 100K faces they run more than 10× slower than trueform, so we did not time them at every size. CGAL's Surface_mesh rejects the 58K and 1M dragons at a non-manifold face. MeshLib runs faster from C++ than from Python, so the gap here is smaller than on the Python page.
Error is the relative error per point against exact MMP distances, on the 24 meshes (up to 1.3M faces) that have an exact reference. Each mesh's median error, averaged over the meshes, is 0.57 % from one seed and 0.20 % from 32 seeds. MeshLib's is 2.3 % from one seed and from 32 seeds. libigl heat's is 36 % and 30 %. CGAL heat's is 13 % and 31 %, on the 20 meshes up to 939K faces.
Best of the timed runs after warm-ups (trueform 15 after 3, 2026-11-01; peers 7 after 2, 2026-10-31), trueform 0.10.8; harness and raw results: benchmarks/tutorials/cpp-geodesics.
Full benchmarks · Documentation
@article{polydera:fast-geodesic-distances-in-cpp,
title={Fast and Accurate Geodesic Distances in C++},
author={Sajovic, {\v{Z}}iga, Polydera},
year={2026},
url={https://polydera.com/tutorials/fast-geodesic-distances-in-cpp},
organization={Polydera}
}