File: BrainModelSurfaceConnectedSearch.cxx

package info (click to toggle)
caret 5.6.4~dfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 31,904 kB
  • ctags: 28,901
  • sloc: cpp: 378,050; python: 6,718; ansic: 5,507; makefile: 333; sh: 46
file content (154 lines) | stat: -rw-r--r-- 3,652 bytes parent folder | download | duplicates (2)
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

#include <algorithm>
#include <stack>

#include "BrainModelSurface.h"
#include "BrainModelSurfaceConnectedSearch.h"
#include "TopologyFile.h"
#include "TopologyHelper.h"

/**
 * Constructor.
 */
BrainModelSurfaceConnectedSearch::BrainModelSurfaceConnectedSearch(BrainSet* bs, 
                                       const BrainModelSurface* bmsIn,
                                       const int startNodeIn,
                                       const std::vector<int>* limitToTheseNodesIn)
   : BrainModelAlgorithm(bs), 
     bms(bmsIn),
     startNode(startNodeIn),
     limitToTheseNodes(limitToTheseNodesIn),
     numNodes(0)
{
}

/**
 * Destructor.
 */
BrainModelSurfaceConnectedSearch::~BrainModelSurfaceConnectedSearch()
{
   visited.clear();
   nodeConnected.clear();
}

/**
 * execute the search.
 */
void
BrainModelSurfaceConnectedSearch::execute() throw (BrainModelAlgorithmException)
{
   numNodes = bms->getNumberOfNodes();
   if (numNodes <= 0) {
      return;
   }
   
   visited.resize(numNodes);
   nodeConnected.resize(numNodes);
   for (int i = 0; i < numNodes; i++) {
      visited[i] = false;
      nodeConnected[i] = false;
   }
   
   //
   // See if search is limited to a subset of nodes
   //
   if (limitToTheseNodes != NULL) {
      //
      // Mark nodes that are not to be searched as already visited
      //
      const int numLimitNodes = static_cast<int>(limitToTheseNodes->size());
      for (int i = 0; i < numLimitNodes; i++) {
         if (i < numNodes) {
            if ((*limitToTheseNodes)[i] == false) {
               visited[i] = true;
            }
         }
      }
   }
      
   //
   // Use a topology helper to get node neighbor information
   //
   const TopologyFile* tf = bms->getTopologyFile();
   //TopologyHelper th(tf, false, true, false);
   const TopologyHelper* th = tf->getTopologyHelper(false, true, false);
   
   //
   // Mark the starting node as connected.
   //
   nodeConnected[startNode] = true;
    
   //
   // Use a stack to help with connected node search.
   //
   std::stack<int> st;
   st.push(startNode);
  
   while(!st.empty()) {
      const int nodeNumber = st.top();
      st.pop();

      //
      // Is node unvisited ?
      //
      if (visited[nodeNumber] == false) {
         visited[nodeNumber] = true;
         
         //
         // Does node meet criteria for acceptance
         // 
         bool useNode = false;
         if (nodeNumber == startNode) {
            useNode = true;
         }
         else if (acceptNode(nodeNumber)) {
            useNode = true;
         }
         
         if (useNode) {
            nodeConnected[nodeNumber] = true;
            
            //
            // Get neighbors of this node
            //
            int numNeighbors = 0;
            const int* neighbors = th->getNodeNeighbors(nodeNumber, numNeighbors);
            
            //
            // add neighbors to search
            //
            for (int i = 0; i < numNeighbors; i++) {
               const int neighborNode = neighbors[i];
               if (visited[neighborNode] == false) {
                  st.push(neighborNode);
               }
            }
         }
      }
   }
}

/**
 * accept/reject a node for continued searching.
 */
bool
BrainModelSurfaceConnectedSearch::acceptNode(const int nodeNumber)
{
   if (nodeNumber >= 0) {
      return true;
   }
   return false;
}

/**
 * Find out if a node is connected.  Call after "execute()".
 */
bool
BrainModelSurfaceConnectedSearch::getNodeConnected(const int nodeNumber) const 
{
   if (nodeNumber < numNodes) {
      return nodeConnected[nodeNumber];
   }
   return false;
}