File: BrainModelSurfaceCutter.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 (188 lines) | stat: -rw-r--r-- 5,774 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
#include <algorithm>
#include <iostream>
#include <vector>

#include <QTime>

#include "vtkLine.h"

#include "BrainModelSurface.h"
#include "BrainModelSurfaceCutter.h"
#include "BorderFile.h"
#include "DebugControl.h"
#include "MathUtilities.h"
#include "TopologyFile.h"

/**
 * Constructor.
 */
BrainModelSurfaceCutter::BrainModelSurfaceCutter(BrainModelSurface* cuttingSurfaceIn,
                                                 BorderFile* cutsFileIn,
                                                 const CUTTING_MODE cuttingModeIn)
   : BrainModelAlgorithm(cuttingSurfaceIn->getBrainSet())
{
   cuttingSurface = cuttingSurfaceIn;
   theCuts        = cutsFileIn;
   cuttingMode    = cuttingModeIn;
}

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

/**
 * Execute the flattening.
 */
void 
BrainModelSurfaceCutter::execute() throw (BrainModelAlgorithmException)
{
   QTime timer;
   timer.start();
   
   //
   // NOTE: We could save some time and use a topology helper.  But, if the topology is messed
   // up and more than two tiles use an edge, not all tiles would be removed.  So, just
   // test all tiles.
   //
   
   //
   // Get the topolgoy file
   //
   TopologyFile* topology = cuttingSurface->getTopologyFile();

   //
   // Get the coordinate file
   //
   const CoordinateFile* coords = cuttingSurface->getCoordinateFile();
   const float* coordXYZ = coords->getCoordinate(0);
   
   //
   // Keep track of tiles that need to be deleted.
   //
   const int numTiles = topology->getNumberOfTiles();   
   std::vector<bool> deleteTheTile(numTiles, false);
   
   //
   // Apply all of the cuts
   //
   const int numCuts = theCuts->getNumberOfBorders();
   for (int cti = 0; cti < numCuts; cti++) {
      //
      // only do cuts with at least two links
      //
      const Border* cut = theCuts->getBorder(cti);
      const int numCutPoints = cut->getNumberOfLinks();
      if (numCutPoints > 1) {
         //
         // test each segment in the cut
         //
         for (int cpi = 0; cpi < (numCutPoints - 1); cpi++) {
            float c1[3], c2[3];
            cut->getLinkXYZ(cpi, c1);
            cut->getLinkXYZ(cpi + 1, c2);
            c1[2] = 0.0;
            c2[2] = 0.0;
            
            //
            // Test this cut segment agains all tiles
            //
            for (int tile = 0; tile < numTiles; tile++) {
               //
               // If tile has not already been marked for deletion
               //
               if (deleteTheTile[tile] == false) {
                  //
                  // Get the vertices of the tile
                  //
                  int v1, v2, v3;
                  topology->getTile(tile, v1, v2, v3);
                  //float p1[3], p2[3], p3[3];
                  //coords->getCoordinate(v1, p1); 
                  //coords->getCoordinate(v2, p2); 
                  //coords->getCoordinate(v3, p3);
                  
                  const float* p1 = &coordXYZ[v1*3];
                  const float* p2 = &coordXYZ[v2*3];
                  const float* p3 = &coordXYZ[v3*3];
                  bool doCutCheck = true;
                  switch(cuttingMode) {
                     case CUTTING_MODE_NORMAL:
                        break;
                     case CUTTING_MODE_NON_NEGATIVE_Z_ONLY:
                        if ((p1[2] < 0.0) || (p2[2] < 0.0) || (p3[2] < 0.0)) {
                           doCutCheck = false;
                        }
                        break;
                  }
                  
                  if (doCutCheck) {
                     //p1[2] = 0.0;
                     //p2[2] = 0.0;
                     //p3[2] = 0.0;
                     
                     //
                     // Check the cut segment against each tile edge
                     //
                     float intersection[2];
                     if (MathUtilities::lineIntersection2D(c1, c2, p1, p2, intersection)) {
                        deleteTheTile[tile] = true;
                     }
                     else if (MathUtilities::lineIntersection2D(c1, c2, p2, p3, intersection)) {
                        deleteTheTile[tile] = true;
                     }
                     else if (MathUtilities::lineIntersection2D(c1, c2, p3, p1, intersection)) {
                        deleteTheTile[tile] = true;
                     }
                  }
               }
            }
         }
      }
   }
   
   //
   // If cuts were made
   //
   if (std::find(deleteTheTile.begin(), deleteTheTile.end(), true) != deleteTheTile.end()) {
      //
      // Remove the cut tiles
      //
      if (DebugControl::getDebugOn()) {
         std::cout << "deleting tiles ";
      }
      int cnt = 0;
      std::vector<int> tilesToDelete;
      for (int i = 0; i < numTiles; i++) {
         if (deleteTheTile[i]) {
            tilesToDelete.push_back(i);
            if (DebugControl::getDebugOn()) {
               std::cout << " " << i;
            }
            cnt++;
         }
      }
      if (DebugControl::getDebugOn()) {
         std::cout << std::endl;
         std::cout << "Deleting " << cnt << " tiles." << std::endl;
      }

      const int numTilesBeforeDelete = topology->getNumberOfTiles();
      topology->deleteTiles(tilesToDelete);
      if (DebugControl::getDebugOn()) {
         std::cout << "Tiles before applying cuts: " << numTilesBeforeDelete << std::endl;
         std::cout << "Tiles after applying cuts: " << topology->getNumberOfTiles() << std::endl;
      }
      
      //
      // Set topolgy file type to cut
      //
      topology->setTopologyType(TopologyFile::TOPOLOGY_TYPE_CUT);
   }
   
   //std::cout << "Total time: " << timer.elapsed() / 1000 << " seconds." << std::endl;
}