Blender V5.0
stl_exporter_tests.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7#include "BKE_appdir.hh"
8
9#include "BLI_fileops.h"
10#include "BLI_string.h"
11
12#include "DEG_depsgraph.hh"
13
14#include "MEM_guardedalloc.h"
15
16#include "IO_stl.hh"
17#include "stl_export.hh"
18
19namespace blender::io::stl {
20
21/* Set this true to keep comparison-failing test output in temp file directory. */
22constexpr bool save_failing_test_output = false;
23
24static std::string read_temp_file_in_string(const std::string &file_path)
25{
26 std::string res;
27 size_t buffer_len;
28 void *buffer = BLI_file_read_text_as_mem(file_path.c_str(), 0, &buffer_len);
29 if (buffer != nullptr) {
30 res.assign((const char *)buffer, buffer_len);
31 MEM_freeN(buffer);
32 }
33 return res;
34}
35
37 public:
38 bool load_file_and_depsgraph(const std::string &filepath)
39 {
40 if (!blendfile_load(filepath.c_str())) {
41 return false;
42 }
44 return true;
45 }
46
47 protected:
49 {
50 _params.ascii_format = true;
51 }
52 void SetUp() override
53 {
54 BlendfileLoadingBaseTest::SetUp();
55 BKE_tempdir_init(nullptr);
56 }
57
63
64 static std::string get_temp_filename(const std::string &filename)
65 {
66 return std::string(BKE_tempdir_base()) + SEP_STR + filename;
67 }
68
76 void compare_to_golden(const std::string &blendfile, const std::string &golden_stl)
77 {
78 if (!load_file_and_depsgraph(blendfile)) {
79 return;
80 }
81
82 std::string out_file_path = get_temp_filename(BLI_path_basename(golden_stl.c_str()));
83 STRNCPY(_params.filepath, out_file_path.c_str());
84 std::string golden_file_path = blender::tests::flags_test_asset_dir() + SEP_STR + golden_stl;
86 std::string output_str = read_temp_file_in_string(out_file_path);
87
88 std::string golden_str = read_temp_file_in_string(golden_file_path);
89 bool are_equal = output_str == golden_str;
90 if (save_failing_test_output && !are_equal) {
91 printf("failing test output in %s\n", out_file_path.c_str());
92 }
93 ASSERT_TRUE(are_equal);
94 if (!save_failing_test_output || are_equal) {
95 BLI_delete(out_file_path.c_str(), false, false);
96 }
97 }
98
100};
101
103{
104 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "all_tris.blend",
105 "io_tests" SEP_STR "stl" SEP_STR "all_tris.stl");
106}
107
109{
110 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "all_quads.blend",
111 "io_tests" SEP_STR "stl" SEP_STR "all_quads.stl");
112}
113
114TEST_F(STLExportTest, non_uniform_scale)
115{
116 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "non_uniform_scale.blend",
117 "io_tests" SEP_STR "stl" SEP_STR "non_uniform_scale.stl");
118}
119
120TEST_F(STLExportTest, cubes_positioned)
121{
122 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "cubes_positioned.blend",
123 "io_tests" SEP_STR "stl" SEP_STR "cubes_positioned.stl");
124}
125
126} // namespace blender::io::stl
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1200
const char * BKE_tempdir_base() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1243
void BKE_tempdir_session_purge()
Definition appdir.cc:1248
File and directory operations.
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:511
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:693
@ DAG_EVAL_VIEWPORT
Read Guarded memory(de)allocation.
virtual void depsgraph_create(eEvaluationMode depsgraph_evaluation_mode)
bool blendfile_load(const char *filepath)
void compare_to_golden(const std::string &blendfile, const std::string &golden_stl)
bool load_file_and_depsgraph(const std::string &filepath)
static std::string get_temp_filename(const std::string &filename)
#define printf(...)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void export_frame(Depsgraph *depsgraph, float scene_unit_scale, const STLExportParams &export_params)
Definition stl_export.cc:42
static std::string read_temp_file_in_string(const std::string &file_path)
constexpr bool save_failing_test_output
TEST_F(STLExportTest, all_tris)
#define SEP_STR
Definition unit.cc:39