Blender  V3.3
deg_builder_transitive.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2015 Blender Foundation. All rights reserved. */
3 
9 
10 #include "MEM_guardedalloc.h"
11 
12 #include "intern/node/deg_node.h"
15 
16 #include "intern/debug/deg_debug.h"
17 #include "intern/depsgraph.h"
19 
20 namespace blender::deg {
21 
22 /* -------------------------------------------------- */
23 
24 /* Performs a transitive reduction to remove redundant relations.
25  * https://en.wikipedia.org/wiki/Transitive_reduction
26  *
27  * XXX The current implementation is somewhat naive and has O(V*E) worst case
28  * runtime.
29  * A more optimized algorithm can be implemented later, e.g.
30  *
31  * http://www.sciencedirect.com/science/article/pii/0304397588900321/pdf?md5=3391e309b708b6f9cdedcd08f84f4afc&pid=1-s2.0-0304397588900321-main.pdf
32  *
33  * Care has to be taken to make sure the algorithm can handle the cyclic case
34  * too! (unless we can to prevent this case early on).
35  */
36 
37 enum {
40 };
41 
43 {
44  if (node->custom_flags & OP_VISITED) {
45  return;
46  }
48  for (Relation *rel : node->inlinks) {
50  /* Do this only in inlinks loop, so the target node does not get
51  * flagged. */
53  }
54 }
55 
57 {
58  int num_removed_relations = 0;
59  Vector<Relation *> relations_to_remove;
60 
61  for (OperationNode *target : graph->operations) {
62  /* Clear tags. */
64  node->custom_flags = 0;
65  }
66  /* Mark nodes from which we can reach the target
67  * start with children, so the target node and direct children are not
68  * flagged. */
69  target->custom_flags |= OP_VISITED;
70  for (Relation *rel : target->inlinks) {
72  }
73  /* Remove redundant paths to the target. */
74  for (Relation *rel : target->inlinks) {
75  if (rel->from->type == NodeType::TIMESOURCE) {
76  /* HACK: time source nodes don't get "custom_flags" flag
77  * set/cleared. */
78  /* TODO: there will be other types in future, so iterators above
79  * need modifying. */
80  continue;
81  }
82  if (rel->from->custom_flags & OP_REACHABLE) {
83  relations_to_remove.append(rel);
84  }
85  }
86  for (Relation *rel : relations_to_remove) {
87  rel->unlink();
88  delete rel;
89  }
90  num_removed_relations += relations_to_remove.size();
91  relations_to_remove.clear();
92  }
93  DEG_DEBUG_PRINTF((::Depsgraph *)graph, BUILD, "Removed %d relations\n", num_removed_relations);
94 }
95 
96 } // namespace blender::deg
Read Guarded memory(de)allocation.
void append(const T &value)
Definition: BLI_vector.hh:433
OperationNode * node
Depsgraph * graph
#define DEG_DEBUG_PRINTF(depsgraph, type,...)
Definition: deg_debug.h:51
void deg_graph_transitive_reduction(Depsgraph *graph)
static void deg_graph_tag_paths_recursive(Node *node)
OperationNodes operations
Definition: depsgraph.h:120
Relations inlinks
Definition: deg_node.h:173