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
//! This module contains inverse-kinematic functionality for the skelly crate.

// pub mod fabrik;
pub mod rotor;

use {
    crate::skelly::{Posture, Skelly},
    core::ops::Add,
    na::Scalar,
};

/// Variants of results for `IkSolver::solve_step` method.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Status {
    /// All constrains and goals are satisfied with error less than configured for solver.
    Solved,

    /// Error in constraints and goals are unsatisfied
    Unsolved,

    /// Returned if solver determined that goals cannot be satisfied given the constraitns.
    Infeasible,
}

pub struct StepResult<T> {
    error: T,
    status: Status,
}

impl<T> StepResult<T>
where
    T: Add<Output = T>,
{
    pub fn solved(error: T) -> Self {
        StepResult {
            error,
            status: Status::Solved,
        }
    }

    pub fn unsolved(error: T) -> Self {
        StepResult {
            error,
            status: Status::Unsolved,
        }
    }

    pub fn infeasible(error: T) -> Self {
        StepResult {
            error,
            status: Status::Infeasible,
        }
    }

    /// Combine two step results into one
    pub fn reduce(self, rhs: Self) -> Self {
        let status = match (self.status, rhs.status) {
            (Status::Infeasible, _) | (_, Status::Infeasible) => Status::Infeasible,
            (Status::Unsolved, _) | (_, Status::Unsolved) => Status::Unsolved,
            (Status::Solved, Status::Solved) => Status::Solved,
        };

        StepResult {
            error: self.error + rhs.error,
            status,
        }
    }
}

/// Trait for ik solvers.
/// Using this common interface user may replace implementation easily.
pub trait IkSolver<T: Scalar> {
    /// Returns new solver with maximum tolerable error.
    fn new(error: T) -> Self;

    /// Performs one step toward solution.
    fn solve_step<D>(&mut self, skelly: &Skelly<T, D>, posture: &mut Posture<T>) -> StepResult<T>;
}