-
-
Notifications
You must be signed in to change notification settings - Fork 777
/
animGroup.cxx
310 lines (270 loc) · 7.45 KB
/
animGroup.cxx
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
305
306
307
308
309
310
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file animGroup.cxx
* @author drose
* @date 1999-02-21
*/
#include "animGroup.h"
#include "animBundle.h"
#include "config_chan.h"
#include "indent.h"
#include "datagram.h"
#include "datagramIterator.h"
#include "bamReader.h"
#include "bamWriter.h"
#include <algorithm>
using std::string;
TypeHandle AnimGroup::_type_handle;
/**
* The default constructor is protected: don't try to create an AnimGroup
* without a parent. To create an AnimChannel hierarchy, you must first
* create an AnimBundle, and use that to create any subsequent children.
*/
AnimGroup::
AnimGroup(const string &name) :
Namable(name),
_children(get_class_type()),
_root(nullptr)
{
}
/**
* Creates a new AnimGroup, just like this one, without copying any children.
* The new copy is added to the indicated parent. Intended to be called by
* make_copy() only.
*/
AnimGroup::
AnimGroup(AnimGroup *parent, const AnimGroup ©) :
Namable(copy),
_children(get_class_type())
{
if (parent != nullptr) {
parent->_children.push_back(this);
_root = parent->_root;
} else {
_root = nullptr;
}
}
/**
* Creates the AnimGroup, and adds it to the indicated parent. The only way
* to delete it subsequently is to delete the entire hierarchy.
*/
AnimGroup::
AnimGroup(AnimGroup *parent, const string &name) :
Namable(name),
_children(get_class_type())
{
nassertv(parent != nullptr);
parent->_children.push_back(this);
_root = parent->_root;
}
/**
*
*/
AnimGroup::
~AnimGroup() {
}
/**
* Returns the number of child nodes of the group.
*/
int AnimGroup::
get_num_children() const {
return _children.size();
}
/**
* Returns the nth child of the group.
*/
AnimGroup *AnimGroup::
get_child(int n) const {
nassertr(n >= 0 && n < (int)_children.size(), nullptr);
return _children[n];
}
/**
* Returns the first child found with the indicated name, or NULL if no such
* child exists. This method searches only the children of this particular
* AnimGroup; it does not recursively search the entire graph. See also
* find_child().
*/
AnimGroup *AnimGroup::
get_child_named(const string &name) const {
Children::const_iterator ci;
for (ci = _children.begin(); ci != _children.end(); ++ci) {
AnimGroup *child = (*ci);
if (child->get_name() == name) {
return child;
}
}
return nullptr;
}
/**
* Returns the first descendant found with the indicated name, or NULL if no
* such descendant exists. This method searches the entire graph beginning at
* this AnimGroup; see also get_child_named().
*/
AnimGroup *AnimGroup::
find_child(const string &name) const {
Children::const_iterator ci;
for (ci = _children.begin(); ci != _children.end(); ++ci) {
AnimGroup *child = (*ci);
if (child->get_name() == name) {
return child;
}
AnimGroup *result = child->find_child(name);
if (result != nullptr) {
return result;
}
}
return nullptr;
}
// An STL object to sort a list of children into alphabetical order.
class AnimGroupAlphabeticalOrder {
public:
bool operator()(const PT(AnimGroup) &a, const PT(AnimGroup) &b) const {
return a->get_name() < b->get_name();
}
};
/**
* Sorts the children nodes at each level of the hierarchy into alphabetical
* order. This should be done after creating the hierarchy, to guarantee that
* the correct names will match up together when the AnimBundle is later bound
* to a PlayerRoot.
*/
void AnimGroup::
sort_descendants() {
std::sort(_children.begin(), _children.end(), AnimGroupAlphabeticalOrder());
Children::iterator ci;
for (ci = _children.begin(); ci != _children.end(); ++ci) {
(*ci)->sort_descendants();
}
}
/**
* Returns the TypeHandle associated with the ValueType we are concerned with.
* This is provided to allow a bit of run-time checking that joints and
* channels are matching properly in type.
*/
TypeHandle AnimGroup::
get_value_type() const {
return TypeHandle::none();
}
/**
* Writes a one-line description of the group.
*/
void AnimGroup::
output(std::ostream &out) const {
out << get_type() << " " << get_name();
}
/**
* Writes a brief description of the group and all of its descendants.
*/
void AnimGroup::
write(std::ostream &out, int indent_level) const {
indent(out, indent_level) << *this;
if (!_children.empty()) {
out << " {\n";
write_descendants(out, indent_level + 2);
indent(out, indent_level) << "}";
}
out << "\n";
}
/**
* Writes a brief description of all of the group's descendants.
*/
void AnimGroup::
write_descendants(std::ostream &out, int indent_level) const {
Children::const_iterator ci;
for (ci = _children.begin(); ci != _children.end(); ++ci) {
(*ci)->write(out, indent_level);
}
}
/**
* Returns a copy of this object, and attaches it to the indicated parent
* (which may be NULL only if this is an AnimBundle). Intended to be called
* by copy_subtree() only.
*/
AnimGroup *AnimGroup::
make_copy(AnimGroup *parent) const {
return new AnimGroup(parent, *this);
}
/**
* Returns a full copy of the subtree at this node and below.
*/
PT(AnimGroup) AnimGroup::
copy_subtree(AnimGroup *parent) const {
PT(AnimGroup) new_group = make_copy(parent);
nassertr(new_group->get_type() == get_type(), (AnimGroup *)this);
Children::const_iterator ci;
for (ci = _children.begin(); ci != _children.end(); ++ci) {
(*ci)->copy_subtree(new_group);
}
return new_group;
}
/**
* Function to write the important information in the particular object to a
* Datagram
*/
void AnimGroup::
write_datagram(BamWriter *manager, Datagram &me) {
me.add_string(get_name());
// Write out the root
manager->write_pointer(me, this->_root);
me.add_uint16(_children.size());
for(int i = 0; i < (int)_children.size(); i++) {
manager->write_pointer(me, _children[i]);
}
}
/**
* Function that reads out of the datagram (or asks manager to read) all of
* the data that is needed to re-create this object and stores it in the
* appropiate place
*/
void AnimGroup::
fillin(DatagramIterator &scan, BamReader *manager) {
set_name(scan.get_string());
manager->read_pointer(scan);
_num_children = scan.get_uint16();
for(int i = 0; i < _num_children; i++)
{
manager->read_pointer(scan);
}
}
/**
* Takes in a vector of pointes to TypedWritable objects that correspond to
* all the requests for pointers that this object made to BamReader.
*/
int AnimGroup::
complete_pointers(TypedWritable **p_list, BamReader *) {
_root = DCAST(AnimBundle, p_list[0]);
for (int i = 1; i < _num_children+1; i++) {
if (p_list[i] == TypedWritable::Null) {
chan_cat->warning() << get_type().get_name()
<< " Ignoring null child" << std::endl;
} else {
_children.push_back(DCAST(AnimGroup, p_list[i]));
}
}
return _num_children+1;
}
/**
* Factory method to generate a AnimGroup object
*/
TypedWritable *AnimGroup::
make_from_bam(const FactoryParams ¶ms) {
AnimGroup *me = new AnimGroup;
DatagramIterator scan;
BamReader *manager;
parse_params(params, scan, manager);
me->fillin(scan, manager);
return me;
}
/**
* Factory method to generate a AnimGroup object
*/
void AnimGroup::
register_with_read_factory() {
BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
}