File: CommandVolumeMapToSurface.cxx

package info (click to toggle)
caret 5.6.1.3~dfsg.1-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 23,352 kB
  • ctags: 27,394
  • sloc: cpp: 363,314; python: 6,718; ansic: 5,507; makefile: 227; sh: 9
file content (359 lines) | stat: -rw-r--r-- 13,822 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
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
/*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 <QFile>

#include "BrainSet.h"
#include "BrainModelVolumeToSurfaceMapperAlgorithmParameters.h"
#include "BrainModelVolumeToSurfaceMapper.h"
#include "CommandVolumeMapToSurface.h"
#include "FileFilters.h"
#include "FileUtilities.h"
#include "MetricFile.h"
#include "PaintFile.h"
#include "ProgramParameters.h"
#include "ScriptBuilderParameters.h"
#include "SpecFile.h"

/**
 * constructor.
 */
CommandVolumeMapToSurface::CommandVolumeMapToSurface()
   : CommandBase("-volume-map-to-surface",
                 "VOLUME MAP TO SURFACE")
{
   mappingDataFile = NULL;
}

/**
 * destructor.
 */
CommandVolumeMapToSurface::~CommandVolumeMapToSurface()
{
   if (mappingDataFile != NULL) {
      delete mappingDataFile;
      mappingDataFile = NULL;
   }
}

/**
 * get the script builder parameters.
 */
void 
CommandVolumeMapToSurface::getScriptBuilderParameters(ScriptBuilderParameters& paramsOut) const
{
   std::vector<QString> algNames;
   std::vector<BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM> algValues;   
   BrainModelVolumeToSurfaceMapperAlgorithmParameters::getAlgorithmNamesAndValues(
                                                                        algNames,
                                                                        algValues);
   QStringList metricPaintFileFilters;
   metricPaintFileFilters << FileFilters::getMetricFileFilter();
   metricPaintFileFilters << FileFilters::getPaintFileFilter();
   metricPaintFileFilters << FileFilters::getSurfaceShapeFileFilter();

   paramsOut.clear();
   paramsOut.addFile("Coordinate File Name", FileFilters::getCoordinateGenericFileFilter());
   paramsOut.addFile("Topology File Name", FileFilters::getTopologyGenericFileFilter());
   paramsOut.addFile("Input Metric File Name", metricPaintFileFilters);
   paramsOut.addFile("Output Metric File Name", metricPaintFileFilters);
   paramsOut.addFile("Output Spec File Name", FileFilters::getSpecFileFilter());
   paramsOut.addListOfItems("Algorithm", algNames, algNames); // yes use names for both
   paramsOut.addMultipleFiles("Input Volume File Names", FileFilters::getVolumeGenericFileFilter());
   paramsOut.addVariableListOfParameters("Mapping Options");
}

/**
 * get full help information.
 */
QString 
CommandVolumeMapToSurface::getHelpInformation() const
{
   std::vector<QString> algNames;
   std::vector<BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM> algValues;   
   BrainModelVolumeToSurfaceMapperAlgorithmParameters::getAlgorithmNamesAndValues(
                                                                        algNames,
                                                                        algValues);

   QString helpInfo =
      (indent3 + getShortDescription() + "\n"
       + indent6 + parameters->getProgramNameWithoutPath() + " " + getOperationSwitch() + "  \n"
       + indent9 + "<coordinate-file-name>\n"
       + indent9 + "<topology-file-name>\n"
       + indent9 + "<input-metric-or-paint-file-name>\n"
       + indent9 + "<output-metric-or-paint-file-name>\n"
       + indent9 + "<algorithm>\n"
       + indent9 + "<input-volume-file-names>\n"
       + indent9 + "[-av  average-voxel-neighbor-cube-size (mm)]\n"
       + indent9 + "[-bf  brain-fish-max-distance\n"
       + indent9 + "      brain-fish-splat factor]\n"
       + indent9 + "[-g   gaussian-neighbor-cube-size (mm)\n"
       + indent9 + "      sigma-norm\n"
       + indent9 + "      sigma-tang\n"
       + indent9 + "      norm below cutoff (mm)\n"
       + indent9 + "      norm above cutoff (mm)\n"
       + indent9 + "      tang-cutoff (mm)]\n"
       + indent9 + "[-mv  maximum-voxel-neighbor-cube-size (mm)]\n"
       + indent9 + "[-sv  strongest-voxel-neighbor-cube-size (mm)]\n"
       + indent9 + "\n"
       + indent9 + "Map volume(s) to a surface metric or paint file.\n"
       + indent9 + "\n"
       + indent9 + "For successful mapping, both the surface and the volume\n"
       + indent9 + "must be in the same stereotaxic space.\n"
       + indent9 + "\n"
       + indent9 + "\"algorithm\" is one of:\n");
   for (unsigned int i = 0; i < algNames.size(); i++) {
      helpInfo += (indent9 + "   " + algNames[i] + "\n");
   }
   helpInfo += ("" 
       + indent9 + "\n"
       + indent9 + "If the input metric or paint file name is not an empty string\n"
       + indent9 + " (\"\"), the newly create metric or paint columns will be \n"
       + indent9 + "appended to the file and then written with the output file \n"
       + indent9 + "name.\n"
       + indent9 + "\n");
      
   return helpInfo;
}

/**
 * execute the command.
 */
void 
CommandVolumeMapToSurface::executeCommand() throw (BrainModelAlgorithmException,
                                     CommandException,
                                     FileException,
                                     ProgramParametersException,
                                     StatisticException)
{
   std::vector<QString> algNames;
   std::vector<BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM> algValues;   
   BrainModelVolumeToSurfaceMapperAlgorithmParameters::getAlgorithmNamesAndValues(
                                                                        algNames,
                                                                        algValues);
   BrainModelVolumeToSurfaceMapperAlgorithmParameters mappingParameters;

   const QString coordinateFileName =
      parameters->getNextParameterAsString("Coordinate File Name");
   const QString topologyFileName =
      parameters->getNextParameterAsString("Topology File Name");
   const QString inputDataFileName =
      parameters->getNextParameterAsString("Input Metric or Paint File Name");
   const QString outputDataFileName =
      parameters->getNextParameterAsString("Output Metric or PaintFile Name");
   const QString algorithmName =
      parameters->getNextParameterAsString("Algorithm Name");
      
   //
   // Verify algorithm name
   //
   bool algNameValid = false;
   for (unsigned int i = 0; i < algNames.size(); i++) {
      if (algorithmName == algNames[i]) {
         mappingParameters.setAlgorithm(algValues[i]);
         algNameValid = true;
         break;
      }
   }
   if (algNameValid == false) {
      throw CommandException("Invalid algorithm name \""
                             + algorithmName
                             + "\".");
   }
      
   std::vector<QString> inputVolumeFileNames;
   bool readingVolumeFileNamesFlag = true;
   while (parameters->getParametersAvailable()) {
      const QString paramValue = parameters->getNextParameterAsString("Map Volume Parameter");
      if (paramValue.isEmpty() == false) {
         if (paramValue[0] == '-') {
            readingVolumeFileNamesFlag = false;
            if (paramValue == "-av") {
               mappingParameters.setAlgorithmMetricAverageVoxelParameters(
                  parameters->getNextParameterAsFloat("Average Voxel Neighbors (mm)"));
            }
            else if (paramValue == "-bf") {
               const float md = parameters->getNextParameterAsFloat("Brain Fish Max Distance");
               const int sf = parameters->getNextParameterAsInt("Brain Fish Splat Factor");
               mappingParameters.setAlgorithmMetricMcwBrainFishParameters(md, sf);
            }
            else if (paramValue == "-g") {
               const float gn = parameters->getNextParameterAsFloat("Gaussian Neighbor Cube Size (mm)");
               const float sn = parameters->getNextParameterAsFloat("Gaussian Sigma Norm");
               const float st = parameters->getNextParameterAsFloat("Gaussian Sigma Tangent");
               const float nb = parameters->getNextParameterAsFloat("Gaussina Norm Below Cutoff");
               const float na = parameters->getNextParameterAsFloat("Gaussian Norm Above Cutoff");
               const float tc = parameters->getNextParameterAsFloat("Gaussian Tangent Cutoff");
               mappingParameters.setAlgorithmMetricGaussianParameters(gn, sn, st, nb, na, tc);
            }
            else if (paramValue == "-mv") {
               mappingParameters.setAlgorithmMetricMaximumVoxelParameters(
                  parameters->getNextParameterAsFloat("Maximum Voxel Neighbors (mm)"));
            }
            else if (paramValue == "-sv") {
               mappingParameters.setAlgorithmMetricStrongestVoxelParameters(
                  parameters->getNextParameterAsFloat("Strongest Voxel Neighbors (mm)"));
            }
            else {
               throw CommandException("Unrecognized option: " + paramValue);
            }
         }
         else if (readingVolumeFileNamesFlag) {
            inputVolumeFileNames.push_back(paramValue);
         }
         else {
            throw CommandException("Unrecognized parameter: " + paramValue);
         }
      }
   }

   const int numberOfVolumeFiles = static_cast<int>(inputVolumeFileNames.size());
   if (numberOfVolumeFiles <= 0) {
      throw CommandException("No volume file names were provided for mapping.");
   }
   
   enum MODE {
      MODE_NONE,
      MODE_METRIC,
      MODE_PAINT,
   };
   MODE mode = MODE_NONE;
   switch (mappingParameters.getAlgorithm()) {
      case BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM_METRIC_AVERAGE_NODES:
      case BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM_METRIC_AVERAGE_VOXEL:
      case BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM_METRIC_ENCLOSING_VOXEL:
      case BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM_METRIC_GAUSSIAN:
      case BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM_METRIC_INTERPOLATED_VOXEL:
      case BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM_METRIC_MAXIMUM_VOXEL:
      case BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM_METRIC_MCW_BRAINFISH:
      case BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM_METRIC_STRONGEST_VOXEL:
         mode = MODE_METRIC;
         break;
      case BrainModelVolumeToSurfaceMapperAlgorithmParameters::ALGORITHM_PAINT_ENCLOSING_VOXEL:
         mode = MODE_PAINT;
         break;
   }
   if (mode == MODE_NONE) {
      throw CommandException("Unable determine type of output file from algorithm name.");
   }
   
   //
   // Create a brain set
   //
   BrainSet brainSet(topologyFileName,
                     coordinateFileName,
                     "",
                     true);
   BrainModelSurface* surface = brainSet.getBrainModelSurface(0);
   if (surface == NULL) {
      throw CommandException("unable to find surface.");
   }
   const TopologyFile* tf = surface->getTopologyFile();
   if (tf == NULL) {
      throw CommandException("unable to find topology.");
   }
   const int numNodes = surface->getNumberOfNodes();
   if (numNodes == 0) {
      throw CommandException("surface contains no nodes.");
   }
   
   //
   // Metric file for mapping
   //
   switch (mode) {
      case MODE_NONE:
         break;
      case MODE_METRIC:
         mappingDataFile = new MetricFile;
         break;
      case MODE_PAINT:
         mappingDataFile = new PaintFile;
         break;
   }
   
   //
   // Read input metric file if it exists
   //
   if (inputDataFileName.isEmpty() == false) {
      if (QFile::exists(inputDataFileName)) {
         mappingDataFile->readFile(inputDataFileName);
      }
   }
   
   //
   // Map all of the volume files
   //
   for (int j = 0; j < numberOfVolumeFiles; j++) {
      //
      // Read the volume file - all of its subvolumes
      //
      std::vector<VolumeFile*> volumes;
      VolumeFile::readFile(inputVolumeFileNames[j],
                           VolumeFile::VOLUME_READ_SELECTION_ALL,
                           volumes,
                           false);
      for (unsigned int i = 0; i < volumes.size(); i++) {
         VolumeFile* vf = volumes[i];
         
         //
         // Name for output column
         //
         QString columnName(FileUtilities::basename(inputVolumeFileNames[j]));
         if (volumes.size() > 0) {
            columnName += ("["
                           + QString::number(i + 1)
                           + "]");
         }
         //
         // Create the mapper
         //
         BrainModelVolumeToSurfaceMapper mapper(&brainSet,
                                                surface,
                                                vf,
                                                mappingDataFile,
                                                mappingParameters,
                                                -1,
                                                columnName);
               
         //
         // Run the mapper
         //
         mapper.execute();
         
         //
         // Delete the volume
         //
         delete vf;
      }
   }
   
   //
   // Save the metric file
   //
   mappingDataFile->writeFile(outputDataFileName);
}