|
| 1 | +use nalgebra::Point3; |
| 2 | + |
| 3 | +use crate::{float_types::Real, mesh::vertex::Vertex}; |
| 4 | + |
| 5 | +impl Vertex { |
| 6 | + /// **Mathematical Foundation: Barycentric Linear Interpolation** |
| 7 | + /// |
| 8 | + /// Compute the barycentric linear interpolation between `self` (`t = 0`) and `other` (`t = 1`). |
| 9 | + /// This implements the fundamental linear interpolation formula: |
| 10 | + /// |
| 11 | + /// ## **Interpolation Formula** |
| 12 | + /// For parameter t ∈ [0,1]: |
| 13 | + /// - **Position**: p(t) = (1-t)·p₀ + t·p₁ = p₀ + t·(p₁ - p₀) |
| 14 | + /// - **Normal**: n(t) = (1-t)·n₀ + t·n₁ = n₀ + t·(n₁ - n₀) |
| 15 | + /// |
| 16 | + /// ## **Mathematical Properties** |
| 17 | + /// - **Affine Combination**: Coefficients sum to 1: (1-t) + t = 1 |
| 18 | + /// - **Endpoint Preservation**: p(0) = p₀, p(1) = p₁ |
| 19 | + /// - **Linearity**: Second derivatives are zero (straight line in parameter space) |
| 20 | + /// - **Convexity**: Result lies on line segment between endpoints |
| 21 | + /// |
| 22 | + /// ## **Geometric Interpretation** |
| 23 | + /// The interpolated vertex represents a point on the edge connecting the two vertices, |
| 24 | + /// with both position and normal vectors smoothly blended. This is fundamental for: |
| 25 | + /// - **Polygon Splitting**: Creating intersection vertices during BSP operations |
| 26 | + /// - **Triangle Subdivision**: Generating midpoints for mesh refinement |
| 27 | + /// - **Smooth Shading**: Interpolating normals across polygon edges |
| 28 | + /// |
| 29 | + /// **Note**: Normals are linearly interpolated (not spherically), which is appropriate |
| 30 | + /// for most geometric operations but may require renormalization for lighting calculations. |
| 31 | + pub fn interpolate(&self, other: &Vertex, t: Real) -> Vertex { |
| 32 | + // For positions (Point3): p(t) = p0 + t * (p1 - p0) |
| 33 | + let new_pos = self.pos + (other.pos - self.pos) * t; |
| 34 | + |
| 35 | + // For normals (Vector3): n(t) = n0 + t * (n1 - n0) |
| 36 | + let new_normal = self.normal + (other.normal - self.normal) * t; |
| 37 | + Vertex::new(new_pos, new_normal) |
| 38 | + } |
| 39 | + |
| 40 | + /// **Mathematical Foundation: Spherical Linear Interpolation (SLERP) for Normals** |
| 41 | + /// |
| 42 | + /// Compute spherical linear interpolation for normal vectors, preserving unit length: |
| 43 | + /// |
| 44 | + /// ## **SLERP Formula** |
| 45 | + /// For unit vectors n₀, n₁ and parameter t ∈ [0,1]: |
| 46 | + /// ```text |
| 47 | + /// slerp(n₀, n₁, t) = (sin((1-t)·Ω) · n₀ + sin(t·Ω) · n₁) / sin(Ω) |
| 48 | + /// ``` |
| 49 | + /// Where Ω = arccos(n₀ · n₁) is the angle between vectors. |
| 50 | + /// |
| 51 | + /// ## **Mathematical Properties** |
| 52 | + /// - **Arc Interpolation**: Follows great circle on unit sphere |
| 53 | + /// - **Constant Speed**: Angular velocity is constant |
| 54 | + /// - **Unit Preservation**: Result is always unit length |
| 55 | + /// - **Orientation**: Shortest path between normals |
| 56 | + /// |
| 57 | + /// This is preferred over linear interpolation for normal vectors in lighting |
| 58 | + /// calculations and smooth shading applications. |
| 59 | + pub fn slerp_interpolate(&self, other: &Vertex, t: Real) -> Vertex { |
| 60 | + // Linear interpolation for position |
| 61 | + let new_pos = self.pos + (other.pos - self.pos) * t; |
| 62 | + |
| 63 | + // Spherical linear interpolation for normals |
| 64 | + let n0 = self.normal.normalize(); |
| 65 | + let n1 = other.normal.normalize(); |
| 66 | + |
| 67 | + let dot = n0.dot(&n1).clamp(-1.0, 1.0); |
| 68 | + |
| 69 | + // If normals are nearly parallel, use linear interpolation |
| 70 | + if (dot.abs() - 1.0).abs() < Real::EPSILON { |
| 71 | + let new_normal = (self.normal + (other.normal - self.normal) * t).normalize(); |
| 72 | + return Vertex::new(new_pos, new_normal); |
| 73 | + } |
| 74 | + |
| 75 | + let omega = dot.acos(); |
| 76 | + let sin_omega = omega.sin(); |
| 77 | + |
| 78 | + if sin_omega.abs() < Real::EPSILON { |
| 79 | + // Fallback to linear interpolation |
| 80 | + let new_normal = (self.normal + (other.normal - self.normal) * t).normalize(); |
| 81 | + return Vertex::new(new_pos, new_normal); |
| 82 | + } |
| 83 | + |
| 84 | + let a = ((1.0 - t) * omega).sin() / sin_omega; |
| 85 | + let b = (t * omega).sin() / sin_omega; |
| 86 | + |
| 87 | + let new_normal = (a * n0 + b * n1).normalize(); |
| 88 | + Vertex::new(new_pos, new_normal) |
| 89 | + } |
| 90 | + |
| 91 | + /// **Mathematical Foundation: Barycentric Coordinates Interpolation** |
| 92 | + /// |
| 93 | + /// Interpolate vertex using barycentric coordinates (u, v, w) with u + v + w = 1: |
| 94 | + /// ```text |
| 95 | + /// p = u·p₁ + v·p₂ + w·p₃ |
| 96 | + /// n = normalize(u·n₁ + v·n₂ + w·n₃) |
| 97 | + /// ``` |
| 98 | + /// |
| 99 | + /// This is fundamental for triangle interpolation and surface parameterization. |
| 100 | + pub fn barycentric_interpolate( |
| 101 | + v1: &Vertex, |
| 102 | + v2: &Vertex, |
| 103 | + v3: &Vertex, |
| 104 | + u: Real, |
| 105 | + v: Real, |
| 106 | + w: Real, |
| 107 | + ) -> Vertex { |
| 108 | + // Ensure barycentric coordinates sum to 1 (normalize if needed) |
| 109 | + let total = u + v + w; |
| 110 | + let (u, v, w) = if total.abs() > Real::EPSILON { |
| 111 | + (u / total, v / total, w / total) |
| 112 | + } else { |
| 113 | + (1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0) // Fallback to centroid |
| 114 | + }; |
| 115 | + |
| 116 | + let new_pos = Point3::from(u * v1.pos.coords + v * v2.pos.coords + w * v3.pos.coords); |
| 117 | + |
| 118 | + let new_normal = (u * v1.normal + v * v2.normal + w * v3.normal).normalize(); |
| 119 | + |
| 120 | + Vertex::new(new_pos, new_normal) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[cfg(test)] |
| 125 | +mod test { |
| 126 | + use nalgebra::{Point3, Vector3}; |
| 127 | + |
| 128 | + use crate::mesh::vertex::Vertex; |
| 129 | + |
| 130 | + fn create_vertices() -> [Vertex; 2] { |
| 131 | + [ |
| 132 | + Vertex::new(Point3::new(0.0, 0.0, 0.0), Vector3::x()), |
| 133 | + Vertex::new(Point3::new(2.0, 2.0, 2.0), Vector3::y()), |
| 134 | + ] |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn linear() { |
| 139 | + let [v1, v2] = create_vertices(); |
| 140 | + |
| 141 | + // Test linear interpolation |
| 142 | + let mid_linear = v1.interpolate(&v2, 0.5); |
| 143 | + assert!( |
| 144 | + (mid_linear.pos - Point3::new(1.0, 1.0, 1.0)).norm() < 1e-10, |
| 145 | + "Linear interpolation midpoint should be (1,1,1)" |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn barycentric() { |
| 151 | + let v1 = Vertex::new(Point3::new(0.0, 0.0, 0.0), Vector3::x()); |
| 152 | + let v2 = Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::y()); |
| 153 | + let v3 = Vertex::new(Point3::new(0.0, 1.0, 0.0), Vector3::z()); |
| 154 | + |
| 155 | + // Test centroid (equal weights) |
| 156 | + let centroid = |
| 157 | + Vertex::barycentric_interpolate(&v1, &v2, &v3, 1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0); |
| 158 | + let expected_pos = Point3::new(1.0 / 3.0, 1.0 / 3.0, 0.0); |
| 159 | + assert!( |
| 160 | + (centroid.pos - expected_pos).norm() < 1e-10, |
| 161 | + "Barycentric centroid should be at (1/3, 1/3, 0)" |
| 162 | + ); |
| 163 | + |
| 164 | + // Test vertex recovery (weight=1 for one vertex) |
| 165 | + let recovered_v1 = Vertex::barycentric_interpolate(&v1, &v2, &v3, 1.0, 0.0, 0.0); |
| 166 | + assert!( |
| 167 | + (recovered_v1.pos - v1.pos).norm() < 1e-10, |
| 168 | + "Barycentric should recover original vertex" |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + /// Test spherical interpolation |
| 174 | + fn slerp() { |
| 175 | + let [v1, v2] = create_vertices(); |
| 176 | + |
| 177 | + let mid_slerp = v1.slerp_interpolate(&v2, 0.5); |
| 178 | + assert!( |
| 179 | + (mid_slerp.pos - Point3::new(1.0, 1.0, 1.0)).norm() < 1e-10, |
| 180 | + "SLERP position should match linear for positions" |
| 181 | + ); |
| 182 | + |
| 183 | + // Normal should be normalized and between the two normals |
| 184 | + assert!( |
| 185 | + (mid_slerp.normal.norm() - 1.0).abs() < 1e-10, |
| 186 | + "SLERP normal should be unit length" |
| 187 | + ); |
| 188 | + } |
| 189 | +} |
0 commit comments