File: BrainModelSurfaceFlattenFullHemisphere.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 (1045 lines) | stat: -rw-r--r-- 37,173 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
/*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 <algorithm>
#include <iostream>
#include <sstream>

#include "AreaColorFile.h"
#include "BorderColorFile.h"
#include "BorderFile.h"
#include "BorderFileProjector.h"
#include "BorderProjectionFile.h"
#include "BrainModelBorderSet.h"
#include "BrainModelSurfaceNodeColoring.h"
#include "BrainModelSurfacePointLocator.h"
#include "BrainModelSurface.h"
#include "BrainModelSurfaceCutter.h"
#include "BrainModelSurfaceFlattenFullHemisphere.h"
#include "BrainSet.h"
#include "DebugControl.h"
#include "DisplaySettingsBorders.h"
#include "FileUtilities.h"
#include "PaintFile.h"
#include "SpecFile.h"
#include "SurfaceShapeFile.h"
#include "TransformationMatrixFile.h"

//
// File globals
//
static const QString medialWallName("MEDIAL.WALL");
static const QString calcarineCutName("CalcarineCut");
static const QString frontalCutName("FrontalCut");
static const QString medialWallDorsalLandmark("LANDMARK.MedWall.DORSAL");
static const QString medialWallVentralLandmark("LANDMARK.MedWall.VENTRAL");
static const QString calcarineLandmark("LANDMARK.CalcarineSulcus");

/**
 * Constructor.
 */
BrainModelSurfaceFlattenFullHemisphere::BrainModelSurfaceFlattenFullHemisphere(
                                                         BrainModelSurface* fiducialSurfaceIn,
                                                         BrainModelSurface* ellipsoidSurfaceIn,
                                                         BorderFile* borderFileIn,
                                                         const float acPositionIn[3],
                                                         const float acOffsetIn[3],
                                                         const bool smoothFiducialMedialWallFlagIn)
   : BrainModelAlgorithm(ellipsoidSurfaceIn->getBrainSet())
{
   fiducialSurface = fiducialSurfaceIn;
   flattenSurface  = ellipsoidSurfaceIn;
   borderFile      = borderFileIn;
   acPosition[0]   = acPositionIn[0];
   acPosition[1]   = acPositionIn[1];
   acPosition[2]   = acPositionIn[2];
   acOffset[0]     = acOffsetIn[0];
   acOffset[1]     = acOffsetIn[1];
   acOffset[2]     = acOffsetIn[2];
   smoothFiducialMedialWallFlag = smoothFiducialMedialWallFlagIn;
}

/**
 * Destructor.
 */
BrainModelSurfaceFlattenFullHemisphere::~BrainModelSurfaceFlattenFullHemisphere()
{
}

/**
 * Execute the flattening.
 */
void 
BrainModelSurfaceFlattenFullHemisphere::execute() throw (BrainModelAlgorithmException)
{
   DisplaySettingsBorders* dsb = brainSet->getDisplaySettingsBorders();
   dsb->setDisplayBorders(true);
   
   const TopologyFile* topologyFile = flattenSurface->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.");
   }
      
   if (borderFile == NULL) {
      throw BrainModelAlgorithmException("Border File not provided.");
   }
   else if (borderFile->getNumberOfBorders() == 0) {
      throw BrainModelAlgorithmException("Border File contains no borders.");
   }
   
   const int numNodes = flattenSurface->getNumberOfNodes();
   
   //
   // Structure naming
   //
   hemStr = flattenSurface->getStructure().getTypeAsAbbreviatedString();
/*
   switch(flattenSurface->getStructure()) {
      case BrainModelSurface::HEMISPHERE_LEFT:
         hemStr = "L");
         break;
      case BrainModelSurface::HEMISPHERE_RIGHT:
         hemStr = "R");
         break;
      case BrainModelSurface::HEMISPHERE_BOTH:
         hemStr = "LR");
         break;
      case BrainModelSurface::HEMISPHERE_UNKNOWN:
         hemStr = "U");
         break;
   }
*/
   
   //
   // original caret coord file and topology file names
   //
   originalCoordFileName = flattenSurface->getFileName();
   if (originalCoordFileName.isEmpty()) {
      std::ostringstream str;
      str << "species.case."
          << hemStr.toAscii().constData()
          << ".descrip."
          << numNodes
          << SpecFile::getCoordinateFileExtension().toAscii().constData();
      originalCoordFileName = str.str().c_str();
   }
   originalTopoFileName = flattenSurface->getTopologyFile()->getFileName();
   if (originalTopoFileName.isEmpty()) {
      std::ostringstream str;
      str << "species.case."
          << hemStr.toAscii().constData()
          << ".descrip."
          << numNodes
          << SpecFile::getTopoFileExtension().toAscii().constData();
      originalTopoFileName = str.str().c_str();
   }
   
   //
   // Change any nodes named medial wall in geography column to gyral.
   // Create the geography column if it does not exist.
   //
   PaintFile* paintFile = brainSet->getPaintFile();
   paintFileEmptyAtStart = (paintFile->getNumberOfColumns() == 0);
   geographyColumnNumber = paintFile->getGeographyColumnNumber();
   if (geographyColumnNumber < 0) {
      if (paintFile->getNumberOfColumns() == 0) {
         paintFile->setNumberOfNodesAndColumns(numNodes, 1);
         geographyColumnNumber = 0;
      }
      else {
         paintFile->addColumns(1);
         geographyColumnNumber = paintFile->getNumberOfColumns() - 1;
      }
      paintFile->setColumnName(geographyColumnNumber, PaintFile::columnNameGeography);
   }

   //
   // Change any nodes named MEDIAL.WALL to GYRAL
   //
   medialWallPaintFileIndex = paintFile->addPaintName(medialWallName);
   const int gyrusIndex = paintFile->addPaintName("GYRAL");
   for (int i = 0; i < numNodes; i++) {
      if (paintFile->getPaint(i, geographyColumnNumber) == medialWallPaintFileIndex) {
         paintFile->setPaint(i, geographyColumnNumber, gyrusIndex);
      }
   }

   //
   // Add the GYRAL and MEDIAL.WALL colors if necessary
   //
   bool addedAreaColor = false;
   AreaColorFile* nodeColors = brainSet->getAreaColorFile();
   bool exactMatch = false;
   nodeColors->getColorIndexByName("GYRAL", exactMatch);
   if (exactMatch == false) {
      nodeColors->addColor("GYRAL", 170, 170, 170, 1, 2);
      addedAreaColor = true;
   }
   exactMatch = false;
   nodeColors->getColorIndexByName(medialWallName, exactMatch);
   if (exactMatch == false) {
      nodeColors->addColor(medialWallName, 255, 0, 0, 1, 2);
      addedAreaColor = true;
   }
   
   //
   // Save the node color file if colors were added
   //
   if (addedAreaColor) {
      QString nodeColorFileName(nodeColors->getFileName());
      
      //
      // Create the node color file name if there is not one
      //
      if (nodeColorFileName.isEmpty() || paintFileEmptyAtStart) {
         QString directory, species, casename, anatomy, hemisphere, description;
         QString descriptionNoType, theDate, numNodes, extension;
         if (FileUtilities::parseCaretDataFileName(originalTopoFileName, directory,
                                                   species, casename, anatomy, hemisphere,
                                                   description, descriptionNoType, theDate,
                                                   numNodes, extension)) {
            nodeColorFileName = FileUtilities::reassembleCaretDataFileName(directory,
                                                       species, casename, anatomy, hemisphere,
                                                       "colors", theDate, numNodes, 
                                                       SpecFile::getAreaColorFileExtension());
         }
         else {
            std::ostringstream str;
            str << "species.case."
               << hemStr.toAscii().constData()
               << ".colors."
               << numNodes.toAscii().constData()
               << SpecFile::getAreaColorFileExtension().toAscii().constData();
            nodeColorFileName = str.str().c_str();
         }
      }
      
      //
      // write the node color file
      //
      brainSet->writeAreaColorFile(nodeColorFileName);
   }
   
   //
   // Make a copy of the surface
   // 
   flattenSurface = new BrainModelSurface(*flattenSurface);
   brainSet->addBrainModel(flattenSurface);
   flattenSurface->appendToCoordinateFileComment("Flattening full hemisphere\n");
   
   //
   // Translate the surface to its center of mass
   //
   flattenSurface->translateToCenterOfMass();
   
   //
   // Convert the ellipsoid into a sphere whose surface area is that of the fiducial surface
   //
   const float surfaceArea = fiducialSurface->getSurfaceArea();
   flattenSurface->convertEllipsoidToSphereWithSurfaceArea(surfaceArea);

   //
   // Save the spherical surface
   //
   const QString sphereCoordName = 
      FileUtilities::changeCaretDataFileDescriptionType(originalCoordFileName, "SPHERE");
   try {
      brainSet->writeCoordinateFile(sphereCoordName,
                                    BrainModelSurface::SURFACE_TYPE_SPHERICAL,
                                    flattenSurface->getCoordinateFile());
   }
   catch (FileException& e) {
      throw BrainModelAlgorithmException(e.whatQString());
   }
                                 
   //
   // Copy the surface
   //
   flattenSurface = new BrainModelSurface(*flattenSurface);
   brainSet->addBrainModel(flattenSurface);
   
   //
   // Get the radius of spherical surface
   //
   const float sphereRadius = flattenSurface->getSphericalSurfaceRadius();
   
   //
   // Set the radius of spherical borders to the radius of the surface
   //
   borderFile->setSphericalBorderRadius(sphereRadius);
   
   //
   // Approximate middle of medial wall with AC and offset
   //
   const float middleMedialWallPosFiducial[3] = { acPosition[0] + acOffset[0],
                                                  acPosition[1] + acOffset[1],
                                                  acPosition[2] + acOffset[2] };
                                          
   //
   // Find node closest to middle of medial wall in fiducial surface
   //
   BrainModelSurfacePointLocator fiducialLocator(fiducialSurface, true);
   const int middleMedialWallNodeIndex = fiducialLocator.getNearestPoint(
                                                   middleMedialWallPosFiducial);
   if (middleMedialWallNodeIndex < 0) {
      throw BrainModelAlgorithmException(
         "Unable to find node on fiducial surface nearest middle of medial wall");
   }
                                                   
   //
   // The borders projected to the spherical surface
   //
   //BorderProjectionFile borderProjectionFile = brainSet->getBorderProjectionFile();
   
   //
   // Delete any existing borders and copy the border file to the surface
   //
   BrainModelBorderSet* bmbs = brainSet->getBorderSet();
   brainSet->deleteAllBorders();
   bmbs->copyBordersFromBorderFile(flattenSurface, borderFile);
   
   //
   // Color the borders
   //
   bmbs->assignColors();
   
   //
   // Project the borders onto the spherical surface
   //
   bmbs->projectBorders(flattenSurface);
   
   //
   // Orient the sphere so that the middle of the medial wall is on the positive Z axix
   //
   CoordinateFile* flattenCoordinates = flattenSurface->getCoordinateFile();
   const float* middleMedialWallSphereCoord = 
      flattenCoordinates->getCoordinate(middleMedialWallNodeIndex);
   flattenSurface->orientPointToNegativeZAxis(middleMedialWallSphereCoord);
   
   //
   // Compress the medial wall
   //
   flattenSurface->convertSphereToCompressedMedialWall(0.5);

   //
   // Rotate the surface so that the medial wall faces the user
   //
   TransformationMatrix tm;
   tm.rotate(TransformationMatrix::ROTATE_Y_AXIS, 180.0);
   if (flattenSurface->getStructure() == Structure::STRUCTURE_TYPE_CORTEX_LEFT) {
      tm.rotate(TransformationMatrix::ROTATE_Z_AXIS, 90.0);
   }
   else {
      tm.rotate(TransformationMatrix::ROTATE_Z_AXIS, -90.0);
   }
   flattenSurface->applyTransformationMatrix(tm);
   
   //
   // Save the compressed medial wall coordinates and move all with positive Z
   // to the X-Y plane
   //
   flattenSurface->pushCoordinates();
   flattenSurface->projectCoordinatesToPlane(
                   BrainModelSurface::COORDINATE_PLANE_MOVE_POSITIVE_Z_TO_ZERO);
       
   //
   // Update the surface's normals
   //
   flattenSurface->computeNormals();
   
   //
   // Save the compressed medial wall surface
   //
   const QString cmwCoordName = 
      FileUtilities::changeCaretDataFileDescriptionType(originalCoordFileName, "CMW");
   try {
      brainSet->writeCoordinateFile(cmwCoordName,
                                    BrainModelSurface::SURFACE_TYPE_COMPRESSED_MEDIAL_WALL,
                                    flattenSurface->getCoordinateFile());
   }
   catch (FileException& e) {
      throw BrainModelAlgorithmException(e.whatQString());
   }

   //
   // Copy the surface
   //
   //moved to executePart2() 11/07/06
   //flattenSurface = new BrainModelSurface(*flattenSurface);
   //brainSet->addBrainModel(flattenSurface);
   
   //
   // Apply the border projection file to the all surfaces
   //
   bmbs->unprojectBordersForAllSurfaces();
   
   //
   // clear the border projection file
   //
   //borderProjectionFile->clear();
   
   //
   // Make sure there is some sort of coloring
   //
   SurfaceShapeFile* ssf = brainSet->getSurfaceShapeFile();
   if (ssf->getNumberOfColumns() > 0) {
      brainSet->getSurfaceUnderlay()->setOverlay(-1, 
                     BrainModelSurfaceOverlay::OVERLAY_SURFACE_SHAPE);
   }
   else {
      PaintFile* pf = brainSet->getPaintFile();
      if (pf->getNumberOfColumns() > 0) {
         brainSet->getSurfaceUnderlay()->setOverlay(-1, 
                     BrainModelSurfaceOverlay::OVERLAY_PAINT);            
      }
   }
}

      
/// Execute the second part of the flattening
void 
BrainModelSurfaceFlattenFullHemisphere::executePart2() 
                            throw (BrainModelAlgorithmException)
{
   const int numNodes = flattenSurface->getNumberOfNodes();
   
   //
   // Get the compressed medial wall border file
   //
   BrainModelBorderSet* bmbs = brainSet->getBorderSet();
   BorderFile* cmwBorder = bmbs->copyBordersOfSpecifiedType(
                            BrainModelSurface::SURFACE_TYPE_COMPRESSED_MEDIAL_WALL);
   if (cmwBorder == NULL) {
      throw BrainModelAlgorithmException("Unable to find compressed medial wall border.");
   }
   
   //
   // Copy the surface
   //
   //moved here 11/7/06
   flattenSurface = new BrainModelSurface(*flattenSurface);
   brainSet->addBrainModel(flattenSurface);

   //
   // Copy the topology file
   //
   const TopologyFile* oldTopology = flattenSurface->getTopologyFile();
   TopologyFile* topology = new TopologyFile(*oldTopology);
   topology->setTopologyType(TopologyFile::TOPOLOGY_TYPE_OPEN);
   brainSet->addTopologyFile(topology);
   flattenSurface->setTopologyFile(topology);
   
   //
   // Save the cmw border
   //
   QString templateCutsBorderFileName;
   {
      QString directory, species, casename, anatomy, hemisphere, description;
      QString descriptionNoType, theDate, numNodes, extension;
      if (FileUtilities::parseCaretDataFileName(originalTopoFileName, directory,
                                                species, casename, anatomy, hemisphere,
                                                description, descriptionNoType, theDate,
                                                numNodes, extension)) {
         templateCutsBorderFileName = FileUtilities::reassembleCaretDataFileName(directory,
                                                      species, casename, anatomy, hemisphere,
                                                      "TemplateCuts", theDate, numNodes, 
                                                      SpecFile::getBorderFileExtension());
      }
      else {
         std::ostringstream str;
         str << "species.case."
            << hemStr.toAscii().constData()
            << ".TemplateCuts."
            << numNodes.toAscii().constData()
            << SpecFile::getBorderFileExtension().toAscii().constData();
         templateCutsBorderFileName = str.str().c_str();
      }
   }
   brainSet->writeBorderFile(templateCutsBorderFileName, flattenSurface,
                             BrainModelSurface::SURFACE_TYPE_COMPRESSED_MEDIAL_WALL, "", "");
   
   //
   // Find the border named medial wall
   //
   const int medialWallBorderIndex = cmwBorder->getBorderIndexByName(medialWallName);
   if (medialWallBorderIndex < 0) {
      throw BrainModelAlgorithmException("Unable to find MEDIAL.WALL border.");
   }
   Border* medialWallBorder = cmwBorder->getBorder(medialWallBorderIndex);
   
   //
   // Find all nodes within the medial wall
   //
   CoordinateFile* flattenCoordinates = flattenSurface->getCoordinateFile();
   std::vector<bool> nodesInsideMedialWallBorder(numNodes, false);
   medialWallBorder->pointsInsideBorder2D(flattenCoordinates->getCoordinate(0), numNodes,
                                        nodesInsideMedialWallBorder, true);
   
   //
   // Set geography for all nodes within the medial wall to MEDIAL.WALL
   //
   PaintFile* paintFile = brainSet->getPaintFile();
   for (int i = 0; i < numNodes; i++) {
      if (nodesInsideMedialWallBorder[i]) {
         paintFile->setPaint(i, geographyColumnNumber, medialWallPaintFileIndex);
      }
   }
   
   //
   // Save the paint file
   //
   QString paintFileName(paintFile->getFileName());
   if (paintFileName.isEmpty() || paintFileEmptyAtStart) {
      QString directory, species, casename, anatomy, hemisphere, description;
      QString descriptionNoType, theDate, numNodes, extension;
      if (FileUtilities::parseCaretDataFileName(originalTopoFileName, directory,
                                                species, casename, anatomy, hemisphere,
                                                description, descriptionNoType, theDate,
                                                numNodes, extension)) {
         paintFileName = FileUtilities::reassembleCaretDataFileName(directory,
                                                      species, casename, anatomy, hemisphere,
                                                      "geography", theDate, numNodes, 
                                                      SpecFile::getAreaColorFileExtension());
      }
      else {
         std::ostringstream str;
         str << "species.case."
            << hemStr.toAscii().constData()
            << ".geography."
            << numNodes.toAscii().constData()
            << SpecFile::getPaintFileExtension().toAscii().constData();
         paintFileName = str.str().c_str();
      }
   }
   brainSet->writePaintFile(paintFileName);

   //
   // Should the fiducial surface have its medial wall smoothed ?
   //
   if (smoothFiducialMedialWallFlag) {
      //
      // Smooth the medial wall of the fiducial surface
      //
      fiducialSurface->arealSmoothing(1.0, 500, 0, &nodesInsideMedialWallBorder);
      fiducialSurface->computeNormals();
      
      //
      // Write the fiducial coord file
      //
      try {
         CoordinateFile* cf = fiducialSurface->getCoordinateFile();
         cf->writeFile(cf->getFileName());
      }
      catch (FileException& e) {
         std::cout << "ERROR: Unable to write fiducial coordinate file after "
                   << "smoothing the medial wall: " << e.whatQString().toAscii().constData() << std::endl;
      }
   }
   
   //
   // Save the geography since applying cuts may modify it
   //
   std::vector<int> savedGeography(numNodes);
   for (int i = 0; i < numNodes; i++) {
      savedGeography[i] = paintFile->getPaint(i, geographyColumnNumber);
   }
   
   //
   // Save the medial wall, calcarine cut, and the frontal cut borders
   //
   saveBordersForLandmarks(cmwBorder);
   
   //
   // Remove the medial wall border from all borders
   //
   std::vector<QString> namesToDelete;
   namesToDelete.push_back(medialWallName);
   bmbs->deleteBordersWithNames(namesToDelete);
   
   //
   // Apply all of the cuts
   //
   BrainModelSurfaceCutter cutter(flattenSurface, cmwBorder, 
                                  BrainModelSurfaceCutter::CUTTING_MODE_NON_NEGATIVE_Z_ONLY);
   try {
      cutter.execute();
   }
   catch (BrainModelAlgorithmException& e) {
      throw e;
   }
   
   //
   // Restore the geography
   //
   for (int i = 0; i < numNodes; i++) {
      paintFile->setPaint(i, geographyColumnNumber, savedGeography[i]);
   }
   paintFile->clearModified();
   
   //
   // Create the landmark borders for deformation
   //
   createDeformationBorders(paintFile);
   
   //
   // Restore the coordinates that were saved when positives projected flat
   //
   flattenSurface->popCoordinates();
   
   //
   // update the normals
   //
   flattenSurface->computeNormals();
   
   //
   // Orient the sphere so that the medial wall is on the negative Z axis
   //
   std::vector<QString> paintNames;
   paintNames.push_back(medialWallName);
   QString errorMessage;
   if (flattenSurface->orientPaintedNodesToNegativeZAxis(brainSet->getPaintFile(),
                                                         paintNames,
                                                         geographyColumnNumber,
                                                         errorMessage)) {
      throw BrainModelAlgorithmException(errorMessage);
   }
   
   //
   // Remove the medial wall
   //
   brainSet->disconnectNodes(topology, nodesInsideMedialWallBorder);
   
   //
   // Remove any stragglers (tiles that are connected to other tiles only at a single node)
   //
   flattenSurface->getTopologyFile()->removeCornerTiles(2);
   flattenSurface->moveDisconnectedNodesToOrigin();

   //
   // Project the sphere flat
   //
   flattenSurface->convertSphereToFlat();
   
   //
   // Smooth just a little bit
   //
   flattenSurface->arealSmoothing(1.0, 5, 0);
   
   //
   // update the normals
   //
   flattenSurface->computeNormals();
   
   //
   // Scale the surface to 10x the fiducial area
   //
   flattenSurface->scaleSurfaceToArea(10.0 * fiducialSurface->getSurfaceArea(), false);

   //
   // Translate to center of mass and scale to fit the window
   //
   flattenSurface->translateToCenterOfMass();
   flattenSurface->updateForDefaultScaling();
         
   //
   // Remove any stragglers (tiles that are connected to other tiles only at a single node)
   //
  // flattenSurface->getTopologyFile()->removeCornerTiles(2);
  // flattenSurface->moveDisconnectedNodesToOrigin();
   
   //
   // Classify the nodes
   //
   brainSet->classifyNodes(topology);
   
   //
   // Save the topology file
   //
   const QString cutTopoName = 
      FileUtilities::changeCaretDataFileDescriptionType(originalTopoFileName, "CUT");
   try {
      brainSet->writeTopologyFile(cutTopoName,
                                  TopologyFile::TOPOLOGY_TYPE_CUT,
                                  topology);
   }
   catch (FileException& e) {
      throw BrainModelAlgorithmException(e.whatQString());
   }
   
   //
   // Save the flat coordinate file
   //
   const QString flatCoordName = 
      FileUtilities::changeCaretDataFileDescriptionType(originalCoordFileName, "InitialFlat");
   try {
      brainSet->writeCoordinateFile(flatCoordName,
                                    BrainModelSurface::SURFACE_TYPE_FLAT,
                                    flattenSurface->getCoordinateFile());
   }
   catch (FileException& e) {
      throw BrainModelAlgorithmException(e.whatQString());
   }
   
   //
   // Set all borders as unmodified
   //
   bmbs->setAllBordersModifiedStatus(false);
   bmbs->setProjectionsModified(false);
}

/**
 * Finds and save the borders for creating landmark borders.  The dorsal and ventral 
 * portions of the medial wall border are determined and the part of the calcarine
 * cut that is outside the medial wall.
 */
void 
BrainModelSurfaceFlattenFullHemisphere::saveBordersForLandmarks(BorderFile* bf)
{
   landmarksForDeformation.clear();
   
   int medialWallIndex   = -1;
   int calcarineCutIndex = -1;
   int frontalCutIndex   = -1;
   
   //
   // Find the MEDIAL.WALL, CalcarineCut, and FrontalCut borders.
   //
   const int numBorders = bf->getNumberOfBorders();
   for (int i = 0; i < numBorders; i++) {
      Border* b = bf->getBorder(i);
      if (b->getNumberOfLinks() > 0) {
         if (b->getName() == medialWallName) {
            //landmarksForDeformation.addBorder(*b);
            medialWallIndex = i;
         }
         else if (b->getName() == calcarineCutName) {
            //landmarksForDeformation.addBorder(*b);
            calcarineCutIndex = i;
         }
         else if (b->getName() == frontalCutName) {
            //landmarksForDeformation.addBorder(*b);
            frontalCutIndex = i;
         }
      }
   }
   
   //
   // If borders missing, print error messages
   //
   if (medialWallIndex < 0) {
         std::cout << "Missing " << medialWallName.toAscii().constData() << " border for deformation landmarks."
                  << std::endl;
   }
   if (calcarineCutIndex < 0) {
         std::cout << "Missing " << calcarineCutName.toAscii().constData() << " border for deformation landmarks."
                  << std::endl;
   }
   if (frontalCutIndex < 0) {
         std::cout << "Missing " << frontalCutName.toAscii().constData() << " border for deformation landmarks."
                  << std::endl;
   }

   //
   // If borders found 
   //
   if ((medialWallIndex >= 0) && (calcarineCutIndex >= 0) && (frontalCutIndex >= 0)) {
      //
      // Find out where medial wall border intersects calcarine and frontal
      //
      Border* medialWallBorder = bf->getBorder(medialWallIndex);
      
      Border* calcarineCutBorder = bf->getBorder(calcarineCutIndex);
      
      Border* frontalCutBorder = bf->getBorder(frontalCutIndex);
      
      //
      // NOTE: When viewing the compressed medial wall surface, the surface is presented in
      // the X-Y plane.      
      //
      // End of calcarine cut border should be most negative X for left, most positive X for right
      //
      float calc0[3], calcLast[3];
      calcarineCutBorder->getLinkXYZ(0, calc0);
      calcarineCutBorder->getLinkXYZ(calcarineCutBorder->getNumberOfLinks() - 1, calcLast);
      if (flattenSurface->getStructure() == Structure::STRUCTURE_TYPE_CORTEX_LEFT) {
         if (calc0[0] > calcLast[0]) {
            calcarineCutBorder->reverseBorderLinks();
         }
      }      
      else {
         if (calc0[0] < calcLast[0]) {
            calcarineCutBorder->reverseBorderLinks();
         }
      }
      
      //
      // Indices to border links where borders intersect
      //
      int medialWallCalcarineIntersect = -1;
      int medialWallFrontalIntersect   = -1;
      int calcarineMedialWallIntersect = -1;
      int frontalMedialWallIntersect   = -1;
      
      //
      // Find intersections
      //
      const bool medialWallCalcarineIntersectionValid =
         medialWallBorder->intersection2D(calcarineCutBorder, 
                                          true,
                                          false,
                                          medialWallCalcarineIntersect,
                                          calcarineMedialWallIntersect);
      const bool medialWallFrontalIntersectionValid =
         medialWallBorder->intersection2D(frontalCutBorder,
                                          true,
                                          false,
                                          medialWallFrontalIntersect,
                                          frontalMedialWallIntersect);
       
      if (medialWallCalcarineIntersectionValid == false) {
         std::cout << "Unable to find medial wall calacarine cut intersection." << std::endl;
      }
      if (medialWallFrontalIntersectionValid == false) {
         std::cout << "Unable to find medial wall frontal cut intersection." << std::endl;
      }
      
      if (medialWallCalcarineIntersectionValid && medialWallFrontalIntersectionValid) {
         if (DebugControl::getDebugOn()) {
            std::cout << "Found medial wall intersections with both the calcarine and " 
                      << "frontal cuts." << std::endl;
         }
         
         //
         // Get the two pieces of the medial wall border
         //
         Border* dorsalMedialWall = medialWallBorder->getSubSet(medialWallCalcarineIntersect,
                                                                medialWallFrontalIntersect);
         if (dorsalMedialWall == NULL) {
            std::cout << "Calcarine to Frontal section of medial wall border is empty" << std::endl;
         }
         Border* ventralMedialWall = medialWallBorder->getSubSet(medialWallFrontalIntersect,
                                                                 medialWallCalcarineIntersect);
         if (ventralMedialWall == NULL) {
            std::cout << "Frontal to Calcarine section of medial wall border is empty" << std::endl;
            if (dorsalMedialWall != NULL) {
               delete dorsalMedialWall;
            }
         }
         
         if ((dorsalMedialWall != NULL) && (ventralMedialWall != NULL)) {
            //
            // Find out which one is dorsal (biggest Y) and ventral (smallest Y)
            //
            float dorsalBounds[6], ventralBounds[6];
            dorsalMedialWall->getBounds(dorsalBounds);
            ventralMedialWall->getBounds(ventralBounds);
            if (dorsalBounds[3] < ventralBounds[3]) {
               //
               // Switch the pointers
               //
               std::swap(dorsalMedialWall, ventralMedialWall);
            }

            //
            // Set the names of the borders
            //
            dorsalMedialWall->setName(medialWallDorsalLandmark);
            ventralMedialWall->setName(medialWallVentralLandmark);
            
            //
            // Reverse links of dorsal border if necessary
            // Most positive X should be at beginning of border for left, most negative for right
            //
            float dorsal0[3], dorsalLast[3];
            dorsalMedialWall->getLinkXYZ(0, dorsal0);
            dorsalMedialWall->getLinkXYZ(dorsalMedialWall->getNumberOfLinks() - 1, dorsalLast);
            if (flattenSurface->getStructure() == Structure::STRUCTURE_TYPE_CORTEX_LEFT) {
               if (dorsal0[0] < dorsalLast[0]) {
                  dorsalMedialWall->reverseBorderLinks();
               }
            }
            else {
               if (dorsal0[0] > dorsalLast[0]) {
                  dorsalMedialWall->reverseBorderLinks();
               }
            }

            //
            // Reverse links of ventral border if necessary
            // Most negative X should be at beginning of border for left, most positive for right
            //
            float ventral0[3], ventralLast[3];
            ventralMedialWall->getLinkXYZ(0, ventral0);
            ventralMedialWall->getLinkXYZ(ventralMedialWall->getNumberOfLinks() - 1, ventralLast);
            if (flattenSurface->getStructure() == Structure::STRUCTURE_TYPE_CORTEX_LEFT) {
               if (ventral0[0] > ventralLast[0]) {
                  ventralMedialWall->reverseBorderLinks();
               }
            }
            else {
               if (ventral0[0] < ventralLast[0]) {
                  ventralMedialWall->reverseBorderLinks();
               }
            }
            
            //
            // Get the calcarine piece from its start to the medial wall
            // 
            Border* calcarinePiece = calcarineCutBorder->getSubSet(0, calcarineMedialWallIntersect - 1);
            calcarinePiece->setName(calcarineLandmark);
            
            //
            // save the borders
            //
            landmarksForDeformation.addBorder(*dorsalMedialWall);
            landmarksForDeformation.addBorder(*ventralMedialWall);
            landmarksForDeformation.addBorder(*calcarinePiece);
            
            //
            // free up memory
            //
            delete dorsalMedialWall;
            delete ventralMedialWall;
            delete calcarinePiece;
         }
      }
   }
}

/**
 * create the landarks for deformation
 */
void 
BrainModelSurfaceFlattenFullHemisphere::createDeformationBorders(PaintFile* pf)
{   
   const int numNodes = flattenSurface->getNumberOfNodes();
   
   //
   // Find the medial wall nodes
   //
   std::vector<bool> nonMedialWallNodes(numNodes, true);
   for (int i = 0; i < numNodes; i++) {
      if (pf->getPaint(i, geographyColumnNumber) == medialWallPaintFileIndex) {
         nonMedialWallNodes[i] = false;
      }
   }
   
   //
   // Create a point locator
   //
   BrainModelSurfacePointLocator locator(flattenSurface, true, false, &nonMedialWallNodes);
   
   //
   // Initialize a border projection file
   //
   BorderProjectionFile bpf;
   
   //
   // Convert the landmarks to a border projection file by dragging each border
   // link to the nearest node.
   //
   for (int m = 0; m < landmarksForDeformation.getNumberOfBorders(); m++) {
      const Border* b = landmarksForDeformation.getBorder(m);
      
      //
      // Create the border projection with the borders attributes
      //
      QString name;
      float center[3], sampling, variance, topographyValue, arealUncertainty;
      b->getData(name, center, sampling, variance, topographyValue, arealUncertainty);
      BorderProjection bp(b->getName(), center, sampling, variance, topographyValue, arealUncertainty);
      
      //
      // Project each border link to the nearest node and save as a border projection link
      //
      int previousNode = -1;
      for (int i = 0; i < b->getNumberOfLinks(); i++) {
         const int nearestNode = locator.getNearestPoint(b->getLinkXYZ(i));
         if (nearestNode != previousNode) {
            const int vertices[3] = { nearestNode, nearestNode, nearestNode };
            const float areas[3] = { 1.0, 0.0, 0.0 };
            BorderProjectionLink bpl(b->getLinkSectionNumber(i), vertices, areas, b->getLinkRadius(i));
            bp.addBorderProjectionLink(bpl);
         }
         previousNode = nearestNode;
      }
      
      //
      // If this projection is not empty
      //
      if (bp.getNumberOfLinks() > 0) {
         //
         // If this is a medial wall border
         //
         if ((bp.getName() == medialWallDorsalLandmark) ||
             (bp.getName() == medialWallVentralLandmark)) {
            //
            // Remove is last link
            //
            bp.removeLastBorderProjectionLink();
         }
         
         //
         // Add the border projection to the file
         //
         bpf.addBorderProjection(bp);
      }
   }
   
   //
   // Save the landmarks in a border projection file
   //
   if (bpf.getNumberOfBorderProjections()) {
      QString borderProjectionFileName;
      QString directory, species, casename, anatomy, hemisphere, description;
      QString descriptionNoType, theDate, numNodes, extension;
      if (FileUtilities::parseCaretDataFileName(originalTopoFileName, directory,
                                                species, casename, anatomy, hemisphere,
                                                description, descriptionNoType, theDate,
                                                numNodes, extension)) {
         borderProjectionFileName = FileUtilities::reassembleCaretDataFileName(directory,
                                                      species, casename, anatomy, hemisphere,
                                                      "LANDMARKS.FromFlattening", theDate, numNodes, 
                                                      SpecFile::getBorderProjectionFileExtension());
      }
      else {
         std::ostringstream str;
         str << "species.case."
            << hemStr.toAscii().constData()
            << ".LANDMARKS.FromFlattening."
            << numNodes.toAscii().constData()
            << SpecFile::getBorderProjectionFileExtension().toAscii().constData();
         borderProjectionFileName = str.str().c_str();
      }
      
      bpf.writeFile(borderProjectionFileName);
      brainSet->addToSpecFile(SpecFile::getBorderProjectionFileTag(), borderProjectionFileName);
   }
}