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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::rc::Rc;
use std::cell::RefCell;
use std::iter;
// use rand::RngUtil;
use alga::general::Real;
use na;
use math::{Vector, Orientation, Rotation, Translation, Isometry};
use detection::constraint::Constraint;
use detection::joint::Joint;
use object::RigidBody;
use resolution::constraint::velocity_constraint::VelocityConstraint;
use resolution::constraint::contact_equation;
use resolution::constraint::contact_equation::{CorrectionMode, CorrectionParameters};
use resolution::constraint::ball_in_socket_equation;
use resolution::constraint::fixed_equation;
use resolution::solver::Solver;
use resolution::constraint::projected_gauss_seidel_solver as pgs;
use resolution::constraint::projected_gauss_seidel_solver::Velocities;
use resolution::constraint::impulse_cache::ImpulseCache;


/// Constraint solver using the projected gauss seidel algorithm and warm-starting.
pub struct AccumulatedImpulseSolver<N: Real> {
    correction:              CorrectionParameters<N>,
    cache:                   ImpulseCache<N>,
    num_first_order_iter:    usize,
    num_second_order_iter:   usize,
    restitution_constraints: Vec<VelocityConstraint<N>>,
    friction_constraints:    Vec<VelocityConstraint<N>>,
    mj_lambda:               Vec<Velocities<N>>
}

impl<N: Real> AccumulatedImpulseSolver<N> {
    /// Creates a new `AccumulatedImpulseSolver`.
    pub fn new(step:                  N,
               correction_mode:       CorrectionMode<N>,
               joint_corr_factor:     N,
               rest_eps:              N,
               num_first_order_iter:  usize,
               num_second_order_iter: usize)
               -> AccumulatedImpulseSolver<N> {
        AccumulatedImpulseSolver {
            num_first_order_iter:    num_first_order_iter,
            num_second_order_iter:   num_second_order_iter,
            restitution_constraints: Vec::new(),
            friction_constraints:    Vec::new(),
            mj_lambda:               Vec::new(),
            cache:                   ImpulseCache::new(step, na::dimension::<Vector<N>>()),

            correction: CorrectionParameters {
                corr_mode:  correction_mode,
                joint_corr: joint_corr_factor,
                rest_eps:   rest_eps
            }
        }
    }

    /// Gets the number of iteration done by the penetration depth correction solver.
    #[inline]
    pub fn num_first_order_iter(&self) -> usize {
        self.num_first_order_iter
    }

    /// Sets the number of iteration done by the penetration depth correction solver.
    #[inline]
    pub fn set_num_first_order_iter(&mut self, num: usize) {
        self.num_first_order_iter = num
    }

    /// Gets the number of iteration done by the velocity constraint solver.
    #[inline]
    pub fn num_second_order_iter(&self) -> usize {
        self.num_second_order_iter
    }

    /// Sets the number of iteration done by the velocity constraint solver.
    #[inline]
    pub fn set_num_second_order_iter(&mut self, num: usize) {
        self.num_second_order_iter = num
    }

    fn resize_buffers(&mut self, num_restitution_equations: usize, num_friction_equations: usize) {
        resize_buffer(&mut self.restitution_constraints,
                      num_restitution_equations,
                      VelocityConstraint::new());

        resize_buffer(&mut self.friction_constraints,
                      num_friction_equations,
                      VelocityConstraint::new());
    }

    fn do_solve(&mut self,
                dt:          N,
                constraints: &[Constraint<N>],
                joints:      &[usize],
                bodies:      &[Rc<RefCell<RigidBody<N>>>]) {
        let num_friction_equations    = (na::dimension::<Vector<N>>() - 1) * self.cache.len();
        let num_restitution_equations = self.cache.len();
        let mut num_joint_equations = 0;

        for i in joints.iter() {
            match constraints[*i] {
                Constraint::BallInSocket(_) => {
                    num_joint_equations = num_joint_equations + na::dimension::<Vector<N>>()
                },
                Constraint::Fixed(_) => {
                    num_joint_equations = num_joint_equations +
                                          na::dimension::<Vector<N>>() +
                                          na::dimension::<Orientation<N>>()
                },
                Constraint::RBRB(_, _, _) => { }
            }
        }

        self.resize_buffers(num_restitution_equations + num_joint_equations, num_friction_equations);

        let mut friction_offset = 0;

        for (i, (_, &(ci, imp))) in self.cache.hash().iter().enumerate() {
            match constraints[ci] {
                Constraint::RBRB(ref rb1, ref rb2, ref c) => {
                    contact_equation::fill_second_order_equation(
                        dt.clone(),
                        c,
                        &*rb1.borrow(), &*rb2.borrow(),
                        &mut self.restitution_constraints[i],
                        i,
                        &mut self.friction_constraints[..],
                        friction_offset,
                        self.cache.impulsions_at(imp),
                        &self.correction);
                },
                _ => { }
            }

            friction_offset = friction_offset + na::dimension::<Vector<N>>() - 1;
        }

        let mut joint_offset = num_restitution_equations;
        for i in joints.iter() {
            let nconstraints = self.restitution_constraints.len();
            match constraints[*i] {
                Constraint::BallInSocket(ref bis) => {
                    ball_in_socket_equation::fill_second_order_equation(
                        dt.clone(),
                        &*bis.borrow(),
                        &mut self.restitution_constraints[joint_offset .. nconstraints], // XXX
                        &self.correction
                    );

                    joint_offset = joint_offset + na::dimension::<Vector<N>>();
                },
                Constraint::Fixed(ref f) => {
                    fixed_equation::fill_second_order_equation(
                        dt.clone(),
                        &*f.borrow(),
                        &mut self.restitution_constraints[joint_offset .. nconstraints], // XXX
                        &self.correction
                    );

                    joint_offset = joint_offset + na::dimension::<Vector<N>>() + na::dimension::<Orientation<N>>();
                },
                Constraint::RBRB(_, _, _) => { }
            }
        }

        resize_buffer(&mut self.mj_lambda, bodies.len(), Velocities::new());

        // FIXME: parametrize by the resolution algorithm?
        pgs::projected_gauss_seidel_solve(
            &mut self.restitution_constraints[..],
            &mut self.friction_constraints[..],
            &mut self.mj_lambda[..],
            bodies.len(),
            self.num_second_order_iter,
            false);

        // FIXME: this is _so_ ugly!
        self.resize_buffers(num_restitution_equations, num_friction_equations);

        for b in bodies.iter() {
            let mut rb = b.borrow_mut();
            let i      = rb.index();

            let curr_lin_vel = rb.lin_vel();
            let curr_ang_vel = rb.ang_vel();

            rb.set_lin_vel_internal(curr_lin_vel + self.mj_lambda[i as usize].lv);
            rb.set_ang_vel_internal(curr_ang_vel + self.mj_lambda[i as usize].av);
        }

        for (i, dv) in self.restitution_constraints.iter().enumerate() {
            let imps = self.cache.push_impulsions();
            imps[0]  = dv.impulse * na::convert::<f64, N>(0.85f64);

            for j in 0usize .. na::dimension::<Vector<N>>() - 1 {
                let fc = &self.friction_constraints[i * (na::dimension::<Vector<N>>() - 1) + j];
                imps[1 + j] = fc.impulse * na::convert::<f64, N>(0.85f64);
            }
        }

        let offset = self.cache.reserved_impulse_offset();
        for (i, (_, kv)) in self.cache.hash_mut().iter_mut().enumerate() {
            *kv = (kv.0, offset + i * na::dimension::<Vector<N>>());
        }

        /*
         * first order resolution
         */
        let needs_correction = !self.correction.corr_mode.pos_corr_factor().is_zero() &&
            constraints.iter().any(|constraint| {
            match *constraint {
                Constraint::RBRB(_, _, ref c) =>
                    c.depth >= self.correction.corr_mode.min_depth_for_pos_corr(),
                _ => false // no first order resolution for joints
            }
        });

        if needs_correction {
            self.resize_buffers(num_restitution_equations, num_friction_equations);

            for (i, (_, &(ci, _))) in self.cache.hash().iter().enumerate() {
                match constraints[ci] {
                    Constraint::RBRB(_, _, ref c) => {
                        contact_equation::reinit_to_first_order_equation(
                            dt.clone(),
                            c,
                            &mut self.restitution_constraints[i],
                            &self.correction);
                    },
                    _ => { }
                }
            }

            // FIXME: parametrize by the resolution algorithm?
            pgs::projected_gauss_seidel_solve(
                &mut self.restitution_constraints[..],
                &mut [][..],
                &mut self.mj_lambda[..],
                bodies.len(),
                self.num_first_order_iter,
                true);

            for b in bodies.iter() {
                let mut rb = b.borrow_mut();
                let i      = rb.index();

                let translation = Translation::from_vector(self.mj_lambda[i as usize].lv * dt);
                let rotation    = Rotation::from_scaled_axis(self.mj_lambda[i as usize].av * dt);

                let center = *rb.center_of_mass();

                let mut delta: Isometry<N> = na::one();
                delta.append_rotation_wrt_point_mut(&rotation, &center);
                delta.append_translation_mut(&translation);

                rb.append_transformation(&delta);
            }
        }
    }
}

impl<N: Real> Solver<N, Constraint<N>> for AccumulatedImpulseSolver<N> {
    fn solve(&mut self, dt: N, constraints: &[Constraint<N>]) {
        // FIXME: bodies index assignment is very ugly
        let mut bodies = Vec::new();

        if constraints.len() != 0 {
            /*
             * Associate the constraints with the cached impulse.
             */
            for (i, cstr) in constraints.iter().enumerate() {
                match *cstr {
                    Constraint::RBRB(ref a, ref b, ref c) => {
                        self.cache.insert(i,
                                          &**a as *const RefCell<RigidBody<N>> as usize,
                                          &**b as *const RefCell<RigidBody<N>> as usize,
                                          na::center(&c.world1, &c.world2));
                    },
                    Constraint::BallInSocket(_) => {
                        // XXX: cache for ball in socket?
                    },
                    Constraint::Fixed(_) => {
                        // XXX: cache for fixed?
                    }
                }
            }

            /*
             * Assign an index to each body.
             */
            // This is a two-passes assignation of index to the rigid bodies.
            // This is not very good, but is the only way to do that without having a separate list
            // of all rigid bodies.
            for c in constraints.iter() {
                match *c {
                    Constraint::RBRB(ref a, ref b, _) => {
                        a.borrow_mut().set_index(-2);
                        b.borrow_mut().set_index(-2)
                    },
                    Constraint::BallInSocket(ref bis) => {
                        let bbis = bis.borrow();
                        match bbis.anchor1().body {
                            Some(ref b) => {
                                b.borrow_mut().set_index(-2)
                            },
                            None    => { }
                        };

                        match bbis.anchor2().body {
                            Some(ref b) => {
                                b.borrow_mut().set_index(-2)
                            },
                            None    => { }
                        }
                    }
                    Constraint::Fixed(ref f) => { // FIXME: code duplication from BallInSocket
                        let bf = f.borrow();
                        match bf.anchor1().body {
                            Some(ref b) => {
                                b.borrow_mut().set_index(-2)
                            },
                            None    => { }
                        };

                        match bf.anchor2().body {
                            Some(ref b) => {
                                b.borrow_mut().set_index(-2)
                            },
                            None    => { }
                        }
                    }
                }
            }

            let mut id = 0;

            fn set_body_index<N: Real>(a:      &Rc<RefCell<RigidBody<N>>>,
                                       bodies: &mut Vec<Rc<RefCell<RigidBody<N>>>>,
                                       id:     &mut isize) {
                let mut ba = a.borrow_mut();
                if ba.index() == -2 {
                    if ba.can_move() {
                        ba.set_index(*id);
                        bodies.push(a.clone());
                        *id = *id + 1;
                    }
                    else {
                        ba.set_index(-1)
                    }
                }
            }

            // FIXME: avoid allocation
            let mut joints = Vec::new();
            for (i, c) in constraints.iter().enumerate() {
                match *c {
                    Constraint::RBRB(ref a, ref b, _) => {
                        set_body_index(a, &mut bodies, &mut id);
                        set_body_index(b, &mut bodies, &mut id);
                    },
                    Constraint::BallInSocket(ref bis) => {
                        joints.push(i);
                        let bbis = bis.borrow();
                        match bbis.anchor1().body {
                            Some(ref b) => set_body_index(b, &mut bodies, &mut id),
                            None        => { }
                        }

                        match bbis.anchor2().body {
                            Some(ref b) => set_body_index(b, &mut bodies, &mut id),
                            None        => { }
                        }
                    },
                    Constraint::Fixed(ref f) => { // FIXME: code duplication from BallInSocket
                        joints.push(i);
                        let bf = f.borrow();
                        match bf.anchor1().body {
                            Some(ref b) => set_body_index(b, &mut bodies, &mut id),
                            None        => { }
                        }

                        match bf.anchor2().body {
                            Some(ref b) => set_body_index(b, &mut bodies, &mut id),
                            None        => { }
                        }
                    }
                }
            }

            self.do_solve(dt.clone(), constraints, &joints[..], &bodies[..]);
            self.cache.swap();
        }
    }
}

fn resize_buffer<A: Clone>(buff: &mut Vec<A>, size: usize, val: A) {
    if buff.len() < size {
        let diff = size - buff.len();
        buff.extend(iter::repeat(val).take(diff));
    }
    else {
        buff.truncate(size)
    }
}