Blender V4.3
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#include "BKE_main.hh"
9
10#include "BLI_fileops.h"
11#include "BLI_string.h"
12
13#include "BLO_readfile.hh"
14
15#include "DEG_depsgraph.hh"
16
17#include "MEM_guardedalloc.h"
18
19#include "IO_stl.hh"
20#include "stl_export.hh"
21
22namespace blender::io::stl {
23
24/* Set this true to keep comparison-failing test output in temp file directory. */
25constexpr bool save_failing_test_output = false;
26
27static std::string read_temp_file_in_string(const std::string &file_path)
28{
29 std::string res;
30 size_t buffer_len;
31 void *buffer = BLI_file_read_text_as_mem(file_path.c_str(), 0, &buffer_len);
32 if (buffer != nullptr) {
33 res.assign((const char *)buffer, buffer_len);
34 MEM_freeN(buffer);
35 }
36 return res;
37}
38
40 public:
41 bool load_file_and_depsgraph(const std::string &filepath)
42 {
43 if (!blendfile_load(filepath.c_str())) {
44 return false;
45 }
47 return true;
48 }
49
50 protected:
60 void SetUp() override
61 {
62 BlendfileLoadingBaseTest::SetUp();
63 BKE_tempdir_init(nullptr);
64 }
65
71
72 static std::string get_temp_filename(const std::string &filename)
73 {
74 return std::string(BKE_tempdir_base()) + SEP_STR + filename;
75 }
76
84 void compare_to_golden(const std::string &blendfile, const std::string &golden_stl)
85 {
86 if (!load_file_and_depsgraph(blendfile)) {
87 return;
88 }
89
90 std::string out_file_path = get_temp_filename(BLI_path_basename(golden_stl.c_str()));
91 STRNCPY(_params.filepath, out_file_path.c_str());
92 std::string golden_file_path = blender::tests::flags_test_asset_dir() + SEP_STR + golden_stl;
94 std::string output_str = read_temp_file_in_string(out_file_path);
95
96 std::string golden_str = read_temp_file_in_string(golden_file_path);
97 bool are_equal = output_str == golden_str;
98 if (save_failing_test_output && !are_equal) {
99 printf("failing test output in %s\n", out_file_path.c_str());
100 }
101 ASSERT_TRUE(are_equal);
102 if (!save_failing_test_output || are_equal) {
103 BLI_delete(out_file_path.c_str(), false, false);
104 }
105 }
106
108};
109
111{
112 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "all_tris.blend",
113 "io_tests" SEP_STR "stl" SEP_STR "all_tris.stl");
114}
115
117{
118 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "all_quads.blend",
119 "io_tests" SEP_STR "stl" SEP_STR "all_quads.stl");
120}
121
122TEST_F(STLExportTest, non_uniform_scale)
123{
124 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "non_uniform_scale.blend",
125 "io_tests" SEP_STR "stl" SEP_STR "non_uniform_scale.stl");
126}
127
128TEST_F(STLExportTest, cubes_positioned)
129{
130 compare_to_golden("io_tests" SEP_STR "blend_geometry" SEP_STR "cubes_positioned.blend",
131 "io_tests" SEP_STR "stl" SEP_STR "cubes_positioned.stl");
132}
133
134} // namespace blender::io::stl
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1190
const char * BKE_tempdir_base() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1211
void BKE_tempdir_session_purge()
Definition appdir.cc:1216
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:502
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define STRNCPY(dst, src)
Definition BLI_string.h:593
external readfile function prototypes.
@ DAG_EVAL_VIEWPORT
@ IO_AXIS_Y
@ IO_AXIS_Z
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:105
void export_frame(Depsgraph *depsgraph, float scene_unit_scale, const STLExportParams &export_params)
Definition stl_export.cc:40
static std::string read_temp_file_in_string(const std::string &file_path)
constexpr bool save_failing_test_output
TEST_F(STLExportTest, all_tris)
eIOAxis up_axis
Definition IO_stl.hh:38
char filepath[FILE_MAX]
Definition IO_stl.hh:36
bool apply_modifiers
Definition IO_stl.hh:42
eIOAxis forward_axis
Definition IO_stl.hh:37
bool ascii_format
Definition IO_stl.hh:43
float global_scale
Definition IO_stl.hh:39
#define SEP_STR
Definition unit.cc:39