Slicer  5.3
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;
45 class vtkStringArray;
46 
93 class vtkSegmentationCore_EXPORT vtkSegmentation : public vtkObject
94 {
95 public:
96  enum
97  {
99  MasterRepresentationModified = 62100,
113  };
114 
115  enum
116  {
128  EXTENT_UNION_OF_EFFECTIVE_SEGMENTS_AND_REFERENCE_GEOMETRY
129  };
130 
132  typedef std::map<std::string, vtkSmartPointer<vtkSegment> > SegmentMap;
133 
134 public:
135  static vtkSegmentation* New();
136  vtkTypeMacro(vtkSegmentation, vtkObject);
137  void PrintSelf(ostream& os, vtkIndent indent) override;
138 
140  virtual void ReadXMLAttributes(const char** atts);
141 
143  virtual void WriteXML(ostream& of, int indent);
144 
146  virtual void DeepCopy(vtkSegmentation* aSegmentation);
147 
149  virtual void CopyConversionParameters(vtkSegmentation* aSegmentation);
150 
152  virtual void GetBounds(double bounds[6]);
153 
156  virtual void ApplyLinearTransform(vtkAbstractTransform* transform);
157 
160  virtual void ApplyNonLinearTransform(vtkAbstractTransform* transform);
161 
162 #ifndef __VTK_WRAP__
163  std::string DetermineCommonLabelmapGeometry(int extentComputationMode = EXTENT_UNION_OF_SEGMENTS, const std::vector<std::string>& segmentIDs = std::vector<std::string>());
172 
178  void DetermineCommonLabelmapExtent(int commonGeometryExtent[6], vtkOrientedImageData* commonGeometryImage,
179  const std::vector<std::string>& segmentIDs = std::vector<std::string>(), bool computeEffectiveExtent=false, bool addPadding=false);
180 #endif // __VTK_WRAP__
181 
183  std::string DetermineCommonLabelmapGeometry(int extentComputationMode, vtkStringArray* segmentIds);
184 
186  void DetermineCommonLabelmapExtent(int commonGeometryExtent[6], vtkOrientedImageData* commonGeometryImage,
187  vtkStringArray* segmentIds, bool computeEffectiveExtent=false, bool addPadding=false);
188 
191  bool SetImageGeometryFromCommonLabelmapGeometry(vtkOrientedImageData* imageData, vtkStringArray* segmentIDs = nullptr,
192  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS);
193 
194 // Segment related methods
195 
208  bool AddSegment(vtkSegment* segment, std::string segmentId = "", std::string insertBeforeSegmentId = "");
209 
213  std::string GenerateUniqueSegmentID(std::string id);
214 
217  void RemoveSegment(std::string segmentId);
218 
221  void RemoveSegment(vtkSegment* segment);
222 
224  void RemoveAllSegments();
225 
228  vtkSegment* GetSegment(std::string segmentId);
229 
231  void GetSegmentIDs(std::vector<std::string> &segmentIds);
232 
234  void GetSegmentIDs(vtkStringArray* segmentIds);
235 
237  std::vector<std::string> GetSegmentIDs();
238 
240  int GetNumberOfSegments() const;
241 
243  vtkSegment* GetNthSegment(unsigned int index) const;
244 
246  std::string GetNthSegmentID(unsigned int index) const;
247 
251  int GetSegmentIndex(const std::string& segmentId);
252 
256  bool SetSegmentIndex(const std::string& segmentId, unsigned int newIndex);
257 
262  void ReorderSegments(std::vector<std::string> segmentIdsToMove, std::string insertBeforeSegmentId = "");
263 
266  std::string GetSegmentIdBySegment(vtkSegment* segment);
267 
271  std::string GetSegmentIdBySegmentName(std::string name);
272 
277  std::vector<vtkSegment*> GetSegmentsByTag(std::string tag, std::string value="");
278 
280  vtkDataObject* GetSegmentRepresentation(std::string segmentId, std::string representationName);
281 
288  bool CopySegmentFromSegmentation(vtkSegmentation* fromSegmentation, std::string segmentId, bool removeFromSource=false);
289 
290 // Representation related methods
291 
295  void GetContainedRepresentationNames(std::vector<std::string>& representationNames);
296 
300  bool ContainsRepresentation(std::string representationName);
301 
303  bool IsMasterRepresentationPolyData();
304 
306  bool IsMasterRepresentationImageData();
307 
309  void GetAvailableRepresentationNames(std::set<std::string>& representationNames) { this->Converter->GetAvailableRepresentationNames(representationNames); };
310 
312  void InvalidateNonMasterRepresentations();
313 
315 
316 #ifndef __VTK_WRAP__
317  bool GenerateMergedLabelmap(vtkOrientedImageData* mergedImageData, int extentComputationMode, vtkOrientedImageData* mergedLabelmapGeometry = nullptr,
330  const std::vector<std::string>& segmentIDs = std::vector<std::string>(), vtkIntArray* labelValues = nullptr);
331 #endif // __VTK_WRAP__
332 
334 
336  bool IsSharedBinaryLabelmap(std::string segmentID);
337 
343  void GetSegmentIDsSharingRepresentation(std::string originalSegmentId, std::string representationName,
344  std::vector<std::string>& sharedSegmentIds, bool includeOriginalSegmentId=true);
345 
350  void GetSegmentIDsSharingBinaryLabelmapRepresentation(std::string originalSegmentId, std::vector<std::string> &sharedSegmentIds,
351  bool includeOriginalSegmentId=true);
352 
356  int GetUniqueLabelValueForSharedLabelmap(std::string segmentId);
357 
360  int GetUniqueLabelValueForSharedLabelmap(vtkOrientedImageData* labelmap);
361 
366  void MergeSegmentLabelmaps(std::vector<std::string> mergeSegmentIds);
367 
369  void SeparateSegmentLabelmap(std::string segmentId);
370 
374  void ClearSegment(std::string segmentId);
375 
377 
380  int GetNumberOfLayers(std::string representationName="");
381 
384  int GetLayerIndex(std::string segmentId, std::string representationName="");
385 
388  vtkDataObject* GetLayerDataObject(int layer, std::string representationName="");
389 
392  void GetLayerObjects(vtkCollection* layerObjects, std::string representationName = "");
393 
396  std::vector<std::string> GetSegmentIDsForLayer(int layer, std::string representationName = "");
397 
400  std::vector<std::string> GetSegmentIDsForDataObject(vtkDataObject* dataObject, std::string representationName = "");
401 
405  void CollapseBinaryLabelmaps(bool forceToSingleLayer=false);
406 
407  // Conversion related methods
408 
419  bool CreateRepresentation(const std::string& targetRepresentationName, bool alwaysConvert=false);
420 
426  bool CreateRepresentation(vtkSegmentationConversionPath* path,
428 
430  void RemoveRepresentation(const std::string& representationName);
431 
436  bool CanAcceptRepresentation(std::string representationName);
437 
440  bool CanAcceptSegment(vtkSegment* segment);
441 
447  std::string AddEmptySegment(std::string segmentId="", std::string segmentName="", double color[3]=nullptr);
448 
450  void GetPossibleConversions(const std::string& targetRepresentationName,
452 
454  void SetConversionParameter(const std::string& name, const std::string& value) { this->Converter->SetConversionParameter(name, value); };
455 
458  std::string GetConversionParameter(const std::string& name) { return this->Converter->GetConversionParameter(name); };
459 
462  vtkSegmentationConversionPath* path) { this->Converter->GetConversionParametersForPath(conversionParameters, path); };
463 
466  std::string SerializeAllConversionParameters();
467 
470  void DeserializeConversionParameters(std::string conversionParametersString);
471 
472 // Get/set methods
473 
475  vtkGetMacro(MasterRepresentationName, std::string);
479  virtual void SetMasterRepresentationName(const std::string& representationName);
480 
483  static void CopySegment(vtkSegment* destination, vtkSegment* source, vtkSegment* baseline,
484  std::map<vtkDataObject*, vtkDataObject*>& cachedRepresentations);
485 
486 protected:
487  bool ConvertSegmentsUsingPath(std::vector<std::string> segmentIDs, vtkSegmentationConversionPath* path, bool overwriteExisting = false);
488 
495  bool ConvertSegmentUsingPath(vtkSegment* segment, vtkSegmentationConversionPath* path, bool overwriteExisting = false);
496 
498  bool ConvertSingleSegment(std::string segmentId, std::string targetRepresentationName);
499 
502  void RemoveSegment(SegmentMap::iterator segmentIt);
503 
508  bool SetMasterRepresentationModifiedEnabled(bool enabled);
509 
514  bool SetSegmentModifiedEnabled(bool enabled);
515 
516 protected:
519  static void OnSegmentModified(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
520 
523  static void OnMasterRepresentationModified(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
524 
527  void UpdateMasterRepresentationObservers();
528 
529 protected:
530  vtkSegmentation();
531  ~vtkSegmentation() override;
532 
533 protected:
536 
542 
545 
547  vtkCallbackCommand* SegmentCallbackCommand;
548 
551 
554 
557 
561 
565  std::deque< std::string > SegmentIds;
566 
567  std::set<vtkSmartPointer<vtkDataObject> > MasterRepresentationCache;
568 
572  friend class qMRMLSegmentEditorWidgetPrivate;
573 
574 private:
575  vtkSegmentation(const vtkSegmentation&) = delete;
576  void operator=(const vtkSegmentation&) = delete;
577 };
578 
579 #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.
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.
Image data containing orientation information.
vtkCallbackCommand * MasterRepresentationCallbackCommand
Command handling master representation modified events.
std::deque< std::string > SegmentIds
Invoked if new segment is added.
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...
Store a list of conversion parameters.
Store multiple segmentation conversion paths.
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...
Store a segmentation conversion path.
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.
void GetConversionParametersForPath(vtkSegmentationConversionParameters *conversionParameters, vtkSegmentationConversionPath *path)
Get names of all conversion parameters used by the selected conversion path.
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.