File: itkProcessObject.cxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,256 kB
  • sloc: cpp: 557,600; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (2011 lines) | stat: -rw-r--r-- 48,495 bytes parent folder | download | duplicates (3)
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
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
/*=========================================================================
 *
 *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
 *
 *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
 *
 *  For complete copyright, license and disclaimer of warranty information
 *  please refer to the NOTICE file at the top of the ITK source tree.
 *
 *=========================================================================*/
#include "itkProcessObject.h"
#include "itkMutexLockHolder.h"

#include <stdio.h>
#include <sstream>
#include <algorithm>

namespace itk
{


namespace
{ // local namespace for managing globals
const size_t ITK_GLOBAL_INDEX_NAMES_NUMBER = 100;
const size_t ITK_GLOBAL_INDEX_NAMES_LENGTH = 4;
const char globalIndexNames[ITK_GLOBAL_INDEX_NAMES_NUMBER][ITK_GLOBAL_INDEX_NAMES_LENGTH] =
{ "_0", "_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"
};

}


ProcessObject
::ProcessObject() :
  m_Inputs(),
  m_Outputs(),
  m_CachedInputReleaseDataFlags(),
  m_RequiredInputNames()
{
/*
 * Instantiate object with no start, end, or progress methods.
 */

  m_NumberOfRequiredInputs = 0;
  m_NumberOfRequiredOutputs = 0;

  m_AbortGenerateData = false;
  m_Progress = 0.0f;
  m_Updating = false;

  DataObjectPointerMap::value_type p("Primary", DataObjectPointer() );
  m_IndexedInputs.push_back( m_Inputs.insert(p).first );
  m_IndexedOutputs.push_back( m_Outputs.insert(p).first );

  m_Threader = MultiThreaderType::New();
  m_NumberOfThreads = m_Threader->GetNumberOfThreads();

  m_ReleaseDataBeforeUpdateFlag = true;

}


DataObject::Pointer
ProcessObject
::MakeOutput( const DataObjectIdentifierType & name )
{
/*
 * This is a default implementation to make sure we have something.
 * Once all the subclasses of ProcessObject provide an appopriate
 * MakeOutput(), then ProcessObject::MakeOutput() can be made pure
 * virtual.
 */

  itkDebugMacro("MakeOutput(" << name << ")");
  if( this->IsIndexedOutputName(name) )
    {
    return this->MakeOutput( this->MakeIndexFromOutputName(name) );
    }
  return static_cast<DataObject *>(DataObject::New().GetPointer());
}


DataObject::Pointer
ProcessObject
::MakeOutput(DataObjectPointerArraySizeType)
{
  return static_cast<DataObject *>(DataObject::New().GetPointer());
}


ProcessObject
::~ProcessObject()
{
/*
 * Destructor for the ProcessObject class. We've got to
 * UnRegister() the use of any input classes.
 */

  // Tell each output that we are going away.  If other objects have a
  // reference to one of these outputs, the data object will not be deleted
  // when the process object is deleted.  However, the data object's source
  // will still point back to the now nonexistent process object if we do not
  // clean things up now.
  for ( DataObjectPointerMap::iterator it = m_Outputs.begin(); it != m_Outputs.end(); ++it )
    {
    if ( it->second )
      {
      // let the output know we no longer want to associate with the object
      it->second->DisconnectSource(this, it->first);
      // let go of our reference to the data object
      it->second = ITK_NULLPTR;
      }
    }
}


void
ProcessObject
::SetNumberOfIndexedInputs(DataObjectPointerArraySizeType num)
{
/*
 * Called by constructor to set up input array.
 */

  if( this->GetNumberOfIndexedInputs() != num )
    {
    if ( num < this->GetNumberOfIndexedInputs() )
      {
      // NB: The primary input must never be removed from the map, or
      // the indexed inputs array!

      // remove the extra inputs
      for( DataObjectPointerArraySizeType i=std::max<DataObjectPointerArraySizeType>(num, 1);
           i<this->GetNumberOfIndexedInputs(); ++i )
        {
        m_Inputs.erase( m_IndexedInputs[i]->first );
        }
      m_IndexedInputs.resize(std::max<DataObjectPointerArraySizeType>(num, 1) );


      if (num < 1 )
        {
        m_IndexedInputs[0]->second = ITK_NULLPTR;
        }
      }
    else
      {
      for ( DataObjectPointerArraySizeType i = m_IndexedInputs.size(); i < num; ++i)
        {
        DataObjectPointerMap::value_type p(this->MakeNameFromInputIndex( i ), DataObjectPointer() );
        // note: insert will not change value if it's already there.
        m_IndexedInputs.push_back ( m_Inputs.insert(p).first );
        }
      }

    this->Modified();
    }
}


ProcessObject::DataObjectPointerArraySizeType
ProcessObject
::GetNumberOfValidRequiredInputs() const
{
/*
 * Get the number of specified inputs
 */
  DataObjectPointerArraySizeType count = 0;
  for( DataObjectPointerArraySizeType i = 0; i < m_NumberOfRequiredInputs; i++ )
    {
    if( this->GetInput( i ) )
      {
      ++count;
      }
    }
  return count;
}


void
ProcessObject
::AddInput(DataObject *input)
{
/*
 * Adds an input to the first null position in the input list.
 * Expands the list memory if necessary
 */
  for ( unsigned idx = 0; idx < this->GetNumberOfIndexedInputs(); ++idx )
    {
    if ( !this->GetInput( idx ) )
      {
      this->SetNthInput( idx, input );
      return;
      }
    }
  this->SetNthInput( this->GetNumberOfIndexedInputs(), input );
}


void
ProcessObject
::RemoveInput(const DataObjectIdentifierType & key)
{
/*
 * Remove an input.
 */

  // if primary or required set to null
  if ( key ==  m_IndexedInputs[0]->first || this->IsRequiredInputName(key) )
    {
    this->SetInput(key, ITK_NULLPTR);
    return;
    }

  // set indexed input to null, remove if last
  for ( DataObjectPointerArraySizeType i = 1; i <  m_IndexedInputs.size(); ++i )
    {
    if ( m_IndexedInputs[i]->first == key )
      {
      this->SetNthInput(i, ITK_NULLPTR);
      if ( i == m_IndexedInputs.size() - 1 )
        {
        // remove the last indexed input
        this->SetNumberOfIndexedInputs( this->GetNumberOfIndexedInputs() -1 );
        }
      return;
      }
    }

  DataObjectPointerMap::iterator it = m_Inputs.find(key);

  if ( it != m_Inputs.end() )
    {
    m_Inputs.erase( it );
    this->Modified();
    }
}


void
ProcessObject
::RemoveInput(DataObjectPointerArraySizeType idx)
{
  if( idx < this->GetNumberOfIndexedInputs() )
    {
    this->RemoveInput( m_IndexedInputs[idx]->first );
    }
  else
    {
    this->RemoveInput( this->MakeNameFromInputIndex( idx ) );
    }
}


void
ProcessObject
::SetInput(const DataObjectIdentifierType & key, DataObject * input)
{
  if( key.empty() )
    {
    itkExceptionMacro("An empty string can't be used as an input identifier");
    }
  DataObjectPointerMap::iterator it = m_Inputs.find(key);
  if( it == m_Inputs.end() )
    {
    // this is a whole new entry
    m_Inputs[key] = input;
    this->Modified();
    }
  else if( it->second.GetPointer() != input )
    {
    // there is already an entry, but not with the right object
    it->second = input;
    this->Modified();
    }
  // the entry is already there - there is nothing to do
}


void
ProcessObject
::SetNthInput(DataObjectPointerArraySizeType idx, DataObject *input)
{
/* Set an Input of this filter. This method
 * does Register()/UnRegister() manually to
 * deal with the fact that smart pointers aren't
 * around to do the reference counting.
 */
  if ( idx >= this->GetNumberOfIndexedInputs() )
    {
    this->SetNumberOfIndexedInputs(idx + 1);
    }
  if ( m_IndexedInputs[idx]->second != input )
    {
    m_IndexedInputs[idx]->second = input;
    this->Modified();
    }
}


void
ProcessObject
::PushBackInput(const DataObject *input)
{
/*
 * Model a queue on the input list by providing a push back
 */

  this->SetNthInput( this->GetNumberOfIndexedInputs(), const_cast< DataObject * >( input ) );
}


void
ProcessObject
::PopBackInput()
{
/*
 * Model a stack on the input list by providing a pop back
 */

  if ( this->GetNumberOfIndexedInputs() > 0 )
    {
    this->SetNumberOfIndexedInputs( this->GetNumberOfIndexedInputs() - 1 );
    }
}


void
ProcessObject
::PushFrontInput(const DataObject *input)
{
  const DataObjectPointerArraySizeType nb = this->GetNumberOfIndexedInputs();
  for( DataObjectPointerArraySizeType i = nb; i > 0; i-- )
    {
    this->SetNthInput( i, this->GetInput(i-1) );
    }
  this->SetNthInput( 0, const_cast< DataObject * >( input ) );
}


void
ProcessObject
::PopFrontInput()
{
  DataObjectPointerArraySizeType nb = this->GetNumberOfIndexedInputs();
  if( nb > 0 )
    {
    for( DataObjectPointerArraySizeType i = 1; i < nb; i++ )
      {
      this->SetNthInput( i-1, this->GetInput( i ) );
      }
    this->SetNumberOfIndexedInputs( nb - 1 );
    }
}


void
ProcessObject
::RemoveOutput(const DataObjectIdentifierType & key)
{
  // if primary or required set to null
  if ( key == m_IndexedOutputs[0]->first )
    {
    this->SetOutput( key, ITK_NULLPTR );
    return;
    }

  // set indexed output to null, remove if last
  for ( DataObjectPointerArraySizeType i = 1; i <  m_IndexedOutputs.size(); ++i )
    {
    if ( m_IndexedOutputs[i]->first == key )
      {
      this->SetNthOutput(i, ITK_NULLPTR);
      if ( i == m_IndexedOutputs.size() - 1 )
        {
        // remove the last indexed input
        this->SetNumberOfIndexedOutputs( this->GetNumberOfIndexedOutputs() -1 );
        }
      return;
      }
    }


  DataObjectPointerMap::iterator it = m_Outputs.find(key);

  if ( it != m_Outputs.end() )
    {
    // let the output know we no longer want to associate with the object
    it->second->DisconnectSource( this, it->first );
    m_Outputs.erase( it );
    // let go of our reference to the data object
    this->Modified();
    }
}


void
ProcessObject
::RemoveOutput(DataObjectPointerArraySizeType idx)
{
  if( idx == this->GetNumberOfIndexedOutputs() - 1 )
    {
    // just remove the last indexed output
    this->SetNumberOfIndexedOutputs( this->GetNumberOfIndexedOutputs() -1 );
    }
  else
    {
    this->RemoveOutput( this->MakeNameFromOutputIndex( idx ) );
    }
}


void
ProcessObject
::SetOutput(const DataObjectIdentifierType & name, DataObject * output)
{
/*
  Set an Output of this filter. This method
 * does Register()/UnRegister() manually to
 * deal with the fact that smart pointers aren't
 * around to do the reference counting.
 */

  // copy the key, because it might be destroyed in that method, so a reference
  // is not enough.
  DataObjectIdentifierType key = name;

  if( key.empty() )
    {
    itkExceptionMacro("An empty string can't be used as an output identifier");
    }

  // does this change anything?
  DataObjectPointerMap::const_iterator it = m_Outputs.find(key);
  if ( it != m_Outputs.end() && it->second.GetPointer() == output)
    {
    return;
    }

  // Keep a handle to the original output and disconnect the old output from
  // the pipeline
  DataObjectPointer oldOutput;
  if ( m_Outputs[key] )
    {
    oldOutput = m_Outputs[key];
    m_Outputs[key]->DisconnectSource(this, key);
    }

  if ( output )
    {
    output->ConnectSource(this, key);
    }
  // save the current reference (which releases the previous reference)
  m_Outputs[key] = output;

  // if we are clearing an output, we need to create a new blank output
  // so we are prepared for the next Update(). this copies the requested
  // region ivar
  if ( !m_Outputs[key] )
    {
    itkDebugMacro(" creating new output object.");
    DataObjectPointer newOutput = this->MakeOutput(key);
    this->SetOutput(key, newOutput);

    // If we had an output object before, copy the requested region
    // ivars and release data flag to the the new output
    if ( oldOutput )
      {
      newOutput->SetRequestedRegion(oldOutput);
      newOutput->SetReleaseDataFlag( oldOutput->GetReleaseDataFlag() );
      }
    }

  this->Modified();
}


void
ProcessObject
::SetNthOutput(DataObjectPointerArraySizeType idx, DataObject *output)
{
  if ( idx >= this->GetNumberOfIndexedOutputs() )
    {
    this->SetNumberOfIndexedOutputs(idx + 1);
    }
  this->SetOutput( m_IndexedOutputs[idx]->first, output );
}


void
ProcessObject
::AddOutput(DataObject *output)
{
/*
 * Adds an output to the first null position in the output list.
 * Expands the list memory if necessary
 */

  for ( DataObjectPointerArraySizeType idx = 0; idx < this->GetNumberOfIndexedOutputs(); ++idx )
    {
    if ( !this->GetOutput( idx ) )
      {
      this->SetNthOutput( idx, output );
      return;
      }
    }
  this->SetNthOutput( this->GetNumberOfIndexedOutputs(), output );
}


void
ProcessObject
::SetNumberOfIndexedOutputs(DataObjectPointerArraySizeType num)
{
/*
 * Called by constructor to set up output array.
 */

  if( this->GetNumberOfIndexedOutputs() != num )
    {
    if ( num < this->GetNumberOfIndexedOutputs() )
      {
      // NB: The primary output must never be removed from the map, or
      // the indexed inputs array!

      // remove the extra outputs
      for( DataObjectPointerArraySizeType i=std::max<DataObjectPointerArraySizeType>(num, 1);
           i<this->GetNumberOfIndexedOutputs(); ++i )
        {
        // an output should never be ITK_NULLPTR
        itkAssertInDebugAndIgnoreInReleaseMacro( m_IndexedOutputs[i]->second );

        // let the output know we no longer want to associate with the
        // object
        m_IndexedOutputs[i]->second->DisconnectSource( this,  m_IndexedOutputs[i]->first );

        m_Outputs.erase( m_IndexedOutputs[i]->first );
        }

      m_IndexedOutputs.resize( std::max<DataObjectPointerArraySizeType>(num, 1) );

      if (num < 1 )
        {
        m_IndexedOutputs[0]->second = ITK_NULLPTR;
        }
      }
    else
      {
      for ( DataObjectPointerArraySizeType i = m_IndexedOutputs.size(); i < num; ++i)
        {
        DataObjectPointerMap::value_type p(this->MakeNameFromOutputIndex( i ), DataObjectPointer() );
        // note: insert will not change value if it's already there.
        m_IndexedOutputs.push_back ( m_Outputs.insert(p).first );
        }
      }

    this->Modified();
    }

}


DataObject *
ProcessObject
::GetOutput(const DataObjectIdentifierType & key)
{
  DataObjectPointerMap::iterator it = m_Outputs.find(key);
  if ( it == m_Outputs.end() )
    {
    return ITK_NULLPTR;
    }
  return it->second.GetPointer();
}


const DataObject *
ProcessObject
::GetOutput(const DataObjectIdentifierType & key) const
{
  DataObjectPointerMap::const_iterator it = m_Outputs.find(key);
  if ( it == m_Outputs.end() )
    {
    return ITK_NULLPTR;
    }
  return it->second.GetPointer();
}


DataObject *
ProcessObject
::GetOutput(DataObjectPointerArraySizeType i)
{
  return m_IndexedOutputs[i]->second;
}


const DataObject *
ProcessObject
::GetOutput(DataObjectPointerArraySizeType i) const
{
  return m_IndexedOutputs[i]->second;
}


void
ProcessObject
::SetPrimaryOutput(DataObject * object)
{
  this->SetOutput(m_IndexedOutputs[0]->first, object);
}


void
ProcessObject
::SetPrimaryOutputName(const DataObjectIdentifierType & key)
{
  if( key != this->m_IndexedOutputs[0]->first )
    {

    DataObjectPointerMap::value_type p(key, DataObjectPointer() );

    // note: insert will not change value if it's already there.
    DataObjectPointerMap::iterator it = this->m_Outputs.insert( p ).first;

    if ( it->second.IsNull() )
      {
      it->second = this->m_IndexedOutputs[0]->second;
      m_Outputs.erase(this->m_IndexedOutputs[0]);
      }
    this->m_IndexedOutputs[0] = it;

    this->Modified();
    }
}


bool
ProcessObject
::HasOutput( const DataObjectIdentifierType & key ) const
{
  DataObjectPointerMap::const_iterator it = m_Outputs.find(key);
  return it != m_Outputs.end();
}


ProcessObject::NameArray
ProcessObject
::GetOutputNames() const
{
  NameArray res;
  res.reserve(m_Outputs.size());
  for ( DataObjectPointerMap::const_iterator it = m_Outputs.begin(); it != m_Outputs.end(); ++it )
    {
    // only include the primary if it's required or set
    if ( it->first != m_IndexedOutputs[0]->first
         || it->second.IsNotNull() )
      {
      res.push_back( it->first );
      }
    }

  return res;
}

// ProcessObject::ConstDataObjectPointerArray
// ProcessObject
// ::GetOutputs() const
// {
//   ConstDataObjectPointerArray res;
//   res.reserve(m_Outputs.size());
//   for ( DataObjectPointerMap::const_iterator it = m_Outputs.begin(); it != m_Outputs.end(); ++it )
//     {
//     res.push_back( it->second.GetPointer() );
//     }
//   return res;
// }

ProcessObject::DataObjectPointerArray
ProcessObject
::GetOutputs()
{
  DataObjectPointerArray res;
  res.reserve(m_Outputs.size());
  for ( DataObjectPointerMap::iterator it = m_Outputs.begin(); it != m_Outputs.end(); ++it )
    {
    // only include the primary if it's required or set
    if ( it->first != m_IndexedOutputs[0]->first
         || it->second.IsNotNull() )
      {
      res.push_back( it->second.GetPointer() );
      }
    }
  return res;
}


ProcessObject::DataObjectPointerArraySizeType
ProcessObject
::GetNumberOfOutputs() const
{
  // only include the primary if it's required or set
  if ( m_IndexedOutputs[0]->second.IsNotNull() )
    {
    return m_Outputs.size();
    }
  return m_Outputs.size() - 1;
}


ProcessObject::DataObjectPointerArraySizeType
ProcessObject
::GetNumberOfIndexedOutputs() const
{
  // this first element should always contain the primary output's
  // name, if this is not true there is an internal logic error.
  itkAssertInDebugAndIgnoreInReleaseMacro( m_IndexedOutputs.size() >= 1 );

  if( m_IndexedOutputs.size() > 1 )
    {
    return m_IndexedOutputs.size();
    }
  return this->GetPrimaryOutput() != ITK_NULLPTR;
}

// ProcessObject::ConstDataObjectPointerArray
// ProcessObject
// ::GetIndexedOutputs() const
// {
//   ConstDataObjectPointerArray res(this->GetNumberOfIndexedOutputs());
//   for ( unsigned int i = 0; i < this->GetNumberOfIndexedOutputs(); i++ )
//     {
//     res[i] = this->GetOutput(i);
//     }
//   return res;
// }

ProcessObject::DataObjectPointerArray
ProcessObject
::GetIndexedOutputs()
{
  DataObjectPointerArray res(this->GetNumberOfIndexedOutputs());
  for ( DataObjectPointerArraySizeType i = 0; i < this->GetNumberOfIndexedOutputs(); i++ )
    {
    res[i] = this->GetOutput(i);
    }
  return res;
}


DataObject *
ProcessObject
::GetInput(const DataObjectIdentifierType & key)
{
  DataObjectPointerMap::iterator it = m_Inputs.find(key);
  if ( it == m_Inputs.end() )
    {
    return ITK_NULLPTR;
    }
  return it->second.GetPointer();
}


const DataObject *
ProcessObject
::GetInput(const DataObjectIdentifierType & key) const
{
  DataObjectPointerMap::const_iterator it = m_Inputs.find(key);
  if ( it == m_Inputs.end() )
    {
    return ITK_NULLPTR;
    }
  return it->second.GetPointer();
}


void
ProcessObject
::SetPrimaryInput(DataObject * object)
{
  if ( m_IndexedInputs[0]->second != object )
    {
    m_IndexedInputs[0]->second = object;
    this->Modified();
    }
}


void
ProcessObject
::SetPrimaryInputName(const DataObjectIdentifierType & key)
{
  this->RemoveRequiredInputName( m_IndexedInputs[0]->first );
  this->AddRequiredInputName( key, 0 );
}


bool
ProcessObject
::HasInput( const DataObjectIdentifierType & key ) const
{
  DataObjectPointerMap::const_iterator it = m_Inputs.find(key);
  return it != m_Inputs.end();
}


ProcessObject::NameArray
ProcessObject
::GetInputNames() const
{
  NameArray res;
  res.reserve(m_Inputs.size());
  for ( DataObjectPointerMap::const_iterator it = m_Inputs.begin(); it != m_Inputs.end(); ++it )
    {
    // only include the primary if it's required or set
    if ( it->first != m_IndexedInputs[0]->first
         || it->second.IsNotNull()
         || this->IsRequiredInputName(it->first) )
      {
      res.push_back( it->first );
      }
    }
  return res;
}


bool
ProcessObject
::AddRequiredInputName( const DataObjectIdentifierType & name )
{
  if( name.empty() )
    {
    itkExceptionMacro("An empty string can't be used as an input identifier");
    }

  if( !m_RequiredInputNames.insert( name ).second )
    {
    return false;
    }


  this->AddOptionalInputName(name);

  if( name == m_IndexedInputs[0]->first && m_NumberOfRequiredInputs == 0 )
    {
    m_NumberOfRequiredInputs = 1;
    }

  return true;
}

void
ProcessObject
::AddOptionalInputName( const DataObjectIdentifierType & name )
{

  if( name.empty() )
    {
    itkExceptionMacro("An empty string can't be used as an input identifier");
    }

  // note: insert will not change value if it's already there.
  m_Inputs.insert( DataObjectPointerMap::value_type(name, DataObjectPointer() ) );

  this->Modified();
}


bool
ProcessObject
::AddRequiredInputName( const DataObjectIdentifierType & name,
                        DataObjectPointerArraySizeType idx )
{

  if( name.empty() )
    {
    itkExceptionMacro("An empty string can't be used as an input identifier");
    }

  if( !m_RequiredInputNames.insert( name ).second )
    {
    itkWarningMacro(<< "Input already \"" << name << "\" already required!");
    // Input already required, but it is not added as indexed input?
    return false;
    }

  this->AddOptionalInputName(name, idx);

  if( name == m_IndexedInputs[0]->first && m_NumberOfRequiredInputs == 0 )
    {
    m_NumberOfRequiredInputs = 1;
    }

  return true;
}

void
ProcessObject
::AddOptionalInputName( const DataObjectIdentifierType & name,
                        DataObjectPointerArraySizeType idx )
{

  if( name.empty() )
    {
    itkExceptionMacro("An empty string can't be used as an input identifier");
    }

  DataObjectPointerMap::value_type p(name, DataObjectPointer() );
  // note: insert will not change value if it's already in named inputs.
  DataObjectPointerMap::iterator it = m_Inputs.insert(p).first;

  if ( idx >= this->GetNumberOfIndexedInputs() )
    {
    this->SetNumberOfIndexedInputs(idx + 1);
    }
  else if( !it->second )
    {
    // if the old index had a data object move that to the new name
    it->second = this->GetInput( m_IndexedInputs[idx]->first );
    }

  // remove name of the old input ( may be new default index name
  // i.e. _1 )
  m_Inputs.erase( m_IndexedInputs[idx]->first );

  m_IndexedInputs[idx] = it;

  this->Modified();
}


bool
ProcessObject
::RemoveRequiredInputName( const DataObjectIdentifierType & name )
{
  if( m_RequiredInputNames.erase( name ) )
    {
    if( name == m_IndexedInputs[0]->first && m_NumberOfRequiredInputs == 1 )
      {
      m_NumberOfRequiredInputs = 0;
      }
    this->Modified();
    return true;
    }
  return false;
}


bool
ProcessObject
::IsRequiredInputName( const DataObjectIdentifierType & name ) const
{
  return m_RequiredInputNames.find( name ) != m_RequiredInputNames.end();
}


void
ProcessObject
::SetRequiredInputNames( const NameArray & names )
{
  m_RequiredInputNames.clear();
  for ( NameArray::const_iterator it = names.begin(); it != names.end(); ++it )
    {
    this->AddRequiredInputName( *it );
    }
  this->Modified();
}


ProcessObject::NameArray
ProcessObject
::GetRequiredInputNames() const
{
  NameArray res;
  res.reserve(m_RequiredInputNames.size());
  for ( NameSet::const_iterator it = m_RequiredInputNames.begin(); it != m_RequiredInputNames.end(); ++it )
    {
    res.push_back( *it );
    }
  return res;
}

// ProcessObject::ConstDataObjectPointerArray
// ProcessObject
// ::GetInputs() const
// {
//   ConstDataObjectPointerArray res;
//   res.reserve(m_Inputs.size());
//   for ( DataObjectPointerMap::const_iterator it = m_Inputs.begin(); it != m_Inputs.end(); ++it )
//     {
//     res.push_back( it->second.GetPointer() );
//     }
//   return res;
// }

ProcessObject::DataObjectPointerArray
ProcessObject
::GetInputs()
{
  DataObjectPointerArray res;
  res.reserve(m_Inputs.size());
  for ( DataObjectPointerMap::iterator it = m_Inputs.begin(); it != m_Inputs.end(); ++it )
    {
    // only include the primary if it's required or set
    if ( it->first != m_IndexedInputs[0]->first
         || it->second.IsNotNull()
         || this->IsRequiredInputName(it->first) )
      {
      res.push_back( it->second.GetPointer() );
      }
    }
  return res;
}


ProcessObject::DataObjectPointerArraySizeType
ProcessObject
::GetNumberOfInputs() const
{
  // only include the primary if it's required or set
  if ( m_IndexedInputs[0]->second.IsNotNull()
       || this->IsRequiredInputName(m_IndexedInputs[0]->first) )
    {
    return m_Inputs.size();
    }
  return m_Inputs.size() - 1;
}


ProcessObject::DataObjectPointerArraySizeType
ProcessObject
::GetNumberOfIndexedInputs() const
{
  // this first element should always contain the primary input's
  // name, if this is not true there is an internal logic error.
  itkAssertInDebugAndIgnoreInReleaseMacro(  m_IndexedInputs.size() >= 1 );

  if (  m_IndexedInputs.size() > 1 )
    {
    return m_IndexedInputs.size();
    }
  return static_cast<ProcessObject::DataObjectPointerArraySizeType>(this->GetPrimaryInput() != ITK_NULLPTR);
}

// ProcessObject::ConstDataObjectPointerArray
// ProcessObject
// ::GetIndexedInputs() const
// {
//   ConstDataObjectPointerArray res(this->GetNumberOfIndexedInputs());
//   for ( DataObjectPointerArraySizeType i = 0; i < this->GetNumberOfIndexedInputs(); i++ )
//     {
//     res[i] = this->GetInput(i);
//     }
//   return res;
// }

ProcessObject::DataObjectPointerArray
ProcessObject
::GetIndexedInputs()
{
  DataObjectPointerArray res(this->GetNumberOfIndexedInputs());
  for ( DataObjectPointerArraySizeType i = 0; i < this->GetNumberOfIndexedInputs(); i++ )
    {
    res[i] = this->GetInput(i);
    }
  return res;
}


ProcessObject::DataObjectIdentifierType
ProcessObject
::MakeNameFromInputIndex(DataObjectPointerArraySizeType idx) const
{
  if( idx == 0 )
    {
    return m_IndexedInputs[0]->first;
    }
  return this->MakeNameFromIndex(idx);
}


ProcessObject::DataObjectIdentifierType
ProcessObject
::MakeNameFromOutputIndex(DataObjectPointerArraySizeType idx) const
{
  if( idx == 0 )
    {
    return this->m_IndexedOutputs[0]->first;
    }
  return this->MakeNameFromIndex(idx);
}


ProcessObject::DataObjectIdentifierType
ProcessObject
::MakeNameFromIndex(DataObjectPointerArraySizeType idx) const
{
  if ( idx < ITK_GLOBAL_INDEX_NAMES_NUMBER )
    {
    return ProcessObject::DataObjectIdentifierType( globalIndexNames[idx] );
    }
  else
    {
    char buf[2+21]; // a 64-bit integer is ~20 decimal places max
    sprintf(buf, "_%u", static_cast<unsigned int>(idx));
    return buf;
    }
}


ProcessObject::DataObjectPointerArraySizeType
ProcessObject
::MakeIndexFromInputName(const DataObjectIdentifierType & name) const
{
  if( name == m_IndexedInputs[0]->first )
    {
    itkDebugMacro("MakeIndexFromName("<<name<<") -> 0");
    return 0;
    }
  return this->MakeIndexFromName( name );
}


ProcessObject::DataObjectPointerArraySizeType
ProcessObject
::MakeIndexFromOutputName(const DataObjectIdentifierType & name) const
{
  if( name == this->m_IndexedOutputs[0]->first )
    {
    itkDebugMacro("MakeIndexFromName("<<name<<") -> 0");
    return 0;
    }
  return this->MakeIndexFromName( name );
}


ProcessObject::DataObjectPointerArraySizeType
ProcessObject
::MakeIndexFromName(const DataObjectIdentifierType & name) const
{
  DataObjectIdentifierType baseName = "_";
  DataObjectPointerArraySizeType baseSize = baseName.size();
  if( name.size() <= baseSize || name.substr(0, baseSize) != baseName )
    {
    itkDebugMacro("MakeIndexFromName("<<name<<") -> exception bad base name");
    itkExceptionMacro(<<"Not an indexed data object: " << name);
    }
  DataObjectIdentifierType idxStr = name.substr(baseSize);
  DataObjectPointerArraySizeType idx;
  if( !(std::istringstream(idxStr) >> idx) )
    {
    itkDebugMacro("MakeIndexFromName("<<name<<") -> exception not an index");
    itkExceptionMacro(<<"Not an indexed data object: " << name);
    }
  itkDebugMacro("MakeIndexFromName("<<name<<") -> "<< idx);
  return idx;
}


bool
ProcessObject
::IsIndexedInputName(const DataObjectIdentifierType & name) const
{
  if( name == m_IndexedInputs[0]->first )
    {
    return true;
    }
  for (  DataObjectPointerArraySizeType i = 0; i < m_IndexedInputs.size(); ++i)
    {
    if ( m_IndexedInputs[i]->first == name )
      {
      return true;
      }
    }
  return false;
}


bool
ProcessObject
::IsIndexedOutputName(const DataObjectIdentifierType & name) const
{
  if( name == m_IndexedOutputs[0]->first )
    {
    return true;
    }
  for (  DataObjectPointerArraySizeType i = 0; i < m_IndexedOutputs.size(); ++i)
    {
    if ( m_IndexedOutputs[i]->first == name )
      {
      return true;
      }
    }
  return false;
}


void
ProcessObject
::UpdateProgress(float progress)
{
/*
 * Update the progress of the process object. If a ProgressMethod exists,
 * execute it. Then set the Progress ivar to amount. The parameter amount
 * should range between (0,1).
 */

  // Clamp the value to be between 0 and 1.
  m_Progress = std::max(progress, 0.0f);
  m_Progress = std::min(m_Progress, 1.0f);

  this->InvokeEvent( ProgressEvent() );
}


bool
ProcessObject
::GetReleaseDataFlag() const
{
  if ( this->GetPrimaryOutput() )
    {
    return this->GetPrimaryOutput()->GetReleaseDataFlag();
    }
  itkWarningMacro(<< "Output doesn't exist!");
  return false;
}


void
ProcessObject
::SetReleaseDataFlag(bool val)
{
  for ( DataObjectPointerMap::iterator it=m_Outputs.begin(); it != m_Outputs.end(); ++it )
    {
    if ( it->second )
      {
      it->second->SetReleaseDataFlag(val);
      }
    }
}


void
ProcessObject
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  Indent indent2 = indent.GetNextIndent();
  if ( !m_Inputs.empty() )
    {
    os << indent << "Inputs: " << std::endl;
    for ( DataObjectPointerMap::const_iterator it = m_Inputs.begin(); it != m_Inputs.end(); ++it )
      {
      std::string req = "";
      if( this->IsRequiredInputName( it->first ) )
        {
        req = " *";
        }
      os << indent2 << it->first<< ": (" << it->second.GetPointer() << ")" << req  << std::endl;
      }
    }
  else
    {
    os << indent << "No Inputs\n";
    }

  os << indent << "Indexed Inputs: " << std::endl;
  unsigned int idx = 0;
  for ( std::vector< DataObjectPointerMap::iterator >::const_iterator it = m_IndexedInputs.begin();
        it != m_IndexedInputs.end();
        ++it, ++idx)
    {
    os << indent2 << idx << ": " << (*it)->first  << " (" << (*it)->second.GetPointer() << ")"<< std::endl;
    }

  if( !m_RequiredInputNames.empty() )
    {
    os << indent << "Required Input Names: ";
    for( NameSet::const_iterator it = m_RequiredInputNames.begin(); it != m_RequiredInputNames.end(); ++it )
      {
      if( it != m_RequiredInputNames.begin() )
        {
        os << ", ";
        }
      os << *it;
      }
    os << std::endl;
    }
  else
    {
    os << indent << "No Required Input Names" << std::endl;
    }
  os << indent << "NumberOfRequiredInputs: "
     << m_NumberOfRequiredInputs << std::endl;

  if ( !m_Outputs.empty() )
    {
    os << indent << "Outputs: " << std::endl;
    for ( DataObjectPointerMap::const_iterator it = m_Outputs.begin(); it != m_Outputs.end(); ++it )
      {
      os << indent2 << it->first << ": (" << it->second.GetPointer() << ")" << std::endl;
      }
    }
  else
    {
    os << indent << "No Outputs\n";
    }
  os << indent << "Indexed Outputs: " << std::endl;
  idx = 0;
  for ( std::vector< DataObjectPointerMap::iterator >::const_iterator it = m_IndexedOutputs.begin();
        it != m_IndexedOutputs.end();
        ++it, ++idx)
    {
    os << indent2 << idx << ": " << (*it)->first  << " (" << (*it)->second.GetPointer() << ")"<< std::endl;
    }

  os << indent << "NumberOfRequiredOutputs: "
     << m_NumberOfRequiredOutputs << std::endl;

  os << indent << "Number Of Threads: "
     << m_NumberOfThreads << std::endl;

  os << indent << "ReleaseDataFlag: "
     << ( this->GetReleaseDataFlag() ? "On" : "Off" ) << std::endl;

  os << indent << "ReleaseDataBeforeUpdateFlag: "
     << ( m_ReleaseDataBeforeUpdateFlag ? "On" : "Off" ) << std::endl;

  os << indent << "AbortGenerateData: " << ( m_AbortGenerateData ? "On" : "Off" ) << std::endl;
  os << indent << "Progress: " << m_Progress << std::endl;

  os << indent << "Multithreader: " << std::endl;
  m_Threader->PrintSelf( os, indent.GetNextIndent() );
}


void
ProcessObject
::Update()
{
  if ( this->GetPrimaryOutput() )
    {
    this->GetPrimaryOutput()->Update();
    }
}


void
ProcessObject
::ResetPipeline()
{
  if ( this->GetPrimaryOutput() )
    {
    this->GetPrimaryOutput()->ResetPipeline();
    }
  else
    {
    // important to make it work on process objects without outputs
    this->PropagateResetPipeline();
    }
}


void
ProcessObject
::PropagateResetPipeline()
{
  //
  // Reset this object.
  //
  // Clear the updating flag.
  m_Updating = 0;

  //
  // Loop through the inputs
  //
  for ( DataObjectPointerMap::iterator it=m_Inputs.begin(); it != m_Inputs.end(); ++it )
    {
    if ( it->second )
      {
      it->second->PropagateResetPipeline();
      }
    }
}


void
ProcessObject
::VerifyPreconditions()
{
  /**
   * Make sure that all the required named inputs are there and non null
   */
  for( NameSet::const_iterator it = this->m_RequiredInputNames.begin(); it != this->m_RequiredInputNames.end(); ++it )
    {
    if ( this->GetInput( *it ) == ITK_NULLPTR )
      {
      itkExceptionMacro(<< "Input " << *it << " is required but not set.");
      }
    }

  /**
   * Verify the require named inputs.
   */
  NameSet::const_iterator i = m_RequiredInputNames.begin();
  while (i != m_RequiredInputNames.end())
    {
    if ( this->GetInput(*i) == ITK_NULLPTR )
      {
      itkExceptionMacro( << "Required Input " << *i << "is not specified!"
                         << " The required inputs are expected to be the first inputs.");

      }
    ++i;
    }

  /**
    * Count the number of required indexed inputs which have been assigned
    */
  const DataObjectPointerArraySizeType validIndexedInputs = this->GetNumberOfValidRequiredInputs();

  if ( validIndexedInputs < this->m_NumberOfRequiredInputs )
    {
    itkExceptionMacro(<< "At least " << this->m_NumberOfRequiredInputs
                      << " of the first " << this->m_NumberOfRequiredInputs
                      << " indexed inputs are required but only " << validIndexedInputs
                      << " are specified."
                      << " The required inputs are expected to be the first inputs.");
    }
}


void
ProcessObject
::VerifyInputInformation()
{
}


void
ProcessObject
::UpdateOutputInformation()
{
  ModifiedTimeType               t1, t2;
  DataObject *                   input;
  DataObject *                   output;

  /**
   * Watch out for loops in the pipeline
   */
  if ( m_Updating )
    {
    /**
     * Since we are in a loop, we will want to update. But if
     * we don't modify this filter, then we will not execute
     * because our OutputInformationMTime will be more recent than
     * the MTime of our output.
     */
    this->Modified();
    return;
    }

  /**
   * Verify that the process object has been configured correctly,
   * that all required inputs are set, and needed parameters are set
   * appropriately before we continue the pipeline, i.e. is the filter
   * in a state that it can be run.
   */
  this->VerifyPreconditions();

  /**
   * We now wish to set the PipelineMTime of each output DataObject to
   * the largest of this ProcessObject's MTime, all input DataObject's
   * PipelineMTime, and all input's MTime.  We begin with the MTime of
   * this ProcessObject.
   */
  t1 = this->GetMTime();

  /**
   * Loop through the inputs
   */
  for ( DataObjectPointerMap::iterator it=m_Inputs.begin(); it != m_Inputs.end(); ++it )
    {
    if ( it->second )
      {
      input = it->second;

      /**
       * Propagate the UpdateOutputInformation call
       */
      m_Updating = true;
      input->UpdateOutputInformation();
      m_Updating = false;

      /**
       * What is the PipelineMTime of this input? Compare this against
       * our current computation to find the largest one.
       */
      t2 = input->GetPipelineMTime();

      if ( t2 > t1 )
        {
        t1 = t2;
        }

      /**
       * Pipeline MTime of the input does not include the MTime of the
       * data object itself. Factor these mtimes into the next PipelineMTime
       */
      t2 = input->GetMTime();
      if ( t2 > t1 )
        {
        t1 = t2;
        }
      }
    }

  /**
   * Call GenerateOutputInformation for subclass specific information.
   * Since UpdateOutputInformation propagates all the way up the pipeline,
   * we need to be careful here to call GenerateOutputInformation only if
   * necessary. Otherwise, we may cause this source to be modified which
   * will cause it to execute again on the next update.
   */
  if ( t1 > m_OutputInformationMTime.GetMTime() )
    {
    for ( DataObjectPointerMap::iterator it=m_Outputs.begin(); it != m_Outputs.end(); ++it )
      {
      output = it->second;
      if ( output )
        {
        output->SetPipelineMTime(t1);
        }
      }

    /**
     * Verify that all the inputs are consistent with the
     * requirements of the filter. For example, subclasses might want
     * to ensure all the inputs are in the same coordinate frame.
     */
    this->VerifyInputInformation();

    /**
     * Finally, generate the output information.
     */
    this->GenerateOutputInformation();

    /**
     * Keep track of the last time GenerateOutputInformation() was called
     */
    m_OutputInformationMTime.Modified();
    }
}


void
ProcessObject
::PropagateRequestedRegion(DataObject *output)
{
  /**
   * check flag to avoid executing forever if there is a loop
   */
  if ( m_Updating )
    {
    return;
    }

  /**
   * Give the subclass a chance to indicate that it will provide
   * more data then required for the output. This can happen, for
   * example, when a source can only produce the whole output.
   * Although this is being called for a specific output, the source
   * may need to enlarge all outputs.
   */
  this->EnlargeOutputRequestedRegion(output);

  /**
   * Give the subclass a chance to define how to set the requested
   * regions for each of its outputs, given this output's requested
   * region.  The default implementation is to make all the output
   * requested regions the same.  A subclass may need to override this
   * method if each output is a different resolution.
   */
  this->GenerateOutputRequestedRegion(output);

  /**
   * Give the subclass a chance to request a larger requested region on
   * the inputs. This is necessary when, for example, a filter
   * requires more data at the "internal" boundaries to
   * produce the boundary values - such as an image filter that
   * derives a new pixel value by applying some operation to a
   * neighborhood of surrounding original values.
   */
  this->GenerateInputRequestedRegion();

  /**
   * Now that we know the input requested region, propagate this
   * through all the inputs.
   */
  m_Updating = true;
  for ( DataObjectPointerMap::iterator it=m_Inputs.begin(); it != m_Inputs.end(); ++it )
    {
    if ( it->second )
      {
      it->second->PropagateRequestedRegion();
      }
    }
  m_Updating = false;
}


void
ProcessObject
::GenerateInputRequestedRegion()
{
/*
 * By default we require all the input to produce the output. This is
 * overridden in the subclasses since we can often produce the output with
 * just a portion of the input data.
 */
  for ( DataObjectPointerMap::iterator it=m_Inputs.begin(); it != m_Inputs.end(); ++it )
    {
    if ( it->second )
      {
      it->second->SetRequestedRegionToLargestPossibleRegion();
      }
    }
}


void
ProcessObject
::GenerateOutputRequestedRegion(DataObject *output)
{
/**
 * By default we set all the output requested regions to be the same.
 */

  for ( DataObjectPointerMap::iterator it=m_Outputs.begin(); it != m_Outputs.end(); ++it )
    {
    if ( it->second && it->second != output )
      {
      it->second->SetRequestedRegion(output);
      }
    }
}


void
ProcessObject
::PrepareOutputs()
{
  if ( this->GetReleaseDataBeforeUpdateFlag() )
    {
    for ( DataObjectPointerMap::iterator it=m_Outputs.begin(); it != m_Outputs.end(); ++it )
      {
      if ( it->second )
        {
        it->second->PrepareForNewData();
        }
      }
    }
}


void
ProcessObject
::ReleaseInputs()
{
  for ( DataObjectPointerMap::iterator it=m_Inputs.begin(); it != m_Inputs.end(); ++it )
    {
    if ( it->second )
      {
      if ( it->second->ShouldIReleaseData() )
        {
        it->second->ReleaseData();
        }
      }
    }
}


void
ProcessObject
::UpdateOutputData( DataObject * itkNotUsed(output) )
{
  /**
   * prevent chasing our tail
   */
  if ( m_Updating )
    {
    return;
    }

  /**
   * Prepare all the outputs. This may deallocate previous bulk data.
   */
  this->PrepareOutputs();

  /**
   * Propagate the update call - make sure everything we
   * might rely on is up-to-date
   * Must call PropagateRequestedRegion before UpdateOutputData if multiple
   * inputs since they may lead back to the same data object.
   */
  m_Updating = true;
  if ( m_Inputs.size() == 1 )
    {
    if ( this->GetPrimaryInput() )
      {
      this->GetPrimaryInput()->UpdateOutputData();
      }
    }
  else
    {
    for ( DataObjectPointerMap::iterator it=m_Inputs.begin(); it != m_Inputs.end(); ++it )
      {
      if ( it->second )
        {
        it->second->PropagateRequestedRegion();
        it->second->UpdateOutputData();
        }
      }
    }

  /**
   * Cache the state of any ReleaseDataFlag's on the inputs. While the
   * filter is executing, we need to set the ReleaseDataFlag's on the
   * inputs to false in case the current filter is implemented using a
   * mini-pipeline (which will try to release the inputs).  After the
   * filter finishes, we restore the state of the ReleaseDataFlag's
   * before the call to ReleaseInputs().
   */
  this->CacheInputReleaseDataFlags();

  /**
   * Tell all Observers that the filter is starting
   */
  this->InvokeEvent( StartEvent() );

  /**
   * GenerateData this object - we have not aborted yet, and our progress
   * before we start to execute is 0.0.
   */
  m_AbortGenerateData = false;
  m_Progress = 0.0f;

  try
    {
    this->GenerateData();
    }
  catch ( ProcessAborted & )
    {
    this->InvokeEvent( AbortEvent() );
    this->ResetPipeline();
    this->RestoreInputReleaseDataFlags();
    throw;
    }
  catch (...)
    {
    this->ResetPipeline();
    this->RestoreInputReleaseDataFlags();
    throw;
    }

  /**
   * If we ended due to aborting, push the progress up to 1.0 (since
   * it probably didn't end there)
   *
   */
  if ( m_AbortGenerateData )
    {
    this->UpdateProgress(1.0f);
    }

  /**
   * Notify end event observers
   */
  this->InvokeEvent( EndEvent() );

  /**
   * Now we have to mark the data as up to date.
   */
  for ( DataObjectPointerMap::iterator it=m_Outputs.begin(); it != m_Outputs.end(); ++it )
    {
    if ( it->second )
      {
      it->second->DataHasBeenGenerated();
      }
    }

  /**
   * Restore the state of any input ReleaseDataFlags
   */
  this->RestoreInputReleaseDataFlags();

  /**
   * Release any inputs if marked for release
   */
  this->ReleaseInputs();

  // Mark that we are no longer updating the data in this filter
  m_Updating = false;
}


void
ProcessObject
::CacheInputReleaseDataFlags()
{
  m_CachedInputReleaseDataFlags.clear();
  for ( DataObjectPointerMap::iterator it = m_Inputs.begin(); it != m_Inputs.end(); ++it )
    {
    if ( it->second )
      {
      m_CachedInputReleaseDataFlags[it->first] = it->second->GetReleaseDataFlag();
      it->second->ReleaseDataFlagOff();
      }
    else
      {
      m_CachedInputReleaseDataFlags[it->first] = false;
      }
    }
}


void
ProcessObject
::RestoreInputReleaseDataFlags()
{
  for ( DataObjectPointerMap::iterator it = m_Inputs.begin(); it != m_Inputs.end(); ++it )
    {
    if ( it->second )
      {
      it->second->SetReleaseDataFlag(m_CachedInputReleaseDataFlags[it->first]);
      }
    }
  m_CachedInputReleaseDataFlags.clear();
}


void
ProcessObject
::GenerateOutputInformation()
{
/*
 * Default implementation - copy information from first input to all outputs
 */

  DataObject * input = this->GetPrimaryInput();

  if ( input )
    {
    for ( DataObjectPointerMap::iterator it=m_Outputs.begin(); it != m_Outputs.end(); ++it )
      {
      if ( it->second )
        {
        it->second->CopyInformation(input);
        }
      }
    }
}


void
ProcessObject
::UpdateLargestPossibleRegion()
{
  this->UpdateOutputInformation();

  if ( this->GetPrimaryOutput() )
    {
    this->GetPrimaryOutput()->SetRequestedRegionToLargestPossibleRegion();
    this->GetPrimaryOutput()->Update();
    }
}


template< typename TDomainPartitioner, typename TAssociate >
ProcessObject::ProcessObjectDomainThreader< TDomainPartitioner, TAssociate >
::ProcessObjectDomainThreader()
{
}


template< typename TDomainPartitioner, typename TAssociate >
ProcessObject::ProcessObjectDomainThreader< TDomainPartitioner, TAssociate >
::~ProcessObjectDomainThreader()
{
}


template< typename TDomainPartitioner, typename TAssociate >
void
ProcessObject::ProcessObjectDomainThreader< TDomainPartitioner, TAssociate >
::DetermineNumberOfThreadsUsed()
{
  MultiThreader * multiThreader = this->m_Associate->GetMultiThreader();
  this->SetMultiThreader( multiThreader );
  multiThreader->SetNumberOfThreads( this->m_Associate->GetNumberOfThreads() );

  Superclass::DetermineNumberOfThreadsUsed();
}


void
ProcessObject
::SetNumberOfRequiredInputs( DataObjectPointerArraySizeType nb )
{
  if( m_NumberOfRequiredInputs != nb )
    {
    m_NumberOfRequiredInputs = nb;
    this->Modified();
    if( m_NumberOfRequiredInputs > 0 )
      {
      this->AddRequiredInputName( m_IndexedInputs[0]->first );
      }
    if( m_NumberOfRequiredInputs == 0 )
      {
      this->RemoveRequiredInputName( m_IndexedInputs[0]->first );
      }
    }
}


#if !defined(ITK_LEGACY_REMOVE)
void
ProcessObject
::SetNumberOfInputs(DataObjectPointerArraySizeType num)
{
  this->SetNumberOfIndexedInputs(num);
}


void
ProcessObject
::SetNumberOfOutputs(DataObjectPointerArraySizeType num)
{
  this->SetNumberOfIndexedOutputs(num);
}


void
ProcessObject
::RemoveInput(DataObject * input)
{
  if( !input )
    {
    return;
    }
  DataObjectPointerArraySizeType nb = this->GetNumberOfIndexedInputs();
  for( DataObjectPointerArraySizeType i = 0; i < nb; i++ )
    {
    if( this->GetInput(i) == input )
      {
      this->RemoveInput( i );
      return;
      }
    }
  itkDebugMacro("tried to remove an input that was not in the list");
}


void
ProcessObject
::RemoveOutput(DataObject * output)
{
  if( !output )
    {
    return;
    }
  DataObjectPointerArraySizeType nb = this->GetNumberOfIndexedOutputs();
  for( DataObjectPointerArraySizeType i = 0; i < nb; i++ )
    {
    if( this->GetOutput(i) == output )
      {
      this->RemoveOutput( i );
      return;
      }
    }
  itkDebugMacro("tried to remove an output that was not in the list");
}
#endif

} // end namespace itk