File: BrainModelSurfaceFlattenPartialHemisphere.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 (304 lines) | stat: -rw-r--r-- 10,768 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

#include <iostream>
#include <sstream>

#include <QGlobalStatic>  // needed on windows for TRUE and FALSE
#include <QMessageBox>

#include "BrainModelSurface.h"
#include "BrainModelSurfaceFlattenPartialHemisphere.h"
#include "BrainSet.h"
#include "DebugControl.h"
#include "FileUtilities.h"
#include "PaintFile.h"

/**
 * Constructor
 */
BrainModelSurfaceFlattenPartialHemisphere::BrainModelSurfaceFlattenPartialHemisphere(
                                          BrainModelSurface* fiducialSurfaceIn,
                                          BrainModelSurface* partialHemisphereSurfaceIn,
                                          const FLATTEN_TYPE flattenSurfaceTypeIn)
   : BrainModelAlgorithm(partialHemisphereSurfaceIn->getBrainSet())
{
   fiducialSurface          = fiducialSurfaceIn;
   partialHemisphereSurface = partialHemisphereSurfaceIn;
   flattenSurfaceType       = flattenSurfaceTypeIn;
}

/**
 * Destructor
 */
BrainModelSurfaceFlattenPartialHemisphere::~BrainModelSurfaceFlattenPartialHemisphere()
{
}

/**
 * Execute the flattening
 */
void 
BrainModelSurfaceFlattenPartialHemisphere::execute() throw (BrainModelAlgorithmException)
{
   const TopologyFile* topologyFile = partialHemisphereSurface->getTopologyFile();
   
   std::vector<int> dummy1, dummy2, dummy3;
   const int numPiecesOfSurface = topologyFile->findIslands(dummy1, dummy2, dummy3);
   if (numPiecesOfSurface > 1) {
      throw BrainModelAlgorithmException(
         "There are multiple pieces of surface.  Use Surface: Topology: Remove Islands\n"
         "to remove them and verify that the surface remains correct.");
   }

   //
   // find the geography paint column
   //
   const PaintFile* pf = brainSet->getPaintFile();
   const int geographyColumn = pf->getGeographyColumnNumber();
   if (geographyColumn < 0) {
      throw BrainModelAlgorithmException(
         "There is no geography paint column which is needed for flattening the\n"
         "partial hemisphere.  The geography paint column contains the \"CUT.FACE\"\n"
         "nodes which are needed to properly orient the partial hemisphere for\n"
         "flattening.");
   }
      
   //
   // original caret coord file and topology file names
   //
   QString originalCoordFileName(partialHemisphereSurface->getFileName());
   if (originalCoordFileName.isEmpty()) {
      originalCoordFileName = "species.case.hem.descrip.num_nodes.coord";
   }
   QString originalTopoFileName(partialHemisphereSurface->getTopologyFile()->getFileName());
   if (originalTopoFileName.isEmpty()) {
      originalTopoFileName = "species.case.hem.descrip.num_nodes.topo";
   }
   
   //
   // Make a copy of the surface
   // 
   partialHemisphereSurface = new BrainModelSurface(*partialHemisphereSurface);
   brainSet->addBrainModel(partialHemisphereSurface);
      
   if (flattenSurfaceType == FLATTEN_TYPE_FIDUCIAL) {
      //
      // Get the surfaced area of the surface
      //
      const float fiducialSurfaceArea = partialHemisphereSurface->getSurfaceArea();
      
      //
      // Convert the surface into a ellipse
      //
      partialHemisphereSurface->convertToEllipsoid();
      
      //
      // Convert the ellipse into a sphere
      //
      partialHemisphereSurface->convertEllipsoidToSphereWithSurfaceArea(fiducialSurfaceArea);
      
      //
      // update the surface's normals
      //
      partialHemisphereSurface->computeNormals();
      
      //
      // Get the number of crossovers
      //
      int numTileCrossovers, numNodeCrossovers;
      partialHemisphereSurface->crossoverCheck(numTileCrossovers, 
                                               numNodeCrossovers,
                                               BrainModelSurface::SURFACE_TYPE_SPHERICAL);
      
      //
      // Parameters for smoothing the surface
      //
      const int smoothingCycles = 20;
      const int smoothingIterations = 100;
      const int smoothingEdgeIterations = 10;
      const float smoothingStrength = 1.0;
      
      //
      // Smooth, reproject to a sphere, update normals, crossover check, and show in main window
      //
      int numCycles = 0;
      while ((numCycles < smoothingCycles) && (numNodeCrossovers > 2)) {
         partialHemisphereSurface->linearSmoothing(smoothingStrength, smoothingIterations,
                                                   smoothingEdgeIterations);
         partialHemisphereSurface->convertToSphereWithSurfaceArea(fiducialSurfaceArea);
         partialHemisphereSurface->computeNormals();
         partialHemisphereSurface->crossoverCheck(numTileCrossovers, 
                                                  numNodeCrossovers,
                                                  BrainModelSurface::SURFACE_TYPE_SPHERICAL);
         brainSet->drawBrainModel(partialHemisphereSurface);
         if (DebugControl::getDebugOn()) {
            std::cout << "Flatten Part Hem: smoothing cycle " << numCycles
                      << " tile crossovers " << numTileCrossovers
                      << " node crossovers " << numNodeCrossovers << std::endl;
         }
         numCycles++;
      }
       
      //
      // If there are crossovers and running in interactive environment,
      // allow user to decide whether or not to continue.
      //
      if (numNodeCrossovers > 2) {
         QWidget* parent = brainSet->getProgressDialogParent();
         if (parent != NULL) {
            std::ostringstream msg;
            msg << "There are " << numNodeCrossovers << " node crossovers.\n"
                << "Continue flattening ?";
            if (QMessageBox::warning(parent, "Crossovers Warning",
                                     msg.str().c_str(), "Yes", "No") != 0) {
               return;
            }
         }
         else {
            if (DebugControl::getDebugOn()) {
               std::cout << "INFO: partial hemisphere has " << numNodeCrossovers << " node crossovers." 
                         << std::endl;
            }
         }
      }
   }
   else if (flattenSurfaceType == FLATTEN_TYPE_ELLIPSOID) {
      //
      // Translate the surface to its center of mass
      //
      partialHemisphereSurface->translateToCenterOfMass();
      
      //
      // Convert the ellipsoid into a sphere whose surface area is that of the fiducial surface
      //
      const float surfaceArea = fiducialSurface->getSurfaceArea();
      partialHemisphereSurface->convertEllipsoidToSphereWithSurfaceArea(surfaceArea);
   }
      
   //
   // Save the spherical surface
   //
   const QString sphereCoordName = 
      FileUtilities::changeCaretDataFileDescriptionType(originalCoordFileName, "SPHERE");
   try {
      brainSet->writeCoordinateFile(sphereCoordName,
                                    BrainModelSurface::SURFACE_TYPE_SPHERICAL,
                                    partialHemisphereSurface->getCoordinateFile());
   }
   catch (FileException& e) {
      throw BrainModelAlgorithmException(e.whatQString());
   }
                                 
   //
   // Copy the surface
   //
   partialHemisphereSurface = new BrainModelSurface(*partialHemisphereSurface);
   brainSet->addBrainModel(partialHemisphereSurface);
   partialHemisphereSurface->appendToCoordinateFileComment("Flattening partial hemisphere\n");
   
   //
   // Orient the sphere so that "CUT.FACE" nodes are on the negative Z axis
   //
   std::vector<QString> paintNames;
   paintNames.push_back("CUT.FACE");
   QString errorMessage;
   if (partialHemisphereSurface->orientPaintedNodesToNegativeZAxis(brainSet->getPaintFile(),
                                                                   paintNames,
                                                                     geographyColumn,
                                                                     errorMessage)) {
      throw BrainModelAlgorithmException(errorMessage);
   }
   
   //
   // Copy topology file and set its type to OPEN
   //
   const TopologyFile* oldTopology = partialHemisphereSurface->getTopologyFile();
   TopologyFile* topology = new TopologyFile(*oldTopology);
   topology->setTopologyType(TopologyFile::TOPOLOGY_TYPE_OPEN);
   brainSet->addTopologyFile(topology);
   partialHemisphereSurface->setTopologyFile(topology);
   
   //
   // Disconnect any nodes with names CUT.FACE or MEDIAL.WALL
   //
   std::vector<QString> disconnectNames;
   disconnectNames.push_back("CUT.FACE");
   disconnectNames.push_back("MEDIAL.WALL");
   brainSet->disconnectNodes(topology, disconnectNames, geographyColumn);
   
   //
   // Remove any stragglers (tiles that are connected to other tiles only at a single node)
   //
   partialHemisphereSurface->getTopologyFile()->removeCornerTiles(2);
   partialHemisphereSurface->moveDisconnectedNodesToOrigin();

   //
   // Classify the nodes
   //
   brainSet->classifyNodes(topology);
   
   //
   // Save the open topology file
   //
   const QString openTopoName = 
      FileUtilities::changeCaretDataFileDescriptionType(originalTopoFileName, "OPEN");
   try {
      brainSet->writeTopologyFile(openTopoName,
                                 TopologyFile::TOPOLOGY_TYPE_OPEN,
                                 topology);
   }
   catch (FileException& e) {
      throw BrainModelAlgorithmException(e.whatQString());
   }
                                 
   //
   // Project the surface flat
   //
   partialHemisphereSurface->convertSphereToFlat();
   
   //
   // Smooth just a little bit
   //
   partialHemisphereSurface->arealSmoothing(1.0, 5, 0);
   
   //
   // Update normals
   //
   partialHemisphereSurface->computeNormals();
   
   //
   // Scale the surface to 10x the fiducial area
   //
   partialHemisphereSurface->scaleSurfaceToArea(10.0 * fiducialSurface->getSurfaceArea(), false);

   //
   // Translate to center of mass and scale to fit the window
   //
   partialHemisphereSurface->translateToCenterOfMass();
   partialHemisphereSurface->updateForDefaultScaling();
               
   //
   // Determine crossovers
   //
   int numTileCrossovers, numNodeCrossovers;
   partialHemisphereSurface->crossoverCheck(numTileCrossovers, 
                                            numNodeCrossovers,
                                            BrainModelSurface::SURFACE_TYPE_FLAT);
   
   //
   // Save the flat surface
   //
   const QString flatCoordName = 
      FileUtilities::changeCaretDataFileDescriptionType(originalCoordFileName, "InitialFlat");
   try {
      brainSet->writeCoordinateFile(flatCoordName,
                                    BrainModelSurface::SURFACE_TYPE_FLAT,
                                    partialHemisphereSurface->getCoordinateFile());
                                 
   }
   catch (FileException& e) {
      throw BrainModelAlgorithmException(e.whatQString());
   }
}