diff options
author | Kévin Dietrich <kevin.dietrich@mailoo.org> | 2016-09-09 06:06:06 +0300 |
---|---|---|
committer | Kévin Dietrich <kevin.dietrich@mailoo.org> | 2016-09-09 06:06:06 +0300 |
commit | 78ea06fea4a74181c25254ed72d50d8a743b6954 (patch) | |
tree | 00db1a293c1f2d5b91a2d8e1eab15cbe654b92cc /source | |
parent | 1558f5b6602ebe8ba31455b1837b2594d5c7f264 (diff) |
Alembic, cleanup: split archive opening code in their own classes and
files.
Also helps keeping platform specific code in separate from the rest of
the code.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/alembic/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/blender/alembic/intern/abc_archive.cc | 175 | ||||
-rw-r--r-- | source/blender/alembic/intern/abc_archive.h | 66 | ||||
-rw-r--r-- | source/blender/alembic/intern/abc_exporter.cc | 81 | ||||
-rw-r--r-- | source/blender/alembic/intern/abc_exporter.h | 2 | ||||
-rw-r--r-- | source/blender/alembic/intern/alembic_capi.cc | 108 |
6 files changed, 254 insertions, 180 deletions
diff --git a/source/blender/alembic/CMakeLists.txt b/source/blender/alembic/CMakeLists.txt index 0b6b2433bd0..6556354b016 100644 --- a/source/blender/alembic/CMakeLists.txt +++ b/source/blender/alembic/CMakeLists.txt @@ -48,6 +48,7 @@ if(APPLE OR WIN32) endif() set(SRC + intern/abc_archive.cc intern/abc_camera.cc intern/abc_customdata.cc intern/abc_curves.cc @@ -62,6 +63,7 @@ set(SRC intern/alembic_capi.cc ABC_alembic.h + intern/abc_archive.h intern/abc_camera.h intern/abc_customdata.h intern/abc_curves.h diff --git a/source/blender/alembic/intern/abc_archive.cc b/source/blender/alembic/intern/abc_archive.cc new file mode 100644 index 00000000000..72c0a2294af --- /dev/null +++ b/source/blender/alembic/intern/abc_archive.cc @@ -0,0 +1,175 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2016 Kévin Dietrich. + * All rights reserved. + * + * ***** END GPL LICENSE BLOCK ***** + * + */ + +#include "abc_archive.h" + +#ifdef WIN32 +# include "utfconv.h" +#endif + +using Alembic::Abc::Exception; +using Alembic::Abc::ErrorHandler; +using Alembic::Abc::IArchive; +using Alembic::Abc::kWrapExisting; +using Alembic::Abc::OArchive; + +static IArchive open_archive(const std::string &filename, + const std::vector<std::istream *> &input_streams, + bool &is_hdf5) +{ + try { + is_hdf5 = false; + Alembic::AbcCoreOgawa::ReadArchive archive_reader(input_streams); + + return IArchive(archive_reader(filename), + kWrapExisting, + ErrorHandler::kThrowPolicy); + } + catch (const Exception &e) { + std::cerr << e.what() << '\n'; + +#ifdef WITH_ALEMBIC_HDF5 + try { + is_hdf5 = true; + Alembic::AbcCoreAbstract::ReadArraySampleCachePtr cache_ptr; + + return IArchive(Alembic::AbcCoreHDF5::ReadArchive(), + filename.c_str(), ErrorHandler::kThrowPolicy, + cache_ptr); + } + catch (const Exception &) { + std::cerr << e.what() << '\n'; + return IArchive(); + } +#else + return IArchive(); +#endif + } + + return IArchive(); +} + +ArchiveReader::ArchiveReader(const char *filename) +{ +#ifdef WIN32 + UTF16_ENCODE(filename); + std::wstring wstr(filename_16); + m_infile.open(wstr.c_str(), std::ios::in | std::ios::binary); + UTF16_UN_ENCODE(filename); +#else + m_infile.open(filename, std::ios::in | std::ios::binary); +#endif + + m_streams.push_back(&m_infile); + + bool is_hdf5; + m_archive = open_archive(filename, m_streams, is_hdf5); + + /* We can't open an HDF5 file from a stream, so close it. */ + if (is_hdf5) { + m_infile.close(); + m_streams.clear(); + } +} + +bool ArchiveReader::valid() const +{ + return m_archive.valid(); +} + +Alembic::Abc::IObject ArchiveReader::getTop() +{ + return m_archive.getTop(); +} + +/* ************************************************************************** */ + +/* This kinda duplicates CreateArchiveWithInfo, but Alembic does not seem to + * have a version supporting streams. */ +static OArchive create_archive(std::ostream *ostream, + const std::string &filename, + const std::string &scene_name, + Alembic::Abc::MetaData &md, + bool ogawa) +{ + md.set(Alembic::Abc::kApplicationNameKey, "Blender"); + md.set(Alembic::Abc::kUserDescriptionKey, scene_name); + + time_t raw_time; + time(&raw_time); + char buffer[128]; + +#if defined _WIN32 || defined _WIN64 + ctime_s(buffer, 128, &raw_time); +#else + ctime_r(&raw_time, buffer); +#endif + + const std::size_t buffer_len = strlen(buffer); + if (buffer_len > 0 && buffer[buffer_len - 1] == '\n') { + buffer[buffer_len - 1] = '\0'; + } + + md.set(Alembic::Abc::kDateWrittenKey, buffer); + + ErrorHandler::Policy policy = ErrorHandler::kThrowPolicy; + +#ifdef WITH_ALEMBIC_HDF5 + if (!ogawa) { + return OArchive(Alembic::AbcCoreHDF5::WriteArchive(), filename, md, policy); + } +#else + static_cast<void>(filename); + static_cast<void>(ogawa); +#endif + + Alembic::AbcCoreOgawa::WriteArchive archive_writer; + return OArchive(archive_writer(ostream, md), kWrapExisting, policy); +} + +ArchiveWriter::ArchiveWriter(const char *filename, const char *scene, bool do_ogawa, Alembic::Abc::MetaData &md) +{ + /* Use stream to support unicode character paths on Windows. */ + if (do_ogawa) { +#ifdef WIN32 + UTF16_ENCODE(filename); + std::wstring wstr(filename_16); + m_outfile.open(wstr.c_str(), std::ios::out | std::ios::binary); + UTF16_UN_ENCODE(filename); +#else + m_outfile.open(filename, std::ios::out | std::ios::binary); +#endif + } + + m_archive = create_archive(&m_outfile, + filename, + scene, + md, + do_ogawa); +} + +OArchive ArchiveWriter::archive() +{ + return m_archive; +} diff --git a/source/blender/alembic/intern/abc_archive.h b/source/blender/alembic/intern/abc_archive.h new file mode 100644 index 00000000000..68d7a5ca70d --- /dev/null +++ b/source/blender/alembic/intern/abc_archive.h @@ -0,0 +1,66 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2016 Kévin Dietrich. + * All rights reserved. + * + * ***** END GPL LICENSE BLOCK ***** + * + */ + +#ifndef __ABC_ARCHIVE_H__ +#define __ABC_ARCHIVE_H__ + +#include <Alembic/Abc/All.h> + +#ifdef WITH_ALEMBIC_HDF5 +# include <Alembic/AbcCoreHDF5/All.h> +#endif + +#include <Alembic/AbcCoreOgawa/All.h> + +#include <fstream> + +/* Wrappers around input and output archives. The goal is to be able to use + * streams so that unicode paths work on Windows (T49112), and to make sure that + * the stream objects remain valid as long as the archives are open. + */ + +class ArchiveReader { + Alembic::Abc::IArchive m_archive; + std::ifstream m_infile; + std::vector<std::istream *> m_streams; + +public: + explicit ArchiveReader(const char *filename); + + bool valid() const; + + Alembic::Abc::IObject getTop(); +}; + +class ArchiveWriter { + Alembic::Abc::OArchive m_archive; + std::ofstream m_outfile; + +public: + explicit ArchiveWriter(const char *filename, const char *scene, bool do_ogawa, Alembic::Abc::MetaData &md); + + Alembic::Abc::OArchive archive(); +}; + +#endif /* __ABC_ARCHIVE_H__ */ diff --git a/source/blender/alembic/intern/abc_exporter.cc b/source/blender/alembic/intern/abc_exporter.cc index 764b1533309..bf1771278a8 100644 --- a/source/blender/alembic/intern/abc_exporter.cc +++ b/source/blender/alembic/intern/abc_exporter.cc @@ -24,16 +24,7 @@ #include <cmath> -#ifdef WITH_ALEMBIC_HDF5 -# include <Alembic/AbcCoreHDF5/All.h> -#endif - -#include <Alembic/AbcCoreOgawa/All.h> - -#ifdef WIN32 -# include "utfconv.h" -#endif - +#include "abc_archive.h" #include "abc_camera.h" #include "abc_curves.h" #include "abc_hair.h" @@ -71,54 +62,6 @@ extern "C" { using Alembic::Abc::TimeSamplingPtr; using Alembic::Abc::OBox3dProperty; - -/* ************************************************************************** */ - -/* This kinda duplicates CreateArchiveWithInfo, but Alembic does not seem to - * have a version supporting streams. */ -static Alembic::Abc::OArchive create_archive(std::ostream *ostream, - const std::string &filename, - const std::string &scene_name, - const Alembic::Abc::Argument &arg0, - const Alembic::Abc::Argument &arg1, - bool ogawa) -{ - Alembic::Abc::MetaData md = GetMetaData(arg0, arg1); - md.set(Alembic::Abc::kApplicationNameKey, "Blender"); - md.set(Alembic::Abc::kUserDescriptionKey, scene_name); - - time_t raw_time; - time(&raw_time); - char buffer[128]; - -#if defined _WIN32 || defined _WIN64 - ctime_s(buffer, 128, &raw_time); -#else - ctime_r(&raw_time, buffer); -#endif - - const std::size_t buffer_len = strlen(buffer); - if (buffer_len > 0 && buffer[buffer_len - 1] == '\n') { - buffer[buffer_len - 1] = '\0'; - } - - md.set(Alembic::Abc::kDateWrittenKey, buffer); - - Alembic::Abc::ErrorHandler::Policy policy = GetErrorHandlerPolicyFromArgs(arg0, arg1); - -#ifdef WITH_ALEMBIC_HDF5 - if (!ogawa) { - return Alembic::Abc::OArchive(Alembic::AbcCoreHDF5::WriteArchive(), filename, md, policy); - } -#else - static_cast<void>(filename); - static_cast<void>(ogawa); -#endif - - Alembic::AbcCoreOgawa::WriteArchive archive_writer; - return Alembic::Abc::OArchive(archive_writer(ostream, md), Alembic::Abc::kWrapExisting, policy); -} - /* ************************************************************************** */ ExportSettings::ExportSettings() @@ -297,26 +240,8 @@ void AbcExporter::operator()(Main *bmain, float &progress, bool &was_canceled) Alembic::AbcCoreAbstract::MetaData md; md.set("FramesPerTimeUnit", str_fps); - Alembic::Abc::Argument arg(md); - - /* Use stream to support unicode character paths on Windows. */ - if (m_settings.export_ogawa) { -#ifdef WIN32 - UTF16_ENCODE(m_filename); - std::wstring wstr(m_filename_16); - m_out_file.open(wstr.c_str(), std::ios::out | std::ios::binary); - UTF16_UN_ENCODE(m_filename); -#else - m_out_file.open(m_filename, std::ios::out | std::ios::binary); -#endif - } - - m_archive = create_archive(&m_out_file, - m_filename, - scene_name, - Alembic::Abc::ErrorHandler::kThrowPolicy, - arg, - m_settings.export_ogawa); + ArchiveWriter writer(m_filename, scene_name.c_str(), m_settings.export_ogawa, md); + m_archive = writer.archive(); /* Create time samplings for transforms and shapes. */ diff --git a/source/blender/alembic/intern/abc_exporter.h b/source/blender/alembic/intern/abc_exporter.h index 6c242f973c4..070eb9ea81a 100644 --- a/source/blender/alembic/intern/abc_exporter.h +++ b/source/blender/alembic/intern/abc_exporter.h @@ -24,7 +24,6 @@ #define __ABC_EXPORTER_H__ #include <Alembic/Abc/All.h> -#include <fstream> #include <map> #include <set> #include <vector> @@ -76,7 +75,6 @@ class AbcExporter { const char *m_filename; - std::ofstream m_out_file; Alembic::Abc::OArchive m_archive; unsigned int m_trans_sampling_index, m_shape_sampling_index; diff --git a/source/blender/alembic/intern/alembic_capi.cc b/source/blender/alembic/intern/alembic_capi.cc index 477448f1835..d332b4b98bc 100644 --- a/source/blender/alembic/intern/alembic_capi.cc +++ b/source/blender/alembic/intern/alembic_capi.cc @@ -22,19 +22,9 @@ #include "../ABC_alembic.h" -#ifdef WITH_ALEMBIC_HDF5 -# include <Alembic/AbcCoreHDF5/All.h> -#endif - -#include <Alembic/AbcCoreOgawa/All.h> #include <Alembic/AbcMaterial/IMaterial.h> -#include <fstream> - -#ifdef WIN32 -# include "utfconv.h" -#endif - +#include "abc_archive.h" #include "abc_camera.h" #include "abc_curves.h" #include "abc_hair.h" @@ -83,13 +73,10 @@ extern "C" { using Alembic::Abc::Int32ArraySamplePtr; using Alembic::Abc::ObjectHeader; -using Alembic::AbcGeom::ErrorHandler; -using Alembic::AbcGeom::Exception; using Alembic::AbcGeom::MetaData; using Alembic::AbcGeom::P3fArraySamplePtr; using Alembic::AbcGeom::kWrapExisting; -using Alembic::AbcGeom::IArchive; using Alembic::AbcGeom::ICamera; using Alembic::AbcGeom::ICurves; using Alembic::AbcGeom::ICurvesSchema; @@ -115,95 +102,16 @@ using Alembic::AbcGeom::V3fArraySamplePtr; using Alembic::AbcMaterial::IMaterial; -static IArchive open_archive(const std::string &filename, - const std::vector<std::istream *> &input_streams, - bool &is_hdf5) -{ - try { - is_hdf5 = false; - Alembic::AbcCoreOgawa::ReadArchive archive_reader(input_streams); - - return IArchive(archive_reader(filename), - kWrapExisting, - ErrorHandler::kThrowPolicy); - } - catch (const Exception &e) { - std::cerr << e.what() << '\n'; - -#ifdef WITH_ALEMBIC_HDF5 - try { - is_hdf5 = true; - Alembic::AbcCoreAbstract::ReadArraySampleCachePtr cache_ptr; - - return IArchive(Alembic::AbcCoreHDF5::ReadArchive(), - filename.c_str(), ErrorHandler::kThrowPolicy, - cache_ptr); - } - catch (const Exception &) { - std::cerr << e.what() << '\n'; - return IArchive(); - } -#else - return IArchive(); -#endif - } - - return IArchive(); -} - -/* Wrapper around an archive to be able to use streams so that unicode paths - * work on Windows (T49112), and to make sure the input stream remains valid as - * long as the archive is open. */ -class ArchiveWrapper { - IArchive m_archive; - std::ifstream m_infile; - std::vector<std::istream *> m_streams; - -public: - explicit ArchiveWrapper(const char *filename) - { -#ifdef WIN32 - UTF16_ENCODE(filename); - std::wstring wstr(filename_16); - m_infile.open(wstr.c_str(), std::ios::in | std::ios::binary); - UTF16_UN_ENCODE(filename); -#else - m_infile.open(filename, std::ios::in | std::ios::binary); -#endif - - m_streams.push_back(&m_infile); - - bool is_hdf5; - m_archive = open_archive(filename, m_streams, is_hdf5); - - /* We can't open an HDF5 file from a stream, so close it. */ - if (is_hdf5) { - m_infile.close(); - m_streams.clear(); - } - } - - bool valid() const - { - return m_archive.valid(); - } - - IObject getTop() - { - return m_archive.getTop(); - } -}; - struct AbcArchiveHandle { int unused; }; -ABC_INLINE ArchiveWrapper *archive_from_handle(AbcArchiveHandle *handle) +ABC_INLINE ArchiveReader *archive_from_handle(AbcArchiveHandle *handle) { - return reinterpret_cast<ArchiveWrapper *>(handle); + return reinterpret_cast<ArchiveReader *>(handle); } -ABC_INLINE AbcArchiveHandle *handle_from_archive(ArchiveWrapper *archive) +ABC_INLINE AbcArchiveHandle *handle_from_archive(ArchiveReader *archive) { return reinterpret_cast<AbcArchiveHandle *>(archive); } @@ -301,7 +209,7 @@ static void gather_objects_paths(const IObject &object, ListBase *object_paths) AbcArchiveHandle *ABC_create_handle(const char *filename, ListBase *object_paths) { - ArchiveWrapper *archive = new ArchiveWrapper(filename); + ArchiveReader *archive = new ArchiveReader(filename); if (!archive->valid()) { delete archive; @@ -636,7 +544,7 @@ static void import_startjob(void *user_data, short *stop, short *do_update, floa data->do_update = do_update; data->progress = progress; - ArchiveWrapper *archive = new ArchiveWrapper(data->filename); + ArchiveReader *archive = new ArchiveReader(data->filename); if (!archive->valid()) { delete archive; @@ -864,7 +772,7 @@ void ABC_import(bContext *C, const char *filepath, float scale, bool is_sequence void ABC_get_transform(AbcArchiveHandle *handle, Object *ob, const char *object_path, float r_mat[4][4], float time, float scale) { - ArchiveWrapper *archive = archive_from_handle(handle); + ArchiveReader *archive = archive_from_handle(handle); if (!archive || !archive->valid()) { return; @@ -1140,7 +1048,7 @@ DerivedMesh *ABC_read_mesh(AbcArchiveHandle *handle, const char **err_str, int read_flag) { - ArchiveWrapper *archive = archive_from_handle(handle); + ArchiveReader *archive = archive_from_handle(handle); if (!archive || !archive->valid()) { *err_str = "Invalid archive!"; |