Slicer  4.11
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  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 
178  static bool ExportSegmentsToLabelmapNode(vtkMRMLSegmentationNode* segmentationNode, std::vector<std::string>& segmentIDs,
179  vtkMRMLLabelMapVolumeNode* labelmapNode, vtkMRMLVolumeNode* referenceVolumeNode = nullptr,
180  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS, vtkMRMLColorTableNode* colorTableNode = nullptr);
181 
189  static bool ExportSegmentsToLabelmapNode(vtkMRMLSegmentationNode* segmentationNode, vtkStringArray* segmentIDs,
190  vtkMRMLLabelMapVolumeNode* labelmapNode, vtkMRMLVolumeNode* referenceVolumeNode = nullptr,
191  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS, vtkMRMLColorTableNode* colorTableNode = nullptr);
192 
199  static bool ExportVisibleSegmentsToLabelmapNode(vtkMRMLSegmentationNode* segmentationNode,
200  vtkMRMLLabelMapVolumeNode* labelmapNode, vtkMRMLVolumeNode* referenceVolumeNode = nullptr,
201  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS);
202 
208  static bool ExportAllSegmentsToLabelmapNode(vtkMRMLSegmentationNode* segmentationNode, vtkMRMLLabelMapVolumeNode* labelmapNode,
209  int extentComputationMode = vtkSegmentation::EXTENT_UNION_OF_EFFECTIVE_SEGMENTS);
210 
214  static bool ImportLabelmapToSegmentationNode(vtkMRMLLabelMapVolumeNode* labelmapNode,
215  vtkMRMLSegmentationNode* segmentationNode, std::string insertBeforeSegmentId="");
216 
223  static bool ImportLabelmapToSegmentationNode(vtkOrientedImageData* labelmapImage,
224  vtkMRMLSegmentationNode* segmentationNode, std::string baseSegmentName="", std::string insertBeforeSegmentId="") ;
225 
228  static bool ImportLabelmapToSegmentationNode(vtkMRMLLabelMapVolumeNode* labelmapNode,
229  vtkMRMLSegmentationNode* segmentationNode, vtkStringArray* updatedSegmentIDs);
230 
233  static bool ImportLabelmapToSegmentationNode(vtkOrientedImageData* labelmapImage,
234  vtkMRMLSegmentationNode* segmentationNode, vtkStringArray* updatedSegmentIDs,
235  vtkGeneralTransform* labelmapToSegmentationTransform=nullptr );
236 
241  bool ImportLabelmapToSegmentationNodeWithTerminology(vtkMRMLLabelMapVolumeNode* labelmapNode,
242  vtkMRMLSegmentationNode* segmentationNode, std::string terminologyContextName, std::string insertBeforeSegmentId="");
243 
245  static bool ImportModelToSegmentationNode(vtkMRMLModelNode* modelNode, vtkMRMLSegmentationNode* segmentationNode, std::string insertBeforeSegmentId = "");
246 
248  static bool ImportModelsToSegmentationNode(
249  vtkIdType folderItemId, vtkMRMLSegmentationNode* segmentationNode, std::string insertBeforeSegmentId = "" );
250 
258  static bool ExportSegmentsClosedSurfaceRepresentationToFiles(std::string destinationFolder,
259  vtkMRMLSegmentationNode* segmentationNode, vtkStringArray* segmentIds = nullptr,
260  std::string fileFormat = "STL", bool lps = true, double sizeScale = 1.0, bool merge = false);
261 
269  static void GetLabelValuesFromColorNode(vtkMRMLSegmentationNode* segmentationNode, vtkMRMLColorTableNode* colorTableNode,
270  vtkStringArray* segmentIds, vtkIntArray* labelValues);
271 
281  static bool ExportSegmentsBinaryLabelmapRepresentationToFiles(std::string destinationFolder,
282  vtkMRMLSegmentationNode* segmentationNode, vtkStringArray* segmentIds = nullptr, std::string extension = "nrrd", bool useCompression = false,
283  vtkMRMLVolumeNode* referenceVolumeNode = nullptr, int extentComputationMode = vtkSegmentation::EXTENT_REFERENCE_GEOMETRY,
284  vtkMRMLColorTableNode* colorTableNode = nullptr);
285 
291  static vtkDataObject* CreateRepresentationForOneSegment(vtkSegmentation* segmentation, std::string segmentID, std::string representationName);
292 
296  static bool ApplyParentTransformToOrientedImageData(
297  vtkMRMLTransformableNode* transformableNode, vtkOrientedImageData* orientedImageData, bool linearInterpolation=false, double backgroundColor[4]=nullptr );
298 
302  static bool ApplyParentTransformToPolyData(vtkMRMLTransformableNode* transformableNode, vtkPolyData* polyData);
303 
313  static bool GetTransformBetweenRepresentationAndSegmentation(
314  vtkMRMLTransformableNode* representationNode, vtkMRMLSegmentationNode* segmentationNode, vtkGeneralTransform* representationToSegmentationTransform );
315 
325  static bool GetSegmentRepresentation(vtkMRMLSegmentationNode* segmentationNode, std::string segmentID, std::string representationName, vtkDataObject* segmentRepresentation, bool applyParentTransform=true);
326 
337  static bool GetSegmentBinaryLabelmapRepresentation(vtkMRMLSegmentationNode* segmentationNode, std::string segmentID, vtkOrientedImageData* imageData, bool applyParentTransform=true);
338 
348  static bool GetSegmentClosedSurfaceRepresentation(vtkMRMLSegmentationNode* segmentationNode,
349  std::string segmentID, vtkPolyData* polyData, bool applyParentTransform = true);
350 
357  enum
358  {
359  MODE_REPLACE = 0,
362  MODE_MERGE_MASK
363  };
364  static bool SetBinaryLabelmapToSegment(vtkOrientedImageData* labelmap, vtkMRMLSegmentationNode* segmentationNode, std::string segmentID,
365  int mergeMode=MODE_REPLACE, const int extent[6]=nullptr, bool minimumOfAllSegments=false, std::vector<std::string> segmentIdsToOverwrite={});
366 
370  bool SetTerminologyToSegmentationFromLabelmapNode(vtkMRMLSegmentationNode* segmentationNode,
371  vtkMRMLLabelMapVolumeNode* labelmapNode, std::string terminologyContextName);
372 
374  vtkMRMLSegmentationNode* GetDefaultSegmentationNode();
375 
377  bool GetDefaultSurfaceSmoothingEnabled();
378  void SetDefaultSurfaceSmoothingEnabled(bool enabled);
379 
381  {
386  LastStatus
387  };
389  static const char* GetSegmentStatusAsHumanReadableString(int segmentStatus);
391  static const char* GetSegmentStatusAsMachineReadableString(int segmentStatus);
393  static int GetSegmentStatusFromMachineReadableString(std::string statusString);
394 
396  static const char* GetStatusTagName();
398  static int GetSegmentStatus(vtkSegment* segment);
400  static void SetSegmentStatus(vtkSegment* segment, int status);
402  static bool ClearSegment(vtkMRMLSegmentationNode* segmentationNode, std::string segmentID);
403 
410  static bool GetSharedSegmentIDsInMask(vtkMRMLSegmentationNode* segmentationNode, std::string sharedSegmentID, vtkOrientedImageData* mask, const int extent[6],
411  std::vector<std::string>& segmentIDs, int maskThreshold = 0.0, bool includeInputSharedSegmentID = false);
412 
417  static bool ReconvertAllRepresentations(vtkMRMLSegmentationNode* segmentationNode,
418  const std::vector<std::string>& segmentIDs={});
419 
425  static void CollapseBinaryLabelmaps(vtkMRMLSegmentationNode* segmentationNode, bool forceToSingleLayer);
426 
427  static void GenerateMergedLabelmapInReferenceGeometry(vtkMRMLSegmentationNode* segmentationNode, vtkMRMLVolumeNode* referenceVolumeNode,
428  vtkStringArray* segmentIDs, int extentComputationMode, vtkOrientedImageData* mergedLabelmap_Reference);
429 
430 protected:
431  void SetMRMLSceneInternal(vtkMRMLScene * newScene) override;
432 
434  void RegisterNodes() override;
435 
441  static void OnSubjectHierarchyUIDAdded(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
442 
444  void OnMRMLSceneNodeAdded(vtkMRMLNode* node) override;
445 
446  static bool ExportSegmentsClosedSurfaceRepresentationToStlFiles(std::string destinationFolder,
447  vtkMRMLSegmentationNode* segmentationNode, std::vector<std::string>& segmentIDs, bool lps, double sizeScale, bool merge);
448  static bool ExportSegmentsClosedSurfaceRepresentationToObjFile(std::string destinationFolder,
449  vtkMRMLSegmentationNode* segmentationNode, std::vector<std::string>& segmentIDs, bool lps, double sizeScale);
450 
454  static std::string GetSafeFileName(std::string originalName);
455 
456 protected:
459 
462 
463 private:
465  void operator=(const vtkSlicerSegmentationsModuleLogic&) = delete;
466 };
467 
468 #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:61
virtual void OnMRMLSceneNodeAdded(vtkMRMLNode *)
This class encapsulates a segmentation that can contain multiple segments and multiple representation...
Extent of common geometry is used as extent.
virtual void SetMRMLSceneInternal(vtkMRMLScene *newScene)
MRML node for representing a volume (image stack).
Extent is computed as union of effective extent of all segments.
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.
MRML node for representing a node with a transform.
MRML node containing segmentationsSegmentation node stores a set of segments (also known as contours ...