File: StructuredReport.cpp

package info (click to toggle)
sight 21.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 36,592 kB
  • sloc: cpp: 228,341; xml: 19,066; ansic: 9,854; python: 302; sh: 135; makefile: 32
file content (327 lines) | stat: -rw-r--r-- 12,277 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/************************************************************************
 *
 * Copyright (C) 2009-2022 IRCAD France
 * Copyright (C) 2012-2016 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "io/dicom/helper/StructuredReport.hpp"

#include "io/dicom/container/sr/DicomSRCodeNode.hpp"
#include "io/dicom/container/sr/DicomSRContainerNode.hpp"
#include "io/dicom/container/sr/DicomSRImageNode.hpp"
#include "io/dicom/container/sr/DicomSRNumNode.hpp"
#include "io/dicom/container/sr/DicomSRSCoord3DNode.hpp"
#include "io/dicom/container/sr/DicomSRSCoordNode.hpp"
#include "io/dicom/container/sr/DicomSRTextNode.hpp"
#include "io/dicom/container/sr/DicomSRUIDRefNode.hpp"
#include "io/dicom/helper/DicomDataReader.hxx"

namespace sight::io::dicom
{

namespace helper
{

//------------------------------------------------------------------------------

SPTR(io::dicom::container::sr::DicomSRContainerNode) StructuredReport::readSR(const gdcm::DataSet& dataset)
{
    SPTR(io::dicom::container::sr::DicomSRContainerNode) result;

    // Value Type - Type 1
    const std::string type = io::dicom::helper::DicomDataReader::getTagValue<0x0040, 0xa040>(dataset);

    // Concept Name Code Sequence - Type 1C
    const io::dicom::container::DicomCodedAttribute& codedAttribute =
        io::dicom::helper::StructuredReport::readCodeSequence<0x0040, 0xa043>(dataset);

    if(type == "CONTAINER" && !codedAttribute.isEmpty())
    {
        result = std::make_shared<io::dicom::container::sr::DicomSRContainerNode>(codedAttribute);
        io::dicom::helper::StructuredReport::readSubNodeContainer(dataset, result);
    }

    return result;
}

//------------------------------------------------------------------------------

void StructuredReport::readSubNodeContainer(
    const gdcm::DataSet& dataset,
    SPTR(io::dicom::container::sr::DicomSRNode)parent
)
{
    // Retrieve the content sequence
    if(dataset.FindDataElement(gdcm::Tag(0x0040, 0xa730)))
    {
        gdcm::SmartPointer<gdcm::SequenceOfItems> sequence =
            dataset.GetDataElement(gdcm::Tag(0x0040, 0xa730)).GetValueAsSQ();

        for(unsigned int i = 1 ; i <= sequence->GetNumberOfItems() ; ++i)
        {
            const gdcm::DataSet& itemDataset = sequence->GetItem(i).GetNestedDataSet();
            io::dicom::helper::StructuredReport::readSubNode(itemDataset, parent);
        }
    }
}

//------------------------------------------------------------------------------

void StructuredReport::readSubNode(
    const gdcm::DataSet& dataset,
    SPTR(io::dicom::container::sr::DicomSRNode)parent
)
{
    SPTR(io::dicom::container::sr::DicomSRNode) node;

    // Value Type - Type 1
    const std::string type = io::dicom::helper::DicomDataReader::getTagValue<0x0040, 0xa040>(dataset);

    // Concept Name Code Sequence - Type 1C
    const io::dicom::container::DicomCodedAttribute& codedAttribute =
        io::dicom::helper::StructuredReport::readCodeSequence<0x0040, 0xa043>(dataset);

    // Relationship Value - Type 1
    const std::string relationship = io::dicom::helper::DicomDataReader::getTagValue<0x0040, 0xa010>(dataset);

    if(type == "TEXT")
    {
        // Text Value - Type 1C
        const std::string textValue = io::dicom::helper::DicomDataReader::getTagValue<0x0040, 0xa160>(dataset);

        // Create Text Node
        node = std::make_shared<io::dicom::container::sr::DicomSRTextNode>(
            codedAttribute,
            relationship,
            textValue
        );
    }
    else if(type == "NUM")
    {
        // Retrieve the measured value sequence
        if(dataset.FindDataElement(gdcm::Tag(0x0040, 0xa300)))
        {
            gdcm::SmartPointer<gdcm::SequenceOfItems> sequence =
                dataset.GetDataElement(gdcm::Tag(0x0040, 0xa300)).GetValueAsSQ();
            if(sequence->GetNumberOfItems() > 0)
            {
                const gdcm::DataSet& itemDataset = sequence->GetItem(1).GetNestedDataSet();

                // Numerical value - Type 1
                const double numValue =
                    io::dicom::helper::DicomDataReader::getTagValue<0x0040, 0xa30a, double>(itemDataset);

                // Measured units code sequence - Type 1
                const io::dicom::container::DicomCodedAttribute& measurementUnits =
                    io::dicom::helper::StructuredReport::readCodeSequence<0x0040, 0x08ea>(itemDataset);

                // Create Num Node
                node = std::make_shared<io::dicom::container::sr::DicomSRNumNode>(
                    codedAttribute,
                    relationship,
                    numValue,
                    measurementUnits
                );
            }
        }
    }
    else if(type == "SCOORD")
    {
        // Graphic Data - Type 1C
        gdcm::Attribute<0x0070, 0x0022> graphicDataAttribute;
        graphicDataAttribute.SetFromDataElement(dataset.GetDataElement(gdcm::Tag(0x0070, 0x0022)));
        const float* graphicData = graphicDataAttribute.GetValues();

        // Graphic Type - Type 1
        const std::string graphicType = io::dicom::helper::DicomDataReader::getTagValue<0x0070, 0x0023>(dataset);

        if(graphicType == "POINT" && graphicDataAttribute.GetNumberOfValues() == 2)
        {
            // Create Text Node
            std::vector<float> scoordVector(graphicData, graphicData + 2);
            node = std::make_shared<io::dicom::container::sr::DicomSRSCoordNode>(
                codedAttribute,
                relationship,
                graphicType,
                scoordVector
            );
        }
        else if(graphicType == "POLYLINE" && graphicDataAttribute.GetNumberOfValues() == 4)
        {
            // Create Text Node
            std::vector<float> scoordVector(graphicData, graphicData + 4);
            node = std::make_shared<io::dicom::container::sr::DicomSRSCoordNode>(
                codedAttribute,
                relationship,
                graphicType,
                scoordVector
            );
        }
        else
        {
            SIGHT_WARN("Graphic type is not handled or data size doesn't match type. The SR node has been ignored.");
        }
    }
    else if(type == "SCOORD3D")
    {
        // Referenced Frame of Reference UID - Type 1
        const std::string frameOfReferenceUID =
            io::dicom::helper::DicomDataReader::getTagValue<0x3006, 0x0024>(dataset);

        // Graphic Data - Type 1C
        gdcm::Attribute<0x0070, 0x0022> graphicDataAttribute;
        graphicDataAttribute.SetFromDataElement(dataset.GetDataElement(gdcm::Tag(0x0070, 0x0022)));
        const float* graphicData = graphicDataAttribute.GetValues();

        // Graphic Type - Type 1
        const std::string graphicType = io::dicom::helper::DicomDataReader::getTagValue<0x0070, 0x0023>(dataset);

        if(graphicType == "POINT" && graphicDataAttribute.GetNumberOfValues() == 3)
        {
            // Create Text Node
            std::vector<float> scoordVector(graphicData, graphicData + 3);
            node = std::make_shared<io::dicom::container::sr::DicomSRSCoord3DNode>(
                codedAttribute,
                relationship,
                graphicType,
                scoordVector,
                frameOfReferenceUID
            );
        }
        else if(graphicType == "POLYLINE" && graphicDataAttribute.GetNumberOfValues() == 6)
        {
            // Create Text Node
            std::vector<float> scoordVector(graphicData, graphicData + 6);
            node = std::make_shared<io::dicom::container::sr::DicomSRSCoord3DNode>(
                codedAttribute,
                relationship,
                graphicType,
                scoordVector,
                frameOfReferenceUID
            );
        }
        else
        {
            SIGHT_WARN("Graphic type is not handled or data size doesn't match type. The SR node has been ignored.");
        }
    }
    else if(type == "IMAGE")
    {
        // Retrieve the referenced SOP sequence
        if(dataset.FindDataElement(gdcm::Tag(0x0008, 0x1199)))
        {
            gdcm::SmartPointer<gdcm::SequenceOfItems> sequence =
                dataset.GetDataElement(gdcm::Tag(0x0008, 0x1199)).GetValueAsSQ();
            if(sequence->GetNumberOfItems() > 0)
            {
                const gdcm::DataSet& itemDataset = sequence->GetItem(1).GetNestedDataSet();

                // Referenced SOP Class UID - Type 1
                const std::string sopClassUID = io::dicom::helper::DicomDataReader::getTagValue<0x0008, 0x1150>(
                    itemDataset
                );

                // Referenced SOP Instance UID  - Type 1
                const std::string sopInstanceUID =
                    io::dicom::helper::DicomDataReader::getTagValue<0x0008, 0x1155>(itemDataset);

                // Referenced Frame Number - Type 1C
                int frameNumber = io::dicom::helper::DicomDataReader::getTagValue<0x0008, 0x1160, int>(itemDataset);

                // Create Num Node
                node = std::make_shared<io::dicom::container::sr::DicomSRImageNode>(
                    codedAttribute,
                    relationship,
                    sopClassUID,
                    sopInstanceUID,
                    frameNumber
                );
            }
        }
    }
    else if(type == "CODE")
    {
        const io::dicom::container::DicomCodedAttribute& codedEntry =
            io::dicom::helper::StructuredReport::readCodeSequence<0x0040, 0xa168>(dataset);

        // Create Code Node
        node = std::make_shared<io::dicom::container::sr::DicomSRCodeNode>(codedAttribute, relationship, codedEntry);
    }
    else if(type == "UIDREF")
    {
        // UID Value - Type 1C
        const std::string uidValue = io::dicom::helper::DicomDataReader::getTagValue<0x0040, 0xa124>(dataset);

        // Create UIDRef Node
        node = std::make_shared<io::dicom::container::sr::DicomSRUIDRefNode>(codedAttribute, relationship, uidValue);
    }
    else if(type == "CONTAINER")
    {
        // Create Container Node
        node = std::make_shared<io::dicom::container::sr::DicomSRContainerNode>(codedAttribute, relationship);
    }

    if(node)
    {
        // Create children
        io::dicom::helper::StructuredReport::readSubNodeContainer(dataset, node);

        // Add node to parent
        parent->addSubNode(node);
    }
}

//------------------------------------------------------------------------------

void StructuredReport::dumpSR(const SPTR(io::dicom::container::sr::DicomSRNode)& root, std::ostream& out)
{
    out << "graph SR {" << std::endl;
    int index = 0;
    StructuredReport::dumpSRNode(root, out, index);
    out << "}" << std::endl;
}

//------------------------------------------------------------------------------

void StructuredReport::dumpSRNode(
    const SPTR(io::dicom::container::sr::DicomSRNode)& root,
    std::ostream& out,
    int& index
)
{
    // Write node
    out << "\t" << index << "[label=\"" << (*root) << "\"][shape=box];" << std::endl;

    const int parentIndex = index;
    for(const SPTR(io::dicom::container::sr::DicomSRNode) & child : root->getSubNodeContainer())
    {
        // Write edge
        out << "\t" << parentIndex << "--" << (index + 1) << "[label=\""
        << child->getRelationship() << "\"];" << std::endl;

        // Write child
        StructuredReport::dumpSRNode(child, out, ++index);
    }
}

//------------------------------------------------------------------------------

} //namespace helper

} //namespace sight::io::dicom