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

#![warn(clippy::all)]
#![warn(rust_2018_idioms)]

/*!
Wrapper for PxArticulationBase
 */

use super::{
    articulation_link::ArticulationLink, base::Base, body::*, math::Bounds, px_type::*,
    transform::*, user_data::UserData,
};
use glam::Mat4;
use physx_macros::*;
use physx_sys::{
    PxArticulationBase, PxArticulationBase_createLink_mut, PxArticulationBase_getNbLinks,
    PxArticulationBase_getSleepThreshold, PxArticulationBase_getSolverIterationCounts,
    PxArticulationBase_getStabilizationThreshold, PxArticulationBase_getWakeCounter,
    PxArticulationBase_getWorldBounds, PxArticulationBase_isSleeping,
    PxArticulationBase_putToSleep_mut, PxArticulationBase_setSleepThreshold_mut,
    PxArticulationBase_setSolverIterationCounts_mut,
    PxArticulationBase_setStabilizationThreshold_mut, PxArticulationBase_setWakeCounter_mut,
    PxArticulationBase_wakeUp_mut, PxArticulationLink, PxRigidActor, PxRigidActor_getGlobalPose,
};
use std::ptr::null_mut;

#[physx_type(inherit = "Base")]
impl ArticulationBase {
    /*
    fixme[tgolsson]: Unsupported so far!
        pub fn set_name(&mut self, name: *const i8) {}
        pub fn get_name(&self) -> *const i8 {}
        pub pub fn get_scene(&self) -> *mut PxScene
        pub pub fn create_link(&mut self, parent: *mut PxArticulationLink, pose: *const PxTransform) -> *mut PxArticulationLink;
        pub pub fn get_aggregate(&self) -> *mut PxAggregate;
        pub pub fn get_impl(&mut self) -> *mut PxArticulationImpl;
        pub pub fn get_impl(&self) -> *const PxArticulationImpl;
        pub pub fn create_articulation_joint(&mut self, parent: *mut PxArticulationLink, parentFrame: *const PxTransform, child: *mut PxArticulationLink, childFrame: *const PxTransform) -> *mut PxArticulationJointBase;
        pub pub fn release_articulation_joint(&mut self, joint: *mut PxArticulationJointBase);
     */

    pub fn new(ptr: *mut PxArticulationBase) -> Self {
        let mut _self = Self::from_ptr(ptr);
        _self.allocate_user_data();
        _self
    }

    pub fn set_solver_iteration_counts(
        &mut self,
        min_position_iters: u32,
        min_velocity_iters: u32,
    ) {
        unsafe {
            PxArticulationBase_setSolverIterationCounts_mut(
                self.get_raw_mut(),
                min_position_iters,
                min_velocity_iters,
            );
        }
    }

    pub fn get_solver_iteration_counts(&self) -> (u32, u32) {
        unsafe {
            let mut min_position_iters: u32 = 0;
            let mut min_velocity_iters: u32 = 0;
            PxArticulationBase_getSolverIterationCounts(
                self.get_raw(),
                &mut min_position_iters as *mut u32,
                &mut min_velocity_iters as *mut u32,
            );
            (min_position_iters, min_velocity_iters)
        }
    }

    /// Check if the articulation is sleeping
    pub fn is_sleeping(&self) -> bool {
        unsafe { PxArticulationBase_isSleeping(self.get_raw()) }
    }

    /// Set the inactivity threshold for sleeping this articulation
    pub fn set_sleep_threshold(&mut self, threshold: f32) {
        unsafe {
            PxArticulationBase_setSleepThreshold_mut(self.get_raw_mut(), threshold);
        }
    }

    /// Read back the inactivity threshold
    pub fn get_sleep_threshold(&self) -> f32 {
        unsafe { PxArticulationBase_getSleepThreshold(self.get_raw()) }
    }

    /// Set the stabilization threshold for this articulation
    pub fn set_stabilization_threshold(&mut self, threshold: f32) {
        unsafe {
            PxArticulationBase_setStabilizationThreshold_mut(self.get_raw_mut(), threshold);
        }
    }

    /// Get the stabilization threshold for this articulation
    pub fn get_stabilization_threshold(&self) -> f32 {
        unsafe { PxArticulationBase_getStabilizationThreshold(self.get_raw()) }
    }

    /// Set the counter for how many steps more the articulation will be awake
    /// if below the energy threshold. When this is non-zero, the articulation
    /// may sleep but is not required to do so depending on other factors.
    pub fn set_wake_counter(&mut self, threshold: f32) {
        unsafe {
            PxArticulationBase_setWakeCounter_mut(self.get_raw_mut(), threshold);
        }
    }

    /// Get the counter for how many steps more the agent will be awake if below the energy threshold.
    pub fn get_wake_counter(&self) -> f32 {
        unsafe { PxArticulationBase_getWakeCounter(self.get_raw()) }
    }

    /// Wake up the articulation
    pub fn wake_up(&mut self) {
        unsafe {
            PxArticulationBase_wakeUp_mut(self.get_raw_mut());
        }
    }

    /// Put the articulation to sleep immediately
    pub fn put_to_sleep(&mut self) {
        unsafe {
            PxArticulationBase_putToSleep_mut(self.get_raw_mut());
        }
    }

    /// Get the total number of links on this articulation
    pub fn get_nb_links(&self) -> usize {
        unsafe { PxArticulationBase_getNbLinks(self.get_raw()) as usize }
    }

    #[inline]
    /// Get an iterator over all links
    pub fn iter_links(&self) -> impl std::iter::Iterator<Item = &ArticulationLink> {
        match self.user_data() {
            UserData::ArticulationBase(data) => data.links.iter(),
            _ => unimplemented!(),
        }
    }

    #[inline]
    /// Get an iterator over all links
    pub fn iter_links_mut(&mut self) -> impl std::iter::Iterator<Item = &mut ArticulationLink> {
        match self.user_data_mut() {
            UserData::ArticulationBase(data) => data.links.iter_mut(),
            _ => unimplemented!(),
        }
    }

    #[inline]
    /// Get an iterator over all links
    pub fn links(&self) -> &Vec<ArticulationLink> {
        match self.user_data() {
            UserData::ArticulationBase(data) => &data.links,
            _ => unimplemented!(),
        }
    }

    #[inline]
    /// Get an iterator over all links
    pub fn links_mut(&mut self) -> &mut Vec<ArticulationLink> {
        match self.user_data_mut() {
            UserData::ArticulationBase(data) => &mut data.links,
            _ => unimplemented!(),
        }
    }

    /// Create a child link for parent, with the given transform
    pub fn create_link(
        &mut self,
        parent: PartHandle,
        transform: Option<Mat4>,
        joint_transform: Option<Mat4>,
    ) -> *mut PxArticulationLink {
        let (parent, trans) = if parent.1 == 0 {
            (null_mut(), Mat4::identity())
        } else {
            let parent_link = parent.1 as *mut PxArticulationLink;
            (parent_link, unsafe {
                px_to_gl_tf(PxRigidActor_getGlobalPose(
                    parent_link as *const PxRigidActor,
                ))
            })
        };

        let transform = joint_transform.unwrap_or_else(Mat4::identity)
            * transform.unwrap_or_else(Mat4::identity);
        // eprintln!("creating link with transform: {}", trans * transform);
        unsafe {
            PxArticulationBase_createLink_mut(
                self.get_raw_mut(),
                parent,
                &gl_to_px_tf(trans * transform),
            )
        }
    }

    /// Get the world bounds of this articulation
    pub fn get_world_bounds(&self, inflation: f32) -> Bounds {
        unsafe { PxArticulationBase_getWorldBounds(self.get_raw(), inflation).into() }
    }

    /// Add a link to this articulation
    pub fn add_link(&mut self, link: ArticulationLink) {
        if let UserData::ArticulationBase(data) = self.user_data_mut() {
            data.links.push(link)
        }
    }

    /// Get the user data
    pub fn user_data(&self) -> &UserData {
        unsafe { &*((*self.ptr).userData as *const UserData) }
    }

    /// Get the user data
    pub fn user_data_mut(&mut self) -> &mut UserData {
        unsafe { &mut *((*self.ptr).userData as *mut UserData) }
    }

    pub(crate) fn allocate_user_data(&mut self) {
        let userdata = Box::new(UserData::new_articulation_base());

        unsafe {
            (*self.ptr).userData = Box::into_raw(userdata) as *mut std::ffi::c_void;
        }
    }
}