use nalgebra as na;
use oxigaf_flame::Mesh;
use crate::gaussian::GaussianModel;
#[derive(Debug)]
pub struct BindingResult {
pub positions: Vec<[f32; 3]>,
pub face_frames: Vec<FaceFrame>,
}
#[derive(Debug, Clone, Copy)]
pub struct FaceFrame {
pub tangent: na::Vector3<f32>,
pub bitangent: na::Vector3<f32>,
pub normal: na::Vector3<f32>,
}
pub fn apply_binding(model: &GaussianModel, mesh: &Mesh) -> BindingResult {
let n = model.len();
let mut positions = Vec::with_capacity(n);
let mut face_frames = Vec::with_capacity(n);
for i in 0..n {
let face_idx = model.face_indices[i] as usize;
let bary = model.barycentric[i];
let offset = model.local_offsets[i];
let face = &mesh.faces[face_idx.min(mesh.faces.len().saturating_sub(1))];
let v0 = &mesh.vertices[face[0] as usize];
let v1 = &mesh.vertices[face[1] as usize];
let v2 = &mesh.vertices[face[2] as usize];
let base_pos = v0.coords * bary[0] + v1.coords * bary[1] + v2.coords * bary[2];
let edge0 = v1 - v0;
let edge1 = v2 - v0;
let face_normal = edge0.cross(&edge1);
let normal_len = face_normal.norm();
let frame = if normal_len > 1e-10 {
let normal = face_normal / normal_len;
let tangent = edge0.normalize();
let bitangent = normal.cross(&tangent);
FaceFrame {
tangent,
bitangent,
normal,
}
} else {
FaceFrame {
tangent: na::Vector3::x(),
bitangent: na::Vector3::y(),
normal: na::Vector3::z(),
}
};
let world_offset =
frame.tangent * offset[0] + frame.bitangent * offset[1] + frame.normal * offset[2];
let pos = base_pos + world_offset;
positions.push([pos.x, pos.y, pos.z]);
face_frames.push(frame);
}
BindingResult {
positions,
face_frames,
}
}
pub fn position_regularization(
current_positions: &[[f32; 3]],
binding_positions: &[[f32; 3]],
) -> f32 {
current_positions
.iter()
.zip(binding_positions.iter())
.map(|(cur, bind)| {
let dx = cur[0] - bind[0];
let dy = cur[1] - bind[1];
let dz = cur[2] - bind[2];
dx * dx + dy * dy + dz * dz
})
.sum()
}
pub fn scale_regularization(scales: &[[f32; 3]], max_scale: f32) -> f32 {
scales
.iter()
.map(|s| {
let excess_x = (s[0].exp() - max_scale).max(0.0);
let excess_y = (s[1].exp() - max_scale).max(0.0);
let excess_z = (s[2].exp() - max_scale).max(0.0);
excess_x * excess_x + excess_y * excess_y + excess_z * excess_z
})
.sum()
}
pub fn frame_to_quaternion(frame: &FaceFrame) -> [f32; 4] {
let rot = na::Matrix3::from_columns(&[frame.tangent, frame.bitangent, frame.normal]);
let uq = na::UnitQuaternion::from_rotation_matrix(&na::Rotation3::from_matrix_unchecked(rot));
let q = uq.quaternion();
[q.i, q.j, q.k, q.w]
}
pub struct FlameBindingBuffers {
pub binding_info: wgpu::Buffer,
pub tbn_frames: wgpu::Buffer,
pub position_grads: wgpu::Buffer,
pub offset_grads: wgpu::Buffer,
pub uniforms: wgpu::Buffer,
pub bind_group: wgpu::BindGroup,
num_gaussians: usize,
num_vertices: usize,
}
impl FlameBindingBuffers {
pub fn new(
device: &wgpu::Device,
bind_group_layout: &wgpu::BindGroupLayout,
num_gaussians: usize,
num_vertices: usize,
) -> Self {
use wgpu::util::DeviceExt;
let binding_info_size = (num_gaussians * 16) as u64; let binding_info = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("flame_binding_info"),
size: binding_info_size,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let tbn_frame_size = (num_vertices * 48) as u64; let tbn_frames = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("flame_tbn_frames"),
size: tbn_frame_size,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let position_grads_size = (num_gaussians * 16) as u64; let position_grads = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("flame_position_grads"),
size: position_grads_size,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let offset_grads_size = (num_gaussians * 16) as u64;
let offset_grads = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("flame_offset_grads"),
size: offset_grads_size,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let uniforms_data = [num_gaussians as u32, 0, 0, 0]; let uniforms = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("flame_binding_uniforms"),
contents: bytemuck::cast_slice(&uniforms_data),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("flame_binding_bwd_bind_group"),
layout: bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: uniforms.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: binding_info.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: tbn_frames.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: position_grads.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 4,
resource: offset_grads.as_entire_binding(),
},
],
});
Self {
binding_info,
tbn_frames,
position_grads,
offset_grads,
uniforms,
bind_group,
num_gaussians,
num_vertices,
}
}
pub fn update_binding_info(&self, queue: &wgpu::Queue, vertex_ids: &[u32]) {
let mut data = Vec::with_capacity(self.num_gaussians * 4);
for &vid in vertex_ids.iter().take(self.num_gaussians) {
data.extend_from_slice(&[vid, 0, 0, 0]);
}
queue.write_buffer(&self.binding_info, 0, bytemuck::cast_slice(&data));
}
pub fn update_tbn_frames(&self, queue: &wgpu::Queue, face_frames: &[FaceFrame]) {
let mut data = Vec::with_capacity(face_frames.len() * 12);
for frame in face_frames.iter().take(self.num_vertices) {
data.extend_from_slice(&[frame.tangent.x, frame.tangent.y, frame.tangent.z, 0.0]);
data.extend_from_slice(&[frame.bitangent.x, frame.bitangent.y, frame.bitangent.z, 0.0]);
data.extend_from_slice(&[frame.normal.x, frame.normal.y, frame.normal.z, 0.0]);
}
queue.write_buffer(&self.tbn_frames, 0, bytemuck::cast_slice(&data));
}
pub fn update_position_gradients(&self, queue: &wgpu::Queue, gradients: &[[f32; 3]]) {
let mut data = Vec::with_capacity(self.num_gaussians * 4);
for grad in gradients.iter().take(self.num_gaussians) {
data.extend_from_slice(&[grad[0], grad[1], grad[2], 0.0]);
}
queue.write_buffer(&self.position_grads, 0, bytemuck::cast_slice(&data));
}
pub fn bind_group(&self) -> &wgpu::BindGroup {
&self.bind_group
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_position_reg_zero_for_matching() {
let positions = vec![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
let binding = vec![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
assert!((position_regularization(&positions, &binding)).abs() < 1e-10);
}
#[test]
fn test_scale_reg_zero_under_max() {
let scales = vec![[0.0, 0.0, 0.0]];
assert!((scale_regularization(&scales, 2.0)).abs() < 1e-10);
}
}