Slicer  5.0
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
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 class vtkAbstractTransform;
41 class vtkCallbackCommand;
42 class vtkCollection;
43 class vtkIntArray;
44 class vtkStringArray;
45 
92 class vtkSegmentationCore_EXPORT vtkSegmentation : public vtkObject
93 {
94 public:
95  enum
96  {
98  MasterRepresentationModified = 62100,
112  };
113 
114  enum
115  {
127  EXTENT_UNION_OF_EFFECTIVE_SEGMENTS_AND_REFERENCE_GEOMETRY
128  };
129 
131  typedef std::map<std::string, vtkSmartPointer<vtkSegment> > SegmentMap;
132 
133 public:
134  static vtkSegmentation* New();
135  vtkTypeMacro(vtkSegmentation, vtkObject);
136  void PrintSelf(ostream& os, vtkIndent indent) override;
137 
139  virtual void ReadXMLAttributes(const char** atts);
140 
142  virtual void WriteXML(ostream& of, int indent);
143 
145  virtual void DeepCopy(vtkSegmentation* aSegmentation);
146 
148  virtual void CopyConversionParameters(vtkSegmentation* aSegmentation);
149 
151  virtual void GetBounds(double bounds[6]);
152 
155  virtual void ApplyLinearTransform(vtkAbstractTransform* transform);
156 
159  virtual void ApplyNonLinearTransform(vtkAbstractTransform* transform);
160 
161 #ifndef __VTK_WRAP__
162  std::string DetermineCommonLabelmapGeometry(int extentComputationMode = EXTENT_UNION_OF_SEGMENTS, const std::vector<std::string>& segmentIDs = std::vector<std::string>());
171 
177  void DetermineCommonLabelmapExtent(int commonGeometryExtent[6], vtkOrientedImageData* commonGeometryImage,
178  const std::vector<std::string>& segmentIDs = std::vector<std::string>(), bool computeEffectiveExtent=false, bool addPadding=false);
179 #endif // __VTK_WRAP__
180 
182  std::string DetermineCommonLabelmapGeometry(int extentComputationMode, vtkStringArray* segmentIds);
183 
185  void DetermineCommonLabelmapExtent(int commonGeometryExtent[6], vtkOrientedImageData* commonGeometryImage,
186  vtkStringArray* segmentIds, bool computeEffectiveExtent=false, bool addPadding=false);
187 
190  bool SetImageGeometryFromCommonLabelmapGeometry(vtkOrientedImageData* imageData, vtkStringArray* segmentIDs = nullptr,
191  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS);
192 
193 // Segment related methods
194 
207  bool AddSegment(vtkSegment* segment, std::string segmentId = "", std::string insertBeforeSegmentId = "");
208 
212  std::string GenerateUniqueSegmentID(std::string id);
213 
216  void RemoveSegment(std::string segmentId);
217 
220  void RemoveSegment(vtkSegment* segment);
221 
223  void RemoveAllSegments();
224 
227  vtkSegment* GetSegment(std::string segmentId);
228 
230  void GetSegmentIDs(std::vector<std::string> &segmentIds);
231 
233  void GetSegmentIDs(vtkStringArray* segmentIds);
234 
236  std::vector<std::string> GetSegmentIDs();
237 
239  int GetNumberOfSegments() const;
240 
242  vtkSegment* GetNthSegment(unsigned int index) const;
243 
245  std::string GetNthSegmentID(unsigned int index) const;
246 
250  int GetSegmentIndex(const std::string& segmentId);
251 
255  bool SetSegmentIndex(const std::string& segmentId, unsigned int newIndex);
256 
261  void ReorderSegments(std::vector<std::string> segmentIdsToMove, std::string insertBeforeSegmentId = "");
262 
265  std::string GetSegmentIdBySegment(vtkSegment* segment);
266 
270  std::string GetSegmentIdBySegmentName(std::string name);
271 
276  std::vector<vtkSegment*> GetSegmentsByTag(std::string tag, std::string value="");
277 
279  vtkDataObject* GetSegmentRepresentation(std::string segmentId, std::string representationName);
280 
287  bool CopySegmentFromSegmentation(vtkSegmentation* fromSegmentation, std::string segmentId, bool removeFromSource=false);
288 
289 // Representation related methods
290 
294  void GetContainedRepresentationNames(std::vector<std::string>& representationNames);
295 
299  bool ContainsRepresentation(std::string representationName);
300 
302  bool IsMasterRepresentationPolyData();
303 
305  bool IsMasterRepresentationImageData();
306 
308  void GetAvailableRepresentationNames(std::set<std::string>& representationNames) { this->Converter->GetAvailableRepresentationNames(representationNames); };
309 
311  void InvalidateNonMasterRepresentations();
312 
314 
315 #ifndef __VTK_WRAP__
316  bool GenerateMergedLabelmap(vtkOrientedImageData* mergedImageData, int extentComputationMode, vtkOrientedImageData* mergedLabelmapGeometry = nullptr,
329  const std::vector<std::string>& segmentIDs = std::vector<std::string>(), vtkIntArray* labelValues = nullptr);
330 #endif // __VTK_WRAP__
331 
333 
335  bool IsSharedBinaryLabelmap(std::string segmentID);
336 
342  void GetSegmentIDsSharingRepresentation(std::string originalSegmentId, std::string representationName,
343  std::vector<std::string>& sharedSegmentIds, bool includeOriginalSegmentId=true);
344 
349  void GetSegmentIDsSharingBinaryLabelmapRepresentation(std::string originalSegmentId, std::vector<std::string> &sharedSegmentIds,
350  bool includeOriginalSegmentId=true);
351 
355  int GetUniqueLabelValueForSharedLabelmap(std::string segmentId);
356 
359  int GetUniqueLabelValueForSharedLabelmap(vtkOrientedImageData* labelmap);
360 
365  void MergeSegmentLabelmaps(std::vector<std::string> mergeSegmentIds);
366 
368  void SeparateSegmentLabelmap(std::string segmentId);
369 
373  void ClearSegment(std::string segmentId);
374 
376 
379  int GetNumberOfLayers(std::string representationName="");
380 
383  int GetLayerIndex(std::string segmentId, std::string representationName="");
384 
387  vtkDataObject* GetLayerDataObject(int layer, std::string representationName="");
388 
391  void GetLayerObjects(vtkCollection* layerObjects, std::string representationName = "");
392 
395  std::vector<std::string> GetSegmentIDsForLayer(int layer, std::string representationName = "");
396 
399  std::vector<std::string> GetSegmentIDsForDataObject(vtkDataObject* dataObject, std::string representationName = "");
400 
404  void CollapseBinaryLabelmaps(bool forceToSingleLayer=false);
405 
406  // Conversion related methods
407 
418  bool CreateRepresentation(const std::string& targetRepresentationName, bool alwaysConvert=false);
419 
425  bool CreateRepresentation(vtkSegmentationConverter::ConversionPathType path,
427 
429  void RemoveRepresentation(const std::string& representationName);
430 
435  bool CanAcceptRepresentation(std::string representationName);
436 
439  bool CanAcceptSegment(vtkSegment* segment);
440 
446  std::string AddEmptySegment(std::string segmentId="", std::string segmentName="", double color[3]=nullptr);
447 
449  void GetPossibleConversions(const std::string& targetRepresentationName,
451 
453  void SetConversionParameter(const std::string& name, const std::string& value) { this->Converter->SetConversionParameter(name, value); };
454 
457  std::string GetConversionParameter(const std::string& name) { return this->Converter->GetConversionParameter(name); };
458 
461  const vtkSegmentationConverter::ConversionPathType& path) { this->Converter->GetConversionParametersForPath(conversionParameters, path); };
462 
465  std::string SerializeAllConversionParameters();
466 
469  void DeserializeConversionParameters(std::string conversionParametersString);
470 
471 // Get/set methods
472 
474  vtkGetMacro(MasterRepresentationName, std::string);
478  virtual void SetMasterRepresentationName(const std::string& representationName);
479 
482  static void CopySegment(vtkSegment* destination, vtkSegment* source, vtkSegment* baseline,
483  std::map<vtkDataObject*, vtkDataObject*>& cachedRepresentations);
484 
485 protected:
486  bool ConvertSegmentsUsingPath(std::vector<std::string> segmentIDs, vtkSegmentationConverter::ConversionPathType path, bool overwriteExisting = false);
487 
494  bool ConvertSegmentUsingPath(vtkSegment* segment, vtkSegmentationConverter::ConversionPathType path, bool overwriteExisting = false);
495 
497  bool ConvertSingleSegment(std::string segmentId, std::string targetRepresentationName);
498 
501  void RemoveSegment(SegmentMap::iterator segmentIt);
502 
507  bool SetMasterRepresentationModifiedEnabled(bool enabled);
508 
513  bool SetSegmentModifiedEnabled(bool enabled);
514 
515 protected:
518  static void OnSegmentModified(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
519 
522  static void OnMasterRepresentationModified(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
523 
526  void UpdateMasterRepresentationObservers();
527 
528 protected:
529  vtkSegmentation();
530  ~vtkSegmentation() override;
531 
532 protected:
535 
541 
544 
546  vtkCallbackCommand* SegmentCallbackCommand;
547 
550 
553 
556 
560 
564  std::deque< std::string > SegmentIds;
565 
566  std::set<vtkSmartPointer<vtkDataObject> > MasterRepresentationCache;
567 
571  friend class qMRMLSegmentEditorWidgetPrivate;
572 
573 private:
574  vtkSegmentation(const vtkSegmentation&) = delete;
575  void operator=(const vtkSegmentation&) = delete;
576 };
577 
578 #endif // __vtkSegmentation_h
Invoked if a representation is created or removed in the segments (e.g., created by conversion from m...
This class encapsulates a segment that is part of a segmentation.
Definition: vtkSegment.h:45
void GetAvailableRepresentationNames(std::set< std::string > &representationNames)
Get all representations supported by the converter.
std::vector< ConversionPathAndCostType > ConversionPathAndCostListType
Extent is computed as union of extent of all segments, with a single-voxel padding added on each side...
std::set< vtkSmartPointer< vtkDataObject > > MasterRepresentationCache
bool MasterRepresentationModifiedEnabled
Modified events of master representations are observed.
void GetConversionParametersForPath(vtkSegmentationConverterRule::ConversionParameterListType &conversionParameters, const vtkSegmentationConverter::ConversionPathType &path)
Get names of all conversion parameters used by the selected conversion path.
Image data containing orientation information.
vtkCallbackCommand * MasterRepresentationCallbackCommand
Command handling master representation modified events.
std::deque< std::string > SegmentIds
Invoked if new segment is added.
std::vector< vtkSegmentationConverterRule * > ConversionPathType
vtkCallbackCommand * SegmentCallbackCommand
Command handling segment modified events.
bool SegmentModifiedEnabled
Modified events of segments are observed.
Invoked if segment IDs order is changed. Not called when a segment is added or removed.
This class encapsulates a segmentation that can contain multiple segments and multiple representation...
std::map< std::string, std::pair< std::string, std::string > > ConversionParameterListType
std::string MasterRepresentationName
std::string GetConversionParameter(const std::string &name)
Extent is computed as union of extent of all segments.
Extent is computed as union of effective extent of all segments.
Invoked when content of any representation (including the master representation) in a segment is chan...
Extent is computed as union of effective extent of all segments, with a single-voxel padding added on...
Invoked if a segment is removed.
void SetConversionParameter(const std::string &name, const std::string &value)
Set a conversion parameter to all rules having this parameter.
Utility functions for resampling oriented image data.
vtkSegmentationConverter * Converter
Converter instance.
std::map< std::string, vtkSmartPointer< vtkSegment > > SegmentMap
Container type for segments. Maps segment IDs to segment objects.
SegmentMap Segments
Container of segments that belong to this segmentation.
Extent of common geometry is used as extent.
MRML node containing segmentationsSegmentation node stores a set of segments (also known as contours ...
Class that can convert between different representations of a segment.