Debian Codegraph @

lib/Alembic/AbcCoreHDF5/Tests/ArchiveTests.cpp

raw
1 //-*****************************************************************************
2 //
3 // Copyright (c) 2009-2013,
4 // Sony Pictures Imageworks Inc. and
5 // Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6 //
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following disclaimer
16 // in the documentation and/or other materials provided with the
17 // distribution.
18 // * Neither the name of Sony Pictures Imageworks, nor
19 // Industrial Light & Magic, nor the names of their contributors may be used
20 // to endorse or promote products derived from this software without specific
21 // prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //
35 //-*****************************************************************************
36
37 #include <Alembic/AbcCoreAbstract/All.h>
38 #include <Alembic/AbcCoreHDF5/All.h>
39 #include <Alembic/Util/All.h>
40
41 #include <Alembic/AbcCoreAbstract/Tests/Assert.h>
42
43 #include <iostream>
44 #include <vector>
45
46 #include <hdf5.h>
47
48 //-*****************************************************************************
49 namespace A5 = Alembic::AbcCoreHDF5;
50
51 namespace ABCA = Alembic::AbcCoreAbstract;
52
53 using ABCA::chrono_t;
54 using Alembic::Util::float32_t;
55 using Alembic::Util::int32_t;
56 using Alembic::Util::byte_t;
57 using Alembic::Util::Dimensions;
58
59 //-*****************************************************************************
60
61 void testReadWriteEmptyArchive()
62 {
63 std::string archiveName = "emptyArchive.abc";
64
65 {
66 A5::WriteArchive w;
67 ABCA::MetaData m;
68 m.set("Bleep", "bloop");
69 m.set("eep", "");
70 m.set("potato", "salad");
71 m.set("geoScope", "tasty");
72
73 ABCA::ArchiveWriterPtr a = w(archiveName, m);
74 ABCA::ObjectWriterPtr archive = a->getTop();
75
76 TESTING_ASSERT(archive->getFullName() == "/");
77 }
78
79 {
80 A5::ReadArchive r;
81 ABCA::ArchiveReaderPtr a = r( archiveName );
82 ABCA::ObjectReaderPtr archive = a->getTop();
83 ABCA::MetaData m = archive->getHeader().getMetaData();
84 TESTING_ASSERT(m.get("Bleep") == "bloop");
85 TESTING_ASSERT(m.get("eep") == "");
86 TESTING_ASSERT(m.get("potato") == "salad");
87 TESTING_ASSERT(m.get("geoScope") == "tasty");
88 TESTING_ASSERT(archive->getName() == "ABC");
89 TESTING_ASSERT(archive->getFullName() == "/");
90 TESTING_ASSERT(archive->getParent() == NULL);
91 TESTING_ASSERT(archive->getNumChildren() == 0);
92
93 // even though we didn't write anything make sure we intrincially have
94 // the default sampling
95 TESTING_ASSERT(a->getNumTimeSamplings() == 1);
96 TESTING_ASSERT(*(a->getTimeSampling(0)) == ABCA::TimeSampling());
97 TESTING_ASSERT(a->getMaxNumSamplesForTimeSamplingIndex(0) == 0);
98
99 ABCA::CompoundPropertyReaderPtr parent = archive->getProperties();
100 TESTING_ASSERT(parent->getNumProperties() == 0);
101
102 // get it again to make sure we clean ourselves up properly
103 A5::ReadArchive r2;
104 ABCA::ArchiveReaderPtr a2 = r2( archiveName );
105 ABCA::ObjectReaderPtr archive2 = a2->getTop();
106 ABCA::CompoundPropertyReaderPtr p2 = archive2->getProperties();
107 TESTING_ASSERT(p2->getNumProperties() == 0);
108 }
109 }
110
111 void testReadWriteTimeSamplingArchive()
112 {
113 std::string archiveName = "timeSampsArchive.abc";
114
115 {
116 A5::WriteArchive w;
117 ABCA::ArchiveWriterPtr a = w(archiveName, ABCA::MetaData());
118
119 // we always have 1
120 TESTING_ASSERT(a->getNumTimeSamplings() == 1);
121
122 // getting a time sampling that doesn't exist should throw
123 TESTING_ASSERT_THROW(a->getTimeSampling(43),
124 Alembic::Util::Exception);
125 TESTING_ASSERT(
126 a->getMaxNumSamplesForTimeSamplingIndex(43) == INDEX_UNKNOWN);
127
128 // first one is the default time sampling
129 TESTING_ASSERT(*(a->getTimeSampling(0)) == ABCA::TimeSampling());
130 TESTING_ASSERT(a->getMaxNumSamplesForTimeSamplingIndex(0) == 0);
131
132 std::vector< double > samps;
133
134 // uniform sampling starts at second 34, 24fps
135 samps.push_back(34.0);
136 ABCA::TimeSampling ts(ABCA::TimeSamplingType(1.0/24.0), samps);
137 uint32_t index = a->addTimeSampling(ts);
138 TESTING_ASSERT(index == 1);
139
140 // even though we add the same thing, we get the same index back
141 index = a->addTimeSampling(ts);
142 TESTING_ASSERT(index == 1);
143
144 // cyclic sampling example
145 samps.push_back(34.25);
146 samps.push_back(34.5);
147 ts = ABCA::TimeSampling(ABCA::TimeSamplingType(3, 1.0), samps);
148 index = a->addTimeSampling(ts);
149 TESTING_ASSERT(index == 2);
150
151 // normally we wouldn't ever call this directly, call it here
152 // for testing purposes only
153 a->setMaxNumSamplesForTimeSamplingIndex(2, 42);
154
155 // really weird acyclic example
156 samps.push_back(300.0);
157 samps.push_back(500.0);
158 ts = ABCA::TimeSampling(
159 ABCA::TimeSamplingType(ABCA::TimeSamplingType::kAcyclic), samps);
160 index = a->addTimeSampling(ts);
161 TESTING_ASSERT(index == 3);
162
163 // now we should have 4
164 TESTING_ASSERT(a->getNumTimeSamplings() == 4);
165 }
166
167 {
168 A5::ReadArchive r;
169 ABCA::ArchiveReaderPtr a = r( archiveName );
170
171 TESTING_ASSERT(a->getNumTimeSamplings() == 4);
172
173 // first one is the default time sampling
174 TESTING_ASSERT(*(a->getTimeSampling(0)) == ABCA::TimeSampling());
175
176 std::vector< double > samps;
177
178 // uniform sampling starts at second 34, 24fps
179 samps.push_back(34.0);
180 ABCA::TimeSampling ts(ABCA::TimeSamplingType(1.0/24.0), samps);
181 TESTING_ASSERT( ts == *(a->getTimeSampling(1)) );
182 TESTING_ASSERT( a->getMaxNumSamplesForTimeSamplingIndex(1) == 0 );
183
184 // cyclic sampling example
185 samps.push_back(34.25);
186 samps.push_back(34.5);
187 ts = ABCA::TimeSampling(ABCA::TimeSamplingType(3, 1.0), samps);
188 TESTING_ASSERT( ts == *(a->getTimeSampling(2)) );
189 TESTING_ASSERT( a->getMaxNumSamplesForTimeSamplingIndex(2) == 42 );
190
191 // really weird acyclic example
192 samps.push_back(300.0);
193 samps.push_back(500.0);
194 ts = ABCA::TimeSampling(
195 ABCA::TimeSamplingType(ABCA::TimeSamplingType::kAcyclic), samps);
196 TESTING_ASSERT( ts == *(a->getTimeSampling(3)) );
197 TESTING_ASSERT( a->getMaxNumSamplesForTimeSamplingIndex(3) == 0 );
198 }
199 }
200
201 void testReadWriteMaxNumSamplesArchive()
202 {
203 std::string archiveName = "timeMaxNumSampsArchive.abc";
204
205 {
206 A5::WriteArchive w;
207 ABCA::ArchiveWriterPtr a = w(archiveName, ABCA::MetaData());
208
209 // we always have 1
210 TESTING_ASSERT(a->getNumTimeSamplings() == 1);
211
212 std::vector< double > samps;
213
214 // uniform sampling starts at second 34, 24fps
215 samps.push_back(34.0);
216 ABCA::TimeSampling ts(ABCA::TimeSamplingType(1.0/24.0), samps);
217 uint32_t index = a->addTimeSampling(ts);
218 TESTING_ASSERT(index == 1);
219
220 // uniform sampling starts at second 72, 24fps
221 samps[0] = 72.0;
222 ABCA::TimeSampling ts2(ABCA::TimeSamplingType(1.0/24.0), samps);
223 index = a->addTimeSampling(ts2);
224 TESTING_ASSERT(index == 2);
225
226 std::string testStr = "test";
227 ABCA::ScalarPropertyWriterPtr propPtr =
228 a->getTop()->getProperties()->createScalarProperty("test",
229 ABCA::MetaData(),
230 ABCA::DataType(Alembic::Util::kStringPOD, 1), 1);
231
232 // set the same thing 3 times
233 propPtr->setSample(&testStr);
234 propPtr->setSample(&testStr);
235 propPtr->setSample(&testStr);
236
237 propPtr =
238 a->getTop()->getProperties()->createScalarProperty("test2",
239 ABCA::MetaData(),
240 ABCA::DataType(Alembic::Util::kStringPOD, 1), 2);
241 propPtr->setSample(&testStr);
242 propPtr->setSample(&testStr);
243 std::string test2Str = "test2";
244 propPtr->setSample(&test2Str);
245 }
246
247 {
248 A5::ReadArchive r;
249 ABCA::ArchiveReaderPtr a = r( archiveName );
250
251
252 std::vector< double > samps;
253
254 // uniform sampling starts at second 34, 24fps
255 samps.push_back(34.0);
256 ABCA::TimeSampling ts(ABCA::TimeSamplingType(1.0/24.0), samps);
257 TESTING_ASSERT( ts == *(a->getTimeSampling(1)) );
258
259 TESTING_ASSERT( a->getMaxNumSamplesForTimeSamplingIndex(0) == 0 );
260 TESTING_ASSERT( a->getMaxNumSamplesForTimeSamplingIndex(1) == 1 );
261 TESTING_ASSERT( a->getMaxNumSamplesForTimeSamplingIndex(2) == 3 );
262 }
263 }
264
265 void writeArchive( const std::string & iName, bool iCache )
266 {
267 ABCA::MetaData m;
268 ABCA::ObjectHeader header("a", m);
269 A5::WriteArchive w( iCache );
270 ABCA::ArchiveWriterPtr a = w(iName, m );
271 ABCA::ObjectWriterPtr root = a->getTop();
272 std::vector< ABCA::ObjectWriterPtr > objVec;
273
274 objVec.push_back(root);
275 objVec.push_back(root->createChild(header));
276 header.setName("b");
277 objVec.push_back(root->createChild(header));
278
279 // add some number of objects to the existing ones (that aren't the root)
280 int numObjs = objVec.size();
281 for (int i = 1; i < numObjs; ++i)
282 {
283 header.setName("a");
284 objVec.push_back(objVec[i]->createChild(header));
285 header.setName("b");
286 objVec.push_back(objVec[i]->createChild(header));
287 header.setName("c");
288 objVec.push_back(objVec[i]->createChild(header));
289 }
290
291 ABCA::DataType i32d(Alembic::Util::kInt32POD, 1);
292
293 // let's add 3 array properties to EVERY object
294 for (std::size_t i = 0; i < objVec.size(); ++i)
295 {
296 ABCA::CompoundPropertyWriterPtr top = objVec[i]->getProperties();
297 std::vector<ABCA::ArrayPropertyWriterPtr> props;
298 props.push_back(top->createArrayProperty("a", m, i32d, 0));
299 props.push_back(top->createArrayProperty("b", m, i32d, 0));
300 props.push_back(top->createArrayProperty("c", m, i32d, 0));
301
302 for (std::size_t i = 1; i < props.size(); ++i)
303 {
304 std::vector <Alembic::Util::int32_t> vali(i, 0);
305 Alembic::Util::Dimensions dims(i);
306 for (std::size_t j = i; j < props.size(); ++j)
307 {
308 props[j]->setSample(
309 ABCA::ArraySample(&(vali.front()), i32d, dims));
310 }
311 }
312 }
313 }
314
315 void readArchive( const std::string & iName, bool iCache )
316 {
317 Alembic::AbcCoreHDF5::ReadArchive r( iCache );
318 ABCA::ArchiveReaderPtr a = r( iName );
319 std::vector< ABCA::ObjectReaderPtr > objs;
320 objs.push_back( a->getTop() );
321 TESTING_ASSERT( objs[0]->getNumChildren() == 2 );
322 objs.push_back( objs[0]->getChild(0) );
323 objs.push_back( objs[0]->getChild(1) );
324
325 std::size_t numObjs = objs.size();
326 for (std::size_t i = 1; i < numObjs; ++i)
327 {
328 TESTING_ASSERT( objs[i]->getNumChildren() == 3 );
329 for (std::size_t j = 0; j < numObjs; ++j)
330 {
331 objs.push_back( objs[i]->getChild(j) );
332 }
333 }
334
335 for (std::size_t i = 0; i < objs.size(); ++i)
336 {
337 TESTING_ASSERT( objs[i]->getProperties()->getNumProperties() == 3 );
338
339 ABCA::CompoundPropertyReaderPtr cpr = objs[i]->getProperties();
340 ABCA::ArrayPropertyReaderPtr apr = cpr->getArrayProperty("a");
341
342 TESTING_ASSERT( 0 ==
343 objs[i]->getProperties()->getArrayProperty("a")->getNumSamples() );
344 TESTING_ASSERT( 1 ==
345 objs[i]->getProperties()->getArrayProperty("b")->getNumSamples() );
346 TESTING_ASSERT( 2 ==
347 objs[i]->getProperties()->getArrayProperty("c")->getNumSamples() );
348 }
349
350 TESTING_ASSERT( a->getMaxNumSamplesForTimeSamplingIndex(0) == 2 );
351 }
352
353 void writeVeryEmptyArchive( const std::string & iName, bool iCache )
354 {
355 ABCA::MetaData m;
356 A5::WriteArchive w( iCache );
357 ABCA::ArchiveWriterPtr a = w(iName, m);
358 }
359
360 void readVeryEmptyArchive( const std::string & iName, bool iCache )
361 {
362 Alembic::AbcCoreHDF5::ReadArchive r( iCache );
363 ABCA::ArchiveReaderPtr a = r( iName );
364 TESTING_ASSERT(a->getTop()->getNumChildren() == 0);
365 }
366
367 int main ( int argc, char *argv[] )
368 {
369 testReadWriteEmptyArchive();
370 testReadWriteTimeSamplingArchive();
371
372 writeArchive("noCacheTest.abc", false);
373 readArchive("noCacheTest.abc", false);
374 readArchive("noCacheTest.abc", true);
375
376 writeArchive("cacheTest.abc", true);
377 readArchive("cacheTest.abc", false);
378 readArchive("cacheTest.abc", true);
379
380 writeVeryEmptyArchive("noCacheTestEmpty.abc", false);
381 readVeryEmptyArchive("noCacheTestEmpty.abc", false);
382 readVeryEmptyArchive("noCacheTestEmpty.abc", true);
383
384 writeVeryEmptyArchive("cacheTestEmpty.abc", true);
385 readVeryEmptyArchive("cacheTestEmpty.abc", false);
386 readVeryEmptyArchive("cacheTestEmpty.abc", true);
387
388 testReadWriteMaxNumSamplesArchive();
389
390 return 0;
391 }