12 #include <tracking/trackFindingCDC/ca/AutomatonCell.h>
13 #include <tracking/trackFindingCDC/numerics/Weight.h>
15 #include <tracking/trackFindingCDC/utilities/WeightedRelation.h>
17 #include <framework/logging/Logger.h>
27 namespace TrackFindingCDC {
31 template<
class ACellHolder>
32 class CellularAutomaton {
36 class CycleException {};
45 ACellHolder*
applyTo(
const std::vector<ACellHolder*>& cellHolders,
46 const std::vector<WeightedRelation<ACellHolder>>& cellHolderRelations)
const
48 B2ASSERT(
"Expected the relations to be sorted",
49 std::is_sorted(cellHolderRelations.begin(), cellHolderRelations.end()));
54 for (ACellHolder* cellHolder : cellHolders) {
69 }
catch (CycleException) {
81 auto lessStartCellState = [](ACellHolder * lhs, ACellHolder * rhs) {
88 auto itStartCellHolder =
89 std::max_element(cellHolders.begin(), cellHolders.end(), lessStartCellState);
90 if (itStartCellHolder == cellHolders.end())
return nullptr;
91 if (not(*itStartCellHolder)->getAutomatonCell().hasStartFlag())
return nullptr;
92 return *itStartCellHolder;
102 const std::vector<WeightedRelation<ACellHolder>>& cellHolderRelations)
const
104 AutomatonCell& cell = cellHolder->getAutomatonCell();
107 if (cell.hasCycleFlag()) {
108 B2DEBUG(100,
"Cycle detected");
109 throw (CycleException());
112 if (cell.hasAssignedFlag()) {
113 return cell.getCellState();
119 Weight finalCellState =
updateState(cellHolder, cellHolderRelations);
122 cell.unsetCycleFlag();
123 return finalCellState;
129 const std::vector<WeightedRelation<ACellHolder>>& cellHolderRelations)
const
131 AutomatonCell& cell = cellHolder->getAutomatonCell();
135 if (cell.hasMaskedFlag()) {
136 cell.setAssignedFlag();
141 Weight maxStateWithContinuation = NAN;
144 bool isPriorityPath =
false;
146 auto continuations = asRange(
147 std::equal_range(cellHolderRelations.begin(), cellHolderRelations.end(), cellHolder));
152 ACellHolder* neighborCellHolder = relation.getTo();
155 if (not neighborCellHolder)
continue;
157 AutomatonCell& neighborCell = neighborCellHolder->getAutomatonCell();
165 Weight neighborCellState =
getFinalCellState(neighborCellHolder, cellHolderRelations);
168 Weight stateWithContinuation = neighborCellState + relation.getWeight();
171 if (std::isnan(maxStateWithContinuation) or maxStateWithContinuation < stateWithContinuation) {
172 maxStateWithContinuation = stateWithContinuation;
179 if (std::isnan(maxStateWithContinuation)) {
181 maxStateWithContinuation = 0;
185 maxStateWithContinuation += cell.getCellWeight();
188 cell.setCellState(maxStateWithContinuation);
189 cell.setPriorityPathFlag(isPriorityPath);
192 cell.setAssignedFlag();
195 return cell.getCellState();
205 for (ACellHolder* cellHolder : cellHolders) {
206 AutomatonCell& cell = cellHolder->getAutomatonCell();
207 cell.unsetTemporaryFlags();
208 if (cell.hasMaskedFlag())
continue;
209 cell.setCellState(NAN);