Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CARLA simulator Carla MeshReconstruction

From Leeroopedia
Knowledge Sources
Domains Geometry, Mesh Generation
Last Updated 2026-02-15 05:00 GMT

Overview

MeshReconstruction.h provides the top-level MarchCube function that reconstructs triangle meshes from signed distance functions using the Marching Cubes algorithm.

Description

This header-and-implementation file (~106 lines) serves as the public entry point for the Marching Cubes mesh reconstruction library within the MeshReconstruction namespace. It provides two overloads of MarchCube: a simple version that uses a default cube count of 50, and a detailed version accepting cube size, iso-level, and an optional gradient function. When no gradient function is provided, a numerical approximation (NumGrad) is computed using finite differences with epsilon 1e-6. The algorithm iterates over a 3D grid of cubes, skips cubes far from the isosurface using a narrow-band optimization, and delegates to Cube::Intersect and Triangulate for the actual surface extraction. Adapted from Paul Bourke's polygonisation reference.

Usage

Include this file to reconstruct a triangle mesh from any signed distance function over a rectangular domain, such as generating terrain or road geometry meshes from mathematical descriptions.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/third-party/marchingcube/MeshReconstruction.h

Signature

namespace MeshReconstruction
{
  // Simple overload with default 50 cubes per axis
  Mesh MarchCube(
      Fun3s const &sdf,
      Rect3 const &domain);

  // Detailed overload with configurable parameters
  Mesh MarchCube(
      Fun3s const &sdf,
      Rect3 const &domain,
      Vec3 const &cubeSize,
      double isoLevel = 0,
      Fun3v sdfGrad = nullptr);
}

Import

#include "third-party/marchingcube/MeshReconstruction.h"

I/O Contract

Parameter Type Direction Description
sdf Fun3s const & In Signed distance function mapping Vec3 to double
domain Rect3 const & In Rectangular 3D domain (min point + size) to reconstruct
cubeSize Vec3 const & In Size of marching cubes; smaller = higher resolution
isoLevel double In Level set value for surface extraction (default 0)
sdfGrad Fun3v In Optional gradient function for vertex normals; uses numerical approx if nullptr
return Mesh Out Triangle mesh with vertices, triangles, and vertex normals

Usage Examples

#include "third-party/marchingcube/MeshReconstruction.h"

using namespace MeshReconstruction;

// Define a sphere SDF
auto sphereSdf = [](Vec3 const &p) {
    return p.x*p.x + p.y*p.y + p.z*p.z - 1.0;
};

Rect3 domain{{-2, -2, -2}, {4, 4, 4}};
Vec3 cubeSize{0.1, 0.1, 0.1};

Mesh mesh = MarchCube(sphereSdf, domain, cubeSize);
// mesh.vertices, mesh.triangles, mesh.vertexNormals are populated

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment