File: BrainModelSurfacePolyhedronNew.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 (416 lines) | stat: -rw-r--r-- 14,997 bytes parent folder | download
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

/*LICENSE_START*/

#include "BrainSet.h"

/*
 *  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 <cmath>

#include "BrainModelSurface.h"
//#define __BRAIN_MODEL_SURFACE_POLYAHEDRON_MAIN__
#include "BrainModelSurfacePolyhedronNew.h"
//#undef __BRAIN_MODEL_SURFACE_POLYAHEDRON_MAIN__
#include "CoordinateFile.h"
#include "TopologyFile.h"
#include "TopologyHelper.h"

//define polyhedron type
const BrainModelSurfacePolyhedronNew::POLYHEDRA
   BrainModelSurfacePolyhedronNew::polyhedra =
      BrainModelSurfacePolyhedronNew::POLYHEDRA_ICOSAHEDRON;


/**
 * constructor.
 */
BrainModelSurfacePolyhedronNew::BrainModelSurfacePolyhedronNew(BrainSet* bs,
                                              const int numberOfDivisionsIn)
   : BrainModelAlgorithm(bs),
        numberOfDivisions(numberOfDivisionsIn)
{
   if (numberOfDivisions < 1) numberOfDivisions = 1;
   surface = NULL;
}

/**
 * destructor.
 */
BrainModelSurfacePolyhedronNew::~BrainModelSurfacePolyhedronNew()
{

}

/**
 * get number of nodes and triangles from the number of iterations.
 */
void
BrainModelSurfacePolyhedronNew::getNumberOfNodesAndTrianglesFromDivisions(
                                        int divisions,
                                        int& numNodesOut,
                                        int& numTrianglesOut)
{

   //
   // Initial number of triangles for initial tetrahedron
   //
   int numTriangles = 0;
   switch (polyhedra) {
      case POLYHEDRA_TETRAHEDRON:
         numTriangles = 4;
         break;
      case POLYHEDRA_OCTAHEDRON:
         numTriangles = 8;
         break;
      case POLYHEDRA_ICOSAHEDRON:
         numTriangles = 20;
         break;
   }

   //
   // Calculate number of nodes using the Euler Characteristic
   //
   // V - E + F = 2
   // V = 2 + E - F
   // 3 edges per triangle with each edge shared
   // V = 2 + ((F * 3) / 2) - F
   // V = 2 + 1.5 * F - F
   // V = 2 + (1.5 - 1) * F
   // V = 2 + 0.5 * F
   // V = 2 + (F / 2)
   //
   numTrianglesOut = numTriangles * divisions * divisions;//yes, its really that simple, the sum of the two triangulars is square
   numNodesOut = 2 + (numTrianglesOut / 2);
}

/**
 * execute the algorithm.
 */
void
BrainModelSurfacePolyhedronNew::execute() throw (BrainModelAlgorithmException)
{
   //
   // Create a surface
   //
   this->surface = new BrainModelSurface(brainSet);
   this->surface->setSurfaceType(BrainModelSurface::SURFACE_TYPE_SPHERICAL);
   this->brainSet->addBrainModel(surface);

   //
   // Set the initial coordinates
   //
   coordinateFile = surface->getCoordinateFile();

   //
   // Create and set the initial topology
   //
   TopologyFile* initTopo = new TopologyFile();
   topologyFile = new TopologyFile();
   initTopo->setTopologyType(TopologyFile::TOPOLOGY_TYPE_CLOSED);
   topologyFile->setTopologyType(TopologyFile::TOPOLOGY_TYPE_CLOSED);
   this->brainSet->addTopologyFile(topologyFile);
   this->surface->setTopologyFile(this->topologyFile);

   //
   // Get needed sizes of files
   //
   int totalTiles, totalNodes;
   getNumberOfNodesAndTrianglesFromDivisions(numberOfDivisions, totalNodes, totalTiles);
   coordinateFile->setNumberOfCoordinates(totalNodes);
   topologyFile->setNumberOfTiles(totalTiles);
   curTiles = 0;
   
   //
   // Create the initial polyhedra
   //
   switch (this->polyhedra) {
      case POLYHEDRA_TETRAHEDRON:
         curNodes = 4;
         coordinateFile->setCoordinate(0,  1.0,  1.0,  1.0);
         coordinateFile->setCoordinate(1, -1.0, -1.0,  1.0);
         coordinateFile->setCoordinate(2, -1.0,  1.0, -1.0);
         coordinateFile->setCoordinate(3,  1.0, -1.0, -1.0);
         initTopo->setNumberOfTiles(4);
         initTopo->setTile(0, 1, 0, 2);
         initTopo->setTile(1, 1, 3, 0);
         initTopo->setTile(2, 2, 3, 0);
         initTopo->setTile(3, 3, 2, 1);
         break;
      case POLYHEDRA_OCTAHEDRON:
         curNodes = 6;
         coordinateFile->setCoordinate(0, -1.0, -1.0,  0.0);
         coordinateFile->setCoordinate(1,  1.0, -1.0,  0.0);
         coordinateFile->setCoordinate(2,  1.0,  1.0,  0.0);
         coordinateFile->setCoordinate(3, -1.0,  1.0,  0.0);
         coordinateFile->setCoordinate(4,  0.0,  0.0, -1.0);
         coordinateFile->setCoordinate(5,  0.0,  0.0,  1.0);
         initTopo->setNumberOfTiles(8);
         initTopo->setTile(0, 0, 1, 5);
         initTopo->setTile(1, 1, 2, 5);
         initTopo->setTile(2, 2, 3, 5);
         initTopo->setTile(3, 3, 0, 5);
         initTopo->setTile(4, 0, 1, 4);
         initTopo->setTile(5, 1, 2, 4);
         initTopo->setTile(6, 2, 3, 4);
         initTopo->setTile(7, 3, 0, 4);
         break;
      case POLYHEDRA_ICOSAHEDRON:
         {
            const double phi = (1.0 + std::sqrt(5.0)) / 2.0;
            curNodes = 12;

            coordinateFile->setCoordinate(0, -phi,  0.0,  1.0);
            coordinateFile->setCoordinate(1,    0, -1.0,  phi);
            coordinateFile->setCoordinate(2,  phi,  0.0,  1.0);
            coordinateFile->setCoordinate(3,    0,  1.0,  phi);

            coordinateFile->setCoordinate(4, -1.0, -phi, 0.0);
            coordinateFile->setCoordinate(5,  1.0, -phi, 0.0);
            coordinateFile->setCoordinate(6,  1.0,  phi, 0.0);
            coordinateFile->setCoordinate(7, -1.0,  phi, 0.0);

            coordinateFile->setCoordinate(8, -phi,  0.0, -1.0);
            coordinateFile->setCoordinate(9,    0, -1.0, -phi);
            coordinateFile->setCoordinate(10, phi,  0.0, -1.0);
            coordinateFile->setCoordinate(11,   0,  1.0, -phi);

            initTopo->setNumberOfTiles(20);
            initTopo->setTile(0, 0, 7, 8);
            initTopo->setTile(1, 0, 3, 7);
            initTopo->setTile(2, 0, 1, 3);
            initTopo->setTile(3, 1, 2, 3);
            initTopo->setTile(4, 1, 5, 2);
            initTopo->setTile(5, 5, 9, 10);
            initTopo->setTile(6, 10, 11, 6);
            initTopo->setTile(7, 4, 1, 0);
            initTopo->setTile(8, 4, 5, 1);
            initTopo->setTile(9, 4, 9, 5);
            initTopo->setTile(10, 5, 10, 2);
            initTopo->setTile(11, 10, 6, 2);
            initTopo->setTile(12, 6, 11, 7);
            initTopo->setTile(13, 11, 9, 8);
            initTopo->setTile(14, 9, 4, 8);
            initTopo->setTile(15, 8, 4, 0);
            initTopo->setTile(16, 11, 8, 7);
            initTopo->setTile(17, 6, 7, 3);
            initTopo->setTile(18, 6, 3, 2);
            initTopo->setTile(19, 11, 10, 9);
         }
         break;
   }
   // Note: if further refinement is required (unlikely), use a dodecahedron, divide each face into 5 triangles, keep track of which vertex is in the
   // center, and always pass that vertex as the first vertex to interp3.  Then, make two separate polynomials to interpolate weights, one for the first
   // vertex and one for the other two.  Then, optimize with a genetic algorithm for both variance of triangle area, and some regularization term to keep
   // the shape of the triangles consistent.  This means the triangles, once projected to the sphere, will each be identical, and have smaller area,
   // causing the polynomial that modifies the linear weights closer to linear, therefore with less edge effects. - Tim C

   //
   // Begin triangle division code - does not rely on spherical, preserves tile orientation
   //
   edgenodes.resize(curNodes - 1);//stores by low node #
   int edgesize = numberOfDivisions + 1;
   int i, j, k, numTiles = initTopo->getNumberOfTiles(), intRows = numberOfDivisions - 1, intCols;
   float tempcoord[3], coord1[3], coord2[3], coord3[3];
   int* edge1 = new int[edgesize], *edge2 = new int[edgesize], *edge3 = new int[edgesize];
   int** facenodes = new int*[edgesize];
   for (i = 0; i < edgesize; ++i)
   {// first index is row from bottom, second index is column from left
      facenodes[i] = new int[edgesize - i];
   }
   
   //
   // Loop through tiles
   //
   for (i = 0; i < numTiles; i++) {
      //
      // Get the tile's nodes
      //
      int node1, node2, node3;
      initTopo->getTile(i, node1, node2, node3);
      
      //convention for visualization: node 1 is bottom left, node 2 is bottom right, node 3 is top
      //tile generation also follows this convention, with the consequence that tile orientation is preserved

      //
      // Get or generate edge nodes
      //
      getEdge(node1, node2, edge1);//bottom edge, left to right
      getEdge(node1, node3, edge2);//left edge, bottom to top
      getEdge(node2, node3, edge3);//right edge, bottom to top
      
      //
      // Get coords for interpolating
      //
      coordinateFile->getCoordinate(node1, coord1);//only 20 times, doesn't matter if we use a local coord aray
      coordinateFile->getCoordinate(node2, coord2);
      coordinateFile->getCoordinate(node3, coord3);
      
      //
      // Copy edge nodes into the face array
      //
      for (j = 0; j < edgesize; ++j)
      {
         facenodes[0][j] = edge1[j];
         facenodes[j][0] = edge2[j];
         facenodes[j][numberOfDivisions - j] = edge3[j];
      }
      
      //
      // Time intensive code start (assuming large numberOfDivisions)
      //
      
      //
      // Generate interior coordinates
      //
      for (j = 1; j < intRows; ++j)
      {
         intCols = numberOfDivisions - j;
         for (k = 1; k < intCols; ++k)
         {
            interp3(coord1, coord2, coord3, j, k, tempcoord);
            coordinateFile->setCoordinate(curNodes, tempcoord);
            facenodes[j][k] = curNodes;
            ++curNodes;
         }
      }
      
      //
      // Generate tiles
      //
      for (j = 0; j < numberOfDivisions; ++j)
      {//lets inner loop short circuit on top row
         for (k = 0; k < numberOfDivisions - j - 1; ++k)
         {//pairs for trapezoidal pieces
            topologyFile->setTile(curTiles, facenodes[j][k], facenodes[j][k + 1], facenodes[j + 1][k]);
            topologyFile->setTile(curTiles + 1, facenodes[j + 1][k], facenodes[j][k + 1], facenodes[j + 1][k + 1]);
            curTiles += 2;
         }//and one more
         topologyFile->setTile(curTiles, facenodes[j][numberOfDivisions - j - 1], facenodes[j][numberOfDivisions - j], facenodes[j + 1][numberOfDivisions - j - 1]);
         ++curTiles;
      }
      
      //
      // Time intensive code end
      //
   }
   //
   // End triangle division code
   //

   this->makeSphere();

   this->surface->convertToSphereWithRadius(100.0);
   delete initTopo;
   for (i = 0; i < edgesize; ++i) delete[] facenodes[i];
   delete[] facenodes;
   delete[] edge1;
   delete[] edge2;
   delete[] edge3;
}

/**
 * Make the surface a sphere.
 */
void
BrainModelSurfacePolyhedronNew::makeSphere()
{
   this->surface->orientTilesOutward(surface->getSurfaceType());
   this->surface->computeNormals();
   this->surface->translateToCenterOfMass();
   this->surface->convertToSphereWithRadius(10000.0);
}

void BrainModelSurfacePolyhedronNew::getEdge(int node1, int node2, int* out)
{//function is called relatively few times, assuming large numberOfDivisions
   bool reverse = false;
   int i, index, edgesize = numberOfDivisions + 1;
   if (node1 > node2)
   {
      reverse = true;
      i = node1;
      node1 = node2;
      node2 = i;
   }
   bool found = false;
   for (i = 0; i < (int)edgenodes[node1].size(); ++i)
   {
      if (edgenodes[node1][i][numberOfDivisions] == node2)
      {
         found = true;
         index = i;
         break;
      }
   }
   if (!found)
   {
      float tempcoord[3], coord1[3], coord2[3], coord3[3] = {0.0f, 0.0f, 0.0f};
      coordinateFile->getCoordinate(node1, coord1);//this function isn't called a lot, so who cares if it uses a local coord array
      coordinateFile->getCoordinate(node2, coord2);
      std::vector<int> tempvec;
      tempvec.resize(edgesize);
      tempvec[0] = node1;
      tempvec[numberOfDivisions] = node2;
      for (i = 1; i < numberOfDivisions; ++i)
      {
         interp3(coord1, coord2, coord3, 0, i, tempcoord);//use 0 as dummy node, with row 0 it is unused
         tempvec[i] = curNodes;
         coordinateFile->setCoordinate(curNodes, tempcoord);
         ++curNodes;
      }
      index = edgenodes[node1].size();
      edgenodes[node1].push_back(tempvec);
   }
   if (reverse)
   {
      for (i = 0; i < edgesize; ++i)
      {
         out[i] = edgenodes[node1][index][edgesize - i - 1];
      }
   } else {
      for (i = 0; i < edgesize; ++i)
      {
         out[i] = edgenodes[node1][index][i];
      }
   }
}

void BrainModelSurfacePolyhedronNew::interp3(float coord1[3], float coord2[3], float coord3[3], int row, int col, float out[3])
{//this is the function to change if you want different spacing
   float weight2 = ((float)col) / numberOfDivisions;//start with flat interpolation weights
   float weight3 = ((float)row) / numberOfDivisions;
   float weight1 = 1.0f - weight2 - weight3;
   //polynomial for each weight - should map 0 to 0 and 1 to 1
   const float quintweight = 0.0537206f;//WEIGHTS TUNED FOR ICOSAHEDRON VIA GENETIC ALGORITHM, ADJUST FOR OTHER INITIAL POLYGONS
   const float quartweight = -0.174466f;//this polynomial should be highly dependent on size of the triangle being interpolated
   const float cubeweight = 0.292547f;
   const float quadweight = -0.456351f;
   const float linweight = 1.0f - quintweight - quartweight - cubeweight - quadweight;//make sure it maps 0 to 0 and 1 to 1
   weight1 = ((((quintweight * weight1 + quartweight) * weight1 + cubeweight) * weight1 + quadweight) * weight1 + linweight) * weight1;//quintic approximation of great arc equal area weight transformation function
   weight2 = ((((quintweight * weight2 + quartweight) * weight2 + cubeweight) * weight2 + quadweight) * weight2 + linweight) * weight2;
   weight3 = ((((quintweight * weight3 + quartweight) * weight3 + cubeweight) * weight3 + quadweight) * weight3 + linweight) * weight3;//three weights no longer sum to 1, but thats ok
   out[0] = coord1[0] * weight1 + coord2[0] * weight2 + coord3[0] * weight3;
   out[1] = coord1[1] * weight1 + coord2[1] * weight2 + coord3[1] * weight3;
   out[2] = coord1[2] * weight1 + coord2[2] * weight2 + coord3[2] * weight3;
}