-
Notifications
You must be signed in to change notification settings - Fork 174
/
aiContext.cpp
304 lines (247 loc) · 7.4 KB
/
aiContext.cpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "pch.h"
#include "aiInternal.h"
#include "aiContext.h"
#include "aiObject.h"
#include "aiAsync.h"
static std::wstring L(const std::string& s)
{
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().from_bytes(s);
}
static std::string S(const std::wstring& w)
{
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(w);
}
static std::string NormalizePath(const char *in_path)
{
std::wstring path;
if (in_path != nullptr) {
path = L(in_path);
#ifdef _WIN32
size_t n = path.length();
for (size_t i = 0; i < n; ++i) {
auto c = path[i];
if (c == L'\\') {
path[i] = L'/';
}
else if (c >= L'A' && c <= L'Z') {
path[i] = L'a' + (c - L'A');
}
}
#endif
}
return S(path);
}
aiContextManager aiContextManager::s_instance;
aiContext* aiContextManager::getContext(int uid)
{
auto it = s_instance.m_contexts.find(uid);
if (it != s_instance.m_contexts.end()) {
DebugLog("Using already created context for gameObject with ID %d", uid);
return it->second.get();
}
auto ctx = new aiContext(uid);
s_instance.m_contexts[uid].reset(ctx);
DebugLog("Register context for gameObject with ID %d", uid);
return ctx;
}
void aiContextManager::destroyContext(int uid)
{
auto it = s_instance.m_contexts.find(uid);
if (it != s_instance.m_contexts.end()) {
DebugLog("Unregister context for gameObject with ID %d", uid);
s_instance.m_contexts.erase(it);
}
}
void aiContextManager::destroyContextsWithPath(const char* asset_path)
{
auto path = NormalizePath(asset_path);
for (auto it = s_instance.m_contexts.begin(); it != s_instance.m_contexts.end();) {
if (it->second->getPath() == path) {
DebugLog("Unregister context for gameObject with ID %s", it->second->getPath().c_str());
s_instance.m_contexts.erase(it++);
}
else {
++it;
}
}
}
aiContextManager::~aiContextManager()
{
if (m_contexts.size()) {
DebugWarning("%lu remaining context(s) registered", m_contexts.size());
}
m_contexts.clear();
}
aiContext::aiContext(int uid)
: m_uid(uid)
{
}
aiContext::~aiContext()
{
reset();
}
Abc::IArchive aiContext::getArchive() const
{
return m_archive;
}
const std::string& aiContext::getPath() const
{
return m_path;
}
int aiContext::getTimeSamplingCount() const
{
return (int)m_timesamplings.size();
}
aiTimeSampling * aiContext::getTimeSampling(int i)
{
return m_timesamplings[i].get();
}
void aiContext::getTimeRange(double& begin, double& end) const
{
begin = end = 0.0;
for (size_t i = 1; i < m_timesamplings.size(); ++i) {
double tmp_begin, tmp_end;
m_timesamplings[i]->getTimeRange(tmp_begin, tmp_end);
if (i == 1) {
begin = tmp_begin;
end = tmp_end;
}
else {
begin = std::min(begin, tmp_begin);
end = std::max(end, tmp_end);
}
}
}
int aiContext::getTimeSamplingCount()
{
return (int)m_timesamplings.size();
}
int aiContext::getTimeSamplingIndex(Abc::TimeSamplingPtr ts)
{
int n = m_archive.getNumTimeSamplings();
for (int i = 0; i < n; ++i) {
if (m_archive.getTimeSampling(i) == ts) {
return i;
}
}
return 0;
}
int aiContext::getUid() const
{
return m_uid;
}
const aiConfig& aiContext::getConfig() const
{
return m_config;
}
void aiContext::setConfig(const aiConfig &config)
{
m_config = config;
}
void aiContext::gatherNodesRecursive(aiObject *n)
{
auto& abc = n->getAbcObject();
size_t num_children = abc.getNumChildren();
for (size_t i = 0; i < num_children; ++i) {
auto *child = n->newChild(abc.getChild(i));
gatherNodesRecursive(child);
}
}
void aiContext::reset()
{
waitAsync();
m_top_node.reset();
m_timesamplings.clear();
m_archive.reset();
m_path.clear();
for (auto s : m_streams) { delete s; }
m_streams.clear();
// m_config is not reset intentionally
}
bool aiContext::load(const char *in_path)
{
auto path = NormalizePath(in_path);
DebugLog("aiContext::load: '%s'", path.c_str());
if (path == m_path && m_archive) {
DebugLog("Context already loaded for gameObject with id %d", m_uid);
return true;
}
DebugLog("Alembic file path changed from '%s' to '%s'. Reset context.", m_path.c_str(), path.c_str());
reset();
if (path.length() == 0) {
return false;
}
m_path = path;
if (!m_archive.valid()) {
DebugLog("Archive '%s' not yet opened", in_path);
try {
DebugLog("Trying to open AbcCoreOgawa::ReadArchive...");
// open archive with wstring path and pass it to Abc::IArchive (Abc::IArchive can not use wstring path...)
auto wpath = L(path);
auto ifs = new std::ifstream(wpath.c_str(), std::ios::in | std::ios::binary);
m_streams.push_back(ifs);
Alembic::AbcCoreOgawa::ReadArchive archive_reader(m_streams);
m_archive = Abc::IArchive(archive_reader(m_path), Abc::kWrapExisting, Abc::ErrorHandler::kThrowPolicy);
}
catch (Alembic::Util::Exception e) {
DebugLog("Failed (%s)", e.what());
// hdf5 archive can not use external stream
// (that means if path contains wide characters, we can't open it. I couldn't find solution..)
for (auto s : m_streams) { delete s; }
m_streams.clear();
try {
DebugLog("Trying to open AbcCoreHDF5::ReadArchive...");
m_archive = Abc::IArchive(AbcCoreHDF5::ReadArchive(), path);
}
catch (Alembic::Util::Exception e2) {
DebugLog("Failed (%s)", e2.what());
}
}
}
else {
DebugLog("Archive '%s' already opened", in_path);
}
if (m_archive.valid()) {
abcObject abc_top = m_archive.getTop();
m_top_node.reset(new aiObject(this, nullptr, abc_top));
gatherNodesRecursive(m_top_node.get());
m_timesamplings.clear();
auto num_time_samplings = (int)m_archive.getNumTimeSamplings();
for (int i = 0; i < num_time_samplings; ++i) {
m_timesamplings.emplace_back(aiCreateTimeSampling(m_archive, i));
}
DebugLog("Succeeded");
return true;
}
else {
DebugError("Invalid archive '%s'", in_path);
reset();
return false;
}
}
aiObject* aiContext::getTopObject() const
{
return m_top_node.get();
}
void aiContext::updateSamples(double time)
{
waitAsync();
auto ss = aiTimeToSampleSelector(time);
eachNodes([ss](aiObject& o) {
o.updateSample(ss);
});
// kick async tasks!
if (!m_async_tasks.empty()) {
aiAsyncManager::instance().queue(m_async_tasks.data(), m_async_tasks.size());
}
}
void aiContext::queueAsync(aiAsync& task)
{
m_async_tasks.push_back(&task);
}
void aiContext::waitAsync()
{
for (auto task : m_async_tasks)
task->wait();
m_async_tasks.clear();
}