Skip to content

Commit

Permalink
Fixed Clippy Warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
davids91 committed Jun 27, 2024
1 parent 2f5e583 commit d0f100d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 72 deletions.
7 changes: 2 additions & 5 deletions src/octree/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ pub(in crate::octree) fn bound_contains(bounds: &Cube, position: &V3c<f32>) -> b
/// Returns with the octant value(i.e. index) of the child for the given position
pub(in crate::octree) fn child_octant_for(bounds: &Cube, position: &V3c<f32>) -> u8 {
debug_assert!(bound_contains(bounds, position));
hash_region(
&(*position - bounds.min_position).into(),
bounds.size as f32,
)
hash_region(&(*position - bounds.min_position), bounds.size)
}

///####################################################################################
Expand Down Expand Up @@ -334,7 +331,7 @@ impl<T: Default + Clone + VoxelData, const DIM: usize> Octree<T, DIM> {
0
}
};
(((leaf_occupied_bits & 0x0000000000330033) > 0) as u8) << 0
(((leaf_occupied_bits & 0x0000000000330033) > 0) as u8)
| (((leaf_occupied_bits & 0x0000000000cc00cc) > 0) as u8) << 1
| (((leaf_occupied_bits & 0x0033003300000000) > 0) as u8) << 2
| (((leaf_occupied_bits & 0x00cc00cc00000000) > 0) as u8) << 3
Expand Down
2 changes: 1 addition & 1 deletion src/octree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<T: Default + PartialEq + Clone + VoxelData, const DIM: usize> Octree<T, DIM
if DIM > size as usize || 0 == size || (size as f32 / DIM as f32).log(2.0).fract() != 0.0 {
return Err(OctreeError::InvalidNodeSize(size));
}
let mut nodes = ObjectPool::<NodeContent<T, DIM>>::with_capacity(DIM as usize);
let mut nodes = ObjectPool::<NodeContent<T, DIM>>::with_capacity(DIM);
let mut node_children = Vec::with_capacity(size.pow(3) as usize);
node_children.push(NodeChildren::new(empty_marker()));
let root_node_key = nodes.push(NodeContent::Nothing); // The first element is the root Node
Expand Down
41 changes: 19 additions & 22 deletions src/octree/raytracing/raytracing_on_cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use crate::spatial::{
raytracing::{
cube_impact_normal,
lut::{OOB_OCTANT, RAY_TO_LEAF_OCCUPANCY_BITMASK_LUT, RAY_TO_NODE_OCCUPANCY_BITMASK_LUT},
Ray,
Ray, FLOAT_ERROR_TOLERANCE,
},
FLOAT_ERROR_TOLERANCE,
};

#[derive(Debug)]
Expand Down Expand Up @@ -71,12 +70,12 @@ impl<T: Default + PartialEq + Clone + std::fmt::Debug + VoxelData, const DIM: us
) -> V3c<f32> {
let p = ray.point_at(*ray_current_distance);
let steps_needed = V3c::new(
p.x - current_bounds.min_position.x as f32
- (current_bounds.size as f32 * ray.direction.x.signum().max(0.)),
p.y - current_bounds.min_position.y as f32
- (current_bounds.size as f32 * ray.direction.y.signum().max(0.)),
p.z - current_bounds.min_position.z as f32
- (current_bounds.size as f32 * ray.direction.z.signum().max(0.)),
p.x - current_bounds.min_position.x
- (current_bounds.size * ray.direction.x.signum().max(0.)),
p.y - current_bounds.min_position.y
- (current_bounds.size * ray.direction.y.signum().max(0.)),
p.z - current_bounds.min_position.z
- (current_bounds.size * ray.direction.z.signum().max(0.)),
);

let d_x = *ray_current_distance + (steps_needed.x * ray_scale_factors.x).abs();
Expand All @@ -86,17 +85,17 @@ impl<T: Default + PartialEq + Clone + std::fmt::Debug + VoxelData, const DIM: us

V3c::new(
if (*ray_current_distance - d_x).abs() < FLOAT_ERROR_TOLERANCE {
(current_bounds.size as f32).copysign(ray.direction.x)
(current_bounds.size).copysign(ray.direction.x)
} else {
0.
},
if (*ray_current_distance - d_y).abs() < FLOAT_ERROR_TOLERANCE {
(current_bounds.size as f32).copysign(ray.direction.y)
(current_bounds.size).copysign(ray.direction.y)
} else {
0.
},
if (*ray_current_distance - d_z).abs() < FLOAT_ERROR_TOLERANCE {
(current_bounds.size as f32).copysign(ray.direction.z)
(current_bounds.size).copysign(ray.direction.z)
} else {
0.
},
Expand Down Expand Up @@ -164,9 +163,9 @@ impl<T: Default + PartialEq + Clone + std::fmt::Debug + VoxelData, const DIM: us
if bitmap_position_full_resolution != prev_bitmap_position_full_resolution {
prev_bitmap_position_full_resolution = bitmap_position_full_resolution;
let start_pos_in_bitmap = flat_projection(
bitmap_position_full_resolution.x as usize,
bitmap_position_full_resolution.y as usize,
bitmap_position_full_resolution.z as usize,
bitmap_position_full_resolution.x,
bitmap_position_full_resolution.y,
bitmap_position_full_resolution.z,
4,
);
if 0 == (RAY_TO_LEAF_OCCUPANCY_BITMASK_LUT[start_pos_in_bitmap]
Expand Down Expand Up @@ -194,16 +193,14 @@ impl<T: Default + PartialEq + Clone + std::fmt::Debug + VoxelData, const DIM: us
#[cfg(debug_assertions)]
{
let relative_point =
ray.point_at(*ray_current_distance) - V3c::from(current_bounds.min_position);
ray.point_at(*ray_current_distance) - current_bounds.min_position;
debug_assert!(
(relative_point.x < FLOAT_ERROR_TOLERANCE
|| (relative_point.x - current_bounds.size as f32) < FLOAT_ERROR_TOLERANCE)
|| (relative_point.x - current_bounds.size) < FLOAT_ERROR_TOLERANCE)
|| (relative_point.y < FLOAT_ERROR_TOLERANCE
|| (relative_point.y - current_bounds.size as f32)
< FLOAT_ERROR_TOLERANCE)
|| (relative_point.y - current_bounds.size) < FLOAT_ERROR_TOLERANCE)
|| (relative_point.z < FLOAT_ERROR_TOLERANCE
|| (relative_point.z - current_bounds.size as f32)
< FLOAT_ERROR_TOLERANCE)
|| (relative_point.z - current_bounds.size) < FLOAT_ERROR_TOLERANCE)
);
}
}
Expand Down Expand Up @@ -243,8 +240,8 @@ impl<T: Default + PartialEq + Clone + std::fmt::Debug + VoxelData, const DIM: us
if let Some(root_hit) = root_bounds.intersect_ray(&ray) {
ray_current_distance = root_hit.impact_distance.unwrap_or(0.);
let target_octant = hash_region(
&(ray.point_at(ray_current_distance) - root_bounds.min_position.into()),
root_bounds.size as f32,
&(ray.point_at(ray_current_distance) - root_bounds.min_position),
root_bounds.size,
);
node_stack.push(NodeStackItem::new(
root_bounds,
Expand Down
19 changes: 9 additions & 10 deletions src/octree/raytracing/tests.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::octree::{Cube, V3c};
use crate::spatial::raytracing::Ray;
use crate::spatial::{raytracing::plane_line_intersection, FLOAT_ERROR_TOLERANCE};
use crate::spatial::raytracing::{plane_line_intersection, FLOAT_ERROR_TOLERANCE};

/// Reference implementation to decide step to sibling boundary
#[allow(dead_code)]
pub(crate) fn get_step_to_next_sibling(current: &Cube, ray: &Ray) -> V3c<f32> {
//Find the point furthest from the ray
let midpoint = V3c::unit((current.size / 2.0) as f32) + current.min_position.into();
let midpoint = V3c::unit(current.size / 2.0) + current.min_position;
let ref_point = midpoint
+ V3c::new(
(current.size as f32 / 2.).copysign(ray.direction.x),
(current.size as f32 / 2.).copysign(ray.direction.y),
(current.size as f32 / 2.).copysign(ray.direction.z),
(current.size / 2.).copysign(ray.direction.x),
(current.size / 2.).copysign(ray.direction.y),
(current.size / 2.).copysign(ray.direction.z),
);

// Find the min of the 3 plane intersections
Expand Down Expand Up @@ -41,17 +41,17 @@ pub(crate) fn get_step_to_next_sibling(current: &Cube, ray: &Ray) -> V3c<f32> {
// Step along the axes with the minimum distances
V3c::new(
if (min_d - x_plane_distance).abs() < FLOAT_ERROR_TOLERANCE {
(current.size as f32).copysign(ray.direction.x)
(current.size).copysign(ray.direction.x)
} else {
0.
},
if (min_d - y_plane_distance).abs() < FLOAT_ERROR_TOLERANCE {
(current.size as f32).copysign(ray.direction.y)
(current.size).copysign(ray.direction.y)
} else {
0.
},
if (min_d - z_plane_distance).abs() < FLOAT_ERROR_TOLERANCE {
(current.size as f32).copysign(ray.direction.z)
(current.size).copysign(ray.direction.z)
} else {
0.
},
Expand All @@ -70,8 +70,7 @@ mod wgpu_tests {
#[cfg(test)]
mod octree_raytracing_tests {
use crate::octree::{raytracing::tests::get_step_to_next_sibling, Albedo, Cube, Octree, V3c};
use crate::spatial::raytracing::Ray;
use crate::spatial::FLOAT_ERROR_TOLERANCE;
use crate::spatial::raytracing::{Ray, FLOAT_ERROR_TOLERANCE};

use rand::{rngs::ThreadRng, Rng};

Expand Down
17 changes: 3 additions & 14 deletions src/octree/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ impl VoxelData for Albedo {
}

fn albedo(&self) -> Albedo {
self.clone()
*self
}

fn user_data(&self) -> u32 {
0u32.into()
0u32
}

fn clear(&mut self) {
Expand Down Expand Up @@ -103,25 +103,14 @@ pub struct Octree<T: Default + Clone + VoxelData, const DIM: usize = 1> {
pub(in crate::octree) node_children: Vec<NodeChildren<u32>>, // Children index values of each Node
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Default, Clone, Copy, Debug, PartialEq)]
pub struct Albedo {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}

impl Default for Albedo {
fn default() -> Self {
Self {
r: 0,
g: 0,
b: 0,
a: 0,
}
}
}

impl Albedo {
pub fn with_red(mut self, r: u8) -> Self {
self.r = r;
Expand Down
8 changes: 4 additions & 4 deletions src/octree/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl<T: Default + PartialEq + Clone + VoxelData, const DIM: usize> Octree<T, DIM
clear_size: u32,
) -> Result<(), OctreeError> {
let position = V3c::<f32>::from(*position);
let root_bounds = Cube::root_bounds(self.octree_size as f32 as f32);
let root_bounds = Cube::root_bounds(self.octree_size as f32);
if !bound_contains(&root_bounds, &position) {
return Err(OctreeError::InvalidPosition {
x: position.x as u32,
Expand Down Expand Up @@ -360,9 +360,9 @@ impl<T: Default + PartialEq + Clone + VoxelData, const DIM: usize> Octree<T, DIM
// If the child of this node was set to NodeContent::Nothing during this clear operation
// it needs to be freed up, and the child index of this node needs to be updated as well
let child_octant = hash_region(
&(V3c::from(child_bounds.min_position - node_bounds.min_position)
+ V3c::unit(child_bounds.size as f32 / 2.)),
node_bounds.size as f32,
&((child_bounds.min_position - node_bounds.min_position)
+ V3c::unit(child_bounds.size / 2.)),
node_bounds.size,
) as usize;
self.node_children[node_key as usize].clear(child_octant);
self.nodes.free(child_key);
Expand Down
2 changes: 0 additions & 2 deletions src/spatial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub mod raytracing;

use crate::spatial::math::{offset_region, vector::V3c};

pub(crate) const FLOAT_ERROR_TOLERANCE: f32 = 0.00001;

#[derive(Default, Clone, Copy, Debug)]
#[cfg_attr(
feature = "serialization",
Expand Down
16 changes: 8 additions & 8 deletions src/spatial/raytracing/lut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ fn generate_lut_64_bits() -> [[u64; 8]; 64] {
}
let direction_position =
hash_direction(&V3c::new(dx as f32, dy as f32, dz as f32));
let moved_x = (x + dx * 4).max(0).min(3);
let moved_y = (y + dy * 4).max(0).min(3);
let moved_z = (z + dz * 4).max(0).min(3);
let moved_x = (x + dx * 4).clamp(0, 3);
let moved_y = (y + dy * 4).clamp(0, 3);
let moved_z = (z + dz * 4).clamp(0, 3);
let min_x = moved_x.min(x);
let max_x = moved_x.max(x);
let min_y = moved_y.min(y);
Expand All @@ -72,7 +72,7 @@ fn generate_lut_64_bits() -> [[u64; 8]; 64] {
}
}
}
bitmap_lut[bitmask_position as usize][direction_position as usize] =
bitmap_lut[bitmask_position][direction_position as usize] =
result_bitmask;
}
}
Expand Down Expand Up @@ -103,9 +103,9 @@ fn generate_lut_8_bits() -> [[u8; 8]; 8] {
}
let direction_position =
hash_direction(&V3c::new(dx as f32, dy as f32, dz as f32));
let moved_x = (x + dx * 2).max(0).min(2);
let moved_y = (y + dy * 2).max(0).min(2);
let moved_z = (z + dz * 2).max(0).min(2);
let moved_x = (x + dx * 2).clamp(0, 2);
let moved_y = (y + dy * 2).clamp(0, 2);
let moved_z = (z + dz * 2).clamp(0, 2);
let min_x = moved_x.min(x);
let max_x = moved_x.max(x);
let min_y = moved_y.min(y);
Expand Down Expand Up @@ -160,7 +160,7 @@ fn generate_octant_step_result_lut() {
if center_after_step.x < 0.
|| center_after_step.x > SPACE_SIZE
|| center_after_step.y < 0.
|| center_after_step.z > SPACE_SIZE
|| center_after_step.y > SPACE_SIZE
|| center_after_step.z < 0.
|| center_after_step.z > SPACE_SIZE
{
Expand Down
13 changes: 7 additions & 6 deletions src/spatial/raytracing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::spatial::{math::vector::V3c, raytracing::lut::OCTANT_STEP_RESULT_LUT,
pub mod lut;
mod tests;

pub(crate) const FLOAT_ERROR_TOLERANCE: f32 = 0.00001;

#[derive(Debug)]
pub struct Ray {
pub origin: V3c<f32>,
Expand Down Expand Up @@ -32,12 +34,12 @@ impl Cube {
pub fn intersect_ray(&self, ray: &Ray) -> Option<CubeRayIntersection> {
debug_assert!(ray.is_valid());

let max_position = V3c::<f32>::from(self.min_position) + V3c::unit(self.size as f32);
let t1 = (self.min_position.x as f32 - ray.origin.x) / ray.direction.x;
let max_position = self.min_position + V3c::unit(self.size);
let t1 = (self.min_position.x - ray.origin.x) / ray.direction.x;
let t2 = (max_position.x - ray.origin.x) / ray.direction.x;
let t3 = (self.min_position.y as f32 - ray.origin.y) / ray.direction.y;
let t3 = (self.min_position.y - ray.origin.y) / ray.direction.y;
let t4 = (max_position.y - ray.origin.y) / ray.direction.y;
let t5 = (self.min_position.z as f32 - ray.origin.z) / ray.direction.z;
let t5 = (self.min_position.z - ray.origin.z) / ray.direction.z;
let t6 = (max_position.z - ray.origin.z) / ray.direction.z;

let tmin = t1.min(t2).max(t3.min(t4)).max(t5.min(t6));
Expand Down Expand Up @@ -98,8 +100,7 @@ pub fn plane_line_intersection(
}

pub fn cube_impact_normal(cube: &Cube, impact_point: &V3c<f32>) -> V3c<f32> {
let mid_to_impact =
V3c::from(cube.min_position) + V3c::unit(cube.size as f32 / 2.) - *impact_point;
let mid_to_impact = cube.min_position + V3c::unit(cube.size / 2.) - *impact_point;
let max_component = mid_to_impact
.x
.abs()
Expand Down

0 comments on commit d0f100d

Please sign in to comment.