Slicer  4.11
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  {
125  EXTENT_UNION_OF_EFFECTIVE_SEGMENTS_PADDED
126  };
127 
129  typedef std::map<std::string, vtkSmartPointer<vtkSegment> > SegmentMap;
130 
131 public:
132  static vtkSegmentation* New();
133  vtkTypeMacro(vtkSegmentation, vtkObject);
134  void PrintSelf(ostream& os, vtkIndent indent) override;
135 
137  virtual void ReadXMLAttributes(const char** atts);
138 
140  virtual void WriteXML(ostream& of, int indent);
141 
143  virtual void DeepCopy(vtkSegmentation* aSegmentation);
144 
146  virtual void CopyConversionParameters(vtkSegmentation* aSegmentation);
147 
149  virtual void GetBounds(double bounds[6]);
150 
153  virtual void ApplyLinearTransform(vtkAbstractTransform* transform);
154 
157  virtual void ApplyNonLinearTransform(vtkAbstractTransform* transform);
158 
159 #ifndef __VTK_WRAP__
160  std::string DetermineCommonLabelmapGeometry(int extentComputationMode = EXTENT_UNION_OF_SEGMENTS, const std::vector<std::string>& segmentIDs = std::vector<std::string>());
169 
175  void DetermineCommonLabelmapExtent(int commonGeometryExtent[6], vtkOrientedImageData* commonGeometryImage,
176  const std::vector<std::string>& segmentIDs = std::vector<std::string>(), bool computeEffectiveExtent=false, bool addPadding=false);
177 #endif // __VTK_WRAP__
178 
180  std::string DetermineCommonLabelmapGeometry(int extentComputationMode, vtkStringArray* segmentIds);
181 
183  void DetermineCommonLabelmapExtent(int commonGeometryExtent[6], vtkOrientedImageData* commonGeometryImage,
184  vtkStringArray* segmentIds, bool computeEffectiveExtent=false, bool addPadding=false);
185 
188  bool SetImageGeometryFromCommonLabelmapGeometry(vtkOrientedImageData* imageData, vtkStringArray* segmentIDs = nullptr,
189  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS);
190 
191 // Segment related methods
192 
205  bool AddSegment(vtkSegment* segment, std::string segmentId = "", std::string insertBeforeSegmentId = "");
206 
210  std::string GenerateUniqueSegmentID(std::string id);
211 
214  void RemoveSegment(std::string segmentId);
215 
218  void RemoveSegment(vtkSegment* segment);
219 
221  void RemoveAllSegments();
222 
225  vtkSegment* GetSegment(std::string segmentId);
226 
228  void GetSegmentIDs(std::vector<std::string> &segmentIds);
229 
231  void GetSegmentIDs(vtkStringArray* segmentIds);
232 
234  int GetNumberOfSegments() const;
235 
237  vtkSegment* GetNthSegment(unsigned int index) const;
238 
240  std::string GetNthSegmentID(unsigned int index) const;
241 
245  int GetSegmentIndex(const std::string& segmentId);
246 
250  bool SetSegmentIndex(const std::string& segmentId, unsigned int newIndex);
251 
256  void ReorderSegments(std::vector<std::string> segmentIdsToMove, std::string insertBeforeSegmentId = "");
257 
260  std::string GetSegmentIdBySegment(vtkSegment* segment);
261 
265  std::string GetSegmentIdBySegmentName(std::string name);
266 
271  std::vector<vtkSegment*> GetSegmentsByTag(std::string tag, std::string value="");
272 
274  vtkDataObject* GetSegmentRepresentation(std::string segmentId, std::string representationName);
275 
282  bool CopySegmentFromSegmentation(vtkSegmentation* fromSegmentation, std::string segmentId, bool removeFromSource=false);
283 
284 // Representation related methods
285 
289  void GetContainedRepresentationNames(std::vector<std::string>& representationNames);
290 
294  bool ContainsRepresentation(std::string representationName);
295 
297  bool IsMasterRepresentationPolyData();
298 
300  bool IsMasterRepresentationImageData();
301 
303  void GetAvailableRepresentationNames(std::set<std::string>& representationNames) { this->Converter->GetAvailableRepresentationNames(representationNames); };
304 
306  void InvalidateNonMasterRepresentations();
307 
309 
310 #ifndef __VTK_WRAP__
311  bool GenerateMergedLabelmap(vtkOrientedImageData* mergedImageData, int extentComputationMode, vtkOrientedImageData* mergedLabelmapGeometry = nullptr,
324  const std::vector<std::string>& segmentIDs = std::vector<std::string>(), vtkIntArray* labelValues = nullptr);
325 #endif // __VTK_WRAP__
326 
328 
330  bool IsSharedBinaryLabelmap(std::string segmentID);
331 
337  void GetSegmentIDsSharingRepresentation(std::string originalSegmentId, std::string representationName,
338  std::vector<std::string>& sharedSegmentIds, bool includeOriginalSegmentId=true);
339 
344  void GetSegmentIDsSharingBinaryLabelmapRepresentation(std::string originalSegmentId, std::vector<std::string> &sharedSegmentIds,
345  bool includeOriginalSegmentId=true);
346 
350  int GetUniqueLabelValueForSharedLabelmap(std::string segmentId);
351 
354  int GetUniqueLabelValueForSharedLabelmap(vtkOrientedImageData* labelmap);
355 
360  void MergeSegmentLabelmaps(std::vector<std::string> mergeSegmentIds);
361 
363  void SeparateSegmentLabelmap(std::string segmentId);
364 
368  void ClearSegment(std::string segmentId);
369 
371 
374  int GetNumberOfLayers(std::string representationName="");
375 
378  int GetLayerIndex(std::string segmentId, std::string representationName="");
379 
382  vtkDataObject* GetLayerDataObject(int layer, std::string representationName="");
383 
386  void GetLayerObjects(vtkCollection* layerObjects, std::string representationName = "");
387 
390  std::vector<std::string> GetSegmentIDsForLayer(int layer, std::string representationName = "");
391 
394  std::vector<std::string> GetSegmentIDsForDataObject(vtkDataObject* dataObject, std::string representationName = "");
395 
399  void CollapseBinaryLabelmaps(bool forceToSingleLayer=false);
400 
401  // Conversion related methods
402 
413  bool CreateRepresentation(const std::string& targetRepresentationName, bool alwaysConvert=false);
414 
420  bool CreateRepresentation(vtkSegmentationConverter::ConversionPathType path,
422 
424  void RemoveRepresentation(const std::string& representationName);
425 
430  bool CanAcceptRepresentation(std::string representationName);
431 
434  bool CanAcceptSegment(vtkSegment* segment);
435 
441  std::string AddEmptySegment(std::string segmentId="", std::string segmentName="", double color[3]=nullptr);
442 
444  void GetPossibleConversions(const std::string& targetRepresentationName,
446 
448  void SetConversionParameter(const std::string& name, const std::string& value) { this->Converter->SetConversionParameter(name, value); };
449 
452  std::string GetConversionParameter(const std::string& name) { return this->Converter->GetConversionParameter(name); };
453 
456  const vtkSegmentationConverter::ConversionPathType& path) { this->Converter->GetConversionParametersForPath(conversionParameters, path); };
457 
460  std::string SerializeAllConversionParameters();
461 
464  void DeserializeConversionParameters(std::string conversionParametersString);
465 
466 // Get/set methods
467 
469  vtkGetMacro(MasterRepresentationName, std::string);
473  virtual void SetMasterRepresentationName(const std::string& representationName);
474 
477  static void CopySegment(vtkSegment* destination, vtkSegment* source, vtkSegment* baseline,
478  std::map<vtkDataObject*, vtkDataObject*>& cachedRepresentations);
479 
480 protected:
481  bool ConvertSegmentsUsingPath(std::vector<std::string> segmentIDs, vtkSegmentationConverter::ConversionPathType path, bool overwriteExisting = false);
482 
489  bool ConvertSegmentUsingPath(vtkSegment* segment, vtkSegmentationConverter::ConversionPathType path, bool overwriteExisting = false);
490 
492  bool ConvertSingleSegment(std::string segmentId, std::string targetRepresentationName);
493 
496  void RemoveSegment(SegmentMap::iterator segmentIt);
497 
502  bool SetMasterRepresentationModifiedEnabled(bool enabled);
503 
508  bool SetSegmentModifiedEnabled(bool enabled);
509 
510 protected:
513  static void OnSegmentModified(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
514 
517  static void OnMasterRepresentationModified(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
518 
521  void UpdateMasterRepresentationObservers();
522 
523 protected:
524  vtkSegmentation();
525  ~vtkSegmentation() override;
526 
527 protected:
530 
536 
539 
541  vtkCallbackCommand* SegmentCallbackCommand;
542 
545 
548 
551 
555 
559  std::deque< std::string > SegmentIds;
560 
561  std::set<vtkSmartPointer<vtkDataObject> > MasterRepresentationCache;
562 
566  friend class qMRMLSegmentEditorWidgetPrivate;
567 
568 private:
569  vtkSegmentation(const vtkSegmentation&) = delete;
570  void operator=(const vtkSegmentation&) = delete;
571 };
572 
573 #endif // __vtkSegmentation_h
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...
Extent is computed as union of extent of all segments.
Invoked if segment IDs order is changed. Not called when a segment is added or removed.
std::vector< ConversionPathAndCostType > ConversionPathAndCostListType
Invoked when content of any representation (including the master representation) in a segment is chan...
std::set< vtkSmartPointer< vtkDataObject > > MasterRepresentationCache
Invoked if new segment is added.
bool MasterRepresentationModifiedEnabled
Modified events of master representations are observed.
Invoked if a representation is created or removed in the segments (e.g., created by conversion from m...
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.
Invoked if a segment is removed.
vtkCallbackCommand * MasterRepresentationCallbackCommand
Command handling master representation modified events.
std::deque< std::string > SegmentIds
std::vector< vtkSegmentationConverterRule * > ConversionPathType
vtkCallbackCommand * SegmentCallbackCommand
Command handling segment modified events.
bool SegmentModifiedEnabled
Modified events of segments are observed.
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
Extent of common geometry is used as extent.
std::string GetConversionParameter(const std::string &name)
Extent is computed as union of effective extent of all segments.
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.
MRML node containing segmentationsSegmentation node stores a set of segments (also known as contours ...
Class that can convert between different representations of a segment.