5#include <glm/geometric.hpp>
15glm::vec3 rootOf(
const StrandsDatas &datas,
size_t curveIndex) {
16 const auto pts = datas.curvePoints(curveIndex);
17 return pts.empty() ? glm::vec3(0.f) : pts[0].
position;
20glm::vec3 sampleCurveAtU(
const StrandsDatas &datas,
size_t curveIndex,
float u) {
21 const auto pts = datas.curvePoints(curveIndex);
22 if (pts.empty())
return {};
23 if (pts.size() == 1)
return pts[0].position;
24 const float t = std::clamp(
u, 0.f, 1.f);
27 while (
i1 + 1 < pts.size() && pts[
i1].u <
t) ++
i1;
28 const size_t i0 =
i1 - 1;
29 const float u0 = pts[
i0].u;
30 const float u1 = pts[
i1].u;
32 if (std::fabs(u1 - u0) > 1e-8f) {
33 alpha = (
t - u0) / (u1 - u0);
35 alpha = float(
i1 -
i0) > 0.f ? 1.f : 0.f;
37 alpha = std::clamp(alpha, 0.f, 1.f);
38 return pts[
i0].position * (1.f - alpha) + pts[
i1].
position * alpha;
55 if (
strands.curveCount() == 0) {
60 const float frac = std::clamp(guideFraction, 0.01f, 1.f);
61 const size_t keep = std::max<size_t>(1,
size_t(std::ceil(
double(
strands.curveCount()) *
double(frac))));
62 const size_t step = std::max<size_t>(1,
strands.curveCount() / keep);
64 std::vector<StrandPoint>
points;
65 std::vector<StrandCurve> curves;
69 for (
size_t ci = 0; ci <
strands.curveCount() && curves.size() < keep; ci +=
step) {
70 const auto pts =
strands.curvePoints(ci);
71 if (pts.size() < 2)
continue;
74 curve.pointCount = uint32_t(pts.size());
76 for (
size_t pi = 0; pi < pts.size(); ++pi) {
81 curves.push_back(
curve);
91 out.setCurves(std::move(curves));
109 const float exp = weightExponent(mode);
110 std::vector<StrandGuideWeights>
out(
strands.curveCount());
112 for (
size_t si = 0; si <
strands.curveCount(); ++si) {
118 std::vector<Cand> cands;
120 for (
size_t gi = 0; gi < guides.
curveCount(); ++gi) {
121 const glm::vec3
d = rootOf(guides, gi) -
root;
122 cands.push_back({uint32_t(gi), glm::dot(
d,
d)});
124 const size_t take = std::min(
size_t(k), cands.size());
125 std::partial_sort(cands.begin(), cands.begin() + std::ptrdiff_t(take), cands.end(),
126 [](
const Cand &
a,
const Cand &
b) { return a.dist2 < b.dist2; });
130 for (
size_t i = 0; i < take; ++i) {
131 const float dist = std::sqrt(std::max(cands[i].dist2, 0.f));
132 const float w = 1.f / std::pow(std::max(dist, 1e-4f), exp);
133 row.influencers[row.count] = {cands[i].index,
w};
138 for (
int i = 0; i < row.count; ++i) row.influencers[i].weight /= wSum;
139 }
else if (row.count > 0) {
140 row.influencers[0].weight = 1.f;
141 for (
int i = 1; i < row.count; ++i) row.influencers[i].weight = 0.f;
155 auto g1 = guidesDeformed.
validate();
160 "interpolateStrands: guide rest/deformed curve count mismatch",
"hair.guides"));
167 std::vector<StrandPoint>
points(strandsRest.
points().begin(), strandsRest.
points().end());
168 std::vector<StrandCurve> curves(strandsRest.
curves().begin(), strandsRest.
curves().end());
170 for (
size_t si = 0; si < strandsRest.
curveCount(); ++si) {
173 if (
curve.pointCount == 0 || row.count <= 0)
continue;
176 glm::vec3 delta(0.f);
177 for (
int i = 0; i < row.count; ++i) {
178 const auto &inf = row.influencers[i];
179 const glm::vec3 rest = rootOf(guidesRest, inf.guideIndex);
180 const glm::vec3 def = rootOf(guidesDeformed, inf.guideIndex);
181 delta += (def - rest) * inf.weight;
183 for (uint32_t pi = 0; pi <
curve.pointCount; ++pi) {
190 for (uint32_t pi = 0; pi <
curve.pointCount; ++pi) {
192 glm::vec3 delta(0.f);
193 for (
int i = 0; i < row.count; ++i) {
194 const auto &inf = row.influencers[i];
195 const glm::vec3 restP = sampleCurveAtU(guidesRest, inf.guideIndex, pt.
u);
196 const glm::vec3 defP = sampleCurveAtU(guidesDeformed, inf.guideIndex, pt.
u);
197 delta += (defP - restP) * inf.weight;
205 out.setCurves(std::move(curves));
217constexpr float kEpsilon = 1e-8f;
226 "GuideSimulator::reset: empty guides",
"hair.sim.guides"));
233 params_.
maxDt = std::max(1e-4f, params_.
maxDt);
235 restPoints_.assign(guides.
points().begin(), guides.
points().end());
247 for (
size_t ci = 0; ci < guides.
curveCount(); ++ci) {
249 if (pts.size() < 2) {
251 "GuideSimulator::reset: guide curve needs >= 2 points",
255 span.pointOffset = uint32_t(pos_.size());
256 span.pointCount = uint32_t(pts.size());
258 for (
size_t pi = 0; pi < pts.size(); ++pi) {
259 const glm::vec3
p = pts[pi].position;
262 pinned_.push_back(pi == 0 ? uint8_t(1) : uint8_t(0));
267 s.i0 = span.pointOffset + uint32_t(pi - 1);
268 s.i1 = span.pointOffset + uint32_t(pi);
269 s.restLength = std::max(seg, kEpsilon);
270 segments_.push_back(
s);
274 curves_.push_back(span);
294 params_.
maxDt = std::max(1e-4f, params_.
maxDt);
300 "GuideSimulator::setPinnedRoots: not ready",
"hair.sim"));
302 if (!rootPositionsXYZ || rootCount !=
int(curves_.size())) {
305 "GuideSimulator::setPinnedRoots: rootCount must equal curveCount",
"hair.sim.roots"));
307 for (
size_t ci = 0; ci < curves_.size(); ++ci) {
308 const uint32_t
idx = curves_[ci].pointOffset;
309 const glm::vec3
p{rootPositionsXYZ[ci * 3 + 0], rootPositionsXYZ[ci * 3 + 1], rootPositionsXYZ[ci * 3 + 2]};
310 if (!std::isfinite(
p.x) || !std::isfinite(
p.y) || !std::isfinite(
p.z)) {
314 const glm::vec3 delta =
p - pos_[
idx];
317 for (uint32_t pi = 1; pi < curves_[ci].pointCount; ++pi) {
318 pos_[
idx + pi] += delta;
319 prev_[
idx + pi] += delta;
325void GuideSimulator::solveDistances(
float dt) {
326 const float dt2 =
dt *
dt;
327 const float alpha = params_.
compliance / std::max(dt2, kEpsilon);
329 for (
int it = 0; it < params_.
iterations; ++it) {
330 for (
const Segment &seg : segments_) {
331 glm::vec3 &p0 = pos_[seg.i0];
332 glm::vec3 &p1 = pos_[seg.i1];
333 const float w0 = pinned_[seg.i0] ? 0.f : 1.f;
334 const float w1 = pinned_[seg.i1] ? 0.f : 1.f;
335 const float wSum = w0 + w1;
336 if (wSum < kEpsilon)
continue;
338 glm::vec3 delta = p1 - p0;
339 float len = glm::length(delta);
340 if (len < kEpsilon) {
341 delta = glm::vec3(0.f, kEpsilon, 0.f);
344 const glm::vec3
n = delta / len;
345 const float C = len - seg.restLength;
346 const float corr = C / (wSum + alpha);
347 p0 +=
n * (corr * w0);
348 p1 -=
n * (corr * w1);
353void GuideSimulator::resolveCollisions() {
355 if (yPlane < -1.0e5f)
return;
356 for (
size_t i = 0; i < pos_.size(); ++i) {
357 if (pinned_[i])
continue;
358 if (pos_[i].
y < yPlane) {
360 if (prev_[i].
y < yPlane) prev_[i].y = yPlane;
370 if (!std::isfinite(
dt) ||
dt <= 0.f) {
375 const float stepDt = std::min(
dt, params_.
maxDt);
376 const float damp = std::pow(std::clamp(params_.
damping, 0.f, 1.f), stepDt);
377 const glm::vec3 accel = params_.
gravity + params_.
wind;
379 for (
size_t i = 0; i < pos_.size(); ++i) {
384 const glm::vec3 cur = pos_[i];
385 const glm::vec3 vel = (cur - prev_[i]) * damp;
386 pos_[i] = cur + vel + accel * (stepDt * stepDt);
390 solveDistances(stepDt);
393 for (
size_t i = 0; i < pos_.size(); ++i) {
394 if (pinned_[i]) pos_[i] = prev_[i];
404 if (restPoints_.size() != pos_.size()) {
406 "GuideSimulator::snapshot: size mismatch",
"hair.sim"));
409 std::vector<StrandPoint>
points = restPoints_;
410 for (
size_t i = 0; i < pos_.size(); ++i) {
411 points[i].position = pos_[i];
413 std::vector<StrandCurve> curves;
414 curves.reserve(curves_.size());
415 for (
const CurveSpan &span : curves_) {
418 curve.pointCount = span.pointCount;
420 for (uint32_t pi = 1; pi < span.pointCount; ++pi) {
421 len += glm::length(pos_[span.pointOffset + pi] - pos_[span.pointOffset + pi - 1]);
424 curves.push_back(
curve);
429 out.setCurves(std::move(curves));
Stable, structured diagnostics shared by engine modules.
std::array< float, 3 > position
std::shared_ptr< const std::vector< glm::vec2 > > points
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
void setParams(const GuideSimParams ¶ms)
Sets the params.
Result< void > step(float dt)
Advance simulation by dt seconds (clamped by params.maxDt).
Result< StrandsDatas > snapshot() const
Export current particle positions as StrandsDatas (same topology). @ownership Returned buffer uniquel...
const GuideSimParams & params() const
Params.
Result< void > reset(const StrandsDatas &guides, const GuideSimParams ¶ms={})
Capture rest topology and pin every curve root. @ownership Copies guides; caller retains the source.
Result< void > setPinnedRoots(const float *rootPositionsXYZ, int rootCount)
Teleport pinned roots (e.g. after skin binding deform).
bool isReady() const
True when ready.
CPU-authoritative strand buffer (UE FHairStrandsDatas analogue).
std::span< const StrandCurve > curves() const
Borrowed curves; invalidated by mutation.
Result< void > validate() const
Verify offsets, counts, and finite values.
void setPoints(std::vector< StrandPoint > points)
Sets the points.
size_t curveCount() const
std::span< const StrandPoint > points() const
Borrowed points; invalidated by mutation.
std::span< const StrandPoint > curvePoints(size_t curveIndex) const
Borrowed points of one curve.
size_t pointCount() const
Procedural hair/fur card meshes + material/LOD helpers (no strand simulation).
InterpolationMode
How render strands follow deformed guides (UE interpolation analogue).
Result< StrandsDatas > extractGuides(const StrandsDatas &strands, float guideFraction)
Pick a deterministic subset of curves as guides (density thinning).
Result< StrandsDatas > interpolateStrands(const StrandsDatas &strandsRest, const StrandsDatas &guidesRest, const StrandsDatas &guidesDeformed, const std::vector< StrandGuideWeights > &weights, InterpolationMode mode)
Drive rest render strands with deformed guides using precomputed weights. @ownership Returned datas u...
Result< std::vector< StrandGuideWeights > > buildGuideWeights(const StrandsDatas &strands, const StrandsDatas &guides, int maxInfluences, InterpolationMode mode)
Build k-nearest guide weights for every render strand (by root distance).
Parameters for CPU guide-strand dynamics (UE groom sim lite analogue).
int iterations
XPBD solver iterations per substep (clamped to [1, 16]).
float collisionY
Infinite ground plane y = collisionY; particles below are pushed up. Set very low (default) to disabl...
float compliance
XPBD distance compliance (m/N); 0 ≈ hard constraint. Larger values allow more stretch.
float damping
Velocity retention per second (0 = freeze, 1 = no damping).
float maxDt
Clamp for a single step (seconds).
One strand as a contiguous point range. @ownership Value type owned by StrandsDatas.
Up to kMaxInfluences guides influencing one render strand. @ownership Owned by the weights table retu...
static constexpr int kMaxInfluences
One control point on a hair curve (UE FHairStrandsDatas point analogue). @ownership Value type owned ...
float u
Root-to-tip parameter in [0, 1].