File: BrainModelSurfaceDistortion.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 (282 lines) | stat: -rw-r--r-- 9,449 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
#include <algorithm>
#include <cmath>

#include "BrainModelSurface.h"
#include "BrainModelSurfaceDistortion.h"
#include "FileUtilities.h"
#include "MathUtilities.h"
#include "StatisticDataGroup.h"
#include "SurfaceShapeFile.h"
#include "TopologyFile.h"
#include "TopologyHelper.h"
#include "vtkTriangle.h"

/**
 * Constructor
 */
BrainModelSurfaceDistortion::BrainModelSurfaceDistortion(BrainSet* brainSetIn,
                                                BrainModelSurface* surfaceIn,
                                                BrainModelSurface* referenceSurfaceIn,
                                                TopologyFile* topologyFileIn,
                                                SurfaceShapeFile* surfaceShapeFileIn,
                                                const int arealDistortionColumnIn,
                                                const int linearDistortionColumnIn,
                                                const QString& arealDistortionNameIn,
                                                const QString& linearDistortionNameIn)
   : BrainModelAlgorithm(brainSetIn)
{
   surface              = surfaceIn;
   referenceSurface     = referenceSurfaceIn;
   topologyFile         = topologyFileIn;
   surfaceShapeFile     = surfaceShapeFileIn;
   arealDistortionColumn = arealDistortionColumnIn;
   linearDistortionColumn = linearDistortionColumnIn;
   arealDistortionName  = arealDistortionNameIn;
   linearDistortionName = linearDistortionNameIn;
}
                        
/**
 * Destructor
 */
BrainModelSurfaceDistortion::~BrainModelSurfaceDistortion()
{
}

/**
 * add/update the distortions in the surface shape file
 */
void 
BrainModelSurfaceDistortion::execute() throw (BrainModelAlgorithmException)
{
   //
   // Coordinate files of both surfaces
   //
   const CoordinateFile* referenceCoords = referenceSurface->getCoordinateFile();
   const CoordinateFile* coords          = surface->getCoordinateFile();
   
   //
   // Get number of nodes
   //
   const int numNodes = referenceCoords->getNumberOfCoordinates();
   
   //
   // Use topology helper to find tiles for each node
   //
   const TopologyHelper th(topologyFile, false, true, false);
   
   //
   // Create comment info about surface used in distortion
   //
   QString commentInfo("Reference Surface: ");
   commentInfo.append(FileUtilities::basename(referenceSurface->getFileName()));
   commentInfo.append("\nSurface:");
   commentInfo.append(FileUtilities::basename(surface->getFileName()));
   
   //
   // Create surface shape file columns if needed
   //
   if (arealDistortionColumn == DISTORTION_COLUMN_CREATE_NEW) {
      if (surfaceShapeFile->getNumberOfColumns() == 0) {
         surfaceShapeFile->setNumberOfNodesAndColumns(surface->getNumberOfNodes(), 1);
      }
      else {
         surfaceShapeFile->addColumns(1);
      }
      arealDistortionColumn = surfaceShapeFile->getNumberOfColumns() - 1;
   }
   if (linearDistortionColumn == DISTORTION_COLUMN_CREATE_NEW) {
      if (surfaceShapeFile->getNumberOfColumns() == 0) {
         surfaceShapeFile->setNumberOfNodesAndColumns(surface->getNumberOfNodes(), 1);
      }
      else {
         surfaceShapeFile->addColumns(1);
      }
      linearDistortionColumn = surfaceShapeFile->getNumberOfColumns() - 1;
   }
   
   //
   // Set column names
   //
   if (arealDistortionColumn >= 0) {
      surfaceShapeFile->setColumnName(arealDistortionColumn, arealDistortionName);
      surfaceShapeFile->setColumnColorMappingMinMax(arealDistortionColumn, -1.0, 1.0);
      surfaceShapeFile->setColumnComment(arealDistortionColumn, commentInfo);
   }
   if (linearDistortionColumn >= 0) {
      surfaceShapeFile->setColumnName(linearDistortionColumn, linearDistortionName);
      surfaceShapeFile->setColumnColorMappingMinMax(linearDistortionColumn, 0.0, 2.0);
      surfaceShapeFile->setColumnComment(linearDistortionColumn, commentInfo);
   }
   
   //
   // If areal distortion should be created
   //
   if (arealDistortionColumn >= 0) {
      //
      // Allocate areal distortion for statistics
      //
      arealDistortionForStatistics.resize(numNodes, 0);
      
      //
      // log(2.0) to avoid recomputation each iteration
      //
      const double log2 = log(2.0);
      
      //
      // Compute distortion for each tile
      //
      const int numTiles = topologyFile->getNumberOfTiles();
      std::vector<float> tileDistortion;
      for (int i = 0; i < numTiles; i++) {
         const int* v = topologyFile->getTile(i);
         
         const float tileAreaReference = MathUtilities::triangleArea(
            (float*)referenceCoords->getCoordinate(v[0]),
            (float*)referenceCoords->getCoordinate(v[1]),
            (float*)referenceCoords->getCoordinate(v[2]));
            
         const float tileArea = MathUtilities::triangleArea(
            (float*)coords->getCoordinate(v[0]),
            (float*)coords->getCoordinate(v[1]),
            (float*)coords->getCoordinate(v[2]));
            
         double distortion = 0.0;
         if (tileAreaReference != 0.0) {
            distortion = tileArea / tileAreaReference;
         }
         else {
            if (tileArea != 0.0) {
               distortion = 10000.0;  // big distortion since denominator is zero
            }
            else {
               distortion = 1.0;   // if both zero then same area
            }
         }
         
         //
         // zero will cause -inf
         //
         if (distortion < 0.00000001) {
            distortion = 0.00000001;
         }
         
         tileDistortion.push_back(log(distortion) / log2);
      }
   
      //
      // Compute areal distortion for all nodes
      //
      for (int i = 0; i < numNodes; i++) {
         float distortion = 1.0;
         const float numberOfNeighbors = th.getNodeNumberOfNeighbors(i);
         
         if (numberOfNeighbors >= 1.0) {
            distortion = 0.0;
            std::vector<int> tiles;
            th.getNodeTiles(i, tiles);
            
            for (int j = 0; j < static_cast<int>(tiles.size()); j++) {
               distortion += tileDistortion[tiles[j]];
            }
            distortion /= numberOfNeighbors;
         }
         surfaceShapeFile->setValue(i, arealDistortionColumn, distortion);
         arealDistortionForStatistics[i] = distortion;
      }
   }
   
   //
   // If linear distortion should be created
   //
   if (linearDistortionColumn >= 0) {
      //
      // Allocate linear distortion for statistics
      //
      linearDistortionForStatistics.resize(numNodes, 0);
      
      //
      // Compute linear distortion for all nodes
      //
      for (int i = 0; i < numNodes; i++) {
         float distortion = 0.0;
         std::vector<int> neighbors;
         th.getNodeNeighbors(i, neighbors);
         const int numberOfNeighbors = neighbors.size();
         
         if (numberOfNeighbors >= 1) {
            const float* me = coords->getCoordinate(i); 
            const float* ref = referenceCoords->getCoordinate(i); 
            
            float distort = 0.0;
            for (int j = 0; j < numberOfNeighbors; j++) {
               const float dist = MathUtilities::distance3D(me, 
                                                  coords->getCoordinate(neighbors[j]));
               const float distRef = MathUtilities::distance3D(ref, 
                                                referenceCoords->getCoordinate(neighbors[j]));
               if (distRef != 0.0) {
                  distort += (dist / distRef);
               }
               else {
                  if (dist != 0.0) {
                     distort += 10000.0;   // big number since denominator is zero
                  }
                  else {
                     distort += 1.0;   // both are zero
                  }
               }
            }
            distortion = distort / static_cast<float>(numberOfNeighbors);
         }
         surfaceShapeFile->setValue(i, linearDistortionColumn, distortion);
         linearDistortionForStatistics[i] = distortion;
      }
   }
}

/**
 * Get statistical measurements for areal distortion
 */
void
BrainModelSurfaceDistortion::getArealDistortionStatistics(StatisticsUtilities::DescriptiveStatistics& stats) const
{
   stats.reset();
   StatisticsUtilities::computeStatistics(arealDistortionForStatistics,
                                          true,
                                          stats);
/*
   StatisticDescriptiveStatistics sds(StatisticDescriptiveStatistics::DATA_TYPE_SAMPLE);
   StatisticDataGroup sdg(&arealDistortionForStatistics);
   sds.addDataGroup(&sdg);
   try {
      sds.execute();
   }
   catch (StatisticException&) {
   }
   stats = sds.getDescriptiveStatistics();
*/
}

/**
 * Get statistical measurements for linear distortion
 */
void
BrainModelSurfaceDistortion::getLinearDistortionStatistics(StatisticsUtilities::DescriptiveStatistics& stats) const
{
   stats.reset();
   StatisticsUtilities::computeStatistics(linearDistortionForStatistics,
                                          true,
                                          stats);
/*
   StatisticDescriptiveStatistics sds(StatisticDescriptiveStatistics::DATA_TYPE_SAMPLE);
   StatisticDataGroup sdg(&linearDistortionForStatistics);
   sds.addDataGroup(&sdg);
   try {
      sds.execute();
   }
   catch (StatisticException&) {
   }
   stats = sds.getDescriptiveStatistics();
*/
}