/
aiSchema.h
303 lines (242 loc) · 8.37 KB
/
aiSchema.h
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
#pragma once
class aiSampleBase;
class aiSchemaBase;
class aiSampleBase
{
public:
aiSampleBase(aiSchemaBase *schema);
virtual ~aiSampleBase();
aiSchemaBase* getSchema() const { return m_schema; }
// Note: dataChanged MUST be true if topologyChanged is
virtual void updateConfig(const aiConfig &config, bool &topologyChanged, bool &dataChanged) = 0;
public:
AbcCoreAbstract::chrono_t m_currentTimeOffset;
AbcCoreAbstract::chrono_t m_currentTimeInterval;
protected:
aiSchemaBase *m_schema;
aiConfig m_config;
};
class aiSchemaBase
{
public:
typedef std::unique_ptr<aiProperty> aiPropertyPtr;
aiSchemaBase(aiObject *obj);
virtual ~aiSchemaBase();
aiObject* getObject() const;
// config at last update time
const aiConfig& getConfig() const;
void setConfigCallback(aiConfigCallback cb, void *arg);
void setSampleCallback(aiSampleCallback cb, void *arg);
void invokeConfigCallback(aiConfig *config) const;
void invokeSampleCallback(aiSampleBase *sample, bool topologyChanged) const;
virtual void cacheSamples(int64_t startIndex, int64_t endIndex)=0;
virtual int getTimeSamplingIndex() const = 0;
virtual int getNumSamples() const = 0;
virtual aiSampleBase* updateSample(const abcSampleSelector& ss) = 0;
virtual aiSampleBase* getSample(const abcSampleSelector& ss) const = 0;
virtual int getSampleIndex(const abcSampleSelector& ss) const = 0;
virtual float getSampleTime(const abcSampleSelector& ss) const = 0;
void readConfig();
bool hasVaryingTopology() const { return m_varyingTopology; }
int getNumProperties() const;
aiProperty* getPropertyByIndex(int i);
aiProperty* getPropertyByName(const std::string& name);
protected:
virtual abcProperties getAbcProperties() = 0;
void setupProperties();
void updateProperties(const abcSampleSelector& ss);
protected:
aiObject *m_obj = nullptr;
aiConfigCallback m_configCb = nullptr;
void *m_configCbArg = nullptr;
aiSampleCallback m_sampleCb = nullptr;
void *m_sampleCbArg = nullptr;
aiConfig m_config;
bool m_constant = false;
bool m_varyingTopology = false;
std::vector<aiPropertyPtr> m_properties; // sorted vector
};
template <class Traits>
class aiTSchema : public aiSchemaBase
{
public:
typedef typename Traits::SampleT Sample;
typedef std::unique_ptr<Sample> SamplePtr;
typedef std::map<int64_t, SamplePtr> SampleCont;
typedef typename Traits::AbcSchemaT AbcSchema;
typedef Abc::ISchemaObject<AbcSchema> AbcSchemaObject;
aiTSchema(aiObject *obj)
: aiSchemaBase(obj)
{
AbcSchemaObject abcObj(obj->getAbcObject(), Abc::kWrapExisting);
m_schema = abcObj.getSchema();
m_constant = m_schema.isConstant();
m_timeSampling = m_schema.getTimeSampling();
m_numSamples = static_cast<int64_t>(m_schema.getNumSamples());
setupProperties();
}
virtual ~aiTSchema()
{
if (dontUseCache() && m_theSample)
{
delete m_theSample;
}
m_samples.clear();
}
int getTimeSamplingIndex() const override
{
return m_obj->getContext()->getTimeSamplingIndex(m_schema.getTimeSampling());
}
int getSampleIndex(const abcSampleSelector& ss) const override
{
return static_cast<int>(ss.getIndex(m_timeSampling, m_numSamples));
}
float getSampleTime(const abcSampleSelector& ss) const override
{
return static_cast<float>(m_timeSampling->getSampleTime(ss.getRequestedIndex()));
}
int getNumSamples() const override
{
return static_cast<int>(m_numSamples);
}
void cacheSamples(int64_t startIndex, int64_t endIndex) override
{
if (m_constant)
return;
if (startIndex==0) readConfig();
for (int64_t i = startIndex; i< endIndex; i++)
{
auto &sp = m_samples[i];
bool unused = i == 0;
sp.reset(readSample(i, unused));
}
}
aiSampleBase* updateSample(const abcSampleSelector& ss) override;
Sample* getSample(const abcSampleSelector& ss) const override
{
DebugLog("aiTSchema::findSample(t=%f)", (float)ss.getRequestedTime());
int sampleIndex = getSampleIndex(ss);
if (dontUseCache())
{
return (sampleIndex == m_lastSampleIndex ? m_theSample : nullptr);
}
else
{
auto it = m_samples.find(sampleIndex);
return (it != m_samples.end() ? it->second.get() : nullptr);
}
}
protected:
inline bool useCache() const
{
return (!m_constant && m_config.cacheSamples);
}
inline bool dontUseCache() const
{
return (m_constant || !m_config.cacheSamples);
}
inline Sample* getSample()
{
if (dontUseCache() && m_theSample)
{
return m_theSample;
}
else
{
return nullptr;
}
}
virtual Sample* readSample(const uint64_t idx, bool &topologyChanged) = 0;
protected:
AbcGeom::ICompoundProperty getAbcProperties() override
{
return m_schema.getUserProperties();
}
protected:
AbcSchema m_schema;
SampleCont m_samples;
Sample* m_theSample = nullptr;
Abc::TimeSamplingPtr m_timeSampling;
int64_t m_numSamples = 0;
int64_t m_lastSampleIndex = -1;
};
template<class Traits>
aiSampleBase* aiTSchema<Traits>::updateSample(const abcSampleSelector& ss)
{
DebugLog("aiTSchema::updateSample()");
readConfig();
Sample* sample;
bool topologyChanged = false;
int64_t sampleIndex = getSampleIndex(ss);
if (dontUseCache())
{
if (m_samples.size() > 0)
{
DebugLog(" Clear cached samples");
m_samples.clear();
}
// don't need to check m_constant here, sampleIndex wouldn't change
if (m_theSample == 0 || sampleIndex != m_lastSampleIndex)
{
DebugLog(" Read sample");
sample = readSample(sampleIndex, topologyChanged);
m_theSample = sample;
}
else
{
DebugLog(" Update sample");
bool dataChanged = false;
sample = m_theSample;
sample->updateConfig(m_config, topologyChanged, dataChanged);
if (!m_config.forceUpdate && !dataChanged && !m_config.interpolateSamples)
{
DebugLog(" Data didn't change, nor update is forced");
sample = 0;
}
}
}
else
{
auto& sp = m_samples[sampleIndex];
if (!sp)
{
DebugLog(" Create new cache sample");
sp.reset(readSample(sampleIndex, topologyChanged));
sample = sp.get();
}
else
{
DebugLog(" Update cached sample");
bool dataChanged = false;
sample = sp.get();
sample->updateConfig(m_config, topologyChanged, dataChanged);
// force update if sample has changed from previously queried one
if (sampleIndex != m_lastSampleIndex)
{
if (m_varyingTopology)
{
topologyChanged = true;
}
}
else if (!dataChanged && !m_config.forceUpdate && !m_config.interpolateSamples)
{
DebugLog(" Data didn't change, nor update is forced");
sample = 0;
}
}
}
m_lastSampleIndex = sampleIndex;
if (sample)
{
if (!m_varyingTopology && m_config.interpolateSamples)
{
AbcCoreAbstract::chrono_t interval = m_schema.getTimeSampling()->getTimeSamplingType().getTimePerCycle();
auto indexTime = m_timeSampling->getSampleTime(sampleIndex);
sample->m_currentTimeOffset = std::max(0.0, std::min((ss.getRequestedTime() - indexTime) / interval, 1.0));
sample->m_currentTimeInterval = interval;
}
invokeSampleCallback(sample, topologyChanged);
}
updateProperties(ss);
return sample;
}