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