Skip to content

Commit

Permalink
Tweak code style
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 30, 2024
1 parent 7229d2c commit 9bed218
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/collada/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//! [COLLADA]: https://en.wikipedia.org/wiki/COLLADA
#![allow(clippy::wildcard_imports)] // TODO

mod geometry;
mod instance;
mod iter;
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
clippy::match_same_arms, // https://github.com/rust-lang/rust-clippy/issues/12044
clippy::missing_panics_doc,
clippy::must_use_candidate,
clippy::naive_bytecount,
clippy::unreadable_literal,
clippy::wildcard_imports, // TODO
)]

#[cfg(any(feature = "collada", feature = "obj", feature = "stl"))]
Expand Down
8 changes: 5 additions & 3 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ impl<B: AsRef<[u8]>> Loader<B> {
/// default reader does not support.
///
/// ```
/// use mesh_loader::Loader;
/// use std::fs;
///
/// use mesh_loader::Loader;
///
/// let loader = Loader::default().custom_reader(|path| {
/// match path.to_str() {
/// Some(url) if url.starts_with("http://") || url.starts_with("https://") => {
/// Some(url) if url.starts_with("https://") || url.starts_with("http://") => {
/// // Fetch online file
/// // ...
/// # unimplemented!()
Expand All @@ -80,9 +81,10 @@ impl<B: AsRef<[u8]>> Loader<B> {
/// This is useful when using mmap.
///
/// ```
/// use std::fs::File;
///
/// use memmap2::Mmap;
/// use mesh_loader::Loader;
/// use std::fs::File;
///
/// let loader = Loader::with_custom_reader(|path| unsafe { Mmap::map(&File::open(path)?) });
/// ```
Expand Down
20 changes: 11 additions & 9 deletions src/obj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn from_slice<B: AsRef<[u8]>, F: FnMut(&Path) -> io::Result<B>>(
) -> io::Result<Scene> {
// If it is UTF-16 with BOM, it is converted to UTF-8, otherwise it is parsed as bytes.
// We don't require UTF-8 here, as we want to support files that are partially non-UTF-8 like:
// https://github.com/assimp/assimp/blob/ac29847d5679c243d7649fe8a5d5e48f0f57c297/test/models/OBJ/regr01.mtl#L67
// https://github.com/assimp/assimp/blob/v5.3.1/test/models/OBJ/regr01.mtl#L67
let bytes = &decode_bytes(bytes)?;
match read_obj(bytes, path, &mut |path, materials, material_map| {
match reader(path) {
Expand Down Expand Up @@ -584,8 +584,9 @@ fn push_vertex(
.push(*normals.get(vn).ok_or(ErrorKind::Oob(vn, 0))?);
}
if !colors.is_empty() {
let color = colors.get(v).ok_or(ErrorKind::Oob(v, 0))?;
mesh.colors[0].push([color[0], color[1], color[2], 1.0]);
let rgb = colors.get(v).ok_or(ErrorKind::Oob(v, 0))?;
// a is 1 by default: https://github.com/assimp/assimp/blob/v5.3.1/code/AssetLib/Obj/ObjFileImporter.cpp#L233
mesh.colors[0].push([rgb[0], rgb[1], rgb[2], 1.]);
}
Ok(())
}
Expand Down Expand Up @@ -760,7 +761,7 @@ fn read_mtl_internal(
if skip_spaces(&mut s) {
let f = read_float1(&mut s, "Tr")?;
if let Some(mat) = &mut mat {
mat.alpha = Some(1.0 - f);
mat.alpha = Some(1. - f);
}
continue;
}
Expand Down Expand Up @@ -805,7 +806,7 @@ fn read_mtl_internal(
if skip_spaces(&mut s) {
let f = read_float1(&mut s, "Ni")?;
if let Some(mat) = &mut mat {
mat.ior = Some(f);
mat.index_of_refraction = Some(f);
}
continue;
}
Expand All @@ -817,8 +818,9 @@ fn read_mtl_internal(
let (name, s_next) = name(s);
if let Some(mat) = mat.replace(Material::default()) {
fn color4(color3: Option<[f32; 3]>) -> Option<Color4> {
let c = color3?;
Some([c[0], c[1], c[2], 1.0])
let rgb = color3?;
// a is 1 by default: https://github.com/assimp/assimp/blob/v5.3.1/code/AssetLib/Obj/ObjFileImporter.cpp#L233
Some([rgb[0], rgb[1], rgb[2], 1.])
}
fn texture_path(
texture: Option<&[u8]>,
Expand Down Expand Up @@ -1054,7 +1056,7 @@ fn read_texture<'a>(s: &mut &'a [u8], mat: &mut Option<Material<'a>>) -> bool {
} else if token(s, b"refl") {
if skip_spaces(s) {
let (_name, s_next) = name(s);
// ignore https://github.com/assimp/assimp/blob/ac29847d5679c243d7649fe8a5d5e48f0f57c297/code/AssetLib/Obj/ObjFileMtlImporter.cpp#L415
// ignore https://github.com/assimp/assimp/blob/v5.3.1/code/AssetLib/Obj/ObjFileMtlImporter.cpp#L415
*s = s_next;
return true;
}
Expand Down Expand Up @@ -1129,7 +1131,7 @@ struct Material<'a> {
alpha: Option<f32>,
shininess: Option<f32>,
illumination_model: Option<u8>,
ior: Option<f32>,
index_of_refraction: Option<f32>,
transparent: Option<[f32; 3]>,

roughness: Option<f32>,
Expand Down
6 changes: 3 additions & 3 deletions src/stl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn read_binary_header(bytes: &[u8], parse_color: bool) -> Result<BinaryHeader<'_
let expect = b"COLOR=";
while s.len() >= expect.len() + 4 {
if token(&mut s, expect) {
const INV_BYTE: f32 = 1.0 / 255.0;
const INV_BYTE: f32 = 1. / 255.;
reverse_color = true;
default_color = [
s[0] as f32 * INV_BYTE,
Expand Down Expand Up @@ -209,11 +209,11 @@ fn read_binary_triangles(header: &BinaryHeader<'_>) -> Mesh {
// Handling colors in STL is not standardized. We use the same way as assimp.
// https://github.com/assimp/assimp/blob/v5.3.1/code/AssetLib/STL/STLLoader.cpp#L502-L529
if triangle.color & has_color_mask != 0 {
const INV_VAL: f32 = 1.0 / 31.0;
const INV_VAL: f32 = 1. / 31.;
if mesh.colors[0].is_empty() {
mesh.colors[0] = vec![header.default_color; num_vertices];
}
let a = 1.0;
let a = 1.;
let color = if header.reverse_color {
let r = (triangle.color & 0x1f) as f32 * INV_VAL;
let g = ((triangle.color & (0x1f << 5)) >> 5) as f32 * INV_VAL;
Expand Down
8 changes: 5 additions & 3 deletions src/utils/float/lemire.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Implementation of the Eisel-Lemire algorithm.
use super::common::BiasedFp;
use super::float::RawFloat;
use super::table::{LARGEST_POWER_OF_FIVE, POWER_OF_FIVE_128, SMALLEST_POWER_OF_FIVE};
use super::{
common::BiasedFp,
float::RawFloat,
table::{LARGEST_POWER_OF_FIVE, POWER_OF_FIVE_128, SMALLEST_POWER_OF_FIVE},
};

/// Compute w * 10^q using an extended-precision float representation.
///
Expand Down
12 changes: 7 additions & 5 deletions src/utils/float/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ pub(crate) mod parse;
mod slow;
mod table;

use self::common::{BiasedFp, ByteSlice};
use self::float::RawFloat;
use self::lemire::compute_float;
use self::parse::{parse_inf_nan, parse_partial_number};
use self::slow::parse_long_mantissa;
use self::{
common::{BiasedFp, ByteSlice},
float::RawFloat,
lemire::compute_float,
parse::{parse_inf_nan, parse_partial_number},
slow::parse_long_mantissa,
};

#[inline]
pub fn parse<T: Float>(bytes: &[u8]) -> Option<T> {
Expand Down

0 comments on commit 9bed218

Please sign in to comment.