Slicer  5.1
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
vtkSlicerSegmentationsModuleLogic.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 // .NAME vtkSlicerSegmentationsModuleLogic - Logic class for segmentation handling
22 // .SECTION Description
23 // This class manages the logic associated with converting and handling
24 // segmentation node objects.
25 
26 #ifndef __vtkSlicerSegmentationsModuleLogic_h
27 #define __vtkSlicerSegmentationsModuleLogic_h
28 
29 // Slicer includes
30 #include "vtkSlicerModuleLogic.h"
31 #include "vtkSlicerSegmentationsModuleLogicExport.h"
32 
33 // Segmentations includes
35 
36 class vtkCallbackCommand;
38 class vtkPolyData;
39 class vtkDataObject;
40 class vtkGeneralTransform;
41 
45 class vtkMRMLVolumeNode;
46 class vtkMRMLModelNode;
48 
50 class VTK_SLICER_SEGMENTATIONS_LOGIC_EXPORT vtkSlicerSegmentationsModuleLogic :
52 {
53 public:
56  void PrintSelf(ostream& os, vtkIndent indent) override;
57 
63  static vtkMRMLSegmentationNode* GetSegmentationNodeForSegmentation(vtkMRMLScene* scene, vtkSegmentation* segmentation);
64 
71  static vtkMRMLSegmentationNode* GetSegmentationNodeForSegment(vtkMRMLScene* scene, vtkSegment* segment, std::string& segmentId);
72 
79  vtkMRMLSegmentationNode* LoadSegmentationFromFile(const char* filename, bool autoOpacities = true, const char* nodeName=nullptr,
80  vtkMRMLColorTableNode* colorTableNode=nullptr);
81 
88  static bool CreateLabelmapVolumeFromOrientedImageData(vtkOrientedImageData* orientedImageData, vtkMRMLLabelMapVolumeNode* labelmapVolumeNode);
89 
97  static bool CopyOrientedImageDataToVolumeNode(vtkOrientedImageData* orientedImageData,
98  vtkMRMLVolumeNode* volumeNode, bool shallowCopy = true, bool shiftImageDataExtentToZeroStart = true);
99 
103  static vtkOrientedImageData* CreateOrientedImageDataFromVolumeNode(vtkMRMLScalarVolumeNode* volumeNode, vtkMRMLTransformNode* outputParentTransformNode = nullptr);
104 
107  static int DoesLabelmapContainSingleLabel(vtkMRMLLabelMapVolumeNode* labelmapVolumeNode);
108 
110  static void GetAllLabelValues(vtkIntArray* labels, vtkImageData* labelmap);
111 
121  static vtkSegment* CreateSegmentFromLabelmapVolumeNode(vtkMRMLLabelMapVolumeNode* labelmapVolumeNode, vtkMRMLSegmentationNode* segmentationNode=nullptr);
122 
132  static vtkSegment* CreateSegmentFromModelNode(vtkMRMLModelNode* modelNode, vtkMRMLSegmentationNode* segmentationNode=nullptr);
133 
135  static vtkMRMLSegmentationNode* GetSegmentationNodeForSegmentSubjectHierarchyItem(vtkIdType segmentShItemID, vtkMRMLScene* scene);
136 
138  static vtkSegment* GetSegmentForSegmentSubjectHierarchyItem(vtkIdType segmentShItemID, vtkMRMLScene* scene);
139 
145  static bool ExportSegmentToRepresentationNode(vtkSegment* segment, vtkMRMLNode* representationNode);
146 
151  static bool ExportSegmentsToModels(vtkMRMLSegmentationNode* segmentationNode,
152  const std::vector<std::string>& segmentIDs, vtkIdType folderItemId);
153 
158  static bool ExportSegmentsToModels(vtkMRMLSegmentationNode* segmentationNode,
159  vtkStringArray* segmentIds, vtkIdType folderItemId);
160 
164  static bool ExportVisibleSegmentsToModels(vtkMRMLSegmentationNode* segmentationNode, vtkIdType folderItemId);
165 
169  static bool ExportAllSegmentsToModels(vtkMRMLSegmentationNode* segmentationNode, vtkIdType folderItemId);
170 
181  static bool ExportSegmentsToLabelmapNode(vtkMRMLSegmentationNode* segmentationNode, const std::vector<std::string>& segmentIDs,
182  vtkMRMLLabelMapVolumeNode* labelmapNode, vtkMRMLVolumeNode* referenceVolumeNode = nullptr,
183  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS, vtkMRMLColorTableNode* colorTableNode = nullptr);
184 
192  static bool ExportSegmentsToLabelmapNode(vtkMRMLSegmentationNode* segmentationNode, vtkStringArray* segmentIDs,
193  vtkMRMLLabelMapVolumeNode* labelmapNode, vtkMRMLVolumeNode* referenceVolumeNode = nullptr,
194  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS, vtkMRMLColorTableNode* colorTableNode = nullptr);
195 
202  static bool ExportVisibleSegmentsToLabelmapNode(vtkMRMLSegmentationNode* segmentationNode,
203  vtkMRMLLabelMapVolumeNode* labelmapNode, vtkMRMLVolumeNode* referenceVolumeNode = nullptr,
204  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS);
205 
211  static bool ExportAllSegmentsToLabelmapNode(vtkMRMLSegmentationNode* segmentationNode, vtkMRMLLabelMapVolumeNode* labelmapNode,
212  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS);
213 
217  static bool ImportLabelmapToSegmentationNode(vtkMRMLLabelMapVolumeNode* labelmapNode,
218  vtkMRMLSegmentationNode* segmentationNode, std::string insertBeforeSegmentId="");
219 
226  static bool ImportLabelmapToSegmentationNode(vtkOrientedImageData* labelmapImage,
227  vtkMRMLSegmentationNode* segmentationNode, std::string baseSegmentName="", std::string insertBeforeSegmentId="") ;
228 
231  static bool ImportLabelmapToSegmentationNode(vtkMRMLLabelMapVolumeNode* labelmapNode,
232  vtkMRMLSegmentationNode* segmentationNode, vtkStringArray* updatedSegmentIDs);
233 
236  static bool ImportLabelmapToSegmentationNode(vtkOrientedImageData* labelmapImage,
237  vtkMRMLSegmentationNode* segmentationNode, vtkStringArray* updatedSegmentIDs,
238  vtkGeneralTransform* labelmapToSegmentationTransform=nullptr );
239 
244  bool ImportLabelmapToSegmentationNodeWithTerminology(vtkMRMLLabelMapVolumeNode* labelmapNode,
245  vtkMRMLSegmentationNode* segmentationNode, std::string terminologyContextName, std::string insertBeforeSegmentId="");
246 
248  static bool ImportModelToSegmentationNode(vtkMRMLModelNode* modelNode, vtkMRMLSegmentationNode* segmentationNode, std::string insertBeforeSegmentId = "");
249 
251  static bool ImportModelsToSegmentationNode(
252  vtkIdType folderItemId, vtkMRMLSegmentationNode* segmentationNode, std::string insertBeforeSegmentId = "" );
253 
261  static bool ExportSegmentsClosedSurfaceRepresentationToFiles(std::string destinationFolder,
262  vtkMRMLSegmentationNode* segmentationNode, vtkStringArray* segmentIds = nullptr,
263  std::string fileFormat = "STL", bool lps = true, double sizeScale = 1.0, bool merge = false);
264 
272  static void GetLabelValuesFromColorNode(vtkMRMLSegmentationNode* segmentationNode, vtkMRMLColorTableNode* colorTableNode,
273  vtkStringArray* segmentIds, vtkIntArray* labelValues);
274 
286  static bool ExportSegmentsBinaryLabelmapRepresentationToFiles(std::string destinationFolder,
287  vtkMRMLSegmentationNode* segmentationNode, vtkStringArray* segmentIds = nullptr, std::string extension = "nrrd", bool useCompression = false,
288  vtkMRMLVolumeNode* referenceVolumeNode = nullptr, int extentComputationMode = vtkSegmentation::EXTENT_REFERENCE_GEOMETRY,
289  vtkMRMLColorTableNode* colorTableNode = nullptr);
290 
296  static vtkDataObject* CreateRepresentationForOneSegment(vtkSegmentation* segmentation, std::string segmentID, std::string representationName);
297 
301  static bool ApplyParentTransformToOrientedImageData(
302  vtkMRMLTransformableNode* transformableNode, vtkOrientedImageData* orientedImageData, bool linearInterpolation=false, double backgroundColor[4]=nullptr );
303 
307  static bool ApplyParentTransformToPolyData(vtkMRMLTransformableNode* transformableNode, vtkPolyData* polyData);
308 
318  static bool GetTransformBetweenRepresentationAndSegmentation(
319  vtkMRMLTransformableNode* representationNode, vtkMRMLSegmentationNode* segmentationNode, vtkGeneralTransform* representationToSegmentationTransform );
320 
330  static bool GetSegmentRepresentation(vtkMRMLSegmentationNode* segmentationNode, std::string segmentID, std::string representationName, vtkDataObject* segmentRepresentation, bool applyParentTransform=true);
331 
342  static bool GetSegmentBinaryLabelmapRepresentation(vtkMRMLSegmentationNode* segmentationNode, std::string segmentID, vtkOrientedImageData* imageData, bool applyParentTransform=true);
343 
353  static bool GetSegmentClosedSurfaceRepresentation(vtkMRMLSegmentationNode* segmentationNode,
354  std::string segmentID, vtkPolyData* polyData, bool applyParentTransform = true);
355 
362  enum
363  {
364  MODE_REPLACE = 0,
367  MODE_MERGE_MASK
368  };
369  static bool SetBinaryLabelmapToSegment(vtkOrientedImageData* labelmap, vtkMRMLSegmentationNode* segmentationNode, std::string segmentID,
370  int mergeMode=MODE_REPLACE, const int extent[6]=nullptr, bool minimumOfAllSegments=false, const std::vector<std::string>& segmentIdsToOverwrite={});
371 
375  bool SetTerminologyToSegmentationFromLabelmapNode(vtkMRMLSegmentationNode* segmentationNode,
376  vtkMRMLLabelMapVolumeNode* labelmapNode, std::string terminologyContextName);
377 
379  vtkMRMLSegmentationNode* GetDefaultSegmentationNode();
380 
382  bool GetDefaultSurfaceSmoothingEnabled();
383  void SetDefaultSurfaceSmoothingEnabled(bool enabled);
384 
386  {
391  LastStatus
392  };
394  static const char* GetSegmentStatusAsHumanReadableString(int segmentStatus);
396  static const char* GetSegmentStatusAsMachineReadableString(int segmentStatus);
398  static int GetSegmentStatusFromMachineReadableString(std::string statusString);
399 
401  static const char* GetStatusTagName();
404  static int GetSegmentStatus(vtkSegment* segment);
408  static void SetSegmentStatus(vtkSegment* segment, int status);
410  static bool ClearSegment(vtkMRMLSegmentationNode* segmentationNode, std::string segmentID);
411 
418  static bool GetSharedSegmentIDsInMask(vtkMRMLSegmentationNode* segmentationNode, std::string sharedSegmentID, vtkOrientedImageData* mask, const int extent[6],
419  std::vector<std::string>& segmentIDs, int maskThreshold = 0.0, bool includeInputSharedSegmentID = false);
420 
425  static bool ReconvertAllRepresentations(vtkMRMLSegmentationNode* segmentationNode,
426  const std::vector<std::string>& segmentIDs={});
427 
433  static void CollapseBinaryLabelmaps(vtkMRMLSegmentationNode* segmentationNode, bool forceToSingleLayer);
434 
442  static void GenerateMergedLabelmapInReferenceGeometry(vtkMRMLSegmentationNode* segmentationNode, vtkMRMLVolumeNode* referenceVolumeNode,
443  vtkStringArray* segmentIDs, int extentComputationMode, vtkOrientedImageData* mergedLabelmap_Reference, vtkIntArray* labelValues=nullptr);
444 
450  static bool IsEffectiveExentOutsideReferenceVolume(vtkMRMLVolumeNode* referenceVolumeNode, vtkMRMLSegmentationNode* segmentationNode,
451  vtkStringArray* segmentIDs = nullptr);
452 
457  static bool IsSegmentationExentOutsideReferenceGeometry(vtkOrientedImageData* referenceGeometry, vtkOrientedImageData* segmentationGeometry);
458 
459 protected:
460  void SetMRMLSceneInternal(vtkMRMLScene * newScene) override;
461 
463  void RegisterNodes() override;
464 
470  static void OnSubjectHierarchyUIDAdded(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
471 
473  void OnMRMLSceneNodeAdded(vtkMRMLNode* node) override;
474 
475  static bool ExportSegmentsClosedSurfaceRepresentationToStlFiles(std::string destinationFolder,
476  vtkMRMLSegmentationNode* segmentationNode, const std::vector<std::string>& segmentIDs, bool lps, double sizeScale, bool merge);
477  static bool ExportSegmentsClosedSurfaceRepresentationToObjFile(std::string destinationFolder,
478  vtkMRMLSegmentationNode* segmentationNode, const std::vector<std::string>& segmentIDs, bool lps, double sizeScale);
479 
483  static std::string GetSafeFileName(std::string originalName);
484 
485 protected:
488 
491 
492 private:
494  void operator=(const vtkSlicerSegmentationsModuleLogic&) = delete;
495 };
496 
497 #endif
void PrintSelf(ostream &os, vtkIndent indent) override
static vtkSlicerModuleLogic * New()
The Usual vtk class functions.
This class encapsulates a segment that is part of a segmentation.
Definition: vtkSegment.h:45
MRML node for representing a transformation between this node space and a parent node space...
MRML node to represent a 3D surface model.
MRML node for representing a label map volume.
MRML node for segmentation storage on disk.
Image data containing orientation information.
A set of MRML Nodes that supports serialization and undo/redo.
Definition: vtkMRMLScene.h:57
virtual void OnMRMLSceneNodeAdded(vtkMRMLNode *)
This class encapsulates a segmentation that can contain multiple segments and multiple representation...
virtual void SetMRMLSceneInternal(vtkMRMLScene *newScene)
Extent is computed as union of effective extent of all segments.
MRML node for representing a volume (image stack).
MRML node for representing a volume (image stack).
Abstract Superclass for all specific types of MRML nodes.
Definition: vtkMRMLNode.h:167
MRML node to represent discrete color information.
vtkCallbackCommand * SubjectHierarchyUIDCallbackCommand
Command handling subject hierarchy UID added events.
Extent of common geometry is used as extent.
MRML node for representing a node with a transform.
MRML node containing segmentationsSegmentation node stores a set of segments (also known as contours ...