-
-
Notifications
You must be signed in to change notification settings - Fork 661
/
itkProcessObject.cxx
1842 lines (1564 loc) · 42.7 KB
/
itkProcessObject.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
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
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://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 <mutex>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include "itkMultiThreaderBase.h"
namespace itk
{
namespace
{ // local namespace for managing globals
constexpr size_t ITK_GLOBAL_INDEX_NAMES_NUMBER = 10;
constexpr size_t ITK_GLOBAL_INDEX_NAMES_LENGTH = 3;
constexpr char globalIndexNames[ITK_GLOBAL_INDEX_NAMES_NUMBER][ITK_GLOBAL_INDEX_NAMES_LENGTH] = {
"_0", "_1", "_2", "_3", "_4", "_5", "_6", "_7", "_8", "_9"
};
} // namespace
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 = 0u;
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(std::move(p)).first);
this->Self::SetMultiThreader(MultiThreaderType::New());
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 appropriate
* MakeOutput(), then ProcessObject::MakeOutput() can be made pure
* virtual.
*/
itkDebugMacro("MakeOutput(" << name << ')');
if (this->IsIndexedOutputName(name))
{
return this->MakeOutput(this->MakeIndexFromOutputName(name));
}
return 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 (auto & output : m_Outputs)
{
if (output.second)
{
// let the output know we no longer want to associate with the object
output.second->DisconnectSource(this, output.first);
// let go of our reference to the data object
output.second = 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 = nullptr;
}
}
else
{
for (DataObjectPointerArraySizeType i = m_IndexedInputs.size(); i < num; ++i)
{
auto iter = m_Inputs.emplace(this->MakeNameFromInputIndex(i), DataObjectPointer()).first;
// note: insert will not change value if it's already there.
m_IndexedInputs.push_back(std::move(iter));
}
}
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 int 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, 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, nullptr);
if (i == m_IndexedInputs.size() - 1)
{
// remove the last indexed input
this->SetNumberOfIndexedInputs(this->GetNumberOfIndexedInputs() - 1);
}
return;
}
}
auto 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");
}
auto 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, 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, nullptr);
if (i == m_IndexedOutputs.size() - 1)
{
// remove the last indexed input
this->SetNumberOfIndexedOutputs(this->GetNumberOfIndexedOutputs() - 1);
}
return;
}
}
auto it = m_Outputs.find(key);
if (it != m_Outputs.end())
{
if (it->second)
{
// 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 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 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 = nullptr;
}
}
else
{
for (DataObjectPointerArraySizeType i = m_IndexedOutputs.size(); i < num; ++i)
{
auto iter = m_Outputs.emplace(this->MakeNameFromOutputIndex(i), DataObjectPointer()).first;
// note: insert will not change value if it's already there.
m_IndexedOutputs.push_back(std::move(iter));
}
}
this->Modified();
}
}
DataObject *
ProcessObject::GetOutput(const DataObjectIdentifierType & key)
{
auto it = m_Outputs.find(key);
if (it == m_Outputs.end())
{
return nullptr;
}
return it->second.GetPointer();
}
const DataObject *
ProcessObject::GetOutput(const DataObjectIdentifierType & key) const
{
auto it = m_Outputs.find(key);
if (it == m_Outputs.end())
{
return 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)
{
// note: insert will not change value if it's already there.
auto it = this->m_Outputs.emplace(key, DataObjectPointer()).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
{
auto it = m_Outputs.find(key);
return it != m_Outputs.end();
}
ProcessObject::NameArray
ProcessObject::GetOutputNames() const
{
NameArray res;
res.reserve(m_Outputs.size());
for (const auto & output : m_Outputs)
{
// only include the primary if it's required or set
if (output.first != m_IndexedOutputs[0]->first || output.second.IsNotNull())
{
res.push_back(output.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 (auto & output : m_Outputs)
{
// only include the primary if it's required or set
if (output.first != m_IndexedOutputs[0]->first || output.second.IsNotNull())
{
res.push_back(output.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() != 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)
{
auto it = m_Inputs.find(key);
if (it == m_Inputs.end())
{
return nullptr;
}
return it->second.GetPointer();
}
const DataObject *
ProcessObject::GetInput(const DataObjectIdentifierType & key) const
{
auto it = m_Inputs.find(key);
if (it == m_Inputs.end())
{
return 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
{
auto it = m_Inputs.find(key);
return it != m_Inputs.end();
}
ProcessObject::NameArray
ProcessObject::GetInputNames() const
{
NameArray res;
res.reserve(m_Inputs.size());
for (const auto & input : m_Inputs)
{
// only include the primary if it's required or set
if (input.first != m_IndexedInputs[0]->first || input.second.IsNotNull() || this->IsRequiredInputName(input.first))
{
res.push_back(input.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.emplace(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");
}
// note: insert will not change value if it's already in named inputs.
auto it = m_Inputs.emplace(name, DataObjectPointer()).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 (const auto & name : names)
{
this->AddRequiredInputName(name);
}
this->Modified();
}
ProcessObject::NameArray
ProcessObject::GetRequiredInputNames() const
{
NameArray res;
res.reserve(m_RequiredInputNames.size());
for (const auto & requiredInputName : m_RequiredInputNames)
{
res.push_back(requiredInputName);
}
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 (auto & input : m_Inputs)
{
// only include the primary if it's required or set
if (input.first != m_IndexedInputs[0]->first || input.second.IsNotNull() || this->IsRequiredInputName(input.first))
{
res.push_back(input.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() != nullptr);
}