gausplat_renderer/scene/gaussian_3d/
property.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! 3DGS property access implementation.

pub use super::*;

use burn::tensor::activation;
use humansize::{format_size, BINARY};

/// Outer property value getters
impl<B: Backend> Gaussian3dScene<B> {
    /// Colors in SH space. (Outer value)
    ///
    /// The shape is `[P, M * 3]`, which derives from `[P, M, 3]`.
    /// - `P` is [`Self::point_count`].
    /// - `M` is [`SH_COUNT_MAX`].
    ///
    /// It is represented as orthonormalized spherical harmonic with RGB channels.
    #[inline]
    pub fn get_colors_sh(&self) -> Tensor<B, 2> {
        Self::make_colors_sh(self.colors_sh.val())
    }

    /// Opacities. (Outer value)
    ///
    /// The shape is `[P, 1]`.
    ///
    /// They range from `0.0` to `1.0`.
    #[inline]
    pub fn get_opacities(&self) -> Tensor<B, 2> {
        Self::make_opacities(self.opacities.val())
    }

    /// 3D Positions. (Outer value)
    ///
    /// The shape is `[P, 3]`.
    #[inline]
    pub fn get_positions(&self) -> Tensor<B, 2> {
        Self::make_positions(self.positions.val())
    }

    /// Rotations. (Outer value)
    ///
    /// The shape is `[P, 4]`.
    ///
    /// They are represented as normalized Hamilton quaternions in scalar-last order,
    /// i.e., `[x, y, z, w]`.
    #[inline]
    pub fn get_rotations(&self) -> Tensor<B, 2> {
        Self::make_rotations(self.rotations.val())
    }

    /// 3D scalings. (Outer value)
    ///
    /// The shape is `[P, 3]`.
    #[inline]
    pub fn get_scalings(&self) -> Tensor<B, 2> {
        Self::make_scalings(self.scalings.val())
    }
}

/// Outer property value makers
impl<B: Backend> Gaussian3dScene<B> {
    /// Making values for [`Gaussian3dScene::get_colors_sh`]
    #[inline]
    pub fn make_colors_sh(colors_sh: Tensor<B, 2>) -> Tensor<B, 2> {
        colors_sh
    }

    /// Making values for [`Gaussian3dScene::get_opacities`]
    #[inline]
    pub fn make_opacities(opacities: Tensor<B, 2>) -> Tensor<B, 2> {
        activation::sigmoid(opacities)
    }

    /// Making values for [`Gaussian3dScene::get_positions`]
    #[inline]
    pub fn make_positions(positions: Tensor<B, 2>) -> Tensor<B, 2> {
        positions
    }

    /// Making values for [`Gaussian3dScene::get_rotations`]
    #[inline]
    pub fn make_rotations(rotations: Tensor<B, 2>) -> Tensor<B, 2> {
        rotations
            .to_owned()
            .div(rotations.powf_scalar(2.0).sum_dim(1).sqrt())
    }

    /// Making values for [`Gaussian3dScene::get_scalings`]
    #[inline]
    pub fn make_scalings(scalings: Tensor<B, 2>) -> Tensor<B, 2> {
        scalings.exp()
    }
}

/// Outer property value setters
impl<B: Backend> Gaussian3dScene<B> {
    /// Setting values for [`Gaussian3dScene::get_colors_sh`]
    pub fn set_colors_sh(
        &mut self,
        colors_sh: Tensor<B, 2>,
    ) -> &mut Self {
        self.set_inner_colors_sh(Self::make_inner_colors_sh(colors_sh));
        self
    }

    /// Setting values for [`Gaussian3dScene::get_opacities`]
    pub fn set_opacities(
        &mut self,
        opacities: Tensor<B, 2>,
    ) -> &mut Self {
        self.set_inner_opacities(Self::make_inner_opacities(opacities))
    }

    /// Setting values for [`Gaussian3dScene::get_positions`]
    pub fn set_positions(
        &mut self,
        positions: Tensor<B, 2>,
    ) -> &mut Self {
        self.set_inner_positions(Self::make_inner_positions(positions))
    }

    /// Setting values for [`Gaussian3dScene::get_rotations`]
    pub fn set_rotations(
        &mut self,
        rotations: Tensor<B, 2>,
    ) -> &mut Self {
        self.set_inner_rotations(Self::make_inner_rotations(rotations))
    }

    /// Setting values for [`Gaussian3dScene::get_scalings`]
    pub fn set_scalings(
        &mut self,
        scalings: Tensor<B, 2>,
    ) -> &mut Self {
        self.set_inner_scalings(Self::make_inner_scalings(scalings))
    }
}

/// Inner property value makers
impl<B: Backend> Gaussian3dScene<B> {
    /// Making values for [`Gaussian3dScene::colors_sh`]
    #[inline]
    pub fn make_inner_colors_sh(colors_sh: Tensor<B, 2>) -> Tensor<B, 2> {
        colors_sh
    }

    /// Making values for [`Gaussian3dScene::opacities`]
    #[inline]
    pub fn make_inner_opacities(opacities: Tensor<B, 2>) -> Tensor<B, 2> {
        opacities.to_owned().div(-opacities + 1.0).log()
    }

    /// Making values for [`Gaussian3dScene::positions`]
    #[inline]
    pub fn make_inner_positions(positions: Tensor<B, 2>) -> Tensor<B, 2> {
        positions
    }

    /// Making values for [`Gaussian3dScene::rotations`]
    #[inline]
    pub fn make_inner_rotations(rotations: Tensor<B, 2>) -> Tensor<B, 2> {
        rotations
    }

    /// Making values for [`Gaussian3dScene::scalings`]
    #[inline]
    pub fn make_inner_scalings(scalings: Tensor<B, 2>) -> Tensor<B, 2> {
        scalings.log()
    }
}

/// Inner property value setters
impl<B: Backend> Gaussian3dScene<B> {
    /// Setting inner values for [`Gaussian3dScene::colors_sh`]
    #[inline]
    pub fn set_inner_colors_sh(
        &mut self,
        colors_sh: Tensor<B, 2>,
    ) -> &mut Self {
        self.colors_sh = Param::initialized(self.colors_sh.id.to_owned(), colors_sh);
        self
    }

    /// Setting inner values for [`Gaussian3dScene::opacities`]
    #[inline]
    pub fn set_inner_opacities(
        &mut self,
        opacities: Tensor<B, 2>,
    ) -> &mut Self {
        self.opacities = Param::initialized(self.opacities.id.to_owned(), opacities);
        self
    }

    /// Setting inner values for [`Gaussian3dScene::positions`]
    #[inline]
    pub fn set_inner_positions(
        &mut self,
        positions: Tensor<B, 2>,
    ) -> &mut Self {
        self.positions = Param::initialized(self.positions.id.to_owned(), positions);
        self
    }

    /// Setting inner values for [`Gaussian3dScene::rotations`]
    #[inline]
    pub fn set_inner_rotations(
        &mut self,
        rotations: Tensor<B, 2>,
    ) -> &mut Self {
        self.rotations = Param::initialized(self.rotations.id.to_owned(), rotations);
        self
    }

    /// Setting inner values for [`Gaussian3dScene::scalings`]
    #[inline]
    pub fn set_inner_scalings(
        &mut self,
        scalings: Tensor<B, 2>,
    ) -> &mut Self {
        self.scalings = Param::initialized(self.scalings.id.to_owned(), scalings);
        self
    }
}

/// Attribute getters
impl<B: Backend> Gaussian3dScene<B> {
    /// The device.
    #[inline]
    pub fn device(&self) -> B::Device {
        self.devices().first().expect("A device").to_owned()
    }

    /// Number of points.
    #[inline]
    pub fn point_count(&self) -> usize {
        let point_count_target = self.colors_sh.dims()[0];
        let point_count_other = self.opacities.dims()[0];
        debug_assert_eq!(point_count_other, point_count_target);
        let point_count_other = self.positions.dims()[0];
        debug_assert_eq!(point_count_other, point_count_target);
        let point_count_other = self.rotations.dims()[0];
        debug_assert_eq!(point_count_other, point_count_target);
        let point_count_other = self.scalings.dims()[0];
        debug_assert_eq!(point_count_other, point_count_target);

        point_count_target
    }

    /// Size of the parameters in bytes.
    #[inline]
    pub fn size(&self) -> usize {
        self.num_params() * size_of::<B::FloatElem>()
    }

    /// Readable size of the parameters.
    #[inline]
    pub fn size_readable(&self) -> String {
        format_size(self.size(), BINARY.decimal_places(1))
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn set_outer_property() {
        use super::*;
        use burn::{backend::NdArray, tensor::Distribution};

        let device = Default::default();

        let input_colors_sh =
            Tensor::<NdArray<f32>, 2>::random([10, 48], Distribution::Default, &device);
        let input_rotations = Tensor::<NdArray<f32>, 2>::ones([10, 4], &device);
        let input_opacities =
            Tensor::<NdArray<f32>, 2>::random([10, 1], Distribution::Default, &device);
        let input_positions =
            Tensor::<NdArray<f32>, 2>::random([10, 3], Distribution::Default, &device);
        let input_scalings =
            Tensor::<NdArray<f32>, 2>::random([10, 3], Distribution::Default, &device)
                .add_scalar(1.0);

        let mut scene = Gaussian3dScene::<NdArray<f32>>::default();

        scene
            .set_colors_sh(input_colors_sh.to_owned())
            .set_opacities(input_opacities.to_owned())
            .set_positions(input_positions.to_owned())
            .set_rotations(input_rotations.to_owned())
            .set_scalings(input_scalings.to_owned());

        assert_eq!(scene.point_count(), 10);

        input_colors_sh
            .into_data()
            .assert_approx_eq(&scene.get_colors_sh().into_data(), 6);
        input_opacities
            .into_data()
            .assert_approx_eq(&scene.get_opacities().into_data(), 6);
        input_positions
            .into_data()
            .assert_approx_eq(&scene.get_positions().into_data(), 6);
        assert!(
            input_rotations
                .not_equal(scene.get_rotations())
                .all()
                .into_scalar(),
            "Rotations should be not equal to unnormalized ones"
        );
        input_scalings
            .into_data()
            .assert_approx_eq(&scene.get_scalings().into_data(), 6);
    }
}