Welcome to mirror list, hosted at ThFree Co, Russian Federation.

openvdb_writer.cc « intern « openvdb « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e886c5a76a8c2cfbd81b425c76901bb7c0b47ace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * ***** 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) 2015 Blender Foundation.
 * All rights reserved.
 *
 * Contributor(s): Kevin Dietrich
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include "openvdb_writer.h"
#include "openvdb_util.h"

OpenVDBWriter::OpenVDBWriter()
    : m_grids(new openvdb::GridPtrVec())
    , m_meta_map(new openvdb::MetaMap())
    , m_save_as_half(false)
{
	m_meta_map->insertMeta("creator", openvdb::StringMetadata("Blender/Smoke"));
}

OpenVDBWriter::~OpenVDBWriter()
{}

void OpenVDBWriter::insert(const openvdb::GridBase::Ptr &grid)
{
	grid->setSaveFloatAsHalf(m_save_as_half);
	m_grids->push_back(grid);
}

void OpenVDBWriter::insert(const openvdb::GridBase &grid)
{
#if (OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER == 3)
	m_grids->push_back(grid.copyGrid());
#else
	m_grids->push_back(grid.copyGridWithNewTree());
#endif
}

void OpenVDBWriter::insertFloatMeta(const openvdb::Name &name, const float value)
{
	try {
		m_meta_map->insertMeta(name, openvdb::FloatMetadata(value));
	}
	CATCH_KEYERROR;
}

void OpenVDBWriter::insertIntMeta(const openvdb::Name &name, const int value)
{
	try {
		m_meta_map->insertMeta(name, openvdb::Int32Metadata(value));
	}
	CATCH_KEYERROR;
}

void OpenVDBWriter::insertVec3sMeta(const openvdb::Name &name, const openvdb::Vec3s &value)
{
	try {
		m_meta_map->insertMeta(name, openvdb::Vec3SMetadata(value));
	}
	CATCH_KEYERROR;
}

void OpenVDBWriter::insertVec3IMeta(const openvdb::Name &name, const openvdb::Vec3I &value)
{
	try {
		m_meta_map->insertMeta(name, openvdb::Vec3IMetadata(value));
	}
	CATCH_KEYERROR;
}

void OpenVDBWriter::insertMat4sMeta(const openvdb::Name &name, const float value[4][4])
{
	openvdb::Mat4s mat = openvdb::Mat4s(
	    value[0][0], value[0][1], value[0][2], value[0][3],
	    value[1][0], value[1][1], value[1][2], value[1][3],
	    value[2][0], value[2][1], value[2][2], value[2][3],
	    value[3][0], value[3][1], value[3][2], value[3][3]);

	try {
		m_meta_map->insertMeta(name, openvdb::Mat4SMetadata(mat));
	}
	CATCH_KEYERROR;
}

void OpenVDBWriter::setFlags(const int compression, const bool save_as_half)
{
	m_compression_flags = compression;
	m_save_as_half = save_as_half;
}

void OpenVDBWriter::write(const openvdb::Name &filename) const
{
	try {
		openvdb::io::File file(filename);
		file.setCompression(m_compression_flags);
		file.write(*m_grids, *m_meta_map);
		file.close();

		/* Should perhaps be an option at some point */
		m_grids->clear();
	}
	/* Mostly to catch exceptions related to Blosc not being supported. */
	catch (const openvdb::IoError &e) {
		std::cerr << e.what() << '\n';
	}
}