17#ifndef AGX_CONSTRAINT_IMPLEMENTATION_H
18#define AGX_CONSTRAINT_IMPLEMENTATION_H
34# pragma warning( disable : 4251 )
37#define DEFAULT_COMPLIANCE 1.0E-8
39#define CONSTRAINT_SUCCESS( success ) \
41 LOGGER_WARNING() << "Unable to create constraint. Constraint cleared." << std::endl << LOGGER_END(); \
43 m_localState.remove( VALID ); \
47#define CONSTRAINT_SUCCESS_RETURN_BOOL( success ) \
49 LOGGER_WARNING() << "Unable to create constraint. Constraint cleared." << std::endl << LOGGER_END(); \
51 m_localState.remove( VALID ); \
58 class ElementaryConstraint;
60 class RegularizationParameters;
61 class SparseRangeReal;
62 class LSquareComplianceMatrix;
63 struct NlmcpCallbackSolverData;
73 : m_numBlocks( 0 ), m_numRows( 0 ), m_numDynBlocks( 0 ) {}
105 DEFAULT_BINARY_TAG = NONE,
106 DEFAULT_MANY_BODY_TAG = MANY_BODY
222 UInt m_numRecurrentMergedBodies;
229 #if !defined(SWIGPYTHON)
344 #if !defined(SWIGPYTHON)
421 int8_t* indexSetState,
845 template<
typename T >
846 T* findSecondaryConstraintGivenType(
const agx::UInt count = 0 )
const;
988 SUPPORTS_COMPLIANCE_MATRIX = 1 << 2
1226 bool bodiesValid()
const;
1231 template<
typename T >
1237 template<
typename T >
1238 const T* getAttachment(
agx::UInt index )
const;
1255 return m_numDynamicBodies;
1260 return m_numDynamicBodies;
1265 return m_numNonStaticBodies;
1270 return m_numNonStaticBodies;
1275 return m_originalInfo;
1280 return m_solverInfo;
1285 return m_permutation;
1290 return m_permutation;
1295 return m_bodyIndices;
1300 return m_bodyIndices;
1307#if !defined(SWIGPYTHON)
1318 return m_info.numDynamicBodies();
1323 return m_info.numNonStaticBodies();
1346 :
BodyView( state.getSolverInfo(), state.getSolverBodies() ), m_state( state )
1351 :
BodyView( other.m_state.getSolverInfo(), other.m_state.getSolverBodies() ), m_state( other.m_state )
1367 agxAssert( i < m_state.getPermutation().size() );
1368 UInt permutedIndex = (
UInt)m_state.getPermutation()[ i ];
1369 agxAssert( permutedIndex < getNumBodies() );
1370 return getBodyAt( permutedIndex );
1375 agxAssert( i < m_state.getPermutation().size() );
1376 UInt permutedIndex = (
UInt)m_state.getPermutation()[ i ];
1377 agxAssert( permutedIndex < getNumBodies() );
1378 return getBodyAt( permutedIndex );
1381 template<
typename T >
1384 UInt currentNumber = 0;
1386 T* ec =
dynamic_cast< T*
>(
m_sc[ i ].get() );
1387 if ( ec !=
nullptr && currentNumber == count )
1389 currentNumber += ec !=
nullptr;
1404 template<
typename T >
1407 return m_attachmentPair[ index ] !=
nullptr ? m_attachmentPair[ index ]->as< T >() :
nullptr;
1410 template<
typename T >
1413 return m_attachmentPair[ index ] !=
nullptr ? m_attachmentPair[ index ]->as< T >() :
nullptr;
1418 return m_attachmentPair[ 0 ] !=
nullptr && !m_attachmentPair[ 0 ]->objectDeleted() &&
1419 m_attachmentPair[ 1 ] !=
nullptr && !m_attachmentPair[ 1 ]->objectDeleted();
1427 return std::atan2( c * b, c * a );
1442# pragma warning(pop)
#define AGXPHYSICS_EXPORT
Interface class used by internal objects running in parallel.
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Class for writing serialized data in binary format to a stream.
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Abstract base class for storing/restoring a line/drums with version control.
Constraint attachment pair class.
Constraint attachment base class for any type of frame.
void setNumRows(BlockType numRows)
BlockType kinBlocks() const
BlockType getNumBlocks() const
BlockType getNumRows() const
BlockType dynBlocks() const
void setNumBlocks(BlockType numBlocks)
Interface to the bodies, either user or solver, since these bodies can differ when e....
const ConstraintRigidBodyContainer & getBodies() const
BodyView(const ConstrainedBodiesState::BodiesInfo &info, const ConstraintRigidBodyContainer &bodies)
Internal, use ConstriantImplementation::getUserView().
agx::UInt getNumBodies() const
agx::UInt getNumNonStaticBodies() const
agx::RigidBody * getBodyAt(agx::UInt i)
agx::UInt getNumDynamicBodies() const
Info with number of dynamic and non-static body counters.
agx::UInt & numDynamicBodies()
agx::UInt & numNonStaticBodies()
Object that holds the state seen from the user and from the solver.
ConstrainedBodiesState()
Default constructor.
const agx::ConstraintRigidBodyContainer & getSolverBodies() const
agx::Bool finalize(agx::Bool valid)
Finalize given state of constraint.
void reset(agx::UInt numBodies)
Resets all data and allocates for a maximum of numBodies.
agx::Bool increment(agx::RigidBody *rb)
Increments data given a rigid body.
const agx::Int32Vector & getPermutation() const
const agx::Int32Vector & getBodyIndices() const
const BodiesInfo & getOriginalInfo() const
const BodiesInfo & getSolverInfo() const
agx::UInt getNumRecurrentMergedBodies() const
Counter that increments each time a merged body already has been registered and comes back.
const ConstrainedBodiesState & getState() const
SolverBodyView(ConstrainedBodiesState &state)
Internal, use ConstriantImplementation::getSolverView().
agx::RigidBody * getPermutedBodyAt(agx::UInt i)
virtual bool getEnable() const
void reset()
Reset the solution vectors of the primary and secondary elementary constraints in the implementation.
virtual agx::ElementaryConstraint * getElementaryConstraint(const agx::String &name) const
Constraint::SolveType m_solveType
Only for supported solvers.
virtual int getViolation(agx::Real *g, int row)
Calculates and pushes the constraint values into array given current row.
virtual bool addElementaryConstraint(const agx::String &name, agx::ElementaryConstraint *elementaryConstraint)
Add elementary constraint (like Spherical, Dot1, Dot2 etc) given name.
agx::ElementaryConstraint::RefContainer & getElementaryConstraints()
virtual agx::Real getAngle(agx::UInt=0) const
If the constraint supports angle calculations and 'dof' is in range, this method returns the angle of...
bool getEnableLinearization() const
virtual void postSystemCallback(DynamicsSystem *system)
Last in the dynamics system step forward loop, this constraint gets a call with the main system.
bool m_enable
User controlled enable flag, constraint ignored by solver if false.
BlockStructure m_blockStructure
The block structure; number of block, rows, dynamic blocks etc.
void setSupportsComplianceMatrix(Bool supportsComplianceMatrix)
Enable/disable compliance matrix callbacks from the direct solver where read/write access to the comp...
virtual void setEnableComputeForces(agx::Bool enable)
Enable (or disable) computation of the forces applied to the dynamic bodies in this constraint.
virtual agx::ElementaryConstraint * getSecondaryConstraint(const agx::String &name) const
virtual bool removeSecondaryConstraint(agx::ElementaryConstraint *secondaryConstraint)
Remove secondary constraint.
agx::UInt calculateNumActiveRows() const
Calculates the current number of active rows, including both elementary and secondary constraints.
void sort()
This method will sort this constraint by global indexing of the bodies.
virtual agx::Attachment * userAPIgetAttachment(const agx::RigidBody *) const
const agx::ElementaryConstraint::RefContainer & getSecondaryConstraints() const
void * customData
Custom data for the solvers to store data into.
virtual bool removeSecondaryConstraint(const agx::String &name)
Remove secondary constraint.
virtual void addNotification()
Callback when this constraint is added to Simulation.
virtual ~ConstraintImplementation()
Destructor.
int m_tag
Tag for this constraint.
ElementaryConstraint::RefContainer m_sc
Vector of secondary constraints.
const BlockStructure & getBlockStructure() const
agx::FrictionController * getFrictionController(agx::UInt number=0) const
virtual size_t updateJacobian(agx::Jacobian6DOFElement *jacobians)
Updated storing of Jacobian matrices.
virtual void getBounds(agx::RangeReal *bounds, agx::Real dt) const
Calculates and pushes the bounds into a sparse range data structure.
UInt m_numRows
Number of rows used by this constraint.
virtual agx::ElementaryConstraint * getElementaryConstraint(const agx::UInt index) const
const agx::ElementaryConstraint::RefContainer & getElementaryConstraints() const
virtual CALLABLE_IGNORE void updateComplianceMatrix(LSquareComplianceMatrix matrix) const
Callback with read/write access to the compliance matrix of this constraint in the direct solver.
void incrementConstraintCount(RigidBody *rb) const
Increment the number of constraints connection to rb.
RealVector calculateCurrentViolation() const
Calculates the current violation of the ordinary degrees of freedom of this constraint,...
agx::Motor1D * getMotor1D(agx::UInt number=0) const
virtual agx::Attachment * userAPIgetAttachment(agx::UInt) const
void onDirectComplianceMatrix(Real *data, unsigned int ld) const
Internal callback from the direct solver with perturbation matrix data.
void setSimulation(agxSDK::Simulation *simulation)
Set the Simulation this constraint belongs to.
void userAPIsetDamping(agx::Real damping, int dof)
Set the damping for constraint row i of this constraint.
void postSolveIndexSetCallback(const int8_t *state)
Callback after the solver is done about the state of each equation in the constraint.
ConstraintImplPtrVector m_subConstraints
Vector with sub-constraints.
virtual void removeNotification()
Callback when this constraint is removed from Simulation.
ConstraintRigidBodyContainer m_bodies
Vector with observer pointers to bodies.
virtual void postSolveCallback(const agx::Jacobian6DOFElement *jacobians, const agx::Real *solution, agx::Real dt)
Peak at the solution after the solver is done.
void setSolveType(Constraint::SolveType type)
Set solve type, either DIRECT, ITERATIVE or DIRECT_AND_ITERATIVE.
virtual agx::Bool getLastForce(agx::UInt bodyIndex, agx::Vec3 &retForce, agx::Vec3 &retTorque, agx::Bool giveForceAtCm=false) const
If 'compute forces' is enabled, returns the last force and torque applied by this constraint on the b...
virtual bool removeElementaryConstraint(agx::ElementaryConstraint *elementaryConstraint)
Remove elementary constraint.
void setBlockRowIndex(agx::UInt blockRowIndex)
Sets the global start index for this constraint in the global, non-permuted matrix.
bool getSolveIterative() const
virtual agx::Real getCurrentSpeed(agx::UInt=0) const
If this method is implemented, a call to this method will return the current speed of the angle.
BodyView getUserView() const
Bodies as viewed from the user.
Constraint::SolveType getSolveType() const
virtual void setEnable(bool enable)
Sets the enable flag.
ConstraintImplPtrVector & getSubConstraints()
void userAPIsetForceRange(agx::RangeReal forceRange, agx::Int dof)
Assign force range, of an elementary constraint, for a given DOF.
virtual agx::Bool getLastForce(const agx::RigidBody *rb, agx::Vec3 &retForce, agx::Vec3 &retTorque, agx::Bool giveForceAtCm=false) const
If 'compute forces' is enabled, returns the last force and torque applied by this constraint on the b...
bool getSolveBoth() const
agx::Real userAPIgetCompliance(agx::UInt dof) const
ConstraintImplementation()
Default constructor.
agx::ElementaryConstraint::RefContainer & getSecondaryConstraints()
agx::Real userAPIgetCurrentForce(agx::UInt dof) const
Consider using getLastForce instead.
agx::Lock1D * getLock1D(agx::UInt number=0) const
SolverBodyView getSolverView() const
Bodies as viewed from the solver.
virtual void preSystemCallback(DynamicsSystem *system)
Before prepare, and before the system writes to the data buffers, this constraint gets a call with th...
virtual bool removeElementaryConstraint(const agx::String &name)
Remove elementary constraint.
UInt m_blockRowIndex
Start block row for this constraint in the global matrix.
virtual void getEpsilon(agx::Real *epsilon, agx::Real h)
Writes regularization parameters for all active rows into epsilon.
virtual void addNlCallbacks(agx::SparseRangeReal &sparseRangeReal, const NlmcpCallbackSolverData &solverData) const
Interface for non-linear callback from solver.
ConstrainedBodiesState m_constrainedBodiesState
State of the included bodies, updated in updateValid.
virtual agx::ElementaryConstraint * getSecondaryConstraint(const agx::UInt index) const
agx::RegularizationParameters * userAPIgetRegularizationParameters(agx::UInt i)
Get the regularization parameter i, i.e., the regularization parameter for equation i which is constr...
bool m_enableLinearization
When necessary, tweak the values sent to the solver to make violation depend linearly on force or tor...
virtual void getParameters(agx::Real *violation, agx::Real *epsilon, agx::Real *damping, bool *holonomic, agx::Real *velocity, agx::RangeReal *bounds, int8_t *indexSetState, agx::Real h, agx::Real minPerturbation=agx::Real(1E-10))
Writes solver parameters such as damping and holonomic.
virtual int getConstraintVelocity(agx::Real *v, int row)
Calculates and pushes the constraint velocities into array given current row.
const agx::RegularizationParameters * userAPIgetRegularizationParameters(agx::UInt i) const
virtual agx::Bool getEnableComputeForces() const
virtual void prepare()
Prepares this constraint.
virtual agx::Bool getLastLocalForce(agx::UInt bodyIndex, agx::Vec3 &retForce, agx::Vec3 &retTorque, agx::Bool giveForceAtCm=false) const
If 'compute forces' is enabled, returns the last force and torque applied by this constraint on the b...
virtual bool updateValid()
Updates the valid conditions for this constraint.
agx::BitState< State, Int32 > LocalState
const ConstraintImplPtrVector & getSubConstraints() const
agx::Real userAPIgetDamping(agx::UInt dof) const
void userAPIsetCompliance(agx::Real compliance, int dof)
Set the compliance for constraint row i of this constraint.
virtual bool addSecondaryConstraint(const agx::String &name, agx::ElementaryConstraint *secondaryConstraint)
Add secondary constraint (like motor, range and/or lock etc) given name.
virtual bool rebind()
The general version of rebind takes the first attachment frame and moves the other attachment frame s...
agx::Real calculateViolationVectorAverageSize()
Gets the average size of the elements in getViolation() by calculating the magnitude and dividing by ...
virtual agx::Bool getLastLocalForce(const agx::RigidBody *rb, agx::Vec3 &retForce, agx::Vec3 &retTorque, agx::Bool giveForceAtCm=false) const
If 'compute forces' is enabled, returns the last force and torque applied by this constraint on the b...
agx::UInt getBlockRowIndex() const
agx::Range1D * getRange1D(agx::UInt number=0) const
virtual agx::AttachmentPair * getAttachmentPair() const
ElementaryConstraint::RefContainer m_ec
Vector of elementary constraints.
agx::RangeReal userAPIgetForceRange(agx::UInt dof) const
Get the force range for DOF dof.
LocalState m_localState
Current local state, updated all the time, not serialized.
void setEnableLinearization(bool enable)
Control whether or not linearization should be performed on the ElementaryConstraints that require it...
T * findSecondaryConstraintGivenType(const agx::UInt count=0) const
agx::UInt getJacobianRowUsage() const
The base class for a constraint.
SolveType
Specifies in what solvers the constraint will be solved.
The complete physical system with bodies, interactions, data layout, time stepper,...
Elementary constraint base class with interface and global constraint functionality.
The object defining a frame of reference and providing transformations operations.
Translational or rotational friction controller for Hinge, Prismatic and CylindricalJoint.
virtual void postSolveCallback(const agx::Jacobian6DOFElement *jacobians, const agx::Real *solution, agx::Real dt) override
Peak at the solution after the solver is done.
bool setupOneOrTwoBodySystem(RigidBodyAttachment *a1, RigidBodyAttachment *a2)
virtual void restore(agxStream::InputArchive &in) override
HighLevelConstraintImplementation()
virtual agx::Bool getLastForce(const RigidBody *rb, Vec3 &retForce, Vec3 &retTorque, agx::Bool giveForceAtCm=false) const override
If 'compute forces' is enabled, returns the last force and torque applied by this constraint on the b...
bool construct(RigidBody *rb1, Frame *rb1AttachmentFrame, RigidBody *rb2, Frame *rb2AttachmentFrame, CreateElementaryConstraintsFunction=nullptr)
virtual agx::Bool getLastForce(agx::UInt bodyIndex, agx::Vec3 &retForce, agx::Vec3 &retTorque, agx::Bool giveForceAtCm=false) const override
If 'compute forces' is enabled, returns the last force and torque applied by this constraint on the b...
virtual agx::Real getAngle(agx::UInt dof=0) const override
If the constraint supports angle calculations and 'dof' is in range, this method returns the angle of...
virtual size_t updateJacobian(agx::Jacobian6DOFElement *jacobians) override
Updates the Jacobian matrices.
virtual void storeLightData(agxStream::StorageStream &str) const override
virtual agx::Bool computeForces(const agx::Jacobian6DOFElement *jacobians, agx::Vec3Vector &result) const
Computes forces on rb1 and rb2 given this constraint has been solved and all the elementary constrain...
AttachmentPair m_attachmentPair
virtual bool rebind() override
The general version of rebind takes the first attachment frame and moves the other attachment frame s...
virtual void computeForces(const agx::Jacobian6DOFElement *jacobians)
Computes the forces, if valid and enabled.
virtual agx::Attachment * userAPIgetAttachment(agx::UInt index) const override
T * getAttachment(agx::UInt index)
virtual void restoreLightData(agxStream::StorageStream &str) override
virtual void prepare() override
Prepares this constraint.
virtual agx::Bool getLastLocalForce(agx::UInt bodyIndex, agx::Vec3 &retForce, agx::Vec3 &retTorque, agx::Bool giveForceAtCm=false) const override
If 'compute forces' is enabled, returns the last force and torque applied by this constraint on the b...
virtual agx::Attachment * userAPIgetAttachment(const agx::RigidBody *rb) const override
virtual ~HighLevelConstraintImplementation()
virtual agx::Bool getLastLocalForce(const agx::RigidBody *rb, agx::Vec3 &retForce, agx::Vec3 &retTorque, agx::Bool giveForceAtCm=false) const override
If 'compute forces' is enabled, returns the last force and torque applied by this constraint on the b...
virtual agx::AttachmentPair * getAttachmentPair() const override
virtual void store(agxStream::OutputArchive &out) const override
virtual void setEnableComputeForces(agx::Bool enable) override
Enable (or disable) computation of the forces applied to the dynamic bodies in this constraint.
bool validateRigidBodiesAndAttachments(RigidBody *rb1, Frame *rb1AttachmentFrame, RigidBody *rb2, Frame *rb2AttachmentFrame)
virtual bool updateValid() override
Updates the valid conditions for this constraint.
AGXSTREAM_DECLARE_ABSTRACT_SERIALIZABLE(agx::HighLevelConstraintImplementation)
virtual agx::Bool getEnableComputeForces() const override
Interface to the lower triangle of the compliance matrix block in the system matrix.
Elementary secondary constraint to keep constraint angle at a given target position.
Pointer to a entity instance of type Physics.ConstraintForces.
Elementary secondary constraint to keep constraint angle within two given values.
Base class providing referencing counted objects.
All ghost variables have compliance and damping attributes.
Constraint attachment class for agx::RigidBody.
The rigid body class, combining a geometric model and a frame of reference.
Implementation of constraint angle ABOUT an axis.
Implementation of constraint angle ALONG an axis.
Essentially a sparse array of bounds.
Elementary secondary constraint to drive something given target speed (translational or rotational).
#define DOXYGEN_END_INTERNAL_BLOCK()
#define DOXYGEN_START_INTERNAL_BLOCK()
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
AGXPHYSICS_EXPORT void addSecondaryConstraints1DOF(agx::HighLevelConstraintImplementation *constraint, agx::Angle *angle)
Add secondary controllers with default name to the constraint.
Vector< RigidBody * > ConstraintRigidBodyContainer
std::pair< Real, Real > RealPair
AGXPHYSICS_EXPORT void addSecondaryConstraints2DOF(agx::HighLevelConstraintImplementation *constraint, agx::SeparationAngle *sepAngle, agx::RotationalAngle *rotAngle)
Add secondary controllers with default name to the constraint.
std::valarray< agx::Real > RealValarray
For this module, vectors are defined using valarray.
Data from the solver the ConstraintNlmcpCallback is part of.