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
use crossbeam_queue::ArrayQueue;
use serde::{Deserialize, Serialize};
#[cfg(not(target_family = "wasm"))]
use std::sync::atomic::{AtomicBool, Ordering};
use std::{
    ops,
    sync::{Arc, Mutex, MutexGuard},
};

use crate::utils::RobotModel;

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct JointNamesAndPositions {
    pub names: Vec<String>,
    pub positions: Vec<f32>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct RobotOrigin {
    pub position: [f32; 3],
    pub quaternion: [f32; 4],
}

impl Default for RobotOrigin {
    fn default() -> Self {
        Self {
            position: [0.0; 3],
            quaternion: [0.0, 0.0, 0.0, 1.0],
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ObjectOrigin {
    pub id: String,
    pub position: [f32; 3],
    pub quaternion: [f32; 4],
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct PointsAndColors {
    pub id: Option<String>,
    pub points: Vec<[f32; 3]>,
    pub colors: Vec<[f32; 3]>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Cube {
    pub id: Option<String>,
    pub extent: Option<[f32; 3]>,
    pub color: Option<[f32; 3]>,
    pub position: Option<[f32; 3]>,
    pub quaternion: Option<[f32; 4]>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Capsule {
    pub id: Option<String>,
    pub height: f32,
    pub radius: f32,
    pub color: Option<[f32; 3]>,
    pub position: Option<[f32; 3]>,
    pub quaternion: Option<[f32; 4]>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct AxisMarker {
    pub id: Option<String>,
    pub size: f32,
    pub position: Option<[f32; 3]>,
    pub quaternion: Option<[f32; 4]>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Relationship {
    pub parent: String,
    pub child: String,
    /// Relative position
    pub position: [f32; 3],
    /// Relative rotation
    pub quaternion: [f32; 4],
}

/// Handle to get and modify the state of the robot.
#[derive(Debug)]
pub struct RobotStateHandle {
    pub(crate) target_joint_positions: Mutex<Option<JointNamesAndPositions>>,
    pub(crate) current_joint_positions: Mutex<JointNamesAndPositions>,
    pub(crate) target_object_origin: ArrayQueue<ObjectOrigin>,
    pub(crate) current_robot_origin: Mutex<RobotOrigin>,
    pub(crate) point_cloud: ArrayQueue<PointsAndColors>,
    pub(crate) cube: ArrayQueue<Cube>,
    pub(crate) capsule: ArrayQueue<Capsule>,
    pub(crate) axis_marker: ArrayQueue<AxisMarker>,
    pub(crate) relationship: ArrayQueue<Relationship>,
    pub(crate) urdf_text: Option<Arc<Mutex<String>>>,
    pub(crate) robot: Mutex<Option<RobotModel>>,
    #[cfg(not(target_family = "wasm"))]
    pub(crate) reload_request: AtomicBool,
}

impl Default for RobotStateHandle {
    fn default() -> Self {
        const QUEUE_CAP: usize = 8;
        Self {
            target_joint_positions: Mutex::default(),
            current_joint_positions: Mutex::default(),
            target_object_origin: ArrayQueue::new(QUEUE_CAP),
            current_robot_origin: Mutex::default(),
            point_cloud: ArrayQueue::new(QUEUE_CAP),
            cube: ArrayQueue::new(QUEUE_CAP),
            capsule: ArrayQueue::new(QUEUE_CAP),
            axis_marker: ArrayQueue::new(QUEUE_CAP),
            relationship: ArrayQueue::new(QUEUE_CAP),
            urdf_text: None,
            robot: Mutex::default(),
            #[cfg(not(target_family = "wasm"))]
            reload_request: AtomicBool::new(false),
        }
    }
}

// Wrapper type to prevent parking_lot updates from becoming a breaking change.
macro_rules! impl_guard {
    ($guard_name:ident($target:ident)) => {
        #[derive(Debug)]
        pub struct $guard_name<'a>(MutexGuard<'a, $target>);
        impl ops::Deref for $guard_name<'_> {
            type Target = $target;
            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }
        impl ops::DerefMut for $guard_name<'_> {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.0
            }
        }
    };
}
impl_guard!(JointNamesAndPositionsLockGuard(JointNamesAndPositions));
impl_guard!(RobotOriginLockGuard(RobotOrigin));
impl_guard!(UrdfTextLockGuard(String));

pub const ROBOT_OBJECT_ID: &str = "robot";

impl RobotStateHandle {
    pub fn current_joint_positions(&self) -> JointNamesAndPositionsLockGuard<'_> {
        JointNamesAndPositionsLockGuard(self.current_joint_positions.lock().unwrap())
    }

    pub fn current_robot_origin(&self) -> RobotOriginLockGuard<'_> {
        RobotOriginLockGuard(self.current_robot_origin.lock().unwrap())
    }

    pub fn urdf_text(&self) -> Option<UrdfTextLockGuard<'_>> {
        Some(UrdfTextLockGuard(self.urdf_text.as_ref()?.lock().unwrap()))
    }

    pub fn set_target_joint_positions(&self, joint_positions: JointNamesAndPositions) {
        *self.target_joint_positions.lock().unwrap() = Some(joint_positions);
    }

    pub fn set_target_robot_origin(&self, robot_origin: RobotOrigin) {
        self.target_object_origin.force_push(ObjectOrigin {
            id: ROBOT_OBJECT_ID.to_owned(),
            position: robot_origin.position,
            quaternion: robot_origin.quaternion,
        });
    }

    pub fn set_target_object_origin(&self, object_origin: ObjectOrigin) {
        self.target_object_origin.force_push(object_origin);
    }

    pub fn set_point_cloud(&self, points_and_colors: PointsAndColors) {
        self.point_cloud.force_push(points_and_colors);
    }

    pub fn set_cube(&self, cube: Cube) {
        self.cube.force_push(cube);
    }

    pub fn set_capsule(&self, capsule: Capsule) {
        self.capsule.force_push(capsule);
    }

    pub fn set_axis_marker(&self, axis_marker: AxisMarker) {
        self.axis_marker.force_push(axis_marker);
    }

    pub fn set_robot(&self, robot: RobotModel) {
        // set_robot may change name or number of joints, so reset target_joint_positions.
        *self.target_joint_positions.lock().unwrap() = None;
        *self.robot.lock().unwrap() = Some(robot);
    }

    pub fn set_relationship(&self, relationship: Relationship) {
        self.relationship.force_push(relationship);
    }

    pub fn take_target_joint_positions(&self) -> Option<JointNamesAndPositions> {
        self.target_joint_positions.lock().unwrap().take()
    }

    pub fn pop_target_object_origin(&self) -> Option<ObjectOrigin> {
        self.target_object_origin.pop()
    }

    pub(crate) fn take_robot(&self) -> Option<RobotModel> {
        self.robot.lock().unwrap().take()
    }

    #[cfg(not(target_family = "wasm"))]
    pub(crate) fn set_reload_request(&self) {
        self.reload_request.swap(true, Ordering::SeqCst);
    }

    #[cfg(not(target_family = "wasm"))]
    pub(crate) fn take_reload_request(&self) -> bool {
        self.reload_request.swap(false, Ordering::SeqCst)
    }
}