fw4spl
reader/iod/SpatialFiducialsIOD.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwGdcmIO/reader/iod/SpatialFiducialsIOD.hpp"
8 
9 #include "fwGdcmIO/helper/DicomDataReader.hxx"
10 #include "fwGdcmIO/reader/ie/SpatialFiducials.hpp"
11 
12 #include <fwData/PointList.hpp>
13 #include <fwData/Vector.hpp>
14 
15 #include <fwDataTools/fieldHelper/Image.hpp>
16 
17 #include <fwMedData/ImageSeries.hpp>
18 
19 #include <boost/algorithm/string/trim.hpp>
20 
21 namespace fwGdcmIO
22 {
23 namespace reader
24 {
25 namespace iod
26 {
27 
28 //------------------------------------------------------------------------------
29 
30 SpatialFiducialsIOD::SpatialFiducialsIOD(const ::fwMedData::DicomSeries::csptr& dicomSeries,
31  const ::fwGdcmIO::container::DicomInstance::sptr& instance,
32  const ::fwLog::Logger::sptr& logger,
33  ProgressCallback progress,
34  CancelRequestedCallback cancel) :
35  ::fwGdcmIO::reader::iod::InformationObjectDefinition(dicomSeries, instance, logger, progress, cancel)
36 {
37 }
38 
39 //------------------------------------------------------------------------------
40 
41 SpatialFiducialsIOD::~SpatialFiducialsIOD()
42 {
44 }
45 
46 //------------------------------------------------------------------------------
47 
48 void SpatialFiducialsIOD::read(::fwMedData::Series::sptr series) throw (::fwGdcmIO::exception::Failed)
49 {
50  // Retrieve images
51  ::fwMedData::ImageSeries::sptr imageSeries = ::fwMedData::ImageSeries::dynamicCast(series);
52  SLM_ASSERT("ImageSeries should not be null.", imageSeries);
53  ::fwData::Image::sptr image = imageSeries->getImage();
54  SLM_ASSERT("::fwData::Image not instanced", image);
55 
56  // Create GDCM Reader
57  SPTR(::gdcm::Reader) reader = std::shared_ptr< ::gdcm::Reader >( new ::gdcm::Reader );
58 
59  // Read the first file
60  ::fwMedData::DicomSeries::DicomContainerType dicomContainer = m_dicomSeries->getDicomContainer();
61 
62  if(dicomContainer.size() > 1)
63  {
64  m_logger->warning("More than one Spatial Fiducials item have been found in the series. "
65  "Only the first one will be read.");
66  }
67 
68  const ::fwMemory::BufferObject::sptr bufferObj = dicomContainer.begin()->second;
69  const ::fwMemory::BufferManager::StreamInfo streamInfo = bufferObj->getStreamInfo();
70  SPTR(std::istream) is = streamInfo.stream;
71  reader->SetStream(*is);
72 
73  const bool success = reader->Read();
74  FW_RAISE_EXCEPTION_IF(::fwGdcmIO::exception::Failed("Unable to read the DICOM instance \""+
75  bufferObj->getStreamInfo().fsFile.string()+
76  "\" using the GDCM Reader."), !success);
77 
78  // Create Information Entity helpers
80  m_dicomSeries, reader, m_instance, imageSeries->getImage(),
82 
83  // Retrieve dataset
84  const ::gdcm::DataSet& datasetRoot = reader->GetFile().GetDataSet();
85 
86  // Retrieve Fiducial Set Sequence
87  const ::gdcm::DataElement& fiducialSetSequenceDataElement =
88  datasetRoot.GetDataElement( ::gdcm::Tag(0x0070, 0x031C) );
89  const ::gdcm::SmartPointer< ::gdcm::SequenceOfItems > fiducialSetSequence =
90  fiducialSetSequenceDataElement.GetValueAsSQ();
91 
92  for(unsigned int i = 1; i <= fiducialSetSequence->GetNumberOfItems(); ++i)
93  {
94  ::gdcm::Item sequenceSetItem = fiducialSetSequence->GetItem(i);
95  const ::gdcm::DataSet& sequenceSetDataset = sequenceSetItem.GetNestedDataSet();
96 
97  const ::gdcm::DataElement& fiducialSequenceDataElement =
98  sequenceSetDataset.GetDataElement( ::gdcm::Tag(0x0070, 0x031E) );
99  const ::gdcm::SmartPointer< ::gdcm::SequenceOfItems > fiducialSequence =
100  fiducialSequenceDataElement.GetValueAsSQ();
101 
102  for(unsigned int j = 1; j <= fiducialSequence->GetNumberOfItems(); ++j)
103  {
104  ::gdcm::Item fiducialItem = fiducialSequence->GetItem(j);
105  const ::gdcm::DataSet& fiducialDataset = fiducialItem.GetNestedDataSet();
106  const std::string shapeType =
107  ::fwGdcmIO::helper::DicomDataReader::getTagValue<0x0070, 0x0306>(fiducialDataset);
108 
109  if(shapeType == "POINT")
110  {
111  spatialFiducialsIE.readLandmark(fiducialDataset);
112  }
113  else
114  {
115  m_logger->warning("Fiducial shape type not supported: \"" + shapeType + "\"");
116  }
117  }
118 
119  }
120 }
121 
122 //------------------------------------------------------------------------------
123 
124 } // namespace iod
125 } // namespace reader
126 } // namespace fwGdcmIO
#define SPTR(_cls_)
CancelRequestedCallback m_cancelRequestedCallback
Cancel information for jobs.
void readLandmark(const ::gdcm::DataSet &fiducialDataset)
Read image landmarks from dataset.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
FWGDCMIO_API SpatialFiducialsIOD(const ::fwMedData::DicomSeries::csptr &dicomSeries, const std::shared_ptr< ::fwGdcmIO::container::DicomInstance > &instance, const ::fwLog::Logger::sptr &logger=nullptr, ProgressCallback progress=nullptr, CancelRequestedCallback cancel=nullptr)
Constructor.
The namespace fwGdcmIO contains reader, writer and helper for dicom data.
Implements a failed exception class for fwGdcmIO.
std::shared_ptr< ::fwGdcmIO::container::DicomInstance > m_instance
DICOM Instance.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
Spatial Fiducials Information Entity class.
ProgressCallback m_progressCallback
Progress callback for jobs.