rapier3d/dynamics/solver/
solver_constraints_set.rs

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
use super::InteractionGroups;
use crate::math::Real;
use na::DVector;

pub(crate) trait ConstraintTypes {
    type OneBody;
    type TwoBodies;
    type GenericOneBody;
    type GenericTwoBodies;

    #[cfg(feature = "simd-is-enabled")]
    type SimdOneBody;
    #[cfg(feature = "simd-is-enabled")]
    type SimdTwoBodies;

    type BuilderOneBody;
    type BuilderTwoBodies;

    type GenericBuilderOneBody;
    type GenericBuilderTwoBodies;

    #[cfg(feature = "simd-is-enabled")]
    type SimdBuilderOneBody;
    #[cfg(feature = "simd-is-enabled")]
    type SimdBuilderTwoBodies;
}

#[derive(Debug)]
pub enum AnyConstraintMut<'a, Constraints: ConstraintTypes> {
    OneBody(&'a mut Constraints::OneBody),
    TwoBodies(&'a mut Constraints::TwoBodies),
    GenericOneBody(&'a mut Constraints::GenericOneBody),
    GenericTwoBodies(&'a mut Constraints::GenericTwoBodies),
    #[cfg(feature = "simd-is-enabled")]
    SimdOneBody(&'a mut Constraints::SimdOneBody),
    #[cfg(feature = "simd-is-enabled")]
    SimdTwoBodies(&'a mut Constraints::SimdTwoBodies),
}

pub(crate) struct SolverConstraintsSet<Constraints: ConstraintTypes> {
    pub generic_jacobians: DVector<Real>,
    pub two_body_interactions: Vec<usize>,
    pub one_body_interactions: Vec<usize>,
    pub generic_two_body_interactions: Vec<usize>,
    pub generic_one_body_interactions: Vec<usize>,
    pub interaction_groups: InteractionGroups,
    pub one_body_interaction_groups: InteractionGroups,

    pub velocity_constraints: Vec<Constraints::TwoBodies>,
    pub generic_velocity_constraints: Vec<Constraints::GenericTwoBodies>,
    #[cfg(feature = "simd-is-enabled")]
    pub simd_velocity_constraints: Vec<Constraints::SimdTwoBodies>,
    pub velocity_one_body_constraints: Vec<Constraints::OneBody>,
    pub generic_velocity_one_body_constraints: Vec<Constraints::GenericOneBody>,
    #[cfg(feature = "simd-is-enabled")]
    pub simd_velocity_one_body_constraints: Vec<Constraints::SimdOneBody>,

    pub velocity_constraints_builder: Vec<Constraints::BuilderTwoBodies>,
    pub generic_velocity_constraints_builder: Vec<Constraints::GenericBuilderTwoBodies>,
    #[cfg(feature = "simd-is-enabled")]
    pub simd_velocity_constraints_builder: Vec<Constraints::SimdBuilderTwoBodies>,
    pub velocity_one_body_constraints_builder: Vec<Constraints::BuilderOneBody>,
    pub generic_velocity_one_body_constraints_builder: Vec<Constraints::GenericBuilderOneBody>,
    #[cfg(feature = "simd-is-enabled")]
    pub simd_velocity_one_body_constraints_builder: Vec<Constraints::SimdBuilderOneBody>,
}

impl<Constraints: ConstraintTypes> SolverConstraintsSet<Constraints> {
    pub fn new() -> Self {
        Self {
            generic_jacobians: DVector::zeros(0),
            two_body_interactions: vec![],
            one_body_interactions: vec![],
            generic_two_body_interactions: vec![],
            generic_one_body_interactions: vec![],
            interaction_groups: InteractionGroups::new(),
            one_body_interaction_groups: InteractionGroups::new(),

            velocity_constraints: vec![],
            generic_velocity_constraints: vec![],
            #[cfg(feature = "simd-is-enabled")]
            simd_velocity_constraints: vec![],
            velocity_one_body_constraints: vec![],
            generic_velocity_one_body_constraints: vec![],
            #[cfg(feature = "simd-is-enabled")]
            simd_velocity_one_body_constraints: vec![],

            velocity_constraints_builder: vec![],
            generic_velocity_constraints_builder: vec![],
            #[cfg(feature = "simd-is-enabled")]
            simd_velocity_constraints_builder: vec![],
            velocity_one_body_constraints_builder: vec![],
            generic_velocity_one_body_constraints_builder: vec![],
            #[cfg(feature = "simd-is-enabled")]
            simd_velocity_one_body_constraints_builder: vec![],
        }
    }

    #[allow(dead_code)] // Useful for debugging.
    pub fn print_counts(&self) {
        println!("Solver constraints:");
        println!(
            "|__ two_body_interactions: {}",
            self.two_body_interactions.len()
        );
        println!(
            "|__ one_body_interactions: {}",
            self.one_body_interactions.len()
        );
        println!(
            "|__ generic_two_body_interactions: {}",
            self.generic_two_body_interactions.len()
        );
        println!(
            "|__ generic_one_body_interactions: {}",
            self.generic_one_body_interactions.len()
        );

        println!(
            "|__ velocity_constraints: {}",
            self.velocity_constraints.len()
        );
        println!(
            "|__ generic_velocity_constraints: {}",
            self.generic_velocity_constraints.len()
        );
        #[cfg(feature = "simd-is-enabled")]
        println!(
            "|__ simd_velocity_constraints: {}",
            self.simd_velocity_constraints.len()
        );
        println!(
            "|__ velocity_one_body_constraints: {}",
            self.velocity_one_body_constraints.len()
        );
        println!(
            "|__ generic_velocity_one_body_constraints: {}",
            self.generic_velocity_one_body_constraints.len()
        );
        #[cfg(feature = "simd-is-enabled")]
        println!(
            "|__ simd_velocity_one_body_constraints: {}",
            self.simd_velocity_one_body_constraints.len()
        );

        println!(
            "|__ velocity_constraints_builder: {}",
            self.velocity_constraints_builder.len()
        );
        println!(
            "|__ generic_velocity_constraints_builder: {}",
            self.generic_velocity_constraints_builder.len()
        );
        #[cfg(feature = "simd-is-enabled")]
        println!(
            "|__ simd_velocity_constraints_builder: {}",
            self.simd_velocity_constraints_builder.len()
        );
        println!(
            "|__ velocity_one_body_constraints_builder: {}",
            self.velocity_one_body_constraints_builder.len()
        );
        println!(
            "|__ generic_velocity_one_body_constraints_builder: {}",
            self.generic_velocity_one_body_constraints_builder.len()
        );
        #[cfg(feature = "simd-is-enabled")]
        println!(
            "|__ simd_velocity_one_body_constraints_builder: {}",
            self.simd_velocity_one_body_constraints_builder.len()
        );
    }

    pub fn clear_constraints(&mut self) {
        self.generic_jacobians.fill(0.0);
        self.velocity_constraints.clear();
        self.velocity_one_body_constraints.clear();
        self.generic_velocity_constraints.clear();
        self.generic_velocity_one_body_constraints.clear();

        #[cfg(feature = "simd-is-enabled")]
        {
            self.simd_velocity_constraints.clear();
            self.simd_velocity_one_body_constraints.clear();
        }
    }

    pub fn clear_builders(&mut self) {
        self.velocity_constraints_builder.clear();
        self.velocity_one_body_constraints_builder.clear();
        self.generic_velocity_constraints_builder.clear();
        self.generic_velocity_one_body_constraints_builder.clear();

        #[cfg(feature = "simd-is-enabled")]
        {
            self.simd_velocity_constraints_builder.clear();
            self.simd_velocity_one_body_constraints_builder.clear();
        }
    }

    // Returns the generic jacobians and a mutable iterator through all the constraints.
    pub fn iter_constraints_mut(
        &mut self,
    ) -> (
        &DVector<Real>,
        impl Iterator<Item = AnyConstraintMut<Constraints>>,
    ) {
        let jac = &self.generic_jacobians;
        let a = self
            .velocity_constraints
            .iter_mut()
            .map(AnyConstraintMut::TwoBodies);
        let b = self
            .generic_velocity_constraints
            .iter_mut()
            .map(AnyConstraintMut::GenericTwoBodies);
        #[cfg(feature = "simd-is-enabled")]
        let c = self
            .simd_velocity_constraints
            .iter_mut()
            .map(AnyConstraintMut::SimdTwoBodies);
        let d = self
            .velocity_one_body_constraints
            .iter_mut()
            .map(AnyConstraintMut::OneBody);
        let e = self
            .generic_velocity_one_body_constraints
            .iter_mut()
            .map(AnyConstraintMut::GenericOneBody);
        #[cfg(feature = "simd-is-enabled")]
        let f = self
            .simd_velocity_one_body_constraints
            .iter_mut()
            .map(AnyConstraintMut::SimdOneBody);

        #[cfg(feature = "simd-is-enabled")]
        return (jac, a.chain(b).chain(c).chain(d).chain(e).chain(f));

        #[cfg(not(feature = "simd-is-enabled"))]
        return (jac, a.chain(b).chain(d).chain(e));
    }
}