Welcome to mirror list, hosted at ThFree Co, Russian Federation.

obj_exporter_tests.cc « tests « wavefront_obj « io « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f12bfd0cea560569f3549ca4a48685d1b3ed2ec3 (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* Apache License, Version 2.0 */

#include <fstream>
#include <gtest/gtest.h>
#include <ios>
#include <memory>
#include <sstream>
#include <string>
#include <system_error>

#include "testing/testing.h"
#include "tests/blendfile_loading_base_test.h"

#include "BKE_appdir.h"
#include "BKE_blender_version.h"

#include "BLI_fileops.h"
#include "BLI_index_range.hh"
#include "BLI_string_utf8.h"
#include "BLI_vector.hh"

#include "DEG_depsgraph.h"

#include "obj_export_file_writer.hh"
#include "obj_export_mesh.hh"
#include "obj_export_nurbs.hh"
#include "obj_exporter.hh"

#include "obj_exporter_tests.hh"

namespace blender::io::obj {

/* This is also the test name. */
class obj_exporter_test : public BlendfileLoadingBaseTest {
 public:
  /**
   * \param filepath: relative to "tests" directory.
   */
  bool load_file_and_depsgraph(const std::string &filepath,
                               const eEvaluationMode eval_mode = DAG_EVAL_VIEWPORT)
  {
    if (!blendfile_load(filepath.c_str())) {
      return false;
    }
    depsgraph_create(eval_mode);
    return true;
  }
};

const std::string all_objects_file = "io_tests/blend_scene/all_objects.blend";
const std::string all_curve_objects_file = "io_tests/blend_scene/all_curves.blend";

TEST_F(obj_exporter_test, filter_objects_curves_as_mesh)
{
  OBJExportParamsDefault _export;
  if (!load_file_and_depsgraph(all_objects_file)) {
    ADD_FAILURE();
    return;
  }
  auto [objmeshes, objcurves]{filter_supported_objects(depsgraph, _export.params)};
  EXPECT_EQ(objmeshes.size(), 17);
  EXPECT_EQ(objcurves.size(), 0);
}

TEST_F(obj_exporter_test, filter_objects_curves_as_nurbs)
{
  OBJExportParamsDefault _export;
  if (!load_file_and_depsgraph(all_objects_file)) {
    ADD_FAILURE();
    return;
  }
  _export.params.export_curves_as_nurbs = true;
  auto [objmeshes, objcurves]{filter_supported_objects(depsgraph, _export.params)};
  EXPECT_EQ(objmeshes.size(), 16);
  EXPECT_EQ(objcurves.size(), 2);
}

TEST_F(obj_exporter_test, filter_objects_selected)
{
  OBJExportParamsDefault _export;
  if (!load_file_and_depsgraph(all_objects_file)) {
    ADD_FAILURE();
    return;
  }
  _export.params.export_selected_objects = true;
  _export.params.export_curves_as_nurbs = true;
  auto [objmeshes, objcurves]{filter_supported_objects(depsgraph, _export.params)};
  EXPECT_EQ(objmeshes.size(), 1);
  EXPECT_EQ(objcurves.size(), 0);
}

TEST(obj_exporter_utils, append_negative_frame_to_filename)
{
  const char path_original[FILE_MAX] = "/my_file.obj";
  const char path_truth[FILE_MAX] = "/my_file-123.obj";
  const int frame = -123;
  char path_with_frame[FILE_MAX] = {0};
  const bool ok = append_frame_to_filename(path_original, frame, path_with_frame);
  EXPECT_TRUE(ok);
  EXPECT_EQ_ARRAY(path_with_frame, path_truth, BLI_strlen_utf8(path_truth));
}

TEST(obj_exporter_utils, append_positive_frame_to_filename)
{
  const char path_original[FILE_MAX] = "/my_file.obj";
  const char path_truth[FILE_MAX] = "/my_file123.obj";
  const int frame = 123;
  char path_with_frame[FILE_MAX] = {0};
  const bool ok = append_frame_to_filename(path_original, frame, path_with_frame);
  EXPECT_TRUE(ok);
  EXPECT_EQ_ARRAY(path_with_frame, path_truth, BLI_strlen_utf8(path_truth));
}

TEST_F(obj_exporter_test, curve_nurbs_points)
{
  if (!load_file_and_depsgraph(all_curve_objects_file)) {
    ADD_FAILURE();
    return;
  }

  OBJExportParamsDefault _export;
  _export.params.export_curves_as_nurbs = true;
  auto [objmeshes_unused, objcurves]{filter_supported_objects(depsgraph, _export.params)};

  for (auto &objcurve : objcurves) {
    if (all_nurbs_truth.count(objcurve->get_curve_name()) != 1) {
      ADD_FAILURE();
      return;
    }
    const NurbsObject *const nurbs_truth = all_nurbs_truth.at(objcurve->get_curve_name()).get();
    EXPECT_EQ(objcurve->total_splines(), nurbs_truth->total_splines());
    for (int spline_index : IndexRange(objcurve->total_splines())) {
      EXPECT_EQ(objcurve->total_spline_vertices(spline_index),
                nurbs_truth->total_spline_vertices(spline_index));
      EXPECT_EQ(objcurve->get_nurbs_degree(spline_index),
                nurbs_truth->get_nurbs_degree(spline_index));
      EXPECT_EQ(objcurve->total_spline_control_points(spline_index),
                nurbs_truth->total_spline_control_points(spline_index));
    }
  }
}

TEST_F(obj_exporter_test, curve_coordinates)
{
  if (!load_file_and_depsgraph(all_curve_objects_file)) {
    ADD_FAILURE();
    return;
  }

  OBJExportParamsDefault _export;
  _export.params.export_curves_as_nurbs = true;
  auto [objmeshes_unused, objcurves]{filter_supported_objects(depsgraph, _export.params)};

  for (auto &objcurve : objcurves) {
    if (all_nurbs_truth.count(objcurve->get_curve_name()) != 1) {
      ADD_FAILURE();
      return;
    }
    const NurbsObject *const nurbs_truth = all_nurbs_truth.at(objcurve->get_curve_name()).get();
    EXPECT_EQ(objcurve->total_splines(), nurbs_truth->total_splines());
    for (int spline_index : IndexRange(objcurve->total_splines())) {
      for (int vertex_index : IndexRange(objcurve->total_spline_vertices(spline_index))) {
        EXPECT_V3_NEAR(objcurve->vertex_coordinates(
                           spline_index, vertex_index, _export.params.scaling_factor),
                       nurbs_truth->vertex_coordinates(spline_index, vertex_index),
                       0.000001f);
      }
    }
  }
}

static std::unique_ptr<OBJWriter> init_writer(const OBJExportParams &params,
                                              const std::string out_filepath)
{
  try {
    auto writer = std::make_unique<OBJWriter>(out_filepath.c_str(), params);
    return writer;
  }
  catch (const std::system_error &ex) {
    std::cerr << ex.code().category().name() << ": " << ex.what() << ": " << ex.code().message()
              << std::endl;
    return nullptr;
  }
}

/* The following is relative to BKE_tempdir_base. */
const char *const temp_file_path = "output.OBJ";

static std::string read_temp_file_in_string(const std::string &file_path)
{
  std::ifstream temp_stream(file_path);
  std::ostringstream input_ss;
  input_ss << temp_stream.rdbuf();
  return input_ss.str();
}

TEST(obj_exporter_writer, header)
{
  /* Because testing doesn't fully initialize Blender, we need the following. */
  BKE_tempdir_init(NULL);
  std::string out_file_path = blender::tests::flags_test_release_dir() + "/" + temp_file_path;
  {
    OBJExportParamsDefault _export;
    std::unique_ptr<OBJWriter> writer = init_writer(_export.params, out_file_path);
    if (!writer) {
      ADD_FAILURE();
      return;
    }
    writer->write_header();
  }
  const std::string result = read_temp_file_in_string(out_file_path);
  using namespace std::string_literals;
  ASSERT_EQ(result, "# Blender "s + BKE_blender_version_string() + "\n" + "# www.blender.org\n");
  BLI_delete(out_file_path.c_str(), false, false);
}

TEST(obj_exporter_writer, mtllib)
{
  std::string out_file_path = blender::tests::flags_test_release_dir() + "/" + temp_file_path;
  {
    OBJExportParamsDefault _export;
    std::unique_ptr<OBJWriter> writer = init_writer(_export.params, out_file_path);
    if (!writer) {
      ADD_FAILURE();
      return;
    }
    writer->write_mtllib_name("/Users/blah.mtl");
    writer->write_mtllib_name("\\C:\\blah.mtl");
  }
  const std::string result = read_temp_file_in_string(out_file_path);
  ASSERT_EQ(result, "mtllib blah.mtl\nmtllib blah.mtl\n");
}

/* Return true if string #a and string #b are equal after their first newline. */
static bool strings_equal_after_first_lines(const std::string &a, const std::string &b)
{
  /* If `dbg_level > 0` then a failing test will print context around the first mismatch. */
  const bool dbg_level = 0;
  const size_t a_len = a.size();
  const size_t b_len = b.size();
  const size_t a_next = a.find_first_of('\n');
  const size_t b_next = b.find_first_of('\n');
  if (a_next == std::string::npos || b_next == std::string::npos) {
    if (dbg_level > 0) {
      std::cout << "Couldn't find newline in one of args\n";
    }
    return false;
  }
  if (dbg_level > 0) {
    if (a.compare(a_next, a_len - a_next, b, b_next, b_len - b_next) != 0) {
      for (int i = 0; i < a_len - a_next && i < b_len - b_next; ++i) {
        if (a[a_next + i] != b[b_next + i]) {
          std::cout << "Difference found at pos " << a_next + i << " of a\n";
          std::cout << "a: " << a.substr(a_next + i, 100) << " ...\n";
          std::cout << "b: " << b.substr(b_next + i, 100) << " ... \n";
          return false;
        }
      }
    }
    else {
      return true;
    }
  }
  return a.compare(a_next, a_len - a_next, b, b_next, b_len - b_next) == 0;
}

/* From here on, tests are whole file tests, testing for golden output. */
class obj_exporter_regression_test : public obj_exporter_test {
 public:
  /**
   * Export the given blend file with the given parameters and
   * test to see if it matches a golden file (ignoring any difference in Blender version number).
   * \param blendfile: input, relative to "tests" directory.
   * \param golden_obj: expected output, relative to "tests" directory.
   * \param params: the parameters to be used for export.
   */
  void compare_obj_export_to_golden(const std::string &blendfile,
                                    const std::string &golden_obj,
                                    const std::string &golden_mtl,
                                    OBJExportParams &params)
  {
    if (!load_file_and_depsgraph(blendfile)) {
      return;
    }
    /* Because testing doesn't fully initialize Blender, we need the following. */
    BKE_tempdir_init(NULL);
    std::string tempdir = std::string(BKE_tempdir_base());
    std::string out_file_path = tempdir + BLI_path_basename(golden_obj.c_str());
    strncpy(params.filepath, out_file_path.c_str(), FILE_MAX);
    params.blen_filepath = blendfile.c_str();
    export_frame(depsgraph, params, out_file_path.c_str());
    std::string output_str = read_temp_file_in_string(out_file_path);

    std::string golden_file_path = blender::tests::flags_test_asset_dir() + "/" + golden_obj;
    std::string golden_str = read_temp_file_in_string(golden_file_path);
    ASSERT_TRUE(strings_equal_after_first_lines(output_str, golden_str));
    BLI_delete(out_file_path.c_str(), false, false);
    if (!golden_mtl.empty()) {
      std::string out_mtl_file_path = tempdir + BLI_path_basename(golden_mtl.c_str());
      std::string output_mtl_str = read_temp_file_in_string(out_mtl_file_path);
      std::string golden_mtl_file_path = blender::tests::flags_test_asset_dir() + "/" + golden_mtl;
      std::string golden_mtl_str = read_temp_file_in_string(golden_mtl_file_path);
      ASSERT_TRUE(strings_equal_after_first_lines(output_mtl_str, golden_mtl_str));
      BLI_delete(out_mtl_file_path.c_str(), false, false);
    }
  }
};

TEST_F(obj_exporter_regression_test, all_tris)
{
  OBJExportParamsDefault _export;
  compare_obj_export_to_golden("io_tests/blend_geometry/all_tris.blend",
                               "io_tests/obj/all_tris.obj",
                               "io_tests/obj/all_tris.mtl",
                               _export.params);
}

TEST_F(obj_exporter_regression_test, all_quads)
{
  OBJExportParamsDefault _export;
  _export.params.scaling_factor = 2.0f;
  _export.params.export_materials = false;
  compare_obj_export_to_golden(
      "io_tests/blend_geometry/all_quads.blend", "io_tests/obj/all_quads.obj", "", _export.params);
}

TEST_F(obj_exporter_regression_test, fgons)
{
  OBJExportParamsDefault _export;
  _export.params.forward_axis = OBJ_AXIS_Y_FORWARD;
  _export.params.up_axis = OBJ_AXIS_Z_UP;
  _export.params.export_materials = false;
  compare_obj_export_to_golden(
      "io_tests/blend_geometry/fgons.blend", "io_tests/obj/fgons.obj", "", _export.params);
}

TEST_F(obj_exporter_regression_test, edges)
{
  OBJExportParamsDefault _export;
  _export.params.forward_axis = OBJ_AXIS_Y_FORWARD;
  _export.params.up_axis = OBJ_AXIS_Z_UP;
  _export.params.export_materials = false;
  compare_obj_export_to_golden(
      "io_tests/blend_geometry/edges.blend", "io_tests/obj/edges.obj", "", _export.params);
}

TEST_F(obj_exporter_regression_test, vertices)
{
  OBJExportParamsDefault _export;
  _export.params.forward_axis = OBJ_AXIS_Y_FORWARD;
  _export.params.up_axis = OBJ_AXIS_Z_UP;
  _export.params.export_materials = false;
  compare_obj_export_to_golden(
      "io_tests/blend_geometry/vertices.blend", "io_tests/obj/vertices.obj", "", _export.params);
}

TEST_F(obj_exporter_regression_test, nurbs_as_nurbs)
{
  OBJExportParamsDefault _export;
  _export.params.forward_axis = OBJ_AXIS_Y_FORWARD;
  _export.params.up_axis = OBJ_AXIS_Z_UP;
  _export.params.export_materials = false;
  _export.params.export_curves_as_nurbs = true;
  compare_obj_export_to_golden(
      "io_tests/blend_geometry/nurbs.blend", "io_tests/obj/nurbs.obj", "", _export.params);
}

TEST_F(obj_exporter_regression_test, nurbs_as_mesh)
{
  OBJExportParamsDefault _export;
  _export.params.forward_axis = OBJ_AXIS_Y_FORWARD;
  _export.params.up_axis = OBJ_AXIS_Z_UP;
  _export.params.export_materials = false;
  _export.params.export_curves_as_nurbs = false;
  compare_obj_export_to_golden(
      "io_tests/blend_geometry/nurbs.blend", "io_tests/obj/nurbs_mesh.obj", "", _export.params);
}

TEST_F(obj_exporter_regression_test, cube_all_data_triangulated)
{
  OBJExportParamsDefault _export;
  _export.params.forward_axis = OBJ_AXIS_Y_FORWARD;
  _export.params.up_axis = OBJ_AXIS_Z_UP;
  _export.params.export_materials = false;
  _export.params.export_triangulated_mesh = true;
  compare_obj_export_to_golden("io_tests/blend_geometry/cube_all_data.blend",
                               "io_tests/obj/cube_all_data_triangulated.obj",
                               "",
                               _export.params);
}

TEST_F(obj_exporter_regression_test, suzanne_all_data)
{
  OBJExportParamsDefault _export;
  _export.params.forward_axis = OBJ_AXIS_Y_FORWARD;
  _export.params.up_axis = OBJ_AXIS_Z_UP;
  _export.params.export_materials = false;
  _export.params.export_smooth_groups = true;
  compare_obj_export_to_golden("io_tests/blend_geometry/suzanne_all_data.blend",
                               "io_tests/obj/suzanne_all_data.obj",
                               "",
                               _export.params);
}

TEST_F(obj_exporter_regression_test, all_objects)
{
  OBJExportParamsDefault _export;
  _export.params.forward_axis = OBJ_AXIS_Y_FORWARD;
  _export.params.up_axis = OBJ_AXIS_Z_UP;
  _export.params.export_smooth_groups = true;
  compare_obj_export_to_golden("io_tests/blend_scene/all_objects.blend",
                               "io_tests/obj/all_objects.obj",
                               "io_tests/obj/all_objects.mtl",
                               _export.params);
}

}  // namespace blender::io::obj