Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-10 08:06:20

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 #include "DicomFileStructure.hh"
0027 
0028 #include "dcmtk/config/osconfig.h"  // make sure OS specific configuration is included
0029 #include "dcmtk/dcmdata/dcdeftag.h"
0030 #include "dcmtk/dcmdata/dcfilefo.h"
0031 #include "dcmtk/dcmdata/dcpixel.h"
0032 #include "dcmtk/dcmdata/dcpixseq.h"
0033 #include "dcmtk/dcmdata/dcpxitem.h"
0034 #include "dcmtk/dcmrt/drtstrct.h"
0035 #include "dcmtk/dcmrt/seq/drtcis.h"  // for ContourImageSequence
0036 #include "dcmtk/dcmrt/seq/drtcs.h"  // for ContourSequence
0037 #include "dcmtk/dcmrt/seq/drtrcs.h"  // for ROIContourSequence
0038 #include "dcmtk/dcmrt/seq/drtrfors.h"  // for ReferencedFrameOfReferenceSequence
0039 #include "dcmtk/dcmrt/seq/drtssrs.h"  // for StructureSetROISequence
0040 
0041 #include "G4ThreeVector.hh"
0042 #include "G4UIcommand.hh"
0043 
0044 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0045 DicomFileStructure::DicomFileStructure(DcmDataset* dset) : DicomVFile(dset) {}
0046 
0047 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0048 void DicomFileStructure::ReadData()
0049 {
0050   DRTStructureSetIOD rtstruct;
0051   OFCondition result = rtstruct.read(*theDataset);
0052   //  DCMRT_INFO("Read RT Structure Set: " << status.text());
0053   if (!result.good()) {
0054     G4Exception("DicomFileStructure::ReadData", "DFS001", FatalException, result.text());
0055   }
0056 
0057   //@@@@@@@@@@@@ DRTReferencedFrameOfReferenceSequence
0058   DRTReferencedFrameOfReferenceSequence refSeq = rtstruct.getReferencedFrameOfReferenceSequence();
0059   if (refSeq.isEmpty()) {
0060     G4Exception("DicomFileStructure::ReadData", "DFS002", JustWarning,
0061                 "DRTReferencedFrameOfReferenceSequence is empty");
0062   }
0063 
0064   G4cout << "@@@@@ NUMBER OF ReferenceSequences " << refSeq.getNumberOfItems() << G4endl;
0065   refSeq.gotoFirstItem();
0066   for (size_t i1 = 0; i1 < refSeq.getNumberOfItems(); i1++) {
0067     DRTReferencedFrameOfReferenceSequence::Item& item = refSeq.getCurrentItem();
0068     OFString uid;
0069     item.getFrameOfReferenceUID(uid);
0070     G4cout << " FrameOfReferenceUID " << uid << G4endl;
0071     DRTRTReferencedStudySequence& reference_study_sequence_ref =
0072       item.getRTReferencedStudySequence();
0073     G4cout << "@@@@ NUMBER OF ReferenceStudySequences "
0074            << reference_study_sequence_ref.getNumberOfItems() << G4endl;
0075     reference_study_sequence_ref.gotoFirstItem();
0076     for (size_t i2 = 0; i2 < reference_study_sequence_ref.getNumberOfItems(); i2++) {
0077       DRTRTReferencedStudySequence::Item& rss_item = reference_study_sequence_ref.getCurrentItem();
0078       DRTRTReferencedSeriesSequence& series_seq_ref = rss_item.getRTReferencedSeriesSequence();
0079       G4cout << "@@@ NUMBER OF SeriesSequences " << series_seq_ref.getNumberOfItems() << G4endl;
0080       series_seq_ref.gotoFirstItem();
0081       for (size_t i3 = 0; i3 < series_seq_ref.getNumberOfItems(); i3++) {
0082         DRTRTReferencedSeriesSequence::Item& ref_series_seq_item = series_seq_ref.getCurrentItem();
0083         DRTContourImageSequence& image_sequence_seq_ref =
0084           ref_series_seq_item.getContourImageSequence();
0085         G4cout << "@@ NUMBER OF ContourImageSequences " << image_sequence_seq_ref.getNumberOfItems()
0086                << G4endl;
0087         image_sequence_seq_ref.gotoFirstItem();
0088         for (size_t i4 = 0; i4 < image_sequence_seq_ref.getNumberOfItems(); i4++) {
0089           DRTContourImageSequence::Item& image_contour_item =
0090             image_sequence_seq_ref.getCurrentItem();
0091           OFString refSOPInstUID;
0092           image_contour_item.getReferencedSOPInstanceUID(refSOPInstUID);
0093           std::cout << "ReferencedSOPInstanceUID= " << refSOPInstUID << std::endl;
0094           image_sequence_seq_ref.gotoNextItem().good();
0095         }  // end if image_sequence_seq_ref
0096         series_seq_ref.gotoNextItem();
0097       }  // end if series_seq_ref good
0098       reference_study_sequence_ref.gotoNextItem();
0099     }  // end if reference_study_sequence_ref good
0100     refSeq.gotoNextItem();
0101   }  // end if refSeq.first item
0102 
0103   //@@@@@@@@@@@@   DRTROISequence
0104   DRTStructureSetROISequence ROISeq = rtstruct.getStructureSetROISequence();
0105   G4cout << "@@@@@ NUMBER OF ROISequences " << ROISeq.getNumberOfItems() << G4endl;
0106   for (size_t i1 = 0; i1 < ROISeq.getNumberOfItems(); i1++) {
0107     DRTStructureSetROISequence::Item& item = ROISeq.getCurrentItem();
0108     OFString ROIName, ROINumber, ROIGenerationAlgorithm;
0109     item.getROINumber(ROINumber);
0110     item.getROIName(ROIName);
0111     item.getROIGenerationAlgorithm(ROIGenerationAlgorithm);
0112     if (ROINumber != "") {
0113       DicomROI* roi = new DicomROI(G4UIcommand::ConvertToInt(ROINumber.c_str()), ROIName.c_str());
0114       theROIs.push_back(roi);
0115       G4cout << " ROI: " << roi->GetNumber() << " " << roi->GetName() << " "
0116              << ROIGenerationAlgorithm << G4endl;
0117     }
0118 
0119     ROISeq.gotoNextItem().good();
0120   }  // end if ROISeq.first item
0121 
0122   //@@@@@@@@@@@@   DRTROIContourSequence
0123   DRTROIContourSequence ROIContourSeq = rtstruct.getROIContourSequence();
0124   if (ROISeq.getNumberOfItems() != ROIContourSeq.getNumberOfItems()) {
0125     G4Exception("DicomFileStructure", "DCS0001", FatalException,
0126                 ("Different number of ROIs and ROI Contours "
0127                  + std::to_string(ROISeq.getNumberOfItems()) + " <> "
0128                  + std::to_string(ROIContourSeq.getNumberOfItems()))
0129                   .c_str());
0130   }
0131 
0132   ROIContourSeq.gotoFirstItem();
0133   for (size_t i1 = 0; i1 < ROIContourSeq.getNumberOfItems(); i1++) {
0134     DRTROIContourSequence::Item& item = ROIContourSeq.getCurrentItem();
0135     OFString displayColor;
0136     item.getROIDisplayColor(displayColor);
0137     //    G4cout << " ROIDisplayColor " << displayColor << G4endl;
0138 
0139     DRTContourSequence contour_seq = item.getContourSequence();
0140     //    G4cout << "@@@@ NUMBER OF ContourSequences " << contour_seq.getNumberOfItems() << G4endl;
0141     contour_seq.gotoFirstItem();
0142     for (size_t i2 = 0; i2 < contour_seq.getNumberOfItems(); i2++) {
0143       //      if (contour_seq.gotoFirstItem().good()) {
0144       //        do {
0145       DRTContourSequence::Item& cs_item = contour_seq.getCurrentItem();
0146 
0147       DicomROIContour* roiC = new DicomROIContour();
0148 
0149       DRTContourImageSequence& contour_image_seq = cs_item.getContourImageSequence();
0150 
0151       contour_image_seq.gotoFirstItem();
0152       for (size_t i3 = 0; i3 < contour_image_seq.getNumberOfItems(); i3++) {
0153         DRTContourImageSequence::Item cis_item = contour_image_seq.getCurrentItem();
0154         OFString refSOPCUID;
0155         cis_item.getReferencedSOPClassUID(refSOPCUID);
0156         OFString refSOPIUID;
0157         cis_item.getReferencedSOPInstanceUID(refSOPIUID);
0158         if (refSOPIUID != "") roiC->AddImageIUID(refSOPIUID.c_str());
0159         contour_image_seq.gotoNextItem();
0160       }  // end if contour_image_seq
0161 
0162       //@@@
0163       OFString geomType;
0164       cs_item.getContourGeometricType(geomType);
0165       Sint32 nPoints;
0166       cs_item.getNumberOfContourPoints(nPoints);
0167       roiC->SetGeomType(geomType);
0168       OFVector<Float64> data;
0169       cs_item.getContourData(data);
0170       std::vector<G4ThreeVector> dataV;
0171       for (Sint32 ii = 0; ii < nPoints * 3; ii++) {
0172         if (ii % 3 == 2) dataV.push_back(G4ThreeVector(data[ii - 2], data[ii - 1], data[ii]));
0173       }
0174       roiC->SetData(dataV);
0175       theROIs[i1]->AddContour(roiC);
0176 
0177       contour_seq.gotoNextItem();
0178     }
0179     ROIContourSeq.gotoNextItem();
0180   }  // end if ROIContourSeq.first item
0181 
0182   //@@@@ Print ROIs
0183   G4cout << " @@@@@@@@@@@ ROIs " << G4endl;
0184   for (size_t ii = 0; ii < theROIs.size(); ii++) {
0185     theROIs[ii]->Print(G4cout);
0186   }
0187 }