forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataNode.cxx
More file actions
320 lines (292 loc) · 9.7 KB
/
dataNode.cxx
File metadata and controls
320 lines (292 loc) · 9.7 KB
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
311
312
313
314
315
316
317
318
319
320
/**
* 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 dataNode.cxx
* @author drose
* @date 2002-03-11
*/
#include "dataNode.h"
#include "dataNodeTransmit.h"
#include "config_dgraph.h"
#include "dcast.h"
using std::string;
TypeHandle DataNode::_type_handle;
/**
* Returns a newly-allocated Node that is a shallow copy of this one. It will
* be a different Node pointer, but its internal data may or may not be shared
* with that of the original Node.
*/
PandaNode *DataNode::
make_copy() const {
return new DataNode(*this);
}
/**
* Collects the data from all of the parent nodes and puts it into one
* DataNodeTransmit object, for processing; calls do_transmit_data() to read
* all the inputs and put the result into the indicated output.
*/
void DataNode::
transmit_data(DataGraphTraverser *trav,
const DataNodeTransmit inputs[],
DataNodeTransmit &output) {
DataNodeTransmit new_input;
new_input.reserve(get_num_inputs());
DataConnections::const_iterator ci;
for (ci = _data_connections.begin(); ci != _data_connections.end(); ++ci) {
const DataConnection &connect = (*ci);
const EventParameter &data =
inputs[connect._parent_index].get_data(connect._output_index);
if (!data.is_empty()) {
new_input.set_data(connect._input_index, data);
}
}
#ifndef NDEBUG
if (dgraph_cat.is_spam()) {
bool any_data = false;
Wires::const_iterator wi;
for (wi = _input_wires.begin(); wi != _input_wires.end(); ++wi) {
const string &name = (*wi).first;
const WireDef &def = (*wi).second;
if (new_input.has_data(def._index)) {
if (!any_data) {
dgraph_cat.spam()
<< *this << " receives:\n";
any_data = true;
}
dgraph_cat.spam(false)
<< " " << name << " = " << new_input.get_data(def._index)
<< "\n";
}
}
}
#endif // NDEBUG
do_transmit_data(trav, new_input, output);
#ifndef NDEBUG
if (dgraph_cat.is_spam()) {
bool any_data = false;
Wires::const_iterator wi;
for (wi = _output_wires.begin(); wi != _output_wires.end(); ++wi) {
const string &name = (*wi).first;
const WireDef &def = (*wi).second;
if (output.has_data(def._index)) {
if (!any_data) {
dgraph_cat.spam()
<< *this << " transmits:\n";
any_data = true;
}
dgraph_cat.spam(false)
<< " " << name << " = " << output.get_data(def._index)
<< "\n";
}
}
}
#endif // NDEBUG
}
/**
* Writes to the indicated ostream a list of all the inputs this DataNode
* might expect to receive.
*/
void DataNode::
write_inputs(std::ostream &out) const {
Wires::const_iterator wi;
for (wi = _input_wires.begin(); wi != _input_wires.end(); ++wi) {
const string &name = (*wi).first;
const WireDef &def = (*wi).second;
out << name << " " << def._data_type << "\n";
}
}
/**
* Writes to the indicated ostream a list of all the outputs this DataNode
* might generate.
*/
void DataNode::
write_outputs(std::ostream &out) const {
Wires::const_iterator wi;
for (wi = _output_wires.begin(); wi != _output_wires.end(); ++wi) {
const string &name = (*wi).first;
const WireDef &def = (*wi).second;
out << name << " " << def._data_type << "\n";
}
}
/**
* Writes to the indicated ostream a list of all the connections currently
* showing between this DataNode and its parent(s).
*/
void DataNode::
write_connections(std::ostream &out) const {
DataConnections::const_iterator ci;
for (ci = _data_connections.begin(); ci != _data_connections.end(); ++ci) {
const DataConnection &connect = (*ci);
nassertv(connect._parent_index >= 0 && connect._parent_index < get_num_parents());
// Now we have to search exhaustively for the input with the matching
// index number.
Wires::const_iterator wi;
bool found = false;
for (wi = _input_wires.begin(); wi != _input_wires.end() && !found; ++wi) {
const string &name = (*wi).first;
const WireDef &def = (*wi).second;
if (def._index == connect._input_index) {
out << name << " " << def._data_type << " from "
<< *get_parent(connect._parent_index) << "\n";
found = true;
}
}
nassertv(found);
}
}
/**
* Adds a new input wire with the given name and the indicated data type. The
* data type should be the TypeHandle for some type that derives from
* TypedReferenceCount, e.g. EventStoreInt, EventStoreDouble, or some fancier
* data type like Texture.
*
* If there is already an input wire defined with the indicated name, its type
* is changed.
*
* The return value is the index into the "input" parameter to
* do_transmit_data() that can be used to access the input data.
*/
int DataNode::
define_input(const string &name, TypeHandle data_type) {
// We shouldn't already be connected.
nassertr(_data_connections.empty(), 0);
Wires::iterator wi;
wi = _input_wires.find(name);
if (wi != _input_wires.end()) {
// This wire already existed; modify it and return the original index.
WireDef &def = (*wi).second;
def._data_type = data_type;
return def._index;
}
// This wire did not already exist; add it.
WireDef &def = _input_wires[name];
def._data_type = data_type;
def._index = _input_wires.size() - 1;
return def._index;
}
/**
* Adds a new output wire with the given name and the indicated data type.
* The data type should be the TypeHandle for some type that derives from
* TypedReferenceCount, e.g. EventStoreInt, EventStoreDouble, or some fancier
* data type like Texture.
*
* If there is already an output wire defined with the indicated name, its
* type is changed.
*
* The return value is the index into the "output" parameter to
* do_transmit_data() where the output data should be stored.
*/
int DataNode::
define_output(const string &name, TypeHandle data_type) {
// We shouldn't already be connected.
nassertr(_data_connections.empty(), 0);
Wires::iterator wi;
wi = _output_wires.find(name);
if (wi != _output_wires.end()) {
// This wire already existed; modify it and return the original index.
WireDef &def = (*wi).second;
def._data_type = data_type;
return def._index;
}
// This wire did not already exist; add it.
WireDef &def = _output_wires[name];
def._data_type = data_type;
def._index = _output_wires.size() - 1;
return def._index;
}
/**
* Called after a scene graph update that either adds or remove parents from
* this node, this just provides a hook for derived PandaNode objects that
* need to update themselves based on the set of parents the node has.
*/
void DataNode::
parents_changed() {
PandaNode::parents_changed();
reconnect();
}
/**
* The virtual implementation of transmit_data(). This function receives an
* array of input parameters and should generate an array of output
* parameters. The input parameters may be accessed with the index numbers
* returned by the define_input() calls that were made earlier (presumably in
* the constructor); likewise, the output parameters should be set with the
* index numbers returned by the define_output() calls.
*/
void DataNode::
do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
DataNodeTransmit &) {
}
/**
* Establishes the input(s) that this DataNode has in common with its parents'
* output(s). Builds up the _data_connections list correspondingly.
*/
void DataNode::
reconnect() {
int num_parents = get_num_parents();
_data_connections.clear();
// Look for each input among one of the parents.
int num_datanode_parents = 0;
Wires::const_iterator wi;
for (wi = _input_wires.begin(); wi != _input_wires.end(); ++wi) {
const string &name = (*wi).first;
const WireDef &input_def = (*wi).second;
int num_found = 0;
for (int i = 0; i < num_parents; i++) {
PandaNode *parent_node = get_parent(i);
if (parent_node->is_of_type(DataNode::get_class_type())) {
DataNode *data_node = DCAST(DataNode, parent_node);
num_datanode_parents++;
Wires::const_iterator pi;
pi = data_node->_output_wires.find(name);
if (pi != data_node->_output_wires.end()) {
const WireDef &output_def = (*pi).second;
num_found++;
if (output_def._data_type != input_def._data_type) {
dgraph_cat.warning()
<< "Ignoring mismatched type for connection " << name
<< " between " << *data_node << " and " << *this << "\n";
} else {
DataConnection dc;
dc._parent_index = i;
dc._output_index = output_def._index;
dc._input_index = input_def._index;
_data_connections.push_back(dc);
}
}
}
}
if (num_found > 1) {
if (dgraph_cat.is_debug()) {
dgraph_cat.debug()
<< "Multiple connections found for " << name << " into " << *this
<< "\n";
}
}
}
if (_data_connections.empty() && get_num_inputs() != 0 &&
num_datanode_parents != 0) {
dgraph_cat.warning()
<< "No data connected to " << *this << "\n";
}
}
/**
* Writes the contents of this object to the datagram for shipping out to a
* Bam file.
*/
void DataNode::
write_datagram(BamWriter *manager, Datagram &dg) {
PandaNode::write_datagram(manager, dg);
}
/**
* This internal function is called by make_from_bam to read in all of the
* relevant data from the BamFile for the new Lens.
*/
void DataNode::
fillin(DatagramIterator &scan, BamReader *manager) {
PandaNode::fillin(scan, manager);
}