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
// Author: Tom Olsson <tom.olsson@embark-studios.com>
// Copyright © 2019, Embark Studios, all rights reserved.
// Created: 16 April 2019

#![warn(clippy::all)]

/*!

*/

use crate::{
    articulation_joint::ArticulationJoint,
    articulation_joint_reduced_coordinate::ArticulationJointReducedCoordinate, base::Base,
    math::PxTransform, traits::Class,
};

use physx_sys::{
    PxArticulationJointBase_getChildPose,
    PxArticulationJointBase_getParentArticulationLink,
    PxArticulationJointBase_getParentPose,
    PxArticulationJointBase_setChildPose_mut,
    PxArticulationJointBase_setParentPose_mut,
    // PxArticulationJointBase_getChildArticulationLink,
    PxRigidActor_getGlobalPose,
};

/*******************************************************************************
 * Section IMPLEMENTATION                                                      *
 ******************************************************************************/

impl<T> ArticulationJointBase for T where T: Class<physx_sys::PxArticulationJointBase> + Base {}
pub trait ArticulationJointBase: Class<physx_sys::PxArticulationJointBase> + Base {
    /// Set the pose of the joint in the child frame
    fn set_child_pose(&mut self, pose: &PxTransform) {
        unsafe {
            PxArticulationJointBase_setChildPose_mut(self.as_mut_ptr(), pose.as_ptr());
        }
    }

    /// Get the pose of the joint in the child frame
    fn get_child_pose(&self) -> PxTransform {
        unsafe { PxArticulationJointBase_getChildPose(self.as_ptr()).into() }
    }

    /// Set the pose of the joint in the parent frame
    fn set_parent_pose(&mut self, pose: &PxTransform) {
        unsafe {
            PxArticulationJointBase_setParentPose_mut(self.as_mut_ptr(), pose.as_ptr());
        }
    }

    /// Get the pose of the joint in the parent frame
    fn get_parent_pose(&self) -> PxTransform {
        unsafe { PxArticulationJointBase_getParentPose(self.as_ptr()).into() }
    }

    /// Get the global pose of this joint.
    fn get_joint_transform_global(&self) -> PxTransform {
        // Avoid dealing with ArticulationLink directly so it's user data
        // type info doesn't need to be passed in here, since it's not needed.
        let outbound_link_global_pose: PxTransform = unsafe {
            PxRigidActor_getGlobalPose(PxArticulationJointBase_getParentArticulationLink(
                self.as_ptr(),
            ) as *mut _)
            .into()
        };
        let joint_pose = self.get_parent_pose();

        // model_to_world * model
        outbound_link_global_pose.transform(&joint_pose)
    }
}

/// A PxArticulatiopnJointBase new type wrapper with methods for safe casts.
pub struct JointMap {
    obj: physx_sys::PxArticulationJointBase,
}

unsafe impl<P> Class<P> for JointMap
where
    physx_sys::PxArticulationJointBase: Class<P>,
{
    fn as_ptr(&self) -> *const P {
        self.obj.as_ptr()
    }

    fn as_mut_ptr(&mut self) -> *mut P {
        self.obj.as_mut_ptr()
    }
}

impl JointMap {
    /// # Safety
    /// this relies on `get_concrete_type` to determine the joint type,
    /// which has had issues with returning ConcreteType::Undefined for Actor subclasses.
    /// `try_cast_map` will return `None` when this would crash.
    pub unsafe fn cast_map<'a, Ret, ArtJoFn, ArcJoFn>(
        &'a mut self,
        mut art_fn: ArtJoFn,
        mut arc_fn: ArcJoFn,
    ) -> Ret
    where
        ArtJoFn: FnMut(&'a mut ArticulationJoint) -> Ret,
        ArcJoFn: FnMut(&'a mut ArticulationJointReducedCoordinate) -> Ret,
    {
        match self.get_concrete_type() {
            crate::base::ConcreteType::ArticulationJoint => {
                art_fn(&mut *(self as *mut _ as *mut ArticulationJoint))
            }
            crate::base::ConcreteType::ArticulationJointReducedCoordinate => {
                arc_fn(&mut *(self as *mut _ as *mut ArticulationJointReducedCoordinate))
            }
            _ => panic!(
                "get_concrete_type for {:?} returned an invalid type: {:?}",
                self.get_concrete_type_name(),
                self.get_concrete_type()
            ),
        }
    }

    pub fn try_cast_map<'a, Ret, ArtJoFn, ArcJoFn>(
        &mut self,
        mut art_fn: ArtJoFn,
        mut arc_fn: ArcJoFn,
    ) -> Option<Ret>
    where
        ArtJoFn: FnMut(&'a mut ArticulationJoint) -> Ret,
        ArcJoFn: FnMut(&'a mut ArticulationJointReducedCoordinate) -> Ret,
    {
        match self.get_concrete_type() {
            crate::base::ConcreteType::ArticulationJoint => Some(art_fn(unsafe {
                &mut *(self as *mut _ as *mut ArticulationJoint)
            })),
            crate::base::ConcreteType::ArticulationJointReducedCoordinate => Some(arc_fn(unsafe {
                &mut *(self as *mut _ as *mut ArticulationJointReducedCoordinate)
            })),
            _ => None,
        }
    }

    pub fn as_articulation_joint(&mut self) -> Option<&mut ArticulationJoint> {
        match self.get_concrete_type() {
            crate::base::ConcreteType::ArticulationJoint => unsafe {
                Some(&mut *(self as *mut _ as *mut ArticulationJoint))
            },
            _ => None,
        }
    }

    pub fn as_articulation_joint_reduced_coordinate(
        &mut self,
    ) -> Option<&mut ArticulationJointReducedCoordinate> {
        match self.get_concrete_type() {
            crate::base::ConcreteType::ArticulationJointReducedCoordinate => unsafe {
                Some(&mut *(self as *mut _ as *mut ArticulationJointReducedCoordinate))
            },
            _ => None,
        }
    }
}