Fraxinus  17.12
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) 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  if(!image)
137  return;
138 
139  //Make sure transfer functions are reset in case something is missing from the preset
140  image->resetTransferFunctions(true, false);
141 
142  ImageLUT2DPtr LUT2D = image->getLookupTable2D();
143  XmlOptionFile node = this->getPresetNode(name);
144 
145  LUT2D->parseXml(node.getElement().namedItem("lookuptable2D"));
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  LUT2D->unsignedCT(true);
151  }
152 }
153 
155 {
156  //Make sure transfer functions are reset in case something is missing from the preset
157  image->resetTransferFunctions(false, true);
158 
159  ImageTF3DPtr transferFunctions = image->getTransferFunctions3D();
160  XmlOptionFile node = this->getPresetNode(name);
161 
162  transferFunctions->parseXml(node.getElement().namedItem("transferfunctions"));
163 
164  Image::ShadingStruct shading = image->getShading();
165  shading.parseXml(node.getElement().namedItem("shading"));
166  image->setShading(shading);
167 
168  // Transfer functions for CT data are signed, so these have to be converted if they are to be used for unsigned CT
169  if ((0 <= image->getMin()) && ("CT" == image->getModality()) && (name != "Transfer function preset...") )
170  {
171  transferFunctions->unsignedCT(true);
172  }
173 }
174 
176 {
177  QStringList presetList;
178 
179  QDomNodeList presetNodeList = mPresetFile.getElement().elementsByTagName("Preset");
180  for (int i = 0; i < presetNodeList.count(); ++i)
181  {
182  QString presetName = presetNodeList.item(i).toElement().attribute("name");
183  if (presetName == "Default")
184  continue;
185  else
186  {
187  QString sourceModality = presetNodeList.item(i).toElement().attribute("modality");
188  if ( (modality == sourceModality) || ("UNKNOWN" == modality) || modality.isEmpty() )
189  presetList << presetName;
190  }
191  }
192 
193  XmlOptionFile customFile = this->getCustomFile();
194  presetNodeList = customFile.getElement().elementsByTagName("Preset");
195  for (int i = 0; i < presetNodeList.count(); ++i)
196  {
197  QString presetName = presetNodeList.item(i).toElement().attribute("name");
198  QString presetModality = presetNodeList.item(i).toElement().attribute("modality");
199  if ( (presetModality == modality) || ("UNKNOWN" == modality) || modality.isEmpty() )
200  presetList << presetName;
201  }
202 
203  return presetList;
204 }
205 
206 void TransferFunctions3DPresets::deletePresetData(QString name, bool _2D, bool _3D)
207 {
208  //todo rewrite
209  std::cout << "TODO rewrite TransferFunctions3DPresets::deletePresetData(QString name, bool _2D, bool _3D)" << std::endl;
210  XmlOptionFile node = this->getPresetNode(name);
211 
212  if (_2D)
213  node.descend("lookuptable2D").deleteNode();
214  if (_3D)
215  node.descend("transferfunctions").deleteNode();
216  if (_2D && _3D)
217  node.deleteNode();
218 
219  emit changed();
220 }
221 
222 }
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
Namespace for all CustusX production code.