| Name | Message | Date |
|---|---|---|
| 📄 camera.rs | 2 hours ago | |
| 📄 ellipsoid.rs | 2 hours ago | |
| 📄 lib.rs | 2 hours ago | |
| 📄 rig.rs | 3 hours ago | |
| 📄 source.rs | 2 hours ago | |
| 📄 splat.rs | 3 hours ago |
📄
rig/src/splat.rs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//! The per-frame Gaussian list: footprint-sized anisotropic surfel splats //! (see PLAN.md, "Core design"). //! //! A surfel is a flat disk aligned to the depth-map normal: //! //! * two tangential scales = `depth * pixel_size / focal_length` //! (pixel size 1 px, focal in pixels ⇒ `depth / fx`), //! * a few-mm thickness along the normal, //! * opacity ~0.9–0.99, //! * rendered with front-to-back alpha blending (Brush's renderer). use glam::{Mat3, Quat, Vec3}; use crate::camera::CameraIntrinsics; /// Thickness along the normal for surfels taken from depth data, in /// metres ("a few mm", PLAN.md). pub const SURFEL_THICKNESS_M: f32 = 0.003; /// One Gaussian splat, in the raw (non-log, non-logit) parameterization /// the pipeline reasons about. Brush's log/logit/SH conversions happen at /// the render boundary, not here. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Splat { /// Centre position in world space, metres. pub mean: Vec3, /// Orientation: maps local (X, Y, Z) to the splat's axes. For surfels /// local Z is the surface normal. Stored as a glam quaternion (x, y, /// z, w) — the layout Brush expects. pub rotation: Quat, /// Per-axis scale in metres (not log scale). pub scale: Vec3, /// Colour, RGB in `[0, 1]`. pub color: [f32; 3], /// Opacity in `[0, 1]` (not logit). pub opacity: f32, } impl Splat { /// A footprint-sized surfel from a depth-map sample. /// /// * `position` — the back-projected depth pixel's 3D position, /// * `normal` — the surface normal at that position (unit length), /// * `depth` — the pixel's depth (distance along the camera's viewing /// axis), used for the footprint, /// * `intrinsics` — the depth camera's intrinsics, for the /// `depth / focal` footprint from PLAN.md, /// * `thickness` — extent along the normal in metres. pub fn surfel( position: Vec3, normal: Vec3, depth: f32, intrinsics: &CameraIntrinsics, thickness: f32, color: [f32; 3], opacity: f32, ) -> Self { let normal = normal.normalize_or_zero(); // Footprint: one pixel's world size at this depth. let footprint_x = depth / intrinsics.fx; let footprint_y = depth / intrinsics.fy; let rotation = surfel_rotation(normal); Self { mean: position, rotation, scale: Vec3::new(footprint_x, footprint_y, thickness), color, opacity: opacity.clamp(0.0, 1.0), } } /// The splat's normal axis in world space (its local +Z). pub fn normal(&self) -> Vec3 { self.rotation * Vec3::Z } } /// A flat disk oriented so local Z is `normal`: local (X, Y) span the /// tangent plane, right-handed. fn surfel_rotation(normal: Vec3) -> Quat { // Reference axis not parallel to the normal (world up, or world X // for near-vertical normals). let reference = if normal.abs().dot(Vec3::Y) > 0.999 { Vec3::X } else { Vec3::Y }; let t1 = normal.cross(reference).normalize_or_zero(); let t2 = normal.cross(t1).normalize_or_zero(); Quat::from_mat3(&Mat3::from_cols(t1, t2, normal)) } /// A frame's Gaussian list. Constructed fresh every frame — there is no /// training (PLAN.md, "Core design") — so this is a plain growable /// collection. #[derive(Debug, Clone, Default, PartialEq)] pub struct SplatBatch { splats: Vec<Splat>, } impl SplatBatch { pub fn new() -> Self { Self::default() } pub fn with_capacity(capacity: usize) -> Self { Self { splats: Vec::with_capacity(capacity), } } pub fn len(&self) -> usize { self.splats.len() } pub fn is_empty(&self) -> bool { self.splats.is_empty() } pub fn push(&mut self, splat: Splat) { self.splats.push(splat); } pub fn clear(&mut self) { self.splats.clear(); } pub fn iter(&self) -> impl Iterator<Item = &Splat> { self.splats.iter() } /// The centroid of the splat means (world space). Useful for aiming /// debug views at the subject. pub fn centroid(&self) -> Option<Vec3> { if self.splats.is_empty() { return None; } let sum = self.splats.iter().fold(Vec3::ZERO, |acc, s| acc + s.mean); Some(sum / self.splats.len() as f32) } } impl<'a> IntoIterator for &'a SplatBatch { type Item = &'a Splat; type IntoIter = std::slice::Iter<'a, Splat>; fn into_iter(self) -> Self::IntoIter { self.splats.iter() } } #[cfg(test)] mod tests { use super::*; use glam::vec3; fn intrinsics() -> CameraIntrinsics { CameraIntrinsics { fx: 500.0, fy: 500.0, cx: 256.0, cy: 212.0, } } #[test] fn surfel_footprint_is_depth_over_focal() { // PLAN.md: two tangential scales = depth * pixel_size / focal. let s = Splat::surfel( vec3(0.0, 0.0, 2.0), vec3(0.0, 0.0, -1.0), 2.0, &intrinsics(), SURFEL_THICKNESS_M, [1.0, 0.0, 0.0], 0.95, ); assert!((s.scale.x - 2.0 / 500.0).abs() < 1e-6); assert!((s.scale.y - 2.0 / 500.0).abs() < 1e-6); // Thickness along the normal, not the footprint. assert!((s.scale.z - SURFEL_THICKNESS_M).abs() < 1e-6); assert!((s.normal() - vec3(0.0, 0.0, -1.0)).length() < 1e-5); assert_eq!(s.opacity, 0.95); } #[test] fn surfel_rotation_maps_local_z_to_normal() { for normal in [ vec3(0.0, 0.0, -1.0), vec3(0.0, 1.0, 0.0), vec3(1.0, 0.0, 0.0), vec3(0.707, 0.707, 0.0), vec3(0.577, 0.577, 0.577), vec3(0.0, -1.0, 0.0), // near the world-up fallback axis ] { let n = normal.normalize(); let q = surfel_rotation(n); // Local Z maps to the normal; the quaternion is a proper // rotation (orthonormal basis). assert!((q * Vec3::Z - n).length() < 1e-5, "normal {n:?}"); assert!((q.length() - 1.0).abs() < 1e-5); } } #[test] fn surfel_opacity_is_clamped() { let s = Splat::surfel( Vec3::ZERO, vec3(0.0, 0.0, -1.0), 1.0, &intrinsics(), SURFEL_THICKNESS_M, [0.0; 3], 1.4, ); assert_eq!(s.opacity, 1.0); } #[test] fn batch_basics() { let mut batch = SplatBatch::with_capacity(4); assert!(batch.is_empty()); assert_eq!(batch.centroid(), None); batch.push(Splat::surfel( vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, -1.0), 2.0, &intrinsics(), SURFEL_THICKNESS_M, [1.0; 3], 0.9, )); batch.push(Splat::surfel( vec3(0.0, 3.0, 0.0), vec3(0.0, 0.0, -1.0), 2.0, &intrinsics(), SURFEL_THICKNESS_M, [1.0; 3], 0.9, )); assert_eq!(batch.len(), 2); assert_eq!(batch.centroid(), Some(vec3(0.5, 1.5, 0.0))); batch.clear(); assert!(batch.is_empty()); } }