File: BrainModelSurfaceBorderToPaintConverter.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 (153 lines) | stat: -rw-r--r-- 5,054 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
#include <limits>

#include "BorderProjectionFile.h"
#include "BrainModelSurface.h"
#include "BrainModelSurfaceBorderToPaintConverter.h"
#include "DebugControl.h"
#include "PaintFile.h"
#include "TopologyFile.h"
#include "TopologyHelper.h"

// constructor
BrainModelSurfaceBorderToPaintConverter::BrainModelSurfaceBorderToPaintConverter(
                           BrainSet* bs,
                           BrainModelSurface* surfaceIn,
                           BorderProjectionFile* borderProjectionFileIn,
                           PaintFile* paintFileIn,
                           int paintColumnNumberIn,
                           QString paintColumnNameIn)
        : BrainModelAlgorithm(bs),
          surface(surfaceIn),
          borderProjectionFile(borderProjectionFileIn),
          paintFile(paintFileIn),
          paintColumnNumber(paintColumnNumberIn),
          paintColumnName(paintColumnNameIn)
{
}

// destructor
BrainModelSurfaceBorderToPaintConverter::~BrainModelSurfaceBorderToPaintConverter()
{

}

// execute the algorithm
void
BrainModelSurfaceBorderToPaintConverter::execute()
                                     throw (BrainModelAlgorithmException)
{
   //
   // Verify inputs
   //
   if (surface == NULL) {
      throw BrainModelAlgorithmException("Surface is invalid.");
   }
   const int numNodes = surface->getNumberOfNodes();
   if (numNodes <= 0) {
      throw BrainModelAlgorithmException("Surface contains no nodes.");
   }
   const CoordinateFile* cf = surface->getCoordinateFile();
   const TopologyFile* topologyFile = surface->getTopologyFile();
   if (topologyFile == NULL) {
      throw BrainModelAlgorithmException("Topology is invalid.");
   }
   const TopologyHelper* th = topologyFile->getTopologyHelper(false, true, false);
   if (borderProjectionFile == NULL) {
      throw BrainModelAlgorithmException("Border Projection File is invalid.");
   }
   const int numBorders = borderProjectionFile->getNumberOfBorderProjections();
   if (numBorders <= 0) {
      throw BrainModelAlgorithmException(
              "Border Projection File contains no borders.");
   }
   if (paintFile == NULL) {
      throw BrainModelAlgorithmException("Paint File is invalid.");
   }

   //
   // Create a new paint column, if needed
   //
   if ((paintColumnNumber < 0) ||
       (paintColumnNumber >= paintFile->getNumberOfColumns())) {
      if (paintFile->getNumberOfColumns() <= 0) {
         paintFile->setNumberOfNodesAndColumns(numNodes,
                                               1);
      }
      else {
         paintFile->addColumns(1);
      }
      paintColumnNumber = paintFile->getNumberOfColumns() - 1;
   }
   paintFile->setColumnName(paintColumnNumber, paintColumnName);

   //
   // Debugging info
   //
   int debugNode = -1;
   if (DebugControl::getDebugOn()) {
      if ((DebugControl::getDebugNodeNumber() >= 0) &&
          (DebugControl::getDebugNodeNumber() < paintFile->getNumberOfNodes())) {
         debugNode = DebugControl::getDebugNodeNumber();
      }
   }

   //
   // Distance from node to nearest border point
   //
   std::vector<float> nodeToBorderDistance(numNodes, std::numeric_limits<float>::max());
   std::vector<QString> nodePaintName(numNodes, "");

   //
   // Loop through the border projections
   //
   for (int i = 0; i < numBorders; i++) {
      const BorderProjection* bp = borderProjectionFile->getBorderProjection(i);
      QString name;
      float center[3], samplingDensity, variance, topography, arealUncertainty;
      bp->getData(name, center, samplingDensity, variance,
                  topography, arealUncertainty);
      const int numLinks = bp->getNumberOfLinks();
      for (int j = 0; j < numLinks; j++) {
         const BorderProjectionLink* bpl = bp->getBorderProjectionLink(j);
         int section, nodeIndices[3];
         float nodeAreas[3], radius;
         bpl->getData(section, nodeIndices, nodeAreas, radius);

         const int n1 = nodeIndices[0];
         const int n2 = nodeIndices[1];
         const int n3 = nodeIndices[2];

         if (th->getNodeHasNeighbors(n1) &&
             th->getNodeHasNeighbors(n2) &&
             th->getNodeHasNeighbors(n3)) {

            float borderPointXYZ[3];
            bpl->unprojectLink(cf, borderPointXYZ);

            //
            // Find nodes closest to border point
            //
            for (int m = 0; m < 3; m++) {
               const int node = nodeIndices[m];
               const float distSQ = cf->getDistanceToPointSquared(node, borderPointXYZ);
               if (distSQ < nodeToBorderDistance[node]) {
                  nodePaintName[node] = name;
                  nodeToBorderDistance[node] = distSQ;
               }
            }
         }
      }
   }

   //
   // Assign the paint names to the nodes
   //
   for (int i = 0; i < numNodes; i++) {
      if (nodePaintName[i].isEmpty() == false) {
         const int paintIndex = this->paintFile->addPaintName(nodePaintName[i]);
         this->paintFile->setPaint(i, paintColumnNumber, paintIndex);
      }
   }
}