1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::utils::*;
use nalgebra_glm as glm;
use wgpu;
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct VoxelPointer {
pub start: u32,
pub length: u32,
}
unsafe impl bytemuck::Zeroable for VoxelPointer {}
unsafe impl bytemuck::Pod for VoxelPointer {}
pub struct VoxelGrid {
pub bb_min: glm::Vec3,
pub bb_max: glm::Vec3,
pub bb_diff: glm::Vec3,
pub bb_size: glm::Vec3,
pub voxel_length: f32,
pub voxels: wgpu::Buffer,
pub voxels_len: usize,
pub voxel_pointers: wgpu::Buffer,
pub voxel_pointers_len: usize,
}
impl VoxelGrid {
pub fn new(device: &wgpu::Device, radius_max: f32, mut atoms: Vec<glm::Vec4>) -> Self {
let solvent_radius_max = 2.0;
let voxel_length = 2.0 * radius_max + 2.0 * solvent_radius_max;
let mut bb_max = glm::vec3(std::f32::NEG_INFINITY, std::f32::NEG_INFINITY, std::f32::NEG_INFINITY);
let mut bb_min = glm::vec3(std::f32::INFINITY, std::f32::INFINITY, std::f32::INFINITY);
for atom in atoms.iter() {
bb_max = glm::max2(&bb_max, &glm::vec4_to_vec3(atom));
bb_min = glm::min2(&bb_min, &glm::vec4_to_vec3(atom));
}
bb_min -= glm::vec3(1.0, 1.0, 1.0);
bb_max += glm::vec3(1.0, 1.0, 1.0);
let bb_center = (bb_max + bb_min) / 2.0;
bb_max = bb_max - bb_center;
bb_min = bb_min - bb_center;
for atom in atoms.iter_mut() {
atom.x -= bb_center.x;
atom.y -= bb_center.y;
atom.z -= bb_center.z;
}
bb_max.apply(|e| e.round_to_multiple(voxel_length as i32));
bb_min.apply(|e| e.round_to_multiple(voxel_length as i32));
let bb_diff = bb_max - bb_min;
let bb_size = bb_diff.apply_into(|e| e.abs() / voxel_length as f32);
let mut voxels_nested: Vec<Vec<glm::Vec4>> = vec![Vec::new(); (bb_size.x * bb_size.y * bb_size.z) as usize];
for atom in atoms.iter() {
let grid_position_vec3 = (atom.xyz() - bb_min) / voxel_length;
let grid_position_ivec3 = glm::vec3(
grid_position_vec3.x as i32,
grid_position_vec3.y as i32,
grid_position_vec3.z as i32,
);
let bb_size = glm::vec3(bb_size.x as i32, bb_size.y as i32, bb_size.z as i32);
let width = bb_size.x;
let height = bb_size.y;
let x = grid_position_ivec3.x;
let y = grid_position_ivec3.y;
let z = grid_position_ivec3.z;
let index = (width * height * z) + (width * y) + x;
voxels_nested[index as usize].push(glm::vec4(atom.x, atom.y, atom.z, 1.0));
}
let mut voxels: Vec<f32> = Vec::new();
let mut voxel_pointers = Vec::new();
let mut count = 0;
for voxel in voxels_nested.iter_mut() {
voxel_pointers.push(VoxelPointer {
start: count,
length: voxel.len() as u32,
});
count += voxel.len() as u32;
for v in voxel {
voxels.push(v[0]);
voxels.push(v[1]);
voxels.push(v[2]);
voxels.push(v[3]);
}
}
let voxels_len = voxels.len() as usize;
let voxels = device.create_buffer_with_data(bytemuck::cast_slice(&voxels), wgpu::BufferUsage::STORAGE_READ);
let voxel_pointers_len = voxel_pointers.len() as usize;
let voxel_pointers = device.create_buffer_with_data(bytemuck::cast_slice(&voxel_pointers), wgpu::BufferUsage::STORAGE_READ);
Self {
bb_min,
bb_max,
bb_diff,
bb_size,
voxel_length,
voxels,
voxels_len,
voxel_pointers,
voxel_pointers_len,
}
}
}