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 MeshSimplify

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

Overview

Simplify.h provides a fast quadric-based mesh simplification algorithm that reduces the polygon count of triangle meshes while preserving visual quality, used by CARLA to optimize generated road and terrain meshes.

Description

This is a third-party header-only library by Sven Forstmann (MIT license, 2014), based on the Fast Quadric Mesh Simplification algorithm. The file (~1160 lines) contains custom vector types (vec3f, vector3), a SymetricMatrix class for quadric error computation, and the core SimplifyMesh namespace with data structures for triangles, vertices, and references. The simplification algorithm iteratively collapses edges with the smallest quadric error metric until the target triangle count is reached. Helper loop macros (loopi, loopj, loopk) are defined for iteration.

Usage

Use this library when procedurally generated meshes (e.g., from OpenDRIVE road geometry) need polygon reduction for rendering performance. The simplification percentage is controlled via OpendriveGenerationParameters::simplification_percentage.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/third-party/simplify/Simplify.h

Signature

struct vec3f {
    double x, y, z;
    // operator+, operator-, operator*, dot(), cross(), normalize(), length()
};

class SymetricMatrix {
public:
    double m[10];
    SymetricMatrix(double c = 0);
    SymetricMatrix(double m11, double m12, ...);
    double det(int a11, int a12, int a13, ...);
    SymetricMatrix operator+(const SymetricMatrix &n) const;
};

namespace Simplify {
    struct Triangle { int v[3]; double err[4]; int deleted, dirty, attr; vec3f n; };
    struct Vertex { vec3f p; int tstart, tcount; SymetricMatrix q; int border; };
    struct Ref { int tid, tvertex; };

    void simplify_mesh(int target_count, double agressiveness = 7, bool verbose = false);
    void simplify_mesh_lossless(bool verbose = false);
}

Import

#include "third-party/simplify/Simplify.h"

I/O Contract

Parameter Type Direction Description
target_count int In Target number of triangles after simplification
agressiveness double In Controls speed vs quality tradeoff (default 7)
verbose bool In Whether to print progress information
Simplify::triangles std::vector<Triangle> In/Out Triangle data populated before call, simplified after
Simplify::vertices std::vector<Vertex> In/Out Vertex data populated before call, simplified after

Usage Examples

#include "third-party/simplify/Simplify.h"

// Populate mesh data
Simplify::triangles.clear();
Simplify::vertices.clear();
// ... fill vertices and triangles ...

// Reduce to 50% of original triangle count
int target = Simplify::triangles.size() / 2;
Simplify::simplify_mesh(target, 7.0, false);

// Access simplified mesh
for (auto &tri : Simplify::triangles) {
    if (!tri.deleted) {
        // use triangle
    }
}

Related Pages

Page Connections

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