```  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
```
```use na::RealField;

/// Logical information of the geometry of a constraint.
#[derive(Copy, Clone, Debug, Default)]
pub struct ConstraintGeometry<N: RealField> {
/// Index of the first entry of the jacobian of the constraint affecting the first body.
pub j_id1: usize,
/// Index of the first entry of the jacobian of the constraint affecting the second body.
pub j_id2: usize,
/// Index of the first entry of the constraint jacobian multiplied by the inverse mass of the first body.
pub wj_id1: usize,
/// Index of the first entry of the constraint jacobian multiplied by the inverse mass of the second body.
pub wj_id2: usize,
/// Number of degree of freedom of the first body.
pub ndofs1: usize,
/// Number of degree of freedom of the second body.
pub ndofs2: usize,
/// Scaling parameter of the SOR-prox method.
pub r: N,
}

impl<N: RealField> ConstraintGeometry<N> {
/// Create a costraint geometry initialized to zero.
#[inline]
pub fn new() -> Self {
ConstraintGeometry {
j_id1: 0,
j_id2: 0,
wj_id1: 0,
wj_id2: 0,
ndofs1: 0,
ndofs2: 0,
r: N::zero(),
}
}

/// Return `true` if this constraint involve a body with zero degrees of freedom.
#[inline]
pub fn is_ground_constraint(&self) -> bool {
self.ndofs1 == 0 || self.ndofs2 == 0
}
}

/// A unilateral (inequality) consraint.
pub struct UnilateralConstraint<N: RealField, Id> {
/// The impulse applied by this constraint.
pub impulse: N,

/// The scaling parameter of the SOR-prox method.
pub r: N,
/// The target velocity change this constraint must apply.
pub rhs: N,

/// The index of the impulse used for its storage in an impuse cache.
pub impulse_id: Id,

/// The assembly index of the first body.
pub assembly_id1: usize,
/// The assembly index of the second body.
pub assembly_id2: usize,

/// Index of the first entry of the jacobian of the constraint affecting the first body.
pub j_id1: usize,
/// Index of the first entry of the constraint jacobian multiplied by the inverse mass of the second body.
pub j_id2: usize,

/// Index of the first entry of the constraint jacobian multiplied by the inverse mass of the first body.
pub wj_id1: usize,
/// Index of the first entry of the constraint jacobian multiplied by the inverse mass of the second body.
pub wj_id2: usize,

/// Number of degree of freedom of the first body.
pub ndofs1: usize,
/// Number of degree of freedom of the second body.
pub ndofs2: usize,
}

impl<N: RealField, Id> UnilateralConstraint<N, Id> {
/// Create a new unilateral constraint.
#[inline]
pub fn new(
geom: ConstraintGeometry<N>,
assembly_id1: usize,
assembly_id2: usize,
rhs: N,
impulse: N,
impulse_id: Id,
) -> Self {
assert!(geom.ndofs1 != 0 && geom.ndofs2 != 0);
UnilateralConstraint {
impulse,
r: geom.r,
rhs,
impulse_id,
assembly_id1,
assembly_id2,
j_id1: geom.j_id1,
j_id2: geom.j_id2,
wj_id1: geom.wj_id1,
wj_id2: geom.wj_id2,
ndofs1: geom.ndofs1,
ndofs2: geom.ndofs2,
}
}
}

/// A unilateral (inequality) constraint between a dynamic body and one without any degrees of freedom.
pub struct UnilateralGroundConstraint<N: RealField, Id> {
/// The impulse applied by the constraint.
pub impulse: N,

/// The scaling parameter used by the SOR-prox method.
pub r: N,
/// The target velocity change this constraint must apply.
pub rhs: N,

/// The index of the impulse used for its storage in an impuse cache.
pub impulse_id: Id,
/// The assembly index of the dynamic body.
pub assembly_id: usize,
/// Index of the first entry of the jacobian of the constraint affecting the dynamic body.
pub j_id: usize,
/// Index of the first entry of the constraint jacobian multiplied by the inverse mass of the dynamic body.
pub wj_id: usize,
/// Number of degree of freedom of the dynamic body.
pub ndofs: usize,
}

impl<N: RealField, Id> UnilateralGroundConstraint<N, Id> {
/// Create a new unilateral ground constraint.
#[inline]
pub fn new(
geom: ConstraintGeometry<N>,
assembly_id1: usize,
assembly_id2: usize,
rhs: N,
impulse: N,
impulse_id: Id,
) -> Self {
if geom.ndofs1 == 0 {
UnilateralGroundConstraint {
impulse,
r: geom.r,
rhs,
impulse_id,
assembly_id: assembly_id2,
j_id: geom.j_id2,
wj_id: geom.wj_id2,
ndofs: geom.ndofs2,
}
} else {
UnilateralGroundConstraint {
impulse,
r: geom.r,
rhs,
impulse_id,
assembly_id: assembly_id1,
j_id: geom.j_id1,
wj_id: geom.wj_id1,
ndofs: geom.ndofs1,
}
}
}
}

/// Limits of impulse applicable by a bilateral constraint.
#[derive(Copy, Clone, Debug)]
pub enum ImpulseLimits<N: RealField> {
/// Limits that are absolute threshold.
Independent {
/// The lower bound of the impulse.
min: N,
/// The upper bound of the impulse.
max: N,
},
/// Limit proportional to the impulse of another unilateral constraint.
Dependent {
/// Index of the unilateral constraint which this limit depends on.
dependency: usize,
/// The coefficient by which the dependent impulse is multiplied to obtain the impulse limit.
coeff: N,
},
}

/// A bilateral (equality) constraint between two bodies.
pub struct BilateralConstraint<N: RealField, Id> {
/// The impulse applied by this constraint.
pub impulse: N,

/// The scaling parameter of the SOR-prox method.
pub r: N,
/// The target velocity change this constraint must apply.
pub rhs: N,

/// Limits of impulse applicable by this constraint.
pub limits: ImpulseLimits<N>,

/// The index of the impulse used for its storage in an impuse cache.
pub impulse_id: Id,

/// The assembly index of the first body.
pub assembly_id1: usize,
/// The assembly index of the second body.
pub assembly_id2: usize,

/// Index of the first entry of the jacobian of the constraint affecting the first body.
pub j_id1: usize,
/// Index of the first entry of the constraint jacobian multiplied by the inverse mass of the second body.
pub j_id2: usize,

/// Index of the first entry of the constraint jacobian multiplied by the inverse mass of the first body.
pub wj_id1: usize,
/// Index of the first entry of the constraint jacobian multiplied by the inverse mass of the second body.
pub wj_id2: usize,

/// Number of degree of freedom of the first body.
pub ndofs1: usize,
/// Number of degree of freedom of the second body.
pub ndofs2: usize,
}

impl<N: RealField, Id> BilateralConstraint<N, Id> {
/// Create a new bilateral constraint.
#[inline]
pub fn new(
geom: ConstraintGeometry<N>,
assembly_id1: usize,
assembly_id2: usize,
limits: ImpulseLimits<N>,
rhs: N,
impulse: N,
impulse_id: Id,
) -> Self {
assert!(geom.ndofs1 != 0 && geom.ndofs2 != 0);
BilateralConstraint {
impulse,
r: geom.r,
rhs,
limits,
impulse_id,
assembly_id1,
assembly_id2,
j_id1: geom.j_id1,
j_id2: geom.j_id2,
wj_id1: geom.wj_id1,
wj_id2: geom.wj_id2,
ndofs1: geom.ndofs1,
ndofs2: geom.ndofs2,
}
}
}

/// A bilateral (equality) constraint between a dynamic body and one without any degrees of freedom.
pub struct BilateralGroundConstraint<N: RealField, Id> {
/// The impulse applied by the constraint.
pub impulse: N,

/// The scaling parameter used by the SOR-prox method.
pub r: N,
/// The target velocity change this constraint must apply.
pub rhs: N,

/// Limits of impulse applicable by this constraint.
pub limits: ImpulseLimits<N>,

/// The index of the impulse used for its storage in an impuse cache.
pub impulse_id: Id,
/// The assembly index of the dynamic body.
pub assembly_id: usize,
/// Index of the first entry of the jacobian of the constraint affecting the dynamic body.
pub j_id: usize,
/// Index of the first entry of the constraint jacobian multiplied by the inverse mass of the dynamic body.
pub wj_id: usize,
/// Number of degree of freedom of the dynamic body.
pub ndofs: usize,
}

impl<N: RealField, Id> BilateralGroundConstraint<N, Id> {
/// Create a new unilateral ground constraint.
#[inline]
pub fn new(
geom: ConstraintGeometry<N>,
assembly_id1: usize,
assembly_id2: usize,
limits: ImpulseLimits<N>,
rhs: N,
impulse: N,
impulse_id: Id,
) -> Self {
if geom.ndofs1 == 0 {
BilateralGroundConstraint {
impulse,
r: geom.r,
rhs,
limits,
impulse_id,
assembly_id: assembly_id2,
j_id: geom.j_id2,
wj_id: geom.wj_id2,
ndofs: geom.ndofs2,
}
} else {
BilateralGroundConstraint {
impulse,
r: geom.r,
rhs,
limits,
impulse_id,
assembly_id: assembly_id1,
j_id: geom.j_id1,
wj_id: geom.wj_id1,
ndofs: geom.ndofs1,
}
}
}
}
```