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

abc_split.cpp « alembic « pointcache « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7a522a3f3c0541df95103931e75e36eef3ef77a4 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//-*****************************************************************************
//
// Copyright (c) 2009-2013,
//  Sony Pictures Imageworks, Inc. and
//  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// *       Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// *       Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// *       Neither the name of Sony Pictures Imageworks, nor
// Industrial Light & Magic nor the names of their contributors may be used
// to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//-*****************************************************************************

/*
 * Copyright 2015, Blender Foundation.
 *
 * 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.
 */

#include <map>

#include <Alembic/AbcGeom/All.h>
#include <Alembic/AbcCoreAbstract/All.h>
#include <Alembic/AbcCoreFactory/All.h>
#include <Alembic/Util/All.h>
#include <Alembic/Abc/TypedPropertyTraits.h>

#include "alembic.h"

extern "C" {
#include "BLI_string.h"
#include "BLI_utildefines.h"

#include "BKE_cache_library.h"
}

using namespace ::Alembic::AbcGeom;

namespace PTC {

static void slice_properties(ICompoundProperty iParent, OCompoundProperty out_parent, TimeSamplingPtr time_sampling, const AbcArchiveFrameFilter &filter);

static void slice_array_property(IArrayProperty iProp, OCompoundProperty out_parent, TimeSamplingPtr time_sampling, const AbcArchiveFrameFilter &filter)
{
	OArrayProperty out(out_parent, iProp.getName(), iProp.getDataType(), iProp.getMetaData(), time_sampling);
	
	ArrayPropertyReaderPtr reader = iProp.getPtr();
	ArrayPropertyWriterPtr writer = out.getPtr();
	
	size_t num_samples = iProp.getNumSamples();
	if (num_samples == 0)
		return;
	
//	index_t istart = reader->getFloorIndex(start).first;
//	index_t iend = reader->getFloorIndex(end).first;
	
	char *buf = NULL;
	
	for (index_t index = 0; index < iProp.getNumSamples(); ++index) {
		chrono_t time = time_sampling->getSampleTime(index);
		if (filter.use_time(time) || writer->getNumSamples() == 0) {
			ArraySamplePtr sample_ptr;
			reader->getSample(index, sample_ptr);
			
			writer->setSample(*sample_ptr);
		}
		else {
			writer->setFromPreviousSample();
		}
	}
	
	if (buf)
		delete[] buf;
}

static void slice_scalar_property(IScalarProperty iProp, OCompoundProperty out_parent, TimeSamplingPtr time_sampling, const AbcArchiveFrameFilter &filter)
{
	OScalarProperty out(out_parent, iProp.getName(), iProp.getDataType(), iProp.getMetaData(), time_sampling);
	
	ScalarPropertyReaderPtr reader = iProp.getPtr();
	ScalarPropertyWriterPtr writer = out.getPtr();
	size_t num_bytes = reader->getDataType().getNumBytes();
	
	size_t num_samples = iProp.getNumSamples();
	if (num_samples == 0)
		return;
	
//	index_t istart = reader->getFloorIndex(start).first;
//	index_t iend = reader->getFloorIndex(end).first;
	
	char *buf = new char[num_bytes];
	
#if 0
	if (istart > ostart) {
		/* fill the gap between start indices with the first sample,
		 * so that output sample times match input sample times as close as possible.
		 */
		for (index_t index = istart)
	}
#endif
	
	for (index_t index = 0; index < iProp.getNumSamples(); ++index) {
		chrono_t time = time_sampling->getSampleTime(index);
		if (filter.use_time(time) || writer->getNumSamples() == 0) {
			reader->getSample(index, (void*)buf);
			
			writer->setSample((void*)buf);
		}
		else {
			writer->setFromPreviousSample();
		}
	}
	
	delete[] buf;
}

static void slice_compound_property(ICompoundProperty iProp, OCompoundProperty out_parent, TimeSamplingPtr time_sampling, const AbcArchiveFrameFilter &filter)
{
	OCompoundProperty out(out_parent, iProp.getName(), iProp.getMetaData());
	
	slice_properties(iProp, out, time_sampling, filter);
}

static void slice_properties(ICompoundProperty iParent, OCompoundProperty out_parent, TimeSamplingPtr time_sampling, const AbcArchiveFrameFilter &filter)
{
	for (size_t i = 0 ; i < iParent.getNumProperties() ; i++) {
		PropertyHeader header = iParent.getPropertyHeader(i);
		
		if (header.isCompound()) {
			slice_compound_property(ICompoundProperty(iParent, header.getName()), out_parent, time_sampling, filter);
		}
		else if (header.isScalar()) {
			slice_scalar_property(IScalarProperty(iParent, header.getName()), out_parent, time_sampling, filter);
		}
		else {
			BLI_assert(header.isArray());
			slice_array_property(IArrayProperty(iParent, header.getName()), out_parent, time_sampling, filter);
		}
	}
}

typedef std::map<ObjectReaderPtr, ObjectWriterPtr> ObjectMap;
typedef std::pair<ObjectReaderPtr, ObjectWriterPtr> ObjectPair;

static void slice_object(IObject iObj, OObject out, TimeSamplingPtr time_sampling, const AbcArchiveFrameFilter &filter, ObjectMap &object_map)
{
	// Get the properties.
	slice_properties(iObj.getProperties(), out.getProperties(), time_sampling, filter);
	
	// now the child objects
	for (size_t i = 0 ; i < iObj.getNumChildren() ; i++) {
		const ObjectHeader &child_header = iObj.getChildHeader(i);
		IObject child = IObject(iObj, child_header.getName());
		
		/* Note: child instances are added later, once all actual objects have been copied */
		if (!child.isInstanceRoot()) {
			/* XXX reuse if the output object already exists.
			 * This should not happen, but currently some root objects are created
			 * in advance when opening writer archives. In the future these will not be needed
			 * and this check will become unnecessary.
			 */
			OObject out_child = out.getChild(child_header.getName());
			if (!out_child)
				out_child = OObject(out, child_header.getName(), child_header.getMetaData());
			object_map[child.getPtr()] = out_child.getPtr();
			
			slice_object(child, out_child, time_sampling, filter, object_map);
		}
	}
}

static void slice_object_instances(IObject iObj, OObject out, const ObjectMap &object_map)
{
	// now the child objects
	for (size_t i = 0 ; i < iObj.getNumChildren() ; i++) {
		const ObjectHeader &child_header = iObj.getChildHeader(i);
		IObject child = IObject(iObj, child_header.getName());
		
		if (child.isInstanceRoot()) {
			ObjectMap::const_iterator it = object_map.find(child.getPtr());
			BLI_assert(it != object_map.end());
			OObject out_target(it->second, kWrapExisting);
			
			out.addChildInstance(out_target, child_header.getName());
		}
		else {
			OObject out_child(out.getChild(child_header.getName()).getPtr(), kWrapExisting);
			slice_object_instances(child, out_child, object_map);
		}
	}
}

void abc_archive_slice(IArchive in, OArchive out, TimeSamplingPtr time_sampling, const AbcArchiveFrameFilter &filter)
{
	ObjectMap object_map;
	
	slice_object(in.getTop(), out.getTop(), time_sampling, filter, object_map);
	slice_object_instances(in.getTop(), out.getTop(), object_map);
}

} /* namespace PTC */