~ubuntu-branches/ubuntu/saucy/caret/saucy-proposed

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*LICENSE_START*/
/*
 *  Copyright 1995-2002 Washington University School of Medicine
 *
 *  http://brainmap.wustl.edu
 *
 *  This file is part of CARET.
 *
 *  CARET is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  CARET is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CARET; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
/*LICENSE_END*/



#include <QGlobalStatic>
#ifdef Q_OS_WIN32
#define NOMINMAX
#endif

#include "TopologyFile.h"
#include "TopologyHelper.h"
#include "vtkCellArray.h"
#include "vtkPolyData.h"
#include "vtkTriangleFilter.h"

/**
 * Comparison operator for EdgeInfo
 */
bool 
operator<(const TopologyEdgeInfo& e1, const TopologyEdgeInfo& e2)
{
   if (e1.node1 == e2.node1) {
      return (e1.node2 < e2.node2);
   }
   return (e1.node1 < e2.node1);
}

/**
 * Constructor.  This will contruct the topological information for the TopologyFile.
 *   "buildEdgeInfo" will allow queries to be made about which tiles are used by an edge.
 *   "buildNodeInfo" will allow queries to be made about the tiles and node neighbors of
 *   nodes.  If "sortNodeInfo" is also set the tiles and node neighbors will be sorted
 *   around each node.
 */
TopologyHelper::TopologyHelper(const TopologyFile* tf,
                               const bool buildEdgeInfo,
                               const bool buildNodeInfo,
                               const bool sortNodeInfo)
{
   nodeSortedInfoBuilt = false;
   nodeInfoBuilt       = false;
   edgeInfoBuilt = false;
   
   const int numTiles = tf->getNumberOfTiles();
   if (numTiles <= 0) {
      return;
   }
   
   if (buildNodeInfo) {
      int maxNodeNum = -1;
   
         //
      // Get the number of nodes
      //
      for (int j = 0; j < numTiles; j++) {
         int n1, n2, n3;
         tf->getTile(j, n1, n2, n3);
         if (n1 > maxNodeNum) maxNodeNum = n1;
         if (n2 > maxNodeNum) maxNodeNum = n2;
         if (n3 > maxNodeNum) maxNodeNum = n3;
      }
      
      //
      // Node indices start at zero so need to increment
      //
      maxNodeNum++; 
      
      //
      // There may be nodes that have been disconnected and not associated with any tiles
      //
      maxNodeNum = std::max(maxNodeNum, tf->getNumberOfNodes());
      
      //
      // Initialize the node structures
      //
      nodes.reserve(maxNodeNum);
      for (int i = 0; i < maxNodeNum; i++) {
         nodes.push_back(NodeInfo(i));
      }
   }
   
   //
   // Keep track of the tiles
   //
   for (int j = 0; j < numTiles; j++) {
      int n1, n2, n3;
      tf->getTile(j, n1, n2, n3);
      if (buildNodeInfo) {
         if (sortNodeInfo) {
            nodes[n1].addNeighbors(j, n2, n3);
            nodes[n2].addNeighbors(j, n3, n1);
            nodes[n3].addNeighbors(j, n1, n2);
         }
         else {
            nodes[n1].addNeighbor(n2);
            nodes[n1].addNeighbor(n3);
            nodes[n2].addNeighbor(n1);
            nodes[n2].addNeighbor(n3);
            nodes[n3].addNeighbor(n1);
            nodes[n3].addNeighbor(n2);
            nodes[n1].addTile(j);
            nodes[n2].addTile(j);
            nodes[n3].addTile(j);
         }
      }
      
      if (buildEdgeInfo) {
         addEdgeInfo(j, n1, n2);
         addEdgeInfo(j, n2, n3);
         addEdgeInfo(j, n3, n1);
      }
   }

   if (buildEdgeInfo) {
      edgeInfoBuilt = true;
   }
   
   if (buildNodeInfo) {
      nodeInfoBuilt = true;
      if (sortNodeInfo) {
         nodeSortedInfoBuilt = true;
         for (unsigned int k = 0; k < nodes.size(); k++) {
            nodes[k].sortNeighbors();
         }
      }
   }
}

/**
 * Constructor.  This will contruct the topological information for the TopologyFile.
 *   "buildEdgeInfo" will allow queries to be made about which tiles are used by an edge.
 *   "buildNodeInfo" will allow queries to be made about the tiles and node neighbors of
 *   nodes.  If "sortNodeInfo" is also set the tiles and node neighbors will be sorted
 *   around each node.
 */
TopologyHelper::TopologyHelper(vtkPolyData* vtkIn,
                               const bool buildEdgeInfo,
                               const bool buildNodeInfo,
                               const bool sortNodeInfo)
{
   vtkPolyData* vtk = vtkIn;
   
   //
   // convert triangle strips to triangles if necessary       
   //
   vtkTriangleFilter *triangleFilter = NULL;
   if (vtk->GetNumberOfStrips() > 0) {
      triangleFilter = vtkTriangleFilter::New();
      triangleFilter->SetInput(vtk);
      triangleFilter->Update();
      vtk = triangleFilter->GetOutput();
   }

   nodeSortedInfoBuilt = false;
   nodeInfoBuilt       = false;
   edgeInfoBuilt = false;
   
   //const int numTiles = tf->getNumberOfTiles();
   //if (numTiles <= 0) {
   //   return;
  // }
   
   if (buildNodeInfo) {
      vtkCellArray* polys = vtk->GetPolys();
      vtkIdType npts;
      vtkIdType* pts;
      const int maxNodeNum = vtk->GetNumberOfPoints();  //-1;
      for (polys->InitTraversal(); polys->GetNextCell(npts,pts); ) {
         if (npts != 3) {
            std::cerr << " Polygon is not a triangle in TopologyHelper, ignored"      
                      << std::endl;
         }
         //maxNodeNum = std::max(pts[0], maxNodeNum);
         //maxNodeNum = std::max(pts[1], maxNodeNum);
         //maxNodeNum = std::max(pts[2], maxNodeNum);
      }
      
      //
      // Node indices start at zero so need to increment
      //
      //maxNodeNum++; 
      
      //
      // Initialize the node structures
      //
      for (int i = 0; i < maxNodeNum; i++) {
         nodes.push_back(NodeInfo(i));
      }
   }
   
   //
   // Keep track of the tiles
   //
   vtkCellArray* polys = vtk->GetPolys();
   int cellId = 0;
   vtkIdType npts;
   vtkIdType* pts;
   for (polys->InitTraversal(); polys->GetNextCell(npts,pts); cellId++) {
      if (npts != 3) {
         std::cerr << " Polygon is not a triangle in TopologyHelper"      
                     << std::endl;
         return;
      }
      int n1 = pts[0];
      int n2 = pts[1];
      int n3 = pts[2];
      if (buildNodeInfo) {
         if (sortNodeInfo) {
            nodes[n1].addNeighbors(cellId, n2, n3);
            nodes[n2].addNeighbors(cellId, n3, n1);
            nodes[n3].addNeighbors(cellId, n1, n2);
         }
         else {
            nodes[n1].addNeighbor(n2);
            nodes[n1].addNeighbor(n3);
            nodes[n2].addNeighbor(n1);
            nodes[n2].addNeighbor(n3);
            nodes[n3].addNeighbor(n1);
            nodes[n3].addNeighbor(n2);
            nodes[n1].addTile(cellId);
            nodes[n2].addTile(cellId);
            nodes[n3].addTile(cellId);
         }
      }
      
      if (buildEdgeInfo) {
         addEdgeInfo(cellId, n1, n2);
         addEdgeInfo(cellId, n2, n3);
         addEdgeInfo(cellId, n3, n1);
      }
   }

   if (buildEdgeInfo) {
      edgeInfoBuilt = true;
   }
   
   if (buildNodeInfo) {
      nodeInfoBuilt = true;
      if (sortNodeInfo) {
         nodeSortedInfoBuilt = true;
         for (unsigned int k = 0; k < nodes.size(); k++) {
            nodes[k].sortNeighbors();
         }
      }
   }
}

/**
 * Destructor.
 */
TopologyHelper::~TopologyHelper()
{
   nodes.clear();
   topologyEdges.clear();
}

/**
 * Keep track of edges
 */
void
TopologyHelper::addEdgeInfo(const int tileNum, const int node1, const int node2)
{
   TopologyEdgeInfo edge(tileNum, node1, node2);
   
   std::set<TopologyEdgeInfo>::iterator iter = topologyEdges.find(edge);
   if (iter == topologyEdges.end()) {
      topologyEdges.insert(edge);
   }
   else {
      TopologyEdgeInfo& e = (TopologyEdgeInfo&)(*iter);
      e.addTile(tileNum);
      //iter->addTile(tileNum);
   }
}

/**
 * Sort a nodes neighbors
 */
void 
TopologyHelper::NodeInfo::sortNeighbors() 
{
   if ((edges.size() > 0) && sortMe) {
      //
      // Nodes that are on the edge (boundary) of a cut surface must be treated specially when sorting.
      // In this case the sorting needs to start with the link whose first node is not used
      // in any other link.  This is true if all tiles are consistently oriented (and they
      // must be).
      //
      int startEdge = -1;
      for (unsigned int k = 0; k < edges.size(); k++) {
         const int nodeNum = edges[k].node1;
         bool foundNode = false;
         
         //
         // See if nodeNum is used in any other edges
         //
         for (unsigned int m = 0; m < edges.size(); m++) {
            if (m != k) {
               if (edges[m].containsNode(nodeNum) == true) {
                  foundNode = true;
                  break;
               }
            }
         }
         
         if (foundNode == false) {
            if (startEdge >= 0) {
               //std::cerr << "INFO: multiple starting edges for node " << nodeNumber << std::endl;
            }
            else {
               startEdge = k;
            }
         }
      }
      
      bool flag = false;
      if ((nodeNumber >= 44508) && (nodeNumber <= 44507)) { // modify for debugging
         std::cerr << "Start Edge for node " << nodeNumber << " is " << startEdge << std::endl;
         for (unsigned int k = 0; k < edges.size(); k++) {
            std::cerr << "   " << edges[k].node1 << " " << edges[k].node2 << std::endl;
         }
         flag = true;
      }

      // startEdge is less than zero if the node's links are all interior links 
      if (startEdge < 0) {
         startEdge = 0;
      }
      
      int currentNode  = edges[startEdge].node1;
      int nextNeighbor = edges[startEdge].node2;
      neighbors.push_back(currentNode);
      tiles.push_back(edges[startEdge].tileNumber);
      const int firstNode = currentNode;
      
      for (unsigned int i = 1; i < edges.size(); i++) {
         neighbors.push_back(nextNeighbor);
         // find edge with node "nextNeighbor" but without node "currentNode"
         const NodeEdgeInfo* e = findEdgeWithPoint(nextNeighbor, currentNode);
         if (e != NULL) {
            tiles.push_back(e->tileNumber);
            currentNode = nextNeighbor;
            nextNeighbor = e->getOtherNode(nextNeighbor);
            if (nextNeighbor < 0) {
               //std::cerr << "INFO: Unable to find neighbor of"
               //            <<  nextNeighbor << std::endl;
            }
         }
         else {
            nextNeighbor = -1;
            //std::cerr << "INFO: Unable to find edge for for node " 
            //            << nodeNumber << std::endl;
            break;
         }
      }
      
      if ((nextNeighbor != firstNode) && (nextNeighbor >= 0)) {
         neighbors.push_back(nextNeighbor);
      }
      
      if (flag) {
         std::cerr << "Node " << nodeNumber << " neighbors: ";
         for (unsigned int n = 0; n < neighbors.size(); n++) {
            std::cerr << "  " << neighbors[n];
         }
         std::cerr << std::endl;
         std::cerr << "Node " << nodeNumber << " tiles: ";
         for (unsigned int n = 0; n < tiles.size(); n++) {
            std::cerr << "  " << tiles[n];
         }
         std::cerr << std::endl;
      }
   }
   edges.clear();
}

/**
 * See if a node has any neighbors
 */
bool
TopologyHelper::getNodeHasNeighbors(const int nodeNum) const
{
   if ((nodeNum >= 0) && (nodeNum < static_cast<int>(nodes.size()))) {
      return (nodes[nodeNum].neighbors.empty() == false);
   }
   return false;
}

/**
 * Get the number of neighbors for a node.
 */
int 
TopologyHelper::getNodeNumberOfNeighbors(const int nodeNum) const
{
   if ((nodeNum >= 0) && (nodeNum < static_cast<int>(nodes.size()))) {
      return nodes[nodeNum].neighbors.size();
   }
   return 0;
}

/**
 * Get the number of boundary edges used by node.
 */
void 
TopologyHelper::getNumberOfBoundaryEdgesForAllNodes(std::vector<int>& numBoundaryEdgesPerNode) const
{
   //
   // should already be allocated
   //
   const int numNodes = getNumberOfNodes();
   if (static_cast<int>(numBoundaryEdgesPerNode.size()) < numNodes) {
      numBoundaryEdgesPerNode.resize(numNodes);
   }
   std::fill(numBoundaryEdgesPerNode.begin(), numBoundaryEdgesPerNode.end(), 0);
   
   //
   // Loop through all edges
   //
   for (std::set<TopologyEdgeInfo>::const_iterator iter = topologyEdges.begin(); 
        iter != topologyEdges.end(); iter++) {
      //
      // Get the tiles
      //
      int tile1, tile2;
      iter->getTiles(tile1, tile2);
      
      //
      // If edges has only one tile
      //
      if ((tile1 >= 0) && (tile2 < 0)) {
         //
         // Increment the count for the node
         //
         int node1, node2;
         iter->getNodes(node1, node2);
         numBoundaryEdgesPerNode[node1]++;
         numBoundaryEdgesPerNode[node2]++;
      }
   }
}
      
/**
 * Get the maximum number of neighbors of all nodes.
 */
int
TopologyHelper::getMaximumNumberOfNeighbors() const
{
   unsigned int maxNeighbors = 0;
   int numNodes = getNumberOfNodes();
   for (int i = 0; i < numNodes; i++) {
      const unsigned int num = nodes[i].neighbors.size();
      if (num > maxNeighbors) {
         maxNeighbors = num;
      }
   }
   return maxNeighbors;
}

/**
 * Get the neighboring nodes for a node
 */
void
TopologyHelper::getNodeNeighbors(const int nodeNum,
                                 std::vector<int>& neighborsOut) const
{
   if ((nodeNum >= 0) && (nodeNum < static_cast<int>(nodes.size()))) {
      neighborsOut = nodes[nodeNum].neighbors;
   }
   else {
      neighborsOut.clear();
   }
}

/**
 * Get the neighbors of a node to a specified depth.
 */
void 
TopologyHelper::getNodeNeighborsToDepth(const int rootNode, 
                                        const int depth,
                                        std::vector<int>& neighborsOut) const
{
   neighborsOut.clear();
   
   const int numNodes = getNumberOfNodes();
   std::vector<int> nodeVisited(numNodes, 0);
   
   std::set<int> nodesMarked;
   nodesMarked.insert(rootNode);
   
   for (int dp = 0; dp < depth; dp++) {
      std::set<int> newNodes;
      
      for (std::set<int>::iterator it = nodesMarked.begin();
           it != nodesMarked.end(); it++) {
         const int node = *it; 
         
         if (nodeVisited[node] == 0) {
            nodeVisited[node] = 1;
            //
            // Get all of the nodes neighbors
            //
            const std::vector<int>& neighbors = nodes[node].neighbors;
            const unsigned int numNeighs = neighbors.size();
            
            for (unsigned int j = 0; j < numNeighs; j++) {
               const int n = neighbors[j];
               if (nodeVisited[n] == 0) {
                  newNodes.insert(n);
               }
            }
         }
      }
      
      nodesMarked.insert(newNodes.begin(), newNodes.end());
   }
   
   for (std::set<int>::iterator it = nodesMarked.begin();
        it != nodesMarked.end(); it++) {
      const int node = *it; 
      if (node != rootNode) {
         neighborsOut.push_back(node);
      }
   }
   
/*   
   const int numNodes = getNumberOfNodes();

   //
   // Neighbors found to depth
   //
   std::vector<int> neighborsFound(numNodes, 0);
   
   //
   // Mark root node as found
   //
   neighborsFound[rootNode] = 1;
   
   //
   // Find nodes to the specified depth
   //
   for (int dp = 0; dp <= depth; dp++) {
      for (int i = 0; i < numNodes; i++) {
         //
         // If this node is not marked yet
         //
         if (neighborsFound[i] == 0) {
            //
            // Get all of the nodes neighbors
            //
            int numNeighs = 0;
            const int* nodeNeighbors = getNodeNeighbors(i, numNeighs);
            
            //
            // Add nodes neighbors for next iteration
            //
            for (int k = 0; k < numNeighs; k++) {
               //
               // If a neighbor is marked
               //
               const int n = nodeNeighbors[k];
               if (neighborsFound[n]) {
                  //
                  // Mark me
                  //
                  neighborsFound[i] = 1;
                  break;
               }
            }
         }
      }
   }
   
   for (int i = 0; i < numNodes; i++) {
      if (i != rootNode) {
         if (neighborsFound[i]) {
            neighborsOut.push_back(i);
         }
      }
   }   
*/

/*   
   //
   // Neighbors found to depth
   //
   std::set<int> neighborsFound;
   
   //
   // Keep track of nodes visited so far
   //
   std::vector<int> visited(numNodes, false);
   
   //
   // List of nodes to search at each depth iteration
   //
   std::vector<int> nodesToSearch;
   nodesToSearch.push_back(rootNode);
   
   //
   // Find nodes to the specified depth
   //
   for (int dp = 0; dp <= depth; dp++) {
      //
      // Nodes for next depth iteration (use set so list is unique)
      //
      std::set<int> nodesForNextIteration;
      
      //
      // For nodes that are to be searched
      //
      const int numToSearch = static_cast<int>(nodesToSearch.size());
      for (int i = 0; i < numToSearch; i++) {
         //
         // Node that is to be searched
         //
         const int node = nodesToSearch[i];
         
         //
         // If this node has not already been visited
         //
         if (visited[node] == false) {
            //
            // Mark this node visited
            //
            visited[node] = true;
            
            //
            // Add it to the list of neighbors
            //
            neighborsFound.insert(node);
            
            //
            // Get all of the nodes neighbors
            //
            int numNeighs = 0;
            const int* nodeNeighbors = getNodeNeighbors(node, numNeighs);
            
            //
            // Add nodes neighbors for next iteration
            //
            for (int k = 0; k < numNeighs; k++) {
               const int n = nodeNeighbors[k];
               if (visited[n] == false) {
                  nodesForNextIteration.insert(n);
               }
            }
         }
      }
      
      //
      // Update list of neighbors to search for next iteration
      //
      nodesToSearch.clear();
      nodesToSearch.insert(nodesToSearch.end(),
                           nodesForNextIteration.begin(), nodesForNextIteration.end());
   }

   //
   // Set the output neighbors exluding the input node
   //
   for (std::set<int>::iterator iter = neighborsFound.begin();
        iter != neighborsFound.end(); iter++) {
      const int n = *iter;
      if (n != rootNode) {
         neighborsOut.push_back(n);
      }
   }
*/
}
      
/**
 * Get the neighboring nodes for a node.  Returns a pointer to an array
 * containing the neighbors.
 */
const int*
TopologyHelper::getNodeNeighbors(const int nodeNum, int& numNeighborsOut) const
{
   if ((nodeNum >= 0) && (nodeNum < static_cast<int>(nodes.size()))) {
      numNeighborsOut = static_cast<int>(nodes[nodeNum].neighbors.size());
      if (numNeighborsOut <= 0) {
         return NULL;
      }
      return &nodes[nodeNum].neighbors[0];
   }
   numNeighborsOut = 0;
   return NULL;
}

/**
 * Get the tiles that use this node.
 */
void
TopologyHelper::getNodeTiles(const int nodeNum, std::vector<int>& tilesOut) const
{
   if ((nodeNum >= 0) && (nodeNum < static_cast<int>(nodes.size()))) {
      tilesOut = nodes[nodeNum].tiles;
   }
   else {
      tilesOut.clear();
   }
}