File: CommandPaintAssignNodesRelativeToLine.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 (343 lines) | stat: -rw-r--r-- 13,338 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
/*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 "BrainModelSurface.h"
#include "BrainModelSurfacePaintAssignRelativeToLine.h"
#include "BrainSet.h"
#include "CommandPaintAssignNodesRelativeToLine.h"
#include "FileFilters.h"
#include "FociProjectionFile.h"
#include "MathUtilities.h"
#include "PaintFile.h"
#include "ProgramParameters.h"
#include "ScriptBuilderParameters.h"

/**
 * constructor.
 */
CommandPaintAssignNodesRelativeToLine::CommandPaintAssignNodesRelativeToLine()
   : CommandBase("-paint-assign-nodes-relative-to-line",
                 "PAINT ASSIGN NODES RELATIVE TO LINE")
{
}

/**
 * destructor.
 */
CommandPaintAssignNodesRelativeToLine::~CommandPaintAssignNodesRelativeToLine()
{
}

/**
 * get the script builder parameters.
 */
void 
CommandPaintAssignNodesRelativeToLine::getScriptBuilderParameters(ScriptBuilderParameters& paramsOut) const
{
   paramsOut.clear();
   paramsOut.addFile("Coordinate File Name", FileFilters::getCoordinateGenericFileFilter());
   paramsOut.addFile("Topology File Name", FileFilters::getTopologyGenericFileFilter());
   paramsOut.addFile("Foci Projection File Name", FileFilters::getFociProjectionFileFilter());
   paramsOut.addString("Name of Focus at Line Start");
   paramsOut.addString("Name of Focus at Line End");
   paramsOut.addFile("Input Paint File Name", FileFilters::getPaintFileFilter());
   paramsOut.addFile("Output Paint File Name", FileFilters::getPaintFileFilter());
   paramsOut.addString("Paint File Column");
   paramsOut.addString("Region Paint Name");
   paramsOut.addString("New Paint Name");
   paramsOut.addFloat("Minimum Distance From Line");
   paramsOut.addFloat("Maximum Distance From Line");
   paramsOut.addVariableListOfParameters("Line Info");
}

      
/**
 * get full help information.
 */
QString 
CommandPaintAssignNodesRelativeToLine::getHelpInformation() const
{
   QString helpInfo =
      (indent3 + getShortDescription() + "\n"
       + indent6 + parameters->getProgramNameWithoutPath() + " " + getOperationSwitch() + "  \n"
       + indent9 + "<coordinate-file-name>\n"
       + indent9 + "<topology-file-name>\n"
       + indent9 + "<foci-projection-file-name>\n"
       + indent9 + "<name-of-focus-at-line-start>\n"
       + indent9 + "<name-of-focus-at-line-end>\n"
       + indent9 + "<input-paint-file-name>\n"
       + indent9 + "<output-paint-file-name>\n"
       + indent9 + "<paint-file-column>\n"
       + indent9 + "<region-paint-name> \n"
       + indent9 + "<new-paint-name>\n"
       + indent9 + "<minimum-distance-from-line> \n"
       + indent9 + "<maximum-distance-from-line> \n"
       + indent9 + "[-limit-to-nodes-with-current-paint-name  name]\n"
       + indent9 + "[-node-x-min  value]\n"
       + indent9 + "[-node-x-max  value]\n"
       + indent9 + "[-node-y-min  value]\n"
       + indent9 + "[-node-y-max  value]\n"
       + indent9 + "[-node-z-min  value]\n"
       + indent9 + "[-node-z-max  value\n"
       + indent9 + "\n"
       + indent9 + "Create an infinite line that goes through the two specified\n"
       + indent9 + "foci.  Assign the new paint name to nodes that are within\n"
       + indent9 + "the minimum and maximum distances on the right side of the \n"
       + indent9 + "line.  Options limit the nodes assigned to those with an   \n"
       + indent9 + "existing paint name and those nodes whose coordinates are \n"
       + indent9 + "within geometric specified limits.  The initial nodes are\n"
       + indent9 + "those adjacent to \"region-paint-name\" and this regions\n"
       + indent9 + "is incrementally expanded until there no additional nodes\n"
       + indent9 + "have their paint set.\n"
       + indent9 + "\n"
       + indent9 + "\"paint-file-column\" is either the number of the column, \n"
       + indent9 + "which starts at one, or the name of the column.  If a\n"
       + indent9 + "name contains spaces, it must be enclosed in double\n"
       + indent9 + "quotes.  Name has priority over number.\n"
       + indent9 + "\n"
       + indent9 + "The input paint file must exist.  If the input paint column\n"
       + indent9 + "is not found and its name is non-numeric, a new column will \n"
       + indent9 + "be added to the paint file if the paint file contains nodes.\n"
       + indent9 + "\n"
       + indent9 + "Uses the last foci with specified names.\n"
       + indent9 + "\n");
      
   return helpInfo;
}

/**
 * execute the command.
 */
void 
CommandPaintAssignNodesRelativeToLine::executeCommand() throw (BrainModelAlgorithmException,
                                     CommandException,
                                     FileException,
                                     ProgramParametersException,
                                     StatisticException)
{
   //
   // Get the parameters
   //
   const QString coordinateFileName =
      parameters->getNextParameterAsString("Coordinate File Name");
   const QString topologyFileName =
      parameters->getNextParameterAsString("Topology File Name");
   const QString fociProjectionFileName =
      parameters->getNextParameterAsString("Projection File Name");
   const QString focusLineStartName =
      parameters->getNextParameterAsString("Focus Name for Line Start");
   const QString focusLineEndName =
      parameters->getNextParameterAsString("Focus Name for Line End");
   const QString inputPaintFileName =
      parameters->getNextParameterAsString("Input Paint File Name");
   const QString outputPaintFileName =
      parameters->getNextParameterAsString("Output Paint File Name");
   const QString paintFileColumn =
      parameters->getNextParameterAsString("Paint File Column");
   const QString regionPaintName =
      parameters->getNextParameterAsString("Region Paint Name");
   const QString newPaintName =
      parameters->getNextParameterAsString("New Paint Name");
   const float minimumDistanceFromLine =
      parameters->getNextParameterAsFloat("Minimum Distance From Line");
   const float maximumDistanceFromLine =
      parameters->getNextParameterAsFloat("Maximum Distance From Line");
     
   //
   // Optional parameter names
   //
   std::vector<QString> existingPaintNames;
   float xMin = -std::numeric_limits<float>::max();
   float xMax =  std::numeric_limits<float>::max();
   float yMin = -std::numeric_limits<float>::max();
   float yMax =  std::numeric_limits<float>::max();
   float zMin = -std::numeric_limits<float>::max();
   float zMax =  std::numeric_limits<float>::max();

   //
   // Get optional parameters
   //
   while (parameters->getParametersAvailable()) {
      const QString paramName = 
         parameters->getNextParameterAsString("ASSIGN PAINT RELATIVE TO LINE parameter");
      if (paramName == "-limit-to-nodes-with-current-paint-name") {
         existingPaintNames.push_back(
            parameters->getNextParameterAsString("Existing paint name"));
      }
      else if (paramName == "-node-x-min") {
         xMin = parameters->getNextParameterAsFloat("Node X Minimum");
      }
      else if (paramName == "-node-x-max") {
         xMax = parameters->getNextParameterAsFloat("Node X Maximum");
      }
      else if (paramName == "-node-y-min") {
         yMin = parameters->getNextParameterAsFloat("Node Y Minimum");
      }
      else if (paramName == "-node-y-max") {
         yMax = parameters->getNextParameterAsFloat("Node Y Maximum");
      }
      else if (paramName == "-node-z-min") {
         zMin = parameters->getNextParameterAsFloat("Node Z Minimum");
      }
      else if (paramName == "-node-z-max") {
         zMax = parameters->getNextParameterAsFloat("Node Z Maximum");
      }
      else {
         throw CommandException("unexpected "
                                "parameter: "
                                + paramName);
      }
   }

   //
   // Create a brain set
   //
   BrainSet brainSet(topologyFileName,
                     coordinateFileName,
                     "",
                     true);
   BrainModelSurface* surface = brainSet.getBrainModelSurface(0);
   if (surface == NULL) {
      throw CommandException("unable to find surface.");
   }
   const CoordinateFile* cf = surface->getCoordinateFile();
   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.");
   }
   
   //
   // Read the input focus projection file
   //
   FociProjectionFile fociProjectionFile;
   fociProjectionFile.readFile(fociProjectionFileName);
   
   //
   // Get the foci for starting and ending the line
   //
   const CellProjection* focusStart = 
      fociProjectionFile.getLastCellProjectionWithName(focusLineStartName);
   if (focusStart == NULL) {
      throw CommandException("Cannot find focus "
                             "for start of line " + focusLineStartName);
   }
   const CellProjection* focusEnd = 
      fociProjectionFile.getLastCellProjectionWithName(focusLineEndName);
   if (focusEnd == NULL) {
      throw CommandException("Cannot find focus "
                             "for end of line " + focusLineEndName);
   }
   
   //
   // Unproject the Foci
   //
   const bool fiducialSurfaceFlag =
      (surface->getSurfaceType() == BrainModelSurface::SURFACE_TYPE_FIDUCIAL);
   const bool flatSurfaceFlag =
      ((surface->getSurfaceType() == BrainModelSurface::SURFACE_TYPE_FLAT) ||
       (surface->getSurfaceType() == BrainModelSurface::SURFACE_TYPE_FLAT_LOBAR));
   float focusStartXYZ[3];
   focusStart->getProjectedPosition(cf,
                                    tf,
                                    fiducialSurfaceFlag,
                                    flatSurfaceFlag,
                                    false,
                                    focusStartXYZ);
   float focusEndXYZ[3];
   focusEnd->getProjectedPosition(cf,
                                    tf,
                                    fiducialSurfaceFlag,
                                    flatSurfaceFlag,
                                    false,
                                    focusEndXYZ);
   //
   // Open paint file and find column
   //
   PaintFile paintFile;
   paintFile.readFile(inputPaintFileName);
   //
   // Get the column number
   //
   const int paintFileColumnNumber = paintFile.getColumnFromNameOrNumber(paintFileColumn, true);
   const int regionPaintNameIndex = paintFile.getPaintIndexFromName(regionPaintName);
   if (regionPaintNameIndex < 0) {
      throw CommandException("Paint name "
                             + regionPaintName
                             + " not found in paint file.");
   }
   const int newPaintNameIndex = paintFile.addPaintName(newPaintName);
   
   //
   // Find indices of existing paint names
   //
   std::vector<int> existingPaintIndices;
   const int numExistingPaintNames = static_cast<int>(existingPaintNames.size());
   for (int i = 0; i < numExistingPaintNames; i++) {
      const int indx = paintFile.getPaintIndexFromName(existingPaintNames[i]);
      if (indx < 0) {
         throw CommandException("existing paint "
                                "name not found "
                                + existingPaintNames[i]);
      }
      existingPaintIndices.push_back(indx);
   }
 
   //
   // Assign nodes
   //  
   const float nodeExtent[6] = {
      xMin,
      xMax,
      yMin,
      yMax,
      zMin,
      zMax
   };
   BrainModelSurfacePaintAssignRelativeToLine paintAssign(&brainSet,
                                                          surface,
                                                          &paintFile,
                                                          paintFileColumnNumber,
                                                          regionPaintNameIndex,
                                                          newPaintNameIndex,
                                                          focusStartXYZ,
                                                          focusEndXYZ,
                                                          minimumDistanceFromLine,
                                                          maximumDistanceFromLine,
                                                          existingPaintIndices,
                                                          nodeExtent);
  
   paintAssign.execute();

   //
   // Write paint file
   //
   paintFile.writeFile(outputPaintFileName);
}