10 #include "imstkVecDataArray.h" 12 #include <unordered_map> 21 copyAndAllocate(
const std::shared_ptr<T>& src, std::shared_ptr<T>& dest)
27 dest = std::make_shared<T>();
74 copyAndAllocate(src.prevVertices, prevVertices);
75 copyAndAllocate(src.vertices, vertices);
76 copyAndAllocate(src.velocities, velocities);
77 copyAndAllocate(src.masses, masses);
78 copyAndAllocate(src.invMasses, invMasses);
80 bodyType = src.bodyType;
83 copyAndAllocate(src.prevOrientations, prevOrientations);
84 copyAndAllocate(src.orientations, orientations);
85 copyAndAllocate(src.angularVelocities, angularVelocities);
86 copyAndAllocate(src.inertias, inertias);
87 copyAndAllocate(src.invInertias, invInertias);
93 externalForce = src.externalForce;
94 externalTorque = src.externalTorque;
102 return (bodyType == Type::DEFORMABLE_ORIENTED || bodyType == Type::RIGID);
105 void setRigid(
const Vec3d& pos,
106 const double mass = 1.0,
107 const Quatd& orientation = Quatd::Identity(),
108 const Mat3d& inertia = Mat3d::Identity())
110 bodyType = PbdBody::Type::RIGID;
112 if (vertices ==
nullptr)
114 vertices = std::make_shared<VecDataArray<double, 3>>();
116 * vertices = { pos };
120 if (orientations ==
nullptr)
122 orientations = std::make_shared<StdVectorOfQuatd>();
124 * orientations = { orientation };
126 if (inertias ==
nullptr)
128 inertias = std::make_shared<StdVectorOfMat3d>();
130 * inertias = { inertia };
133 void setRigidVelocity(
const Vec3d& velocity,
134 const Vec3d& angularVelocity = Vec3d::Zero())
136 bodyType = PbdBody::Type::RIGID;
138 if (velocities ==
nullptr)
140 velocities = std::make_shared<VecDataArray<double, 3>>();
142 * velocities = { velocity };
144 if (angularVelocities ==
nullptr)
146 angularVelocities = std::make_shared<VecDataArray<double, 3>>();
148 * angularVelocities = { angularVelocity };
151 Vec3d getRigidPosition()
153 CHECK(bodyType == Type::RIGID) <<
"Body is not a rigid.";
154 return vertices->at(0);
157 Quatd getRigidOrientation()
159 CHECK(bodyType == Type::RIGID) <<
"Body is not a rigid.";
160 return orientations->at(0);
169 CHECK(bodyType == Type::RIGID) <<
"Body is not a rigid.";
170 (*vertices)[0] = (*prevVertices)[0] = pos;
171 (*orientations)[0] = (*prevOrientations)[0] = orientation;
180 CHECK(bodyType == Type::RIGID) <<
"Body is not a rigid.";
181 (*velocities)[0] = velocity;
182 (*angularVelocities)[0] = angularVelocity;
187 Type bodyType = Type::DEFORMABLE;
189 std::shared_ptr<VecDataArray<double, 3>> prevVertices;
190 std::shared_ptr<VecDataArray<double, 3>> vertices;
192 std::shared_ptr<VecDataArray<double, 3>> velocities;
194 std::shared_ptr<DataArray<double>> masses;
195 std::shared_ptr<DataArray<double>> invMasses;
197 std::shared_ptr<StdVectorOfQuatd> prevOrientations;
198 std::shared_ptr<StdVectorOfQuatd> orientations;
200 std::shared_ptr<VecDataArray<double, 3>> angularVelocities;
202 std::shared_ptr<StdVectorOfMat3d> inertias;
203 std::shared_ptr<StdVectorOfMat3d> invInertias;
213 Vec3d externalForce = Vec3d::Zero();
214 Vec3d externalTorque = Vec3d::Zero();
237 m_bodies.resize(src.m_bodies.size());
238 for (
size_t i = 0; i < m_bodies.size(); i++)
240 if (m_bodies[i] ==
nullptr)
242 m_bodies[i] = std::make_shared<PbdBody>();
244 m_bodies[i]->deepCopy(*src.m_bodies[i]);
248 inline Vec3d& getPosition(
const std::pair<int, int>& bodyParticleId)
const {
return (*m_bodies[bodyParticleId.first]->vertices)[bodyParticleId.second]; }
249 inline Vec3d& getVelocity(
const std::pair<int, int>& bodyParticleId)
const {
return (*m_bodies[bodyParticleId.first]->velocities)[bodyParticleId.second]; }
250 inline Quatd& getOrientation(
const std::pair<int, int>& bodyParticleId)
const {
return (*m_bodies[bodyParticleId.first]->orientations)[bodyParticleId.second]; }
251 inline Vec3d& getAngularVelocity(
const std::pair<int, int>& bodyParticleId)
const {
return (*m_bodies[bodyParticleId.first]->angularVelocities)[bodyParticleId.second]; }
253 inline double getInvMass(
const std::pair<int, int>& bodyParticleId)
const {
return (*m_bodies[bodyParticleId.first]->invMasses)[bodyParticleId.second]; }
254 inline Mat3d& getInvInertia(
const std::pair<int, int>& bodyParticleId)
const {
return (*m_bodies[bodyParticleId.first]->invInertias)[bodyParticleId.second]; }
256 inline PbdBody::Type getBodyType(
const std::pair<int, int>& bodyParticleId)
const {
return m_bodies[bodyParticleId.first]->bodyType; }
258 std::vector<std::shared_ptr<PbdBody>> m_bodies;
Represents a pbd body in the model. This is a data only object. It does no function. PbdBody can be of different types. The types effect what properties it has.
int bodyHandle
Id in the system.
void deepCopy(const PbdBody &src)
Deep copy from src, copying dynamic allocations by value.
std::unordered_map< int, double > fixedNodeInvMass
Map for archiving fixed nodes' mass.
std::unordered_map< int, std::vector< std::shared_ptr< PbdConstraint > > > cellConstraintMap
bool bodyGravity
Switch to activate/deactivate gravity for this Pbd Body. 1 is gravity on, 0 is gravity off...
std::vector< int > fixedNodeIds
Nodal/vertex IDs of the nodes that are fixed.
void overrideRigidPositionAndOrientation(const Vec3d &pos, const Quatd &orientation)
Override the current position and orientation this can be used to handle pauses, switching of tools o...
double uniformMassValue
Mass properties, not used if per vertex masses are given in geometry attributes.
Provides interface for accessing particles from a 2d array of PbdBody,Particles.
bool getOriented() const
The body should have orientations if its DEFORMABLE_ORIENTED or RIGID.
void overrideLinearAndAngularVelocity(const Vec3d &velocity, const Vec3d &angularVelocity)
Override the current linear and angular velocity this can be used to handle pauses, switching of tools or other action that need to transform the body without running through the physics. Can only be used on Rigid bodies.