1#ifndef PBAT_SIM_CONTACT_MULTIBODYTETRAHEDRALMESHSYSTEM_H
2#define PBAT_SIM_CONTACT_MULTIBODYTETRAHEDRALMESHSYSTEM_H
9#include "pbat/graph/BreadthFirstSearch.h"
10#include "pbat/graph/ConnectedComponents.h"
24template <common::CIndex TIndex = Index, common::CArithmetic TScalar = Scalar>
25struct MultibodyTetrahedralMeshSystem
30 MultibodyTetrahedralMeshSystem() =
default;
38 Eigen::Ref<Eigen::Matrix<TScalar, 3, Eigen::Dynamic>> X,
39 Eigen::Ref<Eigen::Matrix<IndexType, 4, Eigen::Dynamic>> T);
49 Eigen::Ref<Eigen::Matrix<TScalar, 3, Eigen::Dynamic>> X,
50 Eigen::Ref<Eigen::Matrix<IndexType, 4, Eigen::Dynamic>> T,
83 template <
class TDerivedT>
86 return T.middleCols(
TP[o],
TP[o + 1] -
TP[o]);
96 template <
class TDerivedX>
99 return X(Eigen::placeholders::all,
V.segment(
VP[o],
VP[o + 1] -
VP[o]));
126 template <
class TDerivedT>
174 return std::make_pair(
FP[o],
FP[o + 1] - 1);
208 Eigen::Vector<TIndex, Eigen::Dynamic>
210 Eigen::Matrix<TIndex, 2, Eigen::Dynamic>
212 Eigen::Matrix<TIndex, 3, Eigen::Dynamic>
215 Eigen::Vector<TIndex, Eigen::Dynamic>
217 Eigen::Vector<TIndex, Eigen::Dynamic>
219 Eigen::Vector<TIndex, Eigen::Dynamic>
221 Eigen::Vector<TIndex, Eigen::Dynamic>
TP;
225 Eigen::Vector<TIndex, Eigen::Dynamic>
CC;
227 Eigen::Matrix<TScalar, 2, Eigen::Dynamic>
233template <common::CIndex TIndex, common::CArithmetic TScalar>
234inline MultibodyTetrahedralMeshSystem<TIndex, TScalar>::MultibodyTetrahedralMeshSystem(
235 Eigen::Ref<Eigen::Matrix<TScalar, 3, Eigen::Dynamic>> X,
236 Eigen::Ref<Eigen::Matrix<IndexType, 4, Eigen::Dynamic>> T)
237 : MultibodyTetrahedralMeshSystem<TIndex, TScalar>()
242template <common::CIndex TIndex, common::CArithmetic TScalar>
244 Eigen::Ref<Eigen::Matrix<TScalar, 3, Eigen::Dynamic>> X,
245 Eigen::Ref<Eigen::Matrix<IndexType, 4, Eigen::Dynamic>> T,
249 PBAT_PROFILE_NAMED_SCOPE(
"pbat.sim.contact.MultibodyTetrahedralMeshSystem.Construct");
253 Eigen::SparseMatrix<IndexType, Eigen::ColMajor, IndexType>
const EG =
257 Eigen::Vector<IndexType, Eigen::Dynamic> ECC(nElements);
260 Eigen::Map<Eigen::Vector<IndexType, Eigen::Dynamic>
const>(
263 Eigen::Map<Eigen::Vector<IndexType, Eigen::Dynamic>
const>(
270 auto verticesToElements =
271 Eigen::Vector<IndexType, Eigen::Dynamic>::LinSpaced(nElements, 0, nElements - 1)
272 .template replicate<1, 4>()
276 CC(T.reshaped()) = ECC(verticesToElements);
278 Eigen::Vector<IndexType, Eigen::Dynamic> Eordering =
282 return ECC[ei] < ECC[ej];
284 for (
auto r = 0; r < T.rows(); ++r)
288 Eigen::Vector<IndexType, Eigen::Dynamic> Xordering =
290 for (
auto d = 0; d < X.rows(); ++d)
295 Eigen::Vector<IndexType, Eigen::Dynamic> XorderingInverse(nNodes);
296 XorderingInverse(Xordering) =
297 Eigen::Vector<IndexType, Eigen::Dynamic>::LinSpaced(nNodes, 0, nNodes - 1);
298 T.reshaped() = XorderingInverse(T.reshaped());
307 auto const nTriangles =
F.cols();
308 for (
auto f = 0, e = 0; f < nTriangles; ++f)
310 for (
auto k = 0; k < 3; ++k)
313 auto j =
F((k + 1) % 3, f);
324 VP.setZero(nComponents + 1);
325 EP.setZero(nComponents + 1);
326 FP.setZero(nComponents + 1);
327 TP.setZero(nComponents + 1);
331 for (
IndexType o = 0; o < nComponents; ++o)
334 auto& vosum =
VP(o + 1);
336 while (vosum < nContactVertices and
CC(
V(vosum)) == o)
339 auto& eosum =
EP(o + 1);
341 while (eosum < nContactEdges and
CC(
E(0, eosum)) == o)
344 auto& fosum =
FP(o + 1);
346 while (fosum < nContactFaces and
CC(
F(0, fosum)) == o)
349 auto& tosum =
TP(o + 1);
351 while (tosum < nElements and
CC(T(0, tosum)) == o)
355 muF.resize(2, nContactFaces);
359template <common::CIndex TIndex, common::CArithmetic TScalar>
363 muF(Eigen::placeholders::all, Eigen::seqN(
FP[o],
FP[o + 1] -
FP[o])).colwise() =
364 Eigen::Vector2<TScalar>(muS, muD);
367template <common::CIndex TIndex, common::CArithmetic TScalar>
370 this->
muF.colwise() = Eigen::Vector2<TScalar>(muS, muD);
373template <common::CIndex TIndex, common::CArithmetic TScalar>
376 io::Archive group = archive[
"pbat.sim.contact.MultibodyTetrahedralMeshSystem"];
388template <common::CIndex TIndex, common::CArithmetic TScalar>
391 io::Archive group = archive[
"pbat.sim.contact.MultibodyTetrahedralMeshSystem"];
This file contains functions to compute the boundary of a mesh.
Permutation of values in-place.
Archive class for reading and writing data to HDF5 files.
Definition Archive.h:29
T ReadData(std::string const &path) const
Read data from the archive.
Definition Archive.h:179
void WriteData(std::string const &path, T const &data)
Write data to the archive.
Definition Archive.h:137
Concepts for common types.
void Permute(TValuesBegin vb, TValuesEnd ve, TPermutationBegin pb)
Permute the values in-place according to the permutation.
Definition Permute.h:39
auto ArgSort(TIndex n, FLess less) -> Eigen::Vector< TIndex, Eigen::Dynamic >
Computes the indices that would sort an array.
Definition ArgSort.h:32
auto SimplexMeshBoundary(Eigen::Ref< Eigen::Matrix< TIndex, Eigen::Dynamic, Eigen::Dynamic > const > const &C, TIndex n) -> std::tuple< Eigen::Vector< TIndex, Eigen::Dynamic >, Eigen::Matrix< TIndex, Eigen::Dynamic, Eigen::Dynamic > >
Obtains the boundary mesh of a simplex mesh.
Definition MeshBoundary.h:40
auto MeshDualGraph(Eigen::DenseBase< TDerivedE > const &E, TIndex nNodes=TIndex(-1), EMeshDualGraphOptions opts=EMeshDualGraphOptions::All) -> Eigen::SparseMatrix< TIndex, Eigen::ColMajor, TIndex >
Construct dual graph of input mesh, i.e. the graph of adjacent elements.
Definition Mesh.h:147
TIndex ConnectedComponents(Eigen::DenseBase< TDerivedP > const &ptr, Eigen::DenseBase< TDerivedAdj > const &adj, Eigen::Ref< Eigen::Vector< TIndex, Eigen::Dynamic > > components, DepthFirstSearch< TIndex > &dfs)
Compute connected components of a graph using depth-first search.
Definition ConnectedComponents.h:27
Profiling utilities for the Physics-Based Animation Toolkit (PBAT)
Definition BreadthFirstSearch.h:14
void Construct(Eigen::Ref< Eigen::Matrix< TScalar, 3, Eigen::Dynamic > > X, Eigen::Ref< Eigen::Matrix< IndexType, 4, Eigen::Dynamic > > T, TScalar muS=0.4, TScalar muD=0.2)
Construct a new Multibody Tetrahedral Mesh System object.
Definition MultibodyTetrahedralMeshSystem.h:243
Eigen::Index NumBodies() const
Get the number of bodies in the multibody system.
Definition MultibodyTetrahedralMeshSystem.h:57
auto ContactTrianglesOf(IndexType o) const
Get triangles of body o
Definition MultibodyTetrahedralMeshSystem.h:75
Eigen::Index NumContactTriangles() const
Get the number of contact triangles.
Definition MultibodyTetrahedralMeshSystem.h:146
auto ContactVerticesRangeFor(IndexType o) const
Get the range of contact vertices for body o
Definition MultibodyTetrahedralMeshSystem.h:158
Eigen::Vector< TIndex, Eigen::Dynamic > FP
Definition MultibodyTetrahedralMeshSystem.h:220
auto TetrahedraOf(IndexType o, Eigen::DenseBase< TDerivedT > const &T) const
Get tetrahedra of body o
Definition MultibodyTetrahedralMeshSystem.h:84
TIndex IndexType
Definition MultibodyTetrahedralMeshSystem.h:27
Eigen::Index NumContactEdges() const
Get the number of contact edges.
Definition MultibodyTetrahedralMeshSystem.h:141
Eigen::Vector< TIndex, Eigen::Dynamic > CC
Definition MultibodyTetrahedralMeshSystem.h:225
auto BodyOfContactVertex(IndexType v) const
Get the body associated with vertex v
Definition MultibodyTetrahedralMeshSystem.h:107
auto ContactVertexPositionsOf(IndexType o, Eigen::DenseBase< TDerivedX > const &X) const
Get vertex positions of body o
Definition MultibodyTetrahedralMeshSystem.h:97
auto BodyOfContactTriangle(IndexType f) const
Get the body associated with triangle f
Definition MultibodyTetrahedralMeshSystem.h:119
auto ContactEdgesOf(IndexType o) const
Get edges of body o
Definition MultibodyTetrahedralMeshSystem.h:69
Eigen::Vector< TIndex, Eigen::Dynamic > EP
Definition MultibodyTetrahedralMeshSystem.h:218
auto TetrahedraRangeFor(IndexType o) const
Get the range of tetrahedra for body o
Definition MultibodyTetrahedralMeshSystem.h:196
Eigen::Index NumContactVertices() const
Get the number of contact vertices.
Definition MultibodyTetrahedralMeshSystem.h:136
auto ContactTrianglesRangeFor(IndexType o) const
Get the range of contact triangles for body o
Definition MultibodyTetrahedralMeshSystem.h:172
Eigen::Index NumTetrahedra() const
Get the number of tetrahedra.
Definition MultibodyTetrahedralMeshSystem.h:151
void SetMaterial(TScalar muS, TScalar muD)
Set the friction coefficients for all bodies.
Definition MultibodyTetrahedralMeshSystem.h:368
Eigen::Matrix< TIndex, 2, Eigen::Dynamic > E
Definition MultibodyTetrahedralMeshSystem.h:211
void Serialize(io::Archive &archive) const
Serialize the multibody tetrahedral mesh system to an HDF5 archive.
Definition MultibodyTetrahedralMeshSystem.h:374
auto BodyOfTetrahedron(IndexType t, Eigen::DenseBase< TDerivedT > const &T) const
Get the body associated with tetrahedron t
Definition MultibodyTetrahedralMeshSystem.h:127
void SetMaterial(IndexType o, TScalar muS, TScalar muD)
Set the friction coefficients for body o
Definition MultibodyTetrahedralMeshSystem.h:361
Scalar ScalarType
Definition MultibodyTetrahedralMeshSystem.h:28
void Deserialize(io::Archive &archive)
Deserialize the multibody tetrahedral mesh system from an HDF5 archive.
Definition MultibodyTetrahedralMeshSystem.h:389
Eigen::Matrix< TIndex, 3, Eigen::Dynamic > F
Definition MultibodyTetrahedralMeshSystem.h:213
Eigen::Vector< TIndex, Eigen::Dynamic > VP
Definition MultibodyTetrahedralMeshSystem.h:216
Eigen::Vector< TIndex, Eigen::Dynamic > V
Definition MultibodyTetrahedralMeshSystem.h:209
MultibodyTetrahedralMeshSystem(Eigen::Ref< Eigen::Matrix< TScalar, 3, Eigen::Dynamic > > X, Eigen::Ref< Eigen::Matrix< IndexType, 4, Eigen::Dynamic > > T)
Construct a new Multibody Tetrahedral Mesh System object.
Definition MultibodyTetrahedralMeshSystem.h:234
Eigen::Vector< TIndex, Eigen::Dynamic > TP
Definition MultibodyTetrahedralMeshSystem.h:221
Eigen::Matrix< Scalar, 2, Eigen::Dynamic > muF
Definition MultibodyTetrahedralMeshSystem.h:228
auto BodyOfContactEdge(IndexType e) const
Get the body associated with edge e
Definition MultibodyTetrahedralMeshSystem.h:113
auto ContactVerticesOf(IndexType o) const
Get vertices of body o
Definition MultibodyTetrahedralMeshSystem.h:63
auto ContactEdgesRangeFor(IndexType o) const
Get the range of contact edges for body o
Definition MultibodyTetrahedralMeshSystem.h:165