Fraxinus  18.10
An IGT application
cxTransferFunctions3DPresets.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 
14 
15 #include <iostream>
16 #include <vtkColorTransferFunction.h>
17 #include <QStringList>
18 #include <QDomElement>
19 #include <QDomDocument>
20 #include <QColor>
21 #include "cxTypeConversions.h"
22 #include "cxXmlOptionItem.h"
23 #include "cxImageTF3D.h"
24 #include "cxImageLUT2D.h"
25 #include "cxImageTFData.h"
26 
27 namespace cx
28 {
29 
31  Presets(presetFile, customFile)
32 {
33 }
34 
35 void TransferFunctions3DPresets::save(QString name, ImagePtr image, bool _2D, bool _3D)
36 {
37  if (_2D)
38  this->save2D(name, image);
39  if (_3D)
40  this->save3D(name, image);
41 }
42 
44 {
45  XmlOptionFile file = this->getCustomFile();
46  file = file.descend("Preset", "name", name);
47 
48  QDomNode tf2DNode = file.getElement("lookuptable2D");
49  while (tf2DNode.hasChildNodes())
50  tf2DNode.removeChild(tf2DNode.firstChild());
51 
52  ImageLUT2DPtr LUT2D = image->getLookupTable2D();
53 
54  // For unsigned CT: Modify transfer function values temporarily prior to save
55  if ((0 <= image->getMin()) && ("CT" == image->getModality()))
56  {
57  LUT2D->unsignedCT(false);
58  }
59 
60  LUT2D->addXml(file.getElement("lookuptable2D"));
61 
62  // Revert the transfer function values back again
63  if ((0 <= image->getMin()) && ("CT" == image->getModality()))
64  {
65  LUT2D->unsignedCT(true);
66  }
67 
68  file.getElement().setAttribute("modality", image->getModality());
69  file.save();
70 
71 // emit changed();
72 }
73 
75 {
76  //create the node to be saved
77  XmlOptionFile file = this->getCustomFile();
78  file = file.descend("Preset", "name", name);
79 
80  QDomNode tf3DNode = file.getElement("transferfunctions");
81  while (tf3DNode.hasChildNodes())
82  tf3DNode.removeChild(tf3DNode.firstChild());
83 
84  ImageTF3DPtr transferFunctions = image->getTransferFunctions3D();
85 
86  // For unsigned CT: Modify transfer function values temporarily prior to save
87  if ((0 <= image->getMin()) && ("CT" == image->getModality()))
88  {
89  transferFunctions->unsignedCT(false);
90  }
91 
92  transferFunctions->addXml(file.getElement("transferfunctions"));
93  image->getShading().addXml(file.getElement("shading"));
94 
95  // Revert the transfer function values back again
96  if ((0 <= image->getMin()) && ("CT" == image->getModality()))
97  {
98  transferFunctions->unsignedCT(true);
99  }
100 
101  file.getElement().setAttribute("modality", image->getModality());
102  file.save();
103 }
104 
105 void TransferFunctions3DPresets::load(QString name, ImagePtr image, bool _2D, bool _3D)
106 {
107  if (_2D)
108  this->load2D(name, image);
109  if (_3D)
110  this->load3D(name, image);
111 }
112 
114 {
115  if(!image)
116  return;
117 
118  //Make sure transfer functions are reset in case something is missing from the preset
119  image->resetTransferFunctions(true, false);
120 
121  ImageLUT2DPtr LUT2D = image->getLookupTable2D();
122  XmlOptionFile node = this->getPresetNode(name);
123 
124  LUT2D->parseXml(node.getElement().namedItem("lookuptable2D"));
125 
126  // Transfer functions for CT data are signed, so these have to be converted if they are to be used for unsigned CT
127  if ((0 <= image->getMin()) && ("CT" == image->getModality()) && (name != "Transfer function preset...") )
128  {
129  LUT2D->unsignedCT(true);
130  }
131 }
132 
134 {
135  //Make sure transfer functions are reset in case something is missing from the preset
136  image->resetTransferFunctions(false, true);
137 
138  ImageTF3DPtr transferFunctions = image->getTransferFunctions3D();
139  XmlOptionFile node = this->getPresetNode(name);
140 
141  transferFunctions->parseXml(node.getElement().namedItem("transferfunctions"));
142 
143  Image::ShadingStruct shading = image->getShading();
144  shading.parseXml(node.getElement().namedItem("shading"));
145  image->setShading(shading);
146 
147  // Transfer functions for CT data are signed, so these have to be converted if they are to be used for unsigned CT
148  if ((0 <= image->getMin()) && ("CT" == image->getModality()) && (name != "Transfer function preset...") )
149  {
150  transferFunctions->unsignedCT(true);
151  }
152 }
153 
155 {
156  QStringList presetList;
157 
158  QDomNodeList presetNodeList = mPresetFile.getElement().elementsByTagName("Preset");
159  for (int i = 0; i < presetNodeList.count(); ++i)
160  {
161  QString presetName = presetNodeList.item(i).toElement().attribute("name");
162  if (presetName == "Default")
163  continue;
164  else
165  {
166  QString sourceModality = presetNodeList.item(i).toElement().attribute("modality");
167  if ( (modality == sourceModality) || ("UNKNOWN" == modality) || modality.isEmpty() )
168  presetList << presetName;
169  }
170  }
171 
172  XmlOptionFile customFile = this->getCustomFile();
173  presetNodeList = customFile.getElement().elementsByTagName("Preset");
174  for (int i = 0; i < presetNodeList.count(); ++i)
175  {
176  QString presetName = presetNodeList.item(i).toElement().attribute("name");
177  QString presetModality = presetNodeList.item(i).toElement().attribute("modality");
178  if ( (presetModality == modality) || ("UNKNOWN" == modality) || modality.isEmpty() )
179  presetList << presetName;
180  }
181 
182  return presetList;
183 }
184 
185 void TransferFunctions3DPresets::deletePresetData(QString name, bool _2D, bool _3D)
186 {
187  //todo rewrite
188  std::cout << "TODO rewrite TransferFunctions3DPresets::deletePresetData(QString name, bool _2D, bool _3D)" << std::endl;
189  XmlOptionFile node = this->getPresetNode(name);
190 
191  if (_2D)
192  node.descend("lookuptable2D").deleteNode();
193  if (_3D)
194  node.descend("transferfunctions").deleteNode();
195  if (_2D && _3D)
196  node.deleteNode();
197 
198  emit changed();
199 }
200 
201 }
void load2D(QString name, ImagePtr image)
void load(QString name, ImagePtr image, bool _2D=true, bool _3D=true)
void parseXml(QDomNode dataNode)
Definition: cxImage.cpp:80
Base class for a group of presets in the system.
Definition: cxPresets.h:40
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
void load3D(QString name, ImagePtr image)
virtual void save()
saves the presets to file
Definition: cxPresets.cpp:43
QStringList generatePresetList(QString modality)
internally generate the preset list
QDomElement getElement()
return the current element
void save3D(QString name, ImagePtr image)
XmlOptionFile mPresetFile
< the name of the last custom preset removed
Definition: cxPresets.h:74
void deletePresetData(QString name, bool _2D=true, bool _3D=true)
Delete the preset data node.
XmlOptionFile getCustomFile()
Definition: cxPresets.cpp:73
TransferFunctions3DPresets(XmlOptionFile presetFile, XmlOptionFile customFile)
boost::shared_ptr< class ImageLUT2D > ImageLUT2DPtr
void save2D(QString name, ImagePtr image)
void changed()
void deleteNode()
Delete the current node.
void save()
save entire document.
XmlOptionFile getPresetNode(const QString &presetName)
Look for a preset with the given name. Create one if not found.
Definition: cxPresets.cpp:78
boost::shared_ptr< class ImageTF3D > ImageTF3DPtr
Helper class for xml files used to store ssc/cx data.
XmlOptionFile descend(QString element) const
step one level down in the xml tree
Namespace for all CustusX production code.