Blender V5.3
IO_gsplat.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "BLI_assert.hh"
8#include "BLI_math_base.hh"
11#include "BLI_span.hh"
12#include "BLI_vector.hh"
13
14#include "BKE_attribute.hh"
15
16namespace blender {
17
18struct PointCloud;
19
20namespace io::gsplat {
21
43
44inline int degree_for_dimension(const int dimension)
45{
46 if (dimension < 3) {
47 return 0;
48 }
49 if (dimension < 8) {
50 return 1;
51 }
52 if (dimension < 15) {
53 return 2;
54 }
55 if (dimension < 24) {
56 return 3;
57 }
58 return 4;
59}
60
61inline int dimension_for_degree(const int degree)
62{
63 switch (degree) {
64 case 0:
65 return 0;
66 case 1:
67 return 3;
68 case 2:
69 return 8;
70 case 3:
71 return 15;
72 case 4:
73 return 24;
74 default:
75 return 0;
76 }
77}
78
79/* Get spherical harmonics for given point.
80 * Essentially a slice of all elements of the sh_attrs at the given index.
81 *
82 * The sh_attrs are used read-only. The reason it is a MutableSpan is for the ease of integration
83 * with the IO code that acquires attributes for write. */
85 const int point_index,
87{
88 BLI_assert(spherical_harmonics.size() == sh_attrs.size());
89 for (const int64_t i : sh_attrs.index_range()) {
90 spherical_harmonics[i] = sh_attrs[i][point_index];
91 }
92}
93
94/* Set spherical harmonics attributes for the given point. */
96 const int point_index,
98{
99 BLI_assert(spherical_harmonics.size() == sh_attrs.size());
100 for (const int k : sh_attrs.index_range()) {
101 sh_attrs[k][point_index] = spherical_harmonics[k];
102 }
103}
104
105/* Files that are saved using the original Gaussian Splatting paper are encoding opacity and scale
106 * in a special way: they save values before an activation functions are applied for fitting. These
107 * activation function needs to be applied in order to get an actually usable scale of the gaussian
108 * and its opacity.
109 *
110 * This class encapsulates implementation of these functions. Formulas are from supplemental code
111 * of the paper "Gaussian Point Splatting" (2026) by Joris Rijsdijk et. al.
112 * See gaussian_loader.cu, recordToCPUGaussian(). */
114 static float decode_opacity(const float opacity)
115 {
116 return math::clamp(1.0f / (1.0f + math::exp(-opacity)), 0.0f, 1.0f);
117 }
119 {
120 return math::exp(scale);
121 }
122};
123
124} // namespace io::gsplat
125} // namespace blender
MutableSpan< math::Quaternion > rotations_for_write()
Definition gsplat.cc:48
Span< MutableSpan< float3 > > sh_for_write()
Definition gsplat.cc:53
GsplatMutableAttributeAccessor(PointCloud &point_cloud, int sh_degrees)
Definition gsplat.cc:13
float3x3 scale(float3x3 mat, float2 scale)
int degree_for_dimension(const int dimension)
Definition IO_gsplat.hh:44
void get_spherical_harmonics(const Span< MutableSpan< float3 > > sh_attrs, const int point_index, const MutableSpan< float3 > spherical_harmonics)
Definition IO_gsplat.hh:84
void set_spherical_harmonics(const Span< MutableSpan< float3 > > sh_attrs, const int point_index, const Span< float3 > spherical_harmonics)
Definition IO_gsplat.hh:95
int dimension_for_degree(const int degree)
Definition IO_gsplat.hh:61
T clamp(const T &a, const T &min, const T &max)
T exp(const T &x)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
VecBase< float, 3 > float3
static float decode_opacity(const float opacity)
Definition IO_gsplat.hh:114
static float3 decode_scale(const float3 scale)
Definition IO_gsplat.hh:118