Blender  V2.93
deg_builder_transitive.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2015 Blender Foundation.
17  * All rights reserved.
18  */
19 
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "intern/node/deg_node.h"
31 
32 #include "intern/debug/deg_debug.h"
33 #include "intern/depsgraph.h"
35 
36 namespace blender::deg {
37 
38 /* -------------------------------------------------- */
39 
40 /* Performs a transitive reduction to remove redundant relations.
41  * https://en.wikipedia.org/wiki/Transitive_reduction
42  *
43  * XXX The current implementation is somewhat naive and has O(V*E) worst case
44  * runtime.
45  * A more optimized algorithm can be implemented later, e.g.
46  *
47  * http://www.sciencedirect.com/science/article/pii/0304397588900321/pdf?md5=3391e309b708b6f9cdedcd08f84f4afc&pid=1-s2.0-0304397588900321-main.pdf
48  *
49  * Care has to be taken to make sure the algorithm can handle the cyclic case
50  * too! (unless we can to prevent this case early on).
51  */
52 
53 enum {
56 };
57 
59 {
60  if (node->custom_flags & OP_VISITED) {
61  return;
62  }
64  for (Relation *rel : node->inlinks) {
66  /* Do this only in inlinks loop, so the target node does not get
67  * flagged. */
69  }
70 }
71 
73 {
74  int num_removed_relations = 0;
75  Vector<Relation *> relations_to_remove;
76 
77  for (OperationNode *target : graph->operations) {
78  /* Clear tags. */
80  node->custom_flags = 0;
81  }
82  /* Mark nodes from which we can reach the target
83  * start with children, so the target node and direct children are not
84  * flagged. */
85  target->custom_flags |= OP_VISITED;
86  for (Relation *rel : target->inlinks) {
88  }
89  /* Remove redundant paths to the target. */
90  for (Relation *rel : target->inlinks) {
91  if (rel->from->type == NodeType::TIMESOURCE) {
92  /* HACK: time source nodes don't get "custom_flags" flag
93  * set/cleared. */
94  /* TODO: there will be other types in future, so iterators above
95  * need modifying. */
96  continue;
97  }
98  if (rel->from->custom_flags & OP_REACHABLE) {
99  relations_to_remove.append(rel);
100  }
101  }
102  for (Relation *rel : relations_to_remove) {
103  rel->unlink();
104  delete rel;
105  }
106  num_removed_relations += relations_to_remove.size();
107  relations_to_remove.clear();
108  }
109  DEG_DEBUG_PRINTF((::Depsgraph *)graph, BUILD, "Removed %d relations\n", num_removed_relations);
110 }
111 
112 } // namespace blender::deg
Read Guarded memory(de)allocation.
void append(const T &value)
Definition: BLI_vector.hh:438
OperationNode * node
Depsgraph * graph
#define DEG_DEBUG_PRINTF(depsgraph, type,...)
Definition: deg_debug.h:68
void deg_graph_transitive_reduction(Depsgraph *graph)
static void deg_graph_tag_paths_recursive(Node *node)
OperationNodes operations
Definition: depsgraph.h:126
Relations inlinks
Definition: deg_node.h:175