23 std::vector<std::uint32_t>
indices;
24 indices.reserve(triangles_.size() * 3);
25 for (
const auto&
triangle : triangles_)
26 for (
int vertex :
triangle.v)
indices.push_back(
static_cast<std::uint32_t
>(vertex));
42 return {
a.
y *
b.z -
a.z *
b.y,
a.z *
b.x -
a.x *
b.z,
a.x *
b.y -
a.y *
b.x};
45float dot(
const Vec3&
a,
const Vec3&
b) {
return a.x *
b.x +
a.y *
b.y +
a.z *
b.z; }
52 return feature ==
"arbitrary_topology" || feature ==
"xpbd_material" || feature ==
"tether" ||
53 feature ==
"volume_pressure" || feature ==
"skin_backstop" || feature ==
"attachment" ||
54 feature ==
"world_collision" || feature ==
"collision_filter" || feature ==
"friction" ||
55 feature ==
"self_collision" || feature ==
"aerodynamics" || feature ==
"runtime_tearing";
59 float backstopDistance,
float backstopRadius,
float compliance) {
60 if (!validIndex(
index))
throw Exception(
"Cloth3D.setSkinConstraint: index out of range");
61 const float normalLength = std::sqrt(
nx *
nx +
ny *
ny +
nz *
nz);
62 if (!std::isfinite(normalLength) || normalLength <= 1e-7f)
63 throw Exception(
"Cloth3D.setSkinConstraint: normal must be finite and non-zero");
64 auto found = std::find_if(skinConstraints_.begin(), skinConstraints_.end(),
65 [
index](
const SkinConstraint& item) { return item.particle == index; });
74 std::isfinite(backstopDistance) ? backstopDistance : -1.f,
75 std::isfinite(backstopRadius) ? std::max(0.f, backstopRadius) : 0.f,
76 std::isfinite(compliance) ? std::max(0.f, compliance) : 0.f,
78 if (
found == skinConstraints_.end())
79 skinConstraints_.push_back(
value);
85 auto found = std::find_if(skinConstraints_.begin(), skinConstraints_.end(),
86 [
index](
const SkinConstraint& item) { return item.particle == index; });
87 if (
found == skinConstraints_.end())
throw Exception(
"Cloth3D.updateSkinReference: particle is not constrained");
88 const float normalLength = std::sqrt(
nx *
nx +
ny *
ny +
nz *
nz);
89 if (!std::isfinite(normalLength) || normalLength <= 1e-7f)
90 throw Exception(
"Cloth3D.updateSkinReference: normal must be finite and non-zero");
100 std::erase_if(skinConstraints_, [
index](
const SkinConstraint& item) {
return item.particle ==
index; });
104 return std::any_of(skinConstraints_.begin(), skinConstraints_.end(),
105 [
index](
const SkinConstraint& item) { return item.particle == index; });
109 if (!validIndex(
index))
throw Exception(
"Cloth3D.attachParticle: index out of range");
110 auto found = std::find_if(attachments_.begin(), attachments_.end(),
111 [
index](
const Attachment& item) { return item.particle == index; });
112 const Attachment
value{
index,
x,
y,
z, std::isfinite(compliance) ? std::max(0.f, compliance) : 0.f, 0.f};
113 if (
found == attachments_.end())
114 attachments_.push_back(
value);
120 auto found = std::find_if(attachments_.begin(), attachments_.end(),
121 [
index](
const Attachment& item) { return item.particle == index; });
122 if (
found == attachments_.end())
throw Exception(
"Cloth3D.updateAttachment: particle is not attached");
129 std::erase_if(attachments_, [
index](
const Attachment& item) {
return item.particle ==
index; });
133 return std::any_of(attachments_.begin(), attachments_.end(),
134 [
index](
const Attachment& item) { return item.particle == index; });
140 tetherCompliance_ = std::isfinite(compliance) ? std::max(0.f, compliance) : 0.f;
143void Cloth3D::setPressure(
float pressure) { pressure_ = std::isfinite(pressure) ? std::max(0.f, pressure) : 0.f; }
146 volumeCompliance_ = std::isfinite(compliance) ? std::max(0.f, compliance) : 0.f;
150 if (restVolume_ == 0.f)
return 0.f;
152 for (
const Tri&
triangle : triangles_) {
153 const Particle&
a = particles_[
static_cast<size_t>(
triangle.v[0])];
154 const Particle&
b = particles_[
static_cast<size_t>(
triangle.v[1])];
155 const Particle&
c = particles_[
static_cast<size_t>(
triangle.v[2])];
156 volume += (double(
a.x) * (double(
b.y) *
c.z - double(
b.z) *
c.y) +
157 double(
a.y) * (double(
b.z) *
c.x - double(
b.x) *
c.z) +
158 double(
a.z) * (double(
b.x) *
c.y - double(
b.y) *
c.x)) /
161 return static_cast<float>(
volume);
164void Cloth3D::solveTetherConstraints(
float dt) {
165 if (tetherScale_ <= 0.f || tethers_.empty() ||
dt <= 0.f)
return;
166 const float alpha = tetherCompliance_ / (
dt *
dt);
167 for (Tether& tether : tethers_) {
168 Particle& particle = particles_[
static_cast<size_t>(tether.particle)];
169 const Particle& anchor = particles_[
static_cast<size_t>(tether.anchor)];
170 if (particle.pinned)
continue;
171 const float dx = particle.x - anchor.x;
172 const float dy = particle.y - anchor.y;
173 const float dz = particle.z - anchor.z;
175 const float maximum = tether.maxLength * tetherScale_;
177 const float weight = particle.inverseMass;
179 tether.lambda += deltaLambda;
186void Cloth3D::solveVolumeConstraint(
float dt) {
187 if (pressure_ <= 0.f || restVolume_ == 0.f ||
dt <= 0.f)
return;
188 std::vector<Vec3> gradients(particles_.size());
189 for (
const Tri&
triangle : triangles_) {
190 const Particle&
a = particles_[
static_cast<size_t>(
triangle.v[0])];
191 const Particle&
b = particles_[
static_cast<size_t>(
triangle.v[1])];
192 const Particle&
c = particles_[
static_cast<size_t>(
triangle.v[2])];
193 const Vec3 av{
a.
x,
a.y,
a.z};
199 for (
const auto& [
index, gradient] :
201 gradients[
static_cast<size_t>(
index)].
x += gradient.x / 6.f;
202 gradients[
static_cast<size_t>(
index)].
y += gradient.y / 6.f;
203 gradients[
static_cast<size_t>(
index)].
z += gradient.z / 6.f;
207 float denominator = 0.f;
208 for (
size_t i = 0; i < particles_.size(); ++i)
209 if (!particles_[i].
pinned) denominator += particles_[i].inverseMass *
dot(gradients[i], gradients[i]);
210 const float alpha = volumeCompliance_ / (
dt *
dt);
211 denominator += alpha;
212 if (denominator <= 1e-10f)
return;
214 const float deltaLambda = (-
constraint - alpha * volumeLambda_) / denominator;
215 volumeLambda_ += deltaLambda;
216 for (
size_t i = 0; i < particles_.size(); ++i) {
217 if (particles_[i].
pinned)
continue;
218 particles_[i].x += particles_[i].inverseMass * gradients[i].x * deltaLambda;
219 particles_[i].y += particles_[i].inverseMass * gradients[i].y * deltaLambda;
220 particles_[i].z += particles_[i].inverseMass * gradients[i].z * deltaLambda;
224void Cloth3D::solveSkinConstraints(
float dt) {
225 if (
dt <= 0.f)
return;
226 for (SkinConstraint& skin : skinConstraints_) {
227 Particle& particle = particles_[
static_cast<size_t>(skin.particle)];
228 if (particle.pinned)
continue;
229 Vec3 delta{particle.
x - skin.x, particle.y - skin.y, particle.z - skin.z};
232 const float alpha = skin.compliance / (
dt *
dt);
233 const float weight = particle.inverseMass;
234 const float dl = (-(
distance - skin.radius) - alpha * skin.lambda) / (
weight + alpha);
240 if (skin.backstopDistance < 0.f || skin.backstopRadius <= 0.f)
continue;
241 const Vec3 center{skin.
x - skin.nx * skin.backstopDistance, skin.y - skin.ny * skin.backstopDistance,
242 skin.z - skin.nz * skin.backstopDistance};
245 if (
distance >= skin.backstopRadius)
continue;
247 :
Vec3{skin.nx, skin.ny, skin.nz};
248 const float correction = skin.backstopRadius -
distance;
255void Cloth3D::solveAttachments(
float dt) {
256 if (
dt <= 0.f)
return;
257 for (Attachment& attachment : attachments_) {
258 Particle& particle = particles_[
static_cast<size_t>(attachment.particle)];
259 const Vec3 delta{particle.
x - attachment.x, particle.y - attachment.y, particle.z - attachment.z};
262 const float alpha = attachment.compliance / (
dt *
dt);
263 const float weight = std::max(particle.inverseMass, 1.f);
264 const float dl = (-
distance - alpha * attachment.lambda) / (
weight + alpha);
265 attachment.lambda += dl;
std::vector< std::uint32_t > indices
std::array< float, 3 > scale
RoadLaneDirection direction
SceneSpatialConstraint constraint
EVENGINE_API_FOUNDATION public API.
Move-only operation result carrying either a value or Status.
float getBendCompliance() const
Return XPBD distance-bend compliance in m/N.
float getPressure() const
Return the closed-mesh target volume ratio.
uint64_t getCollisionCategoryBits() const
Return cloth collision category bits.
void setPressure(float pressure)
Set closed-mesh target volume ratio; zero disables volume solving.
uint64_t getCollisionMaskBits() const
Return cloth collision mask bits.
bool isAttached(int index) const
Return whether a particle currently has an attachment.
void setTetherScale(float scale)
Set tether maximum-length scale; zero disables tethers, one uses baked lengths.
void updateSkinReference(int index, float x, float y, float z, float nx, float ny, float nz)
Update the animated reference pose of an existing skin constraint.
bool supportsFeature(const std::string &feature) const
Query a stable cloth feature name without assuming CPU/GPU parity.
int getTetherConstraintCount() const
Return baked geodesic tether count.
float getVolumeCompliance() const
Return XPBD closed-volume compliance.
float getTetherScale() const
Return the current tether length scale.
void clearSkinConstraint(int index)
Remove a particle's skin/backstop constraint.
int getDistanceConstraintCount() const
Return the current distance-constraint count.
void detachParticle(int index)
Detach a particle; its simulated velocity remains continuous.
float getShearCompliance() const
Return XPBD shear compliance in m/N.
float getCurrentVolume() const
Return current signed mesh volume, or zero for an open model.
bool hasSkinConstraint(int index) const
Return whether a particle has a skin/backstop constraint.
eve::Result< std::vector< std::uint32_t > > captureTriangleIndices() const
Copy current surviving triangle corners into an owning ordered index stream. @ownership Owns returned...
float getTetherCompliance() const
Return XPBD tether compliance in m/N.
void updateAttachment(int index, float x, float y, float z)
Move an existing attachment target without recreating it.
void setVolumeCompliance(float compliance)
Set XPBD closed-volume compliance in m^3/N.
float getStretchCompliance() const
Return XPBD stretch compliance in m/N.
float getCollisionRestitution() const
Return cloth-side restitution.
int getSkinConstraintCount() const
Return active skin-constraint count.
int getTriangleCount() const
Return the current render/collision triangle count, reduced by tearing.
void setTetherCompliance(float compliance)
Set XPBD tether compliance in m/N.
void attachParticle(int index, float x, float y, float z, float compliance=0.f)
Attach a particle to a world-space target using an XPBD point constraint.
void setSkinConstraint(int index, float x, float y, float z, float nx, float ny, float nz, float radius, float backstopDistance=-1.f, float backstopRadius=0.f, float compliance=0.f)
Constrain a particle around an animated skin reference and optional backstop sphere.
float getCollisionFriction() const
Return cloth-side Coulomb friction.
double dot(ForceVector a, ForceVector b)
Optional physics backend for vehicle mobility and body attach.
V3 cross(V3 a, V3 b)
Module-private road bake implementation helper.