Merge branch 'master' into blender2.8
[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 fucking STL
4 #include "intern/abc_util.h"
5 #include "intern/abc_exporter.h"
6
7 extern "C" {
8 #include "BLI_utildefines.h"
9 #include "BLI_math.h"
10 #include "DNA_scene_types.h"
11 }
12
13 #include "DEG_depsgraph.h"
14
15 class TestableAbcExporter : public AbcExporter {
16 public:
17         TestableAbcExporter(Scene *scene, const char *filename, ExportSettings &settings)
18             : AbcExporter(&eval_ctx, scene, filename, settings)
19         {
20                 /* TODO(sergey): Pass scene layer somehow? */
21                 DEG_evaluation_context_init(&eval_ctx, DAG_EVAL_VIEWPORT);
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         EvaluationContext eval_ctx;
37 };
38
39 class AlembicExportTest : public testing::Test
40 {
41 protected:
42         ExportSettings settings;
43         Scene scene;
44         TestableAbcExporter *exporter;
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                 exporter = NULL;
56         }
57
58         virtual void TearDown()
59         {
60                 delete exporter;
61         }
62
63         // Call after setting up the settings.
64         void createExporter()
65         {
66                 exporter = new TestableAbcExporter(&scene, "somefile.abc", settings);
67         }
68 };
69
70
71 TEST_F(AlembicExportTest, TimeSamplesFullShutter) {
72         settings.shutter_open = 0.0;
73         settings.shutter_close = 1.0;
74
75         createExporter();
76         std::vector<double> samples;
77
78         /* test 5 samples per frame */
79         exporter->getShutterSamples(5, true, samples);
80         EXPECT_EQ(5, samples.size());
81         EXPECT_NEAR(1.240, samples[0], 1e-5f);
82         EXPECT_NEAR(1.248, samples[1], 1e-5f);
83         EXPECT_NEAR(1.256, samples[2], 1e-5f);
84         EXPECT_NEAR(1.264, samples[3], 1e-5f);
85         EXPECT_NEAR(1.272, samples[4], 1e-5f);
86
87         /* test same, but using frame number offset instead of time */
88         exporter->getShutterSamples(5, false, samples);
89         EXPECT_EQ(5, samples.size());
90         EXPECT_NEAR(0.0, samples[0], 1e-5f);
91         EXPECT_NEAR(0.2, samples[1], 1e-5f);
92         EXPECT_NEAR(0.4, samples[2], 1e-5f);
93         EXPECT_NEAR(0.6, samples[3], 1e-5f);
94         EXPECT_NEAR(0.8, samples[4], 1e-5f);
95
96         /* use the same setup to test getFrameSet() */
97         std::set<double> frames;
98         exporter->getFrameSet(5, frames);
99         EXPECT_EQ(965, frames.size());
100         EXPECT_EQ(1, frames.count(31.0));
101         EXPECT_EQ(1, frames.count(31.2));
102         EXPECT_EQ(1, frames.count(31.4));
103         EXPECT_EQ(1, frames.count(31.6));
104         EXPECT_EQ(1, frames.count(31.8));
105 }
106
107
108 TEST_F(AlembicExportTest, TimeSamples180degShutter) {
109         settings.shutter_open = -0.25;
110         settings.shutter_close = 0.25;
111
112         createExporter();
113         std::vector<double> samples;
114
115         /* test 5 samples per frame */
116         exporter->getShutterSamples(5, true, samples);
117         EXPECT_EQ(5, samples.size());
118         EXPECT_NEAR(1.230, samples[0], 1e-5f);
119         EXPECT_NEAR(1.234, samples[1], 1e-5f);
120         EXPECT_NEAR(1.238, samples[2], 1e-5f);
121         EXPECT_NEAR(1.242, samples[3], 1e-5f);
122         EXPECT_NEAR(1.246, samples[4], 1e-5f);
123
124         /* test same, but using frame number offset instead of time */
125         exporter->getShutterSamples(5, false, samples);
126         EXPECT_EQ(5, samples.size());
127         EXPECT_NEAR(-0.25, samples[0], 1e-5f);
128         EXPECT_NEAR(-0.15, samples[1], 1e-5f);
129         EXPECT_NEAR(-0.05, samples[2], 1e-5f);
130         EXPECT_NEAR( 0.05, samples[3], 1e-5f);
131         EXPECT_NEAR( 0.15, samples[4], 1e-5f);
132
133         /* Use the same setup to test getFrameSet().
134          * Here only a few numbers are tested, due to rounding issues. */
135         std::set<double> frames;
136         exporter->getFrameSet(5, frames);
137         EXPECT_EQ(965, frames.size());
138         EXPECT_EQ(1, frames.count(30.75));
139         EXPECT_EQ(1, frames.count(30.95));
140         EXPECT_EQ(1, frames.count(31.15));
141 }