refactoring

This commit is contained in:
Irlan
2018-10-08 16:18:28 -03:00
parent b48b16843a
commit f98374143a
47 changed files with 895 additions and 1046 deletions

View File

@@ -16,19 +16,16 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <bounce/dynamics/cloth/cloth.h>
#include <bounce/dynamics/cloth/cloth_mesh.h>
#include <bounce/dynamics/cloth/particle.h>
#include <bounce/dynamics/cloth/force.h>
#include <bounce/dynamics/cloth/spring_force.h>
#include <bounce/dynamics/cloth/bend_force.h>
#include <bounce/dynamics/cloth/cloth_solver.h>
#include <bounce/cloth/cloth.h>
#include <bounce/cloth/cloth_mesh.h>
#include <bounce/cloth/particle.h>
#include <bounce/cloth/force.h>
#include <bounce/cloth/spring_force.h>
#include <bounce/cloth/cloth_solver.h>
#include <bounce/dynamics/world.h>
#include <bounce/dynamics/world_listeners.h>
#include <bounce/dynamics/body.h>
#include <bounce/dynamics/shapes/shape.h>
#include <bounce/collision/collision.h>
#include <bounce/common/memory/stack_allocator.h>
#include <bounce/common/draw.h>
static B3_FORCE_INLINE u32 b3NextIndex(u32 i)
@@ -149,7 +146,7 @@ static u32 b3FindSharedEdges(b3SharedEdge* sharedEdges, const b3ClothMesh* m)
return sharedCount;
}
b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) :
b3Cloth::b3Cloth(const b3ClothDef& def) :
m_particleBlocks(sizeof(b3Particle)),
m_bodyContactBlocks(sizeof(b3BodyContact)),
m_particleContactBlocks(sizeof(b3ParticleContact)),
@@ -158,7 +155,6 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) :
B3_ASSERT(def.mesh);
B3_ASSERT(def.density > 0.0f);
m_world = world;
m_mesh = def.mesh;
m_density = def.density;
@@ -184,7 +180,7 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) :
ComputeMass();
// Create forces
b3StackAllocator* allocator = &m_world->m_stackAllocator;
b3StackAllocator* allocator = &m_stackAllocator;
// Worst-case edge memory
u32 edgeCount = 3 * m->triangleCount;
@@ -218,8 +214,8 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) :
b3Particle* p3 = m_vertexParticles[e->nsv1];
b3Particle* p4 = m_vertexParticles[e->nsv2];
b3BendForceDef fd;
fd.Initialize(p1, p2, p3, p4, def.bending, def.damping);
b3SpringForceDef fd;
fd.Initialize(p3, p4, def.bending, def.damping);
CreateForce(fd);
}
@@ -240,6 +236,9 @@ b3Cloth::b3Cloth(const b3ClothDef& def, b3World* world) :
CreateForce(fd);
}
m_gravity.SetZero();
m_world = nullptr;
}
b3Cloth::~b3Cloth()
@@ -354,33 +353,6 @@ void b3Cloth::ComputeMass()
}
}
void b3Cloth::RayCast(b3RayCastListener* listener, const b3Vec3& p1, const b3Vec3& p2)
{
b3RayCastInput input;
input.p1 = p1;
input.p2 = p2;
input.maxFraction = 1.0f;
for (u32 i = 0; i < m_mesh->triangleCount; ++i)
{
b3RayCastOutput subOutput;
if (RayCast(&subOutput, &input, i))
{
float32 fraction = subOutput.fraction;
b3Vec3 point = (1.0f - fraction) * input.p1 + fraction * input.p2;
b3Vec3 normal = subOutput.normal;
float32 newFraction = listener->ReportCloth(this, point, normal, fraction, i);
if (newFraction == 0.0f)
{
// The client has stopped the query.
return;
}
}
}
}
bool b3Cloth::RayCastSingle(b3ClothRayCastSingleOutput* output, const b3Vec3& p1, const b3Vec3& p2) const
{
b3RayCastInput input;
@@ -440,7 +412,6 @@ bool b3Cloth::RayCast(b3RayCastOutput* output, const b3RayCastInput* input, u32
return false;
}
B3_ASSERT(len > B3_EPSILON);
n /= len;
float32 numerator = b3Dot(n, v1 - p1);
@@ -483,11 +454,8 @@ bool b3Cloth::RayCast(b3RayCastOutput* output, const b3RayCastInput* input, u32
float32 v = b3Dot(QC_x_QA, AB_x_AC);
float32 w = b3Dot(QA_x_QB, AB_x_AC);
// Characteristic length of triangle
const float32 kTol = -B3_EPSILON;
// Is the intersection on the triangle?
if (u > kTol && v > kTol && w > kTol)
if (u >= 0.0f && v >= 0.0f && w >= 0.0f)
{
output->fraction = fraction;
@@ -509,6 +477,12 @@ bool b3Cloth::RayCast(b3RayCastOutput* output, const b3RayCastInput* input, u32
void b3Cloth::UpdateBodyContacts()
{
// Is there a world attached to this cloth?
if (m_world == nullptr)
{
return;
}
B3_PROFILE("Cloth Update Body Contacts");
// Clear buffer
@@ -537,9 +511,13 @@ void b3Cloth::UpdateBodyContacts()
for (b3Body* body = m_world->GetBodyList().m_head; body; body = body->GetNext())
{
if (p->m_type != e_dynamicParticle && body->GetType() != e_dynamicBody)
if (p->m_type != e_dynamicParticle)
{
continue;
}
if (body->GetType() != e_staticBody)
{
// At least one body should be kinematic or dynamic.
continue;
}
@@ -887,7 +865,7 @@ void b3Cloth::Solve(float32 dt, const b3Vec3& gravity)
// Solve
b3ClothSolverDef solverDef;
solverDef.stack = &m_world->m_stackAllocator;
solverDef.stack = &m_stackAllocator;
solverDef.particleCapacity = m_particleList.m_count;
solverDef.forceCapacity = m_forceList.m_count;
solverDef.bodyContactCapacity = m_bodyContactList.m_count;
@@ -937,14 +915,16 @@ void b3Cloth::UpdateContacts()
// Update body contacts
UpdateBodyContacts();
#if 0
// Update particle contacts
UpdateParticleContacts();
// Update triangle contacts
UpdateTriangleContacts();
#endif
}
void b3Cloth::Step(float32 dt, const b3Vec3& gravity)
void b3Cloth::Step(float32 dt)
{
B3_PROFILE("Cloth Step");
@@ -954,7 +934,7 @@ void b3Cloth::Step(float32 dt, const b3Vec3& gravity)
// Solve constraints, integrate state, clear forces and translations.
if (dt > 0.0f)
{
Solve(dt, gravity);
Solve(dt, m_gravity);
}
}

View File

@@ -16,12 +16,12 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <bounce/dynamics/cloth/cloth_contact_solver.h>
#include <bounce/dynamics/cloth/particle.h>
#include <bounce/dynamics/cloth/dense_vec3.h>
#include <bounce/cloth/cloth_contact_solver.h>
#include <bounce/cloth/particle.h>
#include <bounce/cloth/dense_vec3.h>
#include <bounce/common/memory/stack_allocator.h>
#include <bounce/dynamics/shapes/shape.h>
#include <bounce/dynamics/body.h>
#include <bounce/common/memory/stack_allocator.h>
b3ClothContactSolver::b3ClothContactSolver(const b3ClothContactSolverDef& def)
{

View File

@@ -16,10 +16,10 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <bounce/dynamics/cloth/cloth_mesh.h>
#include <bounce/garment/garment.h>
#include <bounce/garment/garment_mesh.h>
#include <bounce/garment/sewing_pattern.h>
#include <bounce/cloth/cloth_mesh.h>
#include <bounce/cloth/garment/garment.h>
#include <bounce/cloth/garment/garment_mesh.h>
#include <bounce/cloth/garment/sewing_pattern.h>
b3GarmentClothMesh::b3GarmentClothMesh()
{

View File

@@ -16,18 +16,18 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <bounce/dynamics/cloth/cloth_solver.h>
#include <bounce/dynamics/cloth/cloth_contact_solver.h>
#include <bounce/dynamics/cloth/cloth.h>
#include <bounce/dynamics/cloth/particle.h>
#include <bounce/dynamics/cloth/force.h>
#include <bounce/dynamics/cloth/dense_vec3.h>
#include <bounce/dynamics/cloth/diag_mat33.h>
#include <bounce/dynamics/cloth/sparse_sym_mat33.h>
#include <bounce/dynamics/shapes/shape.h>
#include <bounce/dynamics/body.h>
#include <bounce/cloth/cloth_solver.h>
#include <bounce/cloth/cloth_contact_solver.h>
#include <bounce/cloth/cloth.h>
#include <bounce/cloth/particle.h>
#include <bounce/cloth/force.h>
#include <bounce/cloth/dense_vec3.h>
#include <bounce/cloth/diag_mat33.h>
#include <bounce/cloth/sparse_sym_mat33.h>
#include <bounce/common/memory/stack_allocator.h>
#include <bounce/common/math/mat.h>
#include <bounce/dynamics/shapes/shape.h>
#include <bounce/dynamics/body.h>
// Here, we solve Ax = b using the Modified Preconditioned Conjugate Gradient (MPCG) algorithm.
// described in the paper:
@@ -298,7 +298,7 @@ void b3ClothSolver::Solve(float32 dt, const b3Vec3& gravity)
// Solve velocity constraints
{
const u32 kVelocityIterations = 5;
const u32 kVelocityIterations = 10;
for (u32 i = 0; i < kVelocityIterations; ++i)
{

View File

@@ -16,9 +16,8 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <bounce/dynamics/cloth/force.h>
#include <bounce/dynamics/cloth/spring_force.h>
#include <bounce/dynamics/cloth/bend_force.h>
#include <bounce/cloth/force.h>
#include <bounce/cloth/spring_force.h>
b3Force* b3Force::Create(const b3ForceDef* def)
{
@@ -31,12 +30,6 @@ b3Force* b3Force::Create(const b3ForceDef* def)
force = new (block) b3SpringForce((b3SpringForceDef*)def);
break;
}
case e_bendForce:
{
void* block = b3Alloc(sizeof(b3BendForce));
force = new (block) b3BendForce((b3BendForceDef*)def);
break;
}
default:
{
B3_ASSERT(false);
@@ -60,13 +53,6 @@ void b3Force::Destroy(b3Force* force)
b3Free(force);
break;
}
case e_bendForce:
{
b3BendForce* o = (b3BendForce*)force;
o->~b3BendForce();
b3Free(force);
break;
}
default:
{
B3_ASSERT(false);

View File

@@ -16,9 +16,9 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <bounce/garment/garment_mesh.h>
#include <bounce/garment/garment.h>
#include <bounce/garment/sewing_pattern.h>
#include <bounce/cloth/garment/garment_mesh.h>
#include <bounce/cloth/garment/garment.h>
#include <bounce/cloth/garment/sewing_pattern.h>
#define ANSI_DECLARATORS
#define REAL double

View File

@@ -16,11 +16,11 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <bounce/dynamics/cloth/particle.h>
#include <bounce/dynamics/cloth/cloth.h>
#include <bounce/dynamics/cloth/cloth_solver.h>
#include <bounce/dynamics/cloth/dense_vec3.h>
#include <bounce/dynamics/cloth/sparse_sym_mat33.h>
#include <bounce/cloth/particle.h>
#include <bounce/cloth/cloth.h>
#include <bounce/cloth/cloth_solver.h>
#include <bounce/cloth/dense_vec3.h>
#include <bounce/cloth/sparse_sym_mat33.h>
void b3BodyContactWorldPoint::Initialize(const b3BodyContact* c, float32 rA, const b3Transform& xfA, float32 rB, const b3Transform& xfB)
{

View File

@@ -16,11 +16,11 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <bounce/dynamics/cloth/spring_force.h>
#include <bounce/dynamics/cloth/particle.h>
#include <bounce/dynamics/cloth/cloth_solver.h>
#include <bounce/dynamics/cloth/dense_vec3.h>
#include <bounce/dynamics/cloth/sparse_sym_mat33.h>
#include <bounce/cloth/spring_force.h>
#include <bounce/cloth/particle.h>
#include <bounce/cloth/cloth_solver.h>
#include <bounce/cloth/dense_vec3.h>
#include <bounce/cloth/sparse_sym_mat33.h>
void b3SpringForceDef::Initialize(b3Particle* particle1, b3Particle* particle2, float32 structuralStiffness, float32 dampingStiffness)
{

View File

@@ -1,128 +0,0 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <bounce/dynamics/cloth/bend_force.h>
#include <bounce/dynamics/cloth/particle.h>
#include <bounce/dynamics/cloth/cloth_solver.h>
#include <bounce/dynamics/cloth/dense_vec3.h>
#include <bounce/dynamics/cloth/sparse_sym_mat33.h>
void b3BendForceDef::Initialize(b3Particle* particle1, b3Particle* particle2, b3Particle* particle3, b3Particle* particle4, float32 structuralStiffness, float32 dampingStiffness)
{
type = e_bendForce;
p1 = particle1;
p2 = particle2;
p3 = particle3;
p4 = particle4;
b3Vec3 x1 = p1->GetPosition();
b3Vec3 x2 = p2->GetPosition();
b3Vec3 x3 = p3->GetPosition();
b3Vec3 x4 = p4->GetPosition();
restDistance = b3Distance(x2, x3);
structural = structuralStiffness;
damping = dampingStiffness;
}
b3BendForce::b3BendForce(const b3BendForceDef* def)
{
m_type = e_bendForce;
m_p1 = def->p1;
m_p2 = def->p2;
m_p3 = def->p3;
m_p4 = def->p4;
m_L0 = def->restDistance;
m_angle0 = def->restAngle;
m_ks = def->structural;
m_kd = def->damping;
}
b3BendForce::~b3BendForce()
{
}
void b3BendForce::Apply(const b3ClothSolverData* data)
{
b3DenseVec3& x = *data->x;
b3DenseVec3& v = *data->v;
b3DenseVec3& f = *data->f;
b3SparseSymMat33& dfdx = *data->dfdx;
b3SparseSymMat33& dfdv = *data->dfdv;
u32 i1 = m_p2->m_solverId;
u32 i2 = m_p3->m_solverId;
b3Vec3 x1 = x[i1];
b3Vec3 v1 = v[i1];
b3Vec3 x2 = x[i2];
b3Vec3 v2 = v[i2];
b3Mat33 I; I.SetIdentity();
b3Vec3 ef; ef.SetZero();
if (m_ks > 0.0f)
{
b3Vec3 dx = x1 - x2;
float32 L = b3Length(dx);
if (L >= m_L0)
{
// Apply tension
b3Vec3 n = dx / L;
ef += -m_ks * (L - m_L0) * n;
// Jacobian
b3Mat33 Jx11 = -m_ks * (b3Outer(dx, dx) + (1.0f - m_L0 / L) * (I - b3Outer(dx, dx)));
b3Mat33 Jx12 = -Jx11;
//b3Mat33 Jx21 = Jx12;
b3Mat33 Jx22 = Jx11;
dfdx(i1, i1) += Jx11;
dfdx(i1, i2) += Jx12;
//dfdx(i2, i1) += Jx21;
dfdx(i2, i2) += Jx22;
}
}
if (m_kd > 0.0f)
{
// Apply damping
b3Vec3 dv = v1 - v2;
ef += -m_kd * dv;
b3Mat33 Jv11 = -m_kd * I;
b3Mat33 Jv12 = -Jv11;
//b3Mat33 Jv21 = Jv12;
b3Mat33 Jv22 = Jv11;
dfdv(i1, i1) += Jv11;
dfdv(i1, i2) += Jv12;
//dfdv(i2, i1) += Jv21;
dfdv(i2, i2) += Jv22;
}
f[i1] += ef;
f[i2] -= ef;
}

View File

@@ -17,7 +17,6 @@
*/
#include <bounce/dynamics/world.h>
#include <bounce/dynamics/cloth/cloth.h>
#include <bounce/dynamics/body.h>
#include <bounce/dynamics/island.h>
#include <bounce/dynamics/world_listeners.h>
@@ -32,7 +31,6 @@ extern u32 b3_gjkCalls, b3_gjkIters, b3_gjkMaxIters;
extern bool b3_convexCache;
b3World::b3World() :
m_clothBlocks(sizeof(b3Cloth)),
m_bodyBlocks(sizeof(b3Body))
{
b3_allocCalls = 0;
@@ -54,14 +52,6 @@ b3World::b3World() :
b3World::~b3World()
{
b3Cloth* c = m_clothList.m_head;
while (c)
{
b3Cloth* c0 = c;
c = c->m_next;
c0->~b3Cloth();
}
b3Body* b = m_bodyList.m_head;
while (b)
{
@@ -93,21 +83,6 @@ void b3World::SetSleeping(bool flag)
}
}
b3Cloth* b3World::CreateCloth(const b3ClothDef& def)
{
void* mem = m_clothBlocks.Allocate();
b3Cloth* c = new(mem) b3Cloth(def, this);
m_clothList.PushFront(c);
return c;
}
void b3World::DestroyCloth(b3Cloth* c)
{
m_clothList.Remove(c);
c->~b3Cloth();
m_clothBlocks.Free(c);
}
b3Body* b3World::CreateBody(const b3BodyDef& def)
{
void* mem = m_bodyBlocks.Allocate();
@@ -166,11 +141,6 @@ void b3World::Step(float32 dt, u32 velocityIterations, u32 positionIterations)
{
Solve(dt, velocityIterations, positionIterations);
}
//SolveTOI
// Step cloth dynamics
StepCloth(dt);
}
void b3World::Solve(float32 dt, u32 velocityIterations, u32 positionIterations)
@@ -366,16 +336,6 @@ void b3World::Solve(float32 dt, u32 velocityIterations, u32 positionIterations)
}
}
void b3World::StepCloth(float32 dt)
{
B3_PROFILE("Step Cloth");
for (b3Cloth* c = m_clothList.m_head; c; c = c->GetNext())
{
c->Step(dt, m_gravity);
}
}
struct b3ShapeRayCastCallback
{
float32 Report(const b3RayCastInput& input, u32 proxyId)
@@ -411,7 +371,7 @@ struct b3ShapeRayCastCallback
const b3BroadPhase* broadPhase;
};
void b3World::RayCastShape(b3RayCastListener* listener, const b3Vec3& p1, const b3Vec3& p2) const
void b3World::RayCast(b3RayCastListener* listener, const b3Vec3& p1, const b3Vec3& p2) const
{
b3RayCastInput input;
input.p1 = p1;
@@ -456,7 +416,7 @@ struct b3RayCastSingleShapeCallback
const b3BroadPhase* broadPhase;
};
bool b3World::RayCastSingleShape(b3RayCastSingleShapeOutput* output, const b3Vec3& p1, const b3Vec3& p2) const
bool b3World::RayCastSingle(b3RayCastSingleOutput* output, const b3Vec3& p1, const b3Vec3& p2) const
{
b3RayCastInput input;
input.p1 = p1;
@@ -489,52 +449,6 @@ bool b3World::RayCastSingleShape(b3RayCastSingleShapeOutput* output, const b3Vec
return false;
}
void b3World::RayCastCloth(b3RayCastListener* listener, const b3Vec3& p1, const b3Vec3& p2) const
{
for (b3Cloth* c = m_clothList.m_head; c; c = c->m_next)
{
c->RayCast(listener, p1, p2);
}
}
bool b3World::RayCastSingleCloth(b3RayCastSingleClothOutput* output, const b3Vec3& p1, const b3Vec3& p2) const
{
b3Cloth* cloth0 = NULL;
b3ClothRayCastSingleOutput output0;
output0.fraction = B3_MAX_FLOAT;
for (b3Cloth* c = m_clothList.m_head; c; c = c->m_next)
{
b3ClothRayCastSingleOutput subOutput;
if (c->RayCastSingle(&subOutput, p1, p2))
{
if (subOutput.fraction < output0.fraction)
{
cloth0 = c;
output0 = subOutput;
}
}
}
if (cloth0 != NULL)
{
u32 triangle = output0.triangle;
float32 fraction = output0.fraction;
b3Vec3 point = (1.0f - fraction) * p1 + fraction * p2;
b3Vec3 normal = output0.normal;
output->cloth = cloth0;
output->triangle = triangle;
output->point = point;
output->normal = normal;
output->fraction = fraction;
return true;
}
return false;
}
struct b3QueryAABBCallback
{
bool Report(u32 proxyID)

View File

@@ -16,8 +16,8 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <bounce/dynamics/rope/rope.h>
#include <bounce/dynamics/spatial.h>
#include <bounce/rope/rope.h>
#include <bounce/rope/spatial.h>
#include <bounce/common/draw.h>
struct b3RopeBody