feat: v0.5.0 P1 + 差异化功能 — Voronoi, Marching Cubes, 曲率, 序列化, Python绑定
CI / Build & Test (push) Failing after 1m31s
CI / Release Build (push) Failing after 16m55s

新增功能:
- core: 2D Voronoi 图(Delaunay 对偶图)
- mesh: Marching Cubes(完整256项三角表 + SDF 辅助函数)
- mesh: 离散曲率(Gaussian/Mean, Cotan 公式)
- foundation: 二进制序列化(版本化格式,Little-Endian)
- python: pybind11 绑定(30+ API 暴露)
- marching_cubes.h: SDF 基本体(球/盒)+ 光滑布尔
This commit is contained in:
ViewDesignEngine
2026-07-23 06:27:43 +00:00
parent 9f2536498b
commit cf13496ff6
12 changed files with 774 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include "vde/core/point.h"
#include <vector>
#include <array>
namespace vde::core {
struct VoronoiCell {
Point2D site; // Generator point
std::vector<Point2D> vertices; // Cell boundary vertices
std::vector<std::array<int, 2>> edges; // Edge indices into vertices
};
/// 2D Voronoi diagram from Delaunay dual graph
/// Returns cells for each input point
std::vector<VoronoiCell> voronoi_2d(const std::vector<Point2D>& points);
} // namespace vde::core
+38
View File
@@ -0,0 +1,38 @@
#pragma once
#include "vde/mesh/halfedge_mesh.h"
#include <vector>
#include <cstdint>
#include <string>
namespace vde::foundation {
// Versioned binary format for fast save/load
// Header: 8 bytes magic + 4 bytes version + 4 bytes flags
constexpr uint64_t VDE_MAGIC = 0x56444547454F4D00ULL; // "VDEGEOM\0"
constexpr uint32_t VDE_FORMAT_VERSION = 1;
struct SerializedMesh {
uint32_t vertex_count;
uint32_t face_count;
std::vector<double> vertices; // xyz xyz ... (flat)
std::vector<int32_t> face_indices; // v0 v1 v2 ...
std::vector<int32_t> face_valence; // 3 for all triangles
};
class BinarySerializer {
public:
/// Serialize mesh to binary buffer
static std::vector<uint8_t> serialize(const HalfedgeMesh& mesh);
/// Deserialize mesh from binary buffer
static HalfedgeMesh deserialize(const std::vector<uint8_t>& data);
/// Write to file
static bool write_file(const std::string& path, const HalfedgeMesh& mesh);
/// Read from file
static HalfedgeMesh read_file(const std::string& path);
};
} // namespace vde::foundation
+47
View File
@@ -0,0 +1,47 @@
#pragma once
#include "vde/core/point.h"
#include <vector>
#include <array>
#include <functional>
namespace vde::mesh {
struct MCMesh {
std::vector<Point3D> vertices;
std::vector<std::array<int, 3>> triangles;
};
/// Marching Cubes for scalar field f(x,y,z)
/// Extracts isosurface at value = iso_level within the bounding box
MCMesh marching_cubes(const std::function<double(double,double,double)>& f,
double iso_level,
const Point3D& bmin, const Point3D& bmax,
int resolution);
/// SDF sphere
inline double sdf_sphere(double x, double y, double z, double cx, double cy, double cz, double r) {
return std::sqrt((x-cx)*(x-cx) + (y-cy)*(y-cy) + (z-cz)*(z-cz)) - r;
}
/// SDF box
inline double sdf_box(double x, double y, double z, double hx, double hy, double hz) {
double dx = std::abs(x) - hx, dy = std::abs(y) - hy, dz = std::abs(z) - hz;
return std::sqrt(std::max(dx,0.0)*std::max(dx,0.0)
+ std::max(dy,0.0)*std::max(dy,0.0)
+ std::max(dz,0.0)*std::max(dz,0.0))
+ std::min(std::max({dx,dy,dz}), 0.0);
}
/// SDF smooth union
inline double sdf_smooth_union(double d1, double d2, double k) {
double h = std::clamp(0.5 + 0.5*(d2-d1)/k, 0.0, 1.0);
return std::lerp(d2, d1, h) - k*h*(1.0-h);
}
/// SDF smooth subtraction
inline double sdf_smooth_subtraction(double d1, double d2, double k) {
double h = std::clamp(0.5 - 0.5*(d2+d1)/k, 0.0, 1.0);
return std::lerp(d2, -d1, h) + k*h*(1.0-h);
}
} // namespace vde::mesh
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include "vde/mesh/halfedge_mesh.h"
#include <vector>
namespace vde::mesh {
struct CurvatureResult {
std::vector<double> gaussian; // per-vertex Gaussian curvature
std::vector<double> mean; // per-vertex mean curvature
std::vector<double> area; // per-vertex Voronoi area
};
/// Discrete curvature using Cotan formula (Meyer et al. 2003)
/// Gaussian: K(v) = (2π - Σθ_j) / A_v (interior vertices)
/// Mean: H(v) = ||Σ(cot α + cot β)·e|| / (2·A_v)
CurvatureResult compute_curvature(const HalfedgeMesh& mesh);
} // namespace vde::mesh