optima 0.0.4

An easy to set up and easy optimization and planning toolbox, particularly for robotics.
Documentation
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#[cfg(not(target_arch = "wasm32"))]
use pyo3::*;

use serde::{Serialize, Deserialize};
use nalgebra::{UnitQuaternion, Rotation3, Vector3, Unit, Matrix3};
use crate::utils::utils_errors::OptimaError;
use crate::utils::utils_traits::ToAndFromRonString;

/// An enum used to represent a rotation or orientation.  The enum affords easy conversion between
/// rotation types and functions over singular or pairs of rotations.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum OptimaRotation {
    RotationMatrix{data: Rotation3<f64>, rotation_type: OptimaRotationType },
    UnitQuaternion{data: UnitQuaternion<f64>, rotation_type: OptimaRotationType }
}
impl OptimaRotation {
    pub fn new_rotation_matrix(data: Rotation3<f64>) -> OptimaRotation {
        OptimaRotation::RotationMatrix { data, rotation_type: OptimaRotationType::RotationMatrix }
    }
    pub fn new_unit_quaternion(data: UnitQuaternion<f64>) -> OptimaRotation {
        OptimaRotation::UnitQuaternion { data, rotation_type: OptimaRotationType::UnitQuaternion }
    }
    pub fn new_unit_quaternion_identity() -> OptimaRotation {
        OptimaRotation::UnitQuaternion { data: Default::default(), rotation_type: OptimaRotationType::UnitQuaternion }
    }
    pub fn new_rotation_matrix_identity() -> OptimaRotation {
        OptimaRotation::RotationMatrix { data: Default::default(), rotation_type: OptimaRotationType::RotationMatrix }
    }
    pub fn new_rotation_matrix_from_euler_angles(rx: f64, ry: f64, rz: f64) -> OptimaRotation {
        let data = Rotation3::from_euler_angles(rx, ry, rz);
        return Self::new_rotation_matrix(data);
    }
    pub fn new_unit_quaternion_from_euler_angles(rx: f64, ry: f64, rz: f64) -> OptimaRotation {
        let q = UnitQuaternion::from_euler_angles(rx, ry, rz);
        return Self::new_unit_quaternion(q);
    }
    pub fn new_rotation_matrix_from_axis_angle(axis: &Unit<Vector3<f64>>, angle: f64) -> OptimaRotation {
        let data = Rotation3::from_axis_angle(axis, angle);
        return Self::new_rotation_matrix(data);
    }
    pub fn new_unit_quaternion_from_axis_angle(axis: &Unit<Vector3<f64>>, angle: f64) -> OptimaRotation {
        let data = UnitQuaternion::from_axis_angle(axis, angle);
        return Self::new_unit_quaternion(data);
    }
    pub fn new_rotation_matrix_from_lookat(lookat_direction: Vector3<f64>, lookat_axis: LookatAxis) -> OptimaRotation {
        assert!(lookat_direction.norm() > 0.0);

        let mut mat = Matrix3::identity();

        let lookat_column = match lookat_axis {
            LookatAxis::X => {0}
            LookatAxis::Y => {1}
            LookatAxis::Z => {2}
            LookatAxis::NegX => {0}
            LookatAxis::NegY => {1}
            LookatAxis::NegZ => {2}
        };
        let multiplier = match lookat_axis {
            LookatAxis::X => {1.0}
            LookatAxis::Y => {1.0}
            LookatAxis::Z => {1.0}
            LookatAxis::NegX => {-1.0}
            LookatAxis::NegY => {-1.0}
            LookatAxis::NegZ => {-1.0}
        };
        let column_a = (lookat_column + 1) % 3;
        let column_b = (lookat_column + 2) % 3;

        let l = multiplier * lookat_direction.normalize();
        mat[(0, lookat_column)] = l[0];
        mat[(1, lookat_column)] = l[1];
        mat[(2, lookat_column)] = l[2];

        let mut u = Vector3::new(0.,0.,1.);
        let dot_test = l.dot(&u);
        if dot_test == 1.0 || dot_test == -1.0 {
            u = Vector3::new(0.,1.,0.);
        }

        let c_a = l.cross(&u).normalize();
        mat[(0, column_a)] = c_a[0];
        mat[(1, column_a)] = c_a[1];
        mat[(2, column_a)] = c_a[2];

        let c_b = l.cross(&c_a).normalize();
        mat[(0, column_b)] = c_b[0];
        mat[(1, column_b)] = c_b[1];
        mat[(2, column_b)] = c_b[2];

        let data = Rotation3::from_matrix_eps(&mat, 0.0001, 1000, Rotation3::identity());
        let e = data.euler_angles();
        if e.0 == 0.0 && e.1 == 0.0 && e.2 == 0.0 {
            return Self::new_rotation_matrix_from_lookat(lookat_direction + Vector3::new(0.00001, 0.00001, 0.00001), lookat_axis);
        }
        return Self::new_rotation_matrix(data);
    }
    pub fn new_quaternion_from_lookat(lookat_direction: Vector3<f64>, lookat_axis: LookatAxis) -> OptimaRotation {
        let r = Self::new_rotation_matrix_from_lookat(lookat_direction, lookat_axis);
        return r.convert(&OptimaRotationType::UnitQuaternion);
    }
    /// Creates new rotation by exponentating the logarithm vector (the vector returned by ln()
    /// function).
    pub fn new_from_exp(ln_vec: &Vector3<f64>, rotation_type: &OptimaRotationType) -> Self {
        return match rotation_type {
            OptimaRotationType::RotationMatrix => {
                let data = Rotation3::new(ln_vec.clone());
                Self::new_rotation_matrix(data)
            }
            OptimaRotationType::UnitQuaternion => {
                let data = UnitQuaternion::new(ln_vec.clone());
                Self::new_unit_quaternion(data)
            }
        }
    }
    /// Converts the rotation to another provided rotation type.
    pub fn convert(&self, target_type: &OptimaRotationType) -> OptimaRotation {
        return match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                match target_type {
                    OptimaRotationType::RotationMatrix => {
                        self.clone()
                    }
                    OptimaRotationType::UnitQuaternion => {
                        let data = UnitQuaternion::from_rotation_matrix(data);
                        Self::new_unit_quaternion(data)
                    }
                }
            }
            OptimaRotation::UnitQuaternion { data, .. } => {
                match target_type {
                    OptimaRotationType::RotationMatrix => {
                        let data: Rotation3<f64> = data.to_rotation_matrix();
                        Self::new_rotation_matrix(data)
                    }
                    OptimaRotationType::UnitQuaternion => {
                        self.clone()
                    }
                }
            }
        }
    }
    /// Inverse rotation such that R * R^-1 = I
    pub fn inverse(&self) -> OptimaRotation {
        return match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                let new_data = data.inverse();
                Self::new_rotation_matrix(new_data)
            }
            OptimaRotation::UnitQuaternion { data, .. } => {
                let new_data = data.inverse();
                Self::new_unit_quaternion(new_data)
            }
        }
    }
    /// The angle that is encoded by the given rotation
    pub fn angle(&self) -> f64 {
        return match self {
            OptimaRotation::RotationMatrix { data, .. } => { data.angle() }
            OptimaRotation::UnitQuaternion { data, .. } => { data.angle() }
        }
    }
    /// Natural logarithm of the rotation.  This can be thought of as the rotation axis that is
    /// scaled by the length of the angle of rotation.
    pub fn ln(&self) -> Vector3<f64> {
        return match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                let scaled_axis = data.scaled_axis();
                scaled_axis
            }
            OptimaRotation::UnitQuaternion { data, .. } => {
                let out_vec: Vector3<f64> = data.ln().vector().into();
                out_vec
            }
        }
    }
    /// Rotation multiplication.
    pub fn multiply(&self, other: &OptimaRotation, conversion_if_necessary: bool) -> Result<OptimaRotation, OptimaError> {
        if self.get_rotation_type() != other.get_rotation_type() {
            return if conversion_if_necessary {
                let new_operand = other.convert(self.get_rotation_type());
                self.multiply(&new_operand, conversion_if_necessary)
            } else {
                Err(OptimaError::new_generic_error_str("incompatible rotation types in multiply.", file!(), line!()))
            }
        }

        return match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                let data0 = data;
                match other {
                    OptimaRotation::RotationMatrix { data, .. } => {
                        let new_data = data0 * data;
                        Ok(Self::new_rotation_matrix(new_data))
                    }
                    _ => { Err(OptimaError::new_generic_error_str("incompatible rotation types in multiply.", file!(), line!())) }
                }
            }
            OptimaRotation::UnitQuaternion { data, rotation_type: _ } => {
                let data0 = data;
                match other {
                    OptimaRotation::UnitQuaternion { data, .. } => {
                        let new_data = data0 * data;
                        Ok(Self::new_unit_quaternion(new_data))
                    }
                    _ => { Err(OptimaError::new_generic_error_str("incompatible rotation types in multiply.", file!(), line!())) }
                }
            }
        }
    }
    /// Rotation multiplication by a point.
    pub fn multiply_by_point(&self, point: &Vector3<f64>) -> Vector3<f64> {
        return match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                data * point
            }
            OptimaRotation::UnitQuaternion { data, .. } => {
                data * point
            }
        }
    }
    /// Returns true if the rotation is identity.
    pub fn is_identity(&self) -> bool {
        return match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                if data.angle() == 0.0 { true } else { false }
            }
            OptimaRotation::UnitQuaternion { data, .. } => {
                if data.angle() == 0.0 { true } else { false }
            }
        }
    }
    /// The displacement between two rotations such that R_self * R_displacement = R_other
    pub fn displacement(&self, other: &OptimaRotation, conversion_if_necessary: bool) -> Result<OptimaRotation, OptimaError> {
        if self.get_rotation_type() != other.get_rotation_type() {
            return if conversion_if_necessary {
                let new_operand = other.convert(self.get_rotation_type());
                self.displacement(&new_operand, conversion_if_necessary)
            } else {
                Err(OptimaError::new_generic_error_str("incompatible rotation types in multiply.", file!(), line!()))
            }
        }

        return match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                let data0 = data;
                match other {
                    OptimaRotation::RotationMatrix { data, .. } => {
                        let new_data = data0.inverse() * data;
                        Ok(Self::new_rotation_matrix(new_data))
                    }
                    _ => { Err(OptimaError::new_generic_error_str("incompatible rotation types in multiply.", file!(), line!())) }
                }
            }
            OptimaRotation::UnitQuaternion { data, rotation_type: _ } => {
                let data0 = data;
                match other {
                    OptimaRotation::UnitQuaternion { data, .. } => {
                        let new_data = data0.inverse() * data;
                        Ok(Self::new_unit_quaternion(new_data))
                    }
                    _ => { Err(OptimaError::new_generic_error_str("incompatible rotation types in multiply.", file!(), line!())) }
                }
            }
        }
    }
    /// The angle between two rotations.
    pub fn angle_between(&self, other: &OptimaRotation, conversion_if_necessary: bool) -> Result<f64, OptimaError> {
        if self.get_rotation_type() != other.get_rotation_type() {
            return if conversion_if_necessary {
                let new_operand = other.convert(self.get_rotation_type());
                self.angle_between(&new_operand, conversion_if_necessary)
            } else {
                Err(OptimaError::new_generic_error_str("incompatible rotation types in multiply.", file!(), line!()))
            }
        }

        return match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                let data0 = data;
                match other {
                    OptimaRotation::RotationMatrix { data, .. } => {
                        let angle_between = data0.angle_to(data);
                        Ok(angle_between)
                    }
                    _ => { Err(OptimaError::new_generic_error_str("incompatible rotation types in multiply.", file!(), line!())) }
                }
            }
            OptimaRotation::UnitQuaternion { data, rotation_type: _ } => {
                let data0 = data;
                match other {
                    OptimaRotation::UnitQuaternion { data, .. } => {
                        let angle_between = data0.angle_to(data);
                        Ok(angle_between)
                    }
                    _ => { Err(OptimaError::new_generic_error_str("incompatible rotation types in multiply.", file!(), line!())) }
                }
            }
        }
    }
    /// Returns the 3x3 rotation matrix encoded by the rotation object.  Returns error if the
    /// underlying representation is not a RotationMatrix.
    pub fn unwrap_rotation_matrix(&self) -> Result<&Rotation3<f64>, OptimaError> {
        return match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                Ok(data)
            }
            OptimaRotation::UnitQuaternion { .. } => {
                Err(OptimaError::new_generic_error_str("tried to unwrap unit quaternion as rotation matrix.", file!(), line!()))
            }
        }
    }
    /// Returns the Unit Quaternion encoded by the rotation object.  Returns error if the
    /// underlying representation is not a UnitQuaternion.
    pub fn unwrap_unit_quaternion(&self) -> Result<&UnitQuaternion<f64>, OptimaError> {
        return match self {
            OptimaRotation::RotationMatrix { .. } => {
                Err(OptimaError::new_generic_error_str("tried to unwrap rotation matrix as unit quaternion.", file!(), line!()))
            }
            OptimaRotation::UnitQuaternion { data, .. } => {
                Ok(data)
            }
        }
    }
    /// Returns the euler angle representation of the rotation.
    pub fn to_euler_angles(&self) -> Vector3<f64> {
        let euler_angles = match self {
            OptimaRotation::RotationMatrix { data, .. } => { data.euler_angles() }
            OptimaRotation::UnitQuaternion { data, .. } => { data.euler_angles() }
        };
        let euler_angles_vec = Vector3::new(euler_angles.0, euler_angles.1, euler_angles.2);
        return euler_angles_vec;
    }
    /// To axis angle representation of a rotation.
    pub fn to_axis_angle(&self) -> (Vector3<f64>, f64) {
        let axis_angle = match self {
            OptimaRotation::RotationMatrix { data, .. } => { data.axis_angle() }
            OptimaRotation::UnitQuaternion { data, .. } => { data.axis_angle() }
        };
        match axis_angle {
            None => {
                (Vector3::new(0.,0.,0.), 0.0)
            }
            Some(axis_angle) => {
                (Vector3::new(axis_angle.0[0], axis_angle.0[1], axis_angle.0[2]), axis_angle.1)
            }
        }
    }
    /// Spherical linear interpolation.
    pub fn slerp(&self, other: &OptimaRotation, t: f64, conversion_if_necessary: bool) -> Result<OptimaRotation, OptimaError> {
        if self.get_rotation_type() != other.get_rotation_type() {
            return if conversion_if_necessary {
                let new_operand = other.convert(self.get_rotation_type());
                self.slerp(&new_operand, t, conversion_if_necessary)
            } else {
                Err(OptimaError::new_generic_error_str("incompatible rotation types in interpolate.", file!(), line!()))
            }
        }

        return match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                let data0 = data;
                match other {
                    OptimaRotation::RotationMatrix { data, .. } => {
                        Ok(Self::new_rotation_matrix(data0.slerp(data, t)))
                    }
                    OptimaRotation::UnitQuaternion { .. } => {
                        Err(OptimaError::new_generic_error_str("incompatible rotation types in interpolate.", file!(), line!()))
                    }
                }
            }
            OptimaRotation::UnitQuaternion { data, .. } => {
                let data0 = data;
                match other {
                    OptimaRotation::RotationMatrix { .. } => {
                        Err(OptimaError::new_generic_error_str("incompatible rotation types in interpolate.", file!(), line!()))
                    }
                    OptimaRotation::UnitQuaternion { data, .. } => {
                        Ok(Self::new_unit_quaternion(data0.slerp(data, t)))
                    }
                }
            }
        }
    }
    fn get_rotation_type(&self) -> &OptimaRotationType {
        return match &self {
            OptimaRotation::RotationMatrix { data: _, rotation_type } => { rotation_type }
            OptimaRotation::UnitQuaternion { data: _, rotation_type } => { rotation_type }
        }
    }
    /// Converts to vector representation.
    ///
    /// If quaternion: [[q_i, q_j, q_k, q_w]]
    ///
    /// If rotation matrix: [[r_00, r_01, r_02], [r_10, r_11, r_12], [r_20, r_21, r_22]]
    pub fn to_vec_representation(&self) -> Vec<Vec<f64>> {
        let mut out_vec = vec![];
        match self {
            OptimaRotation::RotationMatrix { data, .. } => {
                for i in 0..3 {
                    let mut tmp_vec = vec![];
                    for j in 0..3 {
                        tmp_vec.push(data[(i,j)]);
                    }
                    out_vec.push(tmp_vec);
                }
            }
            OptimaRotation::UnitQuaternion { data, .. } => {
                out_vec.push(vec![]);
                out_vec[0].push(data.i);
                out_vec[0].push(data.j);
                out_vec[0].push(data.k);
                out_vec[0].push(data.w);
            }
        }
        out_vec
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub enum OptimaRotationType {
    RotationMatrix,
    UnitQuaternion
}

#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub enum LookatAxis {
    X,Y,Z,NegX,NegY,NegZ
}

#[cfg_attr(not(target_arch = "wasm32"), pyclass, derive(Clone, Debug, Serialize, Deserialize))]
pub struct OptimaRotationPy {
    rotation: OptimaRotation
}
#[cfg(not(target_arch = "wasm32"))]
#[pymethods]
impl OptimaRotationPy {
    #[staticmethod]
    pub fn new_rotation_matrix_from_lookat_py(lookat: Vec<f64>, lookat_axis: &str) -> Self {
        let v = Vector3::new(lookat[0], lookat[1], lookat[2]);
        let rotation = OptimaRotation::new_rotation_matrix_from_lookat(v, LookatAxis::from_ron_string(lookat_axis).expect("error"));
        return Self {
            rotation
        }
    }
    #[staticmethod]
    pub fn new_rotation_matrix_from_euler_angles_py(rx: f64, ry: f64, rz: f64) -> Self {
        let rotation = OptimaRotation::new_rotation_matrix_from_euler_angles(rx, ry, rz);
        return Self {
            rotation
        }
    }
    pub fn to_euler_angles_py(&self) -> Vec<f64> {
        let mut out_vec = vec![];

        let e = self.rotation.to_euler_angles();
        out_vec.push(e[0]);
        out_vec.push(e[1]);
        out_vec.push(e[2]);

        out_vec
    }
    pub fn to_axis_angle_py(&self) -> (Vec<f64>, f64) {
        let mut out_vec = vec![];

        let e = self.rotation.to_axis_angle();
        out_vec.push(e.0[0]);
        out_vec.push(e.0[1]);
        out_vec.push(e.0[2]);

        (out_vec, e.1)
    }
    pub fn print_summary_py(&self) {
        println!("{:?}", self);
    }
}
impl OptimaRotationPy {
    pub fn rotation(&self) -> &OptimaRotation { &self.rotation }
}