File: BrainModelSurfaceCurvature.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 (337 lines) | stat: -rw-r--r-- 10,776 bytes parent folder | download
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
/*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 <cmath>

#include "BrainModelSurface.h"
#include "BrainModelSurfaceCurvature.h"
#include "BrainSet.h"
#include "FileUtilities.h"
#include "MathUtilities.h"
#include "SurfaceShapeFile.h"
#include "TopologyHelper.h"
#include "vtkMath.h"

/**
 * Constructor.
 */
BrainModelSurfaceCurvature::BrainModelSurfaceCurvature(
                                          BrainSet* bs,
                                          const BrainModelSurface* surfaceIn,
                                          SurfaceShapeFile* shapeFileIn,
                                          const int meanCurvatureColumnIn,
                                          const int gaussianCurvatureColumnIn,
                                          const QString& meanCurvatureNameIn,
                                          const QString& gaussianCurvatureNameIn,
                                          const bool computePrincipalCurvaturesIn)
   : BrainModelAlgorithm(bs)
{
   surface = NULL;
   if (surfaceIn != NULL) {
      surface = new BrainModelSurface(*surfaceIn);
   }
   shapeFile               = shapeFileIn;
   meanCurvatureColumn     = meanCurvatureColumnIn;
   gaussianCurvatureColumn = gaussianCurvatureColumnIn;
   meanCurvatureName = meanCurvatureNameIn;
   gaussianCurvatureName = gaussianCurvatureNameIn;
   computePrincipalCurvatures = computePrincipalCurvaturesIn;
   
   kMinColumn = -1;
   kMaxColumn = -1;
}

/**
 * Destructor.
 */
BrainModelSurfaceCurvature::~BrainModelSurfaceCurvature()
{
   if (surface != NULL) {
      delete surface;
      surface = NULL;
   }
}

/**
 * Execute the algorithm.
 */
void 
BrainModelSurfaceCurvature::execute() throw (BrainModelAlgorithmException)
{
   //
   // Update the surface's normals
   //
   surface->computeNormals();
   
   //
   // Get the topology helper for the surface
   //
   const TopologyFile* tf = surface->getTopologyFile();
   const TopologyHelper* th = tf->getTopologyHelper(false, true, true);
   
   const CoordinateFile* cf = surface->getCoordinateFile();

   //
   // Create surface shape file columns if needed
   //
   if (meanCurvatureColumn == CURVATURE_COLUMN_CREATE_NEW) {
      if (shapeFile->getNumberOfColumns() == 0) {
         shapeFile->setNumberOfNodesAndColumns(surface->getNumberOfNodes(), 1);
      }
      else {
         shapeFile->addColumns(1);
      }
      meanCurvatureColumn = shapeFile->getNumberOfColumns() - 1;
   }
   if (gaussianCurvatureColumn == CURVATURE_COLUMN_CREATE_NEW) {
      if (shapeFile->getNumberOfColumns() == 0) {
         shapeFile->setNumberOfNodesAndColumns(surface->getNumberOfNodes(), 1);
      }
      else {
         shapeFile->addColumns(1);
      }
      gaussianCurvatureColumn = shapeFile->getNumberOfColumns() - 1;
   }
   
   //
   // Set column names
   //
   if (meanCurvatureColumn >= 0) {
      shapeFile->setColumnName(meanCurvatureColumn, meanCurvatureName);
      shapeFile->setColumnColorMappingMinMax(meanCurvatureColumn, -1.5, 1.5);
   }
   if (gaussianCurvatureColumn >= 0) {
      shapeFile->setColumnName(gaussianCurvatureColumn, gaussianCurvatureName);
      shapeFile->setColumnColorMappingMinMax(gaussianCurvatureColumn, -1.5, 1.5);
   }
   
   //
   // Do principle curvatures?
   //
   if (computePrincipalCurvatures) {
      if (shapeFile->getNumberOfColumns() == 0) {
         shapeFile->setNumberOfNodesAndColumns(surface->getNumberOfNodes(), 2);
      }
      else {
         shapeFile->addColumns(2);
      }
      kMaxColumn = shapeFile->getNumberOfColumns() - 2;
      kMinColumn = shapeFile->getNumberOfColumns() - 1;
      
      shapeFile->setColumnName(kMaxColumn, "k1 (kmajor, first principal curvature)");
      shapeFile->setColumnColorMappingMinMax(kMaxColumn, -1.5, 1.5);
      shapeFile->setColumnName(kMinColumn, "k2 (kminor, second principal curvature)");
      shapeFile->setColumnColorMappingMinMax(kMinColumn, -1.5, 1.5);
   }
   
   //
   // Compute curvature for each node
   //
   const int numNodes = surface->getNumberOfNodes();
   for (int i = 0; i < numNodes; i++) {         
      float gauss = 0.0;
      float mean  = 0.0;
      float kmax = 0.0;
      float kmin = 0.0;

      std::vector<int> neighbors;
      th->getNodeNeighbors(i, neighbors);
      const int numNeighbors = static_cast<int>(neighbors.size());
      if (numNeighbors > 0) {
         //
         // Position and normal for node
         //
         const float* nodeCoord  = cf->getCoordinate(i);
         const float* nodeNormal = surface->getNormal(i);
         
         //
         // Find differences between node's and its neighbors' coordinates and normals
         //
         std::vector<CurvePoint3D> deltaNormal(numNeighbors);
         std::vector<CurvePoint3D> deltaCoord(numNeighbors);            
         for (int j = 0; j < numNeighbors; j++) {
            const int neighbor = neighbors[j];
            const float* neighCoord  = cf->getCoordinate(neighbor);
            const float* neighNormal = surface->getNormal(neighbor);
            for (int k = 0; k < 3; k++) {
               deltaNormal[j].xyz[k] = neighNormal[k] - nodeNormal[k];
               deltaCoord[j].xyz[k]  = neighCoord[k] - nodeCoord[k];
            }
         }
         
         //
         // Compute basis functions
         //
         float basis[2][3];
         float t1[3];
         projection(deltaCoord[0].xyz, nodeNormal, basis[0]);
         MathUtilities::normalize(basis[0]);
         for (int k = 0; k < 3; k++) {
            t1[k] = -basis[0][k];
         }
         MathUtilities::normalizedCrossProduct(t1, nodeNormal, basis[1]);
         MathUtilities::normalize(basis[1]);
         
         std::vector<CurvePoint3D> dn(numNeighbors);
         std::vector<CurvePoint3D> dc(numNeighbors);
         for (int j = 0; j < numNeighbors; j++) {
            float projected[3];
            projection(deltaNormal[j].xyz, nodeNormal, projected);
            projectToPlane(projected, basis, dn[j].xyz);
            
            projection(deltaCoord[j].xyz, nodeNormal, projected);
            projectToPlane(projected, basis, dc[j].xyz);
         }
         
         determineCurvature(numNeighbors, dc, dn, gauss, mean, kmax, kmin);
      }
      
      if (meanCurvatureColumn != CURVATURE_COLUMN_DO_NOT_GENERATE) {
         shapeFile->setValue(i, meanCurvatureColumn, mean);
      }
      if (gaussianCurvatureColumn != CURVATURE_COLUMN_DO_NOT_GENERATE) {
         shapeFile->setValue(i, gaussianCurvatureColumn, gauss);
      }
      
      if (kMaxColumn >= 0) {
         shapeFile->setValue(i, kMaxColumn, kmax);
      }
      if (kMinColumn >= 0) {
         shapeFile->setValue(i, kMinColumn, kmin);
      }
   }
   
   //
   // Create comment info about surface used in distortion
   //
   QString commentInfo("Surface: ");
   commentInfo.append(FileUtilities::basename(surface->getFileName()));
   if (meanCurvatureColumn != CURVATURE_COLUMN_DO_NOT_GENERATE) {
      shapeFile->setColumnComment(meanCurvatureColumn, commentInfo);
   }
   if (gaussianCurvatureColumn != CURVATURE_COLUMN_DO_NOT_GENERATE) {
      shapeFile->setColumnComment(gaussianCurvatureColumn, commentInfo);
   }
}

/**
 *
 */
void
BrainModelSurfaceCurvature::projectToPlane(const float projected[3],
                                           const float basis[2][3],
                                           float xyz[3])
{
   xyz[0] = MathUtilities::dotProduct(projected, basis[0]);
   xyz[1] = MathUtilities::dotProduct(projected, basis[1]);
   xyz[2] = 0.0;
}

/**
 *
 */
void
BrainModelSurfaceCurvature::projection(const float vector[3],
                                       const float normal[3],
                                       float xyz[3])
{
   const float t2 = MathUtilities::dotProduct(vector, normal);
   for (int i = 0; i < 3; i++) {
      xyz[i] = vector[i] - (t2 * normal[i]);
   }
}

/**
 * Determine the curvatures
 */
void
BrainModelSurfaceCurvature::determineCurvature(const int num,
                                         const std::vector<CurvePoint3D>& dc,
                                         const std::vector<CurvePoint3D>& dn,
                                         float& gauss,
                                         float& mean,
                                         float& kmax,
                                         float& kmin)
{
   float sum1 = 0.0;
   float sum2 = 0.0;
   float sum3 = 0.0;
   float wx   = 0.0;
   float wy   = 0.0;
   float wxy  = 0.0;
   
   for (int i = 0; i < num; i++) {
      sum1 += (dc[i].xyz[0] * dn[i].xyz[0]);
      sum2 += ((dc[i].xyz[0] * dn[i].xyz[1]) + (dc[i].xyz[1] * dn[i].xyz[0]));
      sum3 += (dc[i].xyz[1] * dn[i].xyz[1]);
      wx   += (dc[i].xyz[0] * dc[i].xyz[0]);
      wy   += (dc[i].xyz[1] * dc[i].xyz[1]);
      wxy  += (dc[i].xyz[0] * dc[i].xyz[1]);
   }
   
   const float wx2  = wx  * wx;
   const float wy2  = wy  * wy;
   const float wxy2 = wxy * wxy;
   
   float a = 0.0;
   float b = 0.0;
   float c = 0.0;
   const float t1 = (wx + wy) * (-wxy2 + wx * wy);
   if (t1 > 0.0) {
      a = (sum3 * wxy2 - sum2 * wxy * wy +
           sum1 * (-wxy2 + wx * wy + wy2)) / t1;
      b = (-(sum3 * wx * wxy) + sum2 * wx * wy - sum1 * wxy * wy) / t1;
      c = (-(sum2 * wx * wxy) + sum1 * wxy2 +
           sum3 * (wx2 - wxy2 + wx * wy)) / t1;
   }
   
   const float trC = a + c;
   const float detC = a * c - b * b;
   const float temp = trC * trC - 4 * detC;
   float k1 = 0.0;
   float k2 = 0.0;
   if (temp > 0.0) {
      const float deltaPlus = std::sqrt(temp);
      const float deltaMinus = -deltaPlus;
      k1 = (trC + deltaPlus)  / 2.0;
      k2 = (trC + deltaMinus) / 2.0;
   }
   
   gauss = k1 * k2;
   mean  = (k1 + k2) / 2.0;
   
   //
   // For KMAX and KMIN, is the largest of the two absolute values
   //
   if (std::abs(k1) > std::abs(k2)) {
      kmax = k1;
      kmin = k2;
   }
   else {
      kmax = k2;
      kmin = k1;
   }
}