1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2013 Erwin Coumans  http://bulletphysics.org
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 #ifndef BT_MULTIBODY_CONSTRAINT_H
17 #define BT_MULTIBODY_CONSTRAINT_H
18 
19 #include "LinearMath/btScalar.h"
20 #include "LinearMath/btAlignedObjectArray.h"
21 #include "btMultiBody.h"
22 
23 
24 //Don't change any of the existing enum values, so add enum types at the end for serialization compatibility
25 enum btTypedMultiBodyConstraintType
26 {
27 	MULTIBODY_CONSTRAINT_LIMIT=3,
28 	MULTIBODY_CONSTRAINT_1DOF_JOINT_MOTOR,
29 	MULTIBODY_CONSTRAINT_GEAR,
30 	MULTIBODY_CONSTRAINT_POINT_TO_POINT,
31 	MULTIBODY_CONSTRAINT_SLIDER,
32 	MULTIBODY_CONSTRAINT_SPHERICAL_MOTOR,
33 	MULTIBODY_CONSTRAINT_FIXED,
34 
35 	MAX_MULTIBODY_CONSTRAINT_TYPE,
36 };
37 
38 class btMultiBody;
39 struct btSolverInfo;
40 
41 #include "btMultiBodySolverConstraint.h"
42 
43 struct btMultiBodyJacobianData
44 {
45 	btAlignedObjectArray<btScalar> m_jacobians;
46 	btAlignedObjectArray<btScalar> m_deltaVelocitiesUnitImpulse;  //holds the joint-space response of the corresp. tree to the test impulse in each constraint space dimension
47 	btAlignedObjectArray<btScalar> m_deltaVelocities;             //holds joint-space vectors of all the constrained trees accumulating the effect of corrective impulses applied in SI
48 	btAlignedObjectArray<btScalar> scratch_r;
49 	btAlignedObjectArray<btVector3> scratch_v;
50 	btAlignedObjectArray<btMatrix3x3> scratch_m;
51 	btAlignedObjectArray<btSolverBody>* m_solverBodyPool;
52 	int m_fixedBodyId;
53 };
54 
ATTRIBUTE_ALIGNED16(class)55 ATTRIBUTE_ALIGNED16(class)
56 btMultiBodyConstraint
57 {
58 protected:
59 	btMultiBody* m_bodyA;
60 	btMultiBody* m_bodyB;
61 	int m_linkA;
62 	int m_linkB;
63 
64 	int m_type; //btTypedMultiBodyConstraintType
65 
66 	int m_numRows;
67 	int m_jacSizeA;
68 	int m_jacSizeBoth;
69 	int m_posOffset;
70 
71 	bool m_isUnilateral;
72 	int m_numDofsFinalized;
73 	btScalar m_maxAppliedImpulse;
74 
75 	// warning: the data block lay out is not consistent for all constraints
76 	// data block laid out as follows:
77 	// cached impulses. (one per row.)
78 	// jacobians. (interleaved, row1 body1 then row1 body2 then row2 body 1 etc)
79 	// positions. (one per row.)
80 	btAlignedObjectArray<btScalar> m_data;
81 
82 	void applyDeltaVee(btMultiBodyJacobianData & data, btScalar * delta_vee, btScalar impulse, int velocityIndex, int ndof);
83 
84 	btScalar fillMultiBodyConstraint(btMultiBodySolverConstraint & solverConstraint,
85 									 btMultiBodyJacobianData & data,
86 									 btScalar * jacOrgA, btScalar * jacOrgB,
87 									 const btVector3& constraintNormalAng,
88 
89 									 const btVector3& constraintNormalLin,
90 									 const btVector3& posAworld, const btVector3& posBworld,
91 									 btScalar posError,
92 									 const btContactSolverInfo& infoGlobal,
93 									 btScalar lowerLimit, btScalar upperLimit,
94 									 bool angConstraint = false,
95 
96 									 btScalar relaxation = 1.f,
97 									 bool isFriction = false, btScalar desiredVelocity = 0, btScalar cfmSlip = 0, btScalar damping = 1.0);
98 
99 public:
100 	BT_DECLARE_ALIGNED_ALLOCATOR();
101 
102 	btMultiBodyConstraint(btMultiBody * bodyA, btMultiBody * bodyB, int linkA, int linkB, int numRows, bool isUnilateral, int type);
103 	virtual ~btMultiBodyConstraint();
104 
105 	void updateJacobianSizes();
106 	void allocateJacobiansMultiDof();
107 
108 	int getConstraintType() const
109 	{
110 		return m_type;
111 	}
112 	//many constraints have setFrameInB/setPivotInB. Will use 'getConstraintType' later.
113 	virtual void setFrameInB(const btMatrix3x3& frameInB) {}
114 	virtual void setPivotInB(const btVector3& pivotInB) {}
115 
116 	virtual void finalizeMultiDof() = 0;
117 
118 	virtual int getIslandIdA() const = 0;
119 	virtual int getIslandIdB() const = 0;
120 
121 	virtual void createConstraintRows(btMultiBodyConstraintArray & constraintRows,
122 									  btMultiBodyJacobianData & data,
123 									  const btContactSolverInfo& infoGlobal) = 0;
124 
125 	int getNumRows() const
126 	{
127 		return m_numRows;
128 	}
129 
130 	btMultiBody* getMultiBodyA()
131 	{
132 		return m_bodyA;
133 	}
134 	btMultiBody* getMultiBodyB()
135 	{
136 		return m_bodyB;
137 	}
138 
139 	int getLinkA() const
140 	{
141 		return m_linkA;
142 	}
143 	int getLinkB() const
144 	{
145 		return m_linkB;
146 	}
147 	void internalSetAppliedImpulse(int dof, btScalar appliedImpulse)
148 	{
149 		btAssert(dof >= 0);
150 		btAssert(dof < getNumRows());
151 		m_data[dof] = appliedImpulse;
152 	}
153 
154 	btScalar getAppliedImpulse(int dof)
155 	{
156 		btAssert(dof >= 0);
157 		btAssert(dof < getNumRows());
158 		return m_data[dof];
159 	}
160 	// current constraint position
161 	// constraint is pos >= 0 for unilateral, or pos = 0 for bilateral
162 	// NOTE: ignored position for friction rows.
163 	btScalar getPosition(int row) const
164 	{
165 		return m_data[m_posOffset + row];
166 	}
167 
168 	void setPosition(int row, btScalar pos)
169 	{
170 		m_data[m_posOffset + row] = pos;
171 	}
172 
173 	bool isUnilateral() const
174 	{
175 		return m_isUnilateral;
176 	}
177 
178 	// jacobian blocks.
179 	// each of size 6 + num_links. (jacobian2 is null if no body2.)
180 	// format: 3 'omega' coefficients, 3 'v' coefficients, then the 'qdot' coefficients.
181 	btScalar* jacobianA(int row)
182 	{
183 		return &m_data[m_numRows + row * m_jacSizeBoth];
184 	}
185 	const btScalar* jacobianA(int row) const
186 	{
187 		return &m_data[m_numRows + (row * m_jacSizeBoth)];
188 	}
189 	btScalar* jacobianB(int row)
190 	{
191 		return &m_data[m_numRows + (row * m_jacSizeBoth) + m_jacSizeA];
192 	}
193 	const btScalar* jacobianB(int row) const
194 	{
195 		return &m_data[m_numRows + (row * m_jacSizeBoth) + m_jacSizeA];
196 	}
197 
198 	btScalar getMaxAppliedImpulse() const
199 	{
200 		return m_maxAppliedImpulse;
201 	}
202 	void setMaxAppliedImpulse(btScalar maxImp)
203 	{
204 		m_maxAppliedImpulse = maxImp;
205 	}
206 
207 	virtual void debugDraw(class btIDebugDraw * drawer) = 0;
208 
209 	virtual void setGearRatio(btScalar ratio) {}
210 	virtual void setGearAuxLink(int gearAuxLink) {}
211 	virtual void setRelativePositionTarget(btScalar relPosTarget) {}
212 	virtual void setErp(btScalar erp) {}
213 };
214 
215 #endif  //BT_MULTIBODY_CONSTRAINT_H
216