add a small framework of garments, update the related tests
This commit is contained in:
120
src/bounce/dynamics/cloth/cloth_mesh.cpp
Normal file
120
src/bounce/dynamics/cloth/cloth_mesh.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* 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/cloth_mesh.h>
|
||||
#include <bounce/garment/garment.h>
|
||||
#include <bounce/garment/garment_mesh.h>
|
||||
#include <bounce/garment/sewing_pattern.h>
|
||||
|
||||
b3GarmentClothMesh::b3GarmentClothMesh()
|
||||
{
|
||||
vertexCount = 0;
|
||||
vertices = nullptr;
|
||||
triangleCount = 0;
|
||||
triangles = nullptr;
|
||||
meshCount = 0;
|
||||
meshes = nullptr;
|
||||
sewingLineCount = 0;
|
||||
sewingLines = nullptr;
|
||||
}
|
||||
|
||||
b3GarmentClothMesh::~b3GarmentClothMesh()
|
||||
{
|
||||
b3Free(vertices);
|
||||
b3Free(triangles);
|
||||
b3Free(meshes);
|
||||
b3Free(sewingLines);
|
||||
}
|
||||
|
||||
void b3GarmentClothMesh::Set(const b3GarmentMesh* garment)
|
||||
{
|
||||
B3_ASSERT(vertexCount == 0);
|
||||
for (u32 i = 0; i < garment->meshCount; ++i)
|
||||
{
|
||||
vertexCount += garment->meshes[i].vertexCount;
|
||||
}
|
||||
vertices = (b3Vec3*)b3Alloc(vertexCount * sizeof(b3Vec3));
|
||||
|
||||
B3_ASSERT(triangleCount == 0);
|
||||
for (u32 i = 0; i < garment->meshCount; ++i)
|
||||
{
|
||||
triangleCount += garment->meshes[i].triangleCount;
|
||||
}
|
||||
triangles = (b3ClothMeshTriangle*)b3Alloc(triangleCount * sizeof(b3ClothMeshTriangle));
|
||||
|
||||
B3_ASSERT(meshCount == 0);
|
||||
meshCount = garment->meshCount;
|
||||
meshes = (b3ClothMeshMesh*)b3Alloc(meshCount * sizeof(b3ClothMeshMesh));
|
||||
|
||||
B3_ASSERT(sewingLineCount == 0);
|
||||
sewingLineCount = garment->sewingCount;
|
||||
sewingLines = (b3ClothMeshSewingLine*)b3Alloc(sewingLineCount * sizeof(b3ClothMeshSewingLine));
|
||||
|
||||
u32 vertex_count = 0;
|
||||
for (u32 pattern_index = 0; pattern_index < garment->meshCount; ++pattern_index)
|
||||
{
|
||||
u32 pattern_vertex_count = garment->meshes[pattern_index].vertexCount;
|
||||
|
||||
meshes[pattern_index].vertexCount = pattern_vertex_count;
|
||||
meshes[pattern_index].startVertex = vertex_count;
|
||||
|
||||
for (u32 pattern_vertex = 0; pattern_vertex < pattern_vertex_count; ++pattern_vertex)
|
||||
{
|
||||
vertices[vertex_count].x = garment->meshes[pattern_index].vertices[pattern_vertex].x;
|
||||
vertices[vertex_count].y = garment->meshes[pattern_index].vertices[pattern_vertex].y;
|
||||
vertices[vertex_count].z = 0.0f;
|
||||
|
||||
++vertex_count;
|
||||
}
|
||||
}
|
||||
|
||||
u32 triangle_count = 0;
|
||||
for (u32 pattern_index = 0; pattern_index < garment->meshCount; ++pattern_index)
|
||||
{
|
||||
u32 pattern_triangle_count = garment->meshes[pattern_index].triangleCount;
|
||||
|
||||
meshes[pattern_index].triangleCount = pattern_triangle_count;
|
||||
meshes[pattern_index].startTriangle = triangle_count;
|
||||
|
||||
for (u32 pattern_triangle = 0; pattern_triangle < pattern_triangle_count; ++pattern_triangle)
|
||||
{
|
||||
u32 pattern_v1 = garment->meshes[pattern_index].triangles[pattern_triangle].v1;
|
||||
u32 pattern_v2 = garment->meshes[pattern_index].triangles[pattern_triangle].v2;
|
||||
u32 pattern_v3 = garment->meshes[pattern_index].triangles[pattern_triangle].v3;
|
||||
|
||||
u32 cloth_v1 = meshes[pattern_index].startVertex + pattern_v1;
|
||||
u32 cloth_v2 = meshes[pattern_index].startVertex + pattern_v2;
|
||||
u32 cloth_v3 = meshes[pattern_index].startVertex + pattern_v3;
|
||||
|
||||
triangles[triangle_count].v1 = cloth_v1;
|
||||
triangles[triangle_count].v2 = cloth_v2;
|
||||
triangles[triangle_count].v3 = cloth_v3;
|
||||
|
||||
++triangle_count;
|
||||
}
|
||||
}
|
||||
|
||||
for (u32 sewing_line_index = 0; sewing_line_index < garment->sewingCount; ++sewing_line_index)
|
||||
{
|
||||
sewingLines[sewing_line_index].s1 = garment->sewingLines[sewing_line_index].s1;
|
||||
sewingLines[sewing_line_index].v1 = meshes[sewingLines[sewing_line_index].s1].startVertex + garment->sewingLines[sewing_line_index].v1;
|
||||
|
||||
sewingLines[sewing_line_index].s2 = garment->sewingLines[sewing_line_index].s2;
|
||||
sewingLines[sewing_line_index].v2 = meshes[sewingLines[sewing_line_index].s2].startVertex + garment->sewingLines[sewing_line_index].v2;
|
||||
}
|
||||
}
|
@@ -20,8 +20,8 @@
|
||||
#include <bounce/dynamics/cloth/spring_solver.h>
|
||||
#include <bounce/dynamics/cloth/dense_vec3.h>
|
||||
#include <bounce/dynamics/cloth/sparse_mat33.h>
|
||||
#include <bounce/dynamics/cloth/cloth_mesh.h>
|
||||
#include <bounce/dynamics/shapes/shape.h>
|
||||
#include <bounce/collision/shapes/mesh.h>
|
||||
#include <bounce/common/memory/stack_allocator.h>
|
||||
#include <bounce/common/draw.h>
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
#define B3_CLOTH_BENDING 0
|
||||
|
||||
#define B3_CLOTH_FRICTION 0
|
||||
#define B3_CLOTH_FRICTION 1
|
||||
|
||||
b3SpringCloth::b3SpringCloth()
|
||||
{
|
||||
@@ -86,13 +86,13 @@ struct b3UniqueEdge
|
||||
u32 v1, v2;
|
||||
};
|
||||
|
||||
static u32 b3FindUniqueEdges(b3UniqueEdge* uniqueEdges, const b3Mesh* m)
|
||||
static u32 b3FindUniqueEdges(b3UniqueEdge* uniqueEdges, const b3ClothMesh* m)
|
||||
{
|
||||
u32 uniqueCount = 0;
|
||||
|
||||
for (u32 i = 0; i < m->triangleCount; ++i)
|
||||
{
|
||||
b3Triangle* t1 = m->triangles + i;
|
||||
b3ClothMeshTriangle* t1 = m->triangles + i;
|
||||
u32 i1s[3] = { t1->v1, t1->v2, t1->v3 };
|
||||
|
||||
for (u32 j1 = 0; j1 < 3; ++j1)
|
||||
@@ -138,13 +138,13 @@ struct b3SharedEdge
|
||||
u32 nsv1, nsv2;
|
||||
};
|
||||
|
||||
static u32 b3FindSharedEdges(b3SharedEdge* sharedEdges, const b3Mesh* m)
|
||||
static u32 b3FindSharedEdges(b3SharedEdge* sharedEdges, const b3ClothMesh* m)
|
||||
{
|
||||
u32 sharedCount = 0;
|
||||
|
||||
for (u32 i = 0; i < m->triangleCount; ++i)
|
||||
{
|
||||
b3Triangle* t1 = m->triangles + i;
|
||||
b3ClothMeshTriangle* t1 = m->triangles + i;
|
||||
u32 i1s[3] = { t1->v1, t1->v2, t1->v3 };
|
||||
|
||||
for (u32 j1 = 0; j1 < 3; ++j1)
|
||||
@@ -156,7 +156,7 @@ static u32 b3FindSharedEdges(b3SharedEdge* sharedEdges, const b3Mesh* m)
|
||||
|
||||
for (u32 j = i + 1; j < m->triangleCount; ++j)
|
||||
{
|
||||
b3Triangle* t2 = m->triangles + j;
|
||||
b3ClothMeshTriangle* t2 = m->triangles + j;
|
||||
u32 i2s[3] = { t2->v1, t2->v2, t2->v3 };
|
||||
|
||||
for (u32 j2 = 0; j2 < 3; ++j2)
|
||||
@@ -207,7 +207,7 @@ void b3SpringCloth::Initialize(const b3SpringClothDef& def)
|
||||
|
||||
m_gravity = def.gravity;
|
||||
|
||||
const b3Mesh* m = m_mesh;
|
||||
const b3ClothMesh* m = m_mesh;
|
||||
|
||||
m_massCount = m->vertexCount;
|
||||
m_x = (b3Vec3*)b3Alloc(m_massCount * sizeof(b3Vec3));
|
||||
@@ -244,7 +244,7 @@ void b3SpringCloth::Initialize(const b3SpringClothDef& def)
|
||||
// Initialize mass
|
||||
for (u32 i = 0; i < m->triangleCount; ++i)
|
||||
{
|
||||
b3Triangle* t = m->triangles + i;
|
||||
b3ClothMeshTriangle* t = m->triangles + i;
|
||||
|
||||
b3Vec3 p1 = m->vertices[t->v1];
|
||||
b3Vec3 p2 = m->vertices[t->v2];
|
||||
@@ -288,9 +288,11 @@ void b3SpringCloth::Initialize(const b3SpringClothDef& def)
|
||||
|
||||
#endif
|
||||
|
||||
springCapacity += m->sewingLineCount;
|
||||
|
||||
m_springs = (b3Spring*)b3Alloc(springCapacity * sizeof(b3Spring));
|
||||
|
||||
// Streching
|
||||
// Tension
|
||||
for (u32 i = 0; i < uniqueCount; ++i)
|
||||
{
|
||||
b3UniqueEdge* e = uniqueEdges + i;
|
||||
@@ -336,6 +338,21 @@ void b3SpringCloth::Initialize(const b3SpringClothDef& def)
|
||||
|
||||
m_allocator->Free(uniqueEdges);
|
||||
|
||||
// Sewing
|
||||
for (u32 i = 0; i < m->sewingLineCount; ++i)
|
||||
{
|
||||
b3ClothMeshSewingLine* line = m->sewingLines + i;
|
||||
|
||||
b3Spring* S = m_springs + m_springCount;
|
||||
S->type = e_strechSpring;
|
||||
S->i1 = line->v1;
|
||||
S->i2 = line->v2;
|
||||
S->L0 = 0.0f;
|
||||
S->ks = def.ks;
|
||||
S->kd = def.kd;
|
||||
++m_springCount;
|
||||
}
|
||||
|
||||
B3_ASSERT(m_springCount <= springCapacity);
|
||||
}
|
||||
|
||||
@@ -680,7 +697,7 @@ void b3SpringCloth::Apply() const
|
||||
|
||||
void b3SpringCloth::Draw() const
|
||||
{
|
||||
const b3Mesh* m = m_mesh;
|
||||
const b3ClothMesh* m = m_mesh;
|
||||
|
||||
for (u32 i = 0; i < m->vertexCount; ++i)
|
||||
{
|
||||
@@ -719,10 +736,20 @@ void b3SpringCloth::Draw() const
|
||||
b3Draw_draw->DrawSegment(x1, x2, b3Color_black);
|
||||
}
|
||||
}
|
||||
|
||||
for (u32 i = 0; i < m->sewingLineCount; ++i)
|
||||
{
|
||||
b3ClothMeshSewingLine* s = m->sewingLines + i;
|
||||
|
||||
b3Vec3 x1 = m_x[s->v1];
|
||||
b3Vec3 x2 = m_x[s->v2];
|
||||
|
||||
b3Draw_draw->DrawSegment(x1, x2, b3Color_white);
|
||||
}
|
||||
|
||||
for (u32 i = 0; i < m->triangleCount; ++i)
|
||||
{
|
||||
b3Triangle* t = m->triangles + i;
|
||||
b3ClothMeshTriangle* t = m->triangles + i;
|
||||
|
||||
b3Vec3 v1 = m_x[t->v1];
|
||||
b3Vec3 v2 = m_x[t->v2];
|
||||
@@ -735,4 +762,4 @@ void b3SpringCloth::Draw() const
|
||||
b3Vec3 n2 = -n1;
|
||||
b3Draw_draw->DrawSolidTriangle(n2, v1, v3, v2, b3Color_blue);
|
||||
}
|
||||
}
|
||||
}
|
187
src/bounce/garment/garment_mesh.cpp
Normal file
187
src/bounce/garment/garment_mesh.cpp
Normal file
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* 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/garment/garment_mesh.h>
|
||||
#include <bounce/garment/garment.h>
|
||||
#include <bounce/garment/sewing_pattern.h>
|
||||
|
||||
#define ANSI_DECLARATORS
|
||||
#define REAL double
|
||||
#define VOID void
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <triangle/triangle.h>
|
||||
}
|
||||
|
||||
b3GarmentMesh::b3GarmentMesh()
|
||||
{
|
||||
meshCount = 0;
|
||||
meshes = nullptr;
|
||||
sewingCount = 0;
|
||||
sewingLines = nullptr;
|
||||
garment = nullptr;
|
||||
}
|
||||
|
||||
b3GarmentMesh::~b3GarmentMesh()
|
||||
{
|
||||
for (u32 i = 0; i < meshCount; ++i)
|
||||
{
|
||||
b3Free(meshes[i].vertices);
|
||||
b3Free(meshes[i].triangles);
|
||||
}
|
||||
|
||||
b3Free(meshes);
|
||||
b3Free(sewingLines);
|
||||
}
|
||||
|
||||
//
|
||||
static void b3Set(b3SewingPatternMesh* mesh, float32 desiredArea, const b3SewingPattern* pattern)
|
||||
{
|
||||
B3_ASSERT(desiredArea > B3_EPSILON);
|
||||
|
||||
struct triangulateio in, mid, out;
|
||||
|
||||
// Prepare the input structure
|
||||
in.pointlist = (REAL*)b3Alloc(pattern->vertexCount * 2 * sizeof(REAL));
|
||||
const float32* fp = (float32*)pattern->vertices;
|
||||
for (u32 i = 0; i < 2 * pattern->vertexCount; ++i)
|
||||
{
|
||||
in.pointlist[i] = (REAL)fp[i];
|
||||
}
|
||||
in.pointattributelist = NULL;
|
||||
in.pointmarkerlist = NULL;
|
||||
in.numberofpoints = pattern->vertexCount;
|
||||
in.numberofpointattributes = 0;
|
||||
|
||||
in.trianglelist = NULL;
|
||||
in.triangleattributelist = NULL;
|
||||
|
||||
in.trianglearealist = NULL;
|
||||
|
||||
in.numberoftriangles = 0;
|
||||
in.numberofcorners = 0;
|
||||
in.numberoftriangleattributes = 0;
|
||||
|
||||
in.segmentlist = NULL;
|
||||
in.segmentmarkerlist = NULL;
|
||||
in.numberofsegments = 0;
|
||||
|
||||
in.holelist = NULL;
|
||||
in.numberofholes = 0;
|
||||
|
||||
in.regionlist = NULL;
|
||||
in.numberofregions = 0;
|
||||
|
||||
// Prepare the middle structure
|
||||
mid.pointlist = NULL;
|
||||
mid.pointmarkerlist = NULL;
|
||||
|
||||
mid.trianglelist = NULL;
|
||||
mid.triangleattributelist = NULL;
|
||||
mid.trianglearealist = NULL;
|
||||
mid.neighborlist = NULL;
|
||||
|
||||
mid.segmentlist = NULL;
|
||||
mid.segmentmarkerlist = NULL;
|
||||
|
||||
// Run triangulation
|
||||
// z - zero based indices
|
||||
// p - PSLG
|
||||
// c - preserve the convex hull
|
||||
triangulate("zpc", &in, &mid, NULL);
|
||||
|
||||
// Refine
|
||||
|
||||
// Prepare middle structure
|
||||
mid.trianglearealist = (REAL*)b3Alloc(mid.numberoftriangles * sizeof(REAL));
|
||||
for (int i = 0; i < mid.numberoftriangles; ++i)
|
||||
{
|
||||
mid.trianglearealist[i] = desiredArea;
|
||||
}
|
||||
|
||||
// Prepare output structure
|
||||
out.pointlist = NULL;
|
||||
out.pointmarkerlist = NULL;
|
||||
|
||||
out.trianglelist = NULL;
|
||||
out.trianglearealist = NULL;
|
||||
|
||||
out.segmentlist = NULL;
|
||||
out.segmentmarkerlist = NULL;
|
||||
|
||||
// Run triangulation
|
||||
// z - zero based indices
|
||||
// p - PSLG
|
||||
// c - preserve the convex hull
|
||||
// r - read triangles
|
||||
triangulate("zpcra", &mid, &out, NULL);
|
||||
|
||||
// Convert the output structure
|
||||
|
||||
// The mesh must be empty
|
||||
mesh->vertices = (b3Vec2*)b3Alloc(out.numberofpoints * sizeof(b3Vec2));
|
||||
mesh->vertexCount = out.numberofpoints;
|
||||
for (int i = 0; i < out.numberofpoints; ++i)
|
||||
{
|
||||
mesh->vertices[i].x = (float32)out.pointlist[2 * i + 0];
|
||||
mesh->vertices[i].y = (float32)out.pointlist[2 * i + 1];
|
||||
}
|
||||
|
||||
mesh->triangles = (b3SewingPatternMeshTriangle*)b3Alloc(out.numberoftriangles * sizeof(b3SewingPatternMeshTriangle));
|
||||
mesh->triangleCount = out.numberoftriangles;
|
||||
for (int i = 0; i < out.numberoftriangles; ++i)
|
||||
{
|
||||
b3SewingPatternMeshTriangle triangle;
|
||||
triangle.v1 = out.trianglelist[3 * i + 0];
|
||||
triangle.v2 = out.trianglelist[3 * i + 1];
|
||||
triangle.v3 = out.trianglelist[3 * i + 2];
|
||||
|
||||
mesh->triangles[i] = triangle;
|
||||
}
|
||||
|
||||
// Free the input structure
|
||||
b3Free(in.pointlist);
|
||||
|
||||
// Free the middle structure
|
||||
free(mid.pointlist);
|
||||
free(mid.pointmarkerlist);
|
||||
free(mid.trianglelist);
|
||||
free(mid.triangleattributelist);
|
||||
free(mid.trianglearealist);
|
||||
free(mid.segmentlist);
|
||||
free(mid.segmentmarkerlist);
|
||||
|
||||
// Free the output structure
|
||||
free(out.pointlist);
|
||||
free(out.pointmarkerlist);
|
||||
free(out.trianglelist);
|
||||
free(out.segmentlist);
|
||||
free(out.segmentmarkerlist);
|
||||
}
|
||||
|
||||
void b3GarmentMesh::Set(b3Garment* g, float32 desiredArea)
|
||||
{
|
||||
garment = g;
|
||||
meshCount = garment->patternCount;
|
||||
meshes = (b3SewingPatternMesh*)b3Alloc(garment->patternCount * sizeof(b3SewingPatternMesh));
|
||||
for (u32 i = 0; i < garment->patternCount; ++i)
|
||||
{
|
||||
b3Set(meshes + i, desiredArea, garment->patterns[i]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user