Skip to content

Commit

Permalink
Support URL with assimp enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Oct 5, 2023
1 parent 9cf51a7 commit fec2f4d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tracing::*;
#[cfg(not(target_family = "wasm"))]
fn load_mesh_assimp(
file_string: &str,
data: &str,
scale: na::Vector3<f32>,
opt_urdf_color: &Option<na::Point3<f32>>,
group: &mut SceneNode,
Expand All @@ -23,7 +24,7 @@ fn load_mesh_assimp(
importer.pre_transform_vertices(|x| x.enable = true);
importer.collada_ignore_up_direction(true);
let (meshes, textures, colors) =
convert_assimp_scene_to_kiss3d_mesh(&importer.read_file(file_string)?);
convert_assimp_scene_to_kiss3d_mesh(&importer.read_string(data)?);
info!(
"num mesh, texture, colors = {} {} {}",
meshes.len(),
Expand Down Expand Up @@ -102,8 +103,19 @@ pub fn load_mesh(
let file_string = filename.as_ref();

#[cfg(feature = "assimp")]
if use_assimp && !file_string.starts_with("https://") && !file_string.starts_with("http://") {
return load_mesh_assimp(file_string, scale, opt_color, group, use_texture);
if use_assimp {
let is_url = file_string.starts_with("https://") || file_string.starts_with("http://");
let buf = if is_url {
crate::utils::fetch_read(file_string)?
} else {
use std::io::Read;
let mut file = std::fs::File::open(file_string)?;
let mut buf = vec![];
file.read_to_end(&mut buf)?;
buf
};
let data = String::from_utf8(buf)?;
return load_mesh_assimp(file_string, &data, scale, opt_color, group, use_texture);
}

let filename = Path::new(file_string);
Expand Down
20 changes: 20 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ mod native {
mem::take(&mut self.package_path)
}
}

#[cfg(feature = "assimp")]
/// http request -> return content
pub fn fetch_read(url: &str) -> Result<Vec<u8>> {
use std::io::Read;

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")));
}
Ok(buf)
}
}

#[cfg(target_family = "wasm")]
Expand Down

0 comments on commit fec2f4d

Please sign in to comment.