49 lines
1.6 KiB
C++
49 lines
1.6 KiB
C++
/**
|
|
* @file sdf_torch.cpp
|
|
* @brief SDF 批量求值与梯度计算实现(CPU 后端)
|
|
*/
|
|
#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
|