|
| 1 | +#include "TetModel.h" |
| 2 | +#include "PositionBasedDynamics\PositionBasedDynamics.h" |
| 3 | + |
| 4 | +using namespace PBD; |
| 5 | + |
| 6 | +TetModel::TetModel() : |
| 7 | + m_visMesh(), |
| 8 | + m_particleMesh() |
| 9 | +{ |
| 10 | + m_stiffness = 1.0f; |
| 11 | + m_poissonRatio = 0.3f; |
| 12 | + m_normalizeShear = false; |
| 13 | + m_normalizeStretch = false; |
| 14 | +} |
| 15 | + |
| 16 | +TetModel::~TetModel(void) |
| 17 | +{ |
| 18 | + cleanupModel(); |
| 19 | +} |
| 20 | + |
| 21 | +void TetModel::cleanupModel() |
| 22 | +{ |
| 23 | + m_particleMesh.release(); |
| 24 | +} |
| 25 | + |
| 26 | +TetModel::ParticleMesh &TetModel::getParticleMesh() |
| 27 | +{ |
| 28 | + return m_particleMesh; |
| 29 | +} |
| 30 | + |
| 31 | +TetModel::VisMesh &TetModel::getVisMesh() |
| 32 | +{ |
| 33 | + return m_visMesh; |
| 34 | +} |
| 35 | + |
| 36 | +void TetModel::setGeometry(const unsigned int nPoints, Eigen::Vector3f* coords, const unsigned int nTets, unsigned int* indices) |
| 37 | +{ |
| 38 | + m_particleMesh.release(); |
| 39 | + m_particleMesh.initMemory(nPoints, nTets * 6, nTets * 4, nTets); |
| 40 | + |
| 41 | + ParticleData& pd = m_particleMesh.getVertexData(); |
| 42 | + for (unsigned int i = 0; i < nPoints; i++) |
| 43 | + { |
| 44 | + m_particleMesh.addVertex(coords[i]); |
| 45 | + } |
| 46 | + for (unsigned int i = 0; i < nTets; i++) |
| 47 | + { |
| 48 | + m_particleMesh.addTet(&indices[4 * i]); |
| 49 | + } |
| 50 | + m_particleMesh.buildNeighbors(); |
| 51 | + |
| 52 | + createVisMesh(); |
| 53 | + |
| 54 | + initConstraints(); |
| 55 | +} |
| 56 | + |
| 57 | +void TetModel::reset() |
| 58 | +{ |
| 59 | + const unsigned int nPoints = m_particleMesh.numVertices(); |
| 60 | + ParticleData &pd = m_particleMesh.getVertexData(); |
| 61 | + |
| 62 | + for (unsigned int i = 0; i < nPoints; i++) |
| 63 | + { |
| 64 | + const Eigen::Vector3f& x0 = pd.getPosition0(i); |
| 65 | + pd.getPosition(i) = x0; |
| 66 | + pd.getLastPosition(i) = pd.getPosition(i); |
| 67 | + pd.getVelocity(i).setZero(); |
| 68 | + pd.getAcceleration(i).setZero(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void TetModel::initConstraints() |
| 73 | +{ |
| 74 | + initTetConstraints(); |
| 75 | +} |
| 76 | + |
| 77 | +void TetModel::initTetConstraints() |
| 78 | +{ |
| 79 | + m_tetConstraints.clear(); |
| 80 | + unsigned int nTets = getParticleMesh().numTets(); |
| 81 | + const unsigned int *tets = getParticleMesh().getTets().data(); |
| 82 | + const ParticleData &pd = getParticleMesh().getVertexData(); |
| 83 | + for (unsigned int i = 0; i < nTets; i++) |
| 84 | + { |
| 85 | + const Eigen::Vector3f &x1 = pd.getPosition(tets[4 * i]); |
| 86 | + const Eigen::Vector3f &x2 = pd.getPosition(tets[4 * i + 1]); |
| 87 | + const Eigen::Vector3f &x3 = pd.getPosition(tets[4 * i + 2]); |
| 88 | + const Eigen::Vector3f &x4 = pd.getPosition(tets[4 * i + 3]); |
| 89 | + |
| 90 | + TetConstraint tc; |
| 91 | + PositionBasedDynamics::computeStrainTetraInvRestMat(x1, x2, x3, x4, tc.invRestMat_SBD); |
| 92 | + PositionBasedDynamics::computeFEMTetraInvRestMat(x1, x2, x3, x4, tc.tetVolume, tc.invRestMat_FEM); |
| 93 | + m_tetConstraints.push_back(tc); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +std::vector<TetModel::TetConstraint> & TetModel::getTetConstraints() |
| 98 | +{ |
| 99 | + return m_tetConstraints; |
| 100 | +} |
| 101 | + |
| 102 | +void TetModel::createVisMesh() |
| 103 | +{ |
| 104 | + const unsigned int nVerts = m_particleMesh.numVertices(); |
| 105 | + const ParticleData &pd = getParticleMesh().getVertexData(); |
| 106 | + |
| 107 | + m_visMesh.initMemory(nVerts, m_particleMesh.numEdges(), m_particleMesh.numFaces()); |
| 108 | + |
| 109 | + // Add points |
| 110 | + for (unsigned int i = 0; i < nVerts; i++) |
| 111 | + { |
| 112 | + m_visMesh.addVertex(pd.getPosition0(i)); |
| 113 | + } |
| 114 | + |
| 115 | + // Search for all border faces of the tet mesh |
| 116 | + const IndexedTetMesh<ParticleData>::Face *faceData = m_particleMesh.getFaceData().data(); |
| 117 | + const unsigned int *faces = m_particleMesh.getFaces().data(); |
| 118 | + for (unsigned int i = 0; i < m_particleMesh.numFaces(); i++) |
| 119 | + { |
| 120 | + const IndexedTetMesh<ParticleData>::Face &face = faceData[i]; |
| 121 | + // Found border face |
| 122 | + if ((face.m_tets[1] == 0xffffffff) || (face.m_tets[0] == 0xffffffff)) |
| 123 | + { |
| 124 | + m_visMesh.addFace(&faces[3 * i]); |
| 125 | + } |
| 126 | + } |
| 127 | + m_visMesh.buildNeighbors(); |
| 128 | + |
| 129 | + m_visMesh.updateNormals(); |
| 130 | + m_visMesh.updateVertexNormals(); |
| 131 | +} |
| 132 | + |
0 commit comments