Slicer 5.9
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
Loading...
Searching...
No Matches
vtkSegmentation.h
Go to the documentation of this file.
1/*==============================================================================
2
3 Copyright (c) Laboratory for Percutaneous Surgery (PerkLab)
4 Queen's University, Kingston, ON, Canada. All Rights Reserved.
5
6 See COPYRIGHT.txt
7 or http://www.slicer.org/copyright/copyright.txt for details.
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14
15 This file was originally developed by Csaba Pinter, PerkLab, Queen's University
16 and was supported through the Applied Cancer Research Unit program of Cancer Care
17 Ontario with funds provided by the Ontario Ministry of Health and Long-Term Care
18
19==============================================================================*/
20
21#ifndef __vtkSegmentation_h
22#define __vtkSegmentation_h
23
24// VTK includes
25#include <vtkObject.h>
26#include <vtkSmartPointer.h>
27
28// STD includes
29#include <map>
30#include <deque>
31#include <vector>
32
33// SegmentationCore includes
34#include "vtkSegment.h"
37
38#include "vtkSegmentationCoreConfigure.h"
39
40#include <vtkSingleton.h>
41
42class vtkAbstractTransform;
43class vtkCallbackCommand;
44class vtkCollection;
45class vtkIntArray;
46class vtkMinimalStandardRandomSequence;
48class vtkStringArray;
49
95class vtkSegmentationCore_EXPORT vtkSegmentation : public vtkObject
96{
97public:
98 enum
99 {
115 };
116
117 enum
118 {
131 };
132
134 typedef std::map<std::string, vtkSmartPointer<vtkSegment>> SegmentMap;
135
136public:
138 vtkTypeMacro(vtkSegmentation, vtkObject);
139 void PrintSelf(ostream& os, vtkIndent indent) override;
140
142 virtual void ReadXMLAttributes(const char** atts);
143
145 virtual void WriteXML(ostream& of, int indent);
146
148 virtual void DeepCopy(vtkSegmentation* aSegmentation);
149
151 virtual void CopyConversionParameters(vtkSegmentation* aSegmentation);
152
154 virtual void GetBounds(double bounds[6]);
155
158 virtual void ApplyLinearTransform(vtkAbstractTransform* transform);
159
162 virtual void ApplyNonLinearTransform(vtkAbstractTransform* transform);
163
164#ifndef __VTK_WRAP__
173 std::string DetermineCommonLabelmapGeometry(int extentComputationMode = EXTENT_UNION_OF_SEGMENTS, const std::vector<std::string>& segmentIDs = std::vector<std::string>());
174
180 void DetermineCommonLabelmapExtent(int commonGeometryExtent[6],
181 vtkOrientedImageData* commonGeometryImage,
182 const std::vector<std::string>& segmentIDs = std::vector<std::string>(),
183 bool computeEffectiveExtent = false,
184 bool addPadding = false);
185#endif // __VTK_WRAP__
186
188 std::string DetermineCommonLabelmapGeometry(int extentComputationMode, vtkStringArray* segmentIds);
189
191 void DetermineCommonLabelmapExtent(int commonGeometryExtent[6],
192 vtkOrientedImageData* commonGeometryImage,
193 vtkStringArray* segmentIds,
194 bool computeEffectiveExtent = false,
195 bool addPadding = false);
196
200 vtkStringArray* segmentIDs = nullptr,
201 int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS);
202
203 // Segment related methods
204
217 bool AddSegment(vtkSegment* segment, std::string segmentId = "", std::string insertBeforeSegmentId = "");
218
226 std::string GenerateUniqueSegmentID(std::string id = "");
227
230 void RemoveSegment(std::string segmentId);
231
234 void RemoveSegment(vtkSegment* segment);
235
238
241 vtkSegment* GetSegment(std::string segmentId);
242
244 void GetSegmentIDs(std::vector<std::string>& segmentIds);
245
247 void GetSegmentIDs(vtkStringArray* segmentIds);
248
250 std::vector<std::string> GetSegmentIDs();
251
254
256 vtkSegment* GetNthSegment(unsigned int index) const;
257
259 std::string GetNthSegmentID(unsigned int index) const;
260
264 int GetSegmentIndex(const std::string& segmentId);
265
269 bool SetSegmentIndex(const std::string& segmentId, unsigned int newIndex);
270
275 void ReorderSegments(std::vector<std::string> segmentIdsToMove, std::string insertBeforeSegmentId = "");
276
279 std::string GetSegmentIdBySegment(vtkSegment* segment);
280
284 std::string GetSegmentIdBySegmentName(std::string name);
285
290 std::vector<vtkSegment*> GetSegmentsByTag(std::string tag, std::string value = "");
291
293 vtkDataObject* GetSegmentRepresentation(std::string segmentId, std::string representationName);
294
301 bool CopySegmentFromSegmentation(vtkSegmentation* fromSegmentation, std::string segmentId, bool removeFromSource = false);
302
303 // Representation related methods
304
308 void GetContainedRepresentationNames(std::vector<std::string>& representationNames);
309
313 bool ContainsRepresentation(std::string representationName);
314
317
320 {
321 vtkWarningMacro("vtkSegmentation::IsMasterRepresentationPolyData() method is deprecated, please use IsSourceRepresentationPolyData method instead");
322 return this->IsSourceRepresentationPolyData();
323 };
324
327
330 {
331 vtkWarningMacro("vtkSegmentation::IsMasterRepresentationImageData() method is deprecated, please use IsSourceRepresentationImageData method instead");
332 return this->IsSourceRepresentationImageData();
333 };
334
336 void GetAvailableRepresentationNames(std::set<std::string>& representationNames) { this->Converter->GetAvailableRepresentationNames(representationNames); };
337
340
343 {
344 vtkWarningMacro("vtkSegmentation::InvalidateNonMasterRepresentations() method is deprecated, please use InvalidateNonSourceRepresentations method instead");
346 };
347
349
350#ifndef __VTK_WRAP__
364 int extentComputationMode,
365 vtkOrientedImageData* mergedLabelmapGeometry = nullptr,
366 const std::vector<std::string>& segmentIDs = std::vector<std::string>(),
367 vtkIntArray* labelValues = nullptr);
368#endif // __VTK_WRAP__
369
371
373 bool IsSharedBinaryLabelmap(std::string segmentID);
374
380 void GetSegmentIDsSharingRepresentation(std::string originalSegmentId,
381 std::string representationName,
382 std::vector<std::string>& sharedSegmentIds,
383 bool includeOriginalSegmentId = true);
384
389 void GetSegmentIDsSharingBinaryLabelmapRepresentation(std::string originalSegmentId, std::vector<std::string>& sharedSegmentIds, bool includeOriginalSegmentId = true);
390
394 int GetUniqueLabelValueForSharedLabelmap(std::string segmentId);
395
399
404 void MergeSegmentLabelmaps(std::vector<std::string> mergeSegmentIds);
405
407 void SeparateSegmentLabelmap(std::string segmentId);
408
412 void ClearSegment(std::string segmentId);
413
415
418 int GetNumberOfLayers(std::string representationName = "");
419
422 int GetLayerIndex(std::string segmentId, std::string representationName = "");
423
426 vtkDataObject* GetLayerDataObject(int layer, std::string representationName = "");
427
430 void GetLayerObjects(vtkCollection* layerObjects, std::string representationName = "");
431
434 std::vector<std::string> GetSegmentIDsForLayer(int layer, std::string representationName = "");
435
438 std::vector<std::string> GetSegmentIDsForDataObject(vtkDataObject* dataObject, std::string representationName = "");
439
443 void CollapseBinaryLabelmaps(bool forceToSingleLayer = false);
444
445 // Conversion related methods
446
457 bool CreateRepresentation(const std::string& targetRepresentationName, bool alwaysConvert = false);
458
465
467 void RemoveRepresentation(const std::string& representationName);
468
473 bool CanAcceptRepresentation(std::string representationName);
474
478
484 std::string AddEmptySegment(std::string segmentId = "", std::string segmentName = "", double color[3] = nullptr);
485
487 void GetPossibleConversions(const std::string& targetRepresentationName, vtkSegmentationConversionPaths* paths);
488
490 void SetConversionParameter(const std::string& name, const std::string& value) { this->Converter->SetConversionParameter(name, value); };
491
494 std::string GetConversionParameter(const std::string& name) { return this->Converter->GetConversionParameter(name); };
495
498 {
499 this->Converter->GetConversionParametersForPath(conversionParameters, path);
500 };
501
505
508 void DeserializeConversionParameters(std::string conversionParametersString);
509
510 // Get/set methods
511
513 vtkGetMacro(SourceRepresentationName, std::string);
517 virtual void SetSourceRepresentationName(const std::string& representationName);
518
520 virtual void SetMasterRepresentationName(const std::string& representationName)
521 {
522 vtkWarningMacro("vtkSegmentation::SetMasterRepresentationName() method is deprecated, please use SetSourceRepresentationName method instead");
523 this->SetSourceRepresentationName(representationName);
524 };
525
527 virtual std::string GetMasterRepresentationName()
528 {
529 vtkWarningMacro("vtkSegmentation::GetMasterRepresentationName() method is deprecated, please use GetSourceRepresentationName method instead");
530 return this->GetSourceRepresentationName();
531 };
532
535 static void CopySegment(vtkSegment* destination, vtkSegment* source, vtkSegment* baseline, std::map<vtkDataObject*, vtkDataObject*>& cachedRepresentations);
536
537 vtkSetMacro(UUIDSegmentIDs, bool);
538 vtkGetMacro(UUIDSegmentIDs, bool);
539 vtkBooleanMacro(UUIDSegmentIDs, bool);
540
541 static vtkMinimalStandardRandomSequence* GetSegmentIDRandomSequenceInstance();
542
543protected:
544 bool ConvertSegmentsUsingPath(std::vector<std::string> segmentIDs, vtkSegmentationConversionPath* path, bool overwriteExisting = false);
545
552 bool ConvertSegmentUsingPath(vtkSegment* segment, vtkSegmentationConversionPath* path, bool overwriteExisting = false);
553
555 bool ConvertSingleSegment(std::string segmentId, std::string targetRepresentationName);
556
559 void RemoveSegment(SegmentMap::iterator segmentIt);
560
566
571 bool SetSegmentModifiedEnabled(bool enabled);
572
573protected:
576 static void OnSegmentModified(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
577
580 static void OnSourceRepresentationModified(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
581
585
588 static std::string GenerateUUIDDerivedUID();
589
593 static std::string GenerateRandomSegmentID(int suffixLength, std::string validCharacters = "");
594
600 std::string GenerateUniqueSegmentName(std::string base);
601
602protected:
605
606protected:
609
615
618
620 vtkCallbackCommand* SegmentCallbackCommand;
621
624
627
630
634
638 std::deque<std::string> SegmentIds;
639
640 std::set<vtkSmartPointer<vtkDataObject>> SourceRepresentationCache;
641
643
646
648 static void classInitialize();
649 static void classFinalize();
650
655
656private:
657 vtkSegmentation(const vtkSegmentation&) = delete;
658 void operator=(const vtkSegmentation&) = delete;
659};
660
661VTK_SINGLETON_DECLARE_INITIALIZER(vtkSegmentationCore_EXPORT, vtkSegmentationRandomSequence);
662
663#endif
Image data containing orientation information.
This class encapsulates a segment that is part of a segmentation.
Definition vtkSegment.h:45
Store a segmentation conversion path.
Store multiple segmentation conversion paths.
Class that can convert between different representations of a segment.
bool CopySegmentFromSegmentation(vtkSegmentation *fromSegmentation, std::string segmentId, bool removeFromSource=false)
friend class vtkSegmentationRandomSequenceInitialize
Singleton class managing vtkMinimalStandardRandomSequence used for randomizing segment IDs.
int GetLayerIndex(std::string segmentId, std::string representationName="")
void PrintSelf(ostream &os, vtkIndent indent) override
vtkCallbackCommand * SegmentCallbackCommand
Command handling segment modified events.
void RemoveSegment(std::string segmentId)
void InvalidateNonSourceRepresentations()
Invalidate (remove) non-source representations in all the segments if this segmentation node.
std::string DetermineCommonLabelmapGeometry(int extentComputationMode, vtkStringArray *segmentIds)
Determine common labelmap geometry for whole segmentation, for python compatibility.
std::string SerializeAllConversionParameters()
~vtkSegmentation() override
bool SetSegmentIndex(const std::string &segmentId, unsigned int newIndex)
void SeparateSegmentLabelmap(std::string segmentId)
Moves an existing segment from a shared labelmap into a separate labelmap containing only the specifi...
bool SetSourceRepresentationModifiedEnabled(bool enabled)
void DeserializeConversionParameters(std::string conversionParametersString)
vtkSegmentationConverter * Converter
Converter instance.
bool CanAcceptRepresentation(std::string representationName)
virtual void WriteXML(ostream &of, int indent)
Write this object's information to a MRML file in XML format.
vtkSegment * GetSegment(std::string segmentId)
static void CopySegment(vtkSegment *destination, vtkSegment *source, vtkSegment *baseline, std::map< vtkDataObject *, vtkDataObject * > &cachedRepresentations)
bool CreateRepresentation(const std::string &targetRepresentationName, bool alwaysConvert=false)
std::deque< std::string > SegmentIds
static void OnSegmentModified(vtkObject *caller, unsigned long eid, void *clientData, void *callData)
void RemoveSegment(vtkSegment *segment)
friend class vtkSegmentationModifier
friend class vtkMRMLSegmentationNode
std::string GetNthSegmentID(unsigned int index) const
Get n-th segment ID. Return with "" if no segment is found by that index.
int GetNumberOfLayers(std::string representationName="")
Shared representation layer functions.
static std::string GenerateRandomSegmentID(int suffixLength, std::string validCharacters="")
bool IsMasterRepresentationImageData()
int GetUniqueLabelValueForSharedLabelmap(vtkOrientedImageData *labelmap)
bool SetSegmentModifiedEnabled(bool enabled)
void CollapseBinaryLabelmaps(bool forceToSingleLayer=false)
int GetSegmentIndex(const std::string &segmentId)
friend class vtkSlicerSegmentationsModuleLogic
bool ConvertSegmentUsingPath(vtkSegment *segment, vtkSegmentationConversionPath *path, bool overwriteExisting=false)
void GetContainedRepresentationNames(std::vector< std::string > &representationNames)
virtual void GetBounds(double bounds[6])
Get bounding box in global RAS in the form (xmin,xmax, ymin,ymax, zmin,zmax).
static std::string GenerateUUIDDerivedUID()
bool CreateRepresentation(vtkSegmentationConversionPath *path, vtkSegmentationConversionParameters *parameters)
std::vector< std::string > GetSegmentIDs()
Get IDs for all contained segments, for python compatibility.
virtual void SetSourceRepresentationName(const std::string &representationName)
bool CanAcceptSegment(vtkSegment *segment)
void DetermineCommonLabelmapExtent(int commonGeometryExtent[6], vtkOrientedImageData *commonGeometryImage, const std::vector< std::string > &segmentIDs=std::vector< std::string >(), bool computeEffectiveExtent=false, bool addPadding=false)
bool IsSharedBinaryLabelmap(std::string segmentID)
Shared labelmap utility functions.
static vtkMinimalStandardRandomSequence * GetSegmentIDRandomSequenceInstance()
virtual std::string GetMasterRepresentationName()
std::set< vtkSmartPointer< vtkDataObject > > SourceRepresentationCache
void GetSegmentIDs(vtkStringArray *segmentIds)
Get IDs for all contained segments, for python compatibility.
void ClearSegment(std::string segmentId)
std::vector< std::string > GetSegmentIDsForLayer(int layer, std::string representationName="")
bool SegmentModifiedEnabled
Modified events of segments are observed.
std::map< std::string, vtkSmartPointer< vtkSegment > > SegmentMap
Container type for segments. Maps segment IDs to segment objects.
int GetUniqueLabelValueForSharedLabelmap(std::string segmentId)
virtual void ApplyNonLinearTransform(vtkAbstractTransform *transform)
void RemoveRepresentation(const std::string &representationName)
Removes a representation from all segments if present.
bool IsMasterRepresentationPolyData()
vtkSegment * GetNthSegment(unsigned int index) const
Request segment by index.
std::string GetSegmentIdBySegment(vtkSegment *segment)
bool ConvertSegmentsUsingPath(std::vector< std::string > segmentIDs, vtkSegmentationConversionPath *path, bool overwriteExisting=false)
std::string DetermineCommonLabelmapGeometry(int extentComputationMode=EXTENT_UNION_OF_SEGMENTS, const std::vector< std::string > &segmentIDs=std::vector< std::string >())
vtkDataObject * GetLayerDataObject(int layer, std::string representationName="")
virtual void ApplyLinearTransform(vtkAbstractTransform *transform)
bool SourceRepresentationModifiedEnabled
Modified events of source representations are observed.
@ SegmentsOrderModified
Invoked if segment IDs order is changed. Not called when a segment is added or removed.
@ RepresentationModified
Invoked when content of any representation (including the source representation) in a segment is chan...
@ SegmentAdded
Invoked if new segment is added.
@ SegmentRemoved
Invoked if a segment is removed.
@ SourceRepresentationModified
Invoked when content of the source representation in a segment is changed.
@ ContainedRepresentationNamesModified
Invoked if a representation is created or removed in the segments (e.g., created by conversion from m...
void GetSegmentIDsSharingRepresentation(std::string originalSegmentId, std::string representationName, std::vector< std::string > &sharedSegmentIds, bool includeOriginalSegmentId=true)
friend class qMRMLSegmentEditorWidgetPrivate
void GetSegmentIDsSharingBinaryLabelmapRepresentation(std::string originalSegmentId, std::vector< std::string > &sharedSegmentIds, bool includeOriginalSegmentId=true)
bool GenerateMergedLabelmap(vtkOrientedImageData *mergedImageData, int extentComputationMode, vtkOrientedImageData *mergedLabelmapGeometry=nullptr, const std::vector< std::string > &segmentIDs=std::vector< std::string >(), vtkIntArray *labelValues=nullptr)
Merged labelmap functions.
std::string SourceRepresentationName
void InvalidateNonMasterRepresentations()
std::string GenerateUniqueSegmentID(std::string id="")
void RemoveSegment(SegmentMap::iterator segmentIt)
vtkDataObject * GetSegmentRepresentation(std::string segmentId, std::string representationName)
Get representation from segment.
static vtkSegmentation * New()
std::vector< std::string > GetSegmentIDsForDataObject(vtkDataObject *dataObject, std::string representationName="")
std::string AddEmptySegment(std::string segmentId="", std::string segmentName="", double color[3]=nullptr)
void GetConversionParametersForPath(vtkSegmentationConversionParameters *conversionParameters, vtkSegmentationConversionPath *path)
Get names of all conversion parameters used by the selected conversion path.
bool ConvertSingleSegment(std::string segmentId, std::string targetRepresentationName)
Converts a single segment to a representation.
void UpdateSourceRepresentationObservers()
static void OnSourceRepresentationModified(vtkObject *caller, unsigned long eid, void *clientData, void *callData)
bool AddSegment(vtkSegment *segment, std::string segmentId="", std::string insertBeforeSegmentId="")
virtual void DeepCopy(vtkSegmentation *aSegmentation)
Deep copy one segmentation into another.
void GetSegmentIDs(std::vector< std::string > &segmentIds)
Get IDs for all contained segments.
bool IsSourceRepresentationPolyData()
Determine if source representation is poly data type.
bool ContainsRepresentation(std::string representationName)
virtual void SetMasterRepresentationName(const std::string &representationName)
void GetPossibleConversions(const std::string &targetRepresentationName, vtkSegmentationConversionPaths *paths)
Get all possible conversions between the source representation and a specified target representation.
bool IsSourceRepresentationImageData()
Determine if source representation is (oriented) image data type.
virtual void CopyConversionParameters(vtkSegmentation *aSegmentation)
Copy conversion parameters from another segmentation.
void ReorderSegments(std::vector< std::string > segmentIdsToMove, std::string insertBeforeSegmentId="")
@ EXTENT_UNION_OF_EFFECTIVE_SEGMENTS_AND_REFERENCE_GEOMETRY
Extent is computed as the reference geometry, expanded to contain the union of all segments.
@ EXTENT_UNION_OF_SEGMENTS_PADDED
Extent is computed as union of extent of all segments, with a single-voxel padding added on each side...
@ EXTENT_UNION_OF_EFFECTIVE_SEGMENTS
Extent is computed as union of effective extent of all segments.
@ EXTENT_REFERENCE_GEOMETRY
Extent of common geometry is used as extent.
@ EXTENT_UNION_OF_SEGMENTS
Extent is computed as union of extent of all segments.
@ EXTENT_UNION_OF_EFFECTIVE_SEGMENTS_PADDED
Extent is computed as union of effective extent of all segments, with a single-voxel padding added on...
vtkCallbackCommand * SourceRepresentationCallbackCommand
Command handling source representation modified events.
virtual std::string GetSourceRepresentationName()
Get source representation name.
std::vector< vtkSegment * > GetSegmentsByTag(std::string tag, std::string value="")
std::string GetSegmentIdBySegmentName(std::string name)
virtual void ReadXMLAttributes(const char **atts)
Set attributes from name/value pairs.
static void classFinalize()
void SetConversionParameter(const std::string &name, const std::string &value)
Set a conversion parameter to all rules having this parameter.
void DetermineCommonLabelmapExtent(int commonGeometryExtent[6], vtkOrientedImageData *commonGeometryImage, vtkStringArray *segmentIds, bool computeEffectiveExtent=false, bool addPadding=false)
Determine common labelmap extent for whole segmentation, for python compatibility.
void GetAvailableRepresentationNames(std::set< std::string > &representationNames)
Get all representations supported by the converter.
int GetNumberOfSegments() const
Request the total number of segments, primarily used for iterating over all segments.
void GetLayerObjects(vtkCollection *layerObjects, std::string representationName="")
void MergeSegmentLabelmaps(std::vector< std::string > mergeSegmentIds)
std::string GetConversionParameter(const std::string &name)
bool SetImageGeometryFromCommonLabelmapGeometry(vtkOrientedImageData *imageData, vtkStringArray *segmentIDs=nullptr, int extentComputationMode=vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS)
void RemoveAllSegments()
Remove all segments.
static void classInitialize()
Singleton management functions.
SegmentMap Segments
Container of segments that belong to this segmentation.
std::string GenerateUniqueSegmentName(std::string base)
VTK_SINGLETON_DECLARE_INITIALIZER(vtkSegmentationCore_EXPORT, vtkSegmentationRandomSequence)