Blender V5.3
io_ply_import_gsplat_test.cc
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#include "testing/testing.h"
6
7#include <algorithm>
8#include <cstring>
9#include <string>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_listbase.hh"
16
18
19#include "BKE_attribute.hh"
20#include "BKE_gtest_setup.hh"
21#include "BKE_lib_id.hh"
22#include "BKE_report.hh"
23
25
26#include "IO_gsplat.hh"
27#include "IO_ply.hh"
28
29#include "intern/ply_data.hh"
30#include "ply_import_gsplat.hh"
31
32namespace blender::io::ply {
33
34/* Deterministic gaussian splat PLY data, built fully in memory.
35 *
36 * The f_rest_<i> attribute values encode their (channel, coefficient, point) index, so that any
37 * confusion of the channel-major layout in the importer produces values that fail comparison
38 * loudly rather than shifting silently. */
40 public:
42 /* Total number of f_rest_<i> attributes, i.e. 3x the per-channel coefficient count for
43 * consistent files. */
45
50
51 float3 position(const int point) const
52 {
53 return float3(1.0f, 2.0f, 3.0f) * float(point + 1);
54 }
55 float f_dc(const int point, const int channel) const
56 {
57 return 0.5f + channel + 0.25f * point;
58 }
59 float opacity(const int point) const
60 {
61 return 0.25f * point - 0.5f;
62 }
63 float scale(const int point, const int axis) const
64 {
65 return -1.0f + 0.5f * axis + 0.125f * point;
66 }
67 float rotation(const int point, const int component) const
68 {
69 return (component == 0) ? 1.0f : 0.0625f * point;
70 }
71 float f_rest(const int point, const int attribute_index) const
72 {
73 /* Channel-major layout: the per-channel coefficient count of the file itself determines
74 * which (channel, coefficient) pair an attribute holds. */
75 const int num_file_coefficients = std::max(num_rest_attributes / 3, 1);
76 const int channel = attribute_index / num_file_coefficients;
77 const int coefficient = attribute_index % num_file_coefficients;
78 return 100.0f * channel + coefficient + 0.25f * point;
79 }
80 /* The value the importer is expected to store for a given SH coefficient. */
81 float3 expected_sh(const int point, const int coefficient) const
82 {
83 return float3(coefficient + 0.25f * point,
84 100.0f + coefficient + 0.25f * point,
85 200.0f + coefficient + 0.25f * point);
86 }
87
88 PlyData build() const
89 {
91 for (int point = 0; point < num_points; point++) {
92 data.vertices.append(position(point));
93 }
94
95 auto add_attribute = [&](const std::string &name, auto value_for_point) {
97 for (int point = 0; point < num_points; point++) {
98 attribute.data[point] = value_for_point(point);
99 }
100 data.vertex_custom_attr.append(attribute);
101 };
102
103 for (int channel = 0; channel < 3; channel++) {
104 add_attribute("f_dc_" + std::to_string(channel),
105 [&](const int point) { return f_dc(point, channel); });
106 }
107 add_attribute("opacity", [&](const int point) { return opacity(point); });
108 for (int axis = 0; axis < 3; axis++) {
109 add_attribute("scale_" + std::to_string(axis),
110 [&](const int point) { return scale(point, axis); });
111 }
112 for (int component = 0; component < 4; component++) {
113 add_attribute("rot_" + std::to_string(component),
114 [&](const int point) { return rotation(point, component); });
115 }
116 for (int i = 0; i < num_rest_attributes; i++) {
117 add_attribute("f_rest_" + std::to_string(i),
118 [&](const int point) { return f_rest(point, i); });
119 }
120
121 return data;
122 }
123};
124
125class io_ply_import_gsplat : public testing::Test {
126 public:
127 static void SetUpTestSuite()
128 {
130 }
131 static void TearDownTestSuite()
132 {
134 }
135
136 void SetUp() override
137 {
140 }
141
142 void TearDown() override
143 {
145 }
146
147 bool has_warning_containing(const char *needle)
148 {
149 char *message = BKE_reports_string(&reports_, RPT_WARNING);
150 if (message == nullptr) {
151 return false;
152 }
153 const bool found = strstr(message, needle) != nullptr;
154 MEM_delete(message);
155 return found;
156 }
157
159 {
161 }
162
163 static int count_sh_attributes(const PointCloud &point_cloud)
164 {
165 const bke::AttributeAccessor attributes = point_cloud.attributes();
166 int count = 0;
167 while (attributes.contains("radiance:sh_" + std::to_string(count))) {
168 count++;
169 }
170 return count;
171 }
172
173 void expect_sh_attributes(const PointCloud &point_cloud,
174 const GsplatPlyDataBuilder &builder,
175 const int expected_num_coefficients)
176 {
177 EXPECT_EQ(count_sh_attributes(point_cloud), expected_num_coefficients);
178
179 const bke::AttributeAccessor attributes = point_cloud.attributes();
180 for (int coefficient = 0; coefficient < expected_num_coefficients; coefficient++) {
181 const bke::AttributeReader<float3> sh = attributes.lookup<float3>(
182 "radiance:sh_" + std::to_string(coefficient), bke::AttrDomain::Point);
183 ASSERT_TRUE(sh);
184 for (int point = 0; point < builder.num_points; point++) {
185 const float3 value = sh.varray[point];
186 const float3 expected = builder.expected_sh(point, coefficient);
187 for (int channel = 0; channel < 3; channel++) {
188 EXPECT_FLOAT_EQ(value[channel], expected[channel])
189 << "coefficient " << coefficient << " channel " << channel << " point " << point;
190 }
191 }
192 }
193 }
194
195 protected:
198};
199
200TEST_F(io_ply_import_gsplat, missing_required_attributes)
201{
203 data.vertices.append(float3(0.0f));
204 PointCloud *point_cloud = convert_gsplat_ply_to_point_cloud(data, params_);
205 EXPECT_EQ(point_cloud, nullptr);
206}
207
208TEST_F(io_ply_import_gsplat, basic_attributes)
209{
210 const GsplatPlyDataBuilder builder(3, 9);
211 PointCloud *point_cloud = convert_gsplat_ply_to_point_cloud(builder.build(), params_);
212 ASSERT_NE(point_cloud, nullptr);
213 ASSERT_EQ(point_cloud->totpoint, 3);
215
216 const Span<float3> positions = point_cloud->positions();
217 const bke::AttributeAccessor attributes = point_cloud->attributes();
218 const bke::AttributeReader<float4> radiance_base = attributes.lookup<float4>(
219 "radiance:base", bke::AttrDomain::Point);
220 const bke::AttributeReader<float3> scales = attributes.lookup<float3>("scale",
223 "rotation", bke::AttrDomain::Point);
224 ASSERT_TRUE(radiance_base);
225 ASSERT_TRUE(scales);
226 ASSERT_TRUE(rotations);
227
228 for (int point = 0; point < builder.num_points; point++) {
229 const float4 base = radiance_base.varray[point];
230 const float3 scale = scales.varray[point];
231 const math::Quaternion rotation = rotations.varray[point];
232 for (int axis = 0; axis < 3; axis++) {
233 EXPECT_FLOAT_EQ(positions[point][axis], builder.position(point)[axis]);
234 EXPECT_FLOAT_EQ(base[axis], builder.f_dc(point, axis));
235 EXPECT_FLOAT_EQ(
236 scale[axis],
238 builder.scale(point, 0), builder.scale(point, 1), builder.scale(point, 2)))[axis]);
239 }
240 EXPECT_FLOAT_EQ(base.w,
242 /* The importer normalizes the rotation. */
243 const math::Quaternion expected_rotation = math::normalize(
244 math::Quaternion(builder.rotation(point, 0),
245 builder.rotation(point, 1),
246 builder.rotation(point, 2),
247 builder.rotation(point, 3)));
248 EXPECT_FLOAT_EQ(rotation.w, expected_rotation.w);
249 EXPECT_FLOAT_EQ(rotation.x, expected_rotation.x);
250 EXPECT_FLOAT_EQ(rotation.y, expected_rotation.y);
251 EXPECT_FLOAT_EQ(rotation.z, expected_rotation.z);
252 }
253
254 BKE_id_free(nullptr, &point_cloud->id);
255}
256
257TEST_F(io_ply_import_gsplat, canonical_counts_import_all_coefficients)
258{
259 for (const int num_coefficients : {0, 3, 8, 15, 24}) {
260 const GsplatPlyDataBuilder builder(2, num_coefficients * 3);
261 PointCloud *point_cloud = convert_gsplat_ply_to_point_cloud(builder.build(), params_);
262 ASSERT_NE(point_cloud, nullptr);
263
264 expect_sh_attributes(*point_cloud, builder, num_coefficients);
265 EXPECT_FALSE(has_any_report()) << "for coefficient count " << num_coefficients;
266
267 BKE_id_free(nullptr, &point_cloud->id);
268 }
269}
270
271TEST_F(io_ply_import_gsplat, non_canonical_count_keeps_channels)
272{
273 /* 4 coefficients per channel: only the first complete band (3 coefficients, degree 1) can be
274 * imported, and the values must still come from the right channels of the file's own
275 * channel-major layout. */
276 const GsplatPlyDataBuilder builder(2, 4 * 3);
277 PointCloud *point_cloud = convert_gsplat_ply_to_point_cloud(builder.build(), params_);
278 ASSERT_NE(point_cloud, nullptr);
279
280 expect_sh_attributes(*point_cloud, builder, 3);
281 EXPECT_TRUE(has_warning_containing("spherical harmonics"));
282
283 BKE_id_free(nullptr, &point_cloud->id);
284}
285
286TEST_F(io_ply_import_gsplat, mid_band_count_keeps_channels)
287{
288 /* 17 coefficients per channel: degrees up to 3 (15 coefficients) are complete. */
289 const GsplatPlyDataBuilder builder(2, 17 * 3);
290 PointCloud *point_cloud = convert_gsplat_ply_to_point_cloud(builder.build(), params_);
291 ASSERT_NE(point_cloud, nullptr);
292
293 expect_sh_attributes(*point_cloud, builder, 15);
294 EXPECT_TRUE(has_warning_containing("spherical harmonics"));
295
296 BKE_id_free(nullptr, &point_cloud->id);
297}
298
299TEST_F(io_ply_import_gsplat, count_not_multiple_of_three_drops_sh)
300{
301 const GsplatPlyDataBuilder builder(2, 7);
302 PointCloud *point_cloud = convert_gsplat_ply_to_point_cloud(builder.build(), params_);
303 ASSERT_NE(point_cloud, nullptr);
304
305 EXPECT_EQ(count_sh_attributes(*point_cloud), 0);
306 EXPECT_TRUE(has_warning_containing("spherical harmonics"));
307
308 BKE_id_free(nullptr, &point_cloud->id);
309}
310
311TEST_F(io_ply_import_gsplat, above_max_degree_drops_sh)
312{
313 /* 25 coefficients per channel is beyond the supported degree 4. The channel offsets of such a
314 * file do not line up with any supported layout, so no coefficients can be imported. */
315 const GsplatPlyDataBuilder builder(2, 25 * 3);
316 PointCloud *point_cloud = convert_gsplat_ply_to_point_cloud(builder.build(), params_);
317 ASSERT_NE(point_cloud, nullptr);
318
319 EXPECT_EQ(count_sh_attributes(*point_cloud), 0);
320 EXPECT_TRUE(has_warning_containing("spherical harmonics"));
321
322 BKE_id_free(nullptr, &point_cloud->id);
323}
324
325} // namespace blender::io::ply
Read Blender memory allocation and freeing.
bool contains(StringRef name) const
GAttributeReader lookup(const StringRef name) const
float f_dc(const int point, const int channel) const
GsplatPlyDataBuilder(const int num_points, const int num_rest_attributes)
float rotation(const int point, const int component) const
float scale(const int point, const int axis) const
float3 expected_sh(const int point, const int coefficient) const
float f_rest(const int point, const int attribute_index) const
static int count_sh_attributes(const PointCloud &point_cloud)
void expect_sh_attributes(const PointCloud &point_cloud, const GsplatPlyDataBuilder &builder, const int expected_num_coefficients)
#define attribute(slot)
float3x3 scale(float3x3 mat, float2 scale)
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
void gtest_teardown()
void gtest_setup()
TEST_F(PLYExportTest, WriteHeaderAscii)
PointCloud * convert_gsplat_ply_to_point_cloud(const PlyData &data, const PLYImportParams &params)
QuaternionBase< float > Quaternion
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
@ RPT_WARNING
Definition BKE_report.hh:40
void BKE_reports_free(ReportList *reports)
Definition report.cc:106
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:91
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
VecBase< float, 4 > float4
BMesh const char void * data
void BKE_id_free(Main *bmain, void *idv)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
VecBase< float, 3 > float3
char * BKE_reports_string(ReportList *reports, eReportType level)
Definition report.cc:307
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ReportList * reports
Definition IO_ply.hh:76
ListBaseT< Report > list
Definition BKE_report.hh:79
static float decode_opacity(const float opacity)
Definition IO_gsplat.hh:114
static float3 decode_scale(const float3 scale)
Definition IO_gsplat.hh:118