File: trctrack.cc

package info (click to toggle)
dcmtk 3.6.9-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,648 kB
  • sloc: ansic: 426,874; cpp: 318,177; makefile: 6,401; sh: 4,341; yacc: 1,026; xml: 482; lex: 321; perl: 277
file content (324 lines) | stat: -rw-r--r-- 8,399 bytes parent folder | download | duplicates (4)
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
/*
 *
 *  Copyright (C) 2016-2024, Open Connections GmbH
 *  All rights reserved.  See COPYRIGHT file for details.
 *
 *  This software and supporting documentation are maintained by
 *
 *    OFFIS e.V.
 *    R&D Division Health
 *    Escherweg 2
 *    D-26121 Oldenburg, Germany
 *
 *
 *  Module:  dcmtract
 *
 *  Author:  Michael Onken
 *
 *  Purpose: Class representing a Track from a Tractography Results IOD
 *
 */
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmtract/trctrack.h"
#include "dcmtk/dcmtract/trctypes.h"
#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/dcelem.h"

// default constructor (protected, instance creation via create() function)
TrcTrack::TrcTrack()
: IODComponent()
{
  TrcTrack::resetRules();
}


OFString TrcTrack::getName() const
{
  return "TrackSequenceItem";
}


OFCondition TrcTrack::create(const Float32* trackDataPoints,
                             const size_t numPoints,
                             const Uint16* colors,
                             const size_t numColors,
                             TrcTrack*& track /* result */)
{
  track = new TrcTrack();
  if (!track)
    return EC_MemoryExhausted;

  OFCondition result = track->setTrackData(trackDataPoints, numPoints);
  if (result.good())
  {
    result = track->setRecommendedDisplayCIELabValues(colors, numColors);
  }
  if (result.bad())
  {
    delete track;
    track = NULL;
  }
  return result;
}


void TrcTrack::resetRules()
{
  // Set rules for the attributes within this component
  getRules()->addRule(new IODRule(DCM_PointCoordinatesData, "1","1", getName(), DcmIODTypes::IE_INSTANCE), OFTrue);
  getRules()->addRule(new IODRule(DCM_RecommendedDisplayCIELabValueList, "1","1C", getName(), DcmIODTypes::IE_INSTANCE), OFTrue);
  getRules()->addRule(new IODRule(DCM_RecommendedDisplayCIELabValue, "3","1C", getName(), DcmIODTypes::IE_INSTANCE), OFTrue);
}


OFCondition TrcTrack::check(const OFBool quiet)
{
  OFCondition result;
  // Report errors but ignore them
  IODComponent::check(quiet);
  const Float32* data = NULL;
  unsigned long count = 0;
  // If the actual data is missing, return an error, ignore other errors
  if (m_Item->findAndGetFloat32Array(DCM_PointCoordinatesData, data, &count).good())
  {
    if (count != 0)
    {
      if (count % 3 == 0)
      {
        result = EC_Normal;
      }
      else
      {
        DCMTRACT_ERROR("Point Coordinates Data must have x,y,z coordinates for every point but has length: " << count);
        result = IOD_EC_InvalidElementValue;
      }
    }
    else
    {
      DCMTRACT_ERROR("Point Coordinates Data empty");
      result = IOD_EC_InvalidElementValue;
    }
  }
  else
  {
    DCMTRACT_ERROR("Point Coordinates Data element missing");
    result = IOD_EC_MissingAttribute;
  }
  return result;
}


OFCondition TrcTrack::read(DcmItem& source,
                           OFBool clearOldData)
{
  OFCondition result = IODComponent::read(source, clearOldData);
  if (result.good())
  {
    // fix number of values in point coordinates data if wrong
    if (!fixPointCoordinatesDataVM())
    {
      return TRC_EC_InvalidPointCoordinatesData;
    }
  }
  return result;
}


OFCondition TrcTrack::write(DcmItem& destination)
{
  return IODComponent::write(destination);
}


void TrcTrack::clearData()
{
  IODComponent::clearData();
}


TrcTrack::~TrcTrack()
{
  clearData();
}


size_t TrcTrack::getTrackData(const Float32*& data) const
{
  data = NULL;
  unsigned long count;
  if (m_Item->findAndGetFloat32Array(DCM_PointCoordinatesData, data, &count).good())
  {
    return count / 3;
  }
  return 0;
}


size_t TrcTrack::getNumDataPoints()
{
  DcmElement *elem = NULL;
  if (m_Item->findAndGetElement(DCM_PointCoordinatesData, elem).good())
  {
    Uint32 numBytes = elem->getLengthField();
    if (numBytes % 2 == 1)
    {
      DCMTRACT_WARN("Length of Point Coordinates data is odd (" << numBytes << "), assuming " << numBytes - 1);
      numBytes = numBytes -1;
    }
    // single point coordinate has 4 bytes (Float32), and point consists of 3 coordinates
    return numBytes / sizeof(Float32) / 3;
  }
  return 0;
}


OFBool TrcTrack::getRecommendedDisplayCIELabValue(Uint16& L,
                                                  Uint16& a,
                                                  Uint16& b)
{
  const Uint16* colors = NULL;
  unsigned long count = 0;
  if (m_Item->findAndGetUint16Array(DCM_RecommendedDisplayCIELabValue, colors, &count).good())
  {
    if (count < 3)
    {
      L = a = b = 0;
      return OFFalse;
    }
    else
    {
      L = colors[0];
      a = colors[1];
      b = colors[2];
      return OFTrue;
    }
  }
  else
  {
    return OFFalse;
  }
}


size_t TrcTrack::getRecommendedDisplayCIELabValueList(const Uint16*& colors)
{
  unsigned long count = 0;
  if (m_Item->findAndGetUint16Array(DCM_RecommendedDisplayCIELabValueList, colors, &count).good())
  {
    return count / 3;
  }
  return 0;
}


TrcTypes::E_TrackColorMode TrcTrack::getRecommendedDisplayCIELabMode()
{
  DcmElement *elem = NULL;
  if (m_Item->findAndGetElement(DCM_RecommendedDisplayCIELabValue, elem).good())
  {
    if (elem->getNumberOfValues() == 3)
    {
      if (m_Item->findAndGetElement(DCM_RecommendedDisplayCIELabValueList, elem).good())
      {
        DCMTRACT_DEBUG("Recommended Display CIE Lab Value List as well as Recommended Display CIE Lab Value are set, will try per track coloring");
      }
      return TrcTypes::CM_TRACK;
    }
    else
    {
      DCMTRACT_ERROR("Recommended Display CIE Lab Value is set but has more or less than 3 values");
      return TrcTypes::CM_ERROR;
    }
  }
  // RecommendedDisplayCIELabValue is not set or not set correctly,
  // try RecommendedDisplayCIELabValueList instead
  elem = NULL;
  if (m_Item->findAndGetElement(DCM_RecommendedDisplayCIELabValueList, elem).good())
  {
    size_t numBytes = elem->getLength();
    if (numBytes % 2 == 1)
    {
      DCMTRACT_WARN("Recommended Display CIE Lab Value List has odd length " << numBytes << ", assuming " << numBytes -1);
      numBytes = numBytes - 1;
    }
    if (numBytes > 0)
    {
      // divide by 2 to get number of 16 bit entries and divide by 3 since
      // every color has 3 components
      if (getNumDataPoints() == (numBytes / 2 / 3))
      {
        return TrcTypes::CM_POINTS;
      }
      else
      {
        DCMTRACT_ERROR("Number of colors in Recommended Display CIE Lab Value List does not match number of points in Point Coordinates Data");
        return TrcTypes::CM_ERROR;
      }
    }
    else
    {
      DCMTRACT_ERROR("Recommended Display CIE Lab Value List is empty and can not be used");
      return TrcTypes::CM_ERROR;
    }
  }

  return TrcTypes::CM_TRACKSET;
}



OFCondition TrcTrack::setTrackData(const Float32* trackDataPoints,
                                   const size_t numPoints)
{
  return m_Item->putAndInsertFloat32Array(DCM_PointCoordinatesData, trackDataPoints, OFstatic_cast(unsigned long, numPoints*3));
}



OFCondition TrcTrack::setRecommendedDisplayCIELabValues(const Uint16* colors,
                                                        const size_t numColors)
{
  OFCondition result;
  if ( (numColors == 0) && (colors == NULL))
  {
    m_Item->findAndDeleteElement(DCM_RecommendedDisplayCIELabValue);
    m_Item->findAndDeleteElement(DCM_RecommendedDisplayCIELabValueList);
    return EC_Normal;
  }
  else if (colors == NULL)
  {
    return TRC_EC_InvalidColorInformation;
  }
  if (numColors == 1)
  {
    return m_Item->putAndInsertUint16Array(DCM_RecommendedDisplayCIELabValue, colors, OFstatic_cast(unsigned long, numColors * 3));
  }
  else
  {
    return m_Item->putAndInsertUint16Array(DCM_RecommendedDisplayCIELabValueList, colors, OFstatic_cast(unsigned long, numColors * 3));
  }
}



OFBool TrcTrack::fixPointCoordinatesDataVM()
{
  const Float32* values = NULL;
  unsigned long numValues = 0;
  if (m_Item->findAndGetFloat32Array(DCM_PointCoordinatesData, values, &numValues).good())
  {
    if (numValues % 3 == 0)
    {
      return OFTrue;
    }
    else
    {
      if (m_Item->putAndInsertFloat32Array(DCM_PointCoordinatesData, values, numValues - (numValues % 3)).good())
      {
        DCMTRACT_WARN("Wrong number of values in track (" << numValues << "), cutting off " << numValues % 3 << " coordinates at the end");
        return OFTrue;
      }
    }
  } // else error
  return OFFalse;
}