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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
/*
* 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.
*/
#ifndef __BKE_DERIVED_NODE_TREE_HH__
#define __BKE_DERIVED_NODE_TREE_HH__
/** \file
* \ingroup bke
*
* DerivedNodeTree provides a flattened view on a bNodeTree, i.e. node groups are inlined. It
* builds on top of NodeTreeRef and supports similar queries efficiently.
*
* Every inlined node remembers its path to the parent ("call stack").
*
* Unlinked group node inputs are handled separately from other sockets.
*
* There is a dot graph exporter for debugging purposes.
*/
#include "BKE_node_tree_ref.hh"
namespace blender::bke {
class DSocket;
class DInputSocket;
class DOutputSocket;
class DNode;
class DParentNode;
class DGroupInput;
class DerivedNodeTree;
class DSocket : NonCopyable, NonMovable {
protected:
DNode *node_;
const SocketRef *socket_ref_;
uint id_;
friend DerivedNodeTree;
public:
const DNode &node() const;
uint id() const;
uint index() const;
bool is_input() const;
bool is_output() const;
const DSocket &as_base() const;
const DInputSocket &as_input() const;
const DOutputSocket &as_output() const;
PointerRNA *rna() const;
StringRefNull idname() const;
StringRefNull name() const;
const SocketRef &socket_ref() const;
bNodeSocket *bsocket() const;
bool is_available() const;
};
class DInputSocket : public DSocket {
private:
Vector<DOutputSocket *> linked_sockets_;
Vector<DGroupInput *> linked_group_inputs_;
friend DerivedNodeTree;
public:
const InputSocketRef &socket_ref() const;
Span<const DOutputSocket *> linked_sockets() const;
Span<const DGroupInput *> linked_group_inputs() const;
bool is_linked() const;
};
class DOutputSocket : public DSocket {
private:
Vector<DInputSocket *> linked_sockets_;
friend DerivedNodeTree;
public:
const OutputSocketRef &socket_ref() const;
Span<const DInputSocket *> linked_sockets() const;
};
class DGroupInput : NonCopyable, NonMovable {
private:
const InputSocketRef *socket_ref_;
DParentNode *parent_;
Vector<DInputSocket *> linked_sockets_;
uint id_;
friend DerivedNodeTree;
public:
const InputSocketRef &socket_ref() const;
bNodeSocket *bsocket() const;
const DParentNode *parent() const;
Span<const DInputSocket *> linked_sockets() const;
uint id() const;
StringRefNull name() const;
};
class DNode : NonCopyable, NonMovable {
private:
const NodeRef *node_ref_;
DParentNode *parent_;
Span<DInputSocket *> inputs_;
Span<DOutputSocket *> outputs_;
uint id_;
friend DerivedNodeTree;
public:
const NodeRef &node_ref() const;
const DParentNode *parent() const;
Span<const DInputSocket *> inputs() const;
Span<const DOutputSocket *> outputs() const;
const DInputSocket &input(uint index) const;
const DOutputSocket &output(uint index) const;
uint id() const;
PointerRNA *rna() const;
StringRefNull idname() const;
StringRefNull name() const;
private:
void destruct_with_sockets();
};
class DParentNode : NonCopyable, NonMovable {
private:
const NodeRef *node_ref_;
DParentNode *parent_;
uint id_;
friend DerivedNodeTree;
public:
const DParentNode *parent() const;
const NodeRef &node_ref() const;
uint id() const;
};
using NodeTreeRefMap = Map<bNodeTree *, std::unique_ptr<const NodeTreeRef>>;
class DerivedNodeTree : NonCopyable, NonMovable {
private:
LinearAllocator<> allocator_;
bNodeTree *btree_;
Vector<DNode *> nodes_by_id_;
Vector<DGroupInput *> group_inputs_;
Vector<DParentNode *> parent_nodes_;
Vector<DSocket *> sockets_by_id_;
Vector<DInputSocket *> input_sockets_;
Vector<DOutputSocket *> output_sockets_;
Map<const bNodeType *, Vector<DNode *>> nodes_by_type_;
public:
DerivedNodeTree(bNodeTree *btree, NodeTreeRefMap &node_tree_refs);
~DerivedNodeTree();
Span<const DNode *> nodes() const;
Span<const DNode *> nodes_by_type(StringRefNull idname) const;
Span<const DNode *> nodes_by_type(const bNodeType *nodetype) const;
Span<const DSocket *> sockets() const;
Span<const DInputSocket *> input_sockets() const;
Span<const DOutputSocket *> output_sockets() const;
Span<const DGroupInput *> group_inputs() const;
std::string to_dot() const;
private:
/* Utility functions used during construction. */
void insert_nodes_and_links_in_id_order(const NodeTreeRef &tree_ref,
DParentNode *parent,
Vector<DNode *> &all_nodes);
DNode &create_node(const NodeRef &node_ref,
DParentNode *parent,
MutableSpan<DSocket *> r_sockets_map);
void expand_groups(Vector<DNode *> &all_nodes,
Vector<DGroupInput *> &all_group_inputs,
Vector<DParentNode *> &all_parent_nodes,
NodeTreeRefMap &node_tree_refs);
void expand_group_node(DNode &group_node,
Vector<DNode *> &all_nodes,
Vector<DGroupInput *> &all_group_inputs,
Vector<DParentNode *> &all_parent_nodes,
NodeTreeRefMap &node_tree_refs);
void create_group_inputs_for_unlinked_inputs(DNode &node,
Vector<DGroupInput *> &all_group_inputs);
void relink_group_inputs(const NodeTreeRef &group_ref,
Span<DNode *> nodes_by_id,
DNode &group_node);
void relink_group_outputs(const NodeTreeRef &group_ref,
Span<DNode *> nodes_by_id,
DNode &group_node);
void remove_expanded_group_interfaces(Vector<DNode *> &all_nodes);
void remove_unused_group_inputs(Vector<DGroupInput *> &all_group_inputs);
void store_in_this_and_init_ids(Vector<DNode *> &&all_nodes,
Vector<DGroupInput *> &&all_group_inputs,
Vector<DParentNode *> &&all_parent_nodes);
};
/* --------------------------------------------------------------------
* DSocket inline methods.
*/
inline const DNode &DSocket::node() const
{
return *node_;
}
inline uint DSocket::id() const
{
return id_;
}
inline uint DSocket::index() const
{
return socket_ref_->index();
}
inline bool DSocket::is_input() const
{
return socket_ref_->is_input();
}
inline bool DSocket::is_output() const
{
return socket_ref_->is_output();
}
inline const DSocket &DSocket::as_base() const
{
return *this;
}
inline const DInputSocket &DSocket::as_input() const
{
return *(DInputSocket *)this;
}
inline const DOutputSocket &DSocket::as_output() const
{
return *(DOutputSocket *)this;
}
inline PointerRNA *DSocket::rna() const
{
return socket_ref_->rna();
}
inline StringRefNull DSocket::idname() const
{
return socket_ref_->idname();
}
inline StringRefNull DSocket::name() const
{
return socket_ref_->name();
}
inline const SocketRef &DSocket::socket_ref() const
{
return *socket_ref_;
}
inline bNodeSocket *DSocket::bsocket() const
{
return socket_ref_->bsocket();
}
inline bool DSocket::is_available() const
{
return (socket_ref_->bsocket()->flag & SOCK_UNAVAIL) == 0;
}
/* --------------------------------------------------------------------
* DInputSocket inline methods.
*/
inline const InputSocketRef &DInputSocket::socket_ref() const
{
return socket_ref_->as_input();
}
inline Span<const DOutputSocket *> DInputSocket::linked_sockets() const
{
return linked_sockets_;
}
inline Span<const DGroupInput *> DInputSocket::linked_group_inputs() const
{
return linked_group_inputs_;
}
inline bool DInputSocket::is_linked() const
{
return linked_sockets_.size() > 0 || linked_group_inputs_.size() > 0;
}
/* --------------------------------------------------------------------
* DOutputSocket inline methods.
*/
inline const OutputSocketRef &DOutputSocket::socket_ref() const
{
return socket_ref_->as_output();
}
inline Span<const DInputSocket *> DOutputSocket::linked_sockets() const
{
return linked_sockets_;
}
/* --------------------------------------------------------------------
* DGroupInput inline methods.
*/
inline const InputSocketRef &DGroupInput::socket_ref() const
{
return *socket_ref_;
}
inline bNodeSocket *DGroupInput::bsocket() const
{
return socket_ref_->bsocket();
}
inline const DParentNode *DGroupInput::parent() const
{
return parent_;
}
inline Span<const DInputSocket *> DGroupInput::linked_sockets() const
{
return linked_sockets_;
}
inline uint DGroupInput::id() const
{
return id_;
}
inline StringRefNull DGroupInput::name() const
{
return socket_ref_->name();
}
/* --------------------------------------------------------------------
* DNode inline methods.
*/
inline const NodeRef &DNode::node_ref() const
{
return *node_ref_;
}
inline const DParentNode *DNode::parent() const
{
return parent_;
}
inline Span<const DInputSocket *> DNode::inputs() const
{
return inputs_;
}
inline Span<const DOutputSocket *> DNode::outputs() const
{
return outputs_;
}
inline const DInputSocket &DNode::input(uint index) const
{
return *inputs_[index];
}
inline const DOutputSocket &DNode::output(uint index) const
{
return *outputs_[index];
}
inline uint DNode::id() const
{
return id_;
}
inline PointerRNA *DNode::rna() const
{
return node_ref_->rna();
}
inline StringRefNull DNode::idname() const
{
return node_ref_->idname();
}
inline StringRefNull DNode::name() const
{
return node_ref_->name();
}
/* --------------------------------------------------------------------
* DParentNode inline methods.
*/
inline const DParentNode *DParentNode::parent() const
{
return parent_;
}
inline const NodeRef &DParentNode::node_ref() const
{
return *node_ref_;
}
inline uint DParentNode::id() const
{
return id_;
}
/* --------------------------------------------------------------------
* DerivedNodeTree inline methods.
*/
inline Span<const DNode *> DerivedNodeTree::nodes() const
{
return nodes_by_id_;
}
inline Span<const DNode *> DerivedNodeTree::nodes_by_type(StringRefNull idname) const
{
const bNodeType *nodetype = nodeTypeFind(idname.data());
return this->nodes_by_type(nodetype);
}
inline Span<const DNode *> DerivedNodeTree::nodes_by_type(const bNodeType *nodetype) const
{
const Vector<DNode *> *nodes = nodes_by_type_.lookup_ptr(nodetype);
if (nodes == nullptr) {
return {};
}
else {
return *nodes;
}
}
inline Span<const DSocket *> DerivedNodeTree::sockets() const
{
return sockets_by_id_;
}
inline Span<const DInputSocket *> DerivedNodeTree::input_sockets() const
{
return input_sockets_;
}
inline Span<const DOutputSocket *> DerivedNodeTree::output_sockets() const
{
return output_sockets_;
}
inline Span<const DGroupInput *> DerivedNodeTree::group_inputs() const
{
return group_inputs_;
}
} // namespace blender::bke
#endif /* __BKE_DERIVED_NODE_TREE_HH__ */
|