Skip to content

Commit

Permalink
Merge pull request #18 from asukaminato0721/some-refactor
Browse files Browse the repository at this point in the history
remove some allow && do some refactor
  • Loading branch information
nobuyuki83 authored Jan 7, 2025
2 parents 796d4a9 + a698cdf commit e5d7a4b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
4 changes: 4 additions & 0 deletions del-geo-core/src/mat3_col_major.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ where
fn transpose(&self) -> Self;
fn mult_mat_col_major(&self, other: &Self) -> Self;
fn mult_vec(&self, vec: &[T; 3]) -> [T; 3];
fn transform_homogeneous(&self, x: &[T; 2]) -> Option<[T; 2]>;
}

impl<Real> Mat3ColMajor<Real> for [Real; 9]
Expand Down Expand Up @@ -38,6 +39,9 @@ where
fn mult_vec(&self, vec: &[Real; 3]) -> [Real; 3] {
mult_vec(self, vec)
}
fn transform_homogeneous(&self, x: &[Real; 2]) -> Option<[Real; 2]> {
transform_homogeneous(self, x)
}
}

use std::ops::AddAssign;
Expand Down
4 changes: 2 additions & 2 deletions del-geo-core/src/obb3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ fn test_nearest_to_point3() {
for _iter in 0..100 {
let eps = 1.0e-4;
let dp = [-eps, -eps, -eps, eps, eps, eps].sample(&mut reng);
let q = std::array::from_fn(|i| p_near[i] + dp[i]);
let q = nearest_to_point3(&obb, &q);
let q = p_near.add(&dp);
let q = obb.nearest_to_point3(&q);
let len0 = p.length(&p_near);
let len1 = p.length(&q);
assert!(len0 <= len1);
Expand Down
2 changes: 1 addition & 1 deletion del-geo-core/src/polynomial_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ where
}
let dfr = c1 + two * c2 * r + three * c3 * r * r;
r = r - fr / dfr;
r = num_traits::clamp(r, T::zero(), t);
r = r.clamp(T::zero(), t);
}
if r < T::zero() || r > t {
return None;
Expand Down
43 changes: 23 additions & 20 deletions del-geo-core/src/spherical_harmonics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![allow(clippy::needless_range_loop)]
#![allow(clippy::comparison_chain)]

use num_complex::{Complex, ComplexFloat};
use std::f64::consts::PI;
use std::{cmp::Ordering, f64::consts::PI};

/// Calculate the normalization of the vector.
#[inline]
Expand Down Expand Up @@ -331,6 +328,7 @@ fn calculate_assoc_legendre_poly(l: u64, m: i64, x: f64) -> f64 {
let legendre = legendre_coeff_vec(l);
let legendre_coeff_vec = &legendre[l as usize];
let mut sum_coeff = Vec::new();
#[allow(clippy::needless_range_loop)]
for i in m_abs as usize..=l as usize {
let mut tmp = legendre_coeff_vec[i];
tmp *= factorial(i as u128) as f64 / factorial((i as u32 - m_abs as u32).into()) as f64;
Expand Down Expand Up @@ -362,22 +360,27 @@ pub fn get_spherical_harmonics_coeff(l: i64, m: i64, x: f64, y: f64, z: f64) ->
assert!(l >= 0 && l >= m_abs);
let r = f64::sqrt(x * x + y * y);
let ep = Complex::new(x / r, y / r);
if m == 0 {
f64::sqrt((2.0 * l as f64 + 1.0) / 4.0 / PI) * calculate_assoc_legendre_poly(l as u64, 0, z)
} else if m < 0 {
f64::sqrt(
(2.0 * l as f64 + 1.0) / (4.0 * PI)
* factorial((l as u32 - m_abs as u32).into()) as f64
/ factorial((l as u32 + m_abs as u32).into()) as f64,
) * calculate_assoc_legendre_poly(l as u64, m_abs, z)
* ep.powf(m_abs as f64).im()
} else {
f64::sqrt(
(2.0 * l as f64 + 1.0) * factorial((l as u32 - m_abs as u32).into()) as f64
/ factorial((l as u32 + m_abs as u32).into()) as f64
/ (4.0 * PI),
) * calculate_assoc_legendre_poly(l as u64, m_abs, z)
* ep.powf(m_abs as f64).re()
match m.cmp(&0) {
Ordering::Equal => {
f64::sqrt((2.0 * l as f64 + 1.0) / 4.0 / PI)
* calculate_assoc_legendre_poly(l as u64, 0, z)
}
Ordering::Less => {
f64::sqrt(
(2.0 * l as f64 + 1.0) / (4.0 * PI)
* factorial((l as u32 - m_abs as u32).into()) as f64
/ factorial((l as u32 + m_abs as u32).into()) as f64,
) * calculate_assoc_legendre_poly(l as u64, m_abs, z)
* ep.powf(m_abs as f64).im()
}
_ => {
f64::sqrt(
(2.0 * l as f64 + 1.0) * factorial((l as u32 - m_abs as u32).into()) as f64
/ factorial((l as u32 + m_abs as u32).into()) as f64
/ (4.0 * PI),
) * calculate_assoc_legendre_poly(l as u64, m_abs, z)
* ep.powf(m_abs as f64).re()
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions del-geo-core/src/view_projection.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::mat4_col_major::Mat4ColMajor;

pub struct Perspective {
pub lens: f32,
pub near: f32,
Expand All @@ -23,8 +25,7 @@ impl Perspective {
-self.cam_pos[2],
]);
let scale = crate::mat4_col_major::from_scale_uniform(self.scale);
let ts = crate::mat4_col_major::mult_mat(&transl, &scale);
crate::mat4_col_major::mult_mat(&cam_projection, &ts)
cam_projection.mult_mat(&transl.mult_mat(&scale))
}

pub fn camera_translation(&mut self, asp: f32, cursor_dx: f32, cursor_dy: f32) {
Expand Down
8 changes: 5 additions & 3 deletions del-geo-core/src/view_rotation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::quaternion::Quaternion;

pub struct Trackball {
pub quaternion: [f32; 4],
}
Expand All @@ -10,7 +12,7 @@ impl Trackball {
}

pub fn mat4_col_major(&self) -> [f32; 16] {
crate::quaternion::to_mat4_col_major(&self.quaternion)
self.quaternion.to_mat4_col_major()
}

pub fn camera_rotation(&mut self, cursor_dx: f64, cursor_dy: f64) {
Expand All @@ -20,8 +22,8 @@ impl Trackball {
if a == 0.0 {
return;
}
let dq = crate::quaternion::normalized(&crate::quaternion::from_axisangle(&[-dy, dx, 0.]));
self.quaternion = crate::quaternion::mult_quaternion(&dq, &self.quaternion);
let dq = crate::quaternion::from_axisangle(&[-dy, dx, 0.]).normalized();
self.quaternion = dq.mult_quaternion(&self.quaternion);
}
}

Expand Down

0 comments on commit e5d7a4b

Please sign in to comment.