k/joint/
joint_type.rs

1/*
2  Copyright 2020 Takashi Ogura
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15*/
16use nalgebra::{RealField, Unit, Vector3};
17use std::fmt::{self, Display};
18
19/// Type of Joint, `Fixed`, `Rotational`, `Linear` is supported now
20#[derive(Copy, Debug, Clone)]
21pub enum JointType<T: RealField> {
22    /// Fixed joint. It has no `joint_position` and axis.
23    Fixed,
24    /// Rotational joint around axis. It has an position `[rad]`.
25    Rotational {
26        /// axis of the joint
27        axis: Unit<Vector3<T>>,
28    },
29    /// Linear joint. position is length
30    Linear {
31        /// axis of the joint
32        axis: Unit<Vector3<T>>,
33    },
34}
35
36fn axis_to_string<T: RealField>(axis: &Unit<Vector3<T>>) -> &str {
37    if *axis == Vector3::x_axis() {
38        "+X"
39    } else if *axis == Vector3::y_axis() {
40        "+Y"
41    } else if *axis == Vector3::z_axis() {
42        "+Z"
43    } else if *axis == -Vector3::x_axis() {
44        "-X"
45    } else if *axis == -Vector3::y_axis() {
46        "-Y"
47    } else if *axis == -Vector3::z_axis() {
48        "-Z"
49    } else {
50        ""
51    }
52}
53
54impl<T: RealField> Display for JointType<T> {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        match self {
57            JointType::Fixed => write!(f, "[⚓]"),
58            JointType::Rotational { axis } => write!(f, "[⚙{}]", axis_to_string(axis)),
59            JointType::Linear { axis } => write!(f, "[↕{}]", axis_to_string(axis)),
60        }
61    }
62}