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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use std::collections::HashMap;

#[cfg(not(target_family = "wasm"))]
pub use native::*;
#[cfg(target_family = "wasm")]
pub use wasm::*;

pub(crate) fn replace_package_with_path(
    filename: &str,
    package_path: &HashMap<String, String>,
) -> Option<String> {
    let path = filename.strip_prefix("package://")?;
    let (package_name, path) = path.split_once('/')?;
    let package_path = package_path.get(package_name)?;
    Some(format!(
        "{}/{path}",
        package_path.strip_suffix('/').unwrap_or(package_path),
    ))
}

pub(crate) fn is_url(path: &str) -> bool {
    path.starts_with("https://") || path.starts_with("http://")
}

#[cfg(not(target_family = "wasm"))]
mod native {
    use std::{
        collections::HashMap,
        ffi::OsStr,
        fs, mem,
        path::Path,
        sync::{Arc, Mutex},
    };

    use tracing::error;

    use crate::{utils::is_url, Result};

    fn read_urdf(path: &str, xacro_args: &[(String, String)]) -> Result<(urdf_rs::Robot, String)> {
        let urdf_text = if Path::new(path).extension().and_then(OsStr::to_str) == Some("xacro") {
            urdf_rs::utils::convert_xacro_to_urdf_with_args(path, xacro_args)?
        } else if is_url(path) {
            ureq::get(path)
                .call()
                .map_err(|e| crate::Error::Other(e.to_string()))?
                .into_string()?
        } else {
            fs::read_to_string(path)?
        };
        let robot = urdf_rs::read_from_string(&urdf_text)?;
        Ok((robot, urdf_text))
    }

    #[derive(Debug)]
    pub struct RobotModel {
        pub(crate) path: String,
        pub(crate) urdf_text: Arc<Mutex<String>>,
        robot: urdf_rs::Robot,
        package_path: HashMap<String, String>,
        xacro_args: Vec<(String, String)>,
    }

    impl RobotModel {
        pub fn new(
            path: impl Into<String>,
            package_path: HashMap<String, String>,
            xacro_args: &[(String, String)],
        ) -> Result<Self> {
            let path = path.into();
            let (robot, urdf_text) = read_urdf(&path, xacro_args)?;
            Ok(Self {
                path,
                urdf_text: Arc::new(Mutex::new(urdf_text)),
                robot,
                package_path,
                xacro_args: xacro_args.to_owned(),
            })
        }

        pub async fn from_text(
            path: impl Into<String>,
            urdf_text: impl Into<String>,
            package_path: HashMap<String, String>,
        ) -> Result<Self> {
            let path = path.into();
            let urdf_text = urdf_text.into();
            let robot = urdf_rs::read_from_string(&urdf_text)?;
            Ok(Self {
                path,
                urdf_text: Arc::new(Mutex::new(urdf_text)),
                robot,
                package_path,
                xacro_args: Vec::new(),
            })
        }

        pub(crate) fn get(&mut self) -> &urdf_rs::Robot {
            &self.robot
        }

        pub(crate) fn reload(&mut self) {
            match read_urdf(&self.path, &self.xacro_args) {
                Ok((robot, text)) => {
                    self.robot = robot;
                    *self.urdf_text.lock().unwrap() = text;
                }
                Err(e) => {
                    error!("{e}");
                }
            }
        }

        pub(crate) fn take_package_path_map(&mut self) -> HashMap<String, String> {
            mem::take(&mut self.package_path)
        }
    }

    #[cfg(feature = "assimp")]
    /// http request -> write to tempfile -> return that file
    pub(crate) fn fetch_tempfile(url: &str) -> Result<tempfile::NamedTempFile> {
        use std::io::{Read, Write};

        const RESPONSE_SIZE_LIMIT: usize = 10 * 1_024 * 1_024;

        let mut buf: Vec<u8> = vec![];
        ureq::get(url)
            .call()
            .map_err(|e| crate::Error::Other(e.to_string()))?
            .into_reader()
            .take((RESPONSE_SIZE_LIMIT + 1) as u64)
            .read_to_end(&mut buf)?;
        if buf.len() > RESPONSE_SIZE_LIMIT {
            return Err(crate::Error::Other(format!("{url} is too big")));
        }
        let mut file = tempfile::NamedTempFile::new()?;
        file.write_all(&buf)?;
        Ok(file)
    }
}

#[cfg(target_family = "wasm")]
mod wasm {
    use std::{
        collections::HashMap,
        mem,
        path::Path,
        str,
        sync::{Arc, Mutex},
    };

    use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
    use js_sys::Uint8Array;
    use serde::{Deserialize, Serialize};
    use tracing::debug;
    use wasm_bindgen::JsCast;
    use wasm_bindgen_futures::JsFuture;
    use web_sys::Response;

    use crate::{utils::is_url, Error, Result};

    #[derive(Serialize, Deserialize)]
    pub(crate) struct Mesh {
        pub(crate) path: String,
        data: MeshData,
    }

    impl Mesh {
        pub(crate) fn decode(data: &str) -> Result<Self> {
            let mut mesh: Self = serde_json::from_str(data).map_err(|e| e.to_string())?;
            match &mesh.data {
                MeshData::None => {}
                MeshData::Base64(s) => {
                    mesh.data = MeshData::Bytes(BASE64.decode(s).map_err(|e| e.to_string())?);
                }
                MeshData::Bytes(_) => unreachable!(),
            }
            Ok(mesh)
        }

        pub(crate) fn bytes(&self) -> Option<&[u8]> {
            match &self.data {
                MeshData::None => None,
                MeshData::Bytes(bytes) => Some(bytes),
                MeshData::Base64(_) => unreachable!(),
            }
        }
    }

    #[derive(Serialize, Deserialize)]
    enum MeshData {
        Base64(String),
        Bytes(Vec<u8>),
        None,
    }

    pub fn window() -> Result<web_sys::Window> {
        Ok(web_sys::window().ok_or("failed to get window")?)
    }

    async fn fetch(input_file: &str) -> Result<Response> {
        let promise = window()?.fetch_with_str(input_file);

        let response = JsFuture::from(promise)
            .await?
            .dyn_into::<Response>()
            .unwrap();

        Ok(response)
    }

    pub async fn read_to_string(input_file: impl AsRef<str>) -> Result<String> {
        let promise = fetch(input_file.as_ref()).await?.text()?;

        let s = JsFuture::from(promise).await?;

        Ok(s.as_string()
            .ok_or_else(|| format!("{} is not string", input_file.as_ref()))?)
    }

    pub async fn read(input_file: impl AsRef<str>) -> Result<Vec<u8>> {
        let promise = fetch(input_file.as_ref()).await?.array_buffer()?;

        let bytes = JsFuture::from(promise).await?;

        Ok(Uint8Array::new(&bytes).to_vec())
    }

    pub async fn load_mesh(
        robot: &mut urdf_rs::Robot,
        urdf_path: impl AsRef<Path>,
        package_path: &HashMap<String, String>,
    ) -> Result<()> {
        let urdf_path = urdf_path.as_ref();
        for geometry in robot.links.iter_mut().flat_map(|link| {
            link.visual
                .iter_mut()
                .map(|v| &mut v.geometry)
                .chain(link.collision.iter_mut().map(|c| &mut c.geometry))
        }) {
            if let urdf_rs::Geometry::Mesh { filename, .. } = geometry {
                let input_file = if is_url(filename) {
                    filename.clone()
                } else if filename.starts_with("package://") {
                    crate::utils::replace_package_with_path(filename, package_path).ok_or_else(||
                        format!(
                            "ros package ({filename}) is not supported in wasm; consider using `package-path[]` URL parameter",
                        ))?
                } else if filename.starts_with("file://") {
                    return Err(Error::from(format!(
                        "local file ({filename}) is not supported in wasm",
                    )));
                } else {
                    // We don't use url::Url::path/set_path here, because
                    // urdf_path may be a relative path to a file bundled
                    // with the server. Path::with_file_name works for wasm
                    // where the separator is /, so we use it.
                    urdf_path
                        .with_file_name(&filename)
                        .to_str()
                        .unwrap()
                        .to_string()
                };

                debug!("loading {input_file}");
                let data = MeshData::Base64(BASE64.encode(read(&input_file).await?));

                let new = serde_json::to_string(&Mesh {
                    path: filename.clone(),
                    data,
                })
                .unwrap();
                *filename = new;
            }
        }
        Ok(())
    }

    #[derive(Debug)]
    pub struct RobotModel {
        pub(crate) path: String,
        pub(crate) urdf_text: Arc<Mutex<String>>,
        robot: urdf_rs::Robot,
        package_path: HashMap<String, String>,
    }

    impl RobotModel {
        pub async fn new(
            path: impl Into<String>,
            package_path: HashMap<String, String>,
        ) -> Result<Self> {
            let path = path.into();
            let urdf_text = read_to_string(&path).await?;
            Self::from_text(path, urdf_text, package_path).await
        }

        pub async fn from_text(
            path: impl Into<String>,
            urdf_text: impl Into<String>,
            package_path: HashMap<String, String>,
        ) -> Result<Self> {
            let path = path.into();
            let urdf_text = urdf_text.into();
            let mut robot = urdf_rs::read_from_string(&urdf_text)?;
            load_mesh(&mut robot, &path, &package_path).await?;
            Ok(Self {
                path,
                urdf_text: Arc::new(Mutex::new(urdf_text)),
                robot,
                package_path,
            })
        }

        pub(crate) fn get(&mut self) -> &urdf_rs::Robot {
            &self.robot
        }

        pub(crate) fn take_package_path_map(&mut self) -> HashMap<String, String> {
            mem::take(&mut self.package_path)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_replace_package_with_path() {
        let mut package_path = HashMap::new();
        package_path.insert("a".to_owned(), "path".to_owned());
        assert_eq!(
            replace_package_with_path("package://a/b/c", &package_path),
            Some("path/b/c".to_owned())
        );
        assert_eq!(
            replace_package_with_path("package://a", &package_path),
            None
        );
        assert_eq!(
            replace_package_with_path("package://b/b/c", &package_path),
            None
        );
        assert_eq!(replace_package_with_path("a", &package_path), None);
    }
}