-
Notifications
You must be signed in to change notification settings - Fork 61
/
AlgorithmCiftiSeparate.cxx
745 lines (723 loc) · 34.3 KB
/
AlgorithmCiftiSeparate.cxx
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
/*LICENSE_START*/
/*
* Copyright (C) 2014 Washington University School of Medicine
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*LICENSE_END*/
#include "AlgorithmCiftiSeparate.h"
#include "AlgorithmException.h"
#include "CaretLogger.h"
#include "CaretPointer.h"
#include "CiftiFile.h"
#include "GiftiLabelTable.h"
#include "LabelFile.h"
#include "MetricFile.h"
#include "Vector3D.h"
#include "VolumeFile.h"
#include <cstdlib>
#include <map>
using namespace caret;
using namespace std;
AString AlgorithmCiftiSeparate::getCommandSwitch()
{
return "-cifti-separate";
}
AString AlgorithmCiftiSeparate::getShortDescription()
{
return "WRITE A CIFTI STRUCTURE AS METRIC, LABEL OR VOLUME";
}
OperationParameters* AlgorithmCiftiSeparate::getParameters()
{
OperationParameters* ret = new OperationParameters();
ret->addCiftiParameter(1, "cifti-in", "the cifti to separate a component of");
ret->addStringParameter(2, "direction", "which direction to separate into components, ROW or COLUMN");
ParameterComponent* labelOpt = ret->createRepeatableParameter(3, "-label", "separate a surface model into a surface label file");
labelOpt->addStringParameter(1, "structure", "the structure to output");
labelOpt->addLabelOutputParameter(2, "label-out", "the output label file");
OptionalParameter* labelRoiOpt = labelOpt->createOptionalParameter(3, "-roi", "also output the roi of which vertices have data");
labelRoiOpt->addMetricOutputParameter(1, "roi-out", "the roi output metric");
ParameterComponent* metricOpt = ret->createRepeatableParameter(4, "-metric", "separate a surface model into a metric file");
metricOpt->addStringParameter(1, "structure", "the structure to output");
metricOpt->addMetricOutputParameter(2, "metric-out", "the output metric");
OptionalParameter* metricRoiOpt = metricOpt->createOptionalParameter(3, "-roi", "also output the roi of which vertices have data");
metricRoiOpt->addMetricOutputParameter(1, "roi-out", "the roi output metric");
ParameterComponent* volumeOpt = ret->createRepeatableParameter(5, "-volume", "separate a volume structure into a volume file");
volumeOpt->addStringParameter(1, "structure", "the structure to output");
volumeOpt->addVolumeOutputParameter(2, "volume-out", "the output volume");
OptionalParameter* volumeRoiOpt = volumeOpt->createOptionalParameter(3, "-roi", "also output the roi of which voxels have data");
volumeRoiOpt->addVolumeOutputParameter(1, "roi-out", "the roi output volume");
volumeOpt->createOptionalParameter(4, "-crop", "crop volume to the size of the component rather than using the original volume size");
OptionalParameter* volumeAllOpt = ret->createOptionalParameter(6, "-volume-all", "separate all volume structures into a volume file");
volumeAllOpt->addVolumeOutputParameter(1, "volume-out", "the output volume");
OptionalParameter* volumeAllRoiOpt = volumeAllOpt->createOptionalParameter(2, "-roi", "also output the roi of which voxels have data");
volumeAllRoiOpt->addVolumeOutputParameter(1, "roi-out", "the roi output volume");
OptionalParameter* volumeAllLabelOpt = volumeAllOpt->createOptionalParameter(4, "-label", "output a volume label file indicating the location of structures");
volumeAllLabelOpt->addVolumeOutputParameter(1, "label-out", "the label output volume");
volumeAllOpt->createOptionalParameter(3, "-crop", "crop volume to the size of the data rather than using the original volume size");
AString helpText = AString("For dtseries, dscalar, and dlabel, use COLUMN for <direction>, and if you have a symmetric dconn, COLUMN is more efficient.\n\n") +
"You must specify at least one of -metric, -volume-all, -volume, or -label for this command to do anything. " +
"Output volumes will spatially line up with their original positions, whether or not they are cropped. " +
"Volume files produced by separating a dlabel file, or from the -label suboption of -volume-all, will be label volumes, see -volume-help.\n\n" +
"For each <structure> argument, use one of the following strings:\n";
vector<StructureEnum::Enum> myStructureEnums;
StructureEnum::getAllEnums(myStructureEnums);
for (int i = 0; i < (int)myStructureEnums.size(); ++i)
{
helpText += "\n" + StructureEnum::toName(myStructureEnums[i]);
}
ret->setHelpText(helpText);
return ret;
}
void AlgorithmCiftiSeparate::useParameters(OperationParameters* myParams, ProgressObject* /*myProgObj*/)
{//ignore the progress object for now, and allow specifying multiple options at once
CiftiFile* ciftiIn = myParams->getCifti(1);
AString dirName = myParams->getString(2);
int myDir;
if (dirName == "ROW")
{
myDir = CiftiXML::ALONG_ROW;
} else if (dirName == "COLUMN") {
myDir = CiftiXML::ALONG_COLUMN;
} else {
throw AlgorithmException("incorrect string for direction, use ROW or COLUMN");
}
bool outputRequested = false;
const vector<ParameterComponent*>& labelInstances = myParams->getRepeatableParameterInstances(3);
for (int i = 0; i < (int)labelInstances.size(); ++i)
{
outputRequested = true;
AString structName = labelInstances[i]->getString(1);
bool ok = false;
StructureEnum::Enum myStruct = StructureEnum::fromName(structName, &ok);
if (!ok)
{
throw AlgorithmException("unrecognized structure type: '" + structName + "'");
}
LabelFile* labelOut = labelInstances[i]->getOutputLabel(2);
MetricFile* roiOut = NULL;
OptionalParameter* labelRoiOpt = labelInstances[i]->getOptionalParameter(3);
if (labelRoiOpt->m_present)
{
roiOut = labelRoiOpt->getOutputMetric(1);
}
AlgorithmCiftiSeparate(NULL, ciftiIn, myDir, myStruct, labelOut, roiOut);
}
const vector<ParameterComponent*>& metricInstances = myParams->getRepeatableParameterInstances(4);
for (int i = 0; i < (int)metricInstances.size(); ++i)
{
outputRequested = true;
AString structName = metricInstances[i]->getString(1);
bool ok = false;
StructureEnum::Enum myStruct = StructureEnum::fromName(structName, &ok);
if (!ok)
{
throw AlgorithmException("unrecognized structure type: '" + structName + "'");
}
MetricFile* metricOut = metricInstances[i]->getOutputMetric(2);
MetricFile* roiOut = NULL;
OptionalParameter* metricRoiOpt = metricInstances[i]->getOptionalParameter(3);
if (metricRoiOpt->m_present)
{
roiOut = metricRoiOpt->getOutputMetric(1);
}
AlgorithmCiftiSeparate(NULL, ciftiIn, myDir, myStruct, metricOut, roiOut);
}
const vector<ParameterComponent*>& volumeInstances = myParams->getRepeatableParameterInstances(5);
for (int i = 0; i < (int)volumeInstances.size(); ++i)
{
outputRequested = true;
AString structName = volumeInstances[i]->getString(1);
bool ok = false;
StructureEnum::Enum myStruct = StructureEnum::fromName(structName, &ok);
if (!ok)
{
throw AlgorithmException("unrecognized structure type: '" + structName + "'");
}
VolumeFile* volOut = volumeInstances[i]->getOutputVolume(2);
VolumeFile* roiOut = NULL;
OptionalParameter* volumeRoiOpt = volumeInstances[i]->getOptionalParameter(3);
if (volumeRoiOpt->m_present)
{
roiOut = volumeRoiOpt->getOutputVolume(1);
}
bool cropVol = volumeInstances[i]->getOptionalParameter(4)->m_present;
int64_t offset[3];
AlgorithmCiftiSeparate(NULL, ciftiIn, myDir, myStruct, volOut, offset, roiOut, cropVol);
}
OptionalParameter* volumeAllOpt = myParams->getOptionalParameter(6);
if (volumeAllOpt->m_present)
{
outputRequested = true;
VolumeFile* volOut = volumeAllOpt->getOutputVolume(1);
VolumeFile* roiOut = NULL;
OptionalParameter* volumeAllRoiOpt = volumeAllOpt->getOptionalParameter(2);
if (volumeAllRoiOpt->m_present)
{
roiOut = volumeAllRoiOpt->getOutputVolume(1);
}
bool cropVol = volumeAllOpt->getOptionalParameter(3)->m_present;
VolumeFile* labelOut = NULL;
OptionalParameter* volumeAllLabelOpt = volumeAllOpt->getOptionalParameter(4);
if (volumeAllLabelOpt->m_present)
{
labelOut = volumeAllLabelOpt->getOutputVolume(1);
}
int64_t offset[3];
AlgorithmCiftiSeparate(NULL, ciftiIn, myDir, volOut, offset, roiOut, cropVol, labelOut);
}
if (!outputRequested)
{
CaretLogWarning("no output requested from -cifti-separate, command will do nothing");
}
}
AlgorithmCiftiSeparate::AlgorithmCiftiSeparate(ProgressObject* myProgObj, const CiftiFile* ciftiIn, const int& myDir,
const StructureEnum::Enum& myStruct, MetricFile* metricOut, MetricFile* roiOut) : AbstractAlgorithm(myProgObj)
{
LevelProgress myProgress(myProgObj);
const CiftiXML& myXML = ciftiIn->getCiftiXML();
if (myXML.getNumberOfDimensions() != 2) throw AlgorithmException("cifti separate only supported on 2D cifti");
if (myDir >= myXML.getNumberOfDimensions() || myDir < 0) throw AlgorithmException("direction invalid for input cifti");
if (myXML.getMappingType(myDir) != CiftiMappingType::BRAIN_MODELS) throw AlgorithmException("specified direction does not contain brain models");
if (myXML.getMappingType(1 - myDir) == CiftiMappingType::LABELS) CaretLogWarning("creating a metric file from cifti label data");
const CiftiBrainModelsMap& myBrainModelsMap = myXML.getBrainModelsMap(myDir);
if (!myBrainModelsMap.hasSurfaceData(myStruct)) throw AlgorithmException("specified file and direction does not contain the requested surface structure '" + StructureEnum::toName(myStruct) + "'");
vector<CiftiBrainModelsMap::SurfaceMap> myMap = myBrainModelsMap.getSurfaceMap(myStruct);
int rowSize = ciftiIn->getNumberOfColumns(), colSize = ciftiIn->getNumberOfRows();
if (myDir == CiftiXML::ALONG_COLUMN)
{
int64_t numNodes = myBrainModelsMap.getSurfaceNumberOfNodes(myStruct);
metricOut->setNumberOfNodesAndColumns(numNodes, rowSize);
metricOut->setStructure(myStruct);
const CiftiMappingType& myNamesMap = *(myXML.getMap(1 - myDir));
for (int j = 0; j < rowSize; ++j)
{
metricOut->setMapName(j, myNamesMap.getIndexName(j));
}
if (roiOut != NULL)
{
roiOut->setNumberOfNodesAndColumns(numNodes, 1);
roiOut->setStructure(myStruct);
}
int mapSize = (int)myMap.size();
CaretArray<float> rowScratch(rowSize);
CaretArray<float> nodeUsed(numNodes, 0.0f);
for (int i = 0; i < mapSize; ++i)
{
ciftiIn->getRow(rowScratch, myMap[i].m_ciftiIndex);
nodeUsed[myMap[i].m_surfaceNode] = 1.0f;
for (int j = 0; j < rowSize; ++j)
{
metricOut->setValue(myMap[i].m_surfaceNode, j, rowScratch[j]);
}
}
if (roiOut != NULL)
{
roiOut->setValuesForColumn(0, nodeUsed);
}
for (int i = 0; i < numNodes; ++i)//zero unused columns
{
if (nodeUsed[i] == 0.0f)
{
for (int j = 0; j < rowSize; ++j)
{
metricOut->setValue(i, j, 0.0f);
}
}
}
} else {
int64_t numNodes = myBrainModelsMap.getSurfaceNumberOfNodes(myStruct);
metricOut->setNumberOfNodesAndColumns(numNodes, colSize);
metricOut->setStructure(myStruct);
const CiftiMappingType& myNamesMap = *(myXML.getMap(1 - myDir));
for (int j = 0; j < colSize; ++j)
{
metricOut->setMapName(j, myNamesMap.getIndexName(j));
}
if (roiOut != NULL)
{
roiOut->setNumberOfNodesAndColumns(numNodes, 1);
roiOut->setStructure(myStruct);
}
int mapSize = (int)myMap.size();
CaretArray<float> rowScratch(rowSize), metricScratch(numNodes, 0.0f);
if (roiOut != NULL)
{
CaretArray<float> nodeUsed(numNodes, 0.0f);
for (int i = 0; i < mapSize; ++i)
{
nodeUsed[myMap[i].m_surfaceNode] = 1.0f;
}
roiOut->setValuesForColumn(0, nodeUsed);
}
for (int i = 0; i < colSize; ++i)
{
ciftiIn->getRow(rowScratch, i);
for (int j = 0; j < mapSize; ++j)
{
metricScratch[myMap[j].m_surfaceNode] = rowScratch[myMap[j].m_ciftiIndex];
}
metricOut->setValuesForColumn(i, metricScratch);
}
}
}
AlgorithmCiftiSeparate::AlgorithmCiftiSeparate(ProgressObject* myProgObj, const CiftiFile* ciftiIn, const int& myDir,
const StructureEnum::Enum& myStruct, LabelFile* labelOut, MetricFile* roiOut) : AbstractAlgorithm(myProgObj)
{
LevelProgress myProgress(myProgObj);
const CiftiXML& myXML = ciftiIn->getCiftiXML();
if (myXML.getNumberOfDimensions() != 2) throw AlgorithmException("cifti separate only supported on 2D cifti");
if (myDir >= myXML.getNumberOfDimensions() || myDir < 0) throw AlgorithmException("direction invalid for input cifti");
if (myXML.getMappingType(myDir) != CiftiMappingType::BRAIN_MODELS) throw AlgorithmException("specified direction does not contain brain models");
if (myXML.getMappingType(1 - myDir) != CiftiMappingType::LABELS) throw AlgorithmException("label separate requested on non-label cifti");
const CiftiBrainModelsMap& myBrainModelsMap = myXML.getBrainModelsMap(myDir);
const CiftiLabelsMap& myLabelsMap = myXML.getLabelsMap(1 - myDir);
if (!myBrainModelsMap.hasSurfaceData(myStruct)) throw AlgorithmException("specified file and direction does not contain the requested surface structure '" + StructureEnum::toName(myStruct) + "'");
vector<CiftiBrainModelsMap::SurfaceMap> myMap = myBrainModelsMap.getSurfaceMap(myStruct);
int64_t rowSize = ciftiIn->getNumberOfColumns(), colSize = ciftiIn->getNumberOfRows();
if (myDir == CiftiXML::ALONG_COLUMN)
{
int64_t numNodes = myBrainModelsMap.getSurfaceNumberOfNodes(myStruct);
labelOut->setNumberOfNodesAndColumns(numNodes, rowSize);
labelOut->setStructure(myStruct);
const CiftiMappingType& myNamesMap = *(myXML.getMap(1 - myDir));
for (int j = 0; j < rowSize; ++j)
{
labelOut->setMapName(j, myNamesMap.getIndexName(j));
}
if (roiOut != NULL)
{
roiOut->setNumberOfNodesAndColumns(numNodes, 1);
roiOut->setStructure(myStruct);
}
int64_t mapSize = (int64_t)myMap.size();
CaretArray<float> rowScratch(rowSize);
CaretArray<float> nodeUsed(numNodes, 0.0f);
GiftiLabelTable myTable;
map<int32_t, int32_t> cumulativeRemap;
for (int64_t i = 0; i < rowSize; ++i)
{
map<int32_t, int32_t> thisRemap = myTable.append(*(myLabelsMap.getMapLabelTable(i)));
cumulativeRemap.insert(thisRemap.begin(), thisRemap.end());
}
for (int64_t i = 0; i < mapSize; ++i)
{
ciftiIn->getRow(rowScratch, myMap[i].m_ciftiIndex);
nodeUsed[myMap[i].m_surfaceNode] = 1.0f;
for (int j = 0; j < rowSize; ++j)
{
int32_t inVal = (int32_t)floor(rowScratch[j] + 0.5f);
map<int32_t, int32_t>::const_iterator iter = cumulativeRemap.find(inVal);
if (iter == cumulativeRemap.end())
{
labelOut->setLabelKey(myMap[i].m_surfaceNode, j, inVal);
} else {
labelOut->setLabelKey(myMap[i].m_surfaceNode, j, iter->second);
}
}
}
*(labelOut->getLabelTable()) = myTable;
int32_t unusedLabel = myTable.getUnassignedLabelKey();
if (roiOut != NULL)
{
roiOut->setValuesForColumn(0, nodeUsed);
}
for (int i = 0; i < numNodes; ++i)//set unused columns to unassigned
{
if (nodeUsed[i] == 0.0f)
{
for (int j = 0; j < rowSize; ++j)
{
labelOut->setLabelKey(i, j, unusedLabel);
}
}
}
} else {
int64_t numNodes = myBrainModelsMap.getSurfaceNumberOfNodes(myStruct);
labelOut->setNumberOfNodesAndColumns(numNodes, colSize);
labelOut->setStructure(myStruct);
const CiftiMappingType& myNamesMap = *(myXML.getMap(1 - myDir));
for (int j = 0; j < colSize; ++j)
{
labelOut->setMapName(j, myNamesMap.getIndexName(j));
}
if (roiOut != NULL)
{
roiOut->setNumberOfNodesAndColumns(numNodes, 1);
roiOut->setStructure(myStruct);
}
int64_t mapSize = (int)myMap.size();
if (roiOut != NULL)
{
CaretArray<float> nodeUsed(numNodes, 0.0f);
for (int i = 0; i < mapSize; ++i)
{
nodeUsed[myMap[i].m_surfaceNode] = 1.0f;
}
roiOut->setValuesForColumn(0, nodeUsed);
}
CaretArray<float> rowScratch(rowSize);
CaretArray<int> nodeUsed(numNodes, 0);
for (int64_t j = 0; j < mapSize; ++j)
{
nodeUsed[myMap[j].m_surfaceNode] = 1;
}
GiftiLabelTable myTable;
map<int32_t, int32_t> cumulativeRemap;
for (int64_t i = 0; i < colSize; ++i)
{
map<int32_t, int32_t> thisRemap = myTable.append(*(myLabelsMap.getMapLabelTable(i)));
cumulativeRemap.insert(thisRemap.begin(), thisRemap.end());
}
*(labelOut->getLabelTable()) = myTable;
int32_t unusedLabel = myTable.getUnassignedLabelKey();
for (int64_t i = 0; i < colSize; ++i)
{
ciftiIn->getRow(rowScratch, i);
for (int64_t j = 0; j < mapSize; ++j)
{
int32_t inVal = (int32_t)floor(rowScratch[j] + 0.5f);
map<int32_t, int32_t>::const_iterator iter = cumulativeRemap.find(inVal);
if (iter == cumulativeRemap.end())
{
labelOut->setLabelKey(myMap[j].m_surfaceNode, i, inVal);
} else {
labelOut->setLabelKey(myMap[j].m_surfaceNode, i, iter->second);
}
}
for (int64_t j = 0; j < numNodes; ++j)//set unused columns to unassigned
{
if (nodeUsed[j] == 0)
{
labelOut->setLabelKey(j, i, unusedLabel);
}
}
}
}
}
AlgorithmCiftiSeparate::AlgorithmCiftiSeparate(ProgressObject* myProgObj, const CiftiFile* ciftiIn, const int& myDir,
const StructureEnum::Enum& myStruct, VolumeFile* volOut, int64_t offsetOut[3],
VolumeFile* roiOut, const bool& cropVol) : AbstractAlgorithm(myProgObj)
{
LevelProgress myProgress(myProgObj);
const CiftiXML& myXML = ciftiIn->getCiftiXML();
if (myXML.getNumberOfDimensions() != 2) throw AlgorithmException("cifti separate only supported on 2D cifti");
if (myDir >= ciftiIn->getCiftiXML().getNumberOfDimensions() || myDir < 0) throw AlgorithmException("direction invalid for input cifti");
if (myXML.getMappingType(myDir) != CiftiMappingType::BRAIN_MODELS) throw AlgorithmException("specified direction does not contain brain models");
int rowSize = ciftiIn->getNumberOfColumns(), colSize = ciftiIn->getNumberOfRows();
const CiftiBrainModelsMap& myBrainMap = myXML.getBrainModelsMap(myDir);
const int64_t* myDims = myBrainMap.getVolumeSpace().getDims();
vector<vector<float> > mySform = myBrainMap.getVolumeSpace().getSform();
if (!myBrainMap.hasVolumeData(myStruct)) throw AlgorithmException("specified file and direction does not contain the requested volume structure");
vector<CiftiBrainModelsMap::VolumeMap> myMap = myBrainMap.getVolumeStructureMap(myStruct);
vector<int64_t> newdims;
int64_t numVoxels = (int64_t)myMap.size();
if (cropVol)
{
newdims.resize(3);
getCroppedVolSpace(ciftiIn, myDir, myStruct, newdims.data(), mySform, offsetOut);
} else {
newdims.push_back(myDims[0]);
newdims.push_back(myDims[1]);
newdims.push_back(myDims[2]);
offsetOut[0] = 0;
offsetOut[1] = 0;
offsetOut[2] = 0;
}
if (roiOut != NULL)
{
roiOut->reinitialize(newdims, mySform);
roiOut->setValueAllVoxels(0.0f);
}
CaretArray<float> rowScratch(rowSize);
if (myDir == CiftiXML::ALONG_COLUMN)
{
if (rowSize > 1) newdims.push_back(rowSize);
volOut->reinitialize(newdims, mySform);
volOut->setValueAllVoxels(0.0f);
const CiftiMappingType& myNamesMap = *(myXML.getMap(1 - myDir));
for (int j = 0; j < rowSize; ++j)
{
volOut->setMapName(j, myNamesMap.getIndexName(j));
}
if (myXML.getMappingType(CiftiXML::ALONG_ROW) == CiftiMappingType::LABELS)
{
const CiftiLabelsMap& myLabelsMap = myXML.getLabelsMap(CiftiXML::ALONG_ROW);
volOut->setType(SubvolumeAttributes::LABEL);
for (int j = 0; j < rowSize; ++j)
{
*(volOut->getMapLabelTable(j)) = *(myLabelsMap.getMapLabelTable(j));
}
}
for (int64_t i = 0; i < numVoxels; ++i)
{
int64_t thisvoxel[3] = { myMap[i].m_ijk[0] - offsetOut[0], myMap[i].m_ijk[1] - offsetOut[1], myMap[i].m_ijk[2] - offsetOut[2] };
if (roiOut != NULL)
{
roiOut->setValue(1.0f, thisvoxel);
}
ciftiIn->getRow(rowScratch, myMap[i].m_ciftiIndex);
for (int j = 0; j < rowSize; ++j)
{
volOut->setValue(rowScratch[j], thisvoxel, j);
}
}
} else {
if (colSize > 1) newdims.push_back(colSize);
volOut->reinitialize(newdims, mySform);
volOut->setValueAllVoxels(0.0f);
const CiftiMappingType& myNamesMap = *(myXML.getMap(1 - myDir));
for (int j = 0; j < colSize; ++j)
{
volOut->setMapName(j, myNamesMap.getIndexName(j));
}
if (myXML.getMappingType(CiftiXML::ALONG_COLUMN) == CiftiMappingType::LABELS)
{
const CiftiLabelsMap& myLabelsMap = myXML.getLabelsMap(CiftiXML::ALONG_COLUMN);
volOut->setType(SubvolumeAttributes::LABEL);
for (int j = 0; j < colSize; ++j)
{
*(volOut->getMapLabelTable(j)) = *(myLabelsMap.getMapLabelTable(j));
}
}
for (int64_t i = 0; i < colSize; ++i)
{
ciftiIn->getRow(rowScratch, i);
for (int64_t j = 0; j < numVoxels; ++j)
{
int64_t thisvoxel[3] = { myMap[j].m_ijk[0] - offsetOut[0], myMap[j].m_ijk[1] - offsetOut[1], myMap[j].m_ijk[2] - offsetOut[2] };
if (i == 0 && roiOut != NULL)
{
roiOut->setValue(1.0f, thisvoxel);
}
volOut->setValue(rowScratch[myMap[j].m_ciftiIndex], thisvoxel, i);
}
}
}
}
AlgorithmCiftiSeparate::AlgorithmCiftiSeparate(ProgressObject* myProgObj, const CiftiFile* ciftiIn, const int& myDir, VolumeFile* volOut, int64_t offsetOut[3],
VolumeFile* roiOut, const bool& cropVol, VolumeFile* labelOut): AbstractAlgorithm(myProgObj)
{
LevelProgress myProgress(myProgObj);
const CiftiXML& myXML = ciftiIn->getCiftiXML();
if (myXML.getNumberOfDimensions() != 2) throw AlgorithmException("cifti separate only supported on 2D cifti");
if (myDir >= ciftiIn->getCiftiXML().getNumberOfDimensions() || myDir < 0) throw AlgorithmException("direction invalid for input cifti");
if (myXML.getMappingType(myDir) != CiftiMappingType::BRAIN_MODELS) throw AlgorithmException("specified direction does not contain brain models");
int rowSize = ciftiIn->getNumberOfColumns(), colSize = ciftiIn->getNumberOfRows();
const CiftiBrainModelsMap& myBrainMap = myXML.getBrainModelsMap(myDir);
if (!myBrainMap.hasVolumeData()) throw AlgorithmException("specified file and direction does not contain any volume data");
const int64_t* myDims = myBrainMap.getVolumeSpace().getDims();
vector<vector<float> > mySform = myBrainMap.getVolumeSpace().getSform();
vector<int64_t> newdims;
if (cropVol)
{
newdims.resize(3);
getCroppedVolSpaceAll(ciftiIn, myDir, newdims.data(), mySform, offsetOut);
} else {
newdims.push_back(myDims[0]);
newdims.push_back(myDims[1]);
newdims.push_back(myDims[2]);
offsetOut[0] = 0;
offsetOut[1] = 0;
offsetOut[2] = 0;
}
if (labelOut != NULL)
{
labelOut->reinitialize(newdims, mySform, 1, SubvolumeAttributes::LABEL);
labelOut->setValueAllVoxels(0.0f);//unlabeled key defaults to 0
vector<StructureEnum::Enum> volStructs = myBrainMap.getVolumeStructureList();
GiftiLabelTable structureTable;
for (int i = 0; i < (int)volStructs.size(); ++i)
{
const int32_t structKey = structureTable.addLabel(StructureEnum::toName(volStructs[i]), rand() & 255, rand() & 255, rand() & 255, 255);
const vector<int64_t>& voxelList = myBrainMap.getVoxelList(volStructs[i]);
int64_t structVoxels = (int64_t)voxelList.size();
for (int64_t j = 0; j < structVoxels; j += 3)
{
labelOut->setValue(structKey, voxelList[j] - offsetOut[0], voxelList[j + 1] - offsetOut[1], voxelList[j + 2] - offsetOut[2]);
}
}
*(labelOut->getMapLabelTable(0)) = structureTable;
}
if (roiOut != NULL)
{
roiOut->reinitialize(newdims, mySform);
roiOut->setValueAllVoxels(0.0f);
}
vector<CiftiBrainModelsMap::VolumeMap> myMap = myBrainMap.getFullVolumeMap();
int64_t numVoxels = (int64_t)myMap.size();
CaretArray<float> rowScratch(rowSize);
if (myDir == CiftiXML::ALONG_COLUMN)
{
if (rowSize > 1) newdims.push_back(rowSize);
volOut->reinitialize(newdims, mySform);
volOut->setValueAllVoxels(0.0f);
const CiftiMappingType& myNamesMap = *(myXML.getMap(1 - myDir));
for (int j = 0; j < rowSize; ++j)
{
volOut->setMapName(j, myNamesMap.getIndexName(j));
}
if (myXML.getMappingType(CiftiXML::ALONG_ROW) == CiftiMappingType::LABELS)
{
const CiftiLabelsMap& myLabelsMap = myXML.getLabelsMap(CiftiXML::ALONG_ROW);
volOut->setType(SubvolumeAttributes::LABEL);
for (int j = 0; j < rowSize; ++j)
{
*(volOut->getMapLabelTable(j)) = *(myLabelsMap.getMapLabelTable(j));
}
}
for (int64_t i = 0; i < numVoxels; ++i)
{
int64_t thisvoxel[3] = { myMap[i].m_ijk[0] - offsetOut[0], myMap[i].m_ijk[1] - offsetOut[1], myMap[i].m_ijk[2] - offsetOut[2] };
if (roiOut != NULL)
{
roiOut->setValue(1.0f, thisvoxel);
}
ciftiIn->getRow(rowScratch, myMap[i].m_ciftiIndex);
for (int j = 0; j < rowSize; ++j)
{
volOut->setValue(rowScratch[j], thisvoxel, j);
}
}
} else {
if (colSize > 1) newdims.push_back(colSize);
volOut->reinitialize(newdims, mySform);
volOut->setValueAllVoxels(0.0f);
const CiftiMappingType& myNamesMap = *(myXML.getMap(1 - myDir));
for (int j = 0; j < colSize; ++j)
{
volOut->setMapName(j, myNamesMap.getIndexName(j));
}
if (myXML.getMappingType(CiftiXML::ALONG_COLUMN) == CiftiMappingType::LABELS)
{
const CiftiLabelsMap& myLabelsMap = myXML.getLabelsMap(CiftiXML::ALONG_COLUMN);
volOut->setType(SubvolumeAttributes::LABEL);
for (int j = 0; j < colSize; ++j)
{
*(volOut->getMapLabelTable(j)) = *(myLabelsMap.getMapLabelTable(j));
}
}
for (int64_t i = 0; i < colSize; ++i)
{
ciftiIn->getRow(rowScratch, i);
for (int64_t j = 0; j < numVoxels; ++j)
{
int64_t thisvoxel[3] = { myMap[j].m_ijk[0] - offsetOut[0], myMap[j].m_ijk[1] - offsetOut[1], myMap[j].m_ijk[2] - offsetOut[2] };
if (i == 0 && roiOut != NULL)
{
roiOut->setValue(1.0f, thisvoxel);
}
volOut->setValue(rowScratch[myMap[j].m_ciftiIndex], thisvoxel, i);
}
}
}
}
void AlgorithmCiftiSeparate::getCroppedVolSpace(const CiftiFile* ciftiIn, const int& myDir, const StructureEnum::Enum& myStruct, int64_t dimsOut[3],
vector<vector<float> >& sformOut, int64_t offsetOut[3])
{
const CiftiXML& myXML = ciftiIn->getCiftiXML();
if (myXML.getMappingType(myDir) != CiftiMappingType::BRAIN_MODELS) throw AlgorithmException("specified direction does not contain brain models");
const CiftiBrainModelsMap& myBrainMap = myXML.getBrainModelsMap(myDir);
sformOut = myBrainMap.getVolumeSpace().getSform();
vector<CiftiBrainModelsMap::VolumeMap> myMap = myBrainMap.getVolumeStructureMap(myStruct);
int64_t numVoxels = (int64_t)myMap.size();
if (numVoxels > 0)
{//make a voxel bounding box to minimize memory usage
int extrema[6] = { myMap[0].m_ijk[0],
myMap[0].m_ijk[0],
myMap[0].m_ijk[1],
myMap[0].m_ijk[1],
myMap[0].m_ijk[2],
myMap[0].m_ijk[2]
};
for (int64_t i = 1; i < numVoxels; ++i)
{
if (myMap[i].m_ijk[0] < extrema[0]) extrema[0] = myMap[i].m_ijk[0];
if (myMap[i].m_ijk[0] > extrema[1]) extrema[1] = myMap[i].m_ijk[0];
if (myMap[i].m_ijk[1] < extrema[2]) extrema[2] = myMap[i].m_ijk[1];
if (myMap[i].m_ijk[1] > extrema[3]) extrema[3] = myMap[i].m_ijk[1];
if (myMap[i].m_ijk[2] < extrema[4]) extrema[4] = myMap[i].m_ijk[2];
if (myMap[i].m_ijk[2] > extrema[5]) extrema[5] = myMap[i].m_ijk[2];
}
dimsOut[0] = extrema[1] - extrema[0] + 1;
dimsOut[1] = extrema[3] - extrema[2] + 1;
dimsOut[2] = extrema[5] - extrema[4] + 1;
offsetOut[0] = extrema[0];
offsetOut[1] = extrema[2];
offsetOut[2] = extrema[4];
Vector3D ivec, jvec, kvec, shift;
ivec[0] = sformOut[0][0]; ivec[1] = sformOut[1][0]; ivec[2] = sformOut[2][0];
jvec[0] = sformOut[0][1]; jvec[1] = sformOut[1][1]; jvec[2] = sformOut[2][1];
kvec[0] = sformOut[0][2]; kvec[1] = sformOut[1][2]; kvec[2] = sformOut[2][2];
shift = offsetOut[0] * ivec + offsetOut[1] * jvec + offsetOut[2] * kvec;
sformOut[0][3] += shift[0];//fix the sform to align to the old position with the new dimensions
sformOut[1][3] += shift[1];
sformOut[2][3] += shift[2];
} else {
throw AlgorithmException("cropped volume requested, but no voxels exist in this structure");
}
}
void AlgorithmCiftiSeparate::getCroppedVolSpaceAll(const CiftiFile* ciftiIn, const int& myDir, int64_t dimsOut[3], vector<vector<float> >& sformOut, int64_t offsetOut[3])
{
const CiftiXML& myXML = ciftiIn->getCiftiXML();
if (myXML.getMappingType(myDir) != CiftiMappingType::BRAIN_MODELS) throw AlgorithmException("specified direction does not contain brain models");
const CiftiBrainModelsMap& myBrainMap = myXML.getBrainModelsMap(myDir);
sformOut = myBrainMap.getVolumeSpace().getSform();
vector<CiftiBrainModelsMap::VolumeMap> myMap = myBrainMap.getFullVolumeMap();
int64_t numVoxels = (int64_t)myMap.size();
if (numVoxels > 0)
{//make a voxel bounding box to minimize memory usage
int extrema[6] = { myMap[0].m_ijk[0],
myMap[0].m_ijk[0],
myMap[0].m_ijk[1],
myMap[0].m_ijk[1],
myMap[0].m_ijk[2],
myMap[0].m_ijk[2]
};
for (int64_t i = 1; i < numVoxels; ++i)
{
if (myMap[i].m_ijk[0] < extrema[0]) extrema[0] = myMap[i].m_ijk[0];
if (myMap[i].m_ijk[0] > extrema[1]) extrema[1] = myMap[i].m_ijk[0];
if (myMap[i].m_ijk[1] < extrema[2]) extrema[2] = myMap[i].m_ijk[1];
if (myMap[i].m_ijk[1] > extrema[3]) extrema[3] = myMap[i].m_ijk[1];
if (myMap[i].m_ijk[2] < extrema[4]) extrema[4] = myMap[i].m_ijk[2];
if (myMap[i].m_ijk[2] > extrema[5]) extrema[5] = myMap[i].m_ijk[2];
}
dimsOut[0] = extrema[1] - extrema[0] + 1;
dimsOut[1] = extrema[3] - extrema[2] + 1;
dimsOut[2] = extrema[5] - extrema[4] + 1;
offsetOut[0] = extrema[0];
offsetOut[1] = extrema[2];
offsetOut[2] = extrema[4];
Vector3D ivec, jvec, kvec, shift;
ivec[0] = sformOut[0][0]; ivec[1] = sformOut[1][0]; ivec[2] = sformOut[2][0];
jvec[0] = sformOut[0][1]; jvec[1] = sformOut[1][1]; jvec[2] = sformOut[2][1];
kvec[0] = sformOut[0][2]; kvec[1] = sformOut[1][2]; kvec[2] = sformOut[2][2];
shift = offsetOut[0] * ivec + offsetOut[1] * jvec + offsetOut[2] * kvec;
sformOut[0][3] += shift[0];//fix the sform to align to the old position with the new dimensions
sformOut[1][3] += shift[1];
sformOut[2][3] += shift[2];
} else {
throw AlgorithmException("cropped volume requested, but no voxels exist in the specified direction");
}
}
float AlgorithmCiftiSeparate::getAlgorithmInternalWeight()
{
return 1.0f;//override this if needed, if the progress bar isn't smooth
}
float AlgorithmCiftiSeparate::getSubAlgorithmWeight()
{
return 0.0f;
}