AGXDynamics  2.31.1.0
PowerLineTraverserImpl.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 
18 #ifndef AGXPOWERLINE_POWER_LINE_TRAVERSER_IMPL_H
19 #define AGXPOWERLINE_POWER_LINE_TRAVERSER_IMPL_H
20 
24 
25 #include <agx/Vector.h>
27 
28 
29 
30 namespace agxPowerLine
31 {
32  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
34  : m_typesToVisit(ALL_GRAPH_NODES)
35  , m_allVisitedSuccessfully(true)
36  {
37  }
38 
39 
40 
41  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
43  UnitPtr source,
44  TraversalMode mode)
45  {
46  return this->traverse(agxPowerLine::UNITS, source, mode);
47  }
48 
49 
50 
51  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
53  UnitPtr source,
54  TraversalMode mode)
55  {
56  return this->traverse(agxPowerLine::DIMENSIONS, source, mode);
57  }
58 
59 
60 
61  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
63  UnitPtr source,
64  TraversalMode mode)
65  {
66  return this->traverse(agxPowerLine::CONNECTORS, source, mode);
67  }
68 
69 
70 
71 
72 
73  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
75  DimensionPtr dimension,
76  TraversalMode mode)
77  {
78  // std::cout << "Visiting dimension " << dimension->getBody()->getStaticTypeName() << std::endl;
79  if (m_typesToVisit & agxPowerLine::DIMENSIONS)
80  {
81  m_allVisitedSuccessfully &= this->visit(dimension);
82  }
83 
84  for (auto it = ConnectionIterator<DimensionPtr>(dimension); it.isValid(); ++it)
85  {
86  ConnectorPtr connector = (*it)->getConnector();
87  this->approachConnector(connector, mode);
88  }
89  }
90 
91 
92 
93  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
95  ConnectorPtr connector,
96  TraversalMode mode)
97  {
98  if (m_seenConnectors.contains(connector))
99  {
100  return;
101  }
102 
103  switch (mode)
104  {
105  case CONNECTIONS_AND_COMPOUNDS:
106  if (SubGraphPtr root = connector->getRootEnclosingGraph())
107  {
108  this->collectCompoundNode(root);
109  }
110  else
111  {
112  this->traverseToConnector(connector, CONNECTIONS_AND_COMPOUNDS);
113  }
114  break;
115  case CONNECTIONS_ONLY:
116  this->traverseToConnector(connector, CONNECTIONS_ONLY);
117  break;
118  }
119  }
120 
121 
122 
123  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
125  UnitPtr unit,
126  TraversalMode mode)
127  {
128  if (m_seenUnits.contains(unit))
129  {
130  return;
131  }
132 
133  switch (mode)
134  {
135  case CONNECTIONS_AND_COMPOUNDS:
136  if (SubGraphPtr root = unit->getRootEnclosingGraph())
137  {
138  this->collectCompoundNode(root);
139  }
140  else
141  {
142  this->traverseToUnit(unit, CONNECTIONS_AND_COMPOUNDS);
143  }
144  break;
145  case CONNECTIONS_ONLY:
146  this->traverseToUnit(unit, CONNECTIONS_ONLY);
147  break;
148  }
149  }
150 
151 
152 
153  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
155  ConnectorPtr connector,
156  TraversalMode mode)
157  {
158  m_seenConnectors.insert(connector);
159 
160  if (m_typesToVisit & agxPowerLine::CONNECTORS)
161  {
162  m_allVisitedSuccessfully &= this->visit(connector);
163  }
164 
165  if (this->shouldQueueChildren(connector))
166  {
167  this->queueDimensionsInConnector(connector, mode);
168  }
169 
170  if (mode == CONNECTIONS_AND_COMPOUNDS)
171  {
172  this->traverseInternal(connector);
173  }
174  }
175 
176 
177 
178  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
180  UnitPtr unit,
181  TraversalMode mode)
182  {
183  m_seenUnits.insert(unit);
184 
185  if (m_typesToVisit & agxPowerLine::UNITS)
186  {
187  m_allVisitedSuccessfully &= this->visit(unit);
188  }
189 
190  if (this->shouldQueueChildren(unit))
191  {
192  this->queueDimensionsInUnit(unit);
193  }
194 
195  if (mode == CONNECTIONS_AND_COMPOUNDS)
196  {
197  this->traverseInternal(unit);
198  }
199  }
200 
201 
202 
203  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
205  {
206  // Can never have seen an internal Connector or Unit before because internals
207  // are always visited top-to-bottom whenever a compound SubGraph is found,
208  // regardless of where in that three the compound SubGraph is first encountered.
209 
210  for (size_t i = 0; i < root->getInternalConnectors().size(); ++i)
211  {
212  ConnectorPtr connector = root->getInternalConnectors()[i];
213  agxVerify(!m_seenConnectors.contains(connector));
214  this->traverseToConnector(connector, CONNECTIONS_AND_COMPOUNDS);
215  }
216 
217  for (size_t i = 0; i < root->getInternalUnits().size(); ++i)
218  {
219  UnitPtr unit = root->getInternalUnits()[i];
220  agxVerify(!m_seenUnits.contains(unit));
221  this->traverseToUnit(unit, CONNECTIONS_AND_COMPOUNDS);
222  }
223  }
224 
225 
226 
227 
228  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
230  ConnectorPtr connector,
231  TraversalMode mode)
232  {
233  for (auto it = ConnectionIterator<ConnectorPtr>(connector); it.isValid(); ++it)
234  {
235  DimensionPtr dimension = (*it)->getDimension();
236  if (m_seenDimensions.contains(dimension))
237  continue;
238 
239  UnitPtr unit = dimension->getUnit();
240  if (m_seenUnits.contains(unit))
241  continue;
242 
243  this->approachUnit(unit, mode);
244  }
245  }
246 
247 
248  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
250  {
251  for (auto it = PhysicalDimensionIterator<UnitPtr>(unit); it.isValid(); ++it)
252  {
253  DimensionPtr dimension = *it;
254  if (m_seenDimensions.contains(dimension))
255  continue;
256  m_seenDimensions.insert(dimension);
257  m_queue.push_back(dimension);
258  }
259  }
260 
261 
262 
263  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
265  {
266  if (UnitPtr unit = dynamic_cast<UnitPtr>(root))
267  {
268  if (!m_seenUnits.contains(unit))
269  this->traverseToUnit(unit, CONNECTIONS_AND_COMPOUNDS);
270  }
271  else if (ConnectorPtr connector = dynamic_cast<ConnectorPtr>(root))
272  {
273  if (!m_seenConnectors.contains(connector))
274  this->traverseToConnector(connector, CONNECTIONS_AND_COMPOUNDS);
275  }
276  }
277 
278 
279 
280 
281 
282 
283  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
285  GraphNodeTypes typesToVisit,
286  UnitPtr source,
287  TraversalMode mode)
288  {
289  agxVerify(source != nullptr);
290 
291  // Make sure we didn't leak state from the last traversal.
292  agxVerify(m_allVisitedSuccessfully);
293  agxVerify(m_seenConnectors.empty());
294  agxVerify(m_seenDimensions.empty());
295  agxVerify(m_seenUnits.empty());
296  agxVerify(m_queue.empty());
297 
298 
299  m_typesToVisit = typesToVisit;
300 
301  // std::cout << "\n Starting new traverse." << std::endl;
302 
303  this->approachUnit(source, mode);
304 
305  while (!m_queue.empty())
306  {
307  DimensionPtr dimension = m_queue.back();
308  m_queue.pop_back();
309 
310  this->approachDimension(dimension, mode);
311  }
312 
313  // std::cout << " Traverse complete" << std::endl;
314 
315  // Don't leak state into the next traversal.
316  bool result = m_allVisitedSuccessfully;
317  m_allVisitedSuccessfully = true;
318  m_seenConnectors.clear();
319  m_seenDimensions.clear();
320  m_seenUnits.clear();
321 
322  return result;
323  }
324 
325 
326 
327 
328 
329 
330 
331 
332 
333  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
335  {
336  return true;
337  }
338 
339  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
341  {
342  return true;
343  }
344 
345  template<typename UnitPtr, typename DimensionPtr, typename ConnectorPtr, typename SubGraphPtr>
347  return true;
348  }
349 }
350 #endif
Iterator that iterates over the non-null PhysicalDimensions of a Unit.
Base class for all power line traversers.
GraphNodeTypes
Enumeration listing the types of nodes that the PowerLineTraversers can visit.
#define agxVerify(expr)
Definition: debug.h:131
Iterator that iterates over all, both input and output, connections that have a non-null Connector...
AGXCORE_EXPORT Component * root()