CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 
35 
36 #include <iostream>
37 #include <vtkColorTransferFunction.h>
38 #include <QStringList>
39 #include <QDomElement>
40 #include <QDomDocument>
41 #include <QColor>
42 #include "cxTypeConversions.h"
43 #include "cxXmlOptionItem.h"
44 #include "cxImageTF3D.h"
45 #include "cxImageLUT2D.h"
46 #include "cxImageTFData.h"
47 
48 namespace cx
49 {
50 
52  Presets(presetFile, customFile)
53 {
54 }
55 
56 void TransferFunctions3DPresets::save(QString name, ImagePtr image, bool _2D, bool _3D)
57 {
58  if (_2D)
59  this->save2D(name, image);
60  if (_3D)
61  this->save3D(name, image);
62 }
63 
65 {
66  XmlOptionFile file = this->getCustomFile();
67  file = file.descend("Preset", "name", name);
68 
69  QDomNode tf2DNode = file.getElement("lookuptable2D");
70  while (tf2DNode.hasChildNodes())
71  tf2DNode.removeChild(tf2DNode.firstChild());
72 
73  ImageLUT2DPtr LUT2D = image->getLookupTable2D();
74 
75  // For unsigned CT: Modify transfer function values temporarily prior to save
76  if ((0 <= image->getMin()) && ("CT" == image->getModality()))
77  {
78  LUT2D->unsignedCT(false);
79  }
80 
81  LUT2D->addXml(file.getElement("lookuptable2D"));
82 
83  // Revert the transfer function values back again
84  if ((0 <= image->getMin()) && ("CT" == image->getModality()))
85  {
86  LUT2D->unsignedCT(true);
87  }
88 
89  file.getElement().setAttribute("modality", image->getModality());
90  file.save();
91 
92 // emit changed();
93 }
94 
96 {
97  //create the node to be saved
98  XmlOptionFile file = this->getCustomFile();
99  file = file.descend("Preset", "name", name);
100 
101  QDomNode tf3DNode = file.getElement("transferfunctions");
102  while (tf3DNode.hasChildNodes())
103  tf3DNode.removeChild(tf3DNode.firstChild());
104 
105  ImageTF3DPtr transferFunctions = image->getTransferFunctions3D();
106 
107  // For unsigned CT: Modify transfer function values temporarily prior to save
108  if ((0 <= image->getMin()) && ("CT" == image->getModality()))
109  {
110  transferFunctions->unsignedCT(false);
111  }
112 
113  transferFunctions->addXml(file.getElement("transferfunctions"));
114  image->getShading().addXml(file.getElement("shading"));
115 
116  // Revert the transfer function values back again
117  if ((0 <= image->getMin()) && ("CT" == image->getModality()))
118  {
119  transferFunctions->unsignedCT(true);
120  }
121 
122  file.getElement().setAttribute("modality", image->getModality());
123  file.save();
124 }
125 
126 void TransferFunctions3DPresets::load(QString name, ImagePtr image, bool _2D, bool _3D)
127 {
128  if (_2D)
129  this->load2D(name, image);
130  if (_3D)
131  this->load3D(name, image);
132 }
133 
135 {
136  //Make sure transfer functions are reset in case something is missing from the preset
137  image->resetTransferFunctions(true, false);
138 
139  ImageLUT2DPtr LUT2D = image->getLookupTable2D();
140  XmlOptionFile node = this->getPresetNode(name);
141 
142  LUT2D->parseXml(node.getElement().namedItem("lookuptable2D"));
143 
144  // Transfer functions for CT data are signed, so these have to be converted if they are to be used for unsigned CT
145  if ((0 <= image->getMin()) && ("CT" == image->getModality()) && (name != "Transfer function preset...") )
146  {
147  LUT2D->unsignedCT(true);
148  }
149 }
150 
152 {
153  //Make sure transfer functions are reset in case something is missing from the preset
154  image->resetTransferFunctions(false, true);
155 
156  ImageTF3DPtr transferFunctions = image->getTransferFunctions3D();
157  XmlOptionFile node = this->getPresetNode(name);
158 
159  transferFunctions->parseXml(node.getElement().namedItem("transferfunctions"));
160 
161  Image::ShadingStruct shading = image->getShading();
162  shading.parseXml(node.getElement().namedItem("shading"));
163  image->setShading(shading);
164 
165  // Transfer functions for CT data are signed, so these have to be converted if they are to be used for unsigned CT
166  if ((0 <= image->getMin()) && ("CT" == image->getModality()) && (name != "Transfer function preset...") )
167  {
168  transferFunctions->unsignedCT(true);
169  }
170 }
171 
173 {
174  QStringList presetList;
175 
176  QDomNodeList presetNodeList = mPresetFile.getElement().elementsByTagName("Preset");
177  for (int i = 0; i < presetNodeList.count(); ++i)
178  {
179  QString presetName = presetNodeList.item(i).toElement().attribute("name");
180  if (presetName == "Default")
181  continue;
182  else
183  {
184  QString sourceModality = presetNodeList.item(i).toElement().attribute("modality");
185  if ( (modality == sourceModality) || ("UNKNOWN" == modality) || modality.isEmpty() )
186  presetList << presetName;
187  }
188  }
189 
190  XmlOptionFile customFile = this->getCustomFile();
191  presetNodeList = customFile.getElement().elementsByTagName("Preset");
192  for (int i = 0; i < presetNodeList.count(); ++i)
193  {
194  QString presetName = presetNodeList.item(i).toElement().attribute("name");
195  QString presetModality = presetNodeList.item(i).toElement().attribute("modality");
196  if ( (presetModality == modality) || ("UNKNOWN" == modality) || modality.isEmpty() )
197  presetList << presetName;
198  }
199 
200  return presetList;
201 }
202 
203 void TransferFunctions3DPresets::deletePresetData(QString name, bool _2D, bool _3D)
204 {
205  //todo rewrite
206  std::cout << "TODO rewrite TransferFunctions3DPresets::deletePresetData(QString name, bool _2D, bool _3D)" << std::endl;
207  XmlOptionFile node = this->getPresetNode(name);
208 
209  if (_2D)
210  node.descend("lookuptable2D").deleteNode();
211  if (_3D)
212  node.descend("transferfunctions").deleteNode();
213  if (_2D && _3D)
214  node.deleteNode();
215 
216  emit changed();
217 }
218 
219 }
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:101
Base class for a group of presets in the system.
Definition: cxPresets.h:61
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
void load3D(QString name, ImagePtr image)
virtual void save()
saves the presets to file
Definition: cxPresets.cpp:64
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:95
void deletePresetData(QString name, bool _2D=true, bool _3D=true)
Delete the preset data node.
XmlOptionFile getCustomFile()
Definition: cxPresets.cpp:94
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:99
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