Fix T53544: Crash drawing scene w/o world
[blender.git] / tests / gtests / alembic / abc_export_test.cc
1 #include "testing/testing.h"
2
3 // Keep first since utildefines defines AT which conflicts with STL
4 #include "intern/abc_util.h"
5 #include "intern/abc_exporter.h"
6
7 extern "C" {
8 #include "BLI_utildefines.h"
9 #include "BKE_main.h"
10 #include "BLI_math.h"
11 #include "DNA_scene_types.h"
12 }
13
14 #include "DEG_depsgraph.h"
15
16 class TestableAbcExporter : public AbcExporter {
17 public:
18         TestableAbcExporter(Main *bmain,
19                             const char *filename, ExportSettings &settings)
20             : AbcExporter(bmain, filename, settings)
21         {
22         }
23
24         void getShutterSamples(unsigned int nr_of_samples,
25                                bool time_relative,
26                                std::vector<double> &samples)
27         {
28                 AbcExporter::getShutterSamples(nr_of_samples, time_relative, samples);
29         }
30
31         void getFrameSet(unsigned int nr_of_samples,
32                          std::set<double> &frames) {
33                 AbcExporter::getFrameSet(nr_of_samples, frames);
34         }
35 };
36
37 class AlembicExportTest : public testing::Test
38 {
39 protected:
40         ExportSettings settings;
41         Scene scene;
42         Depsgraph *depsgraph;
43         TestableAbcExporter *exporter;
44         Main *bmain;
45
46         virtual void SetUp()
47         {
48                 settings.frame_start = 31.0;
49                 settings.frame_end = 223.0;
50
51                 /* Fake a 25 FPS scene with a nonzero base (because that's sometimes forgotten) */
52                 scene.r.frs_sec = 50;
53                 scene.r.frs_sec_base = 2;
54
55                 bmain = BKE_main_new();
56
57                 /* TODO(sergey): Pass scene layer somehow? */
58                 ViewLayer *view_layer = (ViewLayer *)scene.view_layers.first;
59                 settings.depsgraph = depsgraph = DEG_graph_new(&scene, view_layer, DAG_EVAL_VIEWPORT);
60
61                 settings.scene = &scene;
62                 settings.view_layer = view_layer;
63
64                 exporter = NULL;
65         }
66
67         virtual void TearDown()
68         {
69                 BKE_main_free(bmain);
70                 DEG_graph_free(depsgraph);
71                 delete exporter;
72         }
73
74         // Call after setting up the settings.
75         void createExporter()
76         {
77                 exporter = new TestableAbcExporter(bmain, "somefile.abc", settings);
78         }
79 };
80
81
82 TEST_F(AlembicExportTest, TimeSamplesFullShutter) {
83         settings.shutter_open = 0.0;
84         settings.shutter_close = 1.0;
85
86         createExporter();
87         std::vector<double> samples;
88
89         /* test 5 samples per frame */
90         exporter->getShutterSamples(5, true, samples);
91         EXPECT_EQ(5, samples.size());
92         EXPECT_NEAR(1.240, samples[0], 1e-5f);
93         EXPECT_NEAR(1.248, samples[1], 1e-5f);
94         EXPECT_NEAR(1.256, samples[2], 1e-5f);
95         EXPECT_NEAR(1.264, samples[3], 1e-5f);
96         EXPECT_NEAR(1.272, samples[4], 1e-5f);
97
98         /* test same, but using frame number offset instead of time */
99         exporter->getShutterSamples(5, false, samples);
100         EXPECT_EQ(5, samples.size());
101         EXPECT_NEAR(0.0, samples[0], 1e-5f);
102         EXPECT_NEAR(0.2, samples[1], 1e-5f);
103         EXPECT_NEAR(0.4, samples[2], 1e-5f);
104         EXPECT_NEAR(0.6, samples[3], 1e-5f);
105         EXPECT_NEAR(0.8, samples[4], 1e-5f);
106
107         /* use the same setup to test getFrameSet() */
108         std::set<double> frames;
109         exporter->getFrameSet(5, frames);
110         EXPECT_EQ(965, frames.size());
111         EXPECT_EQ(1, frames.count(31.0));
112         EXPECT_EQ(1, frames.count(31.2));
113         EXPECT_EQ(1, frames.count(31.4));
114         EXPECT_EQ(1, frames.count(31.6));
115         EXPECT_EQ(1, frames.count(31.8));
116 }
117
118
119 TEST_F(AlembicExportTest, TimeSamples180degShutter) {
120         settings.shutter_open = -0.25;
121         settings.shutter_close = 0.25;
122
123         createExporter();
124         std::vector<double> samples;
125
126         /* test 5 samples per frame */
127         exporter->getShutterSamples(5, true, samples);
128         EXPECT_EQ(5, samples.size());
129         EXPECT_NEAR(1.230, samples[0], 1e-5f);
130         EXPECT_NEAR(1.234, samples[1], 1e-5f);
131         EXPECT_NEAR(1.238, samples[2], 1e-5f);
132         EXPECT_NEAR(1.242, samples[3], 1e-5f);
133         EXPECT_NEAR(1.246, samples[4], 1e-5f);
134
135         /* test same, but using frame number offset instead of time */
136         exporter->getShutterSamples(5, false, samples);
137         EXPECT_EQ(5, samples.size());
138         EXPECT_NEAR(-0.25, samples[0], 1e-5f);
139         EXPECT_NEAR(-0.15, samples[1], 1e-5f);
140         EXPECT_NEAR(-0.05, samples[2], 1e-5f);
141         EXPECT_NEAR( 0.05, samples[3], 1e-5f);
142         EXPECT_NEAR( 0.15, samples[4], 1e-5f);
143
144         /* Use the same setup to test getFrameSet().
145          * Here only a few numbers are tested, due to rounding issues. */
146         std::set<double> frames;
147         exporter->getFrameSet(5, frames);
148         EXPECT_EQ(965, frames.size());
149         EXPECT_EQ(1, frames.count(30.75));
150         EXPECT_EQ(1, frames.count(30.95));
151         EXPECT_EQ(1, frames.count(31.15));
152 }