File: BorderUncertaintyToArealEstimationConverter.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 (330 lines) | stat: -rw-r--r-- 11,202 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
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
/*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 <cmath>
#include <limits>

#include "ArealEstimationFile.h"
#include "BorderFile.h"
#include "BorderUncertaintyToArealEstimationConverter.h"
#include "BrainModelSurface.h"
#include "BrainSet.h"
#include "FileUtilities.h"
#include "MathUtilities.h"
#include "PaintFile.h"

/**
 * Constructor.
 */
BorderUncertaintyToArealEstimationConverter::BorderUncertaintyToArealEstimationConverter(
                                       BrainSet* bs,
                                       BrainModelSurface* surfaceIn,
                                       ArealEstimationFile* arealEstimationFileIn,
                                       BorderFile* borderFileIn,
                                       PaintFile* paintFileIn,
                                       const MODE modeIn,
                                       const int arealEstimationFileColumnIn,
                                       const QString& arealEstimationFileColumnNameIn,
                                       const QString& longNameIn,
                                       const QString& commentIn,
                                       const int paintColumnIn,
                                       const QString paintMatchNameIn,
                                       const bool overrideBorderUncertaintyIn,
                                       const float overrideBorderUncertaintyValueIn)
   : BrainModelAlgorithm(bs)
{
   surface = surfaceIn;
   arealEstimationFile = arealEstimationFileIn;
   borderFile = borderFileIn;
   paintFile = paintFileIn;
   mode = modeIn;
   arealEstimationFileColumn = arealEstimationFileColumnIn;
   arealEstimationFileColumnName = arealEstimationFileColumnNameIn;
   longName = longNameIn;
   comment = commentIn;
   paintColumn = paintColumnIn;
   paintMatchName = paintMatchNameIn;
   overrideBorderUncertainty = overrideBorderUncertaintyIn;
   overrideBorderUncertaintyValue = overrideBorderUncertaintyValueIn;

   comment.append("\nBorder File: ");
   comment.append("\nUncertainty override: ");
   comment.append(FileUtilities::basename(borderFile->getFileName()));
   if (overrideBorderUncertainty) {
      comment.append(QString::number(overrideBorderUncertaintyValue, 'f', 2));
   }
   else {
      comment.append("use values from border file.");
   }
}

//
// Find closest link in all borders from pos in X-Y plane.
//
void
BorderUncertaintyToArealEstimationConverter::findClosestBorderLinks2D(const float* pos,
                                                   std::set<BorderDist>& closestBorders)
{
   closestBorders.clear();
   const int numBorders = borderFile->getNumberOfBorders();
   for (int j = 0; j < numBorders; j++) {
      int closestLink = -1;
      float distanceToLink = std::numeric_limits<float>::max();

      Border* b = borderFile->getBorder(j);
      if (b->getDisplayFlag()) {
         const int numLinks = b->getNumberOfLinks();
         for (int i = 0; i < numLinks; i++) {
            const float* borderPos = b->getLinkXYZ(i);
            const float dx = pos[0] - borderPos[0];
            const float dy = pos[1] - borderPos[1];
            const float dist = dx*dx + dy*dy;
            if (dist < distanceToLink) {
               distanceToLink = dist;
               closestLink = i;
            }
         }

         if (closestLink >= 0) {
            distanceToLink = std::sqrt(distanceToLink);
            BorderDist bd(j, closestLink, distanceToLink);
            closestBorders.insert(bd);
         }
      }
   }
}

float
BorderUncertaintyToArealEstimationConverter::determineSignedDistance(const float* pos, 
                                                                     const BorderDist& bd)
{
   Border* border = borderFile->getBorder(bd.borderNumber);
   const float* borderPos = border->getLinkXYZ(bd.borderNearestLink);

   //
   // vector from border point to node
   //
   float bpToNode[3];
   MathUtilities::subtractVectors((float*)pos, borderPos, bpToNode);
   const float bpMag = std::sqrt(bpToNode[0]*bpToNode[0] + bpToNode[1]*bpToNode[1]);
   if (bpMag != 0.0) {
      bpToNode[0] /= bpMag;
      bpToNode[1] /= bpMag;
   }
   else {
      return 0.0;
   }
   
   //
   // signed distance of node from border point
   // Note: both bpToNode and the border normal are unit vectors so the dot
   // product of bpToNode and the border normal give the cosine of the angle
   // formed by them.
   //
   const float* flatNormal = border->getLinkFlatNormal(bd.borderNearestLink);
   const float cosAngle = bpToNode[0] * flatNormal[0] 
                        + bpToNode[1] * flatNormal[1];
                        
   const float signedDistance = (cosAngle * bpMag);
   return signedDistance;  
}

/**
 * Destructor.
 */
BorderUncertaintyToArealEstimationConverter::~BorderUncertaintyToArealEstimationConverter()
{
}

/**
 * execute the algorithm.
 */
void 
BorderUncertaintyToArealEstimationConverter::execute() throw (BrainModelAlgorithmException)
{
   const int numNodes = surface->getNumberOfNodes();
   if (numNodes <= 0) {
      throw BrainModelAlgorithmException("Surface contains nodes.");
   }
   
   //
   // Make sure we have some paints
   //
   const int numPaintColumns = paintFile->getNumberOfColumns();
   if (numPaintColumns == 0) {
      if (mode == MODE_NODES_WITH_PAINT) {
         throw BrainModelAlgorithmException("The paint file is empty.");
      }
   }
   
   //
   // Make sure there are some borders
   //
   const int numBorders = borderFile->getNumberOfBorders();
   if (numBorders <= 0) {
      throw BrainModelAlgorithmException("The border file is empty.");
   }
   
   //
   // Compute normals for flat borders
   //
   borderFile->computeFlatNormals();
   
   //
   // Determine which paint names match the name entered by the user
   //
   std::vector<bool> matchingPaints;
   switch (mode) {
      case MODE_ALL_NODES:
         break;
      case MODE_NODES_WITH_PAINT:
         {
            const int numPaintNames = paintFile->getNumberOfPaintNames();
            if (numPaintNames <= 0) {
               throw BrainModelAlgorithmException("There are no paint names.");
            }
            matchingPaints.resize(numPaintNames, false);
            for (int j = 0; j < numPaintNames; j++) {
               if (paintFile->getPaintNameFromIndex(j) == paintMatchName) {
                  matchingPaints[j] = true;
               }
            }
         }
         break;
   }
   
   //
   // Get access to surface's Areal Estimation File
   //
   if (arealEstimationFile->getNumberOfColumns() <= 0) {
      arealEstimationFile->setNumberOfNodesAndColumns(numNodes, 1);
      arealEstimationFileColumn = arealEstimationFile->getNumberOfColumns() - 1;
   }
   else {
      if ((arealEstimationFileColumn < 0) || 
          (arealEstimationFileColumn >= arealEstimationFile->getNumberOfColumns())) {
         arealEstimationFile->addColumns(1);
         arealEstimationFileColumn = arealEstimationFile->getNumberOfColumns() - 1;
      }
   }
   arealEstimationFile->setColumnName(arealEstimationFileColumn, arealEstimationFileColumnName);
   
   arealEstimationFile->setLongName(arealEstimationFileColumn, longName);
   arealEstimationFile->setColumnComment(arealEstimationFileColumn, comment);
   
   //
   // Search through the nodes
   //
   const CoordinateFile* cf = surface->getCoordinateFile();
   for (int i = 0; i < numNodes; i++) {
      //
      // See if this node should be used
      //
      bool useNode = false;
      switch (mode) {
         case MODE_ALL_NODES:
            useNode = true;
            break;
         case MODE_NODES_WITH_PAINT:
            useNode = matchingPaints[paintFile->getPaint(i, paintColumn)];
            break;
      }
      
      if (useNode) {
         std::set<BorderDist> closestBorders;
         findClosestBorderLinks2D(cf->getCoordinate(i), closestBorders);
         
         int ctr = 0;
         QString names[4] = { "NULL", "NULL", "NULL", "NULL" };
         float probabilities[4] = { 0.0, 0.0, 0.0, 0.0 };
         float sumProbability = 0.0;
         for (std::set<BorderDist>::iterator cbi = closestBorders.begin();
              cbi != closestBorders.end(); cbi++) {
            const int borderNumber = cbi->borderNumber;
            Border* border = borderFile->getBorder(borderNumber);
            const QString borderName(border->getName());
            
            //
            // Make sure border with same name is not already found
            //
            bool useIt = true;
            for (int ib = 0; ib < ctr; ib++) {
               if (borderName == names[ib]) {
                  useIt = false;
                  break;
               }
            }
            
            if (useIt) {
               names[ctr] = borderName;
               const float signedDistance = determineSignedDistance(
                                                   cf->getCoordinate(i), *cbi);
               float borderUncertainty = border->getArealUncertainty();
               if (overrideBorderUncertainty) {
                  borderUncertainty = overrideBorderUncertaintyValue;
               }
               
               float temp = ((signedDistance / borderUncertainty) - 1.0) * 0.5;
               if (temp < -10.0) {
                  temp = -10.0;
               }
               probabilities[ctr] = exp(temp);
               if (probabilities[ctr] > 1.0) {
                  probabilities[ctr] = 1.0;
               }
               
               sumProbability += probabilities[ctr];
               
               ctr++;
               if (ctr >= 4) {
                  break;
               }
            }
         }
         
         std::set<ProbabilitySort> probSort;
         for (int m = 0; m < ctr; m++) {
            ProbabilitySort ps(names[m], probabilities[m] / sumProbability);
            probSort.insert(ps);
         }
         
         int pctr =0;
         for (std::set<ProbabilitySort>::iterator pi = probSort.begin();
              pi != probSort.end(); pi++) {
            names[pctr] = pi->name;
            probabilities[pctr] = pi->probability;
            pctr++;
         }
         
         arealEstimationFile->setNodeData(i, arealEstimationFileColumn, names, probabilities);
         
      }
   }
}