add reader to create softbodies from vtk files

This commit is contained in:
Xuchen Han
2019-08-11 15:51:49 -07:00
parent f7cd1edf4a
commit cb7257d27b
2 changed files with 106 additions and 1 deletions

View File

@@ -16,6 +16,9 @@ subject to the following restrictions:
#include "btSoftBodyInternals.h" #include "btSoftBodyInternals.h"
#include <stdio.h> #include <stdio.h>
#include <string>
#include <iostream>
#include <sstream>
#include <string.h> #include <string.h>
#include "btSoftBodyHelpers.h" #include "btSoftBodyHelpers.h"
#include "LinearMath/btConvexHull.h" #include "LinearMath/btConvexHull.h"
@@ -1228,3 +1231,101 @@ if(face&&face[0])
printf("Tetras: %u\r\n", psb->m_tetras.size()); printf("Tetras: %u\r\n", psb->m_tetras.size());
return (psb); return (psb);
} }
btSoftBody* btSoftBodyHelpers::CreateFromVtkFile(btSoftBodyWorldInfo& worldInfo, const std::string& vtk_file)
{
std::ifstream fs;
fs.open(vtk_file);
btAssert(fs);
btSoftBody* psb = CreateFromVtkFile(worldInfo, fs);
fs.close();
return psb;
}
btSoftBody* btSoftBodyHelpers::CreateFromVtkFile(btSoftBodyWorldInfo& worldInfo, std::istream& in)
{
typedef btAlignedObjectArray<int> Index;
std::string line;
btAlignedObjectArray<btVector3> X;
btVector3 position;
btAlignedObjectArray<Index> indices;
bool reading_points = false;
bool reading_tets = false;
size_t n_points = 0;
size_t n_tets = 0;
size_t x_count = 0;
size_t indices_count = 0;
while (std::getline(in, line))
{
std::stringstream ss(line);
if (line.size() == (size_t)(0))
{
}
else if (line.substr(0, 6) == "POINTS")
{
reading_points = true;
reading_tets = false;
ss.ignore(128, ' '); // ignore "POINTS"
ss >> n_points;
X.resize(n_points);
}
else if (line.substr(0, 5) == "CELLS")
{
reading_points = false;
reading_tets = true;
ss.ignore(128, ' '); // ignore "CELLS"
ss >> n_tets;
indices.resize(n_tets);
}
else if (line.substr(0, 10) == "CELL_TYPES")
{
reading_points = false;
reading_tets = false;
}
else if (reading_points)
{
btScalar p;
ss >> p;
position.setX(p);
ss >> p;
position.setY(p);
ss >> p;
position.setZ(p);
X[x_count++] = position;
}
else if (reading_tets)
{
ss.ignore(128, ' '); // ignore "4"
Index tet;
tet.resize(4);
for (size_t i = 0; i < 4; i++)
{
ss >> tet[i];
}
indices[indices_count++] = tet;
}
}
btSoftBody* psb = new btSoftBody(&worldInfo, n_points, &X[0], 0);
for (int i = 0; i < n_tets; ++i)
{
const Index& ni = indices[i];
psb->appendTetra(ni[0], ni[1], ni[2], ni[3]);
{
psb->appendLink(ni[0], ni[1], 0, true);
psb->appendLink(ni[1], ni[2], 0, true);
psb->appendLink(ni[2], ni[0], 0, true);
psb->appendLink(ni[0], ni[3], 0, true);
psb->appendLink(ni[1], ni[3], 0, true);
psb->appendLink(ni[2], ni[3], 0, true);
}
}
psb->initializeDmInverse();
printf("Nodes: %u\r\n", psb->m_nodes.size());
printf("Links: %u\r\n", psb->m_links.size());
printf("Faces: %u\r\n", psb->m_faces.size());
printf("Tetras: %u\r\n", psb->m_tetras.size());
return (psb);
}

View File

@@ -17,7 +17,8 @@ subject to the following restrictions:
#define BT_SOFT_BODY_HELPERS_H #define BT_SOFT_BODY_HELPERS_H
#include "btSoftBody.h" #include "btSoftBody.h"
#include <fstream>
#include <string>
// //
// Helpers // Helpers
// //
@@ -140,6 +141,9 @@ struct btSoftBodyHelpers
bool bfacelinks, bool bfacelinks,
bool btetralinks, bool btetralinks,
bool bfacesfromtetras); bool bfacesfromtetras);
static btSoftBody* CreateFromVtkFile(btSoftBodyWorldInfo& worldInfo, const std::string& vtk_file);
static btSoftBody* CreateFromVtkFile(btSoftBodyWorldInfo& worldInfo, std::istream& in);
/// Sort the list of links to move link calculations that are dependent upon earlier /// Sort the list of links to move link calculations that are dependent upon earlier
/// ones as far as possible away from the calculation of those values /// ones as far as possible away from the calculation of those values