64ad721ed7
- CMake: INTERFACE 库修复、vde_compile_options 顺序修复 - 命名空间: 所有模块添加 using 声明 - 类型补全: Ray3Dd、Point4D - 头文件: tolerance 泛型化、std::optional include - 警告: 放宽 -Wconversion/-Wsign-conversion - 构建结果: 0 errors, 22/25 tests passed
52 lines
1.7 KiB
C++
52 lines
1.7 KiB
C++
#pragma once
|
|
#include "vde/core/point.h"
|
|
#include <vector>
|
|
#include <array>
|
|
#include <functional>
|
|
|
|
namespace vde::mesh {
|
|
using core::Point2D;
|
|
using core::Point3D;
|
|
using core::Vector3D;
|
|
|
|
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
|
|
namespace { inline double lerp_impl(double a, double b, double t) { return a + t * (b - a); } }
|
|
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 lerp_impl(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 lerp_impl(d2, -d1, h) + k*h*(1.0-h);
|
|
}
|
|
|
|
} // namespace vde::mesh
|