feat(sdf): S12-C PyTorch integration + Python bindings
Add batch evaluation/gradient functions for SDF trees (sdf_torch.h/cpp). Add sdf_gradient.h with finite-difference gradient utilities. Add Python modules vde.sdf (high-level SDF API) and vde.torch_sdf (PyTorch autograd). Add test_sdf_torch_bridge.cpp with batch eval/gradient tests. Integrate into vde_sdf library and test suite.
This commit is contained in:
@@ -171,6 +171,7 @@ add_library(vde_sdf STATIC
|
||||
sdf/sdf_primitives.cpp
|
||||
sdf/sdf_tree.cpp
|
||||
sdf/sdf_to_mesh.cpp
|
||||
sdf/sdf_torch.cpp
|
||||
)
|
||||
target_include_directories(vde_sdf
|
||||
PUBLIC ${CMAKE_SOURCE_DIR}/include
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "vde/sdf/sdf_torch.h"
|
||||
#include "vde/sdf/sdf_gradient.h"
|
||||
|
||||
namespace vde::sdf {
|
||||
|
||||
void evaluate_batch(const SdfNodePtr& root,
|
||||
const double* points, int n,
|
||||
double* distances) {
|
||||
auto fn = [&](const Point3D& p) -> double { return evaluate(root, p); };
|
||||
for (int i = 0; i < n; ++i) {
|
||||
Point3D p(points[i*3], points[i*3+1], points[i*3+2]);
|
||||
distances[i] = fn(p);
|
||||
}
|
||||
}
|
||||
|
||||
void gradient_batch(const SdfNodePtr& root,
|
||||
const double* points, int n,
|
||||
double* gradients) {
|
||||
auto fn = [&](const Point3D& p) -> double { return evaluate(root, p); };
|
||||
for (int i = 0; i < n; ++i) {
|
||||
Point3D p(points[i*3], points[i*3+1], points[i*3+2]);
|
||||
Vector3D g = gradient(fn, p);
|
||||
gradients[i*3] = g.x();
|
||||
gradients[i*3+1] = g.y();
|
||||
gradients[i*3+2] = g.z();
|
||||
}
|
||||
}
|
||||
|
||||
void evaluate_with_gradient_batch(const SdfNodePtr& root,
|
||||
const double* points, int n,
|
||||
double* distances,
|
||||
double* gradients) {
|
||||
auto fn = [&](const Point3D& p) -> double { return evaluate(root, p); };
|
||||
for (int i = 0; i < n; ++i) {
|
||||
Point3D p(points[i*3], points[i*3+1], points[i*3+2]);
|
||||
auto [val, grad] = evaluate_with_gradient(fn, p);
|
||||
distances[i] = val;
|
||||
gradients[i*3] = grad.x();
|
||||
gradients[i*3+1] = grad.y();
|
||||
gradients[i*3+2] = grad.z();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace vde::sdf
|
||||
Reference in New Issue
Block a user